From 5c0ad1754c6844c27abed6234028d0e9ef37fbff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Mon, 10 Dec 2018 03:02:28 -0300 Subject: [PATCH] More padding work. --HG-- branch : dev-widget-padding --- bin/assets/layouts/test.xml | 8 +- include/eepp/ui/uipushbutton.hpp | 10 +-- projects/linux/ee.includes | 1 + src/eepp/ui/uipushbutton.cpp | 123 ++++++++++++++++++------------- src/eepp/ui/uisprite.cpp | 26 +++++-- src/eepp/ui/uitextureregion.cpp | 35 +++++---- src/eepp/ui/uiwinmenu.cpp | 3 +- 7 files changed, 126 insertions(+), 80 deletions(-) diff --git a/bin/assets/layouts/test.xml b/bin/assets/layouts/test.xml index 9cb6600fa..dbb314487 100644 --- a/bin/assets/layouts/test.xml +++ b/bin/assets/layouts/test.xml @@ -23,9 +23,13 @@ - - + + + + + diff --git a/include/eepp/ui/uipushbutton.hpp b/include/eepp/ui/uipushbutton.hpp index e434ff417..1fc67f832 100644 --- a/include/eepp/ui/uipushbutton.hpp +++ b/include/eepp/ui/uipushbutton.hpp @@ -29,10 +29,6 @@ class EE_API UIPushButton : public UIWidget { virtual const String& getText(); - void setPadding( const Rectf& padding ); - - const Rectf& getPadding() const; - void setIconHorizontalMargin( Int32 margin ); const Int32& getIconHorizontalMargin() const; @@ -83,8 +79,6 @@ class EE_API UIPushButton : public UIWidget { virtual void onSizeChange(); - void autoPadding(); - virtual void onAlphaChange(); virtual void onStateChange(); @@ -93,6 +87,10 @@ class EE_API UIPushButton : public UIWidget { virtual void onThemeLoaded(); + virtual void onAutoSize(); + + virtual void onPaddingChange(); + virtual Uint32 onKeyDown( const KeyEvent& Event ); virtual Uint32 onKeyUp( const KeyEvent& Event ); diff --git a/projects/linux/ee.includes b/projects/linux/ee.includes index 8cad23927..6d9824584 100644 --- a/projects/linux/ee.includes +++ b/projects/linux/ee.includes @@ -6,3 +6,4 @@ ../../src/thirdparty/libvorbis/include ../../src/eepp/audio ../../include/eepp/audio +/usr/include/freetype2/ diff --git a/src/eepp/ui/uipushbutton.cpp b/src/eepp/ui/uipushbutton.cpp index f48d48a81..2842d8b64 100644 --- a/src/eepp/ui/uipushbutton.cpp +++ b/src/eepp/ui/uipushbutton.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include @@ -34,8 +34,10 @@ UIPushButton::UIPushButton() : mIcon = UIImage::New(); mIcon->setParent( this ); + mIcon->setLayoutSizeRules( FIXED, FIXED ); mIcon->setFlags( GfxFlags ); mIcon->unsetFlags( UI_AUTO_SIZE ); + mIcon->setScaleType( UIScaleType::FitInside ); if ( mStyleConfig.IconMinSize.x != 0 && mStyleConfig.IconMinSize.y != 0 ) { mIcon->setSize( mStyleConfig.IconMinSize.asFloat() ); @@ -45,7 +47,7 @@ UIPushButton::UIPushButton() : mIcon->setEnabled( false ); mTextBox = UITextView::New(); - mTextBox->setLayoutSizeRules( FIXED, FIXED ); + mTextBox->setLayoutSizeRules( WRAP_CONTENT, WRAP_CONTENT ); mTextBox->setParent( this ); mTextBox->setVisible( true ); mTextBox->setEnabled( false ); @@ -70,55 +72,96 @@ bool UIPushButton::isType( const Uint32& type ) const { return UIPushButton::getType() == type ? true : UIWidget::isType( type ); } -void UIPushButton::onSizeChange() { - if ( ( mFlags & UI_AUTO_SIZE ) && NULL != getSkin() && 0 == mDpSize.getHeight() ) { +void UIPushButton::onAutoSize() { + if ( ( mFlags & UI_AUTO_SIZE ) && NULL != getSkin() ) { setInternalHeight( getSkinSize().getHeight() ); } - if ( ( mFlags & UI_AUTO_SIZE ) ) { - Int32 txtW = NULL != mTextBox ? PixelDensity::pxToDpI( mTextBox->getTextWidth() ) : 0; - Int32 minSize = txtW + ( NULL != mIcon ? mIcon->getSize().getWidth() : 0 ) - + mStyleConfig.IconHorizontalMargin + mTextBox->getPadding().Left + mTextBox->getPadding().Right + - ( NULL != getSkin() ? getSkin()->getBorderSize().Left + getSkin()->getBorderSize().Right : 0 ); + if ( ( mFlags & UI_AUTO_SIZE ) && NULL != getSkin() && ( 0 == mDpSize.getHeight() || mLayoutHeightRules == WRAP_CONTENT ) ) { + Float h = eemax( PixelDensity::dpToPx( getSkinSize().getHeight() ), mTextBox->getTextHeight() ); - if ( minSize > mDpSize.getWidth() ) { - setInternalWidth( minSize ); + setInternalPixelsHeight( h + mRealPadding.Top + mRealPadding.Bottom ); + } + + if ( ( mFlags & UI_AUTO_SIZE ) || mLayoutWidthRules == WRAP_CONTENT ) { + Int32 txtW = NULL != mTextBox ? mTextBox->getTextWidth() : 0; + + Int32 minSize = txtW + + ( NULL != mIcon ? mIcon->getRealSize().getWidth() : 0 ) + + PixelDensity::dpToPxI( mStyleConfig.IconHorizontalMargin ) + mRealPadding.Left + mRealPadding.Right + + ( NULL != getSkin() ? PixelDensity::dpToPxI( getSkin()->getBorderSize().Left + getSkin()->getBorderSize().Right ) : 0 ); + + if ( minSize > mSize.getWidth() ) { + setInternalPixelsWidth( minSize ); } } +} - if ( NULL != mTextBox ) { - mTextBox->setSize( mDpSize ); - mTextBox->setPosition( 0, 0 ); +void UIPushButton::onPaddingChange() { + onSizeChange(); + + UIWidget::onPaddingChange(); +} + +void UIPushButton::onSizeChange() { + onAutoSize(); + + Rectf autoPadding; + + if ( mFlags & UI_AUTO_PADDING ) { + autoPadding = makePadding( true, true, true, true ); } - mIcon->setPosition( mStyleConfig.IconHorizontalMargin, 0 ); + if ( mRealPadding.Top > autoPadding.Top ) autoPadding.Top = mRealPadding.Top; + if ( mRealPadding.Bottom > autoPadding.Bottom ) autoPadding.Bottom = mRealPadding.Bottom; + if ( mRealPadding.Left > autoPadding.Left ) autoPadding.Left = mRealPadding.Left; + if ( mRealPadding.Right > autoPadding.Right ) autoPadding.Right = mRealPadding.Right; + + mIcon->setPixelsPosition( autoPadding.Left + mStyleConfig.IconHorizontalMargin, 0 ); mIcon->centerVertical(); if ( NULL != mTextBox ) { + Vector2f position; + + switch ( fontVAlignGet( getFlags() ) ) { + case UI_VALIGN_CENTER: + position.y = ( mSize.getHeight() - mTextBox->getRealSize().getHeight() ) / 2; + break; + case UI_VALIGN_BOTTOM: + position.y = mSize.y - mTextBox->getRealSize().getHeight() - autoPadding.Bottom; + break; + case UI_VALIGN_TOP: + position.y = autoPadding.Top; + break; + } + switch ( fontHAlignGet( getFlags() ) ) { - case UI_HALIGN_LEFT: - mTextBox->setPosition( mIcon->getPosition().x + mIcon->getSize().getWidth(), 0 ); - mTextBox->setSize( mDpSize.getWidth() - mIcon->getPosition().x - mIcon->getSize().getWidth(), mDpSize.getHeight() ); + case UI_HALIGN_RIGHT: + position.x = mSize.getWidth() - mTextBox->getRealSize().getWidth() - autoPadding.Right; break; case UI_HALIGN_CENTER: - if ( NULL != mIcon->getDrawable() ) { - Uint32 iconPos = mIcon->getPosition().x + mIcon->getSize().getWidth(); - Uint32 txtOff = mTextBox->getPosition().x + mTextBox->getAlignOffset().x; + position.x = ( mSize.getWidth() - mTextBox->getRealSize().getWidth() ) / 2; - if ( iconPos >= txtOff) { + if ( NULL != mIcon->getDrawable() ) { + Uint32 iconPos = mIcon->getRealPosition().x + mIcon->getRealSize().getWidth(); + + if ( iconPos >= position.x ) { Float px = PixelDensity::dpToPx(1); - mTextBox->setPosition( iconPos + px, mTextBox->getPosition().y ); - - mTextBox->setSize( mDpSize.getWidth() - mIcon->getPosition().x - mIcon->getSize().getWidth() - px, mDpSize.getHeight() ); + position.x = iconPos + px; } } + break; + case UI_HALIGN_LEFT: + position.x = mIcon->getRealPosition().x + mIcon->getRealSize().getWidth(); break; } + + mTextBox->setPixelsPosition( position ); } - if ( NULL != mTextBox && 0 == mTextBox->getText().size() ) { + if ( NULL != mTextBox && mTextBox->getText().empty() ) { mIcon->center(); } } @@ -139,23 +182,11 @@ void UIPushButton::onThemeLoaded() { mStyleConfig.IconHorizontalMargin = RMargin.Left; } - if ( ( mFlags & UI_AUTO_SIZE ) && NULL != getSkin() ) { - setInternalHeight( getSkinSize().getHeight() ); - } - - autoPadding(); - - onSizeChange(); + onAutoSize(); UIWidget::onThemeLoaded(); } -void UIPushButton::autoPadding() { - if ( mFlags & UI_AUTO_PADDING ) { - mTextBox->setPadding( makePadding( true, false, true, false ) ); - } -} - UIPushButton * UIPushButton::setIcon( Drawable * Icon ) { mIcon->setDrawable( Icon ); onSizeChange(); @@ -176,14 +207,6 @@ const String& UIPushButton::getText() { return mTextBox->getText(); } -void UIPushButton::setPadding( const Rectf& padding ) { - mTextBox->setPadding( padding ); -} - -const Rectf& UIPushButton::getPadding() const { - return mTextBox->getPadding(); -} - void UIPushButton::setIconHorizontalMargin( Int32 margin ) { mStyleConfig.IconHorizontalMargin = margin; onSizeChange(); @@ -360,15 +383,15 @@ bool UIPushButton::setAttribute( const NodeAttribute& attribute ) { if ( NULL != mTheme && NULL != ( icon = mTheme->getIconByName( val ) ) ) { setIcon( icon ); - } else if ( NULL != ( icon = GlobalTextureAtlas::instance()->getByName( val ) ) ) { + } else if ( NULL != ( icon = DrawableSearcher::searchByName( val ) ) ) { setIcon( icon ); } } else { attributeSet = UIWidget::setAttribute( attribute ); } - mTextBox->setAttribute( attribute ); - mTextBox->setLayoutSizeRules( FIXED, FIXED ); + if ( !attributeSet && ( String::startsWith( name, "text" ) || String::startsWith( name, "font" ) ) ) + mTextBox->setAttribute( attribute ); return attributeSet; } diff --git a/src/eepp/ui/uisprite.cpp b/src/eepp/ui/uisprite.cpp index ddbd3cdea..6f3eb4f50 100644 --- a/src/eepp/ui/uisprite.cpp +++ b/src/eepp/ui/uisprite.cpp @@ -126,11 +126,21 @@ void UISprite::setRenderMode( const RenderMode& render ) { } void UISprite::updateSize() { - if ( mFlags & UI_AUTO_SIZE ) { - if ( NULL != mSprite ) { + if ( NULL != mSprite ) { + if ( mFlags & UI_AUTO_SIZE ) { if ( NULL != mSprite->getCurrentTextureRegion() && mSprite->getCurrentTextureRegion()->getDpSize().asFloat() != mDpSize ) setSize( mSprite->getCurrentTextureRegion()->getDpSize().asFloat() ); } + + if ( NULL != mSprite->getCurrentTextureRegion() ) { + if ( mLayoutWidthRules == WRAP_CONTENT ) { + setInternalPixelsWidth( mSprite->getCurrentTextureRegion()->getPxSize().getWidth() + mRealPadding.Left + mRealPadding.Right ); + } + + if ( mLayoutHeightRules == WRAP_CONTENT ) { + setInternalPixelsHeight( mSprite->getCurrentTextureRegion()->getPxSize().getHeight() + mRealPadding.Top + mRealPadding.Bottom ); + } + } } } @@ -141,19 +151,19 @@ void UISprite::autoAlign() { TextureRegion * tTextureRegion = mSprite->getCurrentTextureRegion(); if ( HAlignGet( mFlags ) == UI_HALIGN_CENTER ) { - mAlignOffset.x = mDpSize.getWidth() / 2 - tTextureRegion->getDpSize().getWidth() / 2; + mAlignOffset.x = ( mSize.getWidth() - tTextureRegion->getPxSize().getWidth() ) / 2; } else if ( fontHAlignGet( mFlags ) == UI_HALIGN_RIGHT ) { - mAlignOffset.x = mDpSize.getWidth() - tTextureRegion->getDpSize().getWidth(); + mAlignOffset.x = mSize.getWidth() - tTextureRegion->getPxSize().getWidth() - mRealPadding.Right; } else { - mAlignOffset.x = 0; + mAlignOffset.x = mRealPadding.Left; } if ( VAlignGet( mFlags ) == UI_VALIGN_CENTER ) { - mAlignOffset.y = mDpSize.getHeight() / 2 - tTextureRegion->getDpSize().getHeight() / 2; + mAlignOffset.y = ( mSize.getHeight() - tTextureRegion->getPxSize().getHeight() ) / 2; } else if ( fontVAlignGet( mFlags ) == UI_VALIGN_BOTTOM ) { - mAlignOffset.y = mDpSize.getHeight() - tTextureRegion->getDpSize().getHeight(); + mAlignOffset.y = mSize.getHeight() - tTextureRegion->getPxSize().getHeight() - mRealPadding.Bottom; } else { - mAlignOffset.y = 0; + mAlignOffset.y = mRealPadding.Top; } } diff --git a/src/eepp/ui/uitextureregion.cpp b/src/eepp/ui/uitextureregion.cpp index 1660eab9e..f189876a8 100644 --- a/src/eepp/ui/uitextureregion.cpp +++ b/src/eepp/ui/uitextureregion.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace EE { namespace UI { @@ -51,10 +52,18 @@ UITextureRegion * UITextureRegion::setTextureRegion( Graphics::TextureRegion * T } void UITextureRegion::onAutoSize() { - if ( ( mFlags & UI_AUTO_SIZE ) && Sizef::Zero == mDpSize ) { - if ( NULL != mTextureRegion ) { + if ( NULL != mTextureRegion ) { + if ( ( mFlags & UI_AUTO_SIZE ) && Sizef::Zero == mDpSize ) { setSize( mTextureRegion->getDpSize().asFloat() ); } + + if ( mLayoutWidthRules == WRAP_CONTENT ) { + setInternalPixelsWidth( mTextureRegion->getPxSize().getWidth() + mRealPadding.Left + mRealPadding.Right ); + } + + if ( mLayoutHeightRules == WRAP_CONTENT ) { + setInternalPixelsHeight( mTextureRegion->getPxSize().getHeight() + mRealPadding.Top + mRealPadding.Bottom ); + } } } @@ -68,7 +77,7 @@ void UITextureRegion::draw() { if ( mScaleType == UIScaleType::Expand ) { mTextureRegion->setOffset( Vector2i( 0, 0 ) ); - mTextureRegion->setDestSize( Vector2f( (int)mSize.x, (int)mSize.y ) ); + mTextureRegion->setDestSize( Vector2f( (int)mSize.x - mRealPadding.Left - mRealPadding.Right, (int)mSize.y - mRealPadding.Top - mRealPadding.Bottom ) ); autoAlign(); @@ -78,8 +87,8 @@ void UITextureRegion::draw() { mTextureRegion->setOffset( Vector2i( 0, 0 ) ); Sizei pxSize = mTextureRegion->getPxSize(); - Float Scale1 = mSize.x / (Float)pxSize.x; - Float Scale2 = mSize.y / (Float)pxSize.y; + Float Scale1 = ( mSize.x - mRealPadding.Left - mRealPadding.Right ) / (Float)pxSize.x; + Float Scale2 = ( mSize.y - mRealPadding.Top - mRealPadding.Bottom ) / (Float)pxSize.y; if ( Scale1 < 1 || Scale2 < 1 ) { if ( Scale2 < Scale1 ) @@ -152,19 +161,19 @@ void UITextureRegion::autoAlign() { return; if ( HAlignGet( mFlags ) == UI_HALIGN_CENTER ) { - mAlignOffset.x = mSize.getWidth() / 2 - mTextureRegion->getDestSize().x / 2; + mAlignOffset.x = ( mSize.getWidth() - mTextureRegion->getDestSize().x ) / 2; } else if ( fontHAlignGet( mFlags ) == UI_HALIGN_RIGHT ) { - mAlignOffset.x = mSize.getWidth() - mTextureRegion->getDestSize().x; + mAlignOffset.x = mSize.getWidth() - mTextureRegion->getDestSize().x - mRealPadding.Right; } else { - mAlignOffset.x = 0; + mAlignOffset.x = mRealPadding.Left; } if ( VAlignGet( mFlags ) == UI_VALIGN_CENTER ) { - mAlignOffset.y = mSize.getHeight() / 2 - mTextureRegion->getDestSize().y / 2; + mAlignOffset.y = ( mSize.getHeight() - mTextureRegion->getDestSize().y ) / 2; } else if ( fontVAlignGet( mFlags ) == UI_VALIGN_BOTTOM ) { - mAlignOffset.y = mSize.getHeight() - mTextureRegion->getDestSize().y; + mAlignOffset.y = mSize.getHeight() - mTextureRegion->getDestSize().y - mRealPadding.Bottom; } else { - mAlignOffset.y = 0; + mAlignOffset.y = mRealPadding.Top; } } @@ -187,9 +196,9 @@ bool UITextureRegion::setAttribute( const NodeAttribute& attribute ) { const std::string& name = attribute.getName(); if ( "src" == name || "textureregion" == name || "subtexture" == name ) { - DrawableResource * res = NULL; + Drawable * res = NULL; - if ( NULL != ( res = GlobalTextureAtlas::instance()->getByName( attribute.asString() ) ) && res->getDrawableType() == Drawable::TEXTUREREGION ) { + if ( NULL != ( res = TextureAtlasManager::instance()->getTextureRegionByName( attribute.asString() ) ) && res->getDrawableType() == Drawable::TEXTUREREGION ) { setTextureRegion( static_cast( res ) ); } } else if ( "scaletype" == name ) { diff --git a/src/eepp/ui/uiwinmenu.cpp b/src/eepp/ui/uiwinmenu.cpp index ee3646106..e65b9c1bd 100644 --- a/src/eepp/ui/uiwinmenu.cpp +++ b/src/eepp/ui/uiwinmenu.cpp @@ -171,8 +171,9 @@ void UIWinMenu::refreshButtons() { UISelectButton * pbut = it->first; UITextView * tbox = pbut->getTextBox(); + pbut->setLayoutSizeRules( FIXED, FIXED ); pbut->setStyleConfig( mStyleConfig ); - pbut->setSize( PixelDensity::pxToDpI( tbox->getTextWidth() ) + mStyleConfig.ButtonMargin, getSize().getHeight() ); + pbut->setPixelsSize( tbox->getTextWidth() + PixelDensity::dpToPx( mStyleConfig.ButtonMargin ), getRealSize().getHeight() ); pbut->setPosition( xpos, ycenter ); xpos += pbut->getSize().getWidth() + mStyleConfig.MarginBetweenButtons;