From 4a722c7783754c6fead120a6f63cb73d32caf03e Mon Sep 17 00:00:00 2001 From: Nimetu Date: Sun, 14 Aug 2016 02:25:10 +0300 Subject: [PATCH] Changed: Use UI scale change event to change CViewText font scale --HG-- branch : experimental-ui-scaling --- code/nel/include/nel/gui/interface_element.h | 16 +++++- code/nel/include/nel/gui/view_text.h | 1 + code/nel/include/nel/gui/widget_manager.h | 7 +++ code/nel/src/gui/view_text.cpp | 27 ++++++---- code/nel/src/gui/widget_manager.cpp | 56 ++++++++++++++++---- 5 files changed, 87 insertions(+), 20 deletions(-) diff --git a/code/nel/include/nel/gui/interface_element.h b/code/nel/include/nel/gui/interface_element.h index 65a0fcaff..5177e322a 100644 --- a/code/nel/include/nel/gui/interface_element.h +++ b/code/nel/include/nel/gui/interface_element.h @@ -38,6 +38,16 @@ namespace NLGUI class IActionHandler; class CGroupParagraph; + /** + * Interface for UI scale change event + */ + class IInterfaceScaleWatcher + { + public: + virtual ~IInterfaceScaleWatcher(){} + virtual void onInterfaceScaleChanged()=0; + }; + /** * A visitor to walk a tree of interface elements and apply a teartment on them. * @@ -66,7 +76,7 @@ namespace NLGUI * \author Nevrax France * \date 2002 */ - class CInterfaceElement : public CReflectableRefPtrTarget, public NLMISC::IStreamable + class CInterfaceElement : public IInterfaceScaleWatcher, public CReflectableRefPtrTarget, public NLMISC::IStreamable { public: @@ -402,6 +412,10 @@ namespace NLGUI */ virtual void onInvalidateContent() {} + /* Element UI scale change event callback + */ + virtual void onInterfaceScaleChanged() {} + // called by interfaceManager for master window only void resetInvalidCoords(); diff --git a/code/nel/include/nel/gui/view_text.h b/code/nel/include/nel/gui/view_text.h index 3c5b8622d..95b8d2e4a 100644 --- a/code/nel/include/nel/gui/view_text.h +++ b/code/nel/include/nel/gui/view_text.h @@ -70,6 +70,7 @@ namespace NLGUI virtual void checkCoords(); virtual void updateCoords(); virtual void onAddToGroup(); + virtual void onInterfaceScaleChanged(); /// From CInterfaceElement sint32 getMaxUsedW() const; diff --git a/code/nel/include/nel/gui/widget_manager.h b/code/nel/include/nel/gui/widget_manager.h index 8cca08cc6..827e35224 100644 --- a/code/nel/include/nel/gui/widget_manager.h +++ b/code/nel/include/nel/gui/widget_manager.h @@ -49,6 +49,7 @@ namespace NLGUI class CProcedure; class IEditorSelectionWatcher; class IWidgetAdditionWatcher; + class IInterfaceScaleWatcher; /** GUI Widget Manager @@ -530,6 +531,11 @@ namespace NLGUI bool unGroupSelection(); void setMultiSelection( bool b ){ multiSelection = b; } + float getInterfaceScale() const { return _InterfaceScale; } + void notifyInterfaceScaleWatchers(); + void registerInterfaceScaleWatcher(IInterfaceScaleWatcher *watcher); + void unregisterInterfaceScaleWatcher(IInterfaceScaleWatcher *watcher); + bool createNewGUI( const std::string &project, const std::string &window ); private: @@ -623,6 +629,7 @@ namespace NLGUI std::vector< IOnWidgetsDrawnHandler* > onWidgetsDrawnHandlers; std::vector< IEditorSelectionWatcher* > selectionWatchers; std::vector< IWidgetWatcher* > widgetWatchers; + std::vector< IInterfaceScaleWatcher* > scaleWatchers; std::vector< std::string > editorSelection; bool _GroupSelection; diff --git a/code/nel/src/gui/view_text.cpp b/code/nel/src/gui/view_text.cpp index f28f9c3ed..3e649c1c3 100644 --- a/code/nel/src/gui/view_text.cpp +++ b/code/nel/src/gui/view_text.cpp @@ -80,7 +80,7 @@ namespace NLGUI _MultiMaxLine = 0; _Index = 0xFFFFFFFF; - _Scale = 1.0f; + _Scale = CWidgetManager::getInstance()->getInterfaceScale(); _FontWidth= 0; _FontHeight = 0; _FontLegHeight = 0; @@ -113,6 +113,8 @@ namespace NLGUI :CViewBase(param) { setupDefault (); + + CWidgetManager::getInstance()->registerInterfaceScaleWatcher(this); } ///constructor @@ -130,11 +132,15 @@ namespace NLGUI _ShadowOutline = ShadowOutline; setText(Text); computeFontSize (); + + CWidgetManager::getInstance()->registerInterfaceScaleWatcher(this); } // *************************************************************************** CViewText::~CViewText() { + CWidgetManager::getInstance()->unregisterInterfaceScaleWatcher(this); + if (_Index != 0xFFFFFFFF) CViewRenderer::getTextContext(_FontName)->erase (_Index); clearLines(); @@ -893,9 +899,6 @@ namespace NLGUI // *************************************************************************** void CViewText::checkCoords () { - if (_Scale != CViewRenderer::getInstance()->getInterfaceScale()) - invalidateContent(); - if ((_MultiLine)&&(_Parent != NULL)) { // If never setuped, and if text is not empty @@ -1840,9 +1843,6 @@ namespace NLGUI // *************************************************************************** void CViewText::updateTextContext () { - if (_Scale != CViewRenderer::getInstance()->getInterfaceScale()) - computeFontSize(); - NL3D::UTextContext *TextContext = CViewRenderer::getTextContext(_FontName); TextContext->setHotSpot (UTextContext::BottomLeft); @@ -2733,10 +2733,19 @@ namespace NLGUI } // *************************************************************************** - void CViewText::computeFontSize () + void CViewText::onInterfaceScaleChanged() { - _Scale = CViewRenderer::getInstance()->getInterfaceScale(); + _Scale = CWidgetManager::getInstance()->getInterfaceScale(); + computeFontSize (); + invalidateContent(); + + CViewBase::onInterfaceScaleChanged(); + } + + // *************************************************************************** + void CViewText::computeFontSize () + { NL3D::UTextContext *TextContext = CViewRenderer::getTextContext(_FontName); TextContext->setHotSpot (UTextContext::BottomLeft); TextContext->setShaded (_Shadow); diff --git a/code/nel/src/gui/widget_manager.cpp b/code/nel/src/gui/widget_manager.cpp index 5d278a29a..a215dbf08 100644 --- a/code/nel/src/gui/widget_manager.cpp +++ b/code/nel/src/gui/widget_manager.cpp @@ -1850,10 +1850,9 @@ namespace NLGUI class InvalidateTextVisitor : public CInterfaceElementVisitor { public: - InvalidateTextVisitor( bool reset, bool invalidate ) + InvalidateTextVisitor( bool reset) { this->reset = reset; - this->invalidate = invalidate; } void visitGroup( CInterfaceGroup *group ) @@ -1866,17 +1865,13 @@ namespace NLGUI { if( reset ) vt->resetTextIndex(); - if( invalidate ) - vt->invalidateContent(); - else - vt->updateTextContext(); + vt->updateTextContext(); } } } private: bool reset; - bool invalidate; }; // ------------------------------------------------------------------------------------------------ @@ -1889,8 +1884,6 @@ namespace NLGUI CViewRenderer::getInstance()->checkNewScreenSize (); CViewRenderer::getInstance()->getScreenSize (w, h); - bool scaleChanged = _InterfaceScale != CViewRenderer::getInstance()->getInterfaceScale(); - // Update ui:* (limit the master containers to the height of the screen) for (nMasterGroup = 0; nMasterGroup < _MasterGroups.size(); nMasterGroup++) { @@ -1900,6 +1893,13 @@ namespace NLGUI } CViewRenderer::getInstance()->setClipWindow(0, 0, w, h); + bool scaleChanged = _InterfaceScale != CViewRenderer::getInstance()->getInterfaceScale(); + if (scaleChanged) + { + _InterfaceScale = CViewRenderer::getInstance()->getInterfaceScale(); + notifyInterfaceScaleWatchers(); + } + // If all conditions are OK, move windows so they fit correctly with new screen size // Do this work only InGame when Config is loaded moveAllWindowsToNewScreenSize(w,h,true); @@ -1909,7 +1909,7 @@ namespace NLGUI { SMasterGroup &rMG = _MasterGroups[nMasterGroup]; - InvalidateTextVisitor inv( false, scaleChanged ); + InvalidateTextVisitor inv( false); rMG.Group->visitGroupAndChildren( &inv ); rMG.Group->invalidateCoords (); @@ -3697,6 +3697,42 @@ namespace NLGUI } + // ------------------------------------------------------------------------------------------------ + void CWidgetManager::notifyInterfaceScaleWatchers() + { + std::vector< IInterfaceScaleWatcher* >::iterator itr = scaleWatchers.begin(); + while( itr != scaleWatchers.end() ) + { + (*itr)->onInterfaceScaleChanged(); + ++itr; + } + } + + // ------------------------------------------------------------------------------------------------ + void CWidgetManager::registerInterfaceScaleWatcher( IInterfaceScaleWatcher *watcher ) + { + std::vector< IInterfaceScaleWatcher* >::const_iterator itr + = std::find( scaleWatchers.begin(), scaleWatchers.end(), watcher ); + + if( itr != scaleWatchers.end() ) + return; + + scaleWatchers.push_back( watcher ); + } + + // ------------------------------------------------------------------------------------------------ + void CWidgetManager::unregisterInterfaceScaleWatcher( IInterfaceScaleWatcher *watcher ) + { + std::vector< IInterfaceScaleWatcher* >::iterator itr + = std::find( scaleWatchers.begin(), scaleWatchers.end(), watcher ); + + if( itr == scaleWatchers.end() ) + return; + + scaleWatchers.erase( itr ); + } + + // ------------------------------------------------------------------------------------------------ CWidgetManager::CWidgetManager() { LinkHack();