ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

inputdialog.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/system.h"
00024 
00025 #include "gui/gui-manager.h"
00026 #include "gui/ThemeEval.h"
00027 #include "gui/widget.h"
00028 #include "gui/widgets/edittext.h"
00029 
00030 #include "engines/grim/inputdialog.h"
00031 
00032 namespace Grim {
00033 
00034 InputDialog::InputDialog(const Common::String &message, const Common::String &string, bool hasTextField) :
00035         GUI::Dialog(30, 20, 260, 124), _hasTextField(hasTextField),
00036         m_text(nullptr) {
00037 
00038     const int screenW = g_system->getOverlayWidth();
00039     const int screenH = g_system->getOverlayHeight();
00040 
00041     int buttonWidth = g_gui.xmlEval()->getVar("Globals.Button.Width", 0);
00042     int buttonHeight = g_gui.xmlEval()->getVar("Globals.Button.Height", 0);
00043 
00044     // First, determine the size the dialog needs. For this we have to break
00045     // down the string into lines, and taking the maximum of their widths.
00046     // Using this, and accounting for the space the button(s) need, we can set
00047     // the real size of the dialog
00048     Common::Array<Common::String> lines;
00049     int lineCount;
00050     int maxlineWidth = g_gui.getFont().wordWrapText(message, screenW - 2 * 20, lines);
00051 
00052     // Calculate the desired dialog size (maxing out at 300*180 for now)
00053     _w = MAX(maxlineWidth, (2 * buttonWidth) + 10) + 20;
00054 
00055     lineCount = lines.size();
00056 
00057     _h = 30 + kLineHeight;
00058     _h += buttonHeight + 10;
00059 
00060     // Limit the number of lines so that the dialog still fits on the screen.
00061     if (lineCount > (screenH - 20 - _h) / kLineHeight) {
00062         lineCount = (screenH - 20 - _h) / kLineHeight;
00063     }
00064     _h += lineCount * kLineHeight;
00065 
00066     // Center the dialog
00067     _x = (screenW - _w) / 2;
00068     _y = (screenH - _h) / 2;
00069 
00070     // Each line is represented by one static text item.
00071     int height = 10;
00072     for (int i = 0; i < lineCount; i++) {
00073         new GUI::StaticTextWidget(this, 10, height, maxlineWidth, kLineHeight,
00074                                   lines[i], Graphics::kTextAlignCenter);
00075         height += kLineHeight;
00076     }
00077     height += 10;
00078     if (_hasTextField) {
00079         m_text = new GUI::EditTextWidget(this, 10, height, _w - 20, kLineHeight, string, "input");
00080         height += kLineHeight + 10;
00081     }
00082 
00083     new GUI::ButtonWidget(this, 10, height, buttonWidth, buttonHeight, "Ok", nullptr, GUI::kOKCmd, Common::ASCII_RETURN); // Confirm dialog
00084     new GUI::ButtonWidget(this, _w - buttonWidth - 10, height, buttonWidth, buttonHeight, "Cancel", nullptr, GUI::kCloseCmd, Common::ASCII_ESCAPE);   // Cancel dialog
00085 }
00086 
00087 const Common::String &InputDialog::getString() const {
00088     if (_hasTextField) {
00089         return m_text->getEditString();
00090     } else {
00091         // TextBox-less dialogs shouldn't need any getString, but
00092         // this is needed for safety and to avoid warnings.
00093         return _name;
00094     }
00095 
00096 }
00097 
00098 void InputDialog::handleKeyDown(Common::KeyState state) {
00099     if (state.keycode == Common::KEYCODE_RETURN) {
00100         setResult(1);
00101         close();
00102     } else if (state.keycode == Common::KEYCODE_ESCAPE) {
00103         setResult(0);
00104         close();
00105     } else {
00106         GUI::Dialog::handleKeyDown(state);
00107     }
00108 }
00109 
00110 void InputDialog::handleCommand(GUI::CommandSender *sender, uint32 cmd, uint32 data) {
00111     if (cmd == GUI::kOKCmd) {
00112         setResult(1);
00113         close();
00114     } else if (cmd == GUI::kCloseCmd) {
00115         setResult(0);
00116         close();
00117     } else {
00118         GUI::Dialog::handleCommand(sender, cmd, data);
00119     }
00120 }
00121 
00122 }


Generated on Sat May 25 2019 05:00:47 for ResidualVM by doxygen 1.7.1
curved edge   curved edge