MCPcopy Create free account
hub / github.com/SFML/SFML / updateCursorPosition

Method updateCursorPosition

examples/text/Text.cpp:322–343  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

320 }
321
322 void updateCursorPosition(int offset)
323 {
324 const auto& string = text.getString();
325
326 // The Unicode standard recommends that grapheme boundaries
327 // be used as text insertion/deletion points since this is
328 // what most users would expect from text editing software
329
330 // We first increment/decrement the cursor based on user input
331 cursorIndex = static_cast<std::size_t>(
332 std::clamp(static_cast<int>(cursorIndex) + offset, 0, static_cast<int>(string.getSize())));
333
334 // As long as we are not at a grapheme boundary yet, keep
335 // moving the cursor position until we arrive at one
336 while (cursorIndex < string.getSize() && !string.isGraphemeBoundary(cursorIndex))
337 cursorIndex += static_cast<std::size_t>(offset ? (offset / std::abs(offset)) : 0);
338
339 // If we are beyond the end of the string just return the correct
340 // end-of-text position based on the direction of the text
341 cursor.setPosition(
342 cursorPositions[(cursorIndex >= cursorPositions.size()) ? cursorPositions.size() - 1 : cursorIndex]);
343 }
344
345 void handleMousePress(const sf::Vector2f& position)
346 {

Callers 1

mainFunction · 0.80

Calls 4

isGraphemeBoundaryMethod · 0.80
getStringMethod · 0.45
getSizeMethod · 0.45
setPositionMethod · 0.45

Tested by

no test coverage detected