From 2a7bf997b9c4fe3b68134ebcc1b31834965689aa Mon Sep 17 00:00:00 2001 From: kaetemi Date: Fri, 30 Oct 2020 15:06:12 +0800 Subject: [PATCH] Cleanup localization behaviour --- nel/include/nel/gui/view_text.h | 11 +- nel/src/gui/ctrl_text_button.cpp | 7 +- nel/src/gui/group_container.cpp | 32 ++--- nel/src/gui/group_editbox.cpp | 2 +- nel/src/gui/view_text.cpp | 211 ++++++++++++++++++++----------- nel/src/gui/widget_manager.cpp | 1 + 6 files changed, 166 insertions(+), 98 deletions(-) diff --git a/nel/include/nel/gui/view_text.h b/nel/include/nel/gui/view_text.h index 7980e6427..69f9efca7 100644 --- a/nel/include/nel/gui/view_text.h +++ b/nel/include/nel/gui/view_text.h @@ -84,8 +84,10 @@ namespace NLGUI /// Set - void setText(const std::string &text); //< Not localized. Use setHardText to localize strings starting with "ui". TODO: Add a Localize flag, like title in group container. HardText then simply sets localize to true. + void setText(const std::string &text); + void setTextLocalized(const std::string &text, bool localized); void setTextAsUtf16 (const ucstring &text); + void setLocalized(bool localized); void setFontName (const std::string &name); void setFontSize (sint nFontSize, bool coef = true); void setEmbolden (bool nEmbolden); @@ -114,9 +116,10 @@ namespace NLGUI void disableStringSelection(); /// Get - std::string getText() const { return _Text; } + std::string getText() const { return _HardText.empty() ? _Text : _HardText; } ucstring getTextAsUtf16() const; ucstring getHardTextAsUtf16() const; + bool isLocalized() const { return _Localized; } sint getFontSize() const; std::string getFontName() const { return _FontName; } bool getEmbolden() { return _Embolden; } @@ -224,6 +227,7 @@ namespace NLGUI REFLECT_EXPORT_START(CViewText, CViewBase) REFLECT_STRING("text_raw", getText, setText); REFLECT_STRING("hardtext", getHardText, setHardText); + REFLECT_BOOL ("localize", isLocalized, setLocalized); // REFLECT_UCSTRING("uc_text", getTextAsUtf16, setTextAsUtf16); // Deprecate uc_ functions REFLECT_UCSTRING("uc_hardtext", getHardTextAsUtf16, setHardTextAsUtf16); REFLECT_UCSTRING("uc_hardtext_format", getTextAsUtf16, setTextFormatTagedAsUtf16); // FIXME: Name doesn't make sense @@ -248,6 +252,7 @@ namespace NLGUI std::string _HardText; std::string _Text; mutable sint _TextLength; + bool _Localized; /// index of the computed String associated to this text control uint _Index; /// info on the computed String associated to this text control @@ -443,6 +448,8 @@ namespace NLGUI void setup (); void setupDefault (); + void setTextLocalized(const std::string &text); + void setStringSelectionSkipingSpace(uint stringId, const std::string &text, sint charStart, sint charEnd); // void pushString(const ucstring &str, bool deleteSpaceAtStart = false); diff --git a/nel/src/gui/ctrl_text_button.cpp b/nel/src/gui/ctrl_text_button.cpp index c5571a4ca..3be6142e3 100644 --- a/nel/src/gui/ctrl_text_button.cpp +++ b/nel/src/gui/ctrl_text_button.cpp @@ -597,12 +597,7 @@ namespace NLGUI if (prop) { const char *propPtr = prop; - std::string text; - if (NLMISC::startsWith(propPtr, "ui")) - text = CI18N::get(propPtr); - else - text = propPtr; - _ViewText->setText(text); + _ViewText->setTextLocalized(propPtr, true); } } diff --git a/nel/src/gui/group_container.cpp b/nel/src/gui/group_container.cpp index 1416f5ee6..0740bd80f 100644 --- a/nel/src/gui/group_container.cpp +++ b/nel/src/gui/group_container.cpp @@ -2144,24 +2144,20 @@ namespace NLGUI ptr = xmlGetProp (cur, (xmlChar*)"title"); if (ptr) { - if (_Localize) _TitleTextOpened = CI18N::get(string((const char*)ptr)); - else _TitleTextOpened = string((const char*)ptr); - if (_Localize) _TitleTextClosed = CI18N::get(string((const char*)ptr)); - else _TitleTextClosed = string((const char*)ptr); + _TitleTextOpened = (const char *)ptr; + _TitleTextClosed = (const char *)ptr; } ptr = xmlGetProp (cur, (xmlChar*)"title_opened"); if (ptr) { - if (_Localize) _TitleTextOpened = CI18N::get(string((const char*)ptr)); - else _TitleTextOpened = string((const char*)ptr); + _TitleTextOpened = (const char*)ptr; } ptr = xmlGetProp (cur, (xmlChar*)"title_closed"); if (ptr) { - if (_Localize) _TitleTextClosed = CI18N::get(string((const char*)ptr)); - else _TitleTextClosed = string((const char*)ptr); + _TitleTextClosed = (const char *)ptr; } ptr = xmlGetProp (cur, (xmlChar*)"header_active"); @@ -3950,8 +3946,14 @@ namespace NLGUI void CGroupContainer::setLocalize(bool localize) { _Localize = localize; - setTitledOpenedViewText(); - setTitledClosedViewText(); + if (_TitleOpened) + { + _TitleOpened->setLocalized(localize); + } + if (_TitleClosed) + { + _TitleClosed->setLocalized(localize); + } invalidateCoords(); } @@ -4001,10 +4003,7 @@ namespace NLGUI { if (_TitleOpened != NULL) { - if (_Localize && NLMISC::startsWith(_TitleTextOpened, "ui")) - _TitleOpened->setHardText(_TitleTextOpened); - else - _TitleOpened->setText(_TitleTextOpened); + _TitleOpened->setTextLocalized(_TitleTextOpened, _Localize); } } @@ -4013,10 +4012,7 @@ namespace NLGUI { if (_TitleClosed != NULL) { - if (_Localize && NLMISC::startsWith(_TitleTextClosed, "ui")) - _TitleClosed->setHardText(_TitleTextClosed); - else - _TitleClosed->setText(_TitleTextClosed); + _TitleClosed->setTextLocalized(_TitleTextClosed, _Localize); } } diff --git a/nel/src/gui/group_editbox.cpp b/nel/src/gui/group_editbox.cpp index 799452b33..27084dd1b 100644 --- a/nel/src/gui/group_editbox.cpp +++ b/nel/src/gui/group_editbox.cpp @@ -1580,7 +1580,7 @@ namespace NLGUI _ViewText->setParent( this ); _ViewText->setIdRecurse( "edit_text" ); - _ViewText->setHardText( "" ); + _ViewText->setTextLocalized( "", false ); _ViewText->setPosRef( Hotspot_ML ); _ViewText->setParentPosRef( Hotspot_ML ); addView( _ViewText ); diff --git a/nel/src/gui/view_text.cpp b/nel/src/gui/view_text.cpp index 87ebab92d..f96ac535d 100644 --- a/nel/src/gui/view_text.cpp +++ b/nel/src/gui/view_text.cpp @@ -111,6 +111,8 @@ namespace NLGUI _ClampRight = true; // clamp on the right of the text _OverflowText = "..."; + _Localized = true; + _LetterColors = NULL; _Setuped= false; _AutoClampOffset = 0; @@ -211,7 +213,7 @@ namespace NLGUI _Index = 0xFFFFFFFF; _ModulateGlobalColor= vt._ModulateGlobalColor; - + _Localized = vt._Localized; // remove previous lines clearLines(); @@ -244,6 +246,11 @@ namespace NLGUI std::string CViewText::getTextProperty( const std::string &name ) const { + if( name == "localize" ) + { + return toString(_Localized); + } + else if( name == "color" ) { return toString( _Color ); @@ -418,6 +425,18 @@ namespace NLGUI bool CViewText::setTextProperty( const std::string &name, const std::string &value ) { + if( name == "localize" ) + { + bool b; + if (fromString(value, b)) + { + _Localized = b; + setTextLocalized(_HardText.empty() ? _Text : _HardText); // FIXME: setCase? + _TextLength = 0; + } + return true; + } + else if( name == "color" ) { CRGBA c; @@ -649,28 +668,19 @@ namespace NLGUI return true; } else + if( name == "text" ) + { + setTextLocalized(value); // FIXME: setCase? + _TextLength = 0; + invalidateContent(); + return true; + } + else if( name == "hardtext" ) { -#if 1 - if (NLMISC::startsWith(value, "ui")) - { - _Text = CI18N::get(value); - _TextLength = 0; - _HardText = value; - } - else - { - _Text = value; - _TextLength = 0; - _HardText.clear(); - } -#else - _Text = value; + _Localized = true; + setTextLocalized(value); // FIXME: setCase? _TextLength = 0; - _HardText.clear(); - if (NLMISC::startsWith(value, "ui")) - _HardText = _Text; -#endif invalidateContent(); return true; } @@ -705,6 +715,8 @@ namespace NLGUI bool CViewText::serializeTextOptions( xmlNodePtr node ) const { + xmlSetProp( node, BAD_CAST "localize", BAD_CAST toString( _Localized ).c_str() ); + xmlSetProp( node, BAD_CAST "color", BAD_CAST toString( _Color ).c_str() ); xmlSetProp( node, BAD_CAST "global_color", BAD_CAST toString( _ModulateGlobalColor ).c_str() ); @@ -784,7 +796,7 @@ namespace NLGUI serializeTextOptions( node ); - xmlSetProp( node, BAD_CAST "hardtext", BAD_CAST _Text.c_str() ); + xmlSetProp( node, BAD_CAST "text", BAD_CAST (_HardText.empty() ? _Text.c_str() : _HardText.c_str()) ); xmlSetProp( node, BAD_CAST "hardtext_format", BAD_CAST _HardTextFormat.c_str() ); return node; @@ -795,6 +807,9 @@ namespace NLGUI { CXMLAutoPtr prop; + prop = xmlGetProp (cur, (xmlChar*)"localize"); + if (prop) _Localized = convertBool((const char*)prop); + prop= (char*) xmlGetProp( cur, (xmlChar*)"color" ); _Color = CRGBA(255,255,255,255); if (prop) @@ -1000,18 +1015,8 @@ namespace NLGUI if (prop) { const char *propPtr = prop; - if (NLMISC::startsWith(propPtr, "ui")) - { - _HardText = propPtr; - _Text = CI18N::get(propPtr); - _TextLength = 0; - } - else - { - _HardText.clear(); - _Text = propPtr; - _TextLength = 0; - } + _Localized = true; + setTextLocalized(propPtr); setCase(_Text, _CaseMode); _TextLength = 0; } @@ -1427,49 +1432,109 @@ namespace NLGUI } // *************************************************************************** - void CViewText::setText(const std::string &text) + void CViewText::setTextLocalized(const std::string &text, bool localized) { - _HardText.clear(); - // common case: no special format, no case mode => easy cache test - if (_FormatTags.empty() && _CaseMode==CaseNormal) + if (localized != _Localized) { - if (text != _Text) - { - _Text = text; - _TextLength = 0; - // no need to call "setCase (_Text, _CaseMode);" since CaseNormal: - invalidateContent (); - } + _Localized = localized; + + // Always recompute if localization and text changed + setTextLocalized(text); + setCase(_Text, _CaseMode); + _TextLength = 0; + invalidateContent(); } else { - // if the view text had some format before, no choice, must recompute all - if (!_FormatTags.empty()) + setText(text); + } + } + + // *************************************************************************** + void CViewText::setLocalized(bool localized) + { + if (localized != _Localized) + { + const std::string &text = _HardText.empty() ? _Text : _HardText; + _Localized = localized; + if (!text.empty() && NLMISC::startsWith(text, "ui")) { - _Text = text; - setCase (_Text, _CaseMode); + setTextLocalized(text); + setCase(_Text, _CaseMode); _TextLength = 0; - invalidateContent (); - } - // else test if after the case change the cache succeed - else - { - // compute the temp cased text - std::string tempText = text; - setCase (tempText, _CaseMode); - if (tempText != _Text) - { - _Text = tempText; - _TextLength = 0; - invalidateContent(); - } + invalidateContent(); } } - // clear format tags if any - _FormatTags.clear(); + nlassert(_Text.empty() || ((_Localized && (NLMISC::startsWith(getText(), "ui"))) == (_HardText.empty() == _Text.empty()))); + } + + // *************************************************************************** + void CViewText::setTextLocalized(const std::string &text) + { + if (_Localized && NLMISC::startsWith(text, "ui")) + { + _HardText = text; + _Text = CI18N::get(text); + } + else + { + _Text = text; + _HardText.clear(); + } } + // *************************************************************************** + void CViewText::setText(const std::string &text) + { + // common case: no special format, no case mode => easy cache test + if (_FormatTags.empty() && _CaseMode == CaseNormal) + { + if (_HardText.empty() ? text != _Text : text != _HardText) + { + setTextLocalized(text); + _TextLength = 0; + // no need to call "setCase (_Text, _CaseMode);" since CaseNormal: + invalidateContent(); + } + } + else + { + // if the view text had some format before, no choice, must recompute all + if (!_FormatTags.empty()) + { + setTextLocalized(text); + setCase(_Text, _CaseMode); + _TextLength = 0; + invalidateContent(); + } + // else test if after the case change the cache succeed + else + { + // compute the temp cased text + std::string holdText, holdHardText; + holdText.swap(_Text); + holdHardText.swap(_HardText); + setTextLocalized(text); + setCase(_Text, _CaseMode); + if (holdText != _Text) + { + _TextLength = 0; + invalidateContent(); + } + else + { + holdText.swap(_Text); + } + } + } + + nlassert(_Text.empty() || ((_Localized && (NLMISC::startsWith(text, "ui"))) == (_HardText.empty() == _Text.empty()))); + + // clear format tags if any + _FormatTags.clear(); + } + // *************************************************************************** void CViewText::setFontSizing(const std::string &chars, const std::string &fallback) { @@ -2396,15 +2461,12 @@ namespace NLGUI // *************************************************************************** void CViewText::setHardText (const std::string &ht) { - if (NLMISC::startsWith(ht, "ui")) - { - setText(CI18N::get(ht)); - _HardText = ht; - } - else + if (!_Localized) { - setText(ht); + setText(std::string()); + _Localized = true; } + setText(ht); } // *************************************************************************** @@ -3367,6 +3429,7 @@ namespace NLGUI // Copy to Text (preserve Memory) contReset(_Text); _Text = tempText; + _HardText.clear(); _TextLength = 0; CInterfaceGroup *parent = getParent(); @@ -3477,6 +3540,7 @@ namespace NLGUI // Copy to Text (preserve Memory) contReset(_Text); _Text = tempText; + _HardText.clear(); _TextLength = 0; invalidateContent (); } @@ -3560,6 +3624,11 @@ namespace NLGUI #define SERIAL_UINT(val) { uint32 tmp = (uint32) val; f.serial(tmp); val = (uint) tmp; } #define SERIAL_SINT(val) { sint32 tmp = (sint32) val; f.serial(tmp); val = (sint) tmp; } CViewBase::serial(f); + + int version = f.serialVersion(101); + nlassert(version >= 100); + + f.serial(_Localized); SERIAL_SINT(_FontSize); SERIAL_UINT(_FontWidth); SERIAL_UINT(_FontHeight); diff --git a/nel/src/gui/widget_manager.cpp b/nel/src/gui/widget_manager.cpp index 534a0cc1b..9ce889a95 100644 --- a/nel/src/gui/widget_manager.cpp +++ b/nel/src/gui/widget_manager.cpp @@ -1195,6 +1195,7 @@ namespace NLGUI sint32 backupX = groupOver->getX(); // Copy all aspects to the view + vtDst->setLocalized (vtSrc->isLocalized()); vtDst->setText (vtSrc->getText()); vtDst->setFontSize (vtSrc->getFontSize()); vtDst->setColor (vtSrc->getColor());