demotool/ui-dialogs.hh
Emmanuel BENOîT 4774426962 UI - More refactoring
The idea is to split off all UI/display code from e.g. the parser. This
could take a while.
2017-11-23 14:15:17 +01:00

91 lines
2.3 KiB
C++

#pragma once
#ifndef REAL_BUILD
# include "externals.hh"
#endif
/*= DIALOGS ==================================================================*/
// Base class for a modal dialog that can be pushed to the window's modal
// stack.
class A_ModalDialog
{
private:
ebcl::T_Buffer< char > id_;
bool open_{ false };
T_Optional< ImVec2 > initialSize_;
T_StaticArray< ebcl::T_Buffer< char > , 4 > buttons_;
protected:
A_ModalDialog( char const* id ) noexcept;
A_ModalDialog( T_String const& id ) noexcept;
void setInitialSize( const float width ,
const float height ) noexcept
{ initialSize_.setNew( width , height ); }
// Add a button w/ the specified name, return its identifier
uint8_t addButton( char const* name ) noexcept;
uint8_t addButton( T_String const& name ) noexcept;
// Initialisation, called right before the window is opened.
// Does nothing by default.
virtual void initDialog( ) noexcept;
// Draw the dialog box's contents, returns a mask that determines
// which buttons should be enabled.
virtual uint8_t drawDialog( ) noexcept = 0;
// Button click handler. Returns true if the dialog should be
// closed.
virtual bool onButton( uint8_t button ) noexcept = 0;
public:
NO_COPY( A_ModalDialog );
NO_MOVE( A_ModalDialog );
virtual ~A_ModalDialog( );
// Draw and handle the dialog box. Returns true if it must be kept,
// or false if it must be removed from the stack.
bool draw( ) noexcept;
};
using P_ModalDialog = T_OwnPtr< A_ModalDialog >;
// Simple modal message box with configurable buttons
class T_MessageBox : public A_ModalDialog
{
public:
enum E_Button {
BT_OK ,
BT_CANCEL ,
BT_YES ,
BT_NO ,
};
using T_Buttons = T_Flags< E_Button >;
// Result handler
using F_Handler = std::function< void( E_Button ) >;
private:
ebcl::T_Buffer< char > text_;
F_Handler handler_;
uint8_t buttonMask_{ 0 };
T_StaticArray< E_Button , 4 > buttons_;
void setButtons( T_Buttons buttons ) noexcept;
protected:
uint8_t drawDialog( ) noexcept override;
bool onButton( uint8_t button ) noexcept override;
public:
T_MessageBox( char const* title ,
char const* text ,
F_Handler handler = { } ,
T_Buttons buttons = BT_OK ) noexcept;
T_MessageBox( T_String const& title ,
T_String const& text ,
F_Handler handler = { } ,
T_Buttons buttons = BT_OK ) noexcept;
};