UI - Refactoring progress

(see previous log message)
This commit is contained in:
Emmanuel BENOîT 2017-11-24 09:51:05 +01:00
parent 90e1451141
commit fac625eaf9
10 changed files with 175 additions and 133 deletions

View file

@ -35,6 +35,7 @@ COMMON = \
ui-actions.cc \ ui-actions.cc \
ui-app.cc \ ui-app.cc \
ui-colorgrading.cc \ ui-colorgrading.cc \
ui-demo.cc \
ui-dialogs.cc \ ui-dialogs.cc \
ui-imgui-sdl.cc \ ui-imgui-sdl.cc \
ui-odbg.cc \ ui-odbg.cc \
@ -50,7 +51,6 @@ COMMON = \
# END COMMON # END COMMON
DEMO = \ DEMO = \
demo.cc \
main.cc \ main.cc \
# END DEMO # END DEMO

View file

@ -3,7 +3,7 @@
#include "c-opcomp.hh" #include "c-opcomp.hh"
#include <ebcl/Algorithms.hh> #include <ebcl/Algorithms.hh>
#define INVASIVE_TRACES //#define INVASIVE_TRACES
using namespace ebcl; using namespace ebcl;
using namespace ops; using namespace ops;

115
main.cc
View file

@ -1,5 +1,4 @@
#include "externals.hh" #include "externals.hh"
#include "demo.hh"
#include "common.hh" #include "common.hh"
#include "c-sync.hh" #include "c-sync.hh"
@ -7,6 +6,7 @@
#include "ui.hh" #include "ui.hh"
#include "ui-app.hh" #include "ui-app.hh"
#include "ui-demo.hh"
#include "ui-imgui-sdl.hh" #include "ui-imgui-sdl.hh"
#include "ui-odbg.hh" #include "ui-odbg.hh"
#include "ui-opemu.hh" #include "ui-opemu.hh"
@ -31,9 +31,6 @@ struct T_Main
private: private:
bool done = false; bool done = false;
bool capture = false;
ImVec2 mouseInitial;
ImVec2 mouseMove;
uint32_t stopResize = 0; uint32_t stopResize = 0;
ImVec2 prevSize; ImVec2 prevSize;
@ -44,7 +41,6 @@ struct T_Main
void initDemo( ); void initDemo( );
void startIteration( ); void startIteration( );
void handleCapture( );
void makeUI( ); void makeUI( );
void render( ); void render( );
}; };
@ -53,15 +49,16 @@ struct T_Main
T_Main::T_Main( ) T_Main::T_Main( )
{ {
Common::Init( );
UI::Init( ); UI::Init( );
prevSize = ImVec2( -1 , -1 ); prevSize = ImVec2( -1 , -1 );
} }
void T_Main::mainLoop( ) void T_Main::mainLoop( )
{ {
auto& m( UI::Main( ) );
auto& p( UI::Profiler( ) ); auto& p( UI::Profiler( ) );
while ( !done ) { while ( !m.exiting( ) ) {
// Check whether there's a resize in progress
auto const& dspSize( ImGui::GetIO( ).DisplaySize ); auto const& dspSize( ImGui::GetIO( ).DisplaySize );
if ( prevSize.x != dspSize.x || prevSize.y != dspSize.y ) { if ( prevSize.x != dspSize.x || prevSize.y != dspSize.y ) {
const auto doit( prevSize.x > 0 ); const auto doit( prevSize.x > 0 );
@ -71,6 +68,8 @@ void T_Main::mainLoop( )
} }
} }
// If there was a resize and some time has passed,
// re-initialise the demo.
bool needInit( !demo ); bool needInit( !demo );
if ( stopResize > 0 ) { if ( stopResize > 0 ) {
stopResize --; stopResize --;
@ -81,26 +80,31 @@ void T_Main::mainLoop( )
if ( needInit ) { if ( needInit ) {
initDemo( ); initDemo( );
} }
#warning FIXME remove this #warning FIXME remove this
if ( !sequencer ) { if ( !sequencer ) {
sequencer.setNew( ); sequencer.setNew( );
} }
// Update file watcher and shaders
Common::Watcher( ).check( ); Common::Watcher( ).check( );
UI::Shaders( ).update( ); UI::Shaders( ).update( );
// Display
glFinish( ); glFinish( );
p.startFrame( ); p.startFrame( );
p.start( "Full frame" ); p.start( "Full frame" );
startIteration( ); m.handleEvents( );
if ( !done ) {
handleCapture( ); if ( !m.exiting( ) ) {
makeUI( ); makeUI( );
render( ); render( );
p.end( "Full frame" ); m.render( );
UI::Main( ).swap( );
p.endFrame( );
} }
p.end( "Full frame" );
m.swap( );
p.endFrame( );
} }
} }
@ -108,7 +112,6 @@ T_Main::~T_Main( )
{ {
demo.clear( ); demo.clear( );
UI::Shutdown( ); UI::Shutdown( );
Common::Shutdown( );
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -130,83 +133,6 @@ void T_Main::initDemo( )
} }
} }
void T_Main::startIteration( )
{
SDL_Event event;
mouseMove = ImVec2( );
while ( SDL_PollEvent( &event ) ) {
ImGui_ImplSdl_ProcessEvent( &event );
if ( event.type == SDL_QUIT ) {
done = true;
return;
}
if ( capture && event.type == SDL_MOUSEMOTION ) {
mouseMove.x += event.motion.xrel;
mouseMove.y += event.motion.yrel;
}
}
UI::Main( ).startFrame( capture , mouseInitial );
ImGui::GetIO( ).MouseDrawCursor = true;
}
void T_Main::handleCapture( )
{
using namespace ImGui;
auto const& io( GetIO( ) );
const T_MouseButtons mb( ([]() {;
T_MouseButtons mb;
if ( IsMouseDown( 0 ) ) {
mb |= E_MouseButton::LEFT;
}
if ( IsMouseDown( 1 ) ) {
mb |= E_MouseButton::RIGHT;
}
if ( IsMouseDown( 2 ) ) {
mb |= E_MouseButton::MIDDLE;
}
return mb;
})() );
const T_KeyboardModifiers kb( ([&io]() {
T_KeyboardModifiers kb;
if ( io.KeyCtrl ) {
kb |= E_KeyboardModifier::CTRL;
}
if ( io.KeyShift ) {
kb |= E_KeyboardModifier::SHIFT;
}
if ( io.KeyAlt ) {
kb |= E_KeyboardModifier::ALT;
}
return kb;
})() );
const bool appCanGrab( !( ImGui::IsMouseHoveringAnyWindow( )
|| io.WantCaptureMouse
|| io.WantCaptureKeyboard ) );
if ( capture && !mb ) {
capture = false;
CaptureMouseFromApp( false );
SDL_SetRelativeMouseMode( SDL_FALSE );
UI::Main( ).warpMouse( mouseInitial );
SetMouseCursor( ImGuiMouseCursor_Arrow );
} else if ( capture ) {
SetMouseCursor( ImGuiMouseCursor_Move );
UI::Sync( ).handleDragAndDrop( mouseMove , kb , mb );
} else if ( appCanGrab && mb ) {
capture = true;
mouseInitial = GetMousePos( );
CaptureMouseFromApp( true );
SDL_SetRelativeMouseMode( SDL_TRUE );
SetMouseCursor( ImGuiMouseCursor_Move );
}
if ( ( appCanGrab || capture ) && io.MouseWheel ) {
UI::Sync( ).handleWheel( io.MouseWheel , kb , mb );
}
}
void T_Main::makeUI( ) void T_Main::makeUI( )
{ {
using namespace ImGui; using namespace ImGui;
@ -276,13 +202,6 @@ void T_Main::render( )
glClearColor( 0 , 0 , 0 , 1 ); glClearColor( 0 , 0 , 0 , 1 );
glClear( GL_COLOR_BUFFER_BIT ); glClear( GL_COLOR_BUFFER_BIT );
} }
UI::Main( ).handleDialogs( );
glUseProgram( 0 );
glBindProgramPipeline( 0 );
UI::Textures( ).reset( );
glClearColor( 0 , 0 , 0 , 1 );
ImGui::Render( );
} }

