MCPcopy Create free account
hub / github.com/axmolengine/axmol / insertText

Method insertText

core/2d/TextFieldTTF.cpp:254–313  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

252}
253
254void TextFieldTTF::insertText(const char* text, size_t len)
255{
256 std::string insert(text, len);
257
258 // insert \n means input end
259 int pos = static_cast<int>(insert.find(StringUtils::AsciiCharacters::NewLine));
260 if ((int)insert.npos != pos)
261 {
262 len = pos;
263 insert.erase(pos);
264 }
265
266 if (len > 0)
267 {
268 if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), len))
269 {
270 // delegate doesn't want to insert text
271 return;
272 }
273
274 std::size_t countInsertChar = StringUtils::countUTF8Chars(insert);
275 _charCount += countInsertChar;
276
277 if (_cursorEnabled)
278 {
279 std::string sText;
280 sText.reserve(_inputText.length() + insert.length());
281 sText += _inputText;
282 auto pos = StringUtils::getUTF8ByteOffset(sText, _cursorPosition);
283 if (pos != std::string::npos)
284 sText.insert(pos, insert);
285 else
286 sText += insert;
287
288 setCursorPosition(_cursorPosition + countInsertChar);
289
290 setString(sText);
291 }
292 else
293 {
294 std::string sText(_inputText);
295 sText.append(insert);
296 setString(sText);
297 }
298 }
299
300 if ((int)insert.npos == pos)
301 {
302 return;
303 }
304
305 // '\n' inserted, let delegate process first
306 if (_delegate && _delegate->onTextFieldInsertText(this, "\n", 1))
307 {
308 return;
309 }
310
311 // if delegate hasn't processed, detach from IME by default

Callers

nothing calls this directly

Calls 10

countUTF8CharsFunction · 0.85
getUTF8ByteOffsetFunction · 0.85
findMethod · 0.45
eraseMethod · 0.45
onTextFieldInsertTextMethod · 0.45
c_strMethod · 0.45
reserveMethod · 0.45
lengthMethod · 0.45
insertMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected