Loading...
Searching...
No Matches
ml::Hoverable Class Reference

Trait that adds mouse-hover and mouse-leave callbacks to any Core object. More...

#include <Hoverable.h>

Inheritance diagram for ml::Hoverable:

Public Member Functions

void onHover (std::function< void()> callback)
 Register a no-argument callback invoked when the mouse enters this component's bounds.
void onHover (std::function< void(const std::optional< sf::Event > &event)> callback)
 Register a callback invoked when the mouse enters this component's bounds, receiving the raw SFML event.
void onUnhover (std::function< void()> callback)
 Register a no-argument callback invoked when the mouse leaves this component's bounds.
void onUnhover (std::function< void(const std::optional< sf::Event > &event)> callback)
 Register a callback invoked when the mouse leaves this component's bounds, receiving the raw SFML event.
virtual void process (const std::string &key, const std::optional< sf::Event > &event)
 Invoke all callbacks registered for key.
template<typename ENUM_TYPE>
void process (ENUM_TYPE eventName, const std::optional< sf::Event > &event)
 Invoke all callbacks registered for eventName.

Detailed Description

Trait that adds mouse-hover and mouse-leave callbacks to any Core object.

Hoverable is automatically inherited by every ml::Core object. The hover event fires once when the mouse cursor enters a component's bounds; the unhover event fires once when it leaves.

Usage

// Highlight on hover, restore on leave
myRect.onHover([&]{ myRect.setFillColor(sf::Color::Yellow); });
myRect.onUnhover([&]{ myRect.setFillColor(sf::Color::White); });
// With SFML event data
myRect.onHover([](const std::optional<sf::Event>& e){
// e holds the MouseMoved event that triggered the enter
});
static const Color White
static const Color Yellow

To unsubscribe:

myRect.unsubscribe(ml::Event::HOVER);
myRect.unsubscribe(ml::Event::UNHOVER);
@ UNHOVER
Mouse left component bounds.
Definition Event.h:38
@ HOVER
Mouse entered component bounds.
Definition Event.h:37
See also
Clickable, Focusable, ml::Event::HOVER, ml::Event::UNHOVER, Unsubscribable

Definition at line 42 of file Hoverable.h.

Member Function Documentation

◆ onHover() [1/2]

void ml::Hoverable::onHover ( std::function< void()> callback)

Register a no-argument callback invoked when the mouse enters this component's bounds.

Parameters
callbackFunction invoked with no arguments on hover enter.

◆ onHover() [2/2]

void ml::Hoverable::onHover ( std::function< void(const std::optional< sf::Event > &event)> callback)

Register a callback invoked when the mouse enters this component's bounds, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event on hover enter.

◆ onUnhover() [1/2]

void ml::Hoverable::onUnhover ( std::function< void()> callback)

Register a no-argument callback invoked when the mouse leaves this component's bounds.

Parameters
callbackFunction invoked with no arguments on hover leave.

◆ onUnhover() [2/2]

void ml::Hoverable::onUnhover ( std::function< void(const std::optional< sf::Event > &event)> callback)

Register a callback invoked when the mouse leaves this component's bounds, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event on hover leave.

◆ process() [1/2]

virtual void ml::EventReceiver::process ( const std::string & key,
const std::optional< sf::Event > & event )
virtualinherited

Invoke all callbacks registered for key.

Called by framework dispatchers. key is the string produced by EnumKey::get(eventEnum).

Parameters
keyString key identifying the event.
eventSFML event forwarded to each callback.

◆ process() [2/2]

template<typename ENUM_TYPE>
void ml::EventReceiver::process ( ENUM_TYPE eventName,
const std::optional< sf::Event > & event )
inherited

Invoke all callbacks registered for eventName.

Template overload that accepts any enum value directly, converting to a string key via EnumKey::get() internally.

Template Parameters
ENUM_TYPEAny enum type.
Parameters
eventNameThe enum value identifying the event.
eventSFML event forwarded to each callback.

The documentation for this class was generated from the following file: