Cocos2d-x Learning (4): input box with Cursor

Source: Internet
Author: User

Cocos2d-x provides us with a cross-platform input box, CCTextFieldTTF, at first look at the feeling that the provision of the function is very little, when you see the tests TextInputTest example, it is really complicated to use, the reason is nothing more than the tedious setup and judgment. However, the most important thing in the input box is to listen to the input across platforms, not the style! What we want to do is to encapsulate a simple input box based on the needs of the game!

Today, I will take a simple input box with a cursor as an example. I will briefly explain how the input box works and how it is encapsulated to make it easy to use controls, however, this is just a simple model. Currently, only single row input is supported!

 


1. CCTextFieldTTF Analysis
This class is purely an extension of a UI control of the cocos2d-x, when we see its parent class, it will suddenly realize, oh! It turns out to be a subclass of CCLabelTTF! Yes, CCTextFieldTTF is a "dynamic" CCLabelTTF. The so-called dynamic is to dynamically set the text display on the Label when listening to the input. That's all! The listening of the input method is implemented by another parent class CCIMEDelegate.


[Cpp] view plaincopyprint? <Span style = "font-size: 16px;"> class CC_DLL CCTextFieldTTF: public CCLabelTTF, public CCIMEDelegate </span>
<Span style = "font-size: 16px;"> class CC_DLL CCTextFieldTTF: public CCLabelTTF, public CCIMEDelegate </span>
The method is summarized into three categories:

(1) Static initialization method,

[Cpp]
<Span style = "font-size: 16px;">/** creates a CCTextFieldTTF from a fontname, alignment, dimension and font size */
Static CCTextFieldTTF * textfieldwit2d-aceholder (const char * placeholder, const CCSize & dimensions, CCTextAlignment alignment, const char * fontName, float fontSize );
/** Creates a CCLabelTTF from a fontname and font size */
Static CCTextFieldTTF * textfieldwit2d-aceholder (const char * placeholder, const char * fontName, float fontSize );
/** Initializes the CCTextFieldTTF with a font name, alignment, dimension and font size */
Bool initwit2d-aceholder (const char * placeholder, const CCSize & dimensions, CCTextAlignment alignment, const char * fontName, float fontSize );
/** Initializes the CCTextFieldTTF with a font name and font size */
Bool initwit2d-aceholder (const char * placeholder, const char * fontName, float fontSize); </span>
<Span style = "font-size: 16px;">/** creates a CCTextFieldTTF from a fontname, alignment, dimension and font size */
Static CCTextFieldTTF * textfieldwit2d-aceholder (const char * placeholder, const CCSize & dimensions, CCTextAlignment alignment, const char * fontName, float fontSize );
/** Creates a CCLabelTTF from a fontname and font size */
Static CCTextFieldTTF * textfieldwit2d-aceholder (const char * placeholder, const char * fontName, float fontSize );
/** Initializes the CCTextFieldTTF with a font name, alignment, dimension and font size */
Bool initwit2d-aceholder (const char * placeholder, const CCSize & dimensions, CCTextAlignment alignment, const char * fontName, float fontSize );
/** Initializes the CCTextFieldTTF with a font name and font size */
Bool initwit2d-aceholder (const char * placeholder, const char * fontName, float fontSize); </span>


(2) Input Method control method,

[Cpp]
<Span style = "font-size: 16px;"> /**
@ Brief Open keyboard and receive input text.
*/
Virtual bool attachWithIME ();
 
/**
@ Brief End text input and close keyboard.
*/
Virtual bool detachWithIME (); </span>
<Span style = "font-size: 16px;"> /**
@ Brief Open keyboard and receive input text.
*/
Virtual bool attachWithIME ();

/**
@ Brief End text input and close keyboard.
*/
Virtual bool detachWithIME (); </span>


(3) Handling of Character Input

[Cpp]
<Span style = "font-size: 16px;"> <span style = "white-space: pre"> </span> virtual void insertText (const char * text, int len );
<Span style = "white-space: pre"> </span> virtual void deleteBackward ();
<Span style = "white-space: pre"> </span> virtual const char * getContentText (); </span>
<Span style = "font-size: 16px;"> <span style = "white-space: pre"> </span> virtual void insertText (const char * text, int len );
<Span style = "white-space: pre"> </span> virtual void deleteBackward ();
<Span style = "white-space: pre"> </span> virtual const char * getContentText (); </span>
For detailed usage, refer to the TextInputTest example in tests. The usage is very complicated. If we implement it in every Layer that needs to be input, it would be crazy! We should encapsulate the game based on our needs to meet our needs!

 


