Beispiel #1
0
void TextField::setText(const std::string& text)
{
    std::string strText(text);
    
    if (isMaxLengthEnabled())
    {
        int max = _textFieldRenderer->getMaxLength();
        int text_count = _calcCharCount(text.c_str());
        int total = text_count + _calcCharCount(getStringValue().c_str());
        if (total > max)
        {
            int ascii = 0;
            int unicode = 0;
            int end = 0;
            int count = 0;
            
            for (int i = 0; i < total * 3; ++i)
            {
                char value = text[i];
                
                if (value >= 0 && value <= 127) // ascii
                {
                    ascii++;
                    count++;
                }
                else
                {
                    unicode++;
                    if (unicode % 3 == 0)
                    {
                        count++;
                    }
                }
                
                if (count == max)
                {
                    break;
                }
            }
            end = ascii + unicode;
            strText = strText.substr(0, end);
        }
    }
    
    const char* content = strText.c_str();
    if (isPasswordEnabled())
    {
        _textFieldRenderer->setPasswordText(content);
        _textFieldRenderer->setString("");
        _textFieldRenderer->insertText(content, strlen(content));
    }
    else
    {
        _textFieldRenderer->setString(content);
    }
    _textFieldRendererAdaptDirty = true;
    updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
}
Beispiel #2
0
void CCTextFieldTTF::insertText(const char * text, int len)
{
    std::string sInsert(text, len);

    // insert \n means input end
    int nPos = sInsert.find('\n');
    if ((int)sInsert.npos != nPos)
    {
        len = nPos;
        sInsert.erase(nPos);
    }
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    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.swap(sInsert);
    setString(sText.c_str());
#else
    if (len > 0)
    {
        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());
    }
