-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract imgui demo class into a file
- Loading branch information
Showing
4 changed files
with
314 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#include "debug_imgui_demo.h" | ||
|
||
#include <imgui/imgui.h> | ||
|
||
#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. </color> <color_green><color_red>Some red text in the </color>middle.</color> Some long text that will <color_light_blue_yellow>wrap around nicely."; | ||
std::string text = | ||
"Some long text that will wrap around nicely. <color_red>Some red text in the middle.</color> Some long text that will wrap around nicely."; | ||
stuff = std::make_shared<cataimgui::Paragraph>(); | ||
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<cataimgui::Paragraph> &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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<cataimgui::Paragraph> stuff; | ||
}; | ||
|
||
#endif // CATA_SRC_DEBUG_IMGUI_DEMO_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.