Skip to content

Commit 976dcbd

Browse files
committed
Merge branch 'WhyPenguins-sb/fix_UI_ID_generation' into develop
2 parents 1fa1c85 + a8d156c commit 976dcbd

6 files changed

Lines changed: 107 additions & 58 deletions

File tree

coresdk/src/backend/interface_driver.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ namespace splashkit_lib
2929

3030
static mu_Id focused_text_box = 0;
3131

32+
// Hold a stack in parallel to MicroUI's, which we use to
33+
// get incrementing IDs for elements which don't require labels
34+
static std::vector<mu_Id> id_stack;
35+
36+
mu_Id _id_stack_next()
37+
{
38+
id_stack.back()++;
39+
40+
return mu_get_id(ctx, &id_stack.back(), sizeof(id_stack.back()));
41+
}
42+
43+
void _id_stack_push()
44+
{
45+
id_stack.push_back(0);
46+
}
47+
48+
void _id_stack_pop()
49+
{
50+
id_stack.pop_back();
51+
}
52+
3253
static bool element_changed = false;
3354
static bool element_confirmed = false;
3455

@@ -430,11 +451,14 @@ namespace splashkit_lib
430451
mu_get_current_container(ctx)->zindex = -1;
431452
int widths[] = {0};
432453
sk_interface_set_layout(1,widths,0);
454+
455+
_id_stack_push();
433456
}
434457

435458
void sk_interface_end()
436459
{
437460
// end root window
461+
_id_stack_pop();
438462
mu_end_window(ctx);
439463

440464
mu_end(ctx);
@@ -469,41 +493,55 @@ namespace splashkit_lib
469493

470494
bool sk_interface_start_panel(const string& name, rectangle initial_rectangle)
471495
{
472-
return mu_begin_window(ctx, name.c_str(), to_mu(initial_rectangle));
496+
bool open = mu_begin_window(ctx, name.c_str(), to_mu(initial_rectangle));
497+
if (open) _id_stack_push();
498+
499+
return open;
473500
}
474501

475502
void sk_interface_end_panel()
476503
{
504+
_id_stack_pop();
477505
mu_end_window(ctx);
478506
}
479507

480508
bool sk_interface_start_popup(const string& name)
481509
{
482-
return mu_begin_popup(ctx, name.c_str());
510+
bool open = mu_begin_popup(ctx, name.c_str());
511+
if (open) _id_stack_push();
512+
513+
return open;
483514
}
484515

485516
void sk_interface_end_popup()
486517
{
518+
_id_stack_pop();
487519
mu_end_popup(ctx);
488520
}
489521

490522
void sk_interface_start_inset(const string& name)
491523
{
492524
mu_begin_panel(ctx, name.c_str());
525+
_id_stack_push();
493526
}
494527

495528
void sk_interface_end_inset()
496529
{
530+
_id_stack_pop();
497531
mu_end_panel(ctx);
498532
}
499533

500534
bool sk_interface_start_treenode(const string& name)
501535
{
502-
return mu_begin_treenode(ctx, name.c_str());
536+
bool open = mu_begin_treenode(ctx, name.c_str());
537+
if (open) _id_stack_push();
538+
539+
return open;
503540
}
504541

505542
void sk_interface_end_treenode()
506543
{
544+
_id_stack_pop();
507545
mu_end_treenode(ctx);
508546
}
509547

@@ -572,9 +610,10 @@ namespace splashkit_lib
572610
element_confirmed = result & MU_RES_SUBMIT;
573611
}
574612

575-
void sk_interface_push_ptr_id(void* ptr)
613+
void sk_interface_push_temp_id()
576614
{
577-
mu_push_id(ctx, &ptr, sizeof(ptr));
615+
mu_Id id = _id_stack_next();
616+
mu_push_id(ctx, &id, sizeof(id));
578617
}
579618

580619
void sk_interface_pop_id()
@@ -605,7 +644,7 @@ namespace splashkit_lib
605644

