add CCTextFieldDelegate.

This commit is contained in:
yangws 2011-04-28 18:01:29 +08:00
parent d73699aabb
commit 2bb4775051
2 changed files with 112 additions and 14 deletions

View File

@ -31,11 +31,56 @@ THE SOFTWARE.
NS_CC_BEGIN;
/**
@brief A simple text input field with system TTF font.
*/
class CCTextFieldTTF;
class CC_DLL CCTextFieldTTF : public CCLabelTTF, public CCIMEDelegate//, public CCTargetedTouchDelegate
class CC_DLL CCTextFieldDelegate
{
public:
/**
@brief If the sender doesn't want to attach with IME, return true;
*/
bool onTextFieldAttachWithIME(CCTextFieldTTF * sender)
{
return false;
}
/**
@brief If the sender doesn't want to detach with IME, return true;
*/
bool onTextFieldDetachWithIME(CCTextFieldTTF * sender)
{
return false;
}
/**
@brief If the sender doesn't want to insert the text, return true;
*/
bool onTextFieldInsertText(CCTextFieldTTF * sender, const char * text, int nLen)
{
return false;
}
/**
@brief If the sender doesn't want to delete the delText, return true;
*/
bool onTextFieldDeleteBackward(CCTextFieldTTF * sender, const char * delText, int nLen)
{
return false;
}
/**
@brief If doesn't want draw sender as default, return true.
*/
bool onDraw(CCTextFieldTTF * sender)
{
return false;
}
};
/**
@brief A simple text input field with TTF font.
*/
class CC_DLL CCTextFieldTTF : public CCLabelTTF, public CCIMEDelegate
{
public:
CCTextFieldTTF();
@ -66,6 +111,10 @@ public:
// properties
//////////////////////////////////////////////////////////////////////////
CC_SYNTHESIZE(CCTextFieldDelegate *, m_pDelegate, Delegate);
CC_SYNTHESIZE_READONLY(int, m_nCharCount, CharCount);
CC_SYNTHESIZE(ccColor3B, m_ColorSpaceHolder, ColorSpaceHolder);
// input text property
public:
virtual void setString(const char *text);
@ -80,9 +129,10 @@ public:
virtual const char * getPlaceHolder(void);
protected:
std::string * m_pPlaceHolder;
bool m_bLock; // when insertText or deleteBackward called, m_bLock is true
protected:
virtual void draw();
//////////////////////////////////////////////////////////////////////////
// CCIMEDelegate interface
//////////////////////////////////////////////////////////////////////////

View File

@ -29,15 +29,34 @@ THE SOFTWARE.
NS_CC_BEGIN;
static int _calcCharCount(const char * pszText)
{
int n = 0;
char ch = 0;
while (ch = *pszText)
{
CC_BREAK_IF(! ch);
if (0x80 != (0xC0 & ch))
{
++n;
}
++pszText;
}
return n;
}
//////////////////////////////////////////////////////////////////////////
// constructor and destructor
//////////////////////////////////////////////////////////////////////////
CCTextFieldTTF::CCTextFieldTTF()
: m_pInputText(new std::string)
: m_pDelegate(0)
, m_nCharCount(0)
, m_pInputText(new std::string)
, m_pPlaceHolder(new std::string) // prevent CCLabelTTF initWithString assertion
, m_bLock(false)
{
m_ColorSpaceHolder.r = m_ColorSpaceHolder.g = m_ColorSpaceHolder.b = 127;
}
CCTextFieldTTF::~CCTextFieldTTF()
@ -141,12 +160,12 @@ bool CCTextFieldTTF::detachWithIME()
bool CCTextFieldTTF::canAttachWithIME()
{
return true;
return (m_pDelegate) ? (! m_pDelegate->onTextFieldAttachWithIME(this)) : true;
}
bool CCTextFieldTTF::canDetachWithIME()
{
return true;
return (m_pDelegate) ? (! m_pDelegate->onTextFieldAttachWithIME(this)) : true;
}
void CCTextFieldTTF::insertText(const char * text, int len)
@ -171,11 +190,16 @@ void CCTextFieldTTF::insertText(const char * text, int len)
return;
}
m_bLock = true;
if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len))
{
// delegate doesn't want insert text
return;
}
m_nCharCount += _calcCharCount(sInsert.c_str());
std::string sText(*m_pInputText);
sText.append(sInsert);
setString(sText.c_str());
m_bLock = false;
}
void CCTextFieldTTF::deleteBackward()
@ -187,8 +211,6 @@ void CCTextFieldTTF::deleteBackward()
return;
}
m_bLock = true;
// get the delete byte number
int nDeleteLen = 1; // default, erase 1 byte
@ -196,6 +218,13 @@ void CCTextFieldTTF::deleteBackward()
{
++nDeleteLen;
}
if (m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, m_pInputText->c_str() + nStrLen - nDeleteLen, nDeleteLen))
{
// delegate don't wan't delete backward
return;
}
// if delete all text, show space holder string
if (nStrLen <= nDeleteLen)
{
@ -208,7 +237,25 @@ void CCTextFieldTTF::deleteBackward()
// set new input text
std::string sText(m_pInputText->c_str(), nStrLen - nDeleteLen);
setString(sText.c_str());
m_bLock = false;
}
void CCTextFieldTTF::draw()
{
if (m_pDelegate && m_pDelegate->onDraw(this))
{
return;
}
if (m_pInputText->length())
{
CCLabelTTF::draw();
return;
}
// draw placeholder
ccColor3B color = getColor();
setColor(m_ColorSpaceHolder);
CCLabelTTF::draw();
setColor(color);
}
//////////////////////////////////////////////////////////////////////////
@ -233,6 +280,7 @@ void CCTextFieldTTF::setString(const char *text)
{
CCLabelTTF::setString(m_pInputText->c_str());
}
m_nCharCount = _calcCharCount(m_pInputText->c_str());
}
const char* CCTextFieldTTF::getString(void)