2. Custom CursorTextField
(1) first, we need to know which operations the input box control needs to provide. to complete these operations, we need to inherit which parent classes
Stealing a lazy, the input task is handed over to the CCTextFieldTTF of the cocos2d-x, the input box in the string processing we need CCTextFieldDelegate, and from the Layer to determine the liberation of touch, we also need CCTouchDelegate, so, this is what our custom input box declares.


[Cpp]
<Span style = "font-size: 16px;"> class CursorTextField: public CCTextFieldTTF, public CCTextFieldDelegate, public CCTouchDelegate
{
Private:
// Click Start at www.2cto.com
CCPoint m_beginPos;

// Cursor genie
CCSprite * m_pCursorSprite;

// Cursor Animation
CCAction * m_pcursinjection;

// Cursor coordinates
CCPoint m_cursorPos;

// Input box content
Std: string * m_pInputText;
Public:
CursorTextField ();
~ CursorTextField ();

// Static
Static CursorTextField * textfieldwit2d-aceholder (const char * placeholder, const char * fontName, float fontSize );

// CCLayer
Void onEnter ();
Void onExit ();

// Initialize the cursor genie
Void initCursorSprite (int nHeight );

// CCTextFieldDelegate
Virtual bool onTextFieldAttachWithIME (CCTextFieldTTF * pSender );
Virtual bool onTextFieldDetachWithIME (CCTextFieldTTF * pSender );
Virtual bool onTextFieldInsertText (CCTextFieldTTF * pSender, const char * text, int nLen );
Virtual bool onTextFieldDeleteBackward (CCTextFieldTTF * pSender, const char * delText, int nLen );

// CCLayer Touch
Bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
Void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent );

// Determine whether to click in TextField
Bool isInTextField (CCTouch * pTouch );
// Obtain the TextField rectangle.
CCRect getRect ();

// Enable the Input Method
Void openIME ();
// Disable the Input Method
Void closeIME ();
};
 
# Endif </span>
<Span style = "font-size: 16px;"> class CursorTextField: public CCTextFieldTTF, public CCTextFieldDelegate, public CCTouchDelegate
{
Private:
// Click the start position
CCPoint m_beginPos;

// Cursor genie
CCSprite * m_pCursorSprite;

// Cursor Animation
CCAction * m_pcursinjection;

// Cursor coordinates
CCPoint m_cursorPos;

// Input box content
Std: string * m_pInputText;
Public:
CursorTextField ();
~ CursorTextField ();

// Static
Static CursorTextField * textfieldwit2d-aceholder (const char * placeholder, const char * fontName, float fontSize );

// CCLayer
Void onEnter ();
Void onExit ();

// Initialize the cursor genie
Void initCursorSprite (int nHeight );

// CCTextFieldDelegate
Virtual bool onTextFieldAttachWithIME (CCTextFieldTTF * pSender );
Virtual bool onTextFieldDetachWithIME (CCTextFieldTTF * pSender );
Virtual bool onTextFieldInsertText (CCTextFieldTTF * pSender, const char * text, int nLen );
Virtual bool onTextFieldDeleteBackward (CCTextFieldTTF * pSender, const char * delText, int nLen );

// CCLayer Touch
Bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
Void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent );

// Determine whether to click in TextField
Bool isInTextField (CCTouch * pTouch );
// Obtain the TextField rectangle.
CCRect getRect ();

// Enable the Input Method
Void openIME ();
// Disable the Input Method
Void closeIME ();
};

# Endif </span>

(Ps: This is just a simple version, so it does not provide more complex initialization methods. It can be plump as needed)

(2) Implementation of some necessary methods
A. Continued the cocos2d-x style, the static method and the initialization method separated, here is almost a copy of CCTextFieldTTF, but here there is a small knowledge point, is to create a solid color genie approach, the Code is as follows


[Cpp]
<Span style = "font-size: 16px;"> int column = 4;
Int pixels [nHeight] [column];
For (int I = 0; I <nHeight; ++ I ){
For (int j = 0; j <column; ++ j ){
Pixels [I] [j] = 0 xffffffff;
}
}
 
CCTexture2D * texture = new CCTexture2D ();
Texture-> initWithData (pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake (column, nHeight ));

M_pCursorSprite = CCSprite: spriteWithTexture (texture );
</Span>
<Span style = "font-size: 16px;"> int column = 4;
Int pixels [nHeight] [column];
For (int I = 0; I <nHeight; ++ I ){
For (int j = 0; j <column; ++ j ){
Pixels [I] [j] = 0 xffffffff;
}
}

CCTexture2D * texture = new CCTexture2D ();
Texture-> initWithData (pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake (column, nHeight ));

M_pCursorSprite = CCSprite: spriteWithTexture (texture );
</Span> I made a white cursor with a solid color Sprite. Haha, although it is very handy, I do not need to import the cursor image resources when using it!

B. Touch to judge www.2cto.com

