#include "externals.hh" #include "window.hh" #include "imgui_impl_sdl.h" #include /*= A_ModalDialog ============================================================*/ A_ModalDialog::A_ModalDialog( char const* const id ) noexcept : id_{ id , uint32_t( strlen( id ) + 1 ) } {} A_ModalDialog::~A_ModalDialog( ) { } void A_ModalDialog::initDialog( ) noexcept {} void A_ModalDialog::onCancel( ) noexcept { } bool A_ModalDialog::draw( ) noexcept { using namespace ImGui; if ( !open_ ) { OpenPopup( id_.data( ) ); open_ = true; } initDialog( ); if ( initialSize_ ) { SetNextWindowSize( *initialSize_ , ImGuiCond_Appearing ); } bool ok{ false }; bool keep{ BeginPopupModal( id_.data( ) , nullptr , ImGuiWindowFlags_NoResize ) }; if ( keep ) { const bool canUseOK{ drawDialog( ) }; const ImVec2 ws( GetWindowContentRegionMax( ) ); auto const& style( GetStyle( ) ); const float innerWidth{ ws.x - 2 * style.FramePadding.x }; constexpr float nButtons{ 2.f }; const ImVec2 buttonSize{ std::max( 40.f , ( innerWidth - nButtons * style.FramePadding.x ) / nButtons ) , 0.f }; if ( !canUseOK ) { PushItemFlag( ImGuiItemFlags_Disabled , true ); PushStyleVar( ImGuiStyleVar_Alpha , GetStyle( ).Alpha * .5f ); } if ( Button( "OK" , buttonSize ) ) { ok = true; keep = false; } if ( !canUseOK ) { PopItemFlag( ); PopStyleVar( ); } SameLine( 0 ); keep = !Button( "Cancel" , buttonSize ) && keep; if ( !keep ) { CloseCurrentPopup( ); } EndPopup( ); } if ( !keep ) { if ( ok ) { onOK( ); } else { onCancel( ); } } return keep; } /*= T_Window =================================================================*/ namespace { #include "font-awesome.inl" static const ImWchar IconsRanges_[] = { ICON_MIN_FA , ICON_MAX_FA , 0 }; } // namespace T_Window::T_Window( ) { SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER , 1 ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE , 24 ); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE , 8 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION , 2 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION , 2 ); SDL_DisplayMode current; SDL_GetCurrentDisplayMode( 0 , ¤t ); window = SDL_CreateWindow( "DEMO", SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED , 1280 , 720 , SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE ); gl = SDL_GL_CreateContext( window ); glewInit( ); if ( !GLEW_VERSION_4_5 ) { fprintf( stderr , "OpenGL 4.5 required\n" ); exit( 1 ); } ImGui_ImplSdl_Init( window ); using namespace ImGui; StyleColorsDark( ); ImGuiIO& io{ GetIO( ) }; io.IniFilename = nullptr; { ImFontConfig cfg; cfg.SizePixels = 13.0f; defaultFont_ = io.Fonts->AddFontDefault( &cfg ); ImFontConfig icons; icons.MergeMode = true; icons.PixelSnapH = true; io.Fonts->AddFontFromMemoryCompressedBase85TTF( FontAwesome__compressed_data_base85 , 13.f , &icons , IconsRanges_ ); } { ImFontConfig cfg; cfg.SizePixels = 9.0f; smallFont_ = io.Fonts->AddFontDefault( &cfg ); ImFontConfig icons; icons.MergeMode = true; icons.PixelSnapH = true; icons.DstFont = smallFont_; io.Fonts->AddFontFromMemoryCompressedBase85TTF( FontAwesome__compressed_data_base85 , 9.f , &icons , IconsRanges_ ); } } T_Window::~T_Window( ) { ImGui_ImplSdl_Shutdown( ); SDL_GL_DeleteContext( gl ); SDL_DestroyWindow( window ); SDL_Quit( ); } void T_Window::startFrame( const bool capture , ImVec2 const& mouseInitial ) const { ImGui_ImplSdl_NewFrame( window , capture , mouseInitial ); } void T_Window::warpMouse( ImVec2 const& pos ) const { SDL_WarpMouseInWindow( window , pos.x , pos.y ); } void T_Window::swap( ) const { SDL_GL_SwapWindow( window ); } void T_Window::handleDialogs( ) noexcept { const auto nDialogs{ modals_.size( ) }; for ( auto i = 0u ; i < nDialogs ; i ++ ) { const bool keep{ modals_[ i ]->draw( ) }; assert( keep || i + 1 == nDialogs ); if ( !keep ) { modals_.removeLast( ); } } }