axmol/cocos2dx/text_input_node/CCTextFieldTTF.h

177 lines
5.5 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
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__
#include "label_nodes/CCLabelTTF.h"
#include "text_input_node/CCIMEDelegate.h"
#include "touch_dispatcher/CCTouchDelegateProtocol.h"
NS_CC_BEGIN
class TextFieldTTF;
2012-06-20 18:09:11 +08:00
/**
* @addtogroup input
* @{
*/
class CC_DLL TextFieldDelegate
{
public:
/**
@brief If the sender doesn't want to attach to the IME, return true;
*/
virtual bool onTextFieldAttachWithIME(TextFieldTTF * sender)
2013-04-19 09:59:08 +08:00
{
CC_UNUSED_PARAM(sender);
return false;
}
/**
@brief If the sender doesn't want to detach from the IME, return true;
*/
virtual bool onTextFieldDetachWithIME(TextFieldTTF * sender)
{
CC_UNUSED_PARAM(sender);
return false;
}
/**
@brief If the sender doesn't want to insert the text, return true;
*/
virtual bool onTextFieldInsertText(TextFieldTTF * sender, const char * text, int nLen)
{
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;
*/
virtual bool onTextFieldDeleteBackward(TextFieldTTF * sender, const char * delText, int nLen)
{
CC_UNUSED_PARAM(sender);
CC_UNUSED_PARAM(delText);
CC_UNUSED_PARAM(nLen);
return false;
}
/**
@brief If the sender doesn't want to draw, return true.
*/
virtual bool onDraw(TextFieldTTF * sender)
{
CC_UNUSED_PARAM(sender);
return false;
}
};
/**
@brief A simple text input field with TTF font.
*/
class CC_DLL TextFieldTTF : public LabelTTF, public IMEDelegate
{
public:
TextFieldTTF();
virtual ~TextFieldTTF();
2011-04-19 09:32:33 +08:00
//char * description();
/** creates a TextFieldTTF from a fontname, alignment, dimension and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const char *placeholder, const Size& dimensions, TextAlignment alignment, const char *fontName, float fontSize);
/** creates a LabelTTF from a fontname and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize);
/** initializes the TextFieldTTF with a font name, alignment, dimension and font size */
bool initWithPlaceHolder(const char *placeholder, const Size& dimensions, TextAlignment alignment, const char *fontName, float fontSize);
/** initializes the TextFieldTTF with a font name and font size */
2011-04-19 09:32:33 +08:00
bool initWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize);
/**
@brief Open keyboard and receive input text.
*/
virtual bool attachWithIME();
/**
@brief End text input and close keyboard.
*/
virtual bool detachWithIME();
//////////////////////////////////////////////////////////////////////////
2011-04-19 09:32:33 +08:00
// properties
//////////////////////////////////////////////////////////////////////////
2013-04-19 09:59:08 +08:00
CC_SYNTHESIZE(TextFieldDelegate *, _delegate, Delegate);
CC_SYNTHESIZE_READONLY(int, _charCount, CharCount);
virtual const ccColor3B& getColorSpaceHolder();
virtual void setColorSpaceHolder(const ccColor3B& color);
2011-04-28 18:01:29 +08:00
2011-04-19 09:32:33 +08:00
// input text property
public:
virtual void setString(const char *text);
virtual const char* getString(void) const;
2011-04-19 09:32:33 +08:00
protected:
std::string * _inputText;
2011-04-19 09:32:33 +08:00
// place holder text property
// place holder text displayed when there is no text in the text field.
public:
virtual void setPlaceHolder(const char * text);
virtual const char * getPlaceHolder(void);
protected:
std::string * _placeHolder;
ccColor3B _colorSpaceHolder;
public:
2013-04-19 09:59:08 +08:00
virtual void setSecureTextEntry(bool value);
virtual bool isSecureTextEntry();
protected:
bool _secureTextEntry;
protected:
virtual void draw();
//////////////////////////////////////////////////////////////////////////
// IMEDelegate interface
//////////////////////////////////////////////////////////////////////////
virtual bool canAttachWithIME();
virtual bool canDetachWithIME();
virtual void insertText(const char * text, int len);
virtual void deleteBackward();
virtual const char * getContentText();
private:
class LengthStack;
LengthStack * _lens;
};
2012-06-20 18:09:11 +08:00
// end of input group
/// @}
NS_CC_END
#endif // __CC_TEXT_FIELD_H__