mirror of
https://github.com/SFML/SFML.git
synced 2025-01-18 15:25:12 +08:00
Add non-const overload of sf::Event::getIf
This commit is contained in:
parent
3c084bf661
commit
796592edae
@ -316,6 +316,17 @@ public:
|
||||
template <typename TEventSubtype>
|
||||
[[nodiscard]] bool is() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Attempt to get specified event subtype
|
||||
///
|
||||
/// \tparam `TEventSubtype` Type of the desired event subtype
|
||||
///
|
||||
/// \return Address of current event subtype, otherwise `nullptr`
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename TEventSubtype>
|
||||
[[nodiscard]] TEventSubtype* getIf();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Attempt to get specified event subtype
|
||||
///
|
||||
|
@ -58,6 +58,16 @@ bool Event::is() const
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename TEventSubtype>
|
||||
TEventSubtype* Event::getIf()
|
||||
{
|
||||
static_assert(isEventSubtype<TEventSubtype>, "TEventSubtype must be a subtype of sf::Event");
|
||||
if constexpr (isEventSubtype<TEventSubtype>)
|
||||
return std::get_if<TEventSubtype>(&m_data);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename TEventSubtype>
|
||||
const TEventSubtype* Event::getIf() const
|
||||
|
@ -295,6 +295,15 @@ TEST_CASE("[Window] sf::Event")
|
||||
CHECK(sensorChanged.value == sf::Vector3f());
|
||||
}
|
||||
|
||||
SECTION("getIf()")
|
||||
{
|
||||
sf::Event event = sf::Event::MouseMoved{{4, 2}};
|
||||
auto* mouseMoved = event.getIf<sf::Event::MouseMoved>();
|
||||
REQUIRE(mouseMoved);
|
||||
mouseMoved->position = sf::Vector2i(6, 9);
|
||||
CHECK(mouseMoved->position == sf::Vector2i(6, 9));
|
||||
}
|
||||
|
||||
SECTION("visit()")
|
||||
{
|
||||
CHECK(sf::Event(sf::Event::Closed{}).visit(visitor) == "Closed");
|
||||
|
Loading…
Reference in New Issue
Block a user