From 7be2a0378295692763195aaf10d437fd0bb544a9 Mon Sep 17 00:00:00 2001 From: moxian Date: Thu, 26 Dec 2024 02:36:41 -0800 Subject: [PATCH] Extract imgui demo class into a file --- src/debug_imgui_demo.cpp | 143 +++++++++++++++++++ src/debug_imgui_demo.h | 24 ++++ src/debug_menu.cpp | 55 +------- src/main_menu.cpp | 289 ++++++++++++++++++++------------------- 4 files changed, 314 insertions(+), 197 deletions(-) create mode 100644 src/debug_imgui_demo.cpp create mode 100644 src/debug_imgui_demo.h diff --git a/src/debug_imgui_demo.cpp b/src/debug_imgui_demo.cpp new file mode 100644 index 0000000000000..3285eddb2e4d1 --- /dev/null +++ b/src/debug_imgui_demo.cpp @@ -0,0 +1,143 @@ +#include "debug_imgui_demo.h" + +#include + +#include "cata_imgui.h" +#include "ui_manager.h" +#include "input_context.h" +#include "translations.h" + + +debug_imgui_demo_ui::debug_imgui_demo_ui() : + cataimgui::window( _( "ImGui Demo Screen" ) ) +{ + + // char *text = "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely."; + std::string text = + "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely."; + stuff = std::make_shared(); + stuff->append_colored_text( text, c_white ); + +} +cataimgui::bounds debug_imgui_demo_ui::get_bounds() +{ + // return { 0.0f, 0.0f, 0.0f, 0.0f }; + return { -1.f, -1.f, ImGui::GetMainViewport()->Size.x, ImGui::GetMainViewport()->Size.y }; +} + +static void draw_lorem( const std::shared_ptr &stuff ) +{ + +#ifdef TUI + ImGui::SetNextWindowPos( { 0, 0 }, ImGuiCond_Once ); + ImGui::SetNextWindowSize( { 60, 40 }, ImGuiCond_Once ); +#else + ImGui::SetNextWindowSize( { 620, 900 }, ImGuiCond_Once ); +#endif + if( ImGui::Begin( "test" ) ) { +#ifdef TUI + static float wrap_width = 50.0f; + ImGui::SliderFloat( "Wrap width", &wrap_width, 1, 60, "%.0f" ); + float marker_size = 0.0f; +#else + static float wrap_width = 200.0f; + ImGui::SliderFloat( "Wrap width", &wrap_width, -20, 600, "%.0f" ); + float marker_size = ImGui::GetTextLineHeight(); +#endif + +#define WRAP_START() \ + ImDrawList *draw_list = ImGui::GetWindowDrawList(); \ + ImVec2 pos = ImGui::GetCursorScreenPos(); \ + ImVec2 marker_min = ImVec2(pos.x + wrap_width, pos.y); \ + ImVec2 marker_max = \ + ImVec2(pos.x + wrap_width + marker_size, pos.y + marker_size); \ + ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); + +#define WRAP_END() \ + draw_list->AddRectFilled(marker_min, marker_max, \ + IM_COL32(255, 0, 255, 255)); \ + ImGui::PopTextWrapPos(); + + // three sentences in a nicely–wrapped paragraph + if( ImGui::CollapsingHeader( "Plain wrapped text" ) ) { + WRAP_START(); + ImGui::TextWrapped( "%s", + "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely." ); + WRAP_END(); + ImGui::NewLine(); + } + + if( ImGui::CollapsingHeader( "Styled Paragraph" ) ) { + WRAP_START(); + cataimgui::TextStyled( stuff, wrap_width ); + WRAP_END(); + ImGui::NewLine(); + } + + if( ImGui::CollapsingHeader( "Unstyled Paragraph" ) ) { + WRAP_START(); + cataimgui::TextUnstyled( stuff, wrap_width ); + WRAP_END(); + ImGui::NewLine(); + } + + if( ImGui::CollapsingHeader( "Styled Paragraph, no allocations" ) ) { + WRAP_START(); + cataimgui::TextParagraph( c_white, "Some long text that will wrap around nicely.", wrap_width ); + cataimgui::TextParagraph( c_red, " Some red text in the middle.", wrap_width ); + cataimgui::TextParagraph( c_white, " Some long text that will wrap around nicely.", wrap_width ); + ImGui::NewLine(); + WRAP_END(); + ImGui::NewLine(); + } + + if( ImGui::CollapsingHeader( "Naive attempt" ) ) { + WRAP_START(); + // same three sentences, but the color breaks the wrapping + ImGui::TextUnformatted( "Some long text that will wrap around nicely." ); + ImGui::SameLine(); + ImGui::TextColored( c_red, "%s", "Some red text in the middle." ); + ImGui::SameLine(); + ImGui::TextUnformatted( "Some long text that will wrap around nicely." ); + WRAP_END(); + } + } + ImGui::End(); +} + +void debug_imgui_demo_ui::draw_controls() +{ +#ifndef TUI + ImGui::ShowDemoWindow(); +#endif + draw_lorem( stuff ); +} + +void debug_imgui_demo_ui::init() +{ + // The demo makes it's own screen. Don't get in the way + force_to_back = true; +} + +void debug_imgui_demo_ui::run() +{ + init(); + + input_context ctxt( "HELP_KEYBINDINGS" ); + ctxt.register_action( "QUIT" ); + ctxt.register_action( "SELECT" ); + ctxt.register_action( "MOUSE_MOVE" ); + ctxt.register_action( "ANY_INPUT" ); + ctxt.register_action( "HELP_KEYBINDINGS" ); + std::string action; + + ui_manager::redraw(); + + while( is_open ) { + ui_manager::redraw(); + action = ctxt.handle_input( 5 ); + if( action == "QUIT" ) { + break; + } + } +} diff --git a/src/debug_imgui_demo.h b/src/debug_imgui_demo.h new file mode 100644 index 0000000000000..60a6841e70576 --- /dev/null +++ b/src/debug_imgui_demo.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef CATA_SRC_DEBUG_IMGUI_DEMO_H +#define CATA_SRC_DEBUG_IMGUI_DEMO_H + +#include "cata_imgui.h" + +class debug_imgui_demo_ui : public cataimgui::window +{ + public: + debug_imgui_demo_ui(); + void init(); + void run(); + + protected: + void draw_controls() override; + cataimgui::bounds get_bounds() override; + void on_resized() override { + init(); + }; + private: + std::shared_ptr stuff; +}; + +#endif // CATA_SRC_DEBUG_IMGUI_DEMO_H diff --git a/src/debug_menu.cpp b/src/debug_menu.cpp index b6e400ed09706..f239ea444d970 100644 --- a/src/debug_menu.cpp +++ b/src/debug_menu.cpp @@ -48,6 +48,7 @@ #include "creature_tracker.h" #include "cursesdef.h" #include "debug.h" +#include "debug_imgui_demo.h" #include "dialogue.h" #include "dialogue_chatbin.h" #include "dialogue_helpers.h" @@ -3777,63 +3778,11 @@ static void wind_speed() } } -class demo_ui : public cataimgui::window -{ - public: - demo_ui(); - void init(); - void run(); - - protected: - void draw_controls() override; - cataimgui::bounds get_bounds() override; - void on_resized() override { - init(); - }; -}; -demo_ui::demo_ui() : cataimgui::window( _( "ImGui Demo Screen" ) ) {} -cataimgui::bounds demo_ui::get_bounds() -{ - return { 0.0f, 0.0f, 0.0f, 0.0f }; -} -void demo_ui::draw_controls() -{ -#ifndef TUI - ImGui::ShowDemoWindow(); -#endif -} -void demo_ui::init() -{ - // The demo makes it's own screen. Don't get in the way - force_to_back = true; -} - -void demo_ui::run() -{ - init(); - - input_context ctxt( "HELP_KEYBINDINGS" ); - ctxt.register_action( "QUIT" ); - ctxt.register_action( "SELECT" ); - ctxt.register_action( "MOUSE_MOVE" ); - ctxt.register_action( "ANY_INPUT" ); - ctxt.register_action( "HELP_KEYBINDINGS" ); - std::string action; - - ui_manager::redraw(); - while( is_open ) { - ui_manager::redraw(); - action = ctxt.handle_input( 5 ); - if( action == "QUIT" ) { - break; - } - } -} static void run_imgui_demo() { - demo_ui demo; + debug_imgui_demo_ui demo; demo.run(); } diff --git a/src/main_menu.cpp b/src/main_menu.cpp index c079293739e59..ae6b04dd505b6 100644 --- a/src/main_menu.cpp +++ b/src/main_menu.cpp @@ -26,6 +26,7 @@ #include "character_id.h" #include "color.h" #include "debug.h" +#include "debug_imgui_demo.h" #include "enums.h" #include "filesystem.h" #include "game.h" @@ -55,149 +56,149 @@ #include "imgui/imgui.h" -class demo_ui : public cataimgui::window -{ - public: - demo_ui(); - void init(); - void run(); - - protected: - void draw_controls() override; - cataimgui::bounds get_bounds() override; - void on_resized() override { - init(); - }; - - private: - std::shared_ptr stuff; -}; - -demo_ui::demo_ui() : cataimgui::window( _( "ImGui Demo Screen" ) ) -{ - // char *text = "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely."; - std::string text = - "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely."; - stuff = std::make_shared(); - stuff->append_colored_text( text, c_white ); -} - -cataimgui::bounds demo_ui::get_bounds() -{ - return { -1.f, -1.f, float( str_width_to_pixels( TERMX ) ), float( str_height_to_pixels( TERMY ) ) }; -} - -void demo_ui::draw_controls() -{ -#ifndef TUI - ImGui::ShowDemoWindow(); -#endif - -#ifdef TUI - ImGui::SetNextWindowPos( { 0, 0 }, ImGuiCond_Once ); - ImGui::SetNextWindowSize( { 60, 40 }, ImGuiCond_Once ); -#else - ImGui::SetNextWindowSize( { 620, 900 }, ImGuiCond_Once ); -#endif - if( ImGui::Begin( "test" ) ) { -#ifdef TUI - static float wrap_width = 50.0f; - ImGui::SliderFloat( "Wrap width", &wrap_width, 1, 60, "%.0f" ); - float marker_size = 0.0f; -#else - static float wrap_width = 200.0f; - ImGui::SliderFloat( "Wrap width", &wrap_width, -20, 600, "%.0f" ); - float marker_size = ImGui::GetTextLineHeight(); -#endif - -#define WRAP_START() \ - ImDrawList *draw_list = ImGui::GetWindowDrawList(); \ - ImVec2 pos = ImGui::GetCursorScreenPos(); \ - ImVec2 marker_min = ImVec2(pos.x + wrap_width, pos.y); \ - ImVec2 marker_max = \ - ImVec2(pos.x + wrap_width + marker_size, pos.y + marker_size); \ - ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); - -#define WRAP_END() \ - draw_list->AddRectFilled(marker_min, marker_max, \ - IM_COL32(255, 0, 255, 255)); \ - ImGui::PopTextWrapPos(); - - // three sentences in a nicely–wrapped paragraph - if( ImGui::CollapsingHeader( "Plain wrapped text" ) ) { - WRAP_START(); - ImGui::TextWrapped( "%s", - "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely." ); - WRAP_END(); - ImGui::NewLine(); - } - - if( ImGui::CollapsingHeader( "Styled Paragraph" ) ) { - WRAP_START(); - cataimgui::TextStyled( stuff, wrap_width ); - WRAP_END(); - ImGui::NewLine(); - } - - if( ImGui::CollapsingHeader( "Unstyled Paragraph" ) ) { - WRAP_START(); - cataimgui::TextUnstyled( stuff, wrap_width ); - WRAP_END(); - ImGui::NewLine(); - } - - if( ImGui::CollapsingHeader( "Styled Paragraph, no allocations" ) ) { - WRAP_START(); - cataimgui::TextParagraph( c_white, "Some long text that will wrap around nicely.", wrap_width ); - cataimgui::TextParagraph( c_red, " Some red text in the middle.", wrap_width ); - cataimgui::TextParagraph( c_white, " Some long text that will wrap around nicely.", wrap_width ); - ImGui::NewLine(); - WRAP_END(); - ImGui::NewLine(); - } - - if( ImGui::CollapsingHeader( "Naive attempt" ) ) { - WRAP_START(); - // same three sentences, but the color breaks the wrapping - ImGui::TextUnformatted( "Some long text that will wrap around nicely." ); - ImGui::SameLine(); - ImGui::TextColored( c_red, "%s", "Some red text in the middle." ); - ImGui::SameLine(); - ImGui::TextUnformatted( "Some long text that will wrap around nicely." ); - WRAP_END(); - } - } - ImGui::End(); -} - -void demo_ui::init() -{ - // The demo makes it's own screen. Don't get in the way - force_to_back = true; -} - -void demo_ui::run() -{ - init(); - - input_context ctxt( "HELP_KEYBINDINGS" ); - ctxt.register_action( "QUIT" ); - ctxt.register_action( "SELECT" ); - ctxt.register_action( "MOUSE_MOVE" ); - ctxt.register_action( "ANY_INPUT" ); - ctxt.register_action( "HELP_KEYBINDINGS" ); - std::string action; - - ui_manager::redraw(); - - while( is_open ) { - ui_manager::redraw(); - action = ctxt.handle_input( 5 ); - if( action == "QUIT" ) { - break; - } - } -} +// class demo_ui : public cataimgui::window +// { +// public: +// demo_ui(); +// void init(); +// void run(); + +// protected: +// void draw_controls() override; +// cataimgui::bounds get_bounds() override; +// void on_resized() override { +// init(); +// }; + +// private: +// std::shared_ptr stuff; +// }; + +// demo_ui::demo_ui() : cataimgui::window( _( "ImGui Demo Screen" ) ) +// { +// // char *text = "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely."; +// std::string text = +// "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely."; +// stuff = std::make_shared(); +// stuff->append_colored_text( text, c_white ); +// } + +// cataimgui::bounds demo_ui::get_bounds() +// { +// return { -1.f, -1.f, float( str_width_to_pixels( TERMX ) ), float( str_height_to_pixels( TERMY ) ) }; +// } + +// void demo_ui::draw_controls() +// { +// #ifndef TUI +// ImGui::ShowDemoWindow(); +// #endif + +// #ifdef TUI +// ImGui::SetNextWindowPos( { 0, 0 }, ImGuiCond_Once ); +// ImGui::SetNextWindowSize( { 60, 40 }, ImGuiCond_Once ); +// #else +// ImGui::SetNextWindowSize( { 620, 900 }, ImGuiCond_Once ); +// #endif +// if( ImGui::Begin( "test" ) ) { +// #ifdef TUI +// static float wrap_width = 50.0f; +// ImGui::SliderFloat( "Wrap width", &wrap_width, 1, 60, "%.0f" ); +// float marker_size = 0.0f; +// #else +// static float wrap_width = 200.0f; +// ImGui::SliderFloat( "Wrap width", &wrap_width, -20, 600, "%.0f" ); +// float marker_size = ImGui::GetTextLineHeight(); +// #endif + +// #define WRAP_START() \ +// ImDrawList *draw_list = ImGui::GetWindowDrawList(); \ +// ImVec2 pos = ImGui::GetCursorScreenPos(); \ +// ImVec2 marker_min = ImVec2(pos.x + wrap_width, pos.y); \ +// ImVec2 marker_max = \ +// ImVec2(pos.x + wrap_width + marker_size, pos.y + marker_size); \ +// ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); + +// #define WRAP_END() \ +// draw_list->AddRectFilled(marker_min, marker_max, \ +// IM_COL32(255, 0, 255, 255)); \ +// ImGui::PopTextWrapPos(); + +// // three sentences in a nicely–wrapped paragraph +// if( ImGui::CollapsingHeader( "Plain wrapped text" ) ) { +// WRAP_START(); +// ImGui::TextWrapped( "%s", +// "Some long text that will wrap around nicely. Some red text in the middle. Some long text that will wrap around nicely." ); +// WRAP_END(); +// ImGui::NewLine(); +// } + +// if( ImGui::CollapsingHeader( "Styled Paragraph" ) ) { +// WRAP_START(); +// cataimgui::TextStyled( stuff, wrap_width ); +// WRAP_END(); +// ImGui::NewLine(); +// } + +// if( ImGui::CollapsingHeader( "Unstyled Paragraph" ) ) { +// WRAP_START(); +// cataimgui::TextUnstyled( stuff, wrap_width ); +// WRAP_END(); +// ImGui::NewLine(); +// } + +// if( ImGui::CollapsingHeader( "Styled Paragraph, no allocations" ) ) { +// WRAP_START(); +// cataimgui::TextParagraph( c_white, "Some long text that will wrap around nicely.", wrap_width ); +// cataimgui::TextParagraph( c_red, " Some red text in the middle.", wrap_width ); +// cataimgui::TextParagraph( c_white, " Some long text that will wrap around nicely.", wrap_width ); +// ImGui::NewLine(); +// WRAP_END(); +// ImGui::NewLine(); +// } + +// if( ImGui::CollapsingHeader( "Naive attempt" ) ) { +// WRAP_START(); +// // same three sentences, but the color breaks the wrapping +// ImGui::TextUnformatted( "Some long text that will wrap around nicely." ); +// ImGui::SameLine(); +// ImGui::TextColored( c_red, "%s", "Some red text in the middle." ); +// ImGui::SameLine(); +// ImGui::TextUnformatted( "Some long text that will wrap around nicely." ); +// WRAP_END(); +// } +// } +// ImGui::End(); +// } + +// void demo_ui::init() +// { +// // The demo makes it's own screen. Don't get in the way +// force_to_back = true; +// } + +// void demo_ui::run() +// { +// init(); + +// input_context ctxt( "HELP_KEYBINDINGS" ); +// ctxt.register_action( "QUIT" ); +// ctxt.register_action( "SELECT" ); +// ctxt.register_action( "MOUSE_MOVE" ); +// ctxt.register_action( "ANY_INPUT" ); +// ctxt.register_action( "HELP_KEYBINDINGS" ); +// std::string action; + +// ui_manager::redraw(); + +// while( is_open ) { +// ui_manager::redraw(); +// action = ctxt.handle_input( 5 ); +// if( action == "QUIT" ) { +// break; +// } +// } +// } static const mod_id MOD_INFORMATION_dda( "dda" ); static const mod_id MOD_INFORMATION_dda_tutorial( "dda_tutorial" ); @@ -1005,7 +1006,7 @@ bool main_menu::opening_screen() } else if( sel2 == 4 ) { /// Colors all_colors.show_gui(); } else if( sel2 == 5 ) { /// ImGui demo - demo_ui demo; + debug_imgui_demo_ui demo; demo.run(); } break;