2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CC_TEXT_FIELD_H__
|
|
|
|
#define __CC_TEXT_FIELD_H__
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
#include "2d/CCIMEDelegate.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class TextFieldTTF;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup input
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL TextFieldDelegate
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-11-20 08:57:23 +08:00
|
|
|
virtual ~TextFieldDelegate() {}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2012-09-17 15:02:24 +08:00
|
|
|
@brief If the sender doesn't want to attach to the IME, return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual bool onTextFieldAttachWithIME(TextFieldTTF * sender)
|
2013-04-19 09:59:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_UNUSED_PARAM(sender);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-17 15:02:24 +08:00
|
|
|
@brief If the sender doesn't want to detach from the IME, return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual bool onTextFieldDetachWithIME(TextFieldTTF * sender)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(sender);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief If the sender doesn't want to insert the text, return true;
|
|
|
|
*/
|
2014-03-20 10:58:04 +08:00
|
|
|
virtual bool onTextFieldInsertText(TextFieldTTF * sender, const char * text, size_t nLen)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(sender);
|
|
|
|
CC_UNUSED_PARAM(text);
|
|
|
|
CC_UNUSED_PARAM(nLen);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief If the sender doesn't want to delete the delText, return true;
|
|
|
|
*/
|
2014-03-20 16:24:55 +08:00
|
|
|
virtual bool onTextFieldDeleteBackward(TextFieldTTF * sender, const char * delText, size_t nLen)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(sender);
|
|
|
|
CC_UNUSED_PARAM(delText);
|
|
|
|
CC_UNUSED_PARAM(nLen);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-17 15:02:24 +08:00
|
|
|
@brief If the sender doesn't want to draw, return true.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2014-04-09 11:41:20 +08:00
|
|
|
virtual bool onVisit(TextFieldTTF * sender,Renderer *renderer, const Matrix &transform, bool transformUpdated)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(sender);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief A simple text input field with TTF font.
|
|
|
|
*/
|
2014-03-14 21:06:40 +08:00
|
|
|
class CC_DLL TextFieldTTF : public Label, public IMEDelegate
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
TextFieldTTF();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual ~TextFieldTTF();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2011-04-19 09:32:33 +08:00
|
|
|
//char * description();
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** creates a TextFieldTTF from a fontname, alignment, dimension and font size */
|
2013-11-08 03:58:48 +08:00
|
|
|
static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
|
2014-03-14 21:06:40 +08:00
|
|
|
/** creates a TextFieldTTF from a fontname and font size */
|
2013-11-08 03:58:48 +08:00
|
|
|
static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
|
2013-06-20 14:13:12 +08:00
|
|
|
/** initializes the TextFieldTTF with a font name, alignment, dimension and font size */
|
2013-11-08 03:58:48 +08:00
|
|
|
bool initWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
|
2013-06-20 14:13:12 +08:00
|
|
|
/** initializes the TextFieldTTF with a font name and font size */
|
2013-11-08 03:58:48 +08:00
|
|
|
bool initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
|
2011-04-19 09:32:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
@brief Open keyboard and receive input text.
|
|
|
|
*/
|
|
|
|
virtual bool attachWithIME();
|
|
|
|
|
|
|
|
/**
|
2012-09-17 15:02:24 +08:00
|
|
|
@brief End text input and close keyboard.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
virtual bool detachWithIME();
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2011-04-19 09:32:33 +08:00
|
|
|
// properties
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
inline TextFieldDelegate* getDelegate() const { return _delegate; };
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
inline void setDelegate(TextFieldDelegate* delegate) { _delegate = delegate; };
|
|
|
|
|
|
|
|
inline int getCharCount() const { return _charCount; };
|
2014-03-31 11:16:12 +08:00
|
|
|
|
|
|
|
virtual const Color4B& getColorSpaceHolder();
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
virtual void setColorSpaceHolder(const Color3B& color);
|
2014-03-31 11:16:12 +08:00
|
|
|
virtual void setColorSpaceHolder(const Color4B& color);
|
2011-04-28 18:01:29 +08:00
|
|
|
|
2014-03-31 11:16:12 +08:00
|
|
|
virtual void setTextColor(const Color4B& textColor) override;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
2011-04-19 09:32:33 +08:00
|
|
|
// input text property
|
2013-11-08 03:58:48 +08:00
|
|
|
virtual void setString(const std::string& text) override;
|
2013-11-08 04:42:16 +08:00
|
|
|
virtual const std::string& getString() const override;
|
2011-04-19 09:32:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// place holder text property
|
|
|
|
// place holder text displayed when there is no text in the text field.
|
2013-11-08 03:58:48 +08:00
|
|
|
virtual void setPlaceHolder(const std::string& text);
|
|
|
|
virtual const std::string& getPlaceHolder(void) const;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
2013-04-19 09:59:08 +08:00
|
|
|
virtual void setSecureTextEntry(bool value);
|
|
|
|
virtual bool isSecureTextEntry();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-08 21:45:54 +08:00
|
|
|
virtual void visit(Renderer *renderer, const Matrix &parentTransform, bool parentTransformUpdated) override;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-03-14 21:06:40 +08:00
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2013-06-20 14:13:12 +08:00
|
|
|
// IMEDelegate interface
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-11-08 03:58:48 +08:00
|
|
|
virtual bool canAttachWithIME() override;
|
|
|
|
virtual bool canDetachWithIME() override;
|
2014-03-20 10:58:04 +08:00
|
|
|
virtual void insertText(const char * text, size_t len) override;
|
2013-11-08 03:58:48 +08:00
|
|
|
virtual void deleteBackward() override;
|
2013-12-24 15:49:58 +08:00
|
|
|
virtual const std::string& getContentText() override;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
|
|
|
TextFieldDelegate * _delegate;
|
|
|
|
int _charCount;
|
|
|
|
|
|
|
|
std::string _inputText;
|
|
|
|
|
|
|
|
std::string _placeHolder;
|
2014-03-31 11:16:12 +08:00
|
|
|
Color4B _colorSpaceHolder;
|
|
|
|
Color4B _colorText;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
|
|
|
bool _secureTextEntry;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
private:
|
|
|
|
class LengthStack;
|
2013-06-15 14:03:30 +08:00
|
|
|
LengthStack * _lens;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of input group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
#endif // __CC_TEXT_FIELD_H__
|