demotool/window.hh

131 lines
3.2 KiB
C++
Raw Normal View History

#pragma once
#ifndef REAL_BUILD
# include "externals.hh"
#endif
2017-11-22 14:13:02 +01:00
// 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_;
2017-11-22 14:13:02 +01:00
bool open_{ false };
T_Optional< ImVec2 > initialSize_;
T_StaticArray< ebcl::T_Buffer< char > , 4 > buttons_;
2017-11-22 14:13:02 +01:00
protected:
A_ModalDialog( char const* id ) noexcept;
A_ModalDialog( T_String const& id ) noexcept;
2017-11-22 14:13:02 +01:00
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;
2017-11-22 14:13:02 +01:00
// 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;
2017-11-22 14:13:02 +01:00
// Button click handler. Returns true if the dialog should be
// closed.
virtual bool onButton( uint8_t button ) noexcept = 0;
2017-11-22 14:13:02 +01:00
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 >;
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;
};
struct T_Window
{
T_Window( );
~T_Window( );
2017-11-03 09:08:19 +01:00
void startFrame( const bool capture ,
ImVec2 const& mouseInitial ) const;
void warpMouse( ImVec2 const& pos ) const;
void swap( ) const;
2017-11-22 14:13:02 +01:00
void pushDialog( P_ModalDialog dialog ) noexcept
{ modals_.add( std::move( dialog ) ); }
void msgbox( char const* title ,
char const* text ,
T_MessageBox::F_Handler handler = { } ,
T_MessageBox::T_Buttons buttons = T_MessageBox::BT_OK )
{ modals_.add( NewOwned< T_MessageBox >( title , text , handler , buttons ) ); }
void msgbox( T_String const& title ,
T_String const& text ,
T_MessageBox::F_Handler handler = { } ,
T_MessageBox::T_Buttons buttons = T_MessageBox::BT_OK )
{ modals_.add( NewOwned< T_MessageBox >( title , text , handler , buttons ) ); }
2017-11-22 14:13:02 +01:00
void handleDialogs( ) noexcept;
ImFont* defaultFont( ) const noexcept
{ return defaultFont_; }
ImFont* smallFont( ) const noexcept
{ return smallFont_; }
private:
SDL_Window * window;
SDL_GLContext gl;
ImFont* defaultFont_;
ImFont* smallFont_;
2017-11-22 14:13:02 +01:00
T_AutoArray< P_ModalDialog , 8 > modals_;
};