From 427c8e2ff159bc04a06ae558d62ff0e65b4a9832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=C2=ADn=20Lucas=20Golini?= Date: Tue, 11 Apr 2017 14:28:10 -0300 Subject: [PATCH] UIMessageBox with layouts. LinearLayout fixes. Some minor refactor. --HG-- branch : dev --- include/eepp/ui/tools/textureatlaseditor.hpp | 2 - include/eepp/ui/uicontrol.hpp | 2 + include/eepp/ui/uiwidget.hpp | 2 + include/eepp/ui/uiwidgetcreator.hpp | 6 +- src/eepp/ui/tools/textureatlaseditor.cpp | 10 --- src/eepp/ui/uicontrol.cpp | 8 +++ src/eepp/ui/uilinearlayout.cpp | 10 ++- src/eepp/ui/uimanager.cpp | 2 +- src/eepp/ui/uimessagebox.cpp | 71 +++++++++----------- src/eepp/ui/uiwidget.cpp | 8 +++ src/eepp/ui/uiwidgetcreator.cpp | 68 +++++++++---------- 11 files changed, 95 insertions(+), 94 deletions(-) diff --git a/include/eepp/ui/tools/textureatlaseditor.hpp b/include/eepp/ui/tools/textureatlaseditor.hpp index 2c82e3495..399365b85 100644 --- a/include/eepp/ui/tools/textureatlaseditor.hpp +++ b/include/eepp/ui/tools/textureatlaseditor.hpp @@ -67,8 +67,6 @@ class EE_API TextureAtlasEditor { void onSubTextureChange( const UIEvent * Event ); - UITextView * createTextBox( Vector2i Pos, const String& Text ); - void updateControls(); void fillSubTextureList(); diff --git a/include/eepp/ui/uicontrol.hpp b/include/eepp/ui/uicontrol.hpp index 396a06d2c..68353c5b1 100644 --- a/include/eepp/ui/uicontrol.hpp +++ b/include/eepp/ui/uicontrol.hpp @@ -106,6 +106,8 @@ class EE_API UIControl { UIControl * setVerticalAlign( Uint32 valign ); + UIControl * setGravity( Uint32 hvalign ); + UIBackground * setBackgroundFillEnabled( bool enabled ); UIBorder * setBorderEnabled( bool enabled ); diff --git a/include/eepp/ui/uiwidget.hpp b/include/eepp/ui/uiwidget.hpp index 6eb3fe34c..b241a59b1 100644 --- a/include/eepp/ui/uiwidget.hpp +++ b/include/eepp/ui/uiwidget.hpp @@ -106,6 +106,8 @@ class EE_API UIWidget : public UIControlAnim { void notifyLayoutAttrChange(); + void notifyLayoutAttrChangeParent(); + void updateAnchors( const Vector2i & SizeChange ); void alignAgainstLayout(); diff --git a/include/eepp/ui/uiwidgetcreator.hpp b/include/eepp/ui/uiwidgetcreator.hpp index a0d10e3fd..4764a53be 100644 --- a/include/eepp/ui/uiwidgetcreator.hpp +++ b/include/eepp/ui/uiwidgetcreator.hpp @@ -8,11 +8,11 @@ namespace EE { namespace UI { class EE_API UIWidgetCreator { public: - typedef cb::Callback1 CreateUIWidgetCb; + typedef cb::Callback1 CustomWidgetCb; - static UIWidget * createUIWidgetFromName( std::string name ); + static UIWidget * createFromName( std::string widgetName ); - static void addCustomWidgetCallback( std::string widgetName, const CreateUIWidgetCb& cb ); + static void addCustomWidgetCallback( std::string widgetName, const CustomWidgetCb& cb ); static void removeCustomWidgetCallback( std::string widgetName ); diff --git a/src/eepp/ui/tools/textureatlaseditor.cpp b/src/eepp/ui/tools/textureatlaseditor.cpp index a42e7b14a..f0015b0d9 100644 --- a/src/eepp/ui/tools/textureatlaseditor.cpp +++ b/src/eepp/ui/tools/textureatlaseditor.cpp @@ -206,16 +206,6 @@ void TextureAtlasEditor::onDestHChange( const UIEvent * Event ) { } } -UITextView * TextureAtlasEditor::createTextBox( Vector2i Pos, const String& Text ) { - UITextView * txtBox = UITextView::New(); - txtBox->setFontStyle( Text::Shadow ); - txtBox->resetFlags( UI_CONTROL_DEFAULT_ALIGN | UI_ANCHOR_TOP | UI_ANCHOR_RIGHT | UI_AUTO_SIZE ); - txtBox->setParent( mUIContainer ); - txtBox->setPosition( Pos ); - txtBox->setText( Text ); - return txtBox; -} - void TextureAtlasEditor::windowClose( const UIEvent * Event ) { if ( mCloseCb.IsSet() ) mCloseCb(); diff --git a/src/eepp/ui/uicontrol.cpp b/src/eepp/ui/uicontrol.cpp index 487d830ca..cd2494a99 100644 --- a/src/eepp/ui/uicontrol.cpp +++ b/src/eepp/ui/uicontrol.cpp @@ -553,6 +553,14 @@ UIControl * UIControl::setVerticalAlign( Uint32 valign ) { return this; } +UIControl * UIControl::setGravity( Uint32 hvalign ) { + mFlags &= ~( UI_VALIGN_MASK | UI_HALIGN_MASK ); + mFlags |= ( hvalign & ( UI_VALIGN_MASK | UI_HALIGN_MASK ) ) ; + + onAlignChange(); + return this; +} + UIBackground * UIControl::setBackgroundFillEnabled( bool enabled ) { writeFlag( UI_FILL_BACKGROUND, enabled ? 1 : 0 ); diff --git a/src/eepp/ui/uilinearlayout.cpp b/src/eepp/ui/uilinearlayout.cpp index 57d78e2cf..347b87481 100644 --- a/src/eepp/ui/uilinearlayout.cpp +++ b/src/eepp/ui/uilinearlayout.cpp @@ -78,7 +78,7 @@ void UILinearLayout::packVertical() { UIControl * ChildLoop = mChild; while ( NULL != ChildLoop ) { - if ( ChildLoop->isWidget() ) { + if ( ChildLoop->isWidget() && ChildLoop->isVisible() ) { UIWidget * widget = static_cast( ChildLoop ); if ( widget->getLayoutHeightRules() == WRAP_CONTENT ) { @@ -153,6 +153,7 @@ void UILinearLayout::packVertical() { if ( getLayoutHeightRules() == WRAP_CONTENT ) { setInternalHeight( curY ); + notifyLayoutAttrChangeParent(); } else if ( getLayoutHeightRules() == MATCH_PARENT ) { setInternalHeight( getParent()->getSize().getHeight() - mLayoutMargin.Top - mLayoutMargin.Bottom ); } @@ -160,6 +161,7 @@ void UILinearLayout::packVertical() { if ( getLayoutWidthRules() == WRAP_CONTENT && mSize.getWidth() != maxX ) { setInternalWidth( maxX ); packVertical(); + notifyLayoutAttrChangeParent(); } alignAgainstLayout(); @@ -212,7 +214,7 @@ void UILinearLayout::packHorizontal() { ChildLoop = mChild; while ( NULL != ChildLoop ) { - if ( ChildLoop->isWidget() ) { + if ( ChildLoop->isWidget() && ChildLoop->isVisible() ) { UIWidget * widget = static_cast( ChildLoop ); Recti margin = widget->getLayoutMargin(); @@ -236,7 +238,7 @@ void UILinearLayout::packHorizontal() { break; case UI_VALIGN_TOP: default: - pos.y = widget->getLayoutMargin().Left; + pos.y = widget->getLayoutMargin().Top; break; } @@ -252,6 +254,7 @@ void UILinearLayout::packHorizontal() { if ( getLayoutWidthRules() == WRAP_CONTENT ) { setInternalWidth( curX ); + notifyLayoutAttrChangeParent(); } else if ( getLayoutWidthRules() == MATCH_PARENT ) { setInternalWidth( getParent()->getSize().getWidth() - mLayoutMargin.Left - mLayoutMargin.Right ); } @@ -259,6 +262,7 @@ void UILinearLayout::packHorizontal() { if ( getLayoutHeightRules() == WRAP_CONTENT && mSize.getHeight() != maxY ) { setInternalHeight( maxY ); packHorizontal(); + notifyLayoutAttrChangeParent(); } alignAgainstLayout(); diff --git a/src/eepp/ui/uimanager.cpp b/src/eepp/ui/uimanager.cpp index ceb67b303..4f73d8161 100644 --- a/src/eepp/ui/uimanager.cpp +++ b/src/eepp/ui/uimanager.cpp @@ -511,7 +511,7 @@ void UIManager::loadLayoutNodes( pugi::xml_node node, UIControl * parent ) { parent = getMainControl(); for ( pugi::xml_node widget = node; widget; widget = widget.next_sibling() ) { - UIWidget * uiwidget = UIWidgetCreator::createUIWidgetFromName( widget.name() ); + UIWidget * uiwidget = UIWidgetCreator::createFromName( widget.name() ); if ( NULL != uiwidget ) { uiwidget->setParent( parent ); diff --git a/src/eepp/ui/uimessagebox.cpp b/src/eepp/ui/uimessagebox.cpp index 7522bdef1..fb6e3fb83 100644 --- a/src/eepp/ui/uimessagebox.cpp +++ b/src/eepp/ui/uimessagebox.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace EE { namespace UI { @@ -12,42 +13,34 @@ UIMessageBox::UIMessageBox( UI_MSGBOX_TYPE type , String message ) : mMsgBoxType( type ), mCloseWithKey( KEY_UNKNOWN ) { - setInternalSize( Sizei( 1024, 1024 ) ); - - mTextBox = UITextView::New(); - mTextBox->setParent( getContainer() ) - ->setHorizontalAlign( UI_HALIGN_CENTER ) - ->setVerticalAlign( UI_VALIGN_CENTER ); - mTextBox->setText( message ); - - mButtonOK = UIPushButton::New(); - mButtonOK->setParent( getContainer() ) - ->setFlags( UI_AUTO_SIZE ) - ->setSize( 90, 0 ) - ->setPosition( getContainer()->getSize().getWidth() - 96, getContainer()->getSize().getHeight() - mButtonOK->getSize().getHeight() - 8 ); - mButtonOK->setAnchors( UI_ANCHOR_RIGHT ); - - mButtonCancel = UIPushButton::New(); - mButtonCancel->setParent( getContainer() ) - ->setFlags( UI_AUTO_SIZE ) - ->setSize( 90, 0 ) - ->setPosition( mButtonOK->getPosition().x - mButtonOK->getSize().getWidth() - 8, getContainer()->getSize().getHeight() - mButtonOK->getSize().getHeight() - 8 ); - mButtonCancel->setAnchors( UI_ANCHOR_RIGHT ); - - applyDefaultTheme(); - - mTextBox->setPosition( 0, 8 ); - mTextBox->setSize( PixelDensity::pxToDpI( mTextBox->getTextWidth() ) + 24, mTextBox->getTextHeight() ); - setSize( mTextBox->getSize().getWidth() + 24, mTextBox->getSize().getHeight() + mButtonOK->getSize().getHeight() + mStyleConfig.DecorationSize.getHeight() + 16 ); - mTextBox->centerHorizontal(); - mTextBox->setAnchors( UI_ANCHOR_LEFT | UI_ANCHOR_TOP | UI_ANCHOR_RIGHT ); - - setMinWindowSize( getSize() ); - mStyleConfig.WinFlags &= ~UI_WIN_RESIZEABLE; updateWinFlags(); + UILinearLayout * rlay = UILinearLayout::New(); + rlay->setLayoutSizeRules( WRAP_CONTENT, WRAP_CONTENT )->setParent( mContainer ); + + UILinearLayout * vlay = UILinearLayout::NewVertical(); + vlay->setLayoutSizeRules( WRAP_CONTENT, WRAP_CONTENT ) + ->setLayoutMargin( Recti( 8, 8, 8, 8) )->setParent( rlay ); + + mTextBox = UITextView::New(); + mTextBox->setText( message ) + ->setLayoutSizeRules( WRAP_CONTENT, WRAP_CONTENT ) + ->setParent( vlay ); + + UILinearLayout * hlay = UILinearLayout::NewHorizontal(); + hlay->setLayoutMargin( Recti( 0, 8, 0, 0 ) ) + ->setLayoutSizeRules( WRAP_CONTENT, WRAP_CONTENT ) + ->setLayoutGravity( UI_HALIGN_RIGHT | UI_VALIGN_CENTER ) + ->setParent( vlay ); + + mButtonOK = UIPushButton::New(); + mButtonOK->setSize( 90, 0 )->setParent( hlay ); + + mButtonCancel = UIPushButton::New(); + mButtonCancel->setLayoutMargin( Recti( 8, 0, 0, 0 ) )->setSize( 90, 0 )->setParent( hlay ); + switch ( mMsgBoxType ) { case MSGBOX_OKCANCEL: { @@ -76,8 +69,9 @@ UIMessageBox::UIMessageBox( UI_MSGBOX_TYPE type , String message ) : } } - mButtonCancel->toFront(); - mButtonOK->toFront(); + applyDefaultTheme(); + + setMinWindowSize( rlay->getSize() ); } UIMessageBox::~UIMessageBox() { @@ -98,9 +92,6 @@ void UIMessageBox::setTheme( UITheme * Theme ) { mButtonCancel->setIcon( CancelIcon ); } } - - mButtonOK->setPosition( mButtonOK->getPosition().x, getContainer()->getSize().getHeight() - mButtonOK->getSize().getHeight() - 8 ); - mButtonCancel->setPosition( mButtonCancel->getPosition().x, mButtonOK->getPosition().y ); } Uint32 UIMessageBox::onMessage( const UIMessage * Msg ) { @@ -155,13 +146,11 @@ bool UIMessageBox::show() { return b; } -Uint32 UIMessageBox::getCloseWithKey() const -{ +Uint32 UIMessageBox::getCloseWithKey() const { return mCloseWithKey; } -void UIMessageBox::setCloseWithKey(const Uint32 & closeWithKey) -{ +void UIMessageBox::setCloseWithKey(const Uint32 & closeWithKey) { mCloseWithKey = closeWithKey; } diff --git a/src/eepp/ui/uiwidget.cpp b/src/eepp/ui/uiwidget.cpp index 786ff5313..f7302f113 100644 --- a/src/eepp/ui/uiwidget.cpp +++ b/src/eepp/ui/uiwidget.cpp @@ -266,6 +266,7 @@ void UIWidget::onPositionChange() { void UIWidget::onVisibilityChange() { updateAnchorsDistances(); + notifyLayoutAttrChange(); UIControlAnim::onVisibilityChange(); } @@ -277,6 +278,13 @@ void UIWidget::notifyLayoutAttrChange() { messagePost( &msg ); } +void UIWidget::notifyLayoutAttrChangeParent() { + if ( NULL != mParentCtrl ) { + UIMessage msg( this, UIMessage::MsgLayoutAttributeChange ); + mParentCtrl->messagePost( &msg ); + } +} + void UIWidget::updateAnchors( const Vector2i& SizeChange ) { if ( !( mFlags & ( UI_ANCHOR_LEFT | UI_ANCHOR_TOP | UI_ANCHOR_RIGHT | UI_ANCHOR_BOTTOM ) ) ) return; diff --git a/src/eepp/ui/uiwidgetcreator.cpp b/src/eepp/ui/uiwidgetcreator.cpp index f390da238..a20622aee 100644 --- a/src/eepp/ui/uiwidgetcreator.cpp +++ b/src/eepp/ui/uiwidgetcreator.cpp @@ -28,79 +28,79 @@ namespace EE { namespace UI { -typedef std::map widgetCallbackMap; +typedef std::map widgetCallbackMap; static widgetCallbackMap widgetCallback; -UIWidget * UIWidgetCreator::createUIWidgetFromName( std::string name ) { - String::toLowerInPlace( name ); +UIWidget * UIWidgetCreator::createFromName( std::string widgetName ) { + String::toLowerInPlace( widgetName ); - if ( name == "widget" ) { + if ( widgetName == "widget" ) { return UIWidget::New(); - } else if ( name == "horizontallinearlayout" || name == "hll" ) { + } else if ( widgetName == "horizontallinearlayout" || widgetName == "hll" ) { return UILinearLayout::NewHorizontal(); - } else if ( name == "linearlayout" || name == "verticallinearlayout" || name == "vll" ) { + } else if ( widgetName == "linearlayout" || widgetName == "verticallinearlayout" || widgetName == "vll" ) { return UILinearLayout::NewVertical(); - } else if ( name == "relativelayout" ) { + } else if ( widgetName == "relativelayout" ) { return UIRelativeLayout::New(); - } else if ( name == "textview" ) { + } else if ( widgetName == "textview" ) { return UITextView::New(); - } else if ( name == "pushbutton" ) { + } else if ( widgetName == "pushbutton" ) { return UIPushButton::New(); - } else if ( name == "checkbox" ) { + } else if ( widgetName == "checkbox" ) { return UICheckBox::New(); - } else if ( name == "radiobutton" ) { + } else if ( widgetName == "radiobutton" ) { return UIRadioButton::New(); - } else if ( name == "combobox" ) { + } else if ( widgetName == "combobox" ) { return UIComboBox::New(); - } else if ( name == "dropdownlist" ) { + } else if ( widgetName == "dropdownlist" ) { return UIDropDownList::New(); - } else if ( name == "image" ) { + } else if ( widgetName == "image" ) { return UISubTexture::New(); - } else if ( name == "listbox" ) { + } else if ( widgetName == "listbox" ) { return UIListBox::New(); - } else if ( name == "winmenu" ) { + } else if ( widgetName == "winmenu" ) { return UIWinMenu::New(); - } else if ( name == "progressbar" ) { + } else if ( widgetName == "progressbar" ) { return UIProgressBar::New(); - } else if ( name == "scrollbar" ) { + } else if ( widgetName == "scrollbar" ) { return UIScrollBar::New(); - } else if ( name == "slider" ) { + } else if ( widgetName == "slider" ) { return UISlider::New(); - } else if ( name == "spinbox" ) { + } else if ( widgetName == "spinbox" ) { return UISpinBox::New(); - } else if ( name == "sprite" ) { + } else if ( widgetName == "sprite" ) { return UISprite::New(); - } else if ( name == "tab" ) { + } else if ( widgetName == "tab" ) { return UITab::New(); - } else if ( name == "table" ) { + } else if ( widgetName == "table" ) { return UITable::New(); - } else if ( name == "tablecell" ) { + } else if ( widgetName == "tablecell" ) { return UITableCell::New(); - } else if ( name == "tabwidget" ) { + } else if ( widgetName == "tabwidget" ) { return UITabWidget::New(); - } else if ( name == "textedit" ) { + } else if ( widgetName == "textedit" ) { return UITextEdit::New(); - } else if ( name == "textinput" || name == "input" ) { + } else if ( widgetName == "textinput" || widgetName == "input" ) { return UITextInput::New(); - } else if ( name == "textinputpassword" || name == "inputpassword" ) { + } else if ( widgetName == "textinputpassword" || widgetName == "inputpassword" ) { return UITextInputPassword::New(); - } else if ( name == "loader" ) { + } else if ( widgetName == "loader" ) { return UILoader::New(); - } else if ( name == "selectbutton" ) { + } else if ( widgetName == "selectbutton" ) { return UISelectButton::New(); - } else if ( name == "window" ) { + } else if ( widgetName == "window" ) { return UIWindow::New(); } - if ( widgetCallback.find( name ) != widgetCallback.end() ) { - return widgetCallback[ name ].Call( name ); + if ( widgetCallback.find( widgetName ) != widgetCallback.end() ) { + return widgetCallback[ widgetName ].Call( widgetName ); } return NULL; } -void UIWidgetCreator::addCustomWidgetCallback( std::string widgetName, const UIWidgetCreator::CreateUIWidgetCb& cb ) { +void UIWidgetCreator::addCustomWidgetCallback( std::string widgetName, const UIWidgetCreator::CustomWidgetCb& cb ) { widgetCallback[ String::toLower( widgetName ) ] = cb; }