Skip to content

Commit

Permalink
Add support for multiple consoles/windows.
Browse files Browse the repository at this point in the history
Suggesting an initial solution for issue #5
  • Loading branch information
Fredrik Kallevik committed Nov 16, 2024
1 parent c15b52f commit 4d44989
Show file tree
Hide file tree
Showing 3 changed files with 408 additions and 102 deletions.
230 changes: 139 additions & 91 deletions demo/common/nuklear_console_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,85 @@
#include "../../nuklear_console.h"

// Demo
static struct nk_console* console = NULL;
static struct nk_gamepads gamepads;
static nk_bool shouldClose = nk_false;

// Theme
static int theme = THEME_DRACULA;

// Progress
static nk_size progressValue = 50;

// Combobox
static int weapon = 1;

// Property
static int property_int_test = 20;
static float property_float_test = 0.4f;

// Slider
static int slider_int_test = 20;
static float slider_float_test = 0.4f;

// Radio
static int radio_option = 1;
static int radio_option2 = 0;

// Checkbox
static nk_bool checkbox1 = nk_false;
static nk_bool checkbox2 = nk_false;
static nk_bool checkbox3 = nk_false;
static nk_bool checkbox4 = nk_false;
static nk_bool checkbox5 = nk_false;
static nk_bool checkbox6 = nk_true;

// Messages
static int message_count = 0;

// File
static char file_path_buffer[1024] = {0};
static int file_path_buffer_size = 1024;

// Textedit
static const int textedit_buffer_size = 256;
static char textedit_buffer[256] = "vurtun";

// Input
static int gamepad_number = 0;
static enum nk_gamepad_button gamepad_button = NK_GAMEPAD_BUTTON_A;

// Color
static struct nk_colorf color = {0.31f, 1.0f, 0.48f, 1.0f};
struct demo_console_state {
// Theme
int theme;

// Progress
nk_size progressValue;

// Combobox
int weapon;

// Property
int property_int_test;
float property_float_test;

// Slider
int slider_int_test;
float slider_float_test;

// Radio
int radio_option;
int radio_option2;

// Checkbox
nk_bool checkbox1;
nk_bool checkbox2;
nk_bool checkbox3;
nk_bool checkbox4;
nk_bool checkbox5;
nk_bool checkbox6;

// Messages
int message_count;

// File
char file_path_buffer[1024];
int file_path_buffer_size;

// Textedit
int textedit_buffer_size;
char textedit_buffer[256];

// Input
int gamepad_number;
enum nk_gamepad_button gamepad_button;

// Color
struct nk_colorf color;
};

static struct demo_console_state demo_console_state_defaults(void) {
struct demo_console_state state = {
.theme = THEME_DRACULA,
.progressValue = 50,
.weapon = 1,
.property_int_test = 20,
.property_float_test = 0.4f,
.slider_int_test = 20,
.slider_float_test = 0.4f,
.radio_option = 1,
.radio_option2 = 0,
.checkbox1 = nk_false,
.checkbox2 = nk_false,
.checkbox3 = nk_false,
.checkbox4 = nk_false,
.checkbox5 = nk_false,
.checkbox6 = nk_true,
.message_count = 0,
.file_path_buffer_size = 1024,
.textedit_buffer_size = 256,
.color = {0.31f, 1.0f, 0.48f, 1.0f},
.gamepad_number = 0,
.gamepad_button = NK_GAMEPAD_BUTTON_A,
};

return state;
}

