Skip to content

Commit

Permalink
Merge pull request CleverRaven#76404 from CLIDragon/rebalance-ui
Browse files Browse the repository at this point in the history
Remove spacing at start of ImGui uilist and right-align context text.
  • Loading branch information
Maleclypse authored Sep 17, 2024
2 parents 9325eb4 + 8848788 commit c03b36a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/cata_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ cataimgui::client::client( const SDL_Renderer_Ptr &sdl_renderer, const SDL_Windo

// Setup Dear ImGui style
ImGui::StyleColorsDark();

ImGuiStyle &style = ImGui::GetStyle();
// Default cellPadding is {4, 2}. We reduce this to {3, 2}.
ImGui::PushStyleVar( ImGuiStyleVar_CellPadding, ImVec2( 3, style.CellPadding.y ) );

ImGui_ImplSDL2_InitForSDLRenderer( sdl_window.get(), sdl_renderer.get() );
ImGui_ImplSDLRenderer2_Init( sdl_renderer.get() );
}
Expand Down
10 changes: 5 additions & 5 deletions src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,17 +1167,17 @@ bool main_menu::load_character_tab( const std::string &worldname )
for( const save_t &s : savegames ) {
std::optional<std::chrono::seconds> playtime = get_playtime_from_save( cur_world, s );
std::string save_str = s.decoded_name();
std::string playtime_str;
if( playtime ) {
int padding = std::max( 16 - utf8_width( save_str ), 0 ) + 2;
std::chrono::seconds::rep tmp_sec = playtime->count();
int pt_sec = static_cast<int>( tmp_sec % 60 );
int pt_min = static_cast<int>( tmp_sec % 3600 ) / 60;
int pt_hrs = static_cast<int>( tmp_sec / 3600 );
save_str = string_format( "%s%s<color_c_light_blue>[%02d:%02d:%02d]</color>",
save_str, std::string( padding, ' ' ), pt_hrs, pt_min,
static_cast<int>( pt_sec ) );
playtime_str = string_format( "<color_c_light_blue>[%02d:%02d:%02d]</color>",
pt_hrs, pt_min, static_cast<int>( pt_sec ) );
}
mmenu.entries.emplace_back( opt_val++, true, MENU_AUTOASSIGN, save_str );
// TODO: Replace this API to allow adding context without an empty description.
mmenu.entries.emplace_back( opt_val++, true, MENU_AUTOASSIGN, save_str, "", playtime_str );
}
mmenu.entries.emplace_back( opt_val, true, 'q', _( "<- Back to Main Menu" ), c_yellow, c_yellow );
mmenu.query();
Expand Down
5 changes: 3 additions & 2 deletions src/third-party/imgui/imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1490,8 +1490,9 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags, float thickness)
else if (flags & ImGuiSeparatorFlags_Horizontal)
{
// Horizontal Separator
float x1 = window->Pos.x;
float x2 = window->Pos.x + window->Size.x;
// TODO: Remove this change when ImGui is updated to v1.90+
float x1 = window->DC.CursorPos.x;
float x2 = window->WorkRect.Max.x - 1;

// FIXME-WORKRECT: old hack (#205) until we decide of consistent behavior with WorkRect/Indent and Separator
if (g.GroupStack.Size > 0 && g.GroupStack.back().WindowID == window->ID)
Expand Down
28 changes: 22 additions & 6 deletions src/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ class uilist_impl : cataimgui::window
uilist &parent;
public:
explicit uilist_impl( uilist &parent ) : cataimgui::window( "UILIST",
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse ),
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse |
ImGuiWindowFlags_NoNavInputs ),
parent( parent ) {
}

uilist_impl( uilist &parent, const std::string &title ) : cataimgui::window( title,
ImGuiWindowFlags_None | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse ),
ImGuiWindowFlags_None | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse |
ImGuiWindowFlags_NoNavInputs ),
parent( parent ) {
}

Expand All @@ -69,19 +71,27 @@ void uilist_impl::draw_controls()
ImGui::Separator();
}

// An invisible table with three columns. Center column is for the
// menu, left and right are usually invisible. Caller may use
// An invisible table with three columns. Used to create a sidebar effect.
// Ideally we would use a layout engine for this, but ImGui does not natively support any.
// TODO: Investigate using Stack Layout (https://github.com/thedmd/imgui/tree/feature/layout-external)
// Center column is for the menu, left and right are usually invisible. Caller may use
// left/right column to add additional content to the
// window. There should only ever be one row.
if( ImGui::BeginTable( "table", 3,
ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoPadInnerX | ImGuiTableFlags_NoPadOuterX ) ) {
ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoPadInnerX | ImGuiTableFlags_NoPadOuterX |
ImGuiTableFlags_Hideable ) ) {
ImGui::TableSetupColumn( "left", ImGuiTableColumnFlags_WidthFixed, parent.extra_space_left );
ImGui::TableSetupColumn( "menu", ImGuiTableColumnFlags_WidthFixed, parent.calculated_menu_size.x );
ImGui::TableSetupColumn( "right", ImGuiTableColumnFlags_WidthFixed, parent.extra_space_right );

ImGui::TableSetColumnEnabled( 0, parent.extra_space_left < 1.0f ? false : true );
ImGui::TableSetColumnEnabled( 2, parent.extra_space_right < 1.0f ? false : true );

ImGui::TableNextRow();
ImGui::TableSetColumnIndex( 1 );

float entry_height = ImGui::GetTextLineHeightWithSpacing();
ImGuiStyle &style = ImGui::GetStyle();
if( ImGui::BeginChild( "scroll", parent.calculated_menu_size, false ) ) {
if( ImGui::BeginTable( "menu items", 3, ImGuiTableFlags_SizingFixedFit ) ) {
ImGui::TableSetupColumn( "hotkey", ImGuiTableColumnFlags_WidthFixed,
Expand Down Expand Up @@ -131,7 +141,9 @@ void uilist_impl::draw_controls()
// this row is hovered and the hover state just changed, show context for it
parent.hovered = parent.fentries[ i ];
}
ImGui::SameLine( 0, 0 );

// Force the spacing to be set to the padding value.
ImGui::SameLine( 0, style.CellPadding.x );
if( entry.hotkey.has_value() ) {
cataimgui::draw_colored_text( entry.hotkey.value().short_description(),
is_selected ? parent.hilight_color : parent.hotkey_color );
Expand All @@ -146,6 +158,10 @@ void uilist_impl::draw_controls()
cataimgui::draw_colored_text( entry.txt, color );

ImGui::TableSetColumnIndex( 2 );
// Right-align text.
ImVec2 curPos = ImGui::GetCursorScreenPos();
// Remove the edge padding so that the last pixel just touches the border.
ImGui::SetCursorScreenPos( ImVec2( ImMax( 0.0f, curPos.x + style.CellPadding.x ), curPos.y ) );
cataimgui::draw_colored_text( entry.ctxt, color );

ImGui::PopID();
Expand Down

0 comments on commit c03b36a

Please sign in to comment.