From e2d609c05d511aceb9498bc26dba5d175586b588 Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 13 Aug 2014 11:28:37 +0800 Subject: [PATCH] add UITextField color & place hold color, fix double delete bug --- cocos/2d/CCTextFieldTTF.cpp | 15 +++- cocos/platform/desktop/CCGLViewImpl.cpp | 3 - cocos/ui/UITextField.cpp | 20 +++++ cocos/ui/UITextField.h | 4 + .../CocoStudioGUITest/CocosGUIScene.cpp | 2 +- .../CocoStudioGUITest/UISceneManager.cpp | 4 +- .../UITest/CocoStudioGUITest/UISceneManager.h | 1 + .../UITextFieldTest/UITextFieldTest.cpp | 77 +++++++++++++++++++ .../UITextFieldTest/UITextFieldTest.h | 13 ++++ 9 files changed, 131 insertions(+), 8 deletions(-) diff --git a/cocos/2d/CCTextFieldTTF.cpp b/cocos/2d/CCTextFieldTTF.cpp index 5a0848d3a8..fef8b66e66 100644 --- a/cocos/2d/CCTextFieldTTF.cpp +++ b/cocos/2d/CCTextFieldTTF.cpp @@ -259,7 +259,9 @@ const std::string& TextFieldTTF::getContentText() void TextFieldTTF::setTextColor(const Color4B &color) { _colorText = color; - Label::setTextColor(_colorText); + if (_inputText.length() > 0) { + Label::setTextColor(_colorText); + } } void TextFieldTTF::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) @@ -282,11 +284,18 @@ void TextFieldTTF::setColorSpaceHolder(const Color3B& color) _colorSpaceHolder.g = color.g; _colorSpaceHolder.b = color.b; _colorSpaceHolder.a = 255; + if (0 == _inputText.length()) + { + Label::setTextColor(_colorSpaceHolder); + } } void TextFieldTTF::setColorSpaceHolder(const Color4B& color) { _colorSpaceHolder = color; + if (0 == _inputText.length()) { + Label::setTextColor(_colorSpaceHolder); + } } ////////////////////////////////////////////////////////////////////////// @@ -321,7 +330,7 @@ void TextFieldTTF::setString(const std::string &text) } // if there is no input text, display placeholder instead - if (! _inputText.length()) + if (0 == _inputText.length()) { Label::setTextColor(_colorSpaceHolder); Label::setString(_placeHolder); @@ -343,7 +352,7 @@ const std::string& TextFieldTTF::getString() const void TextFieldTTF::setPlaceHolder(const std::string& text) { _placeHolder = text; - if (! _inputText.length()) + if (0 == _inputText.length()) { Label::setTextColor(_colorSpaceHolder); Label::setString(_placeHolder); diff --git a/cocos/platform/desktop/CCGLViewImpl.cpp b/cocos/platform/desktop/CCGLViewImpl.cpp index 515e1272cf..534e52623a 100644 --- a/cocos/platform/desktop/CCGLViewImpl.cpp +++ b/cocos/platform/desktop/CCGLViewImpl.cpp @@ -647,9 +647,6 @@ void GLViewImpl::onGLFWKeyCallback(GLFWwindow *window, int key, int scancode, in EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action); auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->dispatchEvent(&event); - if (key == GLFW_KEY_BACKSPACE && action == GLFW_PRESS) { - IMEDispatcher::sharedDispatcher()->dispatchDeleteBackward(); - } } if (GLFW_RELEASE != action && g_keyCodeMap[key] == EventKeyboard::KeyCode::KEY_BACKSPACE) { diff --git a/cocos/ui/UITextField.cpp b/cocos/ui/UITextField.cpp index 6b79323e6b..4bac5b186d 100644 --- a/cocos/ui/UITextField.cpp +++ b/cocos/ui/UITextField.cpp @@ -445,6 +445,26 @@ const std::string& TextField::getPlaceHolder()const { return _textFieldRenderer->getPlaceHolder(); } + +const Color4B& TextField::getPlaceHolderColor()const +{ + return _textFieldRenderer->getColorSpaceHolder(); +} + +void TextField::setPlaceHolderColor(const cocos2d::Color3B &color) +{ + _textFieldRenderer->setColorSpaceHolder(color); +} + +void TextField::setPlaceHolderColor(const cocos2d::Color4B &color) +{ + _textFieldRenderer->setColorSpaceHolder(color); +} + +void TextField::setTextColor(const cocos2d::Color4B &textColor) +{ + _textFieldRenderer->setTextColor(textColor); +} void TextField::setFontSize(int size) { diff --git a/cocos/ui/UITextField.h b/cocos/ui/UITextField.h index eaea50d952..6738ef2685 100644 --- a/cocos/ui/UITextField.h +++ b/cocos/ui/UITextField.h @@ -135,6 +135,10 @@ public: void setPlaceHolder(const std::string& value); const std::string& getPlaceHolder()const; + const Color4B& getPlaceHolderColor()const; + void setPlaceHolderColor(const Color3B& color); + void setPlaceHolderColor(const Color4B& color); + void setTextColor(const Color4B& textColor); void setFontSize(int size); int getFontSize()const; diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp index 003a4e1f61..c9edfef871 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp @@ -159,7 +159,7 @@ g_guisTests[] = UISceneManager* sceneManager = UISceneManager::sharedUISceneManager(); sceneManager->setCurrentUISceneId(kUITextFieldTest); sceneManager->setMinUISceneId(kUITextFieldTest); - sceneManager->setMaxUISceneId(kUITextFieldTest_TrueTypeFont); + sceneManager->setMaxUISceneId(kUITextFieldTest_PlaceHolderColor); Scene* scene = sceneManager->currentUIScene(); Director::getInstance()->replaceScene(scene); } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp index 37031bf8af..939ae6586a 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp @@ -59,6 +59,7 @@ static const char* s_testArray[] = "UITextFieldTest_Password", "UITextFieldTest_LineWrap", "UITextFieldTest_TrueTypeFont", + "UITextFieldTest_PlaceHolderColor", "UILayoutTest", "UILayoutTest_Color", "UILayoutTest_Gradient", @@ -241,7 +242,8 @@ Scene *UISceneManager::currentUIScene() return UITextFieldTest_LineWrap::sceneWithTitle(s_testArray[_currentUISceneId]); case kUITextFieldTest_TrueTypeFont: return UITextFieldTest_TrueTypeFont::sceneWithTitle(s_testArray[_currentUISceneId]); - + case kUITextFieldTest_PlaceHolderColor: + return UITextFieldTest_PlaceHolderColor::sceneWithTitle(s_testArray[_currentUISceneId]); case kUILayoutTest: return UILayoutTest::sceneWithTitle(s_testArray[_currentUISceneId]); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h index a730422203..4b9403172b 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h @@ -59,6 +59,7 @@ enum kUITextFieldTest_Password, kUITextFieldTest_LineWrap, kUITextFieldTest_TrueTypeFont, + kUITextFieldTest_PlaceHolderColor, kUILayoutTest, kUILayoutTest_Color, kUILayoutTest_Gradient, diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp index 9f3b8485e6..7d3917246e 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp @@ -407,3 +407,80 @@ void UITextFieldTest_TrueTypeFont::textFieldEvent(Ref *pSender, TextField::Event break; } } + +// UITextFieldTest_PlaceHolderColor +UITextFieldTest_PlaceHolderColor::UITextFieldTest_PlaceHolderColor() +: _displayValueLabel(nullptr) +{ + +} + +UITextFieldTest_PlaceHolderColor::~UITextFieldTest_PlaceHolderColor() +{ +} + +bool UITextFieldTest_PlaceHolderColor::init() +{ + if (UIScene::init()) + { + Size widgetSize = _widget->getContentSize(); + + // Add a label in which the textfield events will be displayed + _displayValueLabel = Text::create("Set place hold color","fonts/Marker Felt.ttf",32); + _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); + _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f)); + _uiLayer->addChild(_displayValueLabel); + + // Add the alert + Text* alert = Text::create("TextField","fonts/Marker Felt.ttf",30); + alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); + _uiLayer->addChild(alert); + + // Create the textfield + TextField* textField = TextField::create("input words here","Arial",30); + textField->setPlaceHolder("input text here"); + textField->setPlaceHolderColor(Color4B::GREEN); + textField->setTextColor(Color4B::RED); + textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); + textField->addEventListener(CC_CALLBACK_2(UITextFieldTest_PlaceHolderColor::textFieldEvent, this)); + _uiLayer->addChild(textField); + return true; + } + return false; +} + +void UITextFieldTest_PlaceHolderColor::textFieldEvent(Ref *pSender, TextField::EventType type) +{ + switch (type) + { + case TextField::EventType::ATTACH_WITH_IME: + { + TextField* textField = dynamic_cast(pSender); + Size screenSize = CCDirector::getInstance()->getWinSize(); + textField->runAction(CCMoveTo::create(0.225f, + Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f))); + _displayValueLabel->setString(String::createWithFormat("attach with IME")->getCString()); + } + break; + + case TextField::EventType::DETACH_WITH_IME: + { + TextField* textField = dynamic_cast(pSender); + Size screenSize = CCDirector::getInstance()->getWinSize(); + textField->runAction(CCMoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f))); + _displayValueLabel->setString(String::createWithFormat("detach with IME")->getCString()); + } + break; + + case TextField::EventType::INSERT_TEXT: + _displayValueLabel->setString(String::createWithFormat("insert words")->getCString()); + break; + + case TextField::EventType::DELETE_BACKWARD: + _displayValueLabel->setString(String::createWithFormat("delete word")->getCString()); + break; + + default: + break; + } +} diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h index e44798cb7f..6c62bc14d2 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h @@ -91,4 +91,17 @@ protected: UI_SCENE_CREATE_FUNC(UITextFieldTest_TrueTypeFont) Text* _displayValueLabel; }; + +class UITextFieldTest_PlaceHolderColor : public UIScene +{ +public: + UITextFieldTest_PlaceHolderColor(); + ~UITextFieldTest_PlaceHolderColor(); + bool init(); + void textFieldEvent(Ref* pSender, TextField::EventType type); + +protected: + UI_SCENE_CREATE_FUNC(UITextFieldTest_PlaceHolderColor) + Text* _displayValueLabel; +}; #endif /* defined(__TestCpp__UITextFieldTest__) */