demotool/ui-actions.hh
Emmanuel BENOîT 88e5724fe9 UI - Started working on actions
+ Can be registered
+ Can be used to draw menu items and toolbar buttons
2017-11-23 13:54:39 +01:00

57 lines
1.6 KiB
C++

#pragma once
#include "ui-input.hh"
/*= COMMON ACTION DEFINITIONS ================================================*/
struct T_KeyboardShortcut
{
char character;
T_KeyboardModifiers modifiers;
explicit constexpr T_KeyboardShortcut(
char character ,
T_KeyboardModifiers modifiers = { } ) noexcept
: character{ character } , modifiers{ modifiers }
{}
// Writes the keyboard shortcut to a buffer
void toString( char* buffer , size_t size ) const noexcept;
};
struct T_UIAction
{
// Function to call when the action is executed
using F_ActionHandler = std::function< void( void ) >;
// Function that checks whether the action is enabled
using F_ActionChecker = std::function< bool( void ) >;
T_String id;
T_String text; // Text, if different from the identifier
char const* icon{ nullptr };
T_Optional< T_KeyboardShortcut > shortcut;
F_ActionHandler handler;
F_ActionChecker enabled;
T_UIAction( char const* id ,
F_ActionHandler handler ) noexcept
: id( T_String::Pooled( id ) ) ,
handler( std::move( handler ) )
{ assert( this->handler ); }
T_UIAction( T_String const& id ,
F_ActionHandler handler ) noexcept
: id( id ) , handler( std::move( handler ) )
{ assert( this->handler ); }
T_UIAction& setShortcut( const T_KeyboardShortcut sc ) noexcept
{ shortcut = sc; return *this; }
T_UIAction& setText( T_String const& t ) noexcept
{ text = t; return *this; }
T_UIAction& setIcon( char const* const i ) noexcept
{ icon = i; return *this; }
T_UIAction& setEnabledCheck( F_ActionChecker check ) noexcept
{ enabled = std::move( check ); return *this; }
void menuItem( ) const noexcept;
void tbButton( ) const noexcept;
};