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

View File

@ -109,13 +109,13 @@ public:
//char * description();
/** 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 */
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 */
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 */
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.
@ -147,21 +147,21 @@ public:
// input text property
public:
virtual void setString(const char *text);
virtual void setString(const std::string& text) override;
virtual const char* getString(void) const;
protected:
TextFieldDelegate * _delegate;
int _charCount;
std::string * _inputText;
std::string _inputText;
// place holder text property
// place holder text displayed when there is no text in the text field.
public:
virtual void setPlaceHolder(const char * text);
virtual const char * getPlaceHolder(void);
virtual void setPlaceHolder(const std::string& text);
virtual const std::string& getPlaceHolder(void) const;
protected:
std::string * _placeHolder;
std::string _placeHolder;
Color3B _colorSpaceHolder;
public:
virtual void setSecureTextEntry(bool value);
@ -176,11 +176,11 @@ protected:
// IMEDelegate interface
//////////////////////////////////////////////////////////////////////////
virtual bool canAttachWithIME();
virtual bool canDetachWithIME();
virtual void insertText(const char * text, int len);
virtual void deleteBackward();
virtual const char * getContentText();
virtual bool canAttachWithIME() override;
virtual bool canDetachWithIME() override;
virtual void insertText(const char * text, int len) override;
virtual void deleteBackward() override;
virtual const char * getContentText() override;
private:
class LengthStack;
LengthStack * _lens;

View File

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