void button_clicked(struct nk_console* button, void* user_data) {
NK_UNUSED(user_data);
Expand All @@ -69,8 +98,8 @@ void button_clicked(struct nk_console* button, void* user_data) {
}

void theme_changed(struct nk_console* combobox, void* user_data) {
NK_UNUSED(user_data);
set_style(combobox->ctx, (enum theme)theme);
enum theme t = *(enum theme*)user_data;
set_style(combobox->ctx, t);
}

void exclude_other_checkbox(nk_console* unused, void* user_data) {
Expand All @@ -86,9 +115,9 @@ void toggle_visibility(nk_console* unused, void* user_data) {
}

void nk_console_demo_show_message(struct nk_console* button, void* user_data) {
NK_UNUSED(user_data);
struct demo_console_state* state = (struct demo_console_state*)user_data;
char message[128];
snprintf(message, 128, "This is message #%d!", ++message_count);
snprintf(message, 128, "This is message #%d!", ++state->message_count);
nk_console_show_message(button, message);
}

Expand All @@ -97,9 +126,7 @@ void nk_console_radio_changed(struct nk_console* radio, void* user_data) {
nk_console_show_message(radio, radio->label);
}

nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, struct nk_image image) {
console = nk_console_init(ctx);

void nuklear_console_demo_init(struct nk_context* ctx, struct nk_console* console, struct demo_console_state* state, void* user_data, struct nk_image image) {
nk_gamepad_init(&gamepads, ctx, user_data);
nk_console_set_gamepads(console, &gamepads);

Expand Down Expand Up @@ -138,22 +165,22 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s

nk_console* checkbox_button = nk_console_button(widgets, "Checkboxes");
{
nk_console_checkbox(checkbox_button, "Checkbox", &checkbox1)
nk_console_checkbox(checkbox_button, "Checkbox", &state->checkbox1)
->tooltip = "This is a checkbox!";
nk_console_checkbox(checkbox_button, "Right aligned", &checkbox3)
nk_console_checkbox(checkbox_button, "Right aligned", &state->checkbox3)
->alignment = NK_TEXT_RIGHT;
nk_console_checkbox(checkbox_button, "Disabled Checkbox", &checkbox2)
nk_console_checkbox(checkbox_button, "Disabled Checkbox", &state->checkbox2)
->disabled = nk_true;

// Onchange callbacks can be used to implement custom logic.
// These two checkboxes disable each other when checked.
nk_console* exclude_a = nk_console_checkbox(checkbox_button, "Exclusive A (disables B)", &checkbox4);
nk_console* exclude_b = nk_console_checkbox(checkbox_button, "Exclusive B (disables A)", &checkbox5);
nk_console* exclude_a = nk_console_checkbox(checkbox_button, "Exclusive A (disables B)", &state->checkbox4);
nk_console* exclude_b = nk_console_checkbox(checkbox_button, "Exclusive B (disables A)", &state->checkbox5);
nk_console_add_event_handler(exclude_a, NK_CONSOLE_EVENT_CHANGED, &exclude_other_checkbox, exclude_b, NULL);
nk_console_add_event_handler(exclude_b, NK_CONSOLE_EVENT_CHANGED, &exclude_other_checkbox, exclude_a, NULL);

// Checkbox that will show/hide the below label.
nk_console* checkbox_show_label = nk_console_checkbox(checkbox_button, "Show Label", &checkbox6);
nk_console* checkbox_show_label = nk_console_checkbox(checkbox_button, "Show Label", &state->checkbox6);
nk_console* label_to_show = nk_console_label(checkbox_button, "This label is only shown when the checkbox is checked.");
nk_console_add_event_handler(checkbox_show_label, NK_CONSOLE_EVENT_CHANGED, &toggle_visibility, label_to_show, NULL);

Expand All @@ -179,20 +206,20 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s
nk_console* radios = nk_console_button(widgets, "Radios");
{
nk_console_label(radios, "Option A:");
nk_console_radio(radios, "Radio #1", &radio_option);
nk_console_radio(radios, "Radio #2", &radio_option);
nk_console_radio(radios, "Radio #3", &radio_option);
nk_console_radio(radios, "Radio #4", &radio_option);
nk_console_radio(radios, "Radio #1", &state->radio_option);
nk_console_radio(radios, "Radio #2", &state->radio_option);
nk_console_radio(radios, "Radio #3", &state->radio_option);
nk_console_radio(radios, "Radio #4", &state->radio_option);

nk_console_label(radios, "Option B:");
nk_console_radio(radios, "Left Aligned #1", &radio_option2)->alignment = NK_TEXT_LEFT;
nk_console_radio(radios, "Left Aligned #2", &radio_option2)->alignment = NK_TEXT_LEFT;
nk_console_radio(radios, "Center Aligned #1", &radio_option2)->alignment = NK_TEXT_CENTERED;
nk_console_radio(radios, "Center Aligned #2", &radio_option2)->alignment = NK_TEXT_CENTERED;
nk_console_radio(radios, "Right Aligned #1", &radio_option2)->alignment = NK_TEXT_RIGHT;
nk_console_radio(radios, "Right Aligned #2", &radio_option2)->alignment = NK_TEXT_RIGHT;
nk_console_radio(radios, "Disabled", &radio_option2)->disabled = nk_true;
nk_console_radio(radios, "Invisible", &radio_option2)->visible = nk_false;
nk_console_radio(radios, "Left Aligned #1", &state->radio_option2)->alignment = NK_TEXT_LEFT;
nk_console_radio(radios, "Left Aligned #2", &state->radio_option2)->alignment = NK_TEXT_LEFT;
nk_console_radio(radios, "Center Aligned #1", &state->radio_option2)->alignment = NK_TEXT_CENTERED;
nk_console_radio(radios, "Center Aligned #2", &state->radio_option2)->alignment = NK_TEXT_CENTERED;
nk_console_radio(radios, "Right Aligned #1", &state->radio_option2)->alignment = NK_TEXT_RIGHT;
nk_console_radio(radios, "Right Aligned #2", &state->radio_option2)->alignment = NK_TEXT_RIGHT;
nk_console_radio(radios, "Disabled", &state->radio_option2)->disabled = nk_true;
nk_console_radio(radios, "Invisible", &state->radio_option2)->visible = nk_false;

nk_console_button_onclick(radios, "Back", &nk_console_button_back);
}
Expand Down Expand Up @@ -239,56 +266,56 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s
// Progress Bar
nk_console* progressbar = nk_console_button(widgets, "Progress Bar");
{
nk_console_progress(progressbar, "Progress", &progressValue, 100);
nk_console_progress(progressbar, "Progress", &state->progressValue, 100);
nk_console_button_onclick(progressbar, "Back", &nk_console_button_back);
}

// Input: From any gamepad (-1)
nk_console_input(widgets, "Input Button", -1, &gamepad_number, &gamepad_button);
nk_console_input(widgets, "Input Button", -1, &state->gamepad_number, &state->gamepad_button);

// Combobox
nk_console_combobox(widgets, "ComboBox", "Fists;Chainsaw;Pistol;Shotgun;Chaingun", ';', &weapon)
nk_console_combobox(widgets, "ComboBox", "Fists;Chainsaw;Pistol;Shotgun;Chaingun", ';', &state->weapon)
->tooltip = "Choose a weapon! The chainsaw is the best!";

// Property
nk_console* properties = nk_console_button(widgets, "Property");
{
nk_console_property_int(properties, "Property Int", 10, &property_int_test, 30, 1, 1);
nk_console_property_float(properties, "Property Float", 0.0f, &property_float_test, 2.0f, 0.1f, 1);
nk_console_property_int(properties, "Property Int", 10, &state->property_int_test, 30, 1, 1);
nk_console_property_float(properties, "Property Float", 0.0f, &state->property_float_test, 2.0f, 0.1f, 1);
nk_console_button_onclick(properties, "Back", &nk_console_button_back);
}

// Sliders
nk_console* sliders = nk_console_button(widgets, "Sliders");
{
nk_console_slider_float(sliders, "Slider Float", 0.0f, &slider_float_test, 2.0f, 0.1f)->tooltip = "Slider float is cool! It's what you want to use.";
nk_console_slider_int(sliders, "Slider Int", 0, &slider_int_test, 20, 1);
nk_console_slider_float(sliders, "Slider Float", 0.0f, &state->slider_float_test, 2.0f, 0.1f)->tooltip = "Slider float is cool! It's what you want to use.";
nk_console_slider_int(sliders, "Slider Int", 0, &state->slider_int_test, 20, 1);
nk_console_button_onclick(sliders, "Back", &nk_console_button_back);
}

// Textedit
nk_console* textedit = nk_console_textedit(widgets, "Username", textedit_buffer, textedit_buffer_size);
nk_console* textedit = nk_console_textedit(widgets, "Username", state->textedit_buffer, state->textedit_buffer_size);
nk_console_set_tooltip(textedit, "Enter your username!");

// Color
nk_console_color(widgets, "Select Color", &color, NK_RGBA);
nk_console_color(widgets, "Select Color", &state->color, NK_RGBA);

// File
nk_console_file(widgets, "File", file_path_buffer, file_path_buffer_size);
nk_console_file(widgets, "File", state->file_path_buffer, state->file_path_buffer_size);

// Messages
nk_console_button_onclick(widgets, "Show Message", &nk_console_demo_show_message);
nk_console_button_onclick_handler(widgets, "Show Message", &nk_console_demo_show_message, &state, NULL);

// Back Button
nk_console_button_set_symbol(
nk_console_button_onclick(widgets, "Back", &nk_console_button_back),
NK_SYMBOL_TRIANGLE_LEFT);
}

nk_console* theme_options = nk_console_combobox(console, "Theme", "Black;White;Red;Blue;Dark;Dracula;Default", ';', &theme);
nk_console_add_event(theme_options, NK_CONSOLE_EVENT_CHANGED, &theme_changed);
nk_console* theme_options = nk_console_combobox(console, "Theme", "Black;White;Red;Blue;Dark;Dracula;Default", ';', &state->theme);
nk_console_add_event_handler(theme_options, NK_CONSOLE_EVENT_CHANGED, &theme_changed, &state->theme, NULL);
theme_options->tooltip = "Change the theme of the console!";
set_style(ctx, (enum theme)theme);
set_style(ctx, (enum theme)state->theme);

// Rows
nk_console* calc = nk_console_button(console, "Calculator");
Expand Down Expand Up @@ -342,17 +369,38 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s

nk_console_button(console, "Save Game")->disabled = nk_true;
nk_console_button_onclick(console, "Quit Game", &button_clicked);

return console;
}

nk_bool nuklear_console_demo_render() {
nk_bool nuklear_console_demo_render(nk_console* console) {
nk_console_render(console);

return shouldClose;;
return shouldClose;
}

void nuklear_console_demo_free() {
nk_bool nuklear_console_demo_render_window(nk_console* console, int num, int width, int height, int flags) {
char title[18];
snprintf(title, sizeof(title), "nuklear_console_%d", num);

int margin = 10;

nk_console_render_window(
console,
title,
nk_rect(margin + width * (num - 1), margin, width - margin, height - margin * 2),
flags);

return shouldClose;
}

#if CONSOLE_COUNT < 2
void nuklear_console_demo_free(nk_console* console) {
nk_gamepad_free(nk_console_get_gamepads(console));
nk_console_free(console);
}
#else
void nuklear_console_demo_free(nk_consoles* consoles) {
nk_gamepad_free(&gamepads);
nk_consoles_free(consoles);
}
#endif

Loading

0 comments on commit 4d44989

Please sign in to comment.