110
ui-app.cc
View file

@ -1,6 +1,8 @@
#include "externals.hh" #include "externals.hh"
#include "ui.hh"
#include "ui-app.hh" #include "ui-app.hh"
#include "ui-imgui-sdl.hh" #include "ui-imgui-sdl.hh"
#include "ui-texture.hh"
#include <imgui_internal.h> #include <imgui_internal.h>
@ -29,18 +31,18 @@ T_UIApp::T_UIApp( )
SDL_DisplayMode current; SDL_DisplayMode current;
SDL_GetCurrentDisplayMode( 0 , &current ); SDL_GetCurrentDisplayMode( 0 , &current );
window = SDL_CreateWindow( "DEMO", window_ = SDL_CreateWindow( "DEMO",
SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED ,
1280 , 720 , 1280 , 720 ,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE ); SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE );
gl = SDL_GL_CreateContext( window ); gl_ = SDL_GL_CreateContext( window_ );
glewInit( ); glewInit( );
if ( !GLEW_VERSION_4_5 ) { if ( !GLEW_VERSION_4_5 ) {
fprintf( stderr , "OpenGL 4.5 required\n" ); fprintf( stderr , "OpenGL 4.5 required\n" );
exit( 1 ); exit( 1 );
} }
ImGui_ImplSdl_Init( window ); ImGui_ImplSdl_Init( window_ );
using namespace ImGui; using namespace ImGui;
StyleColorsDark( ); StyleColorsDark( );
@ -76,8 +78,8 @@ T_UIApp::T_UIApp( )
T_UIApp::~T_UIApp( ) T_UIApp::~T_UIApp( )
{ {
ImGui_ImplSdl_Shutdown( ); ImGui_ImplSdl_Shutdown( );
SDL_GL_DeleteContext( gl ); SDL_GL_DeleteContext( gl_ );
SDL_DestroyWindow( window ); SDL_DestroyWindow( window_ );
SDL_Quit( ); SDL_Quit( );
} }
@ -105,22 +107,100 @@ void T_UIApp::actionButton(
} }
} }
void T_UIApp::startFrame( void T_UIApp::handleEvents( ) noexcept
const bool capture ,
ImVec2 const& mouseInitial ) const
{ {
ImGui_ImplSdl_NewFrame( window , capture , mouseInitial ); SDL_Event event;
mMove_ = ImVec2( );
while ( SDL_PollEvent( &event ) ) {
ImGui_ImplSdl_ProcessEvent( &event );
if ( event.type == SDL_QUIT ) {
#warning FIXME request confirmation
exiting_ = true;
return;
} }
void T_UIApp::warpMouse( if ( mCapture_ && event.type == SDL_MOUSEMOTION ) {
ImVec2 const& pos ) const mMove_.x += event.motion.xrel;
{ mMove_.y += event.motion.yrel;
SDL_WarpMouseInWindow( window , pos.x , pos.y ); }
} }
void T_UIApp::swap( ) const ImGui_ImplSdl_NewFrame( window_ , mCapture_ , mInitial_ );
ImGui::GetIO( ).MouseDrawCursor = true;
handleMouseCapture( );
}
void T_UIApp::handleMouseCapture( ) noexcept
{ {
SDL_GL_SwapWindow( window ); using namespace ImGui;
auto const& io( GetIO( ) );
const T_MouseButtons mb( ([]() {;
T_MouseButtons mb;
if ( IsMouseDown( 0 ) ) {
mb |= E_MouseButton::LEFT;
}
if ( IsMouseDown( 1 ) ) {
mb |= E_MouseButton::RIGHT;
}
if ( IsMouseDown( 2 ) ) {
mb |= E_MouseButton::MIDDLE;
}
return mb;
})() );
const T_KeyboardModifiers kb( ([&io]() {
T_KeyboardModifiers kb;
if ( io.KeyCtrl ) {
kb |= E_KeyboardModifier::CTRL;
}
if ( io.KeyShift ) {
kb |= E_KeyboardModifier::SHIFT;
}
if ( io.KeyAlt ) {
kb |= E_KeyboardModifier::ALT;
}
return kb;
})() );
const bool appCanGrab( !( ImGui::IsMouseHoveringAnyWindow( )
|| io.WantCaptureMouse
|| io.WantCaptureKeyboard ) );
if ( mCapture_ && !( mb && mDelegate_ ) ) {
mCapture_ = false;
CaptureMouseFromApp( false );
SDL_SetRelativeMouseMode( SDL_FALSE );
SDL_WarpMouseInWindow( window_ , mInitial_.x , mInitial_.y );
SetMouseCursor( ImGuiMouseCursor_Arrow );
} else if ( mCapture_ && mDelegate_ ) {
SetMouseCursor( ImGuiMouseCursor_Move );
mDelegate_->handleDragAndDrop( mMove_ , kb , mb );
} else if ( appCanGrab && mb && mDelegate_ ) {
mCapture_ = true;
mInitial_ = GetMousePos( );
CaptureMouseFromApp( true );
SDL_SetRelativeMouseMode( SDL_TRUE );
SetMouseCursor( ImGuiMouseCursor_Move );
}
if ( ( appCanGrab || mCapture_ ) && io.MouseWheel && mDelegate_ ) {
mDelegate_->handleWheel( io.MouseWheel , kb , mb );
}
}
void T_UIApp::render( ) noexcept
{
handleDialogs( );
glUseProgram( 0 );
glBindProgramPipeline( 0 );
UI::Textures( ).reset( );
glClearColor( 0 , 0 , 0 , 1 );
ImGui::Render( );
}
void T_UIApp::swap( ) const noexcept
{
SDL_GL_SwapWindow( window_ );
} }
void T_UIApp::handleDialogs( ) noexcept void T_UIApp::handleDialogs( ) noexcept

