From fd23b52f689083a2cd241c22e00794e642931632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=C2=ADn=20Lucas=20Golini?= Date: Tue, 11 Apr 2017 23:44:33 -0300 Subject: [PATCH] Added Translator support for the UI elements created from xml, access with "@string/key". --HG-- branch : dev --- include/eepp/ui/uimanager.hpp | 31 ++++++++++++++-------------- src/eepp/ui/uilistbox.cpp | 2 +- src/eepp/ui/uimanager.cpp | 39 +++++++++++++++++------------------ src/eepp/ui/uimenu.cpp | 7 ++++--- src/eepp/ui/uipushbutton.cpp | 3 ++- src/eepp/ui/uitextedit.cpp | 2 +- src/eepp/ui/uitextinput.cpp | 2 +- src/eepp/ui/uitextview.cpp | 2 +- src/eepp/ui/uiwinmenu.cpp | 2 +- src/test/eetest.cpp | 8 +++---- src/test/eetest.hpp | 1 + 11 files changed, 51 insertions(+), 48 deletions(-) diff --git a/include/eepp/ui/uimanager.hpp b/include/eepp/ui/uimanager.hpp index b9109ac0a..ea542d1c6 100644 --- a/include/eepp/ui/uimanager.hpp +++ b/include/eepp/ui/uimanager.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace pugi { class xml_node; @@ -52,14 +53,6 @@ class EE_API UIManager { const Uint32& getLastPressTrigger() const; - void clipPlaneEnable( const Int32& x, const Int32& y, const Uint32& Width, const Uint32& Height ); - - void clipPlaneDisable(); - - void clipEnable( const Int32& x, const Int32& y, const Uint32& Width, const Uint32& Height ); - - void clipDisable(); - void clipSmartEnable( UIControl * ctrl, const Int32& x, const Int32& y, const Uint32& Width, const Uint32& Height ); void clipSmartDisable( UIControl * ctrl ); @@ -132,17 +125,23 @@ class EE_API UIManager { void loadLayoutFromPack( Pack * pack, const std::string& FilePackPath, UIControl * parent = NULL ); void loadLayoutNodes( pugi::xml_node node, UIControl * parent ); + + void setTranslator( Translator translator ); + + Translator& getTranslator(); + + String getTranslatorString( const std::string& str ); protected: friend class UIControl; friend class UIWindow; - EE::Window::Window * mWindow; - Input * mKM; + EE::Window::Window *mWindow; + Input * mKM; UIWindow * mControl; - UIControl * mFocusControl; - UIControl * mOverControl; + UIControl * mFocusControl; + UIControl * mOverControl; UIControl * mDownControl; - UIControl * mLossFocusControl; + UIControl * mLossFocusControl; std::list mWindowsList; std::list mCloseList; @@ -151,8 +150,8 @@ class EE_API UIManager { Uint32 mResizeCb; Uint32 mFlags; - Color mHighlightFocusColor; - Color mHighlightOverColor; + Color mHighlightFocusColor; + Color mHighlightOverColor; Vector2i mMouseDownPos; bool mInit; @@ -161,6 +160,8 @@ class EE_API UIManager { bool mControlDragging; bool mUseGlobalCursors; + Translator mTranslator; + UIManager(); void inputCallback( InputEvent * Event ); diff --git a/src/eepp/ui/uilistbox.cpp b/src/eepp/ui/uilistbox.cpp index 07e356d41..5f3bdd8af 100644 --- a/src/eepp/ui/uilistbox.cpp +++ b/src/eepp/ui/uilistbox.cpp @@ -1050,7 +1050,7 @@ void UIListBox::loadFromXmlNode(const pugi::xml_node & node) { std::string data = item.text().as_string(); if ( data.size() ) { - items.push_back( String( data ) ); + items.push_back( UIManager::instance()->getTranslatorString( data ) ); } } diff --git a/src/eepp/ui/uimanager.cpp b/src/eepp/ui/uimanager.cpp index 4f73d8161..12597ffda 100644 --- a/src/eepp/ui/uimanager.cpp +++ b/src/eepp/ui/uimanager.cpp @@ -287,35 +287,19 @@ const Uint32& UIManager::getLastPressTrigger() const { return mKM->getLastPressTrigger(); } -void UIManager::clipPlaneEnable( const Int32& x, const Int32& y, const Uint32& Width, const Uint32& Height ) { - GLi->getClippingMask()->clipPlaneEnable( x, y, Width, Height ); -} - -void UIManager::clipPlaneDisable() { - GLi->getClippingMask()->clipPlaneDisable(); -} - -void UIManager::clipEnable( const Int32& x, const Int32& y, const Uint32& Width, const Uint32& Height ) { - GLi->getClippingMask()->clipEnable( x, y, Width, Height ); -} - -void UIManager::clipDisable() { - GLi->getClippingMask()->clipDisable(); -} - void UIManager::clipSmartEnable(UIControl * ctrl, const Int32 & x, const Int32 & y, const Uint32 & Width, const Uint32 & Height) { if ( ctrl->isMeOrParentTreeScaledOrRotated() ) { - clipPlaneEnable( x, y, Width, Height ); + GLi->getClippingMask()->clipPlaneEnable( x, y, Width, Height ); } else { - clipEnable( x, y, Width, Height ); + GLi->getClippingMask()->clipEnable( x, y, Width, Height ); } } void UIManager::clipSmartDisable(UIControl * ctrl) { if ( ctrl->isMeOrParentTreeScaledOrRotated() ) { - clipPlaneDisable(); + GLi->getClippingMask()->clipPlaneDisable(); } else { - clipDisable(); + GLi->getClippingMask()->clipDisable(); } } @@ -524,6 +508,21 @@ void UIManager::loadLayoutNodes( pugi::xml_node node, UIControl * parent ) { } } +void UIManager::setTranslator( Translator translator ) { + mTranslator = translator; +} + +String UIManager::getTranslatorString( const std::string & str ) { + if ( String::startsWith( str, "@string/" ) ) { + String tstr = mTranslator.getString( str.substr( 8 ) ); + + if ( !tstr.empty() ) + return tstr; + } + + return String( str ); +} + void UIManager::loadLayoutFromFile( const std::string& layoutPath, UIControl * parent ) { if ( FileSystem::fileExists( layoutPath ) ) { pugi::xml_document doc; diff --git a/src/eepp/ui/uimenu.cpp b/src/eepp/ui/uimenu.cpp index db9f8c31e..de7fba691 100644 --- a/src/eepp/ui/uimenu.cpp +++ b/src/eepp/ui/uimenu.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace EE { namespace UI { @@ -551,14 +552,14 @@ void UIMenu::loadFromXmlNode( const pugi::xml_node& node ) { if ( name == "menuitem" || name == "item" ) { std::string text( item.attribute("text").as_string() ); std::string icon( item.attribute("icon").as_string() ); - add( String( text ), getIconDrawable( icon ) ); + add( UIManager::instance()->getTranslatorString( text ), getIconDrawable( icon ) ); } else if ( name == "menuseparator" || name == "separator" ) { addSeparator(); } else if ( name == "menucheckbox" || name == "checkbox" ) { std::string text( item.attribute("text").as_string() ); bool active( item.attribute("active").as_bool() ); - addCheckBox( String( text ), active ); + addCheckBox( UIManager::instance()->getTranslatorString( text ), active ); } else if ( name == "menusubmenu" || name == "submenu" ) { std::string text( item.attribute("text").as_string() ); std::string icon( item.attribute("icon").as_string() ); @@ -567,7 +568,7 @@ void UIMenu::loadFromXmlNode( const pugi::xml_node& node ) { subMenu->loadFromXmlNode( item ); - addSubMenu( String( text ), getIconDrawable( icon ), subMenu ); + addSubMenu( UIManager::instance()->getTranslatorString( text ), getIconDrawable( icon ), subMenu ); } } } diff --git a/src/eepp/ui/uipushbutton.cpp b/src/eepp/ui/uipushbutton.cpp index d8a80853a..33e2fc64c 100644 --- a/src/eepp/ui/uipushbutton.cpp +++ b/src/eepp/ui/uipushbutton.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace EE { namespace UI { @@ -345,7 +346,7 @@ void UIPushButton::loadFromXmlNode(const pugi::xml_node & node) { String::toLowerInPlace( name ); if ( "text" == name ) { - setText( ait->as_string() ); + setText( UIManager::instance()->getTranslatorString( ait->as_string() ) ); } else if ( "textovercolor" == name ) { setFontOverColor( Color::fromString( ait->as_string() ) ); } else if ( "icon" == name ) { diff --git a/src/eepp/ui/uitextedit.cpp b/src/eepp/ui/uitextedit.cpp index 4775e100b..6009ae6e7 100644 --- a/src/eepp/ui/uitextedit.cpp +++ b/src/eepp/ui/uitextedit.cpp @@ -445,7 +445,7 @@ void UITextEdit::loadFromXmlNode(const pugi::xml_node & node) { String::toLowerInPlace( name ); if ( "text" == name ) { - setText( ait->as_string() ); + setText( UIManager::instance()->getTranslatorString( ait->as_string() ) ); } else if ( "allowediting" == name ) { setAllowEditing( ait->as_bool() ); } else if ( "verticalscrollmode" == name || "vscrollmode" == name ) { diff --git a/src/eepp/ui/uitextinput.cpp b/src/eepp/ui/uitextinput.cpp index 574227eb9..bdf8b3a7a 100644 --- a/src/eepp/ui/uitextinput.cpp +++ b/src/eepp/ui/uitextinput.cpp @@ -335,7 +335,7 @@ void UITextInput::loadFromXmlNode(const pugi::xml_node & node) { String::toLowerInPlace( name ); if ( "text" == name ) { - setText( ait->as_string() ); + setText( UIManager::instance()->getTranslatorString( ait->as_string() ) ); } else if ( "allowediting" == name ) { setAllowEditing( ait->as_bool() ); } else if ( "maxlength" == name ) { diff --git a/src/eepp/ui/uitextview.cpp b/src/eepp/ui/uitextview.cpp index e8e79a548..bb0d5e659 100644 --- a/src/eepp/ui/uitextview.cpp +++ b/src/eepp/ui/uitextview.cpp @@ -487,7 +487,7 @@ void UITextView::loadFromXmlNode(const pugi::xml_node & node) { String::toLowerInPlace( name ); if ( "text" == name ) { - setText( ait->as_string() ); + setText( UIManager::instance()->getTranslatorString( ait->as_string() ) ); } else if ( "textcolor" == name ) { setFontColor( Color::fromString( ait->as_string() ) ); } else if ( "textshadowcolor" == name ) { diff --git a/src/eepp/ui/uiwinmenu.cpp b/src/eepp/ui/uiwinmenu.cpp index 871e26da2..7c7d7d29b 100644 --- a/src/eepp/ui/uiwinmenu.cpp +++ b/src/eepp/ui/uiwinmenu.cpp @@ -306,7 +306,7 @@ void UIWinMenu::loadFromXmlNode( const pugi::xml_node& node ) { subMenu->loadFromXmlNode( item ); - addMenuButton( String( text ), subMenu ); + addMenuButton( UIManager::instance()->getTranslatorString( text ), subMenu ); } } } diff --git a/src/test/eetest.cpp b/src/test/eetest.cpp index c8969f98d..33687d662 100644 --- a/src/test/eetest.cpp +++ b/src/test/eetest.cpp @@ -14,12 +14,11 @@ void EETest::init() { Log::instance()->setLiveWrite( true ); Log::instance()->setConsoleOutput( true ); - Translator t; - - t.loadFromString( + mTranslator.loadFromString( "" " eepp" " Test %d %s" + " Test Item 2" "" ); @@ -287,6 +286,7 @@ void EETest::createUI() { Uint32 UI_MAN_OPS = 0; //UI_MAN_OPS = UI_MANAGER_HIGHLIGHT_FOCUS | UI_MANAGER_HIGHLIGHT_OVER | UI_MANAGER_DRAW_DEBUG_DATA | UI_MANAGER_DRAW_BOXES; UIManager::instance()->init(UI_MAN_OPS); + UIManager::instance()->setTranslator( mTranslator ); //mTheme = UITheme::loadFromFile( UIThemeDefault::New( mThemeName, mThemeName ), MyPath + mThemeName + "/" ); @@ -725,7 +725,7 @@ void EETest::createNewUI() { " " " " " Test Item" - " Test Item 2" + " @string/test_item" " " " " " Hello!" diff --git a/src/test/eetest.hpp b/src/test/eetest.hpp index 500ac4700..1632c6e4e 100644 --- a/src/test/eetest.hpp +++ b/src/test/eetest.hpp @@ -143,6 +143,7 @@ class EETest : private Thread { VertexBuffer * mVBO; Clock mFTE; + Translator mTranslator; void createCommonDialog(); void onItemClick( const UIEvent * Event );