Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions coresdk/src/backend/interface_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mu_Id> 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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -636,10 +675,9 @@ 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());
mu_Id m_id = _id_stack_next();
mu_Rect r = mu_layout_next(ctx);

// max 512 characters
Expand Down
4 changes: 2 additions & 2 deletions coresdk/src/backend/interface_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
35 changes: 15 additions & 20 deletions coresdk/src/coresdk/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
29 changes: 10 additions & 19 deletions coresdk/src/coresdk/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 31 additions & 4 deletions coresdk/src/test/test_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -132,7 +134,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
Expand All @@ -146,13 +148,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);
}

Expand All @@ -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"))
{
Expand All @@ -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");

Expand All @@ -195,7 +222,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));

Expand Down
2 changes: 0 additions & 2 deletions projects/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
Loading