axmol/cocos/gui/UITextField.h

176 lines
5.5 KiB
C
Raw Normal View History

2013-09-20 22:23:13 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
2013-09-20 22:23:13 +08:00
#ifndef __UITEXTFIELD_H__
#define __UITEXTFIELD_H__
#include "gui/UIWidget.h"
2013-09-20 22:23:13 +08:00
2013-12-23 15:02:52 +08:00
NS_CC_BEGIN
2013-09-20 22:23:13 +08:00
namespace gui {
2013-09-20 22:23:13 +08:00
2013-11-06 16:04:06 +08:00
/**
* @js NA
2013-12-23 15:02:52 +08:00
* @lua NA
2013-11-06 16:04:06 +08:00
*/
2013-12-23 15:02:52 +08:00
class UICCTextField: public TextFieldTTF, public TextFieldDelegate
2013-09-20 22:23:13 +08:00
{
public:
UICCTextField();
~UICCTextField();
2013-12-23 15:02:52 +08:00
2013-09-20 22:23:13 +08:00
virtual void onEnter();
// static
static UICCTextField* create(const char *placeholder, const char *fontName, float fontSize);
// CCTextFieldDelegate
virtual bool onTextFieldAttachWithIME(TextFieldTTF *pSender) override;
virtual bool onTextFieldDetachWithIME(TextFieldTTF * pSender) override;
virtual bool onTextFieldInsertText(TextFieldTTF * pSender, const char * text, int nLen) override;
virtual bool onTextFieldDeleteBackward(TextFieldTTF * pSender, const char * delText, int nLen) override;
2013-09-20 22:23:13 +08:00
void insertText(const char* text, int len);
void deleteBackward();
void openIME();
void closeIME();
void setMaxLengthEnabled(bool enable);
bool isMaxLengthEnabled();
void setMaxLength(int length);
int getMaxLength();
int getCharCount();
void setPasswordEnabled(bool enable);
bool isPasswordEnabled();
void setPasswordStyleText(const char* styleText);
void setPasswordText(const char* text);
void setAttachWithIME(bool attach);
bool getAttachWithIME();
void setDetachWithIME(bool detach);
bool getDetachWithIME();
void setInsertText(bool insert);
bool getInsertText();
void setDeleteBackward(bool deleteBackward);
bool getDeleteBackward();
protected:
2013-11-06 16:04:06 +08:00
bool _maxLengthEnabled;
int _maxLength;
bool _passwordEnabled;
std::string _passwordStyleText;
bool _attachWithIME;
bool _detachWithIME;
bool _insertText;
bool _deleteBackward;
2013-09-20 22:23:13 +08:00
};
typedef enum
{
TEXTFIELD_EVENT_ATTACH_WITH_IME,
TEXTFIELD_EVENT_DETACH_WITH_IME,
2013-11-06 16:04:06 +08:00
TEXTFIELD_EVENT_INSERT_TEXT,
2013-09-20 22:23:13 +08:00
TEXTFIELD_EVENT_DELETE_BACKWARD,
}TextFiledEventType;
2013-12-23 15:02:52 +08:00
typedef void (Object::*SEL_TextFieldEvent)(Object*, TextFiledEventType);
2013-09-20 22:23:13 +08:00
#define textfieldeventselector(_SELECTOR) (SEL_TextFieldEvent)(&_SELECTOR)
2013-12-23 15:02:52 +08:00
/** class UITextField : public Widget
* @js NA
* @lua NA
*/
class TextField : public Widget
2013-09-20 22:23:13 +08:00
{
public:
2013-12-23 15:02:52 +08:00
TextField();
virtual ~TextField();
static TextField* create();
2013-11-14 13:42:10 +08:00
virtual bool init() override;
virtual void initRenderer() override;
2013-12-23 15:02:52 +08:00
void setTouchSize(const Size &size);
void setText(const std::string& text);
void setPlaceHolder(const std::string& value);
2013-09-20 22:23:13 +08:00
void setFontSize(int size);
void setFontName(const std::string& name);
2013-09-20 22:23:13 +08:00
virtual void didNotSelectSelf();
const std::string& getStringValue();
2013-12-23 15:02:52 +08:00
virtual bool onTouchBegan(Touch *touch, Event *unusedEvent) override;
2013-09-20 22:23:13 +08:00
void setMaxLengthEnabled(bool enable);
bool isMaxLengthEnabled();
void setMaxLength(int length);
int getMaxLength();
void setPasswordEnabled(bool enable);
bool isPasswordEnabled();
void setPasswordStyleText(const char* styleText);
2013-11-14 13:42:10 +08:00
virtual void update(float dt) override;
2013-09-20 22:23:13 +08:00
bool getAttachWithIME();
void setAttachWithIME(bool attach);
bool getDetachWithIME();
void setDetachWithIME(bool detach);
bool getInsertText();
void setInsertText(bool insertText);
bool getDeleteBackward();
void setDeleteBackward(bool deleteBackward);
2013-12-23 15:02:52 +08:00
void addEventListenerTextField(Object* target, SEL_TextFieldEvent selecor);
2013-11-06 16:04:06 +08:00
2013-12-23 15:02:52 +08:00
virtual void setAnchorPoint(const Point &pt) override;
2013-11-06 16:04:06 +08:00
2013-09-20 22:23:13 +08:00
/**
* Returns the "class name" of widget.
*/
2013-12-23 15:02:52 +08:00
virtual std::string getDescription() const override;
2013-11-06 16:04:06 +08:00
2013-12-23 15:02:52 +08:00
virtual const Size& getContentSize() const override;
virtual Node* getVirtualRenderer() override;
2013-11-06 16:04:06 +08:00
void attachWithIME();
2013-09-20 22:23:13 +08:00
protected:
// event
void attachWithIMEEvent();
void detachWithIMEEvent();
void insertTextEvent();
void deleteBackwardEvent();
2013-11-14 13:42:10 +08:00
virtual void onSizeChanged() override;
2013-09-20 22:23:13 +08:00
void textfieldRendererScaleChangedWithSize();
2013-12-23 15:02:52 +08:00
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
2013-09-20 22:23:13 +08:00
protected:
UICCTextField* _textFieldRenderer;
2013-11-06 16:04:06 +08:00
2013-09-20 22:23:13 +08:00
float _touchWidth;
float _touchHeight;
bool _useTouchArea;
2013-12-23 15:02:52 +08:00
Object* _textFieldEventListener;
2013-11-08 18:43:06 +08:00
SEL_TextFieldEvent _textFieldEventSelector;
2013-09-20 22:23:13 +08:00
2013-11-06 16:04:06 +08:00
std::string _passwordStyleText;
2013-09-20 22:23:13 +08:00
};
}
2013-09-20 22:23:13 +08:00
2013-12-23 15:02:52 +08:00
NS_CC_END
#endif /* defined(__TextField__) */