#endif
    if ((int)sInsert.npos == nPos) {
        return;
    }
    
    // '\n' has inserted,  let delegate process first
    if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, "\n", 1))
    {
        return;
    }
    
    // if delegate hasn't process, detach with ime as default
    detachWithIME();
}
void RichText::handleTextRenderer(const char *text, const char *fontName, float fontSize, const Color3B &color, GLubyte opacity)
{
    Label* textRenderer = Label::create(text, fontName, fontSize);
    float textRendererWidth = textRenderer->getContentSize().width;
    _leftSpaceWidth -= textRendererWidth;
    if (_leftSpaceWidth < 0.0f)
    {
        float overstepPercent = (-_leftSpaceWidth) / textRendererWidth;
        std::string curText = text;
        size_t stringLength = _calcCharCount(text);
        int leftLength = stringLength * (1.0f - overstepPercent);
        std::string leftWords = curText.substr(0, leftLength);
        std::string cutWords = curText.substr(leftLength, curText.length()-1);
        if (leftLength > 0)
        {
            Label* leftRenderer = Label::create(leftWords.substr(0, leftLength).c_str(), fontName, fontSize);
            leftRenderer->setColor(color);
            leftRenderer->setOpacity(opacity);
            pushToContainer(leftRenderer);
        }

        addNewLine();
        handleTextRenderer(cutWords.c_str(), fontName, fontSize, color, opacity);
    }
    else
    {
        textRenderer->setColor(color);
        textRenderer->setOpacity(opacity);
        pushToContainer(textRenderer);
    }
}
void CCTextInput::insertText(const char * text, int len)
{
    std::string sInsert(text, len);

    // insert \n means input end
    int nPos = sInsert.find('\n');
    if ((int)sInsert.npos != nPos)
    {
        len = nPos;
        sInsert.erase(nPos);
    }
    
    if (len > 0)
    {
		unsigned int inputCharCount = _calcCharCount(sInsert.c_str()) + m_nCharCount;
		if(inputCharCount>m_limitNum){
			return;
		}
        m_nCharCount = inputCharCount;
        std::string sText(*m_pInputText);
        sText.append(sInsert);
        setString(sText.c_str());
    }

    if ((int)sInsert.npos == nPos) {
        return;
    }
    
    // if delegate hasn't processed, detach from IME by default
    closeIME();
}
// input text property
void CCTextInput::setString(const char *text)
{
    CC_SAFE_DELETE(m_pInputText);

    if (text)
    {
        m_pInputText = new std::string(text);
    }
    else
    {
        m_pInputText = new std::string;
    }

    // if there is no input text, display placeholder instead
    if (! m_pInputText->length())
    {
        m_pLabel->setString(m_pPlaceHolder->c_str());
		m_pColor->setColor(m_ColorSpaceHolder);
    }
    else
    {
        m_pLabel->setString(m_pInputText->c_str());
		m_pColor->setColor(m_ColorText);
    }
    m_nCharCount = _calcCharCount(m_pInputText->c_str());
	this->resetView();
}
Beispiel #6
0
void MyTextField::insertText(const char * text, size_t len){
    
    std::string insert(text, len);
    
    if(m_orientation == TextFieldOrientation::VERTICAL){
        //在非\n字符的后面加\n
        if(memcmp("\n", text, len) != 0){
            insert.append("\n");
        }
    }
    
    if (len > 0)
    {
        if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), insert.length()))
        {
            // delegate doesn't want to insert text
            return;
        }
        
        _charCount += _calcCharCount(insert.c_str());
        std::string sText(_inputText);
        sText.append(insert);
        setString(sText);
    }
}
void TextFieldTTF::insertText(const char * text, size_t len)
{
    std::string insert(text, len);

    // insert \n means input end
    int pos = static_cast<int>(insert.find((char)TextFormatter::NewLine));
    if ((int)insert.npos != pos)
    {
        len = pos;
        insert.erase(pos);
    }

    if (len > 0)
    {
        if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), len))
        {
            // delegate doesn't want to insert text
            return;
        }

        int countInsertChar = _calcCharCount(insert.c_str());
        _charCount += countInsertChar;

        if (_cursorEnabled)
        {
            StringUtils::StringUTF8 stringUTF8;

            stringUTF8.replace(_inputText);
            stringUTF8.insert(_cursorPosition, insert);

            setCursorPosition(_cursorPosition + countInsertChar);

            setString(stringUTF8.getAsCharSequence());
        }
        else
        {
            std::string sText(_inputText);
            sText.append(insert);
            setString(sText);
        }
    }

    if ((int)insert.npos == pos) {
        return;
    }

    // '\n' inserted, let delegate process first
    if (_delegate && _delegate->onTextFieldInsertText(this, "\n", 1))
    {
        return;
    }

    // if delegate hasn't processed, detach from IME by default
    detachWithIME();
}
Beispiel #8
0
// input text property
void CCTextFieldTTF::setString(const char *text)
{
    static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00};
    std::string displayText;
    int length;

    CC_SAFE_DELETE(m_pInputText);

    if (text)
    {
        m_pInputText = new std::string(text);
        displayText = *m_pInputText;
        if (m_bSecureTextEntry)
        {
            displayText = "";
            length = m_pInputText->length();
            while (length)
            {
                displayText.append(bulletString);
                --length;
            }
        }
    }
    else
    {
        m_pInputText = new std::string;
    }

    // if there is no input text, display placeholder instead
    if (! m_pInputText->length())
    {
        CCLabelTTF::setString(m_pPlaceHolder->c_str());
    }
    else
    {
     //   CCLabelTTF::setString(displayText.c_str());
        
        std::string  tstring;
        tstring = std::string(m_pInputText->c_str()) + std::string("|");
        CCLabelTTF::setString(tstring.c_str());
        
       /////////
//        CCAction *actionFunc1 =  CCSequence::create(CCCallFuncN::create(this, callfuncN_selector(CCTextFieldTTF::blick)),CCDelayTime::create(1.0), NULL);
//        CCAction*action=(CCFiniteTimeAction *)actionFunc1;
//        
//        this->runAction(action);
      
//        blick();
          this->schedule(schedule_selector(CCTextFieldTTF::blick), 0.5);
        
    }
    m_nCharCount = _calcCharCount(m_pInputText->c_str());
}
// input text property
void TextFieldTTF::setString(const std::string &text)
{
    std::string displayText;

    int charCount = 0;
    if (!text.empty())
    {
        _inputText = text;
        displayText = _inputText;
        charCount = _calcCharCount(_inputText.c_str());
        if (_secureTextEntry)
        {
            displayText = "";
            size_t length = charCount;
            while (length)
            {
                displayText.append(_passwordStyleText);
                --length;
            }
        }
    }
    else
    {
        _inputText = "";
    }

    if (_cursorEnabled && charCount != _charCount)
    {
        _cursorPosition = charCount;
    }

    if (_cursorEnabled)
    {
        // Need for recreate all letters in Label
        Label::removeAllChildrenWithCleanup(false);
    }

    // if there is no input text, display placeholder instead
    if (_inputText.empty() && (!_cursorEnabled || !_isAttachWithIME))
    {
        Label::setTextColor(_colorSpaceHolder);
        Label::setString(_placeHolder);
    }
    else
    {
        makeStringSupportCursor(displayText);

        Label::setTextColor(_colorText);
        Label::setString(displayText);
    }
    _charCount = charCount;
}
Beispiel #10
0
void RichText::handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B &color, GLubyte opacity)
{
    auto fileExist = FileUtils::getInstance()->isFileExist(fontName);
    Label* textRenderer = nullptr;
    if (fileExist)
    {
        textRenderer = Label::createWithTTF(text, fontName, fontSize);
    } 
    else
    {
        textRenderer = Label::createWithSystemFont(text, fontName, fontSize);
    }
    float textRendererWidth = textRenderer->getContentSize().width;
    _leftSpaceWidth -= textRendererWidth;
    if (_leftSpaceWidth < 0.0f)
    {
        float overstepPercent = (-_leftSpaceWidth) / textRendererWidth;
        std::string curText = text;
        size_t stringLength = _calcCharCount(text.c_str());
        int leftLength = stringLength * (1.0f - overstepPercent);
        std::string leftWords = curText.substr(0, leftLength);
        std::string cutWords = curText.substr(leftLength, curText.length()-1);
        if (leftLength > 0)
        {
            Label* leftRenderer = nullptr;
            if (fileExist)
            {
                leftRenderer = Label::createWithTTF(leftWords.substr(0, leftLength).c_str(), fontName, fontSize);
            } 
            else
            {
                leftRenderer = Label::createWithSystemFont(leftWords.substr(0, leftLength).c_str(), fontName, fontSize);
            }
            if (leftRenderer)
            {
                leftRenderer->setColor(color);
                leftRenderer->setOpacity(opacity);
                pushToContainer(leftRenderer);
            }
        }

        addNewLine();
        handleTextRenderer(cutWords.c_str(), fontName, fontSize, color, opacity);
    }
    else
    {
        textRenderer->setColor(color);
        textRenderer->setOpacity(opacity);
        pushToContainer(textRenderer);
    }
}
Beispiel #11
0
void TextFieldTTF::insertText(const char * text, size_t len)
{
    std::string insert(text, len);

    // insert \n means input end
    int pos = static_cast<int>(insert.find('\n'));
    if ((int)insert.npos != pos)
    {
        len = pos;
        insert.erase(pos);
    }

    if (len > 0)
    {
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8 && CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
        if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), len))
        {
            // delegate doesn't want to insert text
            return;
        }

        _charCount += _calcCharCount(insert.c_str());
        std::string sText(_inputText);
        sText.append(insert);
        setString(sText);
