| 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 | { |
no test coverage detected