From 85bd9eed80925386065e816d92a09db4f926c53f Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Sat, 17 Jan 2026 18:44:33 -0500 Subject: [PATCH] Fix raygui body selection - GuiListView return value bug Issue: GuiListView() always returns 0 (known raygui bug), causing body selection to never work. Fix: - Added body_list_scroll and body_list_active to RenderState for persistence - Check body_list_active parameter changes instead of return value - Initialize active state to -1 in main.cpp Reference: https://github.com/raysan5/raygui/issues/448 --- src/main.cpp | 2 ++ src/renderer.cpp | 13 ++++++------- src/renderer.h | 2 ++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6de4694..b464f6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,6 +40,8 @@ void run_gui_simulation(SimulationState* sim) { render_state.selected_body_index = -1; // No selection initially render_state.show_body_list = false; render_state.show_body_info = false; + render_state.body_list_scroll = 0; // Initial scroll position + render_state.body_list_active = -1; // No active item initially bool paused = false; double speed_multiplier = 1.0; diff --git a/src/renderer.cpp b/src/renderer.cpp index 8e00388..ba8d138 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -402,14 +402,13 @@ void render_body_list_ui(SimulationState* sim, RenderState* render_state) { (float)panel_height - 29 }; - int focus = render_state->selected_body_index; // 0-based indexing - if (focus < 0) focus = 0; + int previous_active = render_state->body_list_active; + GuiListView(list_bounds, body_list_text, &render_state->body_list_scroll, &render_state->body_list_active); - int active = 0; // Raygui uses this internally - int result = GuiListView(list_bounds, body_list_text, &focus, &active); - - if (result >= 0 && result < sim->body_count) { - render_state->selected_body_index = result; + // Check if a body was selected (active changed from -1 or previous value) + if (render_state->body_list_active >= 0 && render_state->body_list_active < sim->body_count && + render_state->body_list_active != previous_active) { + render_state->selected_body_index = render_state->body_list_active; render_state->show_body_info = true; } diff --git a/src/renderer.h b/src/renderer.h index a986937..5453cae 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -13,6 +13,8 @@ struct RenderState { int selected_body_index; // -1 = no selection bool show_body_list; // Toggle for list panel bool show_body_info; // Toggle for info panel + int body_list_scroll; // Scroll position for body list + int body_list_active; // Active item index in body list }; // Renderer initialization and cleanup