demotool/ui-actions.cc

95 lines
1.9 KiB
C++

#include "externals.hh"
#include "ui-actions.hh"
#include "ui-utilities.hh"
#include <imgui_internal.h>
/*= T_KeyboardShortcut =======================================================*/
void T_KeyboardShortcut::toString(
char* const buffer ,
const size_t size ) const noexcept
{
const char ch[ 2 ] = {
char( ( modifiers & E_KbdMod::SHIFT )
? toupper( character ) : tolower( character ) ) ,
0
};
snprintf( buffer , size , "%s%s%s" ,
( modifiers & E_KbdMod::CTRL ) ? "C-" : "" ,
( modifiers & E_KbdMod::ALT ) ? "A-" : "" ,
[&]( ) {
switch ( character ) {
case ' ': return "<space>";
case '\n': return "<enter>";
default: return ch;
}
}( ) );
}
namespace ebcl {
M_DEFINE_HASH( T_KeyboardShortcut )
{
return uint32_t( item.character ) << 8
| uint32_t( item.modifiers );
}
} // namespace ebcl
/*= T_UIAction ===============================================================*/
void T_UIAction::menuItem( ) const noexcept
{
const bool e{ enabled ? enabled( ) : true };
T_String const& title{ text ? text : id };
const uint32_t ts{ title.size( ) };
char name[ ts + 1 ];
memcpy( name , title.data( ) , ts );
name[ ts ] = 0;
char kbs[ 20 ];
char const* kbsp = shortcut ? kbs : nullptr;
if ( shortcut ) {
shortcut->toString( kbs , 20 );
}
if ( ImGui::MenuItem( name , kbsp , false , e ) ) {
handler( );
}
}
void T_UIAction::tbButton( ) const noexcept
{
using namespace ImGui;
static T_StringBuilder sb;
sb.clear( ) << ( icon ? icon : "" ) << "##" << id << '\0';
const bool e{ enabled ? enabled( ) : true };
if ( !e ) {
PushDisabled( );
}
const bool rv{ Button( sb.data( ) , ImVec2{ 20 , 0 } ) };
if ( e && IsItemHovered( ) ) {
sb.clear( ) << ( text ? text : id );
if ( shortcut ) {
char kbs[ 20 ];
shortcut->toString( kbs , 20 );
sb << ' ' << kbs;
}
sb << '\0';
BeginTooltip( );
Text( "%s" , sb.data( ) );
EndTooltip( );
}
if ( !e ) {
PopDisabled( );
}
if ( rv ) {
handler( );
}
}