2012-08-15 14:33:56 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 James Chen
|
|
|
|
|
|
|
|
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 __CCEDITTEXT_H__
|
|
|
|
#define __CCEDITTEXT_H__
|
|
|
|
|
|
|
|
#include "cocos2d.h"
|
|
|
|
#include "ExtensionMacros.h"
|
|
|
|
#include "../CCControlExtension/CCControlExtensions.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
class EditBox;
|
|
|
|
class EditBoxImpl;
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
class EditBoxDelegate
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual ~EditBoxDelegate() {};
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is called when an edit box gains focus after keyboard is shown.
|
|
|
|
* @param editBox The edit box object that generated the event.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void editBoxEditingDidBegin(EditBox* editBox) {};
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is called when an edit box loses focus after keyboard is hidden.
|
|
|
|
* @param editBox The edit box object that generated the event.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void editBoxEditingDidEnd(EditBox* editBox) {};
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is called when the edit box text was changed.
|
|
|
|
* @param editBox The edit box object that generated the event.
|
|
|
|
* @param text The new text.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void editBoxTextChanged(EditBox* editBox, const std::string& text) {};
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is called when the return button was pressed or the outside area of keyboard was touched.
|
|
|
|
* @param editBox The edit box object that generated the event.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void editBoxReturn(EditBox* editBox) = 0;
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Class for edit box.
|
|
|
|
*
|
|
|
|
* You can use this widget to gather small amounts of text from the user.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
class EditBox
|
|
|
|
: public ControlButton
|
|
|
|
, public IMEDelegate
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-26 17:45:01 +08:00
|
|
|
enum class KeyboardReturnType
|
|
|
|
{
|
|
|
|
DEFAULT,
|
|
|
|
DONE,
|
|
|
|
SEND,
|
|
|
|
SEARCH,
|
|
|
|
GO
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief The EditBox::InputMode defines the type of text that the user is allowed
|
|
|
|
* to enter.
|
|
|
|
*/
|
|
|
|
enum class InputMode
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The user is allowed to enter any text, including line breaks.
|
|
|
|
*/
|
|
|
|
ANY,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user is allowed to enter an e-mail address.
|
|
|
|
*/
|
|
|
|
EMAIL_ADDRESS,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user is allowed to enter an integer value.
|
|
|
|
*/
|
|
|
|
NUMERIC,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user is allowed to enter a phone number.
|
|
|
|
*/
|
|
|
|
PHONE_NUMBER,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user is allowed to enter a URL.
|
|
|
|
*/
|
|
|
|
URL,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user is allowed to enter a real number value.
|
|
|
|
* This extends kEditBoxInputModeNumeric by allowing a decimal point.
|
|
|
|
*/
|
|
|
|
DECIMAL,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user is allowed to enter any text, except for line breaks.
|
|
|
|
*/
|
|
|
|
SINGLE_LINE,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief The EditBox::InputFlag defines how the input text is displayed/formatted.
|
|
|
|
*/
|
|
|
|
enum class InputFlag
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Indicates that the text entered is confidential data that should be
|
|
|
|
* obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE.
|
|
|
|
*/
|
|
|
|
PASSWORD,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates that the text entered is sensitive data that the
|
|
|
|
* implementation must never store into a dictionary or table for use
|
|
|
|
* in predictive, auto-completing, or other accelerated input schemes.
|
|
|
|
* A credit card number is an example of sensitive data.
|
|
|
|
*/
|
|
|
|
SENSITIVE,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This flag is a hint to the implementation that during text editing,
|
|
|
|
* the initial letter of each word should be capitalized.
|
|
|
|
*/
|
|
|
|
INITIAL_CAPS_WORD,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This flag is a hint to the implementation that during text editing,
|
|
|
|
* the initial letter of each sentence should be capitalized.
|
|
|
|
*/
|
|
|
|
INITIAL_CAPS_SENTENCE,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Capitalize all characters automatically.
|
|
|
|
*/
|
|
|
|
INTIAL_CAPS_ALL_CHARACTERS,
|
|
|
|
};
|
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
/**
|
|
|
|
* create a edit box with size.
|
|
|
|
* @return An autorelease pointer of EditBox, you don't need to release it only if you retain it again.
|
|
|
|
*/
|
|
|
|
static EditBox* create(const Size& size, Scale9Sprite* pNormal9SpriteBg, Scale9Sprite* pPressed9SpriteBg = NULL, Scale9Sprite* pDisabled9SpriteBg = NULL);
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBox(void);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual ~EditBox(void);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init edit box with specified size. This method should be invoked right after constructor.
|
|
|
|
* @param size The size of edit box.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
bool initWithSizeAndBackgroundSprite(const Size& size, Scale9Sprite* pNormal9SpriteBg);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
2013-06-05 14:30:14 +08:00
|
|
|
* Gets/Sets the delegate for edit box.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @lua NA
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void setDelegate(EditBoxDelegate* pDelegate);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxDelegate* getDelegate();
|
2013-05-24 11:42:27 +08:00
|
|
|
/**
|
|
|
|
* Registers a script function that will be called for EditBox events.
|
|
|
|
*
|
|
|
|
* This handler will be removed automatically after onExit() called.
|
|
|
|
* @code
|
|
|
|
* -- lua sample
|
|
|
|
* local function editboxEventHandler(eventType)
|
|
|
|
* if eventType == "began" then
|
|
|
|
* -- triggered when an edit box gains focus after keyboard is shown
|
|
|
|
* elseif eventType == "ended" then
|
|
|
|
* -- triggered when an edit box loses focus after keyboard is hidden.
|
|
|
|
* elseif eventType == "changed" then
|
|
|
|
* -- triggered when the edit box text was changed.
|
|
|
|
* elseif eventType == "return" then
|
|
|
|
* -- triggered when the return button was pressed or the outside area of keyboard was touched.
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
2013-06-20 14:15:53 +08:00
|
|
|
* local editbox = EditBox:create(Size(...), Scale9Sprite:create(...))
|
2013-05-24 11:42:27 +08:00
|
|
|
* editbox = registerScriptEditBoxHandler(editboxEventHandler)
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* @param handler A number that indicates a lua function.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-05-24 11:42:27 +08:00
|
|
|
*/
|
|
|
|
void registerScriptEditBoxHandler(int handler);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-05-24 11:42:27 +08:00
|
|
|
/**
|
|
|
|
* Unregisters a script function that will be called for EditBox events.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-05-24 11:42:27 +08:00
|
|
|
*/
|
|
|
|
void unregisterScriptEditBoxHandler(void);
|
|
|
|
/**
|
|
|
|
* get a script Handler
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-05-24 11:42:27 +08:00
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
int getScriptEditBoxHandler(void){ return _scriptEditBoxHandler ;}
|
2012-08-15 14:33:56 +08:00
|
|
|
/**
|
|
|
|
* Set the text entered in the edit box.
|
|
|
|
* @param pText The given text.
|
|
|
|
*/
|
|
|
|
void setText(const char* pText);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text entered in the edit box.
|
|
|
|
* @return The text entered in the edit box.
|
|
|
|
*/
|
|
|
|
const char* getText(void);
|
2013-02-20 21:41:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the font.
|
|
|
|
* @param pFontName The font name.
|
|
|
|
* @param fontSize The font size.
|
|
|
|
*/
|
|
|
|
void setFont(const char* pFontName, int fontSize);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-04-02 21:31:30 +08:00
|
|
|
/**
|
|
|
|
* Set the font name.
|
|
|
|
* @param pFontName The font name.
|
|
|
|
*/
|
|
|
|
void setFontName(const char* pFontName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the font size.
|
|
|
|
* @param fontSize The font size.
|
|
|
|
*/
|
|
|
|
void setFontSize(int fontSize);
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
/**
|
|
|
|
* Set the font color of the widget's text.
|
|
|
|
*/
|
2013-07-05 16:49:22 +08:00
|
|
|
void setFontColor(const Color3B& color);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-02-25 14:05:38 +08:00
|
|
|
/**
|
|
|
|
* Set the placeholder's font.
|
|
|
|
* @param pFontName The font name.
|
|
|
|
* @param fontSize The font size.
|
|
|
|
*/
|
|
|
|
void setPlaceholderFont(const char* pFontName, int fontSize);
|
|
|
|
|
2013-04-02 21:31:30 +08:00
|
|
|
/**
|
|
|
|
* Set the placeholder's font name.
|
|
|
|
* @param pFontName The font name.
|
|
|
|
*/
|
|
|
|
void setPlaceholderFontName(const char* pFontName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the placeholder's font size.
|
|
|
|
* @param fontSize The font size.
|
|
|
|
*/
|
|
|
|
void setPlaceholderFontSize(int fontSize);
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
/**
|
|
|
|
* Set the font color of the placeholder text when the edit box is empty.
|
2012-08-17 15:24:33 +08:00
|
|
|
* Not supported on IOS.
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-07-05 16:49:22 +08:00
|
|
|
void setPlaceholderFontColor(const Color3B& color);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a text in the edit box that acts as a placeholder when an
|
|
|
|
* edit box is empty.
|
|
|
|
* @param pText The given text.
|
|
|
|
*/
|
|
|
|
void setPlaceHolder(const char* pText);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a text in the edit box that acts as a placeholder when an
|
|
|
|
* edit box is empty.
|
|
|
|
*/
|
|
|
|
const char* getPlaceHolder(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the input mode of the edit box.
|
2013-07-26 17:45:01 +08:00
|
|
|
* @param inputMode One of the EditBox::InputMode constants.
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-07-26 17:45:01 +08:00
|
|
|
void setInputMode(InputMode inputMode);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the maximum input length of the edit box.
|
|
|
|
* Setting this value enables multiline input mode by default.
|
|
|
|
* Available on Android, iOS and Windows Phone.
|
|
|
|
*
|
|
|
|
* @param maxLength The maximum length.
|
|
|
|
*/
|
|
|
|
void setMaxLength(int maxLength);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum input length of the edit box.
|
|
|
|
*
|
|
|
|
* @return Maximum input length.
|
|
|
|
*/
|
|
|
|
int getMaxLength();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the input flags that are to be applied to the edit box.
|
2013-07-26 17:45:01 +08:00
|
|
|
* @param inputFlag One of the EditBox::InputFlag constants.
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-07-26 17:45:01 +08:00
|
|
|
void setInputFlag(InputFlag inputFlag);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the return type that are to be applied to the edit box.
|
2013-07-26 17:45:01 +08:00
|
|
|
* @param returnType One of the EditBox::KeyboardReturnType constants.
|
2012-08-15 14:33:56 +08:00
|
|
|
*/
|
2013-07-26 17:45:01 +08:00
|
|
|
void setReturnType(EditBox::KeyboardReturnType returnType);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
/* override functions */
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void setPosition(const Point& pos);
|
2013-03-03 18:58:51 +08:00
|
|
|
virtual void setVisible(bool visible);
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void setContentSize(const Size& size);
|
|
|
|
virtual void setAnchorPoint(const Point& anchorPoint);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-08-15 14:33:56 +08:00
|
|
|
virtual void visit(void);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-03-12 17:23:12 +08:00
|
|
|
virtual void onEnter(void);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-08-15 14:33:56 +08:00
|
|
|
virtual void onExit(void);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void keyboardWillShow(IMEKeyboardNotificationInfo& info);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void keyboardDidShow(IMEKeyboardNotificationInfo& info);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void keyboardWillHide(IMEKeyboardNotificationInfo& info);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void keyboardDidHide(IMEKeyboardNotificationInfo& info);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/* callback funtions
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-26 14:37:26 +08:00
|
|
|
void touchDownAction(Object *sender, Control::EventType controlEvent);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
protected:
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxImpl* _editBoxImpl;
|
|
|
|
EditBoxDelegate* _delegate;
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-07-26 17:45:01 +08:00
|
|
|
InputMode _editBoxInputMode;
|
|
|
|
InputFlag _editBoxInputFlag;
|
|
|
|
EditBox::KeyboardReturnType _keyboardReturnType;
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _text;
|
|
|
|
std::string _placeHolder;
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _fontName;
|
|
|
|
std::string _placeholderFontName;
|
2013-04-02 21:31:30 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
int _fontSize;
|
|
|
|
int _placeholderFontSize;
|
2013-04-02 21:31:30 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B _colText;
|
|
|
|
Color3B _colPlaceHolder;
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
int _maxLength;
|
|
|
|
float _adjustHeight;
|
|
|
|
int _scriptEditBoxHandler;
|
2012-08-15 14:33:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|
|
|
|
|
|
|
|
#endif /* __CCEDITTEXT_H__ */
|
|
|
|
|