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

Method update

examples/keyboard/Keyboard.cpp:374–413  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

372 }
373
374 void update(sf::Time frameTime)
375 {
376 // Animate m_moveFactors values linearly towards zero
377 static constexpr sf::Time transitionDuration = sf::milliseconds(200);
378 for (float& factor : m_moveFactors)
379 {
380 const float absoluteChange = std::min(std::abs(factor), frameTime / transitionDuration);
381 factor += factor > 0.f ? -absoluteChange : absoluteChange;
382 }
383
384 // Update vertices positions from m_moveFactors and opacity from real-time keyboard state
385 forEachKey(
386 [this](sf::Keyboard::Scancode scancode, const sf::FloatRect& rect)
387 {
388 const auto scancodeIndex = static_cast<std::size_t>(scancode);
389
390 static constexpr std::array square = {
391 sf::Vector2f(0.f, 0.f),
392 sf::Vector2f(1.f, 0.f),
393 sf::Vector2f(1.f, 1.f),
394 sf::Vector2f(0.f, 1.f),
395 };
396 static constexpr std::array cornerIndexes = {0u, 1u, 3u, 3u, 1u, 2u};
397
398 const float moveFactor = m_moveFactors[scancodeIndex];
399 const sf::Vector2f move(0.f, 2.f * moveFactor * (1.f - std::abs(moveFactor)) * padding);
400
401 const bool pressed = sf::Keyboard::isKeyPressed(scancode);
402
403 for (std::size_t vertexIndex = 0u; vertexIndex < 6u; ++vertexIndex)
404 {
405 sf::Vertex& vertex = m_triangles[6u * scancodeIndex + vertexIndex];
406 const sf::Vector2f& corner = square[cornerIndexes[vertexIndex]];
407 static constexpr sf::Vector2f pad(padding, padding);
408 vertex.position = rect.position + pad + (rect.size - 2.f * pad).componentWiseMul(corner) + move;
409 vertex.color.a = pressed ? 96 : 48;
410 }
411 m_labels[scancodeIndex].setPosition(rect.position + rect.size / 2.f + move);
412 });
413 }
414
415private:
416 void draw(sf::RenderTarget& target, sf::RenderStates states) const override

Callers

nothing calls this directly

Calls 2

isKeyPressedFunction · 0.50
setPositionMethod · 0.45

Tested by

no test coverage detected