| 47 | // Make sure all defined operator()s return the same type. |
| 48 | // The operator()s can also have void return type if there is nothing to return. |
| 49 | struct Visitor |
| 50 | { |
| 51 | explicit Visitor(Application& app) : application(app) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | std::optional<std::string> operator()(const sf::Event::Closed&) |
| 56 | { |
| 57 | application.m_window.close(); |
| 58 | return std::nullopt; |
| 59 | } |
| 60 | |
| 61 | std::optional<std::string> operator()(const sf::Event::KeyPressed& keyPress) |
| 62 | { |
| 63 | // When the enter key is pressed, switch to the next handler type |
| 64 | if (keyPress.code == sf::Keyboard::Key::Enter) |
| 65 | { |
| 66 | application.m_handlerType = HandlerType::Overload; |
| 67 | application.m_handlerText.setString("Current Handler: Overload"); |
| 68 | } |
| 69 | |
| 70 | return "Key Pressed: " + sf::Keyboard::getDescription(keyPress.scancode).toAnsiString(); |
| 71 | } |
| 72 | |
| 73 | std::optional<std::string> operator()(const sf::Event::KeyReleased& keyRelease) |
| 74 | { |
| 75 | return "Key Released: " + sf::Keyboard::getDescription(keyRelease.scancode).toAnsiString(); |
| 76 | } |
| 77 | |
| 78 | std::optional<std::string> operator()(const sf::Event::MouseMoved& mouseMoved) |
| 79 | { |
| 80 | return "Mouse Moved: " + vec2ToString(mouseMoved.position); |
| 81 | } |
| 82 | |
| 83 | std::optional<std::string> operator()(const sf::Event::MouseButtonPressed&) |
| 84 | { |
| 85 | return "Mouse Pressed"; |
| 86 | } |
| 87 | |
| 88 | std::optional<std::string> operator()(const sf::Event::TouchBegan& touchBegan) |
| 89 | { |
| 90 | return "Touch Began: " + vec2ToString(touchBegan.position); |
| 91 | } |
| 92 | |
| 93 | std::optional<std::string> operator()(const sf::Event::TouchEnded& touchEnded) |
| 94 | { |
| 95 | return "Touch Ended: " + vec2ToString(touchEnded.position); |
| 96 | } |
| 97 | |
| 98 | std::optional<std::string> operator()(const sf::Event::TouchMoved& touchMoved) |
| 99 | { |
| 100 | return "Touch Moved: " + vec2ToString(touchMoved.position); |
| 101 | } |
| 102 | |
| 103 | // When defining a visitor, make sure all event types can be handled by it. |
| 104 | // If you don't intend on exhaustively specifying an operator() for each |
| 105 | // event type, you can provide a templated operator() that will be selected |
| 106 | // by overload resolution when no other event type matches. |