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

Method handleMousePress

examples/text/Text.cpp:345–411  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

343 }
344
345 void handleMousePress(const sf::Vector2f& position)
346 {
347 // Filter out the event if the press was not on this text
348 if (!boundingBox.getGlobalBounds().contains(position))
349 return;
350
351 const auto& transform = text.getTransform();
352
353 // Since performance isn't really important here we will just use brute-force
354
355 // Glyph box
356 {
357 // Compute the distance between the center of each glyph box
358 // and the mouse position and pick the index with the lowest distance
359 auto closestIndex = std::size_t{0};
360 auto closestDistance = text.getLocalBounds().size.x;
361
362 for (auto i = 0u; i < text.getShapedGlyphs().size(); ++i)
363 {
364 glyphIndex = i;
365 updateGlyphBox(0);
366
367 const auto distance = (transform.transformPoint(glyphBox.getGlobalBounds().getCenter()) - position).length();
368
369 if (distance < closestDistance)
370 {
371 closestIndex = i;
372 closestDistance = distance;
373 }
374 }
375
376 glyphIndex = closestIndex;
377 updateGlyphBox(0);
378 }
379
380 // Cursor
381 {
382 // Compute the distance between the center of each valid cursor position
383 // and the mouse position and pick the index with the lowest distance
384 auto closestIndex = std::size_t{0};
385 auto closestDistance = text.getLocalBounds().size.x;
386
387 cursorIndex = 0u;
388 updateCursorPosition(0);
389
390 // Keep moving the cursor until it no longer wants to move
391 while (true)
392 {
393 const auto distance = (transform.transformPoint(cursor.getGlobalBounds().getCenter()) - position).length();
394
395 if (distance < closestDistance)
396 {
397 closestIndex = cursorIndex;
398 closestDistance = distance;
399 }
400
401 const auto previousCursorIndex = cursorIndex;
402 updateCursorPosition(1);

Callers 1

mainFunction · 0.80

Calls 4

lengthMethod · 0.80
getCenterMethod · 0.80
getGlobalBoundsMethod · 0.45
getLocalBoundsMethod · 0.45

Tested by

no test coverage detected