View file

@ -51,22 +51,49 @@ struct T_UIApp
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void startFrame( const bool capture , void handleEvents( ) noexcept;
ImVec2 const& mouseInitial ) const;
void warpMouse( ImVec2 const& pos ) const;
void swap( ) const; void setMouseDelegate( A_MouseCtrl* const delegate ) noexcept
void handleDialogs( ) noexcept; { mDelegate_ = delegate; }
void clearMouseDelegate( ) noexcept
{ mDelegate_ = nullptr; }
bool exiting( ) const noexcept
{ return exiting_; }
//----------------------------------------------------------------------
void render( ) noexcept;
void swap( ) const noexcept;
private: private:
SDL_Window * window; // Window and GL context
SDL_GLContext gl; SDL_Window* window_;
SDL_GLContext gl_;
// Fonts
ImFont* defaultFont_; ImFont* defaultFont_;
ImFont* smallFont_; ImFont* smallFont_;
// Do we need to quit?
bool exiting_{ false };
// Mouse capture
bool mCapture_{ false };
ImVec2 mInitial_{ 0 , 0 };
ImVec2 mMove_{ 0 , 0 };
A_MouseCtrl* mDelegate_;
// Stack of modal dialogs
T_AutoArray< P_ModalDialog , 8 > modals_; T_AutoArray< P_ModalDialog , 8 > modals_;
// Actions and keyboard shortcuts
T_ObjectTable< T_String , T_UIAction > actions_{ T_ObjectTable< T_String , T_UIAction > actions_{
[]( T_UIAction const& a ) -> T_String { []( T_UIAction const& a ) -> T_String {
return a.id; return a.id;
} }; } };
void handleMouseCapture( ) noexcept;
void handleDialogs( ) noexcept;
}; };