This is necessary. We need to determine whether the touch point is in the input box. If it is in the input box, the input method should be displayed. If it is not, the input method must be disabled. This user experience is good!

Judgment Method


[Cpp]
<Span style = "font-size: 16px;"> bool CursorTextField: isInTextField (cocos2d: CCTouch * pTouch)
{
Return CCRect: CCRectContainsPoint (getRect (), convertTouchToNodeSpaceAR (pTouch ));
}
</Span>
<Span style = "font-size: 16px;"> bool CursorTextField: isInTextField (cocos2d: CCTouch * pTouch)
{
Return CCRect: CCRectContainsPoint (getRect (), convertTouchToNodeSpaceAR (pTouch ));
}
</Span> how to enable and disable the input method (with the cursor displayed by the way)


[Cpp]
<Span style = "font-size: 16px;"> void CursorTextField: openIME ()
{
M_pCursorSprite-> setIsVisible (true );
This-> attachWithIME ();
}
 
Void CursorTextField: closeIME ()
{
M_pCursorSprite-> setIsVisible (false );
This-> detachWithIME ();
} </Span>
<Span style = "font-size: 16px;"> void CursorTextField: openIME ()
{
M_pCursorSprite-> setIsVisible (true );
This-> attachWithIME ();
}

Void CursorTextField: closeIME ()
{
M_pCursorSprite-> setIsVisible (false );
This-> detachWithIME ();
} </Span> c. add and delete strings in the input box.

We can listen to the methods provided by CCTextFieldDelegate, but we need to record the content in the input box, because we need to determine the cursor position based on the length of the string in the input box, when listening to the method for adding characters, if the getString method is called, it will always be one character different, so we need to record it separately, we manually call the setString method, CCTextFieldTTF dynamically changes the width of a string based on its width.


[Cpp]
<Span style = "font-size: 16px;"> bool CursorTextField: onTextFieldInsertText (cocos2d: CCTextFieldTTF * pSender, const char * text, int nLen)
{
CCLOG ("Width: % f", pSender-> getContentSize (). width );
CCLOG ("Text: % s", text );
CCLOG ("Length: % d", nLen );

M_pInputText-> append (text );
SetString (m_pInputText-> c_str ());

M_pCursorSprite-> setPositionX (getContentSize (). width );

Return true;
}
 
Bool CursorTextField: onTextFieldDeleteBackward (cocos2d: CCTextFieldTTF * pSender, const char * delText, int nLen)
{
M_pInputText-> resize (m_pInputText-> size ()-nLen );
SetString (m_pInputText-> c_str ());

M_pCursorSprite-> setPositionX (getContentSize (). width );

If (m_pInputText-> empty ()){
M_pCursorSprite-> setPositionX (0 );
}

Return false;
} </Span>
<Span style = "font-size: 16px;"> bool CursorTextField: onTextFieldInsertText (cocos2d: CCTextFieldTTF * pSender, const char * text, int nLen)
{
CCLOG ("Width: % f", pSender-> getContentSize (). width );
CCLOG ("Text: % s", text );
CCLOG ("Length: % d", nLen );

M_pInputText-> append (text );
SetString (m_pInputText-> c_str ());

M_pCursorSprite-> setPositionX (getContentSize (). width );

Return true;
}

Bool CursorTextField: onTextFieldDeleteBackward (cocos2d: CCTextFieldTTF * pSender, const char * delText, int nLen)
{
M_pInputText-> resize (m_pInputText-> size ()-nLen );
SetString (m_pInputText-> c_str ());

M_pCursorSprite-> setPositionX (getContentSize (). width );

If (m_pInputText-> empty ()){
M_pCursorSprite-> setPositionX (0 );
}

Return false;
} </Span>
OK. The custom input box class is basically ready. Some simple parts are omitted and the source code download link is required at the end!

3. Use
What we want is to use simple, what we want is not to set touch judgment on each layer, what we want is to use like the cocos2d-x provides us with the genie, so use the code below


[Cpp]
<Span style = "font-size: 16px;"> <span style = "white-space: pre"> </span> m_pCursorTextField = CursorTextField :: textfieldwit2d-aceholder ("Input Text", "Thonburi", 64 );
M_pCursorTextField-> setPosition (ccp (winSize. width/2, winSize. height/2 + 100 ));
This-> addChild (m_pCursorTextField); </span>
<Span style = "font-size: 16px;"> <span style = "white-space: pre"> </span> m_pCursorTextField = CursorTextField :: textfieldwit2d-aceholder ("Input Text", "Thonburi", 64 );
M_pCursorTextField-> setPosition (ccp (winSize. width/2, winSize. height/2 + 100 ));
This-> addChild (m_pCursorTextField); </span>
As follows:


Author: oneRain88

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.