Clean up.

--HG--
branch : dev-stateful-drawable
This commit is contained in:
Martín Lucas Golini
2018-12-22 03:34:57 -03:00
parent b6aa5c1e05
commit 0bc90c0b7b
12 changed files with 130 additions and 93 deletions

View File

@@ -3,6 +3,13 @@
#include <string>
#include <eepp/core/string.hpp>
#include <eepp/system/color.hpp>
#include <eepp/math/originpoint.hpp>
#include <eepp/graphics/blendmode.hpp>
using namespace EE::System;
using namespace EE::Math;
using namespace EE::Graphics;
namespace EE { namespace Scene {
@@ -42,6 +49,15 @@ class NodeAttribute {
bool asBool( bool defaultValue = false ) const;
Color asColor() const;
Float asDpDimension( const std::string& defaultValue = "" ) const;
int asDpDimensionI( const std::string& defaultValue = "" ) const;
OriginPoint asOriginPoint() const;
BlendMode asBlendMode() const;
protected:
std::string mName;
std::string mValue;

View File

@@ -1,4 +1,5 @@
#include <eepp/scene/nodeattribute.hpp>
#include <eepp/graphics/pixeldensity.hpp>
namespace EE { namespace Scene {
@@ -66,4 +67,63 @@ bool NodeAttribute::asBool( bool defaultValue ) const {
return (first == '1' || first == 't' || first == 'T' || first == 'y' || first == 'Y');
}
Color NodeAttribute::asColor() const {
return Color::fromString( mValue );
}
Float NodeAttribute::asDpDimension( const std::string& defaultValue ) const {
return Graphics::PixelDensity::toDpFromString( asString( defaultValue ) );
}
int NodeAttribute::asDpDimensionI( const std::string& defaultValue ) const {
return Graphics::PixelDensity::toDpFromStringI( asString( defaultValue ) );
}
static OriginPoint toOriginPoint( std::string val ) {
String::toLowerInPlace( val );
if ( "center" == val ) {
return OriginPoint::OriginCenter;
} else if ( "topleft" == val ) {
return OriginPoint::OriginTopLeft;
} else {
std::vector<std::string> parts = String::split( val, ',' );
if ( parts.size() == 2 ) {
Float x = 0;
Float y = 0;
bool Res1 = String::fromString<Float>( x, parts[0] );
bool Res2 = String::fromString<Float>( y, parts[1] );
if ( Res1 && Res2 ) {
return OriginPoint( x, y );
}
}
}
return OriginPoint::OriginCenter;
}
static BlendMode toBlendMode( std::string val ) {
String::toLowerInPlace( val );
BlendMode blendMode;
if ( val == "add" ) blendMode = BlendAdd;
else if ( val == "alpha" ) blendMode = BlendAlpha;
else if ( val == "multiply" ) blendMode = BlendMultiply;
else if ( val == "none" ) blendMode = BlendNone;
return blendMode;
}
OriginPoint NodeAttribute::asOriginPoint() const {
return toOriginPoint( mValue );
}
BlendMode NodeAttribute::asBlendMode() const {
return toBlendMode( mValue );
}
}}

View File

@@ -207,7 +207,7 @@ bool UIImage::setAttribute( const NodeAttribute& attribute ) {
setScaleType( UIScaleType::None );
}
} else if ( "tint" == name ) {
setColor( Color::fromString( attribute.asString() ) );
setColor( attribute.asColor() );
} else {
return UIWidget::setAttribute( attribute );
}

View File

@@ -989,15 +989,15 @@ bool UIListBox::setAttribute( const NodeAttribute& attribute ) {
if ( "rowheight" == name ) {
setRowHeight( attribute.asInt() );
} else if ( "textcolor" == name ) {
setFontColor( Color::fromString( attribute.asString() ) );
setFontColor( attribute.asColor() );
} else if ( "textshadowcolor" == name ) {
mFontStyleConfig.ShadowColor = ( Color::fromString( attribute.asString() ) );
mFontStyleConfig.ShadowColor = ( attribute.asColor() );
} else if ( "textovercolor" == name ) {
setFontOverColor( Color::fromString( attribute.asString() ) );
setFontOverColor( attribute.asColor() );
} else if ( "textselectedcolor" == name ) {
setFontSelectedColor( Color::fromString( attribute.asString() ) );
setFontSelectedColor( attribute.asColor() );
} else if ( "textselectionbackcolor" == name ) {
mFontStyleConfig.FontSelectionBackColor = ( Color::fromString( attribute.asString() ) );
mFontStyleConfig.FontSelectionBackColor = ( attribute.asColor() );
} else if ( "fontfamily" == name || "fontname" == name ) {
Font * font = FontManager::instance()->getByName( attribute.asString() );

View File

@@ -194,7 +194,7 @@ bool UILoader::setAttribute( const NodeAttribute& attribute ) {
} else if ( "progress" == name ) {
setProgress( attribute.asFloat() );
} else if ( "fillcolor" == name ) {
setFillColor( Color::fromString( attribute.asString() ) );
setFillColor( attribute.asColor() );
} else if ( "radius" == name ) {
setRadius( attribute.asFloat() );
} else if ( "outlinethickness" == name ) {

View File

@@ -225,16 +225,16 @@ bool UIProgressBar::setAttribute( const NodeAttribute& attribute ) {
} else if ( "displaypercent" == name ) {
setDisplayPercent( attribute.asBool() );
} else if ( "fillerpadding" == name ) {
Float val = PixelDensity::toDpFromString( attribute.asString() );
Float val = attribute.asDpDimension();
setFillerPadding( Rectf( val, val, val, val ) );
} else if ( "fillerpaddingleft" == name ) {
setFillerPadding( Rectf( PixelDensity::toDpFromString( attribute.asString() ), mStyleConfig.FillerPadding.Top, mStyleConfig.FillerPadding.Right, mStyleConfig.FillerPadding.Bottom ) );
setFillerPadding( Rectf( attribute.asDpDimension(), mStyleConfig.FillerPadding.Top, mStyleConfig.FillerPadding.Right, mStyleConfig.FillerPadding.Bottom ) );
} else if ( "fillerpaddingright" == name ) {
setFillerPadding( Rectf( mStyleConfig.FillerPadding.Left, mStyleConfig.FillerPadding.Top, PixelDensity::toDpFromString( attribute.asString() ), mStyleConfig.FillerPadding.Bottom ) );
setFillerPadding( Rectf( mStyleConfig.FillerPadding.Left, mStyleConfig.FillerPadding.Top, attribute.asDpDimension(), mStyleConfig.FillerPadding.Bottom ) );
} else if ( "fillerpaddingtop" == name ) {
setFillerPadding( Rectf( mStyleConfig.FillerPadding.Left, PixelDensity::toDpFromString( attribute.asString() ), mStyleConfig.FillerPadding.Right, mStyleConfig.FillerPadding.Bottom ) );
setFillerPadding( Rectf( mStyleConfig.FillerPadding.Left, attribute.asDpDimension(), mStyleConfig.FillerPadding.Right, mStyleConfig.FillerPadding.Bottom ) );
} else if ( "fillerpaddingbottom" == name ) {
setFillerPadding( Rectf( mStyleConfig.FillerPadding.Left, mStyleConfig.FillerPadding.Top, mStyleConfig.FillerPadding.Right, PixelDensity::toDpFromString( attribute.asString() ) ) );
setFillerPadding( Rectf( mStyleConfig.FillerPadding.Left, mStyleConfig.FillerPadding.Top, mStyleConfig.FillerPadding.Right, attribute.asDpDimension() ) );
} else {
return UIWidget::setAttribute( attribute );
}

View File

@@ -376,7 +376,7 @@ bool UIPushButton::setAttribute( const NodeAttribute& attribute ) {
if ( NULL != mSceneNode && mSceneNode->isUISceneNode() )
setText( static_cast<UISceneNode*>( mSceneNode )->getTranslatorString( attribute.asString() ) );
} else if ( "textovercolor" == name ) {
setFontOverColor( Color::fromString( attribute.asString() ) );
setFontOverColor( attribute.asColor() );
} else if ( "icon" == name ) {
std::string val = attribute.asString();
Drawable * icon = NULL;

View File

@@ -93,7 +93,7 @@ bool UISelectButton::setAttribute( const NodeAttribute& attribute ) {
const std::string& name = attribute.getName();
if ( "textselectedcolor" == name ) {
setFontSelectedColor( Color::fromString( attribute.asString() ) );
setFontSelectedColor( attribute.asColor() );
} else {
return UIPushButton::setAttribute( attribute );
}

View File

@@ -190,20 +190,20 @@ bool UITabWidget::setAttribute( const NodeAttribute& attribute ) {
const std::string& name = attribute.getName();
if ( "textcolor" == name ) {
setFontColor( Color::fromString( attribute.asString() ) );
setFontColor( attribute.asColor() );
} else if ( "textshadowcolor" == name ) {
setFontShadowColor( Color::fromString( attribute.asString() ) );
setFontShadowColor( attribute.asColor() );
} else if ( "textovercolor" == name ) {
setFontOverColor( Color::fromString( attribute.asString() ) );
setFontOverColor( attribute.asColor() );
} else if ( "textselectedcolor" == name ) {
setFontSelectedColor( Color::fromString( attribute.asString() ) );
setFontSelectedColor( attribute.asColor() );
} else if ( "fontfamily" == name || "fontname" == name ) {
Font * font = FontManager::instance()->getByName( attribute.asString() );
if ( NULL != font )
setFont( font );
} else if ( "textsize" == name || "fontsize" == name || "charactersize" == name ) {
setCharacterSize( PixelDensity::toDpFromStringI( attribute.asString() ) );
setCharacterSize( attribute.asDpDimensionI() );
} else if ( "textstyle" == name || "fontstyle" == name ) {
std::string valStr = attribute.asString();
String::toLowerInPlace( valStr );
@@ -230,9 +230,9 @@ bool UITabWidget::setAttribute( const NodeAttribute& attribute ) {
setFontStyle( flags );
}
} else if ( "fontoutlinethickness" == name ) {
setOutlineThickness( PixelDensity::toDpFromString( attribute.asString() ) );
setOutlineThickness( attribute.asDpDimension() );
} else if ( "fontoutlinecolor" == name ) {
setOutlineColor( Color::fromString( attribute.asString() ) );
setOutlineColor( attribute.asColor() );
} else if ( "maxtextlength" == name ) {
setMaxTextLength( attribute.asUint(1) );
} else if ( "mintabwidth" == name ) {
@@ -246,7 +246,7 @@ bool UITabWidget::setAttribute( const NodeAttribute& attribute ) {
} else if ( "drawlinebelowtabs" == name ) {
setDrawLineBelowTabs( attribute.asBool() );
} else if ( "linebelowtabscolor" == name ) {
setLineBelowTabsColor( Color::fromString( attribute.asString() ) );
setLineBelowTabsColor( attribute.asColor() );
} else if ( "linebelowtabsyoffset" == name ) {
setLineBelowTabsYOffset( attribute.asInt() );
} else {

View File

@@ -213,7 +213,7 @@ bool UITextureRegion::setAttribute( const NodeAttribute& attribute ) {
setScaleType( UIScaleType::None );
}
} else if ( "tint" == name ) {
setColor( Color::fromString( attribute.asString() ) );
setColor( attribute.asColor() );
} else {
return UIWidget::setAttribute( attribute );
}

View File

@@ -539,22 +539,22 @@ bool UITextView::setAttribute( const NodeAttribute& attribute ) {
if ( NULL != mSceneNode && mSceneNode->isUISceneNode() )
setText( static_cast<UISceneNode*>( mSceneNode )->getTranslatorString( attribute.asString() ) );
} else if ( "textcolor" == name ) {
setFontColor( Color::fromString( attribute.asString() ) );
setFontColor( attribute.asColor() );
} else if ( "textshadowcolor" == name ) {
setFontShadowColor( Color::fromString( attribute.asString() ) );
setFontShadowColor( attribute.asColor() );
} else if ( "textovercolor" == name ) {
mFontStyleConfig.FontOverColor = Color::fromString( attribute.asString() );
mFontStyleConfig.FontOverColor = attribute.asColor();
} else if ( "textselectedcolor" == name ) {
mFontStyleConfig.FontSelectedColor = Color::fromString( attribute.asString() );
mFontStyleConfig.FontSelectedColor = attribute.asColor();
} else if ( "textselectionbackcolor" == name ) {
setSelectionBackColor( Color::fromString( attribute.asString() ) );
setSelectionBackColor( attribute.asColor() );
} else if ( "fontfamily" == name || "fontname" == name ) {
Font * font = FontManager::instance()->getByName( attribute.asString() );
if ( NULL != font )
setFont( font );
} else if ( "textsize" == name || "fontsize" == name || "charactersize" == name ) {
setCharacterSize( PixelDensity::toDpFromStringI( attribute.asString() ) );
setCharacterSize( attribute.asDpDimensionI() );
} else if ( "textstyle" == name || "fontstyle" == name ) {
std::string valStr = attribute.asString();
String::toLowerInPlace( valStr );
@@ -585,9 +585,9 @@ bool UITextView::setAttribute( const NodeAttribute& attribute ) {
setFontStyle( flags );
}
} else if ( "fontoutlinethickness" == name ) {
setOutlineThickness( PixelDensity::toDpFromString( attribute.asString() ) );
setOutlineThickness( attribute.asDpDimension() );
} else if ( "fontoutlinecolor" == name ) {
setOutlineColor( Color::fromString( attribute.asString() ) );
setOutlineColor( attribute.asColor() );
} else if ( "textselection" == name ) {
mFlags|= UI_TEXT_SELECTION_ENABLED;
} else {

View File

@@ -433,45 +433,6 @@ void UIWidget::endAttributesTransaction() {
}
}
static OriginPoint toOriginPoint( std::string val ) {
String::toLowerInPlace( val );
if ( "center" == val ) {
return OriginPoint::OriginCenter;
} else if ( "topleft" == val ) {
return OriginPoint::OriginTopLeft;
} else {
std::vector<std::string> parts = String::split( val, ',' );
if ( parts.size() == 2 ) {
Float x = 0;
Float y = 0;
bool Res1 = String::fromString<Float>( x, parts[0] );
bool Res2 = String::fromString<Float>( y, parts[1] );
if ( Res1 && Res2 ) {
return OriginPoint( x, y );
}
}
}
return OriginPoint::OriginCenter;
}
static BlendMode toBlendMode( std::string val ) {
String::toLowerInPlace( val );
BlendMode blendMode;
if ( val == "add" ) blendMode = BlendAdd;
else if ( val == "alpha" ) blendMode = BlendAlpha;
else if ( val == "multiply" ) blendMode = BlendMultiply;
else if ( val == "none" ) blendMode = BlendNone;
return blendMode;
}
bool UIWidget::setAttribute( const std::string& name, const std::string& value ) {
return setAttribute( NodeAttribute( name, value ) );
}
@@ -484,14 +445,14 @@ bool UIWidget::setAttribute(const NodeAttribute & attribute) {
if ( "id" == name ) {
setId( attribute.value() );
} else if ( "x" == name ) {
setInternalPosition( Vector2f( PixelDensity::toDpFromString( attribute.asString() ), mDpPos.y ) );
setInternalPosition( Vector2f( attribute.asDpDimension(), mDpPos.y ) );
} else if ( "y" == name ) {
setInternalPosition( Vector2f( mDpPos.x, PixelDensity::toDpFromString( attribute.asString() ) ) );
setInternalPosition( Vector2f( mDpPos.x, attribute.asDpDimension() ) );
} else if ( "width" == name ) {
setInternalWidth( PixelDensity::toDpFromStringI( attribute.asString() ) );
setInternalWidth( attribute.asDpDimensionI() );
notifyLayoutAttrChange();
} else if ( "height" == name ) {
setInternalHeight( PixelDensity::toDpFromStringI( attribute.asString() ) );
setInternalHeight( attribute.asDpDimensionI() );
notifyLayoutAttrChange();
} else if ( "background" == name ) {
Drawable * res = NULL;
@@ -499,30 +460,30 @@ bool UIWidget::setAttribute(const NodeAttribute & attribute) {
const std::string attributeName( attribute.asString() );
if ( String::startsWith( attributeName, "#" ) ) {
setBackgroundColor( Color::fromString( attribute.asString() ) );
setBackgroundColor( attribute.asColor() );
} else if ( NULL != ( res = DrawableSearcher::searchByName( attributeName ) ) ) {
setBackgroundDrawable( res, res->getDrawableType() == Drawable::SPRITE );
}
} else if ( "backgroundcolor" == name ) {
setBackgroundColor( Color::fromString( attribute.asString() ) );
setBackgroundColor( attribute.asColor() );
} else if ( "foreground" == name ) {
Drawable * res = NULL;
const std::string attributeName( attribute.asString() );
if ( String::startsWith( attributeName, "#" ) ) {
setForegroundColor( Color::fromString( attribute.asString() ) );
setForegroundColor( attribute.asColor() );
} else if ( NULL != ( res = DrawableSearcher::searchByName( attributeName ) ) ) {
setForegroundDrawable( res, res->getDrawableType() == Drawable::SPRITE );
}
} else if ( "foregroundcolor" == name ) {
setForegroundColor( Color::fromString( attribute.asString() ) );
setForegroundColor( attribute.asColor() );
} else if ( "foregroundcorners" == name ) {
setForegroundCorners( attribute.asUint() );
} else if ( "bordercolor" == name ) {
setBorderColor( Color::fromString( attribute.asString() ) );
setBorderColor( attribute.asColor() );
} else if ( "borderwidth" == name ) {
setBorderWidth( PixelDensity::toDpFromStringI( attribute.asString("1") ) );
setBorderWidth( attribute.asDpDimensionI("1") );
} else if ( "bordercorners" == name || "backgroundcorners" == name ) {
setBackgroundCorners( attribute.asUint() );
} else if ( "visible" == name ) {
@@ -595,16 +556,16 @@ bool UIWidget::setAttribute(const NodeAttribute & attribute) {
}
}
} else if ( "layout_margin" == name ) {
int val = PixelDensity::toDpFromStringI( attribute.asString() );
int val = attribute.asDpDimensionI();
setLayoutMargin( Rect( val, val, val, val ) );
} else if ( "layout_marginleft" == name ) {
setLayoutMargin( Rect( PixelDensity::toDpFromStringI( attribute.asString() ), mLayoutMargin.Top, mLayoutMargin.Right, mLayoutMargin.Bottom ) );
setLayoutMargin( Rect( attribute.asDpDimensionI(), mLayoutMargin.Top, mLayoutMargin.Right, mLayoutMargin.Bottom ) );
} else if ( "layout_marginright" == name ) {
setLayoutMargin( Rect( mLayoutMargin.Left, mLayoutMargin.Top, PixelDensity::toDpFromStringI( attribute.asString() ), mLayoutMargin.Bottom ) );
setLayoutMargin( Rect( mLayoutMargin.Left, mLayoutMargin.Top, attribute.asDpDimensionI(), mLayoutMargin.Bottom ) );
} else if ( "layout_margintop" == name ) {
setLayoutMargin( Rect( mLayoutMargin.Left, PixelDensity::toDpFromStringI( attribute.asString() ), mLayoutMargin.Right, mLayoutMargin.Bottom ) );
setLayoutMargin( Rect( mLayoutMargin.Left, attribute.asDpDimensionI(), mLayoutMargin.Right, mLayoutMargin.Bottom ) );
} else if ( "layout_marginbottom" == name ) {
setLayoutMargin( Rect( mLayoutMargin.Left, mLayoutMargin.Top, mLayoutMargin.Right, PixelDensity::toDpFromStringI( attribute.asString() ) ) );
setLayoutMargin( Rect( mLayoutMargin.Left, mLayoutMargin.Top, mLayoutMargin.Right, attribute.asDpDimensionI() ) );
} else if ( "tooltip" == name ) {
setTooltipText( attribute.asString() );
} else if ( "layout_weight" == name ) {
@@ -699,22 +660,22 @@ bool UIWidget::setAttribute(const NodeAttribute & attribute) {
} else if ( "scale" == name ) {
setScale( attribute.asFloat() );
} else if ( "rotationoriginpoint" == name ) {
setRotationOriginPoint( toOriginPoint( attribute.asString() ) );
setRotationOriginPoint( attribute.asOriginPoint() );
} else if ( "scaleoriginpoint" == name ) {
setScaleOriginPoint( toOriginPoint( attribute.asString() ) );
setScaleOriginPoint( attribute.asOriginPoint() );
} else if ( "blendmode" == name ) {
setBlendMode( toBlendMode( attribute.asString() ) );
setBlendMode( attribute.asBlendMode() );
} else if ( "padding" == name ) {
int val = PixelDensity::toDpFromStringI( attribute.asString() );
int val = attribute.asDpDimensionI();
setPadding( Rectf( val, val, val, val ) );
} else if ( "paddingleft" == name ) {
setPadding( Rectf( PixelDensity::toDpFromString( attribute.asString() ), mPadding.Top, mPadding.Right, mPadding.Bottom ) );
setPadding( Rectf( attribute.asDpDimension(), mPadding.Top, mPadding.Right, mPadding.Bottom ) );
} else if ( "paddingright" == name ) {
setPadding( Rectf( mPadding.Left, mPadding.Top, PixelDensity::toDpFromString( attribute.asString() ), mPadding.Bottom ) );
setPadding( Rectf( mPadding.Left, mPadding.Top, attribute.asDpDimension(), mPadding.Bottom ) );
} else if ( "paddingtop" == name ) {
setPadding( Rectf( mPadding.Left, PixelDensity::toDpFromString( attribute.asString() ), mPadding.Right, mPadding.Bottom ) );
setPadding( Rectf( mPadding.Left, attribute.asDpDimension(), mPadding.Right, mPadding.Bottom ) );
} else if ( "paddingbottom" == name ) {
setPadding( Rectf( mPadding.Left, mPadding.Top, mPadding.Right, PixelDensity::toDpFromString( attribute.asString() ) ) );
setPadding( Rectf( mPadding.Left, mPadding.Top, mPadding.Right, attribute.asDpDimension() ) );
} else {
attributeSet = false;
}