#else
        size_t existlen = _inputText.length();
        if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str() + existlen, len - existlen))
        {
            // delegate doesn't want to insert text
            return;
        }
        setString(insert);
#endif
    }

    if ((int)insert.npos == pos) {
        return;
    }

    // '\n' inserted, let delegate process first
    if (_delegate && _delegate->onTextFieldInsertText(this, "\n", 1))
    {
        return;
    }

    // if delegate hasn't processed, detach from IME by default
    detachWithIME();
}
void CCTextFieldTTF::insertText(const char * text, int len)
{
    std::string sInsert(text, len);

    // insert \n means input end
    int nPos = sInsert.find('\n');
    //找到了 换行符号 输入就是 -1  len = -1
    //支持换行符
    if (!isMultiLine) {
        if ((int)sInsert.npos != nPos)
        {
            len = nPos;
            sInsert.erase(nPos);
        }
    }
    
    //换行符就不输入文字了
    if (len > 0)
    {
        //代理处理插入了字符 只在有最长限制的时候
        if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len))
        {
            // delegate doesn't want to insert text
            return;
        }

        m_nCharCount += _calcCharCount(sInsert.c_str());
        std::string sText(*m_pInputText);
        sText.append(sInsert);
        setString(sText.c_str());
    }

    //不是换行符 处理完毕 或者支持多行文本
    if (isMultiLine || (int)sInsert.npos == nPos) {
        return;
    }

    //代理处理换行符
    // '\n' inserted, let delegate process first
    if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, "\n", 1))
    {
        return;
    }

    //当点击背后的屏幕的时候 没有在键盘范围则退出场景即可
    // if delegate hasn't processed, detach from IME by default
    detachWithIME();
}
Beispiel #13
0
void CCTextFieldTTF::insertText(const char * text, int len)
{
    std::string sInsert(text, len);

    // insert \n means input end
    int nPos = sInsert.find('\n');
    if ((int)sInsert.npos != nPos)
    {
        len = nPos;
        sInsert.erase(nPos);
    }

    if (len > 0)
    {
        if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len))
        {
            // delegate doesn't want to insert text
            return;
        }

        m_nCharCount += _calcCharCount(sInsert.c_str());
        std::string sText(*m_pInputText);
        sText.append(sInsert);
        setString(sText.c_str());
        //////
        
    
        ///////
   
     
    }

    if ((int)sInsert.npos == nPos) {
        return;
    }

    // '\n' inserted, let delegate process first
    if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, "\n", 1))
    {
        return;
    }

    // if delegate hasn't processed, detach from IME by default
    detachWithIME();
}
Beispiel #14
0
void UICCTextField::setPasswordText(const char *text)
{
    std::string tempStr = "";
    int text_count = _calcCharCount(text);
    int max = text_count;
    
    if (_maxLengthEnabled)
    {
        if (text_count > _maxLength)
        {
            max = _maxLength;
        }
    }
    
    for (int i = 0; i < max; ++i)
    {
        tempStr.append(_passwordStyleText);
    }
    
    Label::setString(tempStr);
}
// input text property
void CCTextFieldTTF::setString(const char *text)
{
    static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00};
    std::string displayText;
    int length;

    CC_SAFE_DELETE(m_pInputText);

    if (text)
    {
        m_pInputText = new std::string(text);
        displayText = *m_pInputText;
        if (m_bSecureTextEntry)
        {
            displayText = "";
            length = m_pInputText->length();
            while (length)
            {
                displayText.append(bulletString);
                --length;
            }
        }
    }
    else
    {
        m_pInputText = new std::string;
    }

    // if there is no input text, display placeholder instead
    if (! m_pInputText->length())
    {
        CCLabelTTF::setString(m_pPlaceHolder->c_str());
    }
    else
    {
        CCLabelTTF::setString(displayText.c_str());
    }
    m_nCharCount = _calcCharCount(m_pInputText->c_str());
}
Beispiel #16
0
// input text property
void TextFieldTTF::setString(const std::string &text)
{
    static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00};
    std::string displayText;
    size_t length;

    if (text.length()>0)
    {
        _inputText = text;
        displayText = _inputText;
        if (_secureTextEntry)
        {
            displayText = "";
            length = _inputText.length();
            while (length)
            {
                displayText.append(bulletString);
                --length;
            }
        }
    }
    else
    {
        _inputText = "";
    }

    // if there is no input text, display placeholder instead
    if (0 == _inputText.length())
    {
        Label::setTextColor(_colorSpaceHolder);
        Label::setString(_placeHolder);
    }
    else
    {
        Label::setTextColor(_colorText);
        Label::setString(displayText);
    }
    _charCount = _calcCharCount(_inputText.c_str());
}
// input text property
void CCTextFieldTTF::setString(const char *text)
{
    CC_SAFE_DELETE(m_pInputText);

    if (text)
    {
        m_pInputText = new std::string(text);
    }
    else
    {
        m_pInputText = new std::string;
    }

    // if there is no input text, display placeholder instead
    if (! m_pInputText->length())
    {
        CCLabelTTF::setString(m_pPlaceHolder->c_str());
    }
    else
    {
        CCLabelTTF::setString(m_pInputText->c_str());
    }
    m_nCharCount = _calcCharCount(m_pInputText->c_str());
}
Beispiel #18
0
void UICCTextField::insertText(const char * text, int len)
{
    std::string input_text = text;
    
    if (strcmp(text, "\n") != 0)
    {
        if (_maxLengthEnabled)
        {
            int text_count = _calcCharCount(getString().c_str());
            if (text_count >= _maxLength)
            {
                // password
                if (_passwordEnabled)
                {
                    setPasswordText(getString().c_str());
                }
                return;
            }
            
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32))
            int input_count = _calcCharCount(text);
            int total = total = text_count + input_count;
            
            if (total > _maxLength)
            {
                int end = 0;
                int length = _maxLength - text_count;
                
                for (int i = 0; i < length; ++i)
                {
                    char value = text[i];
                    
                    if (value >= 0 && value <= 127) // ascii
                    {
                        end++;
                    }
                    else
                    {
                        end += 3;
                    }
                }
                input_text = input_text.substr(0, end);
                len  = end;
            }
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
            int input_count = _calcCharCount(text);
            if (input_count > _maxLength)
            {
                int ascii = 0;
                int unicode = 0;
                int end = 0;
                int count = 0;
                
                for (int i = 0; i < input_count * 3; ++i)
                {
                    char value = text[i];
                    
                    if (value >= 0 && value <= 127) // ascii
                    {
                        ascii++;
                        count++;
                    }
                    else
                    {
                        unicode++;
                        if (unicode % 3 == 0)
                        {
                            count++;
                        }
                    }
                    
                    if (count == _maxLength)
                    {
                        break;
                    }
                }
                end = ascii + unicode;
                input_text = input_text.substr(0, end);
                len  = end;
            }
#endif
        }
    }
    TextFieldTTF::insertText(input_text.c_str(), len);
    
    // password
    if (_passwordEnabled)
    {
        if (TextFieldTTF::getCharCount() > 0)
        {
            setPasswordText(getString().c_str());
        }
    }
}
Beispiel #19
0
void CATextField::insertText(const char * text, int len)
{
    if (spaceHolderIsOn)
    {
        m_pText->setText("");
        m_pText->setTextcolor(m_cTextColor);
        spaceHolderIsOn=false;
        
    }
    std::string sInsert(text, len);
    
    // insert \n means input end
    int nPos = sInsert.find('\n');
    if ((int)sInsert.npos != nPos)
    {
        len = nPos;
        sInsert.erase(nPos);
    }
    
    if (len > 0)
    {
        if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len))
        {
            
            
            // delegate doesn't want to insert text
            return;
        }
        m_nCharCount += _calcCharCount(sInsert.c_str());
        std::string sText(m_pText->getText());
        std::string oldstr = sText;
        sText.append(sInsert);
        
        float length = 0;
        
        m_pText->setText(sText.c_str());
        
        CCRect rect = CCRectZero;
        rect.size = this->getBounds().size;
        rect.size.width = MIN(this->getBounds().size.width, m_pText->getLabelSize().width);
        
        if (m_pText->getLabelSize().width < this->getBounds().size.width)
        {
            m_pText->setFrame(rect);
            m_pMark->setCenterOrigin(CCPoint(m_pText->getLabelSize().width, this->getBounds().size.height/2));
        }
        else
        {
            m_pText->setText(oldstr);
            m_pText->setFrame(rect);
            m_pMark->setCenterOrigin(CCPoint(m_pText->getLabelSize().width, this->getBounds().size.height/2));
        }
        
        if (_oldPos != 0)
        {
            length = m_pText->getLabelSize().width - _oldPos;
        }
        else
        {
            length = m_pText->getLabelSize().width;
        }
        
    }
    
    if ((int)sInsert.npos == nPos) {
        return;
    }
    
    if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, "\n", 1))
    {
        return;
    }
    
    // if delegate hasn't processed, detach from IME by default
    detachWithIME();
}
Beispiel #20
0
void CCtrlTextFieldTTF::insertText(const char * text, int len)
{
	std::string sInsert(text, len);

	// insert \n means input end
	int nPos = sInsert.find('\n');
	if ((int)sInsert.npos != nPos)
	{
		len = nPos;
		sInsert.erase(nPos);
	}

	if (len > 0)
	{
		if (EditInputFlagAll == m_nInputType)
		{
			if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len))
			{
				// delegate doesn't want to insert text
				return;
			}

			m_strText.append(sInsert);
			m_nCharCount += _calcCharCount(sInsert.c_str());
			std::string sText(*m_pInputText);
			sText.append(sInsert);
			setString(sText.c_str());
		}
		else if (EditInputFlagPassword == m_nInputType)
		{
			int nInsertSize = _calcCharCount(sInsert.c_str());
			std::string strInsert = "";
			for (int i=0; i<nInsertSize; ++i)
			{
				strInsert.append("*");
			}

			if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, strInsert.c_str(), nInsertSize))
			{
				// delegate doesn't want to insert text
				return;
			}

			m_strText.append(sInsert);
			m_nCharCount += nInsertSize;
			std::string sText(*m_pInputText);
			sText.append(strInsert);			

			setString(sText.c_str());
		}

		if (EventChanged)
		{
			EventChanged();
		}
	}

	if ((int)sInsert.npos == nPos) {
		return;
	}

	// '\n' inserted, let delegate process first
	if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, "\n", 1))
	{
		return;
	}

	// if delegate hasn't processed, detach from IME by default
	detachWithIME();
}