From ae6fda1faf510328c31efe355d7ba71005209191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Mon, 11 Nov 2019 02:53:01 -0300 Subject: [PATCH] UIWindow modal mode fixes. UIColor picker improvements. --HG-- branch : dev --- include/eepp/ui/tools/uicolorpicker.hpp | 15 +++-- include/eepp/ui/uihelper.hpp | 2 +- include/eepp/ui/uiwindow.hpp | 6 +- src/eepp/maps/mapeditor/uimap.cpp | 2 +- src/eepp/ui/tools/uicolorpicker.cpp | 68 ++++++++++++++++--- src/eepp/ui/uiwindow.cpp | 89 ++++++++----------------- src/test/eetest.cpp | 25 ++++--- src/test/eetest.hpp | 2 +- 8 files changed, 114 insertions(+), 95 deletions(-) diff --git a/include/eepp/ui/tools/uicolorpicker.hpp b/include/eepp/ui/tools/uicolorpicker.hpp index 8506fee23..bbd828736 100644 --- a/include/eepp/ui/tools/uicolorpicker.hpp +++ b/include/eepp/ui/tools/uicolorpicker.hpp @@ -16,13 +16,14 @@ namespace EE { namespace UI { namespace Tools { class EE_API UIColorPicker { public: typedef std::function ColorPickedCb; - typedef std::function ColorPickerCloseCb; - static UIColorPicker * NewWindow( const ColorPickedCb& colorPickedCb = ColorPickedCb(), const ColorPickerCloseCb& closeCb = ColorPickerCloseCb(), const Uint32& winFlags = UI_WIN_DEFAULT_FLAGS | UI_WIN_SHADOW | UI_WIN_FRAME_BUFFER, const Sizef& winSize = Sizef( 320, 478 ) ); + static UIColorPicker * NewModal( Node* nodeCreator, const ColorPickedCb& colorPickedCb = ColorPickedCb(), const Uint8& modalAlpha = 120, const Uint32& winFlags = UI_WIN_NO_DECORATION | UI_WIN_MODAL | UI_WIN_DRAGABLE_CONTAINER | UI_WIN_FRAME_BUFFER, const Sizef& winSize = Sizef( 320, 470 ) ); - static UIColorPicker * New( UIWindow * attach = NULL, const ColorPickedCb& colorPickedCb = ColorPickedCb(), const ColorPickerCloseCb& closeCb = ColorPickerCloseCb() ); + static UIColorPicker * NewWindow( const ColorPickedCb& colorPickedCb = ColorPickedCb(), const Uint32& winFlags = UI_WIN_DEFAULT_FLAGS | UI_WIN_SHADOW | UI_WIN_FRAME_BUFFER, const Sizef& winSize = Sizef( 320, 478 ), const Uint8& modalAlpha = 0 ); - UIColorPicker( UIWindow * attach = NULL, const ColorPickedCb& colorPickedCb = ColorPickedCb(), const ColorPickerCloseCb& closeCb = ColorPickerCloseCb() ); + static UIColorPicker * New( UIWindow * attach = NULL, const ColorPickedCb& colorPickedCb = ColorPickedCb(), const Uint8& modalAlpha = 0 ); + + UIColorPicker( UIWindow * attach = NULL, const ColorPickedCb& colorPickedCb = ColorPickedCb(), const Uint8& modalAlpha = 0 ); void setColor( const Color& color ); @@ -38,7 +39,6 @@ class EE_API UIColorPicker { Node * mUIContainer; UIWidget * mRoot; ColorPickedCb mPickedCb; - ColorPickerCloseCb mCloseCb; UIImage * mColorPicker; UIImage * mHuePicker; UIWidget * mVerticalLine; @@ -53,6 +53,7 @@ class EE_API UIColorPicker { Colorf mHsv; Color mRgb; std::string mHexColor; + Uint8 mModalAlpha; bool mUpdating; void windowClose( const Event * Event ); @@ -74,6 +75,10 @@ class EE_API UIColorPicker { void onColorPickerEvent( const MouseEvent* mouseEvent ); void onHuePickerEvent( const MouseEvent* mouseEvent ); + + Uint8 getModalAlpha() const; + + void setModalAlpha( const Uint8& modalAlpha ); }; }}} diff --git a/include/eepp/ui/uihelper.hpp b/include/eepp/ui/uihelper.hpp index 1d9f5c2e0..4a1e7ea5b 100644 --- a/include/eepp/ui/uihelper.hpp +++ b/include/eepp/ui/uihelper.hpp @@ -104,7 +104,7 @@ enum UI_MANAGER_FLAGS { }; enum UI_WINDOW_FLAGS { - UI_WIN_NO_BORDER = ( 1 << 0 ), + UI_WIN_NO_DECORATION = ( 1 << 0 ), UI_WIN_CLOSE_BUTTON = ( 1 << 1 ), UI_WIN_MINIMIZE_BUTTON = ( 1 << 2 ), UI_WIN_MAXIMIZE_BUTTON = ( 1 << 3 ), diff --git a/include/eepp/ui/uiwindow.hpp b/include/eepp/ui/uiwindow.hpp index 6518bf73f..5029bfc44 100644 --- a/include/eepp/ui/uiwindow.hpp +++ b/include/eepp/ui/uiwindow.hpp @@ -239,10 +239,6 @@ class EE_API UIWindow : public UIWidget { void createModalControl(); - void enableByModal(); - - void disableByModal(); - void resizeCursor(); void applyMinWinSize(); @@ -268,6 +264,8 @@ class EE_API UIWindow : public UIWidget { UISceneNode * getUISceneNode(); void onContainerPositionChange(const Event * Event); + + void setupModal(); }; }} diff --git a/src/eepp/maps/mapeditor/uimap.cpp b/src/eepp/maps/mapeditor/uimap.cpp index 054b81c82..8d054a5cd 100644 --- a/src/eepp/maps/mapeditor/uimap.cpp +++ b/src/eepp/maps/mapeditor/uimap.cpp @@ -12,7 +12,7 @@ UIMap * UIMap::New( UITheme * Theme, TileMap * Map ) { } UIMap::UIMap( UITheme * Theme, TileMap * Map ) : - UIWindow( SIMPLE_LAYOUT, StyleConfig( UI_WIN_NO_BORDER | UI_WIN_FRAME_BUFFER ) ), + UIWindow( SIMPLE_LAYOUT, StyleConfig( UI_WIN_NO_DECORATION | UI_WIN_FRAME_BUFFER ) ), mMap( Map ), mCurLayer( NULL ), mEditingMode( 0 ), diff --git a/src/eepp/ui/tools/uicolorpicker.cpp b/src/eepp/ui/tools/uicolorpicker.cpp index 22559e016..5e5f16b29 100644 --- a/src/eepp/ui/tools/uicolorpicker.cpp +++ b/src/eepp/ui/tools/uicolorpicker.cpp @@ -9,31 +9,63 @@ #include #include #include +#include namespace EE { namespace UI { namespace Tools { -UIColorPicker* UIColorPicker::NewWindow( const ColorPickedCb& colorPickedCb, const ColorPickerCloseCb& closeCb, const Uint32& winFlags, const Sizef& winSize ) { +UIColorPicker* UIColorPicker::NewModal( Node* nodeCreator, const UIColorPicker::ColorPickedCb& colorPickedCb, const Uint8& modalAlpha, const Uint32& winFlags, const Sizef& winSize ) { + UIColorPicker * colorPicker = NewWindow( colorPickedCb, winFlags, winSize, modalAlpha ); + UIWindow * pickerWin = colorPicker->getUIWindow(); + Sizef windowSize( pickerWin->getSceneNode()->getSize() ); + Sizef nodeSize( nodeCreator->getSize() ); + Vector2f nodePos( nodeCreator->getPosition() ); + nodeCreator->getParent()->nodeToWorld( nodePos ); + nodePos = PixelDensity::pxToDp( nodePos ); + + + if ( nodePos.y + nodeSize.getHeight() + winSize.getHeight() <= windowSize.getHeight() && + nodePos.x + winSize.getWidth() <= windowSize.getWidth() ) { + pickerWin->setPosition( nodePos.x, nodePos.y + nodeSize.getHeight() ); + } else if ( nodePos.y - winSize.getHeight() >= 0.f && + nodePos.x + winSize.getWidth() <= windowSize.getWidth() ) { + pickerWin->setPosition( nodePos.x, nodePos.y - winSize.getHeight() ); + } else if ( nodePos.y + nodeSize.getHeight() / 2 - winSize.getHeight() / 2 >= 0 && + nodePos.y + nodeSize.getHeight() / 2 - winSize.getHeight() / 2 + winSize.getHeight() <= windowSize.getHeight() ) { + if ( nodePos.x - winSize.getWidth() >= 0 ) { + pickerWin->setPosition( nodePos.x - winSize.getWidth(), nodePos.y + nodeSize.getHeight() / 2 - winSize.getHeight() / 2 ); + } else if ( nodePos.x + nodeSize.getWidth() + winSize.getWidth() <= windowSize.getWidth() ) { + pickerWin->setPosition( nodePos.x + nodeSize.getWidth(), nodePos.y + nodeSize.getHeight() / 2 - winSize.getHeight() / 2 ); + } else { + pickerWin->center(); + } + } else { + pickerWin->center(); + } + + return colorPicker; +} + +UIColorPicker* UIColorPicker::NewWindow( const ColorPickedCb& colorPickedCb, const Uint32& winFlags, const Sizef& winSize, const Uint8& modalAlpha ) { UIWindow * tWin = UIWindow::New(); tWin->setSizeWithDecoration( winSize )->setPosition( 0, 0 ); UIWindow::StyleConfig windowStyleConfig = tWin->getStyleConfig(); windowStyleConfig.WinFlags = winFlags; windowStyleConfig.MinWindowSize = winSize; tWin->setStyleConfig( windowStyleConfig ); - UIColorPicker * colorPicker = Tools::UIColorPicker::New( tWin, colorPickedCb, closeCb ); + UIColorPicker * colorPicker = Tools::UIColorPicker::New( tWin, colorPickedCb, modalAlpha ); tWin->show(); return colorPicker; } -UIColorPicker* UIColorPicker::New( UIWindow* attachTo, const UIColorPicker::ColorPickedCb& colorPickedCb, const UIColorPicker::ColorPickerCloseCb& closeCb ) { - return eeNew( UIColorPicker, ( attachTo, colorPickedCb, closeCb ) ); +UIColorPicker* UIColorPicker::New( UIWindow* attachTo, const UIColorPicker::ColorPickedCb& colorPickedCb, const Uint8& modalAlpha ) { + return eeNew( UIColorPicker, ( attachTo, colorPickedCb, modalAlpha ) ); } -UIColorPicker::UIColorPicker( UIWindow* attachTo, const UIColorPicker::ColorPickedCb& colorPickedCb, const UIColorPicker::ColorPickerCloseCb& closeCb ) : +UIColorPicker::UIColorPicker(UIWindow* attachTo, const UIColorPicker::ColorPickedCb& colorPickedCb, const Uint8& modalAlpha ) : mUIWindow( attachTo ), mUIContainer( NULL ), mRoot( NULL ), mPickedCb( colorPickedCb ), - mCloseCb( closeCb ), mColorPicker( NULL ), mHuePicker( NULL ), mVerticalLine( NULL ), @@ -48,6 +80,7 @@ UIColorPicker::UIColorPicker( UIWindow* attachTo, const UIColorPicker::ColorPick mHsv(0, 1, 1, 1), mRgb(Color::fromHsv(mHsv)), mHexColor(mRgb.toHexString( false )), + mModalAlpha(modalAlpha), mUpdating(false) { if ( NULL == mUIWindow ) { @@ -173,6 +206,18 @@ UIColorPicker::UIColorPicker( UIWindow* attachTo, const UIColorPicker::ColorPick mUIContainer->addEventListener( Event::OnClose, cb::Make1( this, &UIColorPicker::windowClose ) ); } + if ( mUIWindow->isModal() ) { + if ( mModalAlpha != 0.f ) { + mUIWindow->getModalControl()->setBackgroundColor( Color( 0, 0, 0, mModalAlpha ) ); + mUIWindow->getModalControl()->runAction( Actions::Fade::New( 0.f, mModalAlpha, UIThemeManager::instance()->getControlsFadeOutTime() ) ); + } + mUIWindow->getModalControl()->addEventListener( Event::MouseClick, [&] ( const Event* event ) { + if ( mModalAlpha != 0.f ) + mUIWindow->getModalControl()->runAction( Actions::FadeOut::New( UIThemeManager::instance()->getControlsFadeOutTime() ) ); + mUIWindow->closeWindow(); + } ); + } + mRoot->bind( "color_picker_rect", mColorPicker ); mRoot->bind( "hue_picker", mHuePicker ); mRoot->bind( "vertical_line", mVerticalLine ); @@ -227,10 +272,15 @@ UIWindow* UIColorPicker::getUIWindow() const { return mUIWindow; } -void UIColorPicker::windowClose( const Event * ) { - if ( mCloseCb ) - mCloseCb(); +Uint8 UIColorPicker::getModalAlpha() const { + return mModalAlpha; +} +void UIColorPicker::setModalAlpha( const Uint8& modalAlpha ) { + mModalAlpha = modalAlpha; +} + +void UIColorPicker::windowClose( const Event * ) { eeDelete( this ); } diff --git a/src/eepp/ui/uiwindow.cpp b/src/eepp/ui/uiwindow.cpp index 39fec3392..fd58c3848 100644 --- a/src/eepp/ui/uiwindow.cpp +++ b/src/eepp/ui/uiwindow.cpp @@ -126,7 +126,7 @@ void UIWindow::updateWinFlags() { setDragEnabled( false ); } - if ( !( mStyleConfig.WinFlags & UI_WIN_NO_BORDER ) ) { + if ( !( mStyleConfig.WinFlags & UI_WIN_NO_DECORATION ) ) { if ( NULL == mWindowDecoration ) { mWindowDecoration = UINode::New(); mWindowDecoration->writeNodeFlag( NODE_FLAG_OWNED_BY_NODE, 1 ); @@ -381,49 +381,6 @@ void UIWindow::createModalControl() { mModalCtrl->setSize( Ctrl->getSize() ); mModalCtrl->updateAnchorsDistances(); } - - mModalCtrl->setEnabled( false ); - mModalCtrl->setVisible( false ); - - disableByModal(); -} - -void UIWindow::enableByModal() { - if ( isModal() && NULL != mSceneNode ) { - Node * CtrlChild = mSceneNode->getFirstChild(); - - while ( NULL != CtrlChild ) - { - if ( CtrlChild != mModalCtrl && - CtrlChild != this && - CtrlChild->getNodeFlags() & NODE_FLAG_DISABLED_BY_NODE ) - { - CtrlChild->setEnabled( true ); - CtrlChild->writeNodeFlag( NODE_FLAG_DISABLED_BY_NODE, 0 ); - } - - CtrlChild = CtrlChild->getNextNode(); - } - } -} - -void UIWindow::disableByModal() { - if ( isModal() && NULL != mSceneNode ) { - Node * CtrlChild = mSceneNode->getFirstChild(); - - while ( NULL != CtrlChild ) - { - if ( CtrlChild != mModalCtrl && - CtrlChild != this && - CtrlChild->isEnabled() ) - { - CtrlChild->setEnabled( false ); - CtrlChild->writeNodeFlag( NODE_FLAG_DISABLED_BY_NODE, 1 ); - } - - CtrlChild = CtrlChild->getNextNode(); - } - } } Uint32 UIWindow::getType() const { @@ -444,11 +401,6 @@ void UIWindow::closeWindow() { if ( NULL != mButtonMinimize ) mButtonMinimize->setEnabled( false ); - if ( NULL != mModalCtrl ) { - mModalCtrl->close(); - mModalCtrl = NULL; - } - if ( Time::Zero != UIThemeManager::instance()->getControlsFadeOutTime() ) runAction( Actions::Sequence::New( Actions::FadeOut::New( UIThemeManager::instance()->getControlsFadeOutTime() ), Actions::Close::New() ) ); else @@ -458,7 +410,12 @@ void UIWindow::closeWindow() { void UIWindow::close() { UIWidget::close(); - enableByModal(); + if ( NULL != mModalCtrl ) { + mModalCtrl->setEnabled( false ); + mModalCtrl->setVisible( false ); + mModalCtrl->close(); + mModalCtrl = NULL; + } } void UIWindow::setTheme( UITheme * Theme ) { @@ -467,7 +424,7 @@ void UIWindow::setTheme( UITheme * Theme ) { if ( NULL != mContainer ) mContainer->setThemeSkin ( Theme, "winback" ); - if ( !( mStyleConfig.WinFlags & UI_WIN_NO_BORDER ) ) { + if ( !( mStyleConfig.WinFlags & UI_WIN_NO_DECORATION ) ) { mWindowDecoration->setThemeSkin ( Theme, "windeco" ); mBorderLeft->setThemeSkin ( Theme, "winborderleft" ); mBorderRight->setThemeSkin ( Theme, "winborderright" ); @@ -977,6 +934,18 @@ UINode * UIWindow::getButtonMinimize() const { return mButtonMinimize; } +void UIWindow::setupModal() { + if ( isModal() ) { + createModalControl(); + + mModalCtrl->setEnabled( true ); + mModalCtrl->setVisible( true ); + mModalCtrl->toFront(); + + toFront(); + } +} + bool UIWindow::show() { if ( !isVisible() ) { setEnabled( true ); @@ -986,19 +955,13 @@ bool UIWindow::show() { runAction( Actions::Fade::New( mStyleConfig.BaseAlpha == getAlpha() ? 0.f : mAlpha, mStyleConfig.BaseAlpha, UIThemeManager::instance()->getControlsFadeOutTime() ) ); - if ( isModal() ) { - createModalControl(); - - mModalCtrl->setEnabled( true ); - mModalCtrl->setVisible( true ); - mModalCtrl->toFront(); - - toFront(); - } + setupModal(); return true; } + setupModal(); + return false; } @@ -1089,7 +1052,7 @@ void UIWindow::setTitle( const String& text ) { mTitle->setHorizontalAlign( getHorizontalAlign() ); mTitle->setVerticalAlign( getVerticalAlign() ); mTitle->setEnabled( false ); - mTitle->setVisible( !( mStyleConfig.WinFlags & UI_WIN_NO_BORDER ) ); + mTitle->setVisible( !( mStyleConfig.WinFlags & UI_WIN_NO_DECORATION ) ); } fixTitleSize(); @@ -1364,7 +1327,7 @@ UIWidget * UIWindow::getModalControl() const { void UIWindow::resizeCursor() { UISceneNode * sceneNode = getUISceneNode(); - if ( NULL == sceneNode || !isMouseOverMeOrChilds() || !sceneNode->getUseGlobalCursors() || ( mStyleConfig.WinFlags & UI_WIN_NO_BORDER ) || !isResizeable() ) + if ( NULL == sceneNode || !isMouseOverMeOrChilds() || !sceneNode->getUseGlobalCursors() || ( mStyleConfig.WinFlags & UI_WIN_NO_DECORATION ) || !isResizeable() ) return; EventDispatcher * eventDispatcher = sceneNode->getEventDispatcher(); @@ -1447,7 +1410,7 @@ bool UIWindow::setAttribute( const NodeAttribute& attribute, const Uint32& state else if ( "dragable" == cur ) winflags |= UI_WIN_DRAGABLE_CONTAINER; else if ( "shadow" == cur ) winflags |= UI_WIN_SHADOW; else if ( "modal" == cur ) winflags |= UI_WIN_MODAL; - else if ( "noborder" == cur || "borderless" == cur ) winflags |= UI_WIN_NO_BORDER; + else if ( "noborder" == cur || "borderless" == cur ) winflags |= UI_WIN_NO_DECORATION; else if ( "resizeable" == cur ) winflags |= UI_WIN_RESIZEABLE; else if ( "sharealpha" == cur ) winflags |= UI_WIN_SHARE_ALPHA_WITH_CHILDS; else if ( "buttonactions" == cur ) winflags |= UI_WIN_USE_DEFAULT_BUTTONS_ACTIONS; diff --git a/src/test/eetest.cpp b/src/test/eetest.cpp index f166f94ca..a7454ae22 100644 --- a/src/test/eetest.cpp +++ b/src/test/eetest.cpp @@ -692,6 +692,9 @@ void EETest::createNewUI() { pushButton->setPosition( 50, 560 )->setSize( 200, 0 )->setParent( container ); pushButton->setText( "PushButton" ); pushButton->setIcon( mTheme->getIconByName( "ok" ) ); + pushButton->addEventListener( Event::MouseClick, [&, pushButton] ( const Event* event ) { + createColorPicker( pushButton ); + } ); UISprite * sprite = UISprite::New(); sprite->setFlags( UI_AUTO_SIZE ); @@ -932,13 +935,13 @@ void EETest::createETGEditor() { tWin->show(); } -void EETest::createColorPicker() { - mColorPicker = Tools::UIColorPicker::NewWindow( [&](Color color) { +void EETest::createColorPicker( Node * node ) { + mColorPicker = Tools::UIColorPicker::NewModal( node, [&](Color color) { UIMessageBox * msgBox = UIMessageBox::New( UIMessageBox::OK, color.toHexString() ); msgBox->center(); msgBox->show(); - }, [&] { mColorPicker = NULL; } ); - mColorPicker->getUIWindow()->center(); + } ); + //mColorPicker->getUIWindow()->center(); } void EETest::createCommonDialog() { @@ -1003,10 +1006,10 @@ void EETest::createDecoratedWindow() { const String& txt = menuItem->getText(); if ( "Hide Border" == txt ) { - win->setWinFlags( win->getWinFlags() | UI_WIN_NO_BORDER ); + win->setWinFlags( win->getWinFlags() | UI_WIN_NO_DECORATION ); menuItem->setText( "Show Border" ); } else if ( "Show Border" == txt ) { - win->setWinFlags( win->getWinFlags() & ~UI_WIN_NO_BORDER ); + win->setWinFlags( win->getWinFlags() & ~UI_WIN_NO_DECORATION ); menuItem->setText( "Hide Border" ); } else if ( "Close" == txt ) { win->closeWindow(); @@ -1061,11 +1064,11 @@ void EETest::onCloseClick( const Event * ) { mUIWindow = NULL; } -void EETest::onItemClick( const Event * Event ) { - if ( !Event->getNode()->isType( UI_TYPE_MENUITEM ) ) +void EETest::onItemClick( const Event * event ) { + if ( !event->getNode()->isType( UI_TYPE_MENUITEM ) ) return; - const String& txt = Event->getNode()->asType()->getText(); + const String& txt = event->getNode()->asType()->getText(); if ( "Show Screen 1" == txt ) { setScreen( 0 ); @@ -1088,7 +1091,7 @@ void EETest::onItemClick( const Event * Event ) { mWindow->stopTextInput(); } } else if ( "Show Window" == txt ) { - UIMenuCheckBox * Chk = Event->getNode()->asType(); + UIMenuCheckBox * Chk = event->getNode()->asType(); C->toFront(); C->setVisible( true ); @@ -1122,7 +1125,7 @@ void EETest::onItemClick( const Event * Event ) { } else if ( "Texture Atlas Editor" == txt ) { createETGEditor(); } else if ( "Color Picker" == txt ) { - createColorPicker(); + createColorPicker( event->getNode() ); } else if ( "Multi Viewport" == txt ) { MultiViewportMode = !MultiViewportMode; } else if ( "Open..." == txt ) { diff --git a/src/test/eetest.hpp b/src/test/eetest.hpp index 674228ff8..caaa43e03 100644 --- a/src/test/eetest.hpp +++ b/src/test/eetest.hpp @@ -237,7 +237,7 @@ class EETest : private Thread { void createMapEditor(); - void createColorPicker(); + void createColorPicker( Node * node ); void onMapEditorClose();