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

Trait that adds keyboard-focus and blur callbacks to any Core object. More...

#include <Malena/Traits/Interaction/Focusable.h>

Inheritance diagram for ml::Focusable:
[legend]

Public Member Functions

void onBlur (std::function< void()> callback, bool overwrite=true)
 Register a no-argument callback invoked when this component loses keyboard focus.
void onBlur (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=true)
 Register a callback invoked when this component loses keyboard focus, receiving the raw SFML event.
void onFocus (std::function< void()> callback, bool overwrite=true)
 Register a no-argument callback invoked when this component gains keyboard focus.
void onFocus (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=true)
 Register a callback invoked when this component gains keyboard focus, 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.

Public Attributes

Core_selfCore = nullptr

Detailed Description

Trait that adds keyboard-focus and blur callbacks to any Core object.

Focusable is automatically inherited by every ml::Core object. A component gains focus when the user clicks it; it loses focus (blurs) when the user clicks elsewhere or focus is programmatically transferred. While focused, a component receives keyboard events via Keyable.

Usage

// Show a focus ring on focus, hide it on blur
myInput.onFocus([&]{ focusRing.setOutlineThickness(2.f); });
myInput.onBlur([&]{ focusRing.setOutlineThickness(0.f); });
// With SFML event data
myInput.onFocus([](const std::optional<sf::Event>& e){
// e holds the MouseButtonReleased event that triggered focus
});

To unsubscribe:

myInput.unsubscribe(ml::Event::FOCUS);
myInput.unsubscribe(ml::Event::BLUR);
@ FOCUS
Component gained keyboard focus.
Definition Event.h:51
@ BLUR
Component lost keyboard focus.
Definition Event.h:52
See also
Keyable, Clickable, ml::Event::FOCUS, ml::Event::BLUR, Unsubscribable

Definition at line 45 of file Focusable.h.

Member Function Documentation

◆ onBlur() [1/2]

void ml::Focusable::onBlur ( std::function< void()> callback,
bool overwrite = true )

Register a no-argument callback invoked when this component loses keyboard focus.

Parameters
callbackFunction invoked with no arguments when focus is lost.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onBlur() [2/2]

void ml::Focusable::onBlur ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = true )

Register a callback invoked when this component loses keyboard focus, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event when focus is lost.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onFocus() [1/2]

void ml::Focusable::onFocus ( std::function< void()> callback,
bool overwrite = true )

Register a no-argument callback invoked when this component gains keyboard focus.

Parameters
callbackFunction invoked with no arguments when focus is gained.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onFocus() [2/2]

void ml::Focusable::onFocus ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = true )

Register a callback invoked when this component gains keyboard focus, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event when focus is gained.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ 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.

Member Data Documentation

◆ _selfCore

Core* ml::EventReceiver::_selfCore = nullptr
inherited

The owning Core, stamped by Core's constructor.

Traits subscribe from inside their OWN constructors (ml::TextInput calls onFocus(), ComponentCore subscribes to CLICK/HOVER/DRAG), at which point the derived type does not exist yet. Recovering the Core by dynamic_cast there is undefined: Clang returns something usable, MSVC throws "Access violation - no RTTI data!" and aborts the process. Core sets this on every one of its EventReceiver subobjects before any derived constructor runs, so no RTTI is ever needed.

Definition at line 93 of file EventReceiver.h.


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