UITextInput hint support.

--HG--
branch : dev
This commit is contained in:
Martín Lucas Golini
2019-12-12 01:58:14 -03:00
parent 3a21f2e640
commit bdbae585c9
7 changed files with 295 additions and 9 deletions
+9 -1
View File
@@ -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 {
+37
View File
@@ -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();
@@ -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 );
+203 -4
View File
@@ -4,6 +4,7 @@
#include <eepp/graphics/primitives.hpp>
#include <eepp/graphics/font.hpp>
#include <eepp/graphics/text.hpp>
#include <eepp/graphics/fontmanager.hpp>
#include <pugixml/pugixml.hpp>
#include <eepp/ui/uiscenenode.hpp>
#include <eepp/ui/css/propertydefinition.hpp>
@@ -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<UISceneNode*>( 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;
}
}}
+19 -2
View File
@@ -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() {
+3 -2
View File
@@ -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;
+12
View File
@@ -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();