From 66847120b14d9dbab6cf5c05f297b52c3788965e Mon Sep 17 00:00:00 2001 From: Sean Boettger Date: Tue, 13 May 2025 04:38:30 +1000 Subject: [PATCH 1/3] Revert "Add id/label to all text_box usage" This reverts commit 4538f4fa06576f55dd233d764156af6be5728a23. --- coresdk/src/backend/interface_driver.cpp | 6 ++--- coresdk/src/backend/interface_driver.h | 2 +- coresdk/src/coresdk/interface.cpp | 33 ++++++++++-------------- coresdk/src/coresdk/interface.h | 29 +++++++-------------- coresdk/src/test/test_ui.cpp | 6 ++--- projects/cmake/CMakeLists.txt | 2 -- 6 files changed, 31 insertions(+), 47 deletions(-) diff --git a/coresdk/src/backend/interface_driver.cpp b/coresdk/src/backend/interface_driver.cpp index 019d666c..07cb4831 100644 --- a/coresdk/src/backend/interface_driver.cpp +++ b/coresdk/src/backend/interface_driver.cpp @@ -636,10 +636,10 @@ namespace splashkit_lib return temp_value; } - std::string sk_interface_text_box(const std::string& id, const std::string& value) + std::string sk_interface_text_box(const std::string& value) { - // const std::string* id = &value; - mu_Id m_id = mu_get_id(ctx, id.c_str(), id.length()); + const std::string* id = &value; + mu_Id m_id = mu_get_id(ctx, &id, sizeof(id)); mu_Rect r = mu_layout_next(ctx); // max 512 characters diff --git a/coresdk/src/backend/interface_driver.h b/coresdk/src/backend/interface_driver.h index c5467bcb..a775d894 100644 --- a/coresdk/src/backend/interface_driver.h +++ b/coresdk/src/backend/interface_driver.h @@ -61,7 +61,7 @@ namespace splashkit_lib bool sk_interface_checkbox(const string& label_text, const bool& value); float sk_interface_slider(const float& value, float min_value, float max_value); float sk_interface_number(const float& value, float step); - std::string sk_interface_text_box(const std::string& id, const std::string& value); + std::string sk_interface_text_box(const std::string& value); void sk_interface_color_box(color clr); diff --git a/coresdk/src/coresdk/interface.cpp b/coresdk/src/coresdk/interface.cpp index a9d302e9..e1c08a7d 100644 --- a/coresdk/src/coresdk/interface.cpp +++ b/coresdk/src/coresdk/interface.cpp @@ -1133,38 +1133,33 @@ namespace splashkit_lib } std::string text_box(const string& label_text, const std::string& value) - { - return text_box(label_text, value, false); - } - - std::string text_box(const string& label_text, const std::string& value, bool show_label) { _interface_sanity_check(); - if (show_label) - { - enter_column(); - _two_column_layout(); - - splashkit_lib::label_element(label_text); - } + enter_column(); + _two_column_layout(); - std::string res = sk_interface_text_box(label_text, value); + splashkit_lib::label_element(label_text); + std::string res = text_box(value); - if (show_label) - { - leave_column(); - } + leave_column(); return res; } - std::string text_box(const string& label_text, const string& value, const rectangle& rect) + std::string text_box(const string& value, const rectangle& rect) { _interface_sanity_check(); sk_interface_set_layout_next(rect, true); - return text_box(label_text, value, false); + return text_box(value); + } + + std::string text_box(const std::string& value) + { + _interface_sanity_check(); + + return sk_interface_text_box(value); } bool last_element_changed() diff --git a/coresdk/src/coresdk/interface.h b/coresdk/src/coresdk/interface.h index 7683e5fc..50f65f61 100644 --- a/coresdk/src/coresdk/interface.h +++ b/coresdk/src/coresdk/interface.h @@ -645,47 +645,38 @@ namespace splashkit_lib * my_string = text_box("Name", my_string); * ``` * - * @param label_text Unique identifier for the text box (not drawn) + * @param label_text The label to show in front of the text box * @param value The current value of the text box * @return The updated value of the text box + * + * @attribute suffix labeled */ string text_box(const string& label_text, const string& value); /** - * Creates a text entry box with a label that can be shown. + * Creates a text entry box at a specific position on screen. * Returns the updated value of the text box. * * Example usage: * ```c++ - * my_string = text_box("Name", my_string, true); + * my_string = text_box("Name", my_string); * ``` * - * @param label_text Unique identifier for the text box (not drawn) * @param value The current value of the text box - * @param show_label Whether to show the label or not + * @param rect The rectangle to display the button in * @return The updated value of the text box * - * @attribute suffix labeled + * @attribute suffix at_position */ - string text_box(const string& label_text, const string& value, bool show_label); + string text_box(const string& value, const rectangle& rect); /** - * Creates a text entry box at a specific position on screen. + * Creates a text entry box with a label. * Returns the updated value of the text box. - * - * Example usage: - * ```c++ - * my_string = text_box("Name", my_string, rectangle_from(0,0,100,100)); - * ``` - * - * @param label_text Unique identifier for the text box (not drawn) * @param value The current value of the text box - * @param rect The rectangle to display the button in * @return The updated value of the text box - * - * @attribute suffix at_position */ - string text_box(const string& label_text, const string& value, const rectangle& rect); + string text_box(const string& value); /** * Returns if the last created element was changed at all (such as dragged, typed in, etc) diff --git a/coresdk/src/test/test_ui.cpp b/coresdk/src/test/test_ui.cpp index ea1caf85..efa68451 100644 --- a/coresdk/src/test/test_ui.cpp +++ b/coresdk/src/test/test_ui.cpp @@ -146,13 +146,13 @@ void run_ui_test() val3 = number_box("Number: ", val3, 1.0); // Show two text boxes - text_box_val1 = text_box("V1", text_box_val1); + text_box_val1 = text_box("Text:", text_box_val1); if (last_element_confirmed()) { checkbox_val = true; } set_interface_font(fontB); - text_box_val2 = text_box("Text:", text_box_val2, true); + text_box_val2 = text_box("Text:", text_box_val2); set_interface_font(fontA); } @@ -195,7 +195,7 @@ void run_ui_test() write_line("Button1 pressed"); } val2 = slider(val2, 0, 40, {40, 170, 150, 20}); - text_box_val2 = text_box("V2", text_box_val2, rectangle_from(40, 200, 150, 20)); + text_box_val2 = text_box(text_box_val2, rectangle_from(40, 200, 150, 20)); interface_style_panel(rectangle_from(0, 600-200, 600, 200)); diff --git a/projects/cmake/CMakeLists.txt b/projects/cmake/CMakeLists.txt index 40ba82a2..7b898131 100644 --- a/projects/cmake/CMakeLists.txt +++ b/projects/cmake/CMakeLists.txt @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.10) project(splashkit) -set(CMAKE_BUILD_TYPE Debug) - cmake_policy(SET CMP0083 NEW) include(CheckPIESupported) check_pie_supported() From 127212b08f235e95b753aac55768879011b2cbdf Mon Sep 17 00:00:00 2001 From: Sean Boettger Date: Tue, 13 May 2025 06:39:09 +1000 Subject: [PATCH 2/3] fix: make sliders that share `val2` in test_ui use same range (just makes things a bit clearer) --- coresdk/src/test/test_ui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coresdk/src/test/test_ui.cpp b/coresdk/src/test/test_ui.cpp index efa68451..76f0b93c 100644 --- a/coresdk/src/test/test_ui.cpp +++ b/coresdk/src/test/test_ui.cpp @@ -132,7 +132,7 @@ void run_ui_test() val1 = slider("Slider", val1, -25, 25); // Show two sliders without labels - val2 = slider(val2, 0, 100); + val2 = slider(val2, 0, 40); val3 = slider(val3, -25, 25); // Show checkbox that's checked when we drag the slider From 5368b7fe46c39a4abf46969968ef6bac146e46d8 Mon Sep 17 00:00:00 2001 From: Sean Boettger Date: Tue, 13 May 2025 06:42:01 +1000 Subject: [PATCH 3/3] fix: UI ID stack to replace using variable addresses as IDs + test case --- coresdk/src/backend/interface_driver.cpp | 58 ++++++++++++++++++++---- coresdk/src/backend/interface_driver.h | 2 +- coresdk/src/coresdk/interface.cpp | 2 +- coresdk/src/test/test_ui.cpp | 27 +++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/coresdk/src/backend/interface_driver.cpp b/coresdk/src/backend/interface_driver.cpp index 07cb4831..df213660 100644 --- a/coresdk/src/backend/interface_driver.cpp +++ b/coresdk/src/backend/interface_driver.cpp @@ -29,6 +29,27 @@ namespace splashkit_lib static mu_Id focused_text_box = 0; + // Hold a stack in parallel to MicroUI's, which we use to + // get incrementing IDs for elements which don't require labels + static std::vector id_stack; + + mu_Id _id_stack_next() + { + id_stack.back()++; + + return mu_get_id(ctx, &id_stack.back(), sizeof(id_stack.back())); + } + + void _id_stack_push() + { + id_stack.push_back(0); + } + + void _id_stack_pop() + { + id_stack.pop_back(); + } + static bool element_changed = false; static bool element_confirmed = false; @@ -430,11 +451,14 @@ namespace splashkit_lib mu_get_current_container(ctx)->zindex = -1; int widths[] = {0}; sk_interface_set_layout(1,widths,0); + + _id_stack_push(); } void sk_interface_end() { // end root window + _id_stack_pop(); mu_end_window(ctx); mu_end(ctx); @@ -469,41 +493,55 @@ namespace splashkit_lib bool sk_interface_start_panel(const string& name, rectangle initial_rectangle) { - return mu_begin_window(ctx, name.c_str(), to_mu(initial_rectangle)); + bool open = mu_begin_window(ctx, name.c_str(), to_mu(initial_rectangle)); + if (open) _id_stack_push(); + + return open; } void sk_interface_end_panel() { + _id_stack_pop(); mu_end_window(ctx); } bool sk_interface_start_popup(const string& name) { - return mu_begin_popup(ctx, name.c_str()); + bool open = mu_begin_popup(ctx, name.c_str()); + if (open) _id_stack_push(); + + return open; } void sk_interface_end_popup() { + _id_stack_pop(); mu_end_popup(ctx); } void sk_interface_start_inset(const string& name) { mu_begin_panel(ctx, name.c_str()); + _id_stack_push(); } void sk_interface_end_inset() { + _id_stack_pop(); mu_end_panel(ctx); } bool sk_interface_start_treenode(const string& name) { - return mu_begin_treenode(ctx, name.c_str()); + bool open = mu_begin_treenode(ctx, name.c_str()); + if (open) _id_stack_push(); + + return open; } void sk_interface_end_treenode() { + _id_stack_pop(); mu_end_treenode(ctx); } @@ -572,9 +610,10 @@ namespace splashkit_lib element_confirmed = result & MU_RES_SUBMIT; } - void sk_interface_push_ptr_id(void* ptr) + void sk_interface_push_temp_id() { - mu_push_id(ctx, &ptr, sizeof(ptr)); + mu_Id id = _id_stack_next(); + mu_push_id(ctx, &id, sizeof(id)); } void sk_interface_pop_id() @@ -605,7 +644,7 @@ namespace splashkit_lib bool sk_interface_checkbox(const string& label_text, const bool& value) { - sk_interface_push_ptr_id((void*)&value); + sk_interface_push_temp_id(); int temp_value = value; update_elements_changed(mu_checkbox(ctx, label_text.c_str(), &temp_value)); @@ -616,7 +655,7 @@ namespace splashkit_lib float sk_interface_slider(const float& value, float min_value, float max_value) { - sk_interface_push_ptr_id((void*)&value); + sk_interface_push_temp_id(); float temp_value = value; update_elements_changed(mu_slider(ctx, &temp_value, min_value, max_value)); @@ -627,7 +666,7 @@ namespace splashkit_lib float sk_interface_number(const float& value, float step) { - sk_interface_push_ptr_id((void*)&value); + sk_interface_push_temp_id(); float temp_value = value; update_elements_changed(mu_number(ctx, &temp_value, step)); @@ -638,8 +677,7 @@ namespace splashkit_lib std::string sk_interface_text_box(const std::string& value) { - const std::string* id = &value; - mu_Id m_id = mu_get_id(ctx, &id, sizeof(id)); + mu_Id m_id = _id_stack_next(); mu_Rect r = mu_layout_next(ctx); // max 512 characters diff --git a/coresdk/src/backend/interface_driver.h b/coresdk/src/backend/interface_driver.h index a775d894..47f019d8 100644 --- a/coresdk/src/backend/interface_driver.h +++ b/coresdk/src/backend/interface_driver.h @@ -51,7 +51,7 @@ namespace splashkit_lib int sk_interface_get_layout_width(); int sk_interface_get_layout_height(); - void sk_interface_push_ptr_id(void* ptr); + void sk_interface_push_temp_id(); void sk_interface_pop_id(); bool sk_interface_header(const string& label_text); diff --git a/coresdk/src/coresdk/interface.cpp b/coresdk/src/coresdk/interface.cpp index e1c08a7d..4c89ec32 100644 --- a/coresdk/src/coresdk/interface.cpp +++ b/coresdk/src/coresdk/interface.cpp @@ -990,7 +990,7 @@ namespace splashkit_lib _interface_sanity_check(); enter_column(); - sk_interface_push_ptr_id((void*)&clr); + sk_interface_push_temp_id(); color temp_value = clr; if (hsb) diff --git a/coresdk/src/test/test_ui.cpp b/coresdk/src/test/test_ui.cpp index 76f0b93c..008bd475 100644 --- a/coresdk/src/test/test_ui.cpp +++ b/coresdk/src/test/test_ui.cpp @@ -28,11 +28,13 @@ void run_ui_test() // Some variables to test the elements with bool checkbox_val = false; + bool checkbox_val2 = false; float val1 = 0; float val2 = 0; float val3 = 0; std::string text_box_val1 = "Type here!"; std::string text_box_val2 = "And here!"; + std::string text_box_val3 = "Option text"; // A sprite to test bitmap buttons animation_script player_animations = load_animation_script("player_animations", "player_animations.txt"); @@ -165,6 +167,17 @@ void run_ui_test() if (start_panel("Second Window", rectangle_from(300, 200, 240, 186))) { + checkbox_val2 = checkbox("ID Handle Check", checkbox_val2); + if (checkbox_val2) + { + start_inset("Options", 25); + text_box_val3 = text_box("Text:", text_box_val3); + end_inset("Options"); + start_inset("Options2", 25); + text_box_val3 = text_box("Text:", text_box_val3); + end_inset("Options2"); + } + start_inset("TreeView", -25); if (start_treenode("Node1")) { @@ -182,6 +195,20 @@ void run_ui_test() checkbox("It works right?", true); end_treenode("Node2"); } + if (start_treenode("ID Test 2")) + { + // This demonstrates an edge case with the current ID system + // Focus on Box 1 will switch back and forth between it and Box 2 + // This test can be used to check if future work has fixed this + static int time = 0; + time += 1; + if (time % 120 < 60) + { + text_box_val1 = text_box("Box 1:", text_box_val1); + } + text_box_val2 = text_box("Box 2:", text_box_val2); + end_treenode("ID Test 2"); + } end_inset("TreeView");