From bdbae585c94fe25b6a9bc69e1da9ca9ff24d2791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 12 Dec 2019 01:58:14 -0300 Subject: [PATCH] UITextInput hint support. --HG-- branch : dev --- include/eepp/ui/css/propertydefinition.hpp | 10 +- include/eepp/ui/uitextinput.hpp | 37 ++++ src/eepp/ui/css/stylesheetspecification.cpp | 12 ++ src/eepp/ui/uitextinput.cpp | 207 +++++++++++++++++++- src/eepp/ui/uitextinputpassword.cpp | 21 +- src/eepp/ui/uitextview.cpp | 5 +- src/tools/uieditor/uieditor.cpp | 12 ++ 7 files changed, 295 insertions(+), 9 deletions(-) diff --git a/include/eepp/ui/css/propertydefinition.hpp b/include/eepp/ui/css/propertydefinition.hpp index b8b3a2eda..772ec06a2 100644 --- a/include/eepp/ui/css/propertydefinition.hpp +++ b/include/eepp/ui/css/propertydefinition.hpp @@ -176,7 +176,15 @@ enum class PropertyId : Uint32 { MarginBetweenButtons = String::hash( "margin-between-buttons" ), ButtonMargin = String::hash( "button-margin" ), MenuHeight = String::hash( "menu-height" ), - FirstButtonMarginLeft = String::hash( "first-button-margin-left" ) + FirstButtonMarginLeft = String::hash( "first-button-margin-left" ), + Hint = String::hash( "hint" ), + HintColor = String::hash( "hint-color" ), + HintShadowColor = String::hash( "hint-shadow-color" ), + HintFontFamily = String::hash( "hint-font-family" ), + HintFontSize = String::hash( "hint-font-size" ), + HintFontStyle = String::hash( "hint-font-style" ), + HintStrokeWidth = String::hash( "hint-stroke-width" ), + HintStrokeColor = String::hash( "hint-stroke-color" ), }; enum class PropertyType : Uint32 { diff --git a/include/eepp/ui/uitextinput.hpp b/include/eepp/ui/uitextinput.hpp index fe04e21e2..555f353f1 100644 --- a/include/eepp/ui/uitextinput.hpp +++ b/include/eepp/ui/uitextinput.hpp @@ -56,10 +56,45 @@ class EE_API UITextInput : public UITextView { virtual std::string getPropertyString(const PropertyDefinition* propertyDef); virtual UIWidget * setPadding(const Rectf& padding); + + const String& getHint() const; + + UITextInput* setHint(const String& hint); + + const Color& getHintColor() const; + + UITextInput* setHintColor(const Color& hintColor); + + const Color& getHintShadowColor() const; + + UITextInput* setHintShadowColor(const Color& shadowColor); + + Font * getHintFont(); + + UITextInput* setHintFont( Font * font ); + + Uint32 getHintCharacterSize() const; + + UITextView* setHintCharacterSize(const Uint32& characterSize); + + const Uint32& getHintFontStyle() const; + + UITextView * setHintFontStyle( const Uint32& fontStyle ); + + const Float & getHintOutlineThickness() const; + + UITextView * setHintOutlineThickness( const Float& outlineThickness ); + + const Color& getHintOutlineColor() const; + + UITextView * setHintOutlineColor( const Color& outlineColor ); + protected: InputTextBuffer mTextBuffer; Float mWaitCursorTime; Vector2f mCurPos; + Text * mHintCache; + FontStyleConfig mHintStyleConfig; int mCursorPos; bool mAllowEditing; bool mShowingWait; @@ -87,6 +122,8 @@ class EE_API UITextInput : public UITextView { virtual Uint32 onPressEnter(); + virtual void onFontChanged(); + void onThemeLoaded(); virtual void onCursorPosChange(); diff --git a/src/eepp/ui/css/stylesheetspecification.cpp b/src/eepp/ui/css/stylesheetspecification.cpp index 273e17f2c..17b5f89cf 100644 --- a/src/eepp/ui/css/stylesheetspecification.cpp +++ b/src/eepp/ui/css/stylesheetspecification.cpp @@ -274,6 +274,18 @@ void StyleSheetSpecification::registerDefaultProperties() { registerProperty( "word-wrap", "", false ).setType( PropertyType::Bool ); + registerProperty( "hint", "", false ).setType( PropertyType::String ); + registerProperty( "hint-color", "", false ).setType( PropertyType::Color ); + registerProperty( "hint-shadow-color", "", false ).setType( PropertyType::Color ); + registerProperty( "hint-font-size", "", false ).setType( PropertyType::NumberFloat ); + registerProperty( "hint-font-style", "", false ).setType( PropertyType::String ); + registerProperty( "hint-stroke-width", "", false ) + .setType( PropertyType::NumberFloat ) + .addAlias( "hintoutlinethickness" ); + registerProperty( "hint-stroke-color", "", false ) + .setType( PropertyType::Color ); + registerProperty( "hint-font-family", "", false ).addAlias( "hint-font-name" ); + // Shorthands registerProperty( "background-position", "", false ).setType( PropertyType::NumberLength ); registerProperty( "transition", "", false ); diff --git a/src/eepp/ui/uitextinput.cpp b/src/eepp/ui/uitextinput.cpp index 1fe1ab509..07cf21cb0 100644 --- a/src/eepp/ui/uitextinput.cpp +++ b/src/eepp/ui/uitextinput.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,8 @@ UITextInput::UITextInput( const std::string& tag ) : mAllowEditing( true ), mShowingWait( true ) { + mHintCache = Text::New(); + subscribeScheduledUpdate(); setFlags( UI_AUTO_PADDING | UI_AUTO_SIZE | UI_TEXT_SELECTION_ENABLED ); @@ -46,6 +49,7 @@ UITextInput::UITextInput() : {} UITextInput::~UITextInput() { + eeSAFE_DELETE( mHintCache ); } Uint32 UITextInput::getType() const { @@ -105,7 +109,44 @@ void UITextInput::updateWaitingCursor( const Time& time ) { } void UITextInput::draw() { - UITextView::draw(); + if ( mVisible && 0.f != mAlpha ) { + UINode::draw(); + + if ( mTextCache->getTextWidth() ) { + drawSelection( mTextCache ); + + if ( isClipped() ) { + clipSmartEnable( + mScreenPos.x + mRealPadding.Left, + mScreenPos.y + mRealPadding.Top, + mSize.getWidth() - mRealPadding.Left - mRealPadding.Right, + mSize.getHeight() - mRealPadding.Top - mRealPadding.Bottom + ); + } + + mTextCache->setAlign( getFlags() ); + mTextCache->draw( (Float)mScreenPosi.x + (int)mRealAlignOffset.x + (int)mRealPadding.Left, mFontLineCenter + (Float)mScreenPosi.y + (int)mRealAlignOffset.y + (int)mRealPadding.Top, Vector2f::One, 0.f, getBlendMode() ); + + if ( isClipped() ) { + clipSmartDisable(); + } + } else if ( !mHintCache->getString().empty() && !mTextBuffer.isActive() ) { + if ( isClipped() ) { + clipSmartEnable( + mScreenPos.x + mRealPadding.Left, + mScreenPos.y + mRealPadding.Top, + mSize.getWidth() - mRealPadding.Left - mRealPadding.Right, + mSize.getHeight() - mRealPadding.Top - mRealPadding.Bottom + ); + } + + mHintCache->draw( (Float)mScreenPosi.x + (int)mRealAlignOffset.x + (int)mRealPadding.Left, mFontLineCenter + (Float)mScreenPosi.y + (int)mRealAlignOffset.y + (int)mRealPadding.Top, Vector2f::One, 0.f, getBlendMode() ); + + if ( isClipped() ) { + clipSmartDisable(); + } + } + } drawWaitingCursor(); } @@ -409,6 +450,22 @@ std::string UITextInput::getPropertyString( const PropertyDefinition* propertyDe return getInputTextBuffer()->onlyNumbersAllowed() ? "true" : "false"; case PropertyId::AllowDot: return getInputTextBuffer()->dotsInNumbersAllowed() ? "true" : "false"; + case PropertyId::Hint: + return getHint().toUtf8(); + case PropertyId::HintColor: + return getHintColor().toHexString(); + case PropertyId::HintShadowColor: + return getHintShadowColor().toHexString(); + case PropertyId::HintFontSize: + return String::format( "%ddp", getHintCharacterSize() ); + case PropertyId::HintFontFamily: + return NULL != getHintFont() ? getFont()->getName() : ""; + case PropertyId::HintFontStyle: + return Text::styleFlagToString( getHintFontStyle() ); + case PropertyId::HintStrokeWidth: + return String::toStr( PixelDensity::dpToPx( getHintOutlineThickness() ) ); + case PropertyId::HintStrokeColor: + return getHintOutlineColor().toHexString(); default: return UITextView::getPropertyString( propertyDef ); } @@ -419,9 +476,8 @@ bool UITextInput::applyProperty( const StyleSheetProperty& attribute ) { switch ( attribute.getPropertyDefinition()->getPropertyId() ) { case PropertyId::Text: - if ( NULL != mSceneNode && mSceneNode->isUISceneNode() ) { - setText( static_cast( mSceneNode )->getTranslatorString( attribute.asString() ) ); - } + if ( NULL != getUISceneNode() ) + setText( getUISceneNode()->getTranslatorString( attribute.asString() ) ); break; case PropertyId::AllowEditing: setAllowEditing( attribute.asBool() ); @@ -438,6 +494,31 @@ bool UITextInput::applyProperty( const StyleSheetProperty& attribute ) { case PropertyId::AllowDot: getInputTextBuffer()->setAllowOnlyNumbers( getInputTextBuffer()->onlyNumbersAllowed(), attribute.asBool() ); break; + case PropertyId::Hint: + if ( NULL != getUISceneNode() ) + setHint( getUISceneNode()->getTranslatorString( attribute.asString() ) ); + break; + case PropertyId::HintColor: + setHintColor( attribute.asColor() ); + break; + case PropertyId::HintShadowColor: + setHintShadowColor( attribute.asColor() ); + break; + case PropertyId::HintFontSize: + setHintCharacterSize( attribute.asDpDimensionI() ); + break; + case PropertyId::HintFontFamily: + setHintFont( FontManager::instance()->getByName( attribute.asString() ) ); + break; + case PropertyId::HintFontStyle: + setHintFontStyle( attribute.asFontStyle() ); + break; + case PropertyId::HintStrokeWidth: + setHintOutlineThickness( PixelDensity::dpToPx( attribute.asDpDimension() ) ); + break; + case PropertyId::HintStrokeColor: + setHintOutlineColor( attribute.asColor() ); + break; default: return UITextView::applyProperty( attribute ); } @@ -457,4 +538,122 @@ UIWidget * UITextInput::setPadding( const Rectf& padding ) { return this; } +void UITextInput::onFontChanged() { + if ( getHintFont() == NULL ) { + setHintFont( getFont() ); + } +} + +const String& UITextInput::getHint() const { + return mHintCache->getString(); +} + +UITextInput* UITextInput::setHint(const String& hint) { + if ( hint != mHintCache->getString() ) { + mHintCache->setString( hint ); + invalidateDraw(); + } + + return this; +} + +const Color& UITextInput::getHintColor() const { + return mHintStyleConfig.getFontColor(); +} + +UITextInput* UITextInput::setHintColor(const Color& hintColor) { + if ( hintColor != mHintStyleConfig.getFontColor() ) { + mHintCache->setFillColor( hintColor ); + mHintStyleConfig.FontColor = hintColor; + invalidateDraw(); + } + + return this; +} + +const Color& UITextInput::getHintShadowColor() const { + return mHintStyleConfig.getFontShadowColor(); +} + +UITextInput* UITextInput::setHintShadowColor(const Color& shadowColor) { + if ( shadowColor != mHintStyleConfig.getFontShadowColor() ) { + mHintCache->setShadowColor( shadowColor ); + mHintStyleConfig.ShadowColor = shadowColor; + invalidateDraw(); + } + + return this; +} + +Font* UITextInput::getHintFont() { + return mHintStyleConfig.getFont(); +} + +UITextInput* UITextInput::setHintFont( Font* font ) { + if ( mHintStyleConfig.getFont() != font ) { + mHintCache->setFont( font ); + mHintStyleConfig.Font = font; + invalidateDraw(); + } + + return this; +} + +Uint32 UITextInput::getHintCharacterSize() const { + return mHintStyleConfig.getFontCharacterSize(); +} + +UITextView * UITextInput::setHintCharacterSize( const Uint32 & characterSize ) { + if ( mHintCache->getCharacterSize() != characterSize ) { + mHintCache->setCharacterSize( characterSize ); + mHintStyleConfig.CharacterSize = characterSize; + invalidateDraw(); + } + + return this; +} + +const Uint32 &UITextInput::getHintFontStyle() const { + return mHintStyleConfig.Style; +} + +const Float &UITextInput::getHintOutlineThickness() const { + return mHintStyleConfig.OutlineThickness; +} + +UITextView * UITextInput::setHintOutlineThickness( const Float & outlineThickness ) { + if ( mHintStyleConfig.OutlineThickness != outlineThickness ) { + mHintCache->setOutlineThickness( outlineThickness ); + mHintStyleConfig.OutlineThickness = outlineThickness; + invalidateDraw(); + } + + return this; +} + +const Color &UITextInput::getHintOutlineColor() const { + return mHintStyleConfig.OutlineColor; +} + +UITextView * UITextInput::setHintOutlineColor(const Color & outlineColor) { + if ( mHintStyleConfig.OutlineColor != outlineColor ) { + mHintStyleConfig.OutlineColor = outlineColor; + Color newColor( outlineColor.r, outlineColor.g, outlineColor.b, outlineColor.a * mAlpha / 255.f ); + mHintCache->setOutlineColor( newColor ); + invalidateDraw(); + } + + return this; +} + +UITextView * UITextInput::setHintFontStyle(const Uint32 & fontStyle) { + if ( mHintStyleConfig.Style != fontStyle ) { + mHintCache->setStyle( fontStyle ); + mHintStyleConfig.Style = fontStyle; + invalidateDraw(); + } + + return this; +} + }} diff --git a/src/eepp/ui/uitextinputpassword.cpp b/src/eepp/ui/uitextinputpassword.cpp index ae48f6c3b..43fdacede 100644 --- a/src/eepp/ui/uitextinputpassword.cpp +++ b/src/eepp/ui/uitextinputpassword.cpp @@ -27,9 +27,9 @@ void UITextInputPassword::draw() { if ( mVisible && 0.f != mAlpha ) { UINode::draw(); - drawSelection( mPassCache ); - if ( mPassCache->getTextWidth() ) { + drawSelection( mPassCache ); + if ( isClipped() ) { clipSmartEnable( mScreenPos.x + mRealPadding.Left, @@ -42,6 +42,21 @@ void UITextInputPassword::draw() { mPassCache->setAlign( getFlags() ); mPassCache->draw( (Float)mScreenPosi.x + (int)mRealAlignOffset.x + (int)mRealPadding.Left, (Float)mScreenPosi.y + (int)mRealAlignOffset.y + (int)mRealPadding.Top, Vector2f::One, 0.f, getBlendMode() ); + if ( isClipped() ) { + clipSmartDisable(); + } + } else if ( !mHintCache->getString().empty() && !mTextBuffer.isActive() ) { + if ( isClipped() ) { + clipSmartEnable( + mScreenPos.x + mRealPadding.Left, + mScreenPos.y + mRealPadding.Top, + mSize.getWidth() - mRealPadding.Left - mRealPadding.Right, + mSize.getHeight() - mRealPadding.Top - mRealPadding.Bottom + ); + } + + mHintCache->draw( (Float)mScreenPosi.x + (int)mRealAlignOffset.x + (int)mRealPadding.Left, mFontLineCenter + (Float)mScreenPosi.y + (int)mRealAlignOffset.y + (int)mRealPadding.Top, Vector2f::One, 0.f, getBlendMode() ); + if ( isClipped() ) { clipSmartDisable(); } @@ -133,6 +148,8 @@ void UITextInputPassword::updateFontStyleConfig() { mPassCache->setFont( mFontStyleConfig.getFont() ); mPassCache->setFillColor( mFontStyleConfig.getFontColor() ); mPassCache->setShadowColor( mFontStyleConfig.getFontShadowColor() ); + mPassCache->setOutlineColor( mFontStyleConfig.getOutlineColor() ); + mPassCache->setOutlineThickness( mFontStyleConfig.getOutlineThickness() ); } void UITextInputPassword::onStateChange() { diff --git a/src/eepp/ui/uitextview.cpp b/src/eepp/ui/uitextview.cpp index 4937a9491..97a80fe92 100644 --- a/src/eepp/ui/uitextview.cpp +++ b/src/eepp/ui/uitextview.cpp @@ -72,9 +72,9 @@ void UITextView::draw() { if ( mVisible && 0.f != mAlpha ) { UINode::draw(); - drawSelection( mTextCache ); - if ( mTextCache->getTextWidth() ) { + drawSelection( mTextCache ); + if ( isClipped() ) { clipSmartEnable( mScreenPos.x + mRealPadding.Left, @@ -105,6 +105,7 @@ UITextView * UITextView::setFont( Graphics::Font * font ) { recalculate(); onFontChanged(); notifyLayoutAttrChange(); + invalidateDraw(); } return this; diff --git a/src/tools/uieditor/uieditor.cpp b/src/tools/uieditor/uieditor.cpp index 513a44de8..614e1e9d3 100644 --- a/src/tools/uieditor/uieditor.cpp +++ b/src/tools/uieditor/uieditor.cpp @@ -718,6 +718,18 @@ void mainLoop() { resizeWindowToLayout(); } + if ( window->getInput()->isKeyUp( KEY_F6 ) ) { + uiSceneNode->setHighlightOver( !uiSceneNode->getHighlightOver() ); + } + + if ( window->getInput()->isKeyUp( KEY_F7 ) ) { + uiSceneNode->setDrawBoxes( !uiSceneNode->getDrawBoxes() ); + } + + if ( window->getInput()->isKeyUp( KEY_F8 ) ) { + uiSceneNode->setDrawDebugData( !uiSceneNode->getDrawDebugData() ); + } + if ( mousePos != window->getInput()->getMousePos() ) { mousePos = window->getInput()->getMousePos(); mouseClock.restart();