Skip to content

Commit 5368b7f

Browse files
committed
fix: UI ID stack to replace using variable addresses as IDs + test case
1 parent 127212b commit 5368b7f

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

coresdk/src/backend/interface_driver.cpp

Lines changed: 48 additions & 10 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));
@@ -638,8 +677,7 @@ namespace splashkit_lib
638677

639678
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, sizeof(id));
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: 1 addition & 1 deletion
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);

coresdk/src/coresdk/interface.cpp

Lines changed: 1 addition & 1 deletion
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)

coresdk/src/test/test_ui.cpp

Lines changed: 27 additions & 0 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");
@@ -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

0 commit comments

Comments
 (0)