2017-11-23 13:54:27 +01:00
|
|
|
#include "externals.hh"
|
|
|
|
#include "ui-actions.hh"
|
2017-11-23 14:43:15 +01:00
|
|
|
#include "ui-utilities.hh"
|
2017-11-23 13:54:27 +01:00
|
|
|
|
|
|
|
#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_KeyboardModifier::SHIFT )
|
|
|
|
? toupper( character ) : tolower( character ) ) ,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
snprintf( buffer , size , "%s%s%s" ,
|
|
|
|
( modifiers & E_KeyboardModifier::CTRL ) ? "C-" : "" ,
|
|
|
|
( modifiers & E_KeyboardModifier::ALT ) ? "A-" : "" ,
|
|
|
|
[&]( ) {
|
|
|
|
switch ( character ) {
|
|
|
|
case ' ': return "<space>";
|
|
|
|
case '\n': return "<enter>";
|
|
|
|
default: return ch;
|
|
|
|
}
|
|
|
|
}( ) );
|
|
|
|
}
|
|
|
|
|
2017-11-24 11:34:17 +01:00
|
|
|
namespace ebcl {
|
|
|
|
M_DEFINE_HASH( T_KeyboardShortcut )
|
|
|
|
{
|
|
|
|
return uint32_t( item.character ) << 8
|
|
|
|
| uint32_t( item.modifiers );
|
|
|
|
}
|
|
|
|
} // namespace ebcl
|
|
|
|
|
2017-11-23 13:54:27 +01:00
|
|
|
|
|
|
|
/*= 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 ) {
|
2017-11-23 14:43:15 +01:00
|
|
|
PushDisabled( );
|
2017-11-23 13:54:27 +01:00
|
|
|
}
|
|
|
|
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 ) {
|
2017-11-23 14:43:15 +01:00
|
|
|
PopDisabled( );
|
2017-11-23 13:54:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( rv ) {
|
|
|
|
handler( );
|
|
|
|
}
|
|
|
|
}
|