TextField uses std::string& in the public API

and uses std::string instead of std::string* internally
This commit is contained in:
Ricardo Quesada 2013-11-07 11:58:48 -08:00
parent 237f7a7839
commit e4662b371a
3 changed files with 68 additions and 82 deletions

View File

@ -53,8 +53,8 @@ static int _calcCharCount(const char * pszText)
TextFieldTTF::TextFieldTTF() TextFieldTTF::TextFieldTTF()
: _delegate(0) : _delegate(0)
, _charCount(0) , _charCount(0)
, _inputText(new std::string) , _inputText("")
, _placeHolder(new std::string) // prevent LabelTTF initWithString assertion , _placeHolder("") // prevent LabelTTF initWithString assertion
, _secureTextEntry(false) , _secureTextEntry(false)
{ {
_colorSpaceHolder.r = _colorSpaceHolder.g = _colorSpaceHolder.b = 127; _colorSpaceHolder.r = _colorSpaceHolder.g = _colorSpaceHolder.b = 127;
@ -62,43 +62,41 @@ TextFieldTTF::TextFieldTTF()
TextFieldTTF::~TextFieldTTF() TextFieldTTF::~TextFieldTTF()
{ {
CC_SAFE_DELETE(_inputText);
CC_SAFE_DELETE(_placeHolder);
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// static constructor // static constructor
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
TextFieldTTF * TextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, const Size& dimensions, TextHAlignment alignment, const char *fontName, float fontSize) TextFieldTTF * TextFieldTTF::textFieldWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize)
{ {
TextFieldTTF *pRet = new TextFieldTTF(); TextFieldTTF *ret = new TextFieldTTF();
if(pRet && pRet->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize)) if(ret && ret->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
{ {
pRet->autorelease(); ret->autorelease();
if (placeholder) if (placeholder.size()>0)
{ {
pRet->setPlaceHolder(placeholder); ret->setPlaceHolder(placeholder);
} }
return pRet; return ret;
} }
CC_SAFE_DELETE(pRet); CC_SAFE_DELETE(ret);
return NULL; return NULL;
} }
TextFieldTTF * TextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize) TextFieldTTF * TextFieldTTF::textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize)
{ {
TextFieldTTF *pRet = new TextFieldTTF(); TextFieldTTF *ret = new TextFieldTTF();
if(pRet && pRet->initWithString("", fontName, fontSize)) if(ret && ret->initWithString("", fontName, fontSize))
{ {
pRet->autorelease(); ret->autorelease();
if (placeholder) if (placeholder.size()>0)
{ {
pRet->setPlaceHolder(placeholder); ret->setPlaceHolder(placeholder);
} }
return pRet; return ret;
} }
CC_SAFE_DELETE(pRet); CC_SAFE_DELETE(ret);
return NULL; return NULL;
} }
@ -106,23 +104,15 @@ TextFieldTTF * TextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, c
// initialize // initialize
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
bool TextFieldTTF::initWithPlaceHolder(const char *placeholder, const Size& dimensions, TextHAlignment alignment, const char *fontName, float fontSize) bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize)
{ {
if (placeholder) _placeHolder = placeholder;
{ return LabelTTF::initWithString(_placeHolder, fontName, fontSize, dimensions, alignment);
CC_SAFE_DELETE(_placeHolder);
_placeHolder = new std::string(placeholder);
}
return LabelTTF::initWithString(_placeHolder->c_str(), fontName, fontSize, dimensions, alignment);
} }
bool TextFieldTTF::initWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize) bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize)
{ {
if (placeholder) _placeHolder = std::string(placeholder);
{ return LabelTTF::initWithString(_placeHolder, fontName, fontSize);
CC_SAFE_DELETE(_placeHolder);
_placeHolder = new std::string(placeholder);
}
return LabelTTF::initWithString(_placeHolder->c_str(), fontName, fontSize);
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -190,9 +180,9 @@ void TextFieldTTF::insertText(const char * text, int len)
} }
_charCount += _calcCharCount(sInsert.c_str()); _charCount += _calcCharCount(sInsert.c_str());
std::string sText(*_inputText); std::string sText(_inputText);
sText.append(sInsert); sText.append(sInsert);
setString(sText.c_str()); setString(sText);
} }
if ((int)sInsert.npos == nPos) { if ((int)sInsert.npos == nPos) {
@ -211,7 +201,7 @@ void TextFieldTTF::insertText(const char * text, int len)
void TextFieldTTF::deleteBackward() void TextFieldTTF::deleteBackward()
{ {
int nStrLen = _inputText->length(); int nStrLen = _inputText.length();
if (! nStrLen) if (! nStrLen)
{ {
// there is no string // there is no string
@ -221,12 +211,12 @@ void TextFieldTTF::deleteBackward()
// get the delete byte number // get the delete byte number
int nDeleteLen = 1; // default, erase 1 byte int nDeleteLen = 1; // default, erase 1 byte
while(0x80 == (0xC0 & _inputText->at(nStrLen - nDeleteLen))) while(0x80 == (0xC0 & _inputText.at(nStrLen - nDeleteLen)))
{ {
++nDeleteLen; ++nDeleteLen;
} }
if (_delegate && _delegate->onTextFieldDeleteBackward(this, _inputText->c_str() + nStrLen - nDeleteLen, nDeleteLen)) if (_delegate && _delegate->onTextFieldDeleteBackward(this, _inputText.c_str() + nStrLen - nDeleteLen, nDeleteLen))
{ {
// delegate doesn't wan't to delete backwards // delegate doesn't wan't to delete backwards
return; return;
@ -235,21 +225,20 @@ void TextFieldTTF::deleteBackward()
// if all text deleted, show placeholder string // if all text deleted, show placeholder string
if (nStrLen <= nDeleteLen) if (nStrLen <= nDeleteLen)
{ {
CC_SAFE_DELETE(_inputText); _inputText = "";
_inputText = new std::string;
_charCount = 0; _charCount = 0;
LabelTTF::setString(_placeHolder->c_str()); LabelTTF::setString(_placeHolder);
return; return;
} }
// set new input text // set new input text
std::string sText(_inputText->c_str(), nStrLen - nDeleteLen); std::string sText(_inputText.c_str(), nStrLen - nDeleteLen);
setString(sText.c_str()); setString(sText);
} }
const char * TextFieldTTF::getContentText() const char * TextFieldTTF::getContentText()
{ {
return _inputText->c_str(); return _inputText.c_str();
} }
void TextFieldTTF::draw() void TextFieldTTF::draw()
@ -258,7 +247,7 @@ void TextFieldTTF::draw()
{ {
return; return;
} }
if (_inputText->length()) if (_inputText.length())
{ {
LabelTTF::draw(); LabelTTF::draw();
return; return;
@ -286,22 +275,20 @@ void TextFieldTTF::setColorSpaceHolder(const Color3B& color)
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// input text property // input text property
void TextFieldTTF::setString(const char *text) void TextFieldTTF::setString(const std::string &text)
{ {
static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00}; static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00};
std::string displayText; std::string displayText;
int length; int length;
CC_SAFE_DELETE(_inputText); if (text.length()>0)
if (text)
{ {
_inputText = new std::string(text); _inputText = text;
displayText = *_inputText; displayText = _inputText;
if (_secureTextEntry) if (_secureTextEntry)
{ {
displayText = ""; displayText = "";
length = _inputText->length(); length = _inputText.length();
while (length) while (length)
{ {
displayText.append(bulletString); displayText.append(bulletString);
@ -311,40 +298,39 @@ void TextFieldTTF::setString(const char *text)
} }
else else
{ {
_inputText = new std::string; _inputText = "";
} }
// if there is no input text, display placeholder instead // if there is no input text, display placeholder instead
if (! _inputText->length()) if (! _inputText.length())
{ {
LabelTTF::setString(_placeHolder->c_str()); LabelTTF::setString(_placeHolder);
} }
else else
{ {
LabelTTF::setString(displayText.c_str()); LabelTTF::setString(displayText);
} }
_charCount = _calcCharCount(_inputText->c_str()); _charCount = _calcCharCount(_inputText.c_str());
} }
const char* TextFieldTTF::getString(void) const const char* TextFieldTTF::getString(void) const
{ {
return _inputText->c_str(); return _inputText.c_str();
} }
// place holder text property // place holder text property
void TextFieldTTF::setPlaceHolder(const char * text) void TextFieldTTF::setPlaceHolder(const std::string& text)
{ {
CC_SAFE_DELETE(_placeHolder); _placeHolder = text;
_placeHolder = (text) ? new std::string(text) : new std::string; if (! _inputText.length())
if (! _inputText->length())
{ {
LabelTTF::setString(_placeHolder->c_str()); LabelTTF::setString(_placeHolder);
} }
} }
const char * TextFieldTTF::getPlaceHolder(void) const std::string& TextFieldTTF::getPlaceHolder() const
{ {
return _placeHolder->c_str(); return _placeHolder;
} }
// secureTextEntry // secureTextEntry

View File

@ -109,13 +109,13 @@ public:
//char * description(); //char * description();
/** creates a TextFieldTTF from a fontname, alignment, dimension and font size */ /** creates a TextFieldTTF from a fontname, alignment, dimension and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const char *placeholder, const Size& dimensions, TextHAlignment alignment, const char *fontName, float fontSize); static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
/** creates a LabelTTF from a fontname and font size */ /** creates a LabelTTF from a fontname and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize); static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
/** initializes the TextFieldTTF with a font name, alignment, dimension and font size */ /** initializes the TextFieldTTF with a font name, alignment, dimension and font size */
bool initWithPlaceHolder(const char *placeholder, const Size& dimensions, TextHAlignment alignment, const char *fontName, float fontSize); bool initWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
/** initializes the TextFieldTTF with a font name and font size */ /** initializes the TextFieldTTF with a font name and font size */
bool initWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize); bool initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
/** /**
@brief Open keyboard and receive input text. @brief Open keyboard and receive input text.
@ -147,21 +147,21 @@ public:
// input text property // input text property
public: public:
virtual void setString(const char *text); virtual void setString(const std::string& text) override;
virtual const char* getString(void) const; virtual const char* getString(void) const;
protected: protected:
TextFieldDelegate * _delegate; TextFieldDelegate * _delegate;
int _charCount; int _charCount;
std::string * _inputText; std::string _inputText;
// place holder text property // place holder text property
// place holder text displayed when there is no text in the text field. // place holder text displayed when there is no text in the text field.
public: public:
virtual void setPlaceHolder(const char * text); virtual void setPlaceHolder(const std::string& text);
virtual const char * getPlaceHolder(void); virtual const std::string& getPlaceHolder(void) const;
protected: protected:
std::string * _placeHolder; std::string _placeHolder;
Color3B _colorSpaceHolder; Color3B _colorSpaceHolder;
public: public:
virtual void setSecureTextEntry(bool value); virtual void setSecureTextEntry(bool value);
@ -176,11 +176,11 @@ protected:
// IMEDelegate interface // IMEDelegate interface
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
virtual bool canAttachWithIME(); virtual bool canAttachWithIME() override;
virtual bool canDetachWithIME(); virtual bool canDetachWithIME() override;
virtual void insertText(const char * text, int len); virtual void insertText(const char * text, int len) override;
virtual void deleteBackward(); virtual void deleteBackward() override;
virtual const char * getContentText(); virtual const char * getContentText() override;
private: private:
class LengthStack; class LengthStack;
LengthStack * _lens; LengthStack * _lens;

View File

@ -139,7 +139,7 @@ void UICCTextField::insertText(const char * text, int len)
{ {
if (cocos2d::TextFieldTTF::getCharCount() > 0) if (cocos2d::TextFieldTTF::getCharCount() > 0)
{ {
setPasswordText(_inputText->c_str()); setPasswordText(_inputText.c_str());
} }
} }
} }
@ -153,7 +153,7 @@ void UICCTextField::deleteBackward()
// password // password
if (_passwordEnabled) if (_passwordEnabled)
{ {
setPasswordText(_inputText->c_str()); setPasswordText(_inputText.c_str());
} }
} }
} }