From b7fe74141e575eecf856b7e9846bceb18f86bed2 Mon Sep 17 00:00:00 2001 From: auria Date: Sun, 15 Aug 2010 23:51:34 +0000 Subject: [PATCH] Converted the 'new player' dialog to use XML files git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@5742 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/guiengine/engine.cpp | 6 ++ src/guiengine/layout_manager.cpp | 9 +++ src/guiengine/modaldialog.cpp | 7 +++ src/guiengine/screen.cpp | 7 +++ src/guiengine/screen_loader.cpp | 7 ++- .../dialogs/enter_player_name_dialog.cpp | 57 ++++--------------- .../dialogs/enter_player_name_dialog.hpp | 6 +- 7 files changed, 48 insertions(+), 51 deletions(-) diff --git a/src/guiengine/engine.cpp b/src/guiengine/engine.cpp index 0c0617be7..cc18649d6 100644 --- a/src/guiengine/engine.cpp +++ b/src/guiengine/engine.cpp @@ -54,6 +54,7 @@ \li \ref widget10 \li \ref widget11 \li \ref widget12 + \li \ref widget13 \ref props \li \ref prop1 @@ -240,6 +241,11 @@ \n + \subsection widget13 WTYPE_TEXTBOX + Name in XML files: \c "textbox" + + A text field where the user can type text + \n \n \section props Properties diff --git a/src/guiengine/layout_manager.cpp b/src/guiengine/layout_manager.cpp index 4e50c82f3..fcf626000 100644 --- a/src/guiengine/layout_manager.cpp +++ b/src/guiengine/layout_manager.cpp @@ -139,6 +139,15 @@ void LayoutManager::readCoords(Widget* self, ITopLevelWidgetContainer* topLevelC // lines are required, we need to specify a height explicitely label_h = dim.Height + self->getHeightNeededAroundLabel(); } + if (self->getType() == WTYPE_TEXTBOX) + { + IGUIFont* font = (self->m_title_font ? GUIEngine::getTitleFont() : GUIEngine::getFont()); + + // get text height, a text box is always as high as the text it could contain + core::dimension2d< u32 > dim = font->getDimension( L"X" ); + label_h = dim.Height + self->getHeightNeededAroundLabel(); + + } // ---- read dimension // width diff --git a/src/guiengine/modaldialog.cpp b/src/guiengine/modaldialog.cpp index f0a73ccf1..e85f182da 100644 --- a/src/guiengine/modaldialog.cpp +++ b/src/guiengine/modaldialog.cpp @@ -55,6 +55,13 @@ ModalDialog::ModalDialog(const float percentWidth, const float percentHeight) void ModalDialog::loadFromFile(const char* xmlFile) { IrrXMLReader* xml = irr::io::createIrrXMLReader( (file_manager->getGUIDir() + "/" + xmlFile).c_str() ); + if (xml == NULL) + { + fprintf(stderr, "Cannot open file %s\n", xmlFile); + assert(false); + return; + } + Screen::parseScreenFileDiv(xml, m_children, m_irrlicht_window); delete xml; diff --git a/src/guiengine/screen.cpp b/src/guiengine/screen.cpp index dcd186764..73b235184 100644 --- a/src/guiengine/screen.cpp +++ b/src/guiengine/screen.cpp @@ -114,6 +114,13 @@ void Screen::loadFromFile() assert(m_magic_number == 0xCAFEC001); IrrXMLReader* xml = irr::io::createIrrXMLReader( (file_manager->getGUIDir() + "/" + m_filename).c_str() ); + if (xml == NULL) + { + fprintf(stderr, "Cannot open file %s\n", m_filename.c_str()); + assert(false); + return; + } + parseScreenFileDiv(xml, m_widgets); m_loaded = true; calculateLayout(); diff --git a/src/guiengine/screen_loader.cpp b/src/guiengine/screen_loader.cpp index ed9c4e505..30f424963 100644 --- a/src/guiengine/screen_loader.cpp +++ b/src/guiengine/screen_loader.cpp @@ -145,9 +145,14 @@ void Screen::parseScreenFileDiv(irr::io::IrrXMLReader* xml, ptr_vector& { append_to.push_back(new ListWidget()); } + else if (!strcmp("textbox", xml->getNodeName())) + { + append_to.push_back(new TextBoxWidget()); + } else { - std::cerr << "/!\\ Warning /!\\ : unknown tag found in STK GUI file : '" << xml->getNodeName() << "'" << std::endl; + std::cerr << "/!\\ Warning /!\\ : unknown tag found in STK GUI file : '" + << xml->getNodeName() << "'" << std::endl; continue; } diff --git a/src/states_screens/dialogs/enter_player_name_dialog.cpp b/src/states_screens/dialogs/enter_player_name_dialog.cpp index 1c885aa6c..b9ec4a687 100644 --- a/src/states_screens/dialogs/enter_player_name_dialog.cpp +++ b/src/states_screens/dialogs/enter_player_name_dialog.cpp @@ -34,59 +34,19 @@ using namespace irr::gui; EnterPlayerNameDialog::EnterPlayerNameDialog(const float w, const float h) : ModalDialog(w, h) { - m_label_ctrl = new LabelWidget(); + loadFromFile("enter_player_name_dialog.stkgui"); - //I18N: In the 'add new player' dialog - m_label_ctrl->m_text = _("Enter the new player's name"); - - m_label_ctrl->m_properties[PROP_TEXT_ALIGN] = "center"; - m_label_ctrl->m_x = 0; - m_label_ctrl->m_y = 0; - m_label_ctrl->m_w = m_area.getWidth(); - m_label_ctrl->m_h = m_area.getHeight()/3; - m_label_ctrl->setParent(m_irrlicht_window); - - m_children.push_back(m_label_ctrl); - m_label_ctrl->add(); - - // ---- - - //IGUIFont* font = GUIEngine::getFont(); - const int textHeight = GUIEngine::getFontHeight(); - - const int textAreaYFrom = m_area.getHeight()/2 - textHeight/2; - - textCtrl = new TextBoxWidget(); - textCtrl->m_text = ""; - textCtrl->m_x = 50; - textCtrl->m_y = textAreaYFrom - 10; - textCtrl->m_w = m_area.getWidth()-100; - textCtrl->m_h = textHeight + 5; - textCtrl->setParent(m_irrlicht_window); - m_children.push_back(textCtrl); - textCtrl->add(); + TextBoxWidget* textCtrl = (TextBoxWidget*)Screen::getWidget("textfield", &m_children); + assert(textCtrl != NULL); textCtrl->setFocusForPlayer(PLAYER_ID_GAME_MASTER); - - // TODO : add Ok button - - cancelButton = new ButtonWidget(); - cancelButton->m_properties[PROP_ID] = "cancel"; - cancelButton->m_text = _("Cancel"); - cancelButton->m_x = 15; - cancelButton->m_y = m_area.getHeight() - textHeight - 12; - cancelButton->m_w = m_area.getWidth() - 30; - cancelButton->m_h = textHeight + 6; - cancelButton->setParent(m_irrlicht_window); - - m_children.push_back(cancelButton); - cancelButton->add(); - } // ----------------------------------------------------------------------------- EnterPlayerNameDialog::~EnterPlayerNameDialog() { + // FIXME: what is this code for? + TextBoxWidget* textCtrl = (TextBoxWidget*)Screen::getWidget("textfield", &m_children); textCtrl->getIrrlichtElement()->remove(); } @@ -94,7 +54,7 @@ EnterPlayerNameDialog::~EnterPlayerNameDialog() GUIEngine::EventPropagation EnterPlayerNameDialog::processEvent(const std::string& eventSource) { - if(eventSource == "cancel") + if (eventSource == "cancel") { dismiss(); return GUIEngine::EVENT_BLOCK; @@ -108,6 +68,7 @@ void EnterPlayerNameDialog::onEnterPressedInternal() { // ---- Cancel button pressed const int playerID = 0; // FIXME: don't hardcode player 0? + ButtonWidget* cancelButton = (ButtonWidget*)Screen::getWidget("cancel", &m_children); if (GUIEngine::isFocusedForPlayer(cancelButton, playerID)) { std::string fakeEvent = "cancel"; @@ -116,13 +77,15 @@ void EnterPlayerNameDialog::onEnterPressedInternal() } // ---- Otherwise, accept entered name + TextBoxWidget* textCtrl = (TextBoxWidget*)Screen::getWidget("textfield", &m_children); stringw playerName = textCtrl->getText(); if (playerName.size() > 0) { const bool success = OptionsScreenPlayers::getInstance()->gotNewPlayerName( playerName ); if (!success) { - m_label_ctrl->setText(_("Cannot add a player with this name.")); + LabelWidget* label = (LabelWidget*)Screen::getWidget("title", &m_children); + label->setText(_("Cannot add a player with this name.")); sfx_manager->quickSound( "use_anvil" ); return; } diff --git a/src/states_screens/dialogs/enter_player_name_dialog.hpp b/src/states_screens/dialogs/enter_player_name_dialog.hpp index 4668e30c8..f94604583 100644 --- a/src/states_screens/dialogs/enter_player_name_dialog.hpp +++ b/src/states_screens/dialogs/enter_player_name_dialog.hpp @@ -30,9 +30,9 @@ namespace GUIEngine class EnterPlayerNameDialog : public GUIEngine::ModalDialog { - GUIEngine::LabelWidget* m_label_ctrl; - GUIEngine::TextBoxWidget* textCtrl; - GUIEngine::ButtonWidget* cancelButton; + //GUIEngine::LabelWidget* m_label_ctrl; + //GUIEngine::TextBoxWidget* textCtrl; + //GUIEngine::ButtonWidget* cancelButton; public: /**