Merge pull request #7762 from andyque/addAlignmentToUILabel

add UITextField color & place hold color, fix double delete bug
This commit is contained in:
minggo 2014-08-14 09:38:57 +08:00
commit 0b86e129b0
9 changed files with 131 additions and 8 deletions

View File

@ -259,8 +259,10 @@ const std::string& TextFieldTTF::getContentText()
void TextFieldTTF::setTextColor(const Color4B &color) void TextFieldTTF::setTextColor(const Color4B &color)
{ {
_colorText = color; _colorText = color;
if (_inputText.length() > 0) {
Label::setTextColor(_colorText); Label::setTextColor(_colorText);
} }
}
void TextFieldTTF::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) 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.g = color.g;
_colorSpaceHolder.b = color.b; _colorSpaceHolder.b = color.b;
_colorSpaceHolder.a = 255; _colorSpaceHolder.a = 255;
if (0 == _inputText.length())
{
Label::setTextColor(_colorSpaceHolder);
}
} }
void TextFieldTTF::setColorSpaceHolder(const Color4B& color) void TextFieldTTF::setColorSpaceHolder(const Color4B& color)
{ {
_colorSpaceHolder = 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 there is no input text, display placeholder instead
if (! _inputText.length()) if (0 == _inputText.length())
{ {
Label::setTextColor(_colorSpaceHolder); Label::setTextColor(_colorSpaceHolder);
Label::setString(_placeHolder); Label::setString(_placeHolder);
@ -343,7 +352,7 @@ const std::string& TextFieldTTF::getString() const
void TextFieldTTF::setPlaceHolder(const std::string& text) void TextFieldTTF::setPlaceHolder(const std::string& text)
{ {
_placeHolder = text; _placeHolder = text;
if (! _inputText.length()) if (0 == _inputText.length())
{ {
Label::setTextColor(_colorSpaceHolder); Label::setTextColor(_colorSpaceHolder);
Label::setString(_placeHolder); Label::setString(_placeHolder);

View File

@ -647,9 +647,6 @@ void GLViewImpl::onGLFWKeyCallback(GLFWwindow *window, int key, int scancode, in
EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action); EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action);
auto dispatcher = Director::getInstance()->getEventDispatcher(); auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->dispatchEvent(&event); 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) if (GLFW_RELEASE != action && g_keyCodeMap[key] == EventKeyboard::KeyCode::KEY_BACKSPACE)
{ {

View File

@ -446,6 +446,26 @@ const std::string& TextField::getPlaceHolder()const
return _textFieldRenderer->getPlaceHolder(); 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) void TextField::setFontSize(int size)
{ {
if (_fontType == FontType::SYSTEM) { if (_fontType == FontType::SYSTEM) {

View File

@ -135,6 +135,10 @@ public:
void setPlaceHolder(const std::string& value); void setPlaceHolder(const std::string& value);
const std::string& getPlaceHolder()const; 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); void setFontSize(int size);
int getFontSize()const; int getFontSize()const;

View File

@ -159,7 +159,7 @@ g_guisTests[] =
UISceneManager* sceneManager = UISceneManager::sharedUISceneManager(); UISceneManager* sceneManager = UISceneManager::sharedUISceneManager();
sceneManager->setCurrentUISceneId(kUITextFieldTest); sceneManager->setCurrentUISceneId(kUITextFieldTest);
sceneManager->setMinUISceneId(kUITextFieldTest); sceneManager->setMinUISceneId(kUITextFieldTest);
sceneManager->setMaxUISceneId(kUITextFieldTest_TrueTypeFont); sceneManager->setMaxUISceneId(kUITextFieldTest_PlaceHolderColor);
Scene* scene = sceneManager->currentUIScene(); Scene* scene = sceneManager->currentUIScene();
Director::getInstance()->replaceScene(scene); Director::getInstance()->replaceScene(scene);
} }

View File

@ -59,6 +59,7 @@ static const char* s_testArray[] =
"UITextFieldTest_Password", "UITextFieldTest_Password",
"UITextFieldTest_LineWrap", "UITextFieldTest_LineWrap",
"UITextFieldTest_TrueTypeFont", "UITextFieldTest_TrueTypeFont",
"UITextFieldTest_PlaceHolderColor",
"UILayoutTest", "UILayoutTest",
"UILayoutTest_Color", "UILayoutTest_Color",
"UILayoutTest_Gradient", "UILayoutTest_Gradient",
@ -241,7 +242,8 @@ Scene *UISceneManager::currentUIScene()
return UITextFieldTest_LineWrap::sceneWithTitle(s_testArray[_currentUISceneId]); return UITextFieldTest_LineWrap::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUITextFieldTest_TrueTypeFont: case kUITextFieldTest_TrueTypeFont:
return UITextFieldTest_TrueTypeFont::sceneWithTitle(s_testArray[_currentUISceneId]); return UITextFieldTest_TrueTypeFont::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUITextFieldTest_PlaceHolderColor:
return UITextFieldTest_PlaceHolderColor::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUILayoutTest: case kUILayoutTest:
return UILayoutTest::sceneWithTitle(s_testArray[_currentUISceneId]); return UILayoutTest::sceneWithTitle(s_testArray[_currentUISceneId]);

View File

@ -59,6 +59,7 @@ enum
kUITextFieldTest_Password, kUITextFieldTest_Password,
kUITextFieldTest_LineWrap, kUITextFieldTest_LineWrap,
kUITextFieldTest_TrueTypeFont, kUITextFieldTest_TrueTypeFont,
kUITextFieldTest_PlaceHolderColor,
kUILayoutTest, kUILayoutTest,
kUILayoutTest_Color, kUILayoutTest_Color,
kUILayoutTest_Gradient, kUILayoutTest_Gradient,

View File

@ -407,3 +407,80 @@ void UITextFieldTest_TrueTypeFont::textFieldEvent(Ref *pSender, TextField::Event
break; 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<TextField*>(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<TextField*>(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;
}
}

View File

@ -91,4 +91,17 @@ protected:
UI_SCENE_CREATE_FUNC(UITextFieldTest_TrueTypeFont) UI_SCENE_CREATE_FUNC(UITextFieldTest_TrueTypeFont)
Text* _displayValueLabel; 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__) */ #endif /* defined(__TestCpp__UITextFieldTest__) */