606645
bool sk_interface_checkbox(const string& label_text, const bool& value)
607646
{
608-
sk_interface_push_ptr_id((void*)&value);
647+
sk_interface_push_temp_id();
609648

610649
int temp_value = value;
611650
update_elements_changed(mu_checkbox(ctx, label_text.c_str(), &temp_value));
@@ -616,7 +655,7 @@ namespace splashkit_lib
616655

617656
float sk_interface_slider(const float& value, float min_value, float max_value)
618657
{
619-
sk_interface_push_ptr_id((void*)&value);
658+
sk_interface_push_temp_id();
620659

621660
float temp_value = value;
622661
update_elements_changed(mu_slider(ctx, &temp_value, min_value, max_value));
@@ -627,7 +666,7 @@ namespace splashkit_lib
627666

628667
float sk_interface_number(const float& value, float step)
629668
{
630-
sk_interface_push_ptr_id((void*)&value);
669+
sk_interface_push_temp_id();
631670

632671
float temp_value = value;
633672
update_elements_changed(mu_number(ctx, &temp_value, step));
@@ -636,10 +675,9 @@ namespace splashkit_lib
636675
return temp_value;
637676
}
638677

639-
std::string sk_interface_text_box(const std::string& id, const std::string& value)
678+
std::string sk_interface_text_box(const std::string& value)
640679
{
641-
// const std::string* id = &value;
642-
mu_Id m_id = mu_get_id(ctx, id.c_str(), id.length());
680+
mu_Id m_id = _id_stack_next();
643681
mu_Rect r = mu_layout_next(ctx);
644682

645683
// max 512 characters

coresdk/src/backend/interface_driver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace splashkit_lib
5151
int sk_interface_get_layout_width();
5252
int sk_interface_get_layout_height();
5353

54-
void sk_interface_push_ptr_id(void* ptr);
54+
void sk_interface_push_temp_id();
5555
void sk_interface_pop_id();
5656

5757
bool sk_interface_header(const string& label_text);
@@ -61,7 +61,7 @@ namespace splashkit_lib
6161
bool sk_interface_checkbox(const string& label_text, const bool& value);
6262
float sk_interface_slider(const float& value, float min_value, float max_value);
6363
float sk_interface_number(const float& value, float step);
64-
std::string sk_interface_text_box(const std::string& id, const std::string& value);
64+
std::string sk_interface_text_box(const std::string& value);
6565

6666
void sk_interface_color_box(color clr);
6767

coresdk/src/coresdk/interface.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ namespace splashkit_lib
990990
_interface_sanity_check();
991991
enter_column();
992992

993-
sk_interface_push_ptr_id((void*)&clr);
993+
sk_interface_push_temp_id();
994994

995995
color temp_value = clr;
996996
if (hsb)
@@ -1133,38 +1133,33 @@ namespace splashkit_lib
11331133
}
11341134

11351135
std::string text_box(const string& label_text, const std::string& value)
1136-
{
1137-
return text_box(label_text, value, false);
1138-
}
1139-
1140-
std::string text_box(const string& label_text, const std::string& value, bool show_label)
11411136
{
11421137
_interface_sanity_check();
11431138

1144-
if (show_label)
1145-
{
1146-
enter_column();
1147-
_two_column_layout();
1148-
1149-
splashkit_lib::label_element(label_text);
1150-
}
1139+
enter_column();
1140+
_two_column_layout();
11511141

1152-
std::string res = sk_interface_text_box(label_text, value);
1142+
splashkit_lib::label_element(label_text);
1143+
std::string res = text_box(value);
11531144

1154-
if (show_label)
1155-
{
1156-
leave_column();
1157-
}
1145+
leave_column();
11581146

11591147
return res;
11601148
}
11611149

1162-
std::string text_box(const string& label_text, const string& value, const rectangle& rect)
1150+
std::string text_box(const string& value, const rectangle& rect)
11631151
{
11641152
_interface_sanity_check();
11651153

11661154
sk_interface_set_layout_next(rect, true);
1167-
return text_box(label_text, value, false);
1155+
return text_box(value);
1156+
}
1157+
1158+
std::string text_box(const std::string& value)
1159+
{
1160+
_interface_sanity_check();
1161+
1162+
return sk_interface_text_box(value);
11681163
}
11691164

11701165
bool last_element_changed()

coresdk/src/coresdk/interface.h

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -645,47 +645,38 @@ namespace splashkit_lib
645645
* my_string = text_box("Name", my_string);
646646
* ```
647647
*
648-
* @param label_text Unique identifier for the text box (not drawn)
648+
* @param label_text The label to show in front of the text box
649649
* @param value The current value of the text box
650650
* @return The updated value of the text box
651+
*
652+
* @attribute suffix labeled
651653
*/
652654
string text_box(const string& label_text, const string& value);
653655

654656
/**
655-
* Creates a text entry box with a label that can be shown.
657+
* Creates a text entry box at a specific position on screen.
656658
* Returns the updated value of the text box.
657659
*
658660
* Example usage:
659661
* ```c++
660-
* my_string = text_box("Name", my_string, true);
662+
* my_string = text_box("Name", my_string);
661663
* ```
662664
*
663-
* @param label_text Unique identifier for the text box (not drawn)
664665
* @param value The current value of the text box
665-
* @param show_label Whether to show the label or not
666+
* @param rect The rectangle to display the button in
666667
* @return The updated value of the text box
667668
*
668-
* @attribute suffix labeled
669+
* @attribute suffix at_position
669670
*/
670-
string text_box(const string& label_text, const string& value, bool show_label);
671+
string text_box(const string& value, const rectangle& rect);
671672

672673
/**
673-
* Creates a text entry box at a specific position on screen.
674+
* Creates a text entry box with a label.
674675
* Returns the updated value of the text box.
675-
*
676-
* Example usage:
677-
* ```c++
678-
* my_string = text_box("Name", my_string, rectangle_from(0,0,100,100));
679-
* ```
680-
*
681-
* @param label_text Unique identifier for the text box (not drawn)
682676
* @param value The current value of the text box
683-
* @param rect The rectangle to display the button in
684677
* @return The updated value of the text box
685-
*
686-
* @attribute suffix at_position
687678
*/
688-
string text_box(const string& label_text, const string& value, const rectangle& rect);
679+
string text_box(const string& value);
689680

690681
/**
691682
* Returns if the last created element was changed at all (such as dragged, typed in, etc)

coresdk/src/test/test_ui.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ void run_ui_test()
2828

2929
// Some variables to test the elements with
3030
bool checkbox_val = false;
31+
bool checkbox_val2 = false;
3132
float val1 = 0;
3233
float val2 = 0;
3334
float val3 = 0;
3435
std::string text_box_val1 = "Type here!";
3536
std::string text_box_val2 = "And here!";
37+
std::string text_box_val3 = "Option text";
3638

3739
// A sprite to test bitmap buttons
3840
animation_script player_animations = load_animation_script("player_animations", "player_animations.txt");
@@ -132,7 +134,7 @@ void run_ui_test()
132134
val1 = slider("Slider", val1, -25, 25);
133135

134136
// Show two sliders without labels
135-
val2 = slider(val2, 0, 100);
137+
val2 = slider(val2, 0, 40);
136138
val3 = slider(val3, -25, 25);
137139

138140
// Show checkbox that's checked when we drag the slider
@@ -146,13 +148,13 @@ void run_ui_test()
146148
val3 = number_box("Number: ", val3, 1.0);
147149

148150
// Show two text boxes
149-
text_box_val1 = text_box("V1", text_box_val1);
151+
text_box_val1 = text_box("Text:", text_box_val1);
150152
if (last_element_confirmed())
151153
{
152154
checkbox_val = true;
153155
}
154156
set_interface_font(fontB);
155-
text_box_val2 = text_box("Text:", text_box_val2, true);
157+
text_box_val2 = text_box("Text:", text_box_val2);
156158
set_interface_font(fontA);
157159
}
158160

@@ -165,6 +167,17 @@ void run_ui_test()
165167

166168
if (start_panel("Second Window", rectangle_from(300, 200, 240, 186)))
167169
{
170+
checkbox_val2 = checkbox("ID Handle Check", checkbox_val2);
171+
if (checkbox_val2)
172+
{
173+
start_inset("Options", 25);
174+
text_box_val3 = text_box("Text:", text_box_val3);
175+
end_inset("Options");
176+
start_inset("Options2", 25);
177+
text_box_val3 = text_box("Text:", text_box_val3);
178+
end_inset("Options2");
179+
}
180+
168181
start_inset("TreeView", -25);
169182
if (start_treenode("Node1"))
170183
{
@@ -182,6 +195,20 @@ void run_ui_test()
182195
checkbox("It works right?", true);
183196
end_treenode("Node2");
184197
}
198+
if (start_treenode("ID Test 2"))
199+
{
200+
// This demonstrates an edge case with the current ID system
201+
// Focus on Box 1 will switch back and forth between it and Box 2
202+
// This test can be used to check if future work has fixed this
203+
static int time = 0;
204+
time += 1;
205+
if (time % 120 < 60)
206+
{
207+
text_box_val1 = text_box("Box 1:", text_box_val1);
208+
}
209+
text_box_val2 = text_box("Box 2:", text_box_val2);
210+
end_treenode("ID Test 2");
211+
}
185212

186213
end_inset("TreeView");
187214

@@ -195,7 +222,7 @@ void run_ui_test()
195222
write_line("Button1 pressed");
196223
}
197224
val2 = slider(val2, 0, 40, {40, 170, 150, 20});
198-
text_box_val2 = text_box("V2", text_box_val2, rectangle_from(40, 200, 150, 20));
225+
text_box_val2 = text_box(text_box_val2, rectangle_from(40, 200, 150, 20));
199226

200227
interface_style_panel(rectangle_from(0, 600-200, 600, 200));
201228

projects/cmake/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(splashkit)
33

4-
set(CMAKE_BUILD_TYPE Debug)
5-
64
cmake_policy(SET CMP0083 NEW)
75
include(CheckPIESupported)
86
include(ExternalProject)

0 commit comments

Comments
 (0)