Added Translator support for the UI elements created from xml, access with "@string/key".

--HG--
branch : dev
This commit is contained in:
Martí­n Lucas Golini
2017-04-11 23:44:33 -03:00
parent 427c8e2ff1
commit fd23b52f68
11 changed files with 51 additions and 48 deletions

View File

@@ -7,6 +7,7 @@
#include <eepp/window/window.hpp>
#include <eepp/window/cursorhelper.hpp>
#include <eepp/system/pack.hpp>
#include <eepp/system/translator.hpp>
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<UIWindow*> mWindowsList;
std::list<UIControl*> 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 );

View File

@@ -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 ) );
}
}

View File

@@ -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;

View File

@@ -4,6 +4,7 @@
#include <eepp/graphics/drawablesearcher.hpp>
#include <eepp/helper/pugixml/pugixml.hpp>
#include <eepp/ui/uipopupmenu.hpp>
#include <eepp/ui/uimanager.hpp>
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 );
}
}
}

View File

@@ -2,6 +2,7 @@
#include <eepp/graphics/text.hpp>
#include <eepp/helper/pugixml/pugixml.hpp>
#include <eepp/graphics/globaltextureatlas.hpp>
#include <eepp/ui/uimanager.hpp>
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 ) {

View File

@@ -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 ) {

View File

@@ -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 ) {

View File

@@ -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 ) {

View File

@@ -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 );
}
}
}

View File

@@ -14,12 +14,11 @@ void EETest::init() {
Log::instance()->setLiveWrite( true );
Log::instance()->setConsoleOutput( true );
Translator t;
t.loadFromString(
mTranslator.loadFromString(
"<resources language='en'>"
" <string name='app_name'>eepp</string>"
" <string name='formatted'>Test %d %s</string>"
" <string name='test_item'>Test Item 2</string>"
"</resources>"
);
@@ -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() {
" <TextInput text='test' layout_width='match_parent' layout_height='wrap_content' />"
" <DropDownList layout_width='match_parent' layout_height='wrap_content' selectedIndex='0'>"
" <item>Test Item</item>"
" <item>Test Item 2</item>"
" <item>@string/test_item</item>"
" </DropDownList>"
" <ListBox layout_width='match_parent' layout_height='match_parent' layout_weight='1'>"
" <item>Hello!</item>"

View File

@@ -143,6 +143,7 @@ class EETest : private Thread {
VertexBuffer * mVBO;
Clock mFTE;
Translator mTranslator;
void createCommonDialog();
void onItemClick( const UIEvent * Event );