From 90087ae5a7db99cd00606bb44aa5a0b2910ae962 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 1 Apr 2016 09:31:18 +0800 Subject: [PATCH] improve textfield code --- cocos/2d/CCTextFieldTTF.cpp | 9 +++++++-- cocos/2d/CCTextFieldTTF.h | 4 ++-- cocos/ui/UITextField.cpp | 13 +------------ cocos/ui/UITextField.h | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/cocos/2d/CCTextFieldTTF.cpp b/cocos/2d/CCTextFieldTTF.cpp index 088df9cfe8..395e019d60 100644 --- a/cocos/2d/CCTextFieldTTF.cpp +++ b/cocos/2d/CCTextFieldTTF.cpp @@ -63,6 +63,7 @@ TextFieldTTF::TextFieldTTF() , _placeHolder("") // prevent Label initWithString assertion , _colorText(Color4B::WHITE) , _secureTextEntry(false) +,_passwordStyleText("\u25CF") , _cursorEnabled(false) , _cursorPosition(0) , _cursorChar(CURSOR_DEFAULT_CHAR) @@ -71,7 +72,6 @@ TextFieldTTF::TextFieldTTF() { _colorSpaceHolder.r = _colorSpaceHolder.g = _colorSpaceHolder.b = 127; _colorSpaceHolder.a = 255; - _passwordStyleText = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00}; } TextFieldTTF::~TextFieldTTF() @@ -422,7 +422,7 @@ void TextFieldTTF::update(float delta) _cursorShowingTime = CURSOR_TIME_SHOW_HIDE; } // before cursor inserted '\b', need next letter - auto sprite = getLetter(_cursorPosition + 1); + auto sprite = getLetter((int)_cursorPosition + 1); if (sprite) { @@ -687,6 +687,11 @@ void TextFieldTTF::setSecureTextEntry(bool value) void TextFieldTTF::setPasswordTextStyle(const std::string &text) { + if (text.length() < 1) + { + return; + } + if (text != _passwordStyleText) { _passwordStyleText = text; setString(_inputText); diff --git a/cocos/2d/CCTextFieldTTF.h b/cocos/2d/CCTextFieldTTF.h index d2419ccda9..261dc4b8fa 100644 --- a/cocos/2d/CCTextFieldTTF.h +++ b/cocos/2d/CCTextFieldTTF.h @@ -162,7 +162,7 @@ public: * Query the currently inputed character count. *@return The total input character count. */ - inline int getCharCount() const { return _charCount; }; + inline std::size_t getCharCount() const { return _charCount; }; /** * Query the color of place holder. @@ -279,7 +279,7 @@ protected: virtual void controlKey(EventKeyboard::KeyCode keyCode) override; TextFieldDelegate * _delegate; - int _charCount; + std::size_t _charCount; std::string _inputText; diff --git a/cocos/ui/UITextField.cpp b/cocos/ui/UITextField.cpp index b7387fee48..97b66f17c6 100644 --- a/cocos/ui/UITextField.cpp +++ b/cocos/ui/UITextField.cpp @@ -194,7 +194,7 @@ int UICCTextField::getMaxLength()const return _maxLength; } -int UICCTextField::getCharCount()const +std::size_t UICCTextField::getCharCount()const { return TextFieldTTF::getCharCount(); } @@ -211,15 +211,6 @@ bool UICCTextField::isPasswordEnabled()const void UICCTextField::setPasswordStyleText(const std::string& styleText) { - if (styleText.length() > 1) - { - return; - } - char value = styleText[0]; - if (value < 33 || value > 126) - { - return; - } this->setPasswordTextStyle(styleText); } @@ -573,8 +564,6 @@ int TextField::getMaxLength()const void TextField::setPasswordEnabled(bool enable) { _textFieldRenderer->setPasswordEnabled(enable); - if (enable) - setPasswordStyleText(getPasswordStyleText()); } bool TextField::isPasswordEnabled()const diff --git a/cocos/ui/UITextField.h b/cocos/ui/UITextField.h index f97ce9d6b1..c2ab9803f1 100644 --- a/cocos/ui/UITextField.h +++ b/cocos/ui/UITextField.h @@ -119,7 +119,7 @@ public: * Return the total inputed characters. *@return Total inputed character count. */ - int getCharCount()const; + std::size_t getCharCount()const; /**