void CATextField::deleteBackward() { if (m_nInputType==KEY_BOARD_INPUT_PASSWORD) { m_sText.clear(); m_vTextFiledChars.clear(); this->updateImage(); m_iString_l_length = 0; m_iString_r_length = 0; m_iString_left_offX = 0; m_iCurPos = 0; m_curSelCharRange = std::make_pair(0, 0); m_pCursorMark->setCenterOrigin(CCPoint(m_iHoriMargins + getCursorX(), m_obContentSize.height / 2)); return; } if (m_iCurPos==0 || m_sText.empty()) return; int nDeleteLen = 1; while (0x80 == (0xC0 & m_sText.at(m_iCurPos - nDeleteLen))) { ++nDeleteLen; } m_sText.erase(m_iCurPos - nDeleteLen, nDeleteLen); m_iCurPos -= nDeleteLen; m_curSelCharRange = std::make_pair(m_iCurPos, m_iCurPos); CC_RETURN_IF(m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, m_sText.c_str(), (int)m_sText.length())); m_vTextFiledChars.erase(m_vTextFiledChars.begin() + getStringCharCount(m_sText.substr(0, m_iCurPos))); adjustCursorMoveBackward(); }
void analyzeString(const char * text, int len) { std::string strLeft = m_sText.substr(0, m_iCurPos); std::string strRight = m_sText.substr(m_iCurPos, m_sText.size()); std::string cszNewText(text, len); std::u32string cszU32Text; StringUtils::UTF8ToUTF32(cszNewText, cszU32Text); for (int i = 0; i < cszU32Text.size(); i++) { std::u32string c; c += cszU32Text[i]; TextAttribute t; int iStrLen1 = getStringLength(strLeft); std::string str; StringUtils::UTF32ToUTF8(c, str); strLeft += str; t.charSize = str.size(); int iStrLen2 = getStringLength(strLeft); t.charlength = iStrLen2 - iStrLen1; m_vTextFiledChars.insert(m_vTextFiledChars.begin() + (getStringCharCount(strLeft) - 1), t); m_iCurPos += t.charSize; } m_sText = strLeft + strRight; m_curSelCharRange = std::make_pair(m_iCurPos, m_iCurPos); }
void CATextField::analyzeString(const char * text, int len) { std::string strLeft = m_sText.substr(0, m_iCurPos); std::string strRight = m_sText.substr(m_iCurPos, m_sText.size()); for (int i = 0; i < len; i++) { TextAttribute t; int iStrLen1 = getStringLength(strLeft); if (text[i] >= 0 && text[i] <= 127) { t.charSize = 1; strLeft += text[i]; } else { t.charSize = 3; strLeft += text[i++]; strLeft += text[i++]; strLeft += text[i]; } int iStrLen2 = getStringLength(strLeft); t.charlength = iStrLen2 - iStrLen1; m_vTextFiledChars.insert(m_vTextFiledChars.begin() + (getStringCharCount(strLeft)-1), t); m_iCurPos += t.charSize; } m_sText = strLeft + strRight; m_curSelCharRange = std::make_pair(m_iCurPos, m_iCurPos); }
void CATextField::ccStartSelect() { if (m_nInputType == KEY_BOARD_INPUT_PASSWORD) return; if (m_sText.empty()) return; int index = getStringCharCount(m_sText.substr(0, m_iCurPos)); if (index == 0) { m_curSelCharRange.first = m_iCurPos; m_curSelCharRange.second = m_iCurPos + m_vTextFiledChars.front().charSize; } else { m_curSelCharRange.first = m_iCurPos - m_vTextFiledChars[index - 1].charSize; m_curSelCharRange.second = m_iCurPos; } CATextSelectView* pSelCharsView = CATextSelectView::create(); bool l, r; CCRect cc = getZZCRect(&l, &r); pSelCharsView->showTextSelView(convertRectToWorldSpace(cc), this, l, r); m_pCursorMark->setVisible(false); // CATextArrowView::hideTextArrowView(); }
void CATextField::deleteBackward() { CC_RETURN_IF(m_iCurPos == 0 || m_sText.empty()); if (m_nInputType==KEY_BOARD_INPUT_PASSWORD) { CC_RETURN_IF(m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, m_sText.c_str(), (int)m_sText.length(), m_iCurPos - (int)m_sText.length())); CC_RETURN_IF(m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, m_sText.c_str(), (int)m_sText.length())); m_sText.clear(); m_vTextFiledChars.clear(); this->updateImage(); m_iString_l_length = 0; m_iString_r_length = 0; m_iString_o_length = 0; m_iCurPos = 0; m_curSelCharRange = std::make_pair(0, 0); setCursorPosition(); return; } std::string cszDelStr; if (m_curSelCharRange.first != m_curSelCharRange.second) { cszDelStr = m_sText.substr(m_curSelCharRange.first, m_curSelCharRange.second - m_curSelCharRange.first); } else { int nDeleteLen = 1; while (0x80 == (0xC0 & m_sText.at(m_iCurPos - nDeleteLen))) { ++nDeleteLen; } cszDelStr = m_sText.substr(m_iCurPos - nDeleteLen, nDeleteLen); } CC_RETURN_IF(m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, cszDelStr.c_str(), (int)cszDelStr.length(), m_iCurPos - (int)cszDelStr.length())); CC_RETURN_IF(m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, cszDelStr.c_str(), (int)cszDelStr.length())); CC_RETURN_IF(execCurSelCharRange()); int nDeleteLen = (int)cszDelStr.size(); m_sText.erase(m_iCurPos - nDeleteLen, nDeleteLen); m_iCurPos -= nDeleteLen; m_curSelCharRange = std::make_pair(m_iCurPos, m_iCurPos); m_vTextFiledChars.erase(m_vTextFiledChars.begin() + getStringCharCount(m_sText.substr(0, m_iCurPos))); adjustCursorMove(false); CCLog("··· deleteBackward text:%s", m_sText.c_str()); }
void CATextView::ccTouchEnded(CATouch *pTouch, CAEvent *pEvent) { if (CATextToolBarView::isTextToolBarShow()) return; if (m_bMoved) { m_bMoved = false; return; } CCPoint point = this->convertTouchToNodeSpace(pTouch); if (this->getBounds().containsPoint(point)) { if (canAttachWithIME() && !m_pTextSelView->isTextViewShow()) { becomeFirstResponder(); int iCurLine = 0; int iCurPosX = 0; calculateSelChars(point, iCurLine, iCurPosX, m_iCurPos); m_pCursorMark->setCenterOrigin(CCPoint(iCurPosX, m_iLineHeight*1.25f*iCurLine + m_iLineHeight / 2)); CCPoint pt = m_pCursorMark->getCenterOrigin(); m_pTextArrView->showTextArrView(CCPoint(pt.x, pt.y + m_iLineHeight*1.2f + m_pContainerView->getContentOffset().y)); m_curSelCharRange = std::pair<int,int>(m_iCurPos, m_iCurPos); #if CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID CCEGLView * pGlView = CAApplication::getApplication()->getOpenGLView(); pGlView->setIMECursorPos(getStringCharCount(m_szText), getContentText()); #endif } } else { if (canDetachWithIME()) { resignFirstResponder(); } } CATouchView::ccTouchEnded(pTouch, pEvent); }
void deleteBackward() { CC_RETURN_IF(m_sText.empty() || m_iCurPos==0); if (m_pTextFieldX->isSecureTextEntry()) { clearText(); updateImage(); return; } if (m_curSelCharRange.first != m_curSelCharRange.second) { if (!textValueChanged(m_curSelCharRange.first, m_curSelCharRange.second - m_curSelCharRange.first, "")) return; CC_RETURN_IF(execCurSelCharRange()); } else { int nDeleteLen = 1; while (0x80 == (0xC0 & m_sText.at(m_iCurPos - nDeleteLen))) { ++nDeleteLen; } if (!textValueChanged(m_iCurPos, nDeleteLen, "")) return; m_sText.erase(m_iCurPos - nDeleteLen, nDeleteLen); m_iCurPos -= nDeleteLen; m_curSelCharRange = std::make_pair(m_iCurPos, m_iCurPos); m_vTextFiledChars.erase(m_vTextFiledChars.begin() + getStringCharCount(m_sText.substr(0, m_iCurPos))); adjustCursorMove(); } }
int CATextView::getCursorPos() { return getStringCharCount(m_szText.substr(0, m_iCurPos)); }
int CATextField::getCursorPos() { return getStringCharCount(m_sText.substr(0, m_iCurPos)); }
void CATextField::deleteBackward() { if (m_nInputType==KEY_BOARD_INPUT_PASSWORD) { m_sText=""; spaceHolderIsOn=true; this->updateImage(); m_pMark->setCenterOrigin(CCPoint(labelOrginX+getCursorX(), this->getBounds().size.height/2)); return; } if(spaceHolderIsOn) { return; } int nStrLen =m_sLeft_string.length(); int nDeleteLen = 1; // default, erase 1 byte if (nStrLen == 0) { return; } while(0x80 == (0xC0 & m_sLeft_string.at(m_sLeft_string.length() - nDeleteLen))) { ++nDeleteLen; } if (m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, m_sLeft_string.c_str() + nStrLen - nDeleteLen, nDeleteLen)) { // delegate doesn't wan't to delete backwards return; } // if all text deleted, show placeholder string if (m_sText.length() <= nDeleteLen) { m_sLeft_string=""; spaceHolderIsOn=true; m_vByteLengthArr.clear(); TextAttribute tt; tt.charsize =0; tt.charlength =0; m_vByteLengthArr.push_back(tt); m_pMark->setCenterOrigin(CCPoint(labelOrginX, this->getBounds().size.height/2)); this->updateImage(); return; } int length = MAX(0, nStrLen - nDeleteLen); std::string sText(m_sLeft_string.c_str(), length); char str[512]=""; sprintf(str, "%s%s",sText.c_str(),m_sRight_string.c_str()); m_sText = str; this->updateImage(); m_vByteLengthArr.erase(m_vByteLengthArr.begin()+getStringCharCount(m_sLeft_string)); m_sLeft_string = sText.c_str(); float left_width = getStringLength(m_sLeft_string); m_fString_left_length = m_rLabelRect.size.width; //left_width; m_fString_right_length = m_rLabelRect.size.width - m_fString_left_length; if (m_fString_right_length + getCursorX() < labelWidth) { m_fString_left_offX = MIN(0, labelWidth - m_rLabelRect.size.width); } CCRect rect = CCRectZero; rect.size = this->getBounds().size; CCRect r = m_rLabelRect; r.origin.x = -m_fString_left_offX; r.size.width = getStringViewLength(); this->setImageRect(r); m_pMark->setCenterOrigin(CCPoint( getCursorX()+labelOrginX, this->getBounds().size.height/2)); return; }
void CATextField::insertText(const char * text, int len) { if (len>3) { analyzeString(text,len); return; } if (m_nInputType == KEY_BOARD_INPUT_PASSWORD) { if (len>=2) return; if (spaceHolderIsOn) { spaceHolderIsOn = false; m_sText=""; } m_sText = m_sPassWord; m_sText.append(text); std::string password=""; for (int i=0; i<m_sText.length(); i++) { password.append("*"); } m_sPassWord = m_sText; m_sText = password; this->updateImage(); CCRect r = m_rLabelRect; r.origin.x = 0; r.origin.x = MAX(labelWidth-m_rLabelRect.size.width, 0); r.origin.x = MIN(0, r.origin.x); r.size.width = MIN(m_rLabelRect.size.width, labelWidth); this->setImageRect(r); float offsetX = MIN(labelWidth, m_rLabelRect.size.width); m_pMark->setCenterOrigin(CCPoint(offsetX+labelOrginX,this->getBounds().size.height/2)); return; } if (!strcmp(text, "\n")) { return; } std::string inputstr; if (spaceHolderIsOn) { spaceHolderIsOn = false; m_sText = ""; } if(isEditing) { isEditing =false; } willBg->setVisible(false); if (len > 0) { if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this,text, len)) { // delegate doesn't want to insert text return; } m_sLeft_string.append(text); float left_width = getStringLength(m_sLeft_string); TextAttribute t; t.charlength = left_width - m_fString_left_length; t.charsize = strlen(text); m_vByteLengthArr.insert(m_vByteLengthArr.begin()+getStringCharCount(m_sLeft_string), t); char str[512]=""; sprintf(str, "%s%s",m_sLeft_string.c_str(),m_sRight_string.c_str()); m_sText = str; this->updateImage(); m_fString_left_length = m_rLabelRect.size.width; //left_width; m_fString_right_length = m_rLabelRect.size.width - m_fString_left_length; if (m_fString_left_length + m_fString_left_offX > labelWidth) { m_fString_left_offX = labelWidth - m_fString_left_length; } CCRect rect = CCRectZero; rect.size = this->getBounds().size; CCRect r = m_rLabelRect; r.origin.x = -m_fString_left_offX; r.size.width = getStringViewLength(); this->setImageRect(r); } m_pMark->setCenterOrigin(CCPoint(getCursorX()+labelOrginX,this->getBounds().size.height/2)); }