--- /dev/null
+/*
+Minetest-c55
+Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "guiTextInputMenu.h"
+#include "debug.h"
+#include "serialization.h"
+#include <string>
+
+GUITextInputMenu::GUITextInputMenu(gui::IGUIEnvironment* env,
+ gui::IGUIElement* parent, s32 id,
+ int *active_menu_count,
+ TextDest *dest,
+ std::wstring initial_text
+):
+ GUIModalMenu(env, parent, id, active_menu_count),
+ m_dest(dest),
+ m_initial_text(initial_text)
+{
+}
+
+GUITextInputMenu::~GUITextInputMenu()
+{
+ removeChildren();
+}
+
+void GUITextInputMenu::removeChildren()
+{
+ {
+ gui::IGUIElement *e = getElementFromId(256);
+ if(e != NULL)
+ e->remove();
+ }
+ {
+ gui::IGUIElement *e = getElementFromId(257);
+ if(e != NULL)
+ e->remove();
+ }
+}
+
+void GUITextInputMenu::regenerateGui(v2u32 screensize)
+{
+ std::wstring text;
+
+ {
+ gui::IGUIElement *e = getElementFromId(256);
+ if(e != NULL)
+ {
+ text = e->getText();
+ }
+ else
+ {
+ text = m_initial_text;
+ m_initial_text = L"";
+ }
+ }
+
+ /*
+ Remove stuff
+ */
+ removeChildren();
+
+ /*
+ Calculate new sizes and positions
+ */
+ core::rect<s32> rect(
+ screensize.X/2 - 580/2,
+ screensize.Y/2 - 300/2,
+ screensize.X/2 + 580/2,
+ screensize.Y/2 + 300/2
+ );
+
+ DesiredRect = rect;
+ recalculateAbsolutePosition(false);
+
+ v2s32 size = rect.getSize();
+
+ /*
+ Add stuff
+ */
+ {
+ core::rect<s32> rect(0, 0, 300, 30);
+ rect = rect + v2s32(size.X/2-300/2, size.Y/2-30/2-25);
+ gui::IGUIElement *e =
+ Environment->addEditBox(text.c_str(), rect, true, this, 256);
+ Environment->setFocus(e);
+ }
+ {
+ core::rect<s32> rect(0, 0, 140, 30);
+ rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
+ Environment->addButton(rect, this, 257, L"Proceed");
+ }
+}
+
+void GUITextInputMenu::drawMenu()
+{
+ gui::IGUISkin* skin = Environment->getSkin();
+ if (!skin)
+ return;
+ video::IVideoDriver* driver = Environment->getVideoDriver();
+
+ video::SColor bgcolor(140,0,0,0);
+ driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
+
+ gui::IGUIElement::draw();
+}
+
+bool GUITextInputMenu::OnEvent(const SEvent& event)
+{
+ if(event.EventType==EET_KEY_INPUT_EVENT)
+ {
+ if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
+ {
+ quitMenu();
+ return true;
+ }
+ }
+ if(event.EventType==EET_GUI_EVENT)
+ {
+ if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
+ && isVisible())
+ {
+ if(!canTakeFocus(event.GUIEvent.Element))
+ {
+ dstream<<"GUITextInputMenu: Not allowing focus change."
+ <<std::endl;
+ // Returning true disables focus change
+ return true;
+ }
+ }
+ if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
+ {
+ switch(event.GUIEvent.Caller->getID())
+ {
+ case 257:
+ if(m_dest)
+ {
+ gui::IGUIElement *e = getElementFromId(256);
+ if(e != NULL)
+ {
+ m_dest->gotText(e->getText());
+ }
+ delete m_dest;
+ }
+ quitMenu();
+ break;
+ }
+ }
+ }
+
+ return Parent ? Parent->OnEvent(event) : false;
+}
+
#include "constants.h"\r
#include "strfnd.h"\r
#include "porting.h"\r
-#include "guiPauseMenu.h"\r
#include "irrlichtwrapper.h"\r
#include "gettime.h"\r
#include "porting.h"\r
+#include "guiPauseMenu.h"\r
#include "guiInventoryMenu.h"\r
+#include "guiTextInputMenu.h"\r
\r
IrrlichtWrapper *g_irrlicht;\r
\r
// This is a copy of the inventory that the client's environment has\r
Inventory local_inventory;\r
\r
-std::wstring g_text_buffer;\r
-bool g_text_buffer_accepted = false;\r
-\r
-// When true, the mouse and keyboard are grabbed\r
-bool g_game_focused = true;\r
-\r
/*\r
Debug streams\r
*/\r
// This is the one method that we have to implement\r
virtual bool OnEvent(const SEvent& event)\r
{\r
+ /*\r
+ React to nothing here if a menu is active\r
+ */\r
+ if(noMenuActive() == false)\r
+ {\r
+ clearInput();\r
+ return false;\r
+ }\r
+\r
// Remember whether each key is down or up\r
if(event.EventType == irr::EET_KEY_INPUT_EVENT)\r
{\r
if(event.KeyInput.PressedDown)\r
{\r
//dstream<<"Pressed key: "<<(char)event.KeyInput.Key<<std::endl;\r
- if(g_game_focused == false)\r
- {\r
- s16 key = event.KeyInput.Key;\r
- if(key == irr::KEY_RETURN || key == irr::KEY_ESCAPE)\r
- {\r
- g_text_buffer_accepted = true;\r
- }\r
- else if(key == irr::KEY_BACK)\r
- {\r
- if(g_text_buffer.size() > 0)\r
- g_text_buffer = g_text_buffer.substr\r
- (0, g_text_buffer.size()-1);\r
- }\r
- else\r
- {\r
- wchar_t wc = event.KeyInput.Char;\r
- if(wc != 0)\r
- g_text_buffer += wc;\r
- }\r
- }\r
\r
- //if(pauseMenu != NULL)\r
+ /*\r
+ Launch menus\r
+ */\r
+\r
if(guienv != NULL && guiroot != NULL && g_device != NULL)\r
{\r
if(event.KeyInput.Key == irr::KEY_ESCAPE)\r
{\r
- if(g_game_focused == true && noMenuActive())\r
- {\r
- dstream<<DTIME<<"MyEventReceiver: "\r
- <<"Launching pause menu"<<std::endl;\r
- // It will delete itself by itself\r
- GUIPauseMenu *menu = new\r
- GUIPauseMenu(guienv, guiroot, -1, g_device,\r
- &g_active_menu_count);\r
- menu->drop();\r
- return true;\r
- }\r
+ dstream<<DTIME<<"MyEventReceiver: "\r
+ <<"Launching pause menu"<<std::endl;\r
+ // It will delete itself by itself\r
+ (new GUIPauseMenu(guienv, guiroot, -1, g_device,\r
+ &g_active_menu_count))->drop();\r
+ return true;\r
}\r
- }\r
-\r
- //if(inventoryMenu != NULL)\r
- if(guienv != NULL && guiroot != NULL && g_device != NULL)\r
- {\r
if(event.KeyInput.Key == irr::KEY_KEY_I)\r
{\r
- if(g_game_focused == true && noMenuActive())\r
- {\r
- dstream<<DTIME<<"MyEventReceiver: "\r
- <<"Launching inventory"<<std::endl;\r
- GUIInventoryMenu *inventoryMenu = new\r
- GUIInventoryMenu(guienv, guiroot, -1,\r
- &local_inventory, &inventory_action_queue,\r
- &g_active_menu_count);\r
- inventoryMenu->drop();\r
- return true;\r
- }\r
+ dstream<<DTIME<<"MyEventReceiver: "\r
+ <<"Launching inventory"<<std::endl;\r
+ (new GUIInventoryMenu(guienv, guiroot, -1,\r
+ &local_inventory, &inventory_action_queue,\r
+ &g_active_menu_count))->drop();\r
+ return true;\r
}\r
}\r
\r
// Material selection\r
if(event.KeyInput.Key == irr::KEY_KEY_F)\r
{\r
- if(g_game_focused == true)\r
- {\r
- if(g_selected_item < PLAYER_INVENTORY_SIZE-1)\r
- g_selected_item++;\r
- else\r
- g_selected_item = 0;\r
- dstream<<DTIME<<"Selected item: "\r
- <<g_selected_item<<std::endl;\r
- }\r
+ if(g_selected_item < PLAYER_INVENTORY_SIZE-1)\r
+ g_selected_item++;\r
+ else\r
+ g_selected_item = 0;\r
+ dstream<<DTIME<<"Selected item: "\r
+ <<g_selected_item<<std::endl;\r
}\r
\r
// Viewing range selection\r
- if(event.KeyInput.Key == irr::KEY_KEY_R\r
- && g_game_focused)\r
+ if(event.KeyInput.Key == irr::KEY_KEY_R)\r
{\r
JMutexAutoLock lock(g_range_mutex);\r
if(g_viewing_range_all)\r
}\r
\r
// Print debug stacks\r
- if(event.KeyInput.Key == irr::KEY_KEY_P\r
- && g_game_focused)\r
+ if(event.KeyInput.Key == irr::KEY_KEY_P)\r
{\r
dstream<<"-----------------------------------------"\r
<<std::endl;\r
return keyIsDown[keyCode];\r
}\r
\r
- MyEventReceiver()\r
+ void clearInput()\r
{\r
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)\r
keyIsDown[i] = false;\r
+ \r
leftclicked = false;\r
rightclicked = false;\r
leftreleased = false;\r
right_active = false;\r
}\r
\r
+ MyEventReceiver()\r
+ {\r
+ clearInput();\r
+ }\r
+\r
bool leftclicked;\r
bool rightclicked;\r
bool leftreleased;\r
\r
InputHandler *g_input = NULL;\r
\r
-void focusGame()\r
-{\r
- g_input->clear();\r
- g_game_focused = true;\r
-}\r
-\r
-void unFocusGame()\r
-{\r
- g_game_focused = false;\r
-}\r
-\r
class RealInputHandler : public InputHandler\r
{\r
public:\r
\r
virtual bool getLeftClicked()\r
{\r
- if(g_game_focused == false)\r
- return false;\r
return m_receiver->leftclicked;\r
}\r
virtual bool getRightClicked()\r
{\r
- if(g_game_focused == false)\r
- return false;\r
return m_receiver->rightclicked;\r
}\r
virtual void resetLeftClicked()\r
\r
virtual bool getLeftReleased()\r
{\r
- if(g_game_focused == false)\r
- return false;\r
return m_receiver->leftreleased;\r
}\r
virtual bool getRightReleased()\r
{\r
- if(g_game_focused == false)\r
- return false;\r
return m_receiver->rightreleased;\r
}\r
virtual void resetLeftReleased()\r
Text input system\r
*/\r
\r
-struct TextDest\r
-{\r
- virtual void sendText(std::string text) = 0;\r
-};\r
-\r
struct TextDestSign : public TextDest\r
{\r
TextDestSign(v3s16 blockpos, s16 id, Client *client)\r
m_id = id;\r
m_client = client;\r
}\r
- void sendText(std::string text)\r
+ void gotText(std::wstring text)\r
{\r
+ std::string ntext = wide_to_narrow(text);\r
dstream<<"Changing text of a sign object: "\r
- <<text<<std::endl;\r
- m_client->sendSignText(m_blockpos, m_id, text);\r
+ <<ntext<<std::endl;\r
+ m_client->sendSignText(m_blockpos, m_id, ntext);\r
}\r
\r
v3s16 m_blockpos;\r
Client *m_client;\r
};\r
\r
-struct TextInput\r
-{\r
- TextDest *dest;\r
- gui::IGUIStaticText* guitext;\r
- /*std::wstring buffer;\r
- bool buffer_accepted;*/\r
-\r
- TextInput()\r
- {\r
- dest = NULL;\r
- guitext = NULL;\r
- //buffer_accepted = false;\r
- }\r
-\r
- void start(TextDest *a_dest)\r
- {\r
- unFocusGame();\r
-\r
- guitext = guienv->addStaticText(L"",\r
- core::rect<s32>(150,100,550,120),\r
- true, // border?\r
- false, // wordwrap?\r
- NULL);\r
-\r
- guitext->setDrawBackground(true);\r
-\r
- g_text_buffer = L"";\r
- g_text_buffer_accepted = false;\r
-\r
- dest = a_dest;\r
- }\r
-};\r
-\r
int main(int argc, char *argv[])\r
{\r
/*\r
{\r
snprintf(connect_name, 100, "%s", cmd_args.get("address").c_str());\r
}\r
- else if(g_settings.get("address") != "" && is_yes(g_settings.get("host_game")) == false)\r
- {\r
- std::cout<<g_settings.get("address")<<std::endl;\r
- snprintf(connect_name, 100, "%s", g_settings.get("address").c_str());\r
- }\r
- else\r
+ else if(is_yes(g_settings.get("host_game")) == false)\r
{\r
- std::cout<<"Address to connect to [empty = host a game]: ";\r
- std::cin.getline(connect_name, 100);\r
+ if(g_settings.get("address") != "")\r
+ {\r
+ std::cout<<g_settings.get("address")<<std::endl;\r
+ snprintf(connect_name, 100, "%s", g_settings.get("address").c_str());\r
+ }\r
+ else\r
+ {\r
+ std::cout<<"Address to connect to [empty = host a game]: ";\r
+ std::cin.getline(connect_name, 100);\r
+ }\r
}\r
\r
if(connect_name[0] == 0){\r
Add some gui stuff\r
*/\r
\r
- // Text input system\r
- TextInput text_input;\r
- \r
GUIQuickInventory *quick_inventory = new GUIQuickInventory\r
(guienv, NULL, v2s32(10, 70), 5, &local_inventory);\r
\r
guiroot = guienv->addStaticText(L"",\r
core::rect<s32>(0, 0, 10000, 10000));\r
\r
- // Pause menu\r
- //pauseMenu = new GUIPauseMenu(guienv, root, -1, device);\r
- \r
- // Inventory menu\r
- /*inventoryMenu = new GUIInventoryMenu(guienv, guiroot, -1, &local_inventory,\r
- &inventory_action_queue);*/\r
-\r
- //pauseMenu->launch();\r
- //inventoryMenu->launch();\r
+ // Test the text input system\r
+ /*(new GUITextInputMenu(guienv, guiroot, -1, &g_active_menu_count,\r
+ NULL))->drop();*/\r
\r
// First line of debug text\r
gui::IGUIStaticText *guitext = guienv->addStaticText(\r
v2u32 screensize = driver->getScreenSize();\r
core::vector2d<s32> displaycenter(screensize.X/2,screensize.Y/2);\r
\r
- /*pauseMenu->resizeGui();\r
- inventoryMenu->resizeGui();*/\r
-\r
// Hilight boxes collected during the loop and displayed\r
core::list< core::aabbox3d<f32> > hilightboxes;\r
\r
Viewing range\r
*/\r
\r
- //updateViewingRange(dtime, &client);\r
updateViewingRange(busytime, &client);\r
\r
/*\r
*/\r
g_input->step(dtime);\r
\r
- /*\r
- Special keys\r
- */\r
- /*if(g_esc_pressed)\r
- {\r
- break;\r
- }*/\r
- /*if(g_i_pressed)\r
- {\r
- inventoryMenu->setVisible(true);\r
- g_i_pressed = false;\r
- }*/\r
-\r
/*\r
Player speed control\r
*/\r
\r
- if(g_game_focused)\r
{\r
/*bool a_up,\r
bool a_down,\r
);\r
client.setPlayerControl(control);\r
}\r
- else\r
- {\r
- // Set every key to inactive\r
- PlayerControl control;\r
- client.setPlayerControl(control);\r
- }\r
\r
- //timer1.stop();\r
/*\r
Process environment\r
*/\r
Mouse and camera control\r
*/\r
\r
- if((device->isWindowActive()\r
- && g_game_focused\r
- && noMenuActive()\r
- )\r
- || random_input)\r
+ if((device->isWindowActive() && noMenuActive()) || random_input)\r
{\r
if(!random_input)\r
device->getCursorControl()->setVisible(false);\r
if(selected_object->getTypeId() == MAPBLOCKOBJECT_TYPE_SIGN)\r
{\r
dstream<<"Sign object right-clicked"<<std::endl;\r
+ \r
+ if(random_input == false)\r
+ {\r
+ // Get a new text for it\r
\r
- text_input.start(new TextDestSign(\r
- selected_object->getBlock()->getPos(),\r
- selected_object->getId(),\r
- &client));\r
+ TextDest *dest = new TextDestSign(\r
+ selected_object->getBlock()->getPos(),\r
+ selected_object->getId(),\r
+ &client);\r
\r
- if(random_input)\r
- {\r
- g_text_buffer = L"ASD LOL 8)";\r
- g_text_buffer_accepted = true;\r
+ SignObject *sign_object = (SignObject*)selected_object;\r
+\r
+ std::wstring wtext =\r
+ narrow_to_wide(sign_object->getText());\r
+\r
+ (new GUITextInputMenu(guienv, guiroot, -1,\r
+ &g_active_menu_count, dest,\r
+ wtext))->drop();\r
}\r
}\r
/*\r
delete a;\r
}\r
\r
- if(text_input.guitext != NULL)\r
- {\r
- /*wchar_t temptext[100];\r
- swprintf(temptext, 100,\r
- SWPRINTF_CHARSTRING,\r
- g_text_buffer.substr(0,99).c_str()\r
- );*/\r
- text_input.guitext->setText(g_text_buffer.c_str());\r
- }\r
-\r
- /*\r
- Text input stuff\r
- */\r
- if(text_input.guitext != NULL && g_text_buffer_accepted)\r
- {\r
- text_input.guitext->remove();\r
- text_input.guitext = NULL;\r
- \r
- if(text_input.dest != NULL)\r
- {\r
- std::string text = wide_to_narrow(g_text_buffer);\r
- dstream<<"Sending text: "<<text<<std::endl;\r
- text_input.dest->sendText(text);\r
- delete text_input.dest;\r
- text_input.dest = NULL;\r
- }\r
-\r
- focusGame();\r
- }\r
-\r
- //guiupdatetimer.stop();\r
-\r
/*\r
Drawing begins\r
*/\r