View file

@ -1,10 +1,10 @@
#include "externals.hh" #include "externals.hh"
#include "demo.hh"
#include "common.hh" #include "common.hh"
#include "c-opcomp.hh" #include "c-opcomp.hh"
#include "c-sync.hh" #include "c-sync.hh"
#include "ui-demo.hh"
#include "ui-opemu.hh" #include "ui-opemu.hh"
#include "ui-rendertarget.hh" #include "ui-rendertarget.hh"

View file

View file

@ -64,6 +64,11 @@ T_UISync::T_UISync( )
addui( "cam" , sov::UICamera ); addui( "cam" , sov::UICamera );
} }
T_UISync::~T_UISync( )
{
UI::Main( ).clearMouseDelegate( );
}
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
namespace { namespace {
@ -170,6 +175,22 @@ void T_UISync::makeOverridesWindow( )
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
void T_UISync::delegateMouse(
T_String const& id ,
P_MouseCtrl delegate ) noexcept
{
mouseDelegateName_ = id;
mouseDelegate_ = std::move( delegate );
UI::Main( ).setMouseDelegate( this );
}
void T_UISync::clearMouseDelegate( ) noexcept
{
mouseDelegateName_ = T_String{ };
mouseDelegate_ = P_MouseCtrl{ };
UI::Main( ).clearMouseDelegate( );
}
void T_UISync::handleDragAndDrop( void T_UISync::handleDragAndDrop(
ImVec2 const& move , ImVec2 const& move ,
T_KeyboardModifiers modifiers , T_KeyboardModifiers modifiers ,

View file

@ -11,6 +11,7 @@ class T_UISync : public A_MouseCtrl
void( A_SyncOverride& , uint32_t& , T_StringBuilder& ) >; void( A_SyncOverride& , uint32_t& , T_StringBuilder& ) >;
T_UISync( ); T_UISync( );
~T_UISync( );
bool& overridesWindowEnabled( ) noexcept bool& overridesWindowEnabled( ) noexcept
{ return ovWindow_; } { return ovWindow_; }
@ -21,17 +22,8 @@ class T_UISync : public A_MouseCtrl
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void delegateMouse( T_String const& id , void delegateMouse( T_String const& id ,
P_MouseCtrl delegate ) noexcept P_MouseCtrl delegate ) noexcept;
{ void clearMouseDelegate( ) noexcept;
mouseDelegateName_ = id;
mouseDelegate_ = std::move( delegate );
}
void clearMouseDelegate( ) noexcept
{
mouseDelegateName_ = T_String{ };
mouseDelegate_ = P_MouseCtrl{ };
}
bool isCurrentDelegate( T_String const& delegate ) noexcept bool isCurrentDelegate( T_String const& delegate ) noexcept
{ return mouseDelegateName_ == delegate; } { return mouseDelegateName_ == delegate; }

3
ui.cc
View file

@ -1,4 +1,5 @@
#include "externals.hh" #include "externals.hh"
#include "common.hh"
#include "ui.hh" #include "ui.hh"
#include "ui-app.hh" #include "ui-app.hh"
#include "ui-odbg.hh" #include "ui-odbg.hh"
@ -28,12 +29,14 @@ std::aligned_storage_t< sizeof( UIData_ ) , alignof( UIData_ ) > Instance_;
void UI::Init( ) noexcept void UI::Init( ) noexcept
{ {
Common::Init( );
new ((char*)&Instance_) UIData_( ); new ((char*)&Instance_) UIData_( );
} }
void UI::Shutdown( ) noexcept void UI::Shutdown( ) noexcept
{ {
((UIData_*)(char*)&Instance_)->~UIData_( ); ((UIData_*)(char*)&Instance_)->~UIData_( );
Common::Shutdown( );
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/