File tree Expand file tree Collapse file tree 3 files changed +30
-0
lines changed
Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -316,6 +316,17 @@ class Event
316316 template <typename TEventSubtype>
317317 [[nodiscard]] bool is () const ;
318318
319+ // //////////////////////////////////////////////////////////
320+ // / \brief Attempt to get specified event subtype
321+ // /
322+ // / \tparam `TEventSubtype` Type of the desired event subtype
323+ // /
324+ // / \return Address of current event subtype, otherwise `nullptr`
325+ // /
326+ // //////////////////////////////////////////////////////////
327+ template <typename TEventSubtype>
328+ [[nodiscard]] TEventSubtype* getIf ();
329+
319330 // //////////////////////////////////////////////////////////
320331 // / \brief Attempt to get specified event subtype
321332 // /
Original file line number Diff line number Diff line change @@ -58,6 +58,16 @@ bool Event::is() const
5858}
5959
6060
61+ // //////////////////////////////////////////////////////////
62+ template <typename TEventSubtype>
63+ TEventSubtype* Event::getIf ()
64+ {
65+ static_assert (isEventSubtype<TEventSubtype>, " TEventSubtype must be a subtype of sf::Event" );
66+ if constexpr (isEventSubtype<TEventSubtype>)
67+ return std::get_if<TEventSubtype>(&m_data);
68+ }
69+
70+
6171// //////////////////////////////////////////////////////////
6272template <typename TEventSubtype>
6373const TEventSubtype* Event::getIf () const
Original file line number Diff line number Diff line change @@ -295,6 +295,15 @@ TEST_CASE("[Window] sf::Event")
295295 CHECK (sensorChanged.value == sf::Vector3f ());
296296 }
297297
298+ SECTION (" getIf()" )
299+ {
300+ sf::Event event = sf::Event::MouseMoved{{4 , 2 }};
301+ auto * mouseMoved = event.getIf <sf::Event::MouseMoved>();
302+ REQUIRE (mouseMoved);
303+ mouseMoved->position = sf::Vector2i (6 , 9 );
304+ CHECK (mouseMoved->position == sf::Vector2i (6 , 9 ));
305+ }
306+
298307 SECTION (" visit()" )
299308 {
300309 CHECK (sf::Event (sf::Event::Closed{}).visit (visitor) == " Closed" );
You can’t perform that action at this time.
0 commit comments