From c76380f5698c9c3e6ad3ca6dc2acd7bc15614852 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 6 Feb 2026 11:27:57 -0500 Subject: [PATCH] Fix UI: maneuver status updates, ZII initialization, helper functions for cache checks --- AGENTS.md | 1 + src/ui_renderer.cpp | 48 ++++++++++++++++++++++++++------------------- src/ui_renderer.h | 1 + 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 7a65bd0..40cb141 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -20,6 +20,7 @@ - No trailing whitespace in any files (including markdown) - Pre-commit hook automatically strips it - For markdown line breaks, use
tag instead of two trailing spaces +- ZII (Zero Is Initialization) pattern: Initialize structs using `= {0}` or `= {NULL}` instead of individual field assignments. This guarantees all fields (including padding) are zeroed out. Example: `MyStruct s = {0};` ## File Reading Policy - Ask before reading files unless immediately necessary for current task diff --git a/src/ui_renderer.cpp b/src/ui_renderer.cpp index 3374515..e392abf 100644 --- a/src/ui_renderer.cpp +++ b/src/ui_renderer.cpp @@ -21,6 +21,25 @@ #define MANEUVER_LIST_HEIGHT 450 #define MANEUVER_LIST_Y_OFFSET 320 +static inline bool needs_body_list_rebuild(UIState* ui_state, SimulationState* sim) { + return (ui_state->cached_body_count != sim->body_count || + ui_state->cached_craft_count != sim->craft_count || + ui_state->body_list_buffer == NULL); +} + +static inline bool needs_maneuver_list_rebuild(UIState* ui_state, SimulationState* sim, int current_executed_count) { + return (ui_state->cached_maneuver_count != sim->maneuver_count || + ui_state->cached_executed_count != current_executed_count || + ui_state->maneuver_list_buffer == NULL); +} + +static inline bool needs_hohmann_rebuild(UIState* ui_state, SimulationState* sim) { + return (ui_state->cached_body_count != sim->body_count || + ui_state->cached_craft_count != sim->craft_count || + ui_state->craft_list_buffer == NULL || + ui_state->body_dropdown_buffer == NULL); +} + void gui_init() { GuiLoadStyleAmber(); } @@ -96,9 +115,7 @@ void render_body_list_ui(SimulationState* sim, RenderState* render_state, UIStat int total_items = sim->body_count + sim->craft_count; // Check if buffer needs rebuilding - bool needs_rebuild = (ui_state->cached_body_count != sim->body_count || - ui_state->cached_craft_count != sim->craft_count || - ui_state->body_list_buffer == NULL); + bool needs_rebuild = needs_body_list_rebuild(ui_state, sim); if (needs_rebuild) { int buffer_size = total_items * (64 + 3 + 1) + 1; @@ -362,9 +379,13 @@ void render_maneuver_list_ui(SimulationState* sim, RenderState* render_state, UI GuiWindowBox(panel_bounds, title); int craft_maneuver_count = 0; + int current_executed_count = 0; for (int i = 0; i < sim->maneuver_count; i++) { if (sim->maneuvers[i].craft_index == ui_state->selected_craft_index) { craft_maneuver_count++; + if (sim->maneuvers[i].executed) { + current_executed_count++; + } } } @@ -407,8 +428,7 @@ void render_maneuver_list_ui(SimulationState* sim, RenderState* render_state, UI } // Check if buffer needs rebuilding - bool needs_rebuild = (ui_state->cached_maneuver_count != sim->maneuver_count || - ui_state->maneuver_list_buffer == NULL); + bool needs_rebuild = needs_maneuver_list_rebuild(ui_state, sim, current_executed_count); if (needs_rebuild) { int buffer_size = craft_maneuver_count * (64 + 10 + 1) + 1; @@ -437,6 +457,7 @@ void render_maneuver_list_ui(SimulationState* sim, RenderState* render_state, UI } ui_state->cached_maneuver_count = sim->maneuver_count; + ui_state->cached_executed_count = current_executed_count; } Rectangle list_bounds = { @@ -748,10 +769,7 @@ void render_maneuver_hohmann_tab(SimulationState* sim, UIState* ui_state, int x, } } - bool needs_rebuild = (ui_state->cached_body_count != sim->body_count || - ui_state->cached_craft_count != sim->craft_count || - ui_state->craft_list_buffer == NULL || - ui_state->body_dropdown_buffer == NULL); + bool needs_rebuild = needs_hohmann_rebuild(ui_state, sim); if (needs_rebuild) { int craft_buffer_size = sim->craft_count * 64 + 1; @@ -1440,17 +1458,7 @@ void delete_maneuver_from_dialog(SimulationState* sim, UIState* ui_state) { } void ui_state_init_buffers(UIState* ui_state) { - ui_state->cached_body_count = 0; - ui_state->cached_craft_count = 0; - ui_state->cached_maneuver_count = 0; - ui_state->body_list_buffer_size = 0; - ui_state->body_list_buffer = NULL; - ui_state->craft_list_buffer_size = 0; - ui_state->craft_list_buffer = NULL; - ui_state->body_dropdown_buffer_size = 0; - ui_state->body_dropdown_buffer = NULL; - ui_state->maneuver_list_buffer_size = 0; - ui_state->maneuver_list_buffer = NULL; + *ui_state = (UIState){0}; } void ui_state_free_buffers(UIState* ui_state) { diff --git a/src/ui_renderer.h b/src/ui_renderer.h index 737667b..5ac570f 100644 --- a/src/ui_renderer.h +++ b/src/ui_renderer.h @@ -54,6 +54,7 @@ struct UIState { int cached_body_count; int cached_craft_count; int cached_maneuver_count; + int cached_executed_count; int body_list_buffer_size; char* body_list_buffer; int craft_list_buffer_size;