From 739c415e7388ddf5ccc17cfc91aed38efb68048a Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Tue, 3 Mar 2026 01:28:09 -0700 Subject: [PATCH 1/4] WIP/exploratory export styles --- demo/common/style_configurator.c | 755 ++++++++++++++++++++++++++++++- demo/sdl_renderer/main.c | 4 +- nuklear.h | 10 + src/nuklear_style.c | 10 + 4 files changed, 776 insertions(+), 3 deletions(-) diff --git a/demo/common/style_configurator.c b/demo/common/style_configurator.c index ad7de96f0..7b076bcbe 100644 --- a/demo/common/style_configurator.c +++ b/demo/common/style_configurator.c @@ -23,6 +23,744 @@ static const char* symbols[NK_SYMBOL_MAX] = "MINUS" }; + + /*Not re-entrant/thread-safe*/ +static const char* +export_color(struct nk_color c) +{ + static char buf[32]; + sprintf(buf, "{ %u, %u, %u, %u }", c.r, c.g, c.b, c.a); + return buf; +} + +static void +export_global_color_table(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT], nk_bool use_labels) +{ + int i; + struct nk_color* c = color_table; + + const char** names = nk_get_color_names(); + + puts("{"); + for ( i=0; istyle; + table = (!table) ? nk_default_color_style: table; + + /* default text */ + text = &style->text; + text->color = table[NK_COLOR_TEXT]; + text->padding = nk_vec2(0,0); + text->color_factor = 1.0f; + text->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + /* default button */ + button = &style->button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_BUTTON]); + button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]); + button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]); + button->border_color = table[NK_COLOR_BORDER]; + button->text_background = table[NK_COLOR_BUTTON]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(2.0f,2.0f); + button->image_padding = nk_vec2(0.0f,0.0f); + button->touch_padding = nk_vec2(0.0f, 0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 1.0f; + button->rounding = 4.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + + /* contextual button */ + button = &style->contextual_button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]); + button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]); + button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]); + button->border_color = table[NK_COLOR_WINDOW]; + button->text_background = table[NK_COLOR_WINDOW]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(2.0f,2.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 0.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + + /* menu button */ + button = &style->menu_button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]); + button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]); + button->active = nk_style_item_color(table[NK_COLOR_WINDOW]); + button->border_color = table[NK_COLOR_WINDOW]; + button->text_background = table[NK_COLOR_WINDOW]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(2.0f,2.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 0.0f; + button->rounding = 1.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + + /* checkbox toggle */ + toggle = &style->checkbox; + nk_zero_struct(*toggle); + toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]); + toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]); + toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]); + toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]); + toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]); + toggle->userdata = nk_handle_ptr(0); + toggle->text_background = table[NK_COLOR_WINDOW]; + toggle->text_normal = table[NK_COLOR_TEXT]; + toggle->text_hover = table[NK_COLOR_TEXT]; + toggle->text_active = table[NK_COLOR_TEXT]; + toggle->padding = nk_vec2(2.0f, 2.0f); + toggle->touch_padding = nk_vec2(0,0); + toggle->border_color = nk_rgba(0,0,0,0); + toggle->border = 0.0f; + toggle->spacing = 4; + toggle->color_factor = 1.0f; + toggle->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + /* option toggle */ + toggle = &style->option; + nk_zero_struct(*toggle); + toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]); + toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]); + toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]); + toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]); + toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]); + toggle->userdata = nk_handle_ptr(0); + toggle->text_background = table[NK_COLOR_WINDOW]; + toggle->text_normal = table[NK_COLOR_TEXT]; + toggle->text_hover = table[NK_COLOR_TEXT]; + toggle->text_active = table[NK_COLOR_TEXT]; + toggle->padding = nk_vec2(3.0f, 3.0f); + toggle->touch_padding = nk_vec2(0,0); + toggle->border_color = nk_rgba(0,0,0,0); + toggle->border = 0.0f; + toggle->spacing = 4; + toggle->color_factor = 1.0f; + toggle->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + /* selectable */ + select = &style->selectable; + nk_zero_struct(*select); + select->normal = nk_style_item_color(table[NK_COLOR_SELECT]); + select->hover = nk_style_item_color(table[NK_COLOR_SELECT]); + select->pressed = nk_style_item_color(table[NK_COLOR_SELECT]); + select->normal_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]); + select->hover_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]); + select->pressed_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]); + select->text_normal = table[NK_COLOR_TEXT]; + select->text_hover = table[NK_COLOR_TEXT]; + select->text_pressed = table[NK_COLOR_TEXT]; + select->text_normal_active = table[NK_COLOR_TEXT]; + select->text_hover_active = table[NK_COLOR_TEXT]; + select->text_pressed_active = table[NK_COLOR_TEXT]; + select->padding = nk_vec2(2.0f,2.0f); + select->image_padding = nk_vec2(2.0f,2.0f); + select->touch_padding = nk_vec2(0,0); + select->userdata = nk_handle_ptr(0); + select->rounding = 0.0f; + select->color_factor = 1.0f; + select->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + select->draw_begin = 0; + select->draw_end = 0; + + /* slider */ + slider = &style->slider; + nk_zero_struct(*slider); + slider->normal = nk_style_item_hide(); + slider->hover = nk_style_item_hide(); + slider->active = nk_style_item_hide(); + slider->bar_normal = table[NK_COLOR_SLIDER]; + slider->bar_hover = table[NK_COLOR_SLIDER]; + slider->bar_active = table[NK_COLOR_SLIDER]; + slider->bar_filled = table[NK_COLOR_SLIDER_CURSOR]; + slider->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]); + slider->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]); + slider->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]); + slider->inc_symbol = NK_SYMBOL_TRIANGLE_RIGHT; + slider->dec_symbol = NK_SYMBOL_TRIANGLE_LEFT; + slider->cursor_size = nk_vec2(16,16); + slider->padding = nk_vec2(2,2); + slider->spacing = nk_vec2(2,2); + slider->userdata = nk_handle_ptr(0); + slider->show_buttons = nk_false; + slider->bar_height = 8; + slider->rounding = 0; + slider->color_factor = 1.0f; + slider->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + slider->draw_begin = 0; + slider->draw_end = 0; + + /* slider buttons */ + button = &style->slider.inc_button; + button->normal = nk_style_item_color(nk_rgb(40,40,40)); + button->hover = nk_style_item_color(nk_rgb(42,42,42)); + button->active = nk_style_item_color(nk_rgb(44,44,44)); + button->border_color = nk_rgb(65,65,65); + button->text_background = nk_rgb(40,40,40); + button->text_normal = nk_rgb(175,175,175); + button->text_hover = nk_rgb(175,175,175); + button->text_active = nk_rgb(175,175,175); + button->padding = nk_vec2(8.0f,8.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 1.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + style->slider.dec_button = style->slider.inc_button; + + /* knob */ + knob = &style->knob; + nk_zero_struct(*knob); + knob->normal = nk_style_item_hide(); + knob->hover = nk_style_item_hide(); + knob->active = nk_style_item_hide(); + knob->knob_normal = table[NK_COLOR_KNOB]; + knob->knob_hover = table[NK_COLOR_KNOB]; + knob->knob_active = table[NK_COLOR_KNOB]; + knob->cursor_normal = table[NK_COLOR_KNOB_CURSOR]; + knob->cursor_hover = table[NK_COLOR_KNOB_CURSOR_HOVER]; + knob->cursor_active = table[NK_COLOR_KNOB_CURSOR_ACTIVE]; + + knob->knob_border_color = table[NK_COLOR_BORDER]; + knob->knob_border = 1.0f; + + knob->padding = nk_vec2(2,2); + knob->spacing = nk_vec2(2,2); + knob->cursor_width = 2; + knob->color_factor = 1.0f; + knob->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + knob->userdata = nk_handle_ptr(0); + knob->draw_begin = 0; + knob->draw_end = 0; + + /* progressbar */ + prog = &style->progress; + nk_zero_struct(*prog); + prog->normal = nk_style_item_color(table[NK_COLOR_SLIDER]); + prog->hover = nk_style_item_color(table[NK_COLOR_SLIDER]); + prog->active = nk_style_item_color(table[NK_COLOR_SLIDER]); + prog->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]); + prog->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]); + prog->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]); + prog->border_color = nk_rgba(0,0,0,0); + prog->cursor_border_color = nk_rgba(0,0,0,0); + prog->userdata = nk_handle_ptr(0); + prog->padding = nk_vec2(4,4); + prog->rounding = 0; + prog->border = 0; + prog->cursor_rounding = 0; + prog->cursor_border = 0; + prog->color_factor = 1.0f; + prog->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + prog->draw_begin = 0; + prog->draw_end = 0; + + /* scrollbars */ + scroll = &style->scrollh; + nk_zero_struct(*scroll); + scroll->normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR]); + scroll->hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR]); + scroll->active = nk_style_item_color(table[NK_COLOR_SCROLLBAR]); + scroll->cursor_normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR]); + scroll->cursor_hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_HOVER]); + scroll->cursor_active = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE]); + scroll->dec_symbol = NK_SYMBOL_CIRCLE_SOLID; + scroll->inc_symbol = NK_SYMBOL_CIRCLE_SOLID; + scroll->userdata = nk_handle_ptr(0); + scroll->border_color = table[NK_COLOR_SCROLLBAR]; + scroll->cursor_border_color = table[NK_COLOR_SCROLLBAR]; + scroll->padding = nk_vec2(0,0); + scroll->show_buttons = nk_false; + scroll->border = 0; + scroll->rounding = 0; + scroll->border_cursor = 0; + scroll->rounding_cursor = 0; + scroll->color_factor = 1.0f; + scroll->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + scroll->draw_begin = 0; + scroll->draw_end = 0; + style->scrollv = style->scrollh; + + /* scrollbars buttons */ + button = &style->scrollh.inc_button; + button->normal = nk_style_item_color(nk_rgb(40,40,40)); + button->hover = nk_style_item_color(nk_rgb(42,42,42)); + button->active = nk_style_item_color(nk_rgb(44,44,44)); + button->border_color = nk_rgb(65,65,65); + button->text_background = nk_rgb(40,40,40); + button->text_normal = nk_rgb(175,175,175); + button->text_hover = nk_rgb(175,175,175); + button->text_active = nk_rgb(175,175,175); + button->padding = nk_vec2(4.0f,4.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 1.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + style->scrollh.dec_button = style->scrollh.inc_button; + style->scrollv.inc_button = style->scrollh.inc_button; + style->scrollv.dec_button = style->scrollh.inc_button; + + /* edit */ + edit = &style->edit; + nk_zero_struct(*edit); + edit->normal = nk_style_item_color(table[NK_COLOR_EDIT]); + edit->hover = nk_style_item_color(table[NK_COLOR_EDIT]); + edit->active = nk_style_item_color(table[NK_COLOR_EDIT]); + edit->cursor_normal = table[NK_COLOR_TEXT]; + edit->cursor_hover = table[NK_COLOR_TEXT]; + edit->cursor_text_normal= table[NK_COLOR_EDIT]; + edit->cursor_text_hover = table[NK_COLOR_EDIT]; + edit->border_color = table[NK_COLOR_BORDER]; + edit->text_normal = table[NK_COLOR_TEXT]; + edit->text_hover = table[NK_COLOR_TEXT]; + edit->text_active = table[NK_COLOR_TEXT]; + edit->selected_normal = table[NK_COLOR_TEXT]; + edit->selected_hover = table[NK_COLOR_TEXT]; + edit->selected_text_normal = table[NK_COLOR_EDIT]; + edit->selected_text_hover = table[NK_COLOR_EDIT]; + edit->scrollbar_size = nk_vec2(10,10); + edit->scrollbar = style->scrollv; + edit->padding = nk_vec2(4,4); + edit->row_padding = 2; + edit->cursor_size = 4; + edit->border = 1; + edit->rounding = 0; + edit->color_factor = 1.0f; + edit->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + /* property */ + property = &style->property; + nk_zero_struct(*property); + property->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]); + property->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]); + property->active = nk_style_item_color(table[NK_COLOR_PROPERTY]); + property->border_color = table[NK_COLOR_BORDER]; + property->label_normal = table[NK_COLOR_TEXT]; + property->label_hover = table[NK_COLOR_TEXT]; + property->label_active = table[NK_COLOR_TEXT]; + property->sym_left = NK_SYMBOL_TRIANGLE_LEFT; + property->sym_right = NK_SYMBOL_TRIANGLE_RIGHT; + property->userdata = nk_handle_ptr(0); + property->padding = nk_vec2(4,4); + property->border = 1; + property->rounding = 10; + property->draw_begin = 0; + property->draw_end = 0; + property->color_factor = 1.0f; + property->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + /* property buttons */ + button = &style->property.dec_button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]); + button->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]); + button->active = nk_style_item_color(table[NK_COLOR_PROPERTY]); + button->border_color = nk_rgba(0,0,0,0); + button->text_background = table[NK_COLOR_PROPERTY]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(0.0f,0.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 0.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + style->property.inc_button = style->property.dec_button; + + /* property edit */ + edit = &style->property.edit; + nk_zero_struct(*edit); + edit->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]); + edit->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]); + edit->active = nk_style_item_color(table[NK_COLOR_PROPERTY]); + edit->border_color = nk_rgba(0,0,0,0); + edit->cursor_normal = table[NK_COLOR_TEXT]; + edit->cursor_hover = table[NK_COLOR_TEXT]; + edit->cursor_text_normal= table[NK_COLOR_EDIT]; + edit->cursor_text_hover = table[NK_COLOR_EDIT]; + edit->text_normal = table[NK_COLOR_TEXT]; + edit->text_hover = table[NK_COLOR_TEXT]; + edit->text_active = table[NK_COLOR_TEXT]; + edit->selected_normal = table[NK_COLOR_TEXT]; + edit->selected_hover = table[NK_COLOR_TEXT]; + edit->selected_text_normal = table[NK_COLOR_EDIT]; + edit->selected_text_hover = table[NK_COLOR_EDIT]; + edit->padding = nk_vec2(0,0); + edit->cursor_size = 8; + edit->border = 0; + edit->rounding = 0; + edit->color_factor = 1.0f; + edit->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + /* chart */ + chart = &style->chart; + nk_zero_struct(*chart); + chart->background = nk_style_item_color(table[NK_COLOR_CHART]); + chart->border_color = table[NK_COLOR_BORDER]; + chart->selected_color = table[NK_COLOR_CHART_COLOR_HIGHLIGHT]; + chart->color = table[NK_COLOR_CHART_COLOR]; + chart->padding = nk_vec2(4,4); + chart->border = 0; + chart->rounding = 0; + chart->color_factor = 1.0f; + chart->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + chart->show_markers = nk_true; + + /* combo */ + combo = &style->combo; + combo->normal = nk_style_item_color(table[NK_COLOR_COMBO]); + combo->hover = nk_style_item_color(table[NK_COLOR_COMBO]); + combo->active = nk_style_item_color(table[NK_COLOR_COMBO]); + combo->border_color = table[NK_COLOR_BORDER]; + combo->label_normal = table[NK_COLOR_TEXT]; + combo->label_hover = table[NK_COLOR_TEXT]; + combo->label_active = table[NK_COLOR_TEXT]; + combo->sym_normal = NK_SYMBOL_TRIANGLE_DOWN; + combo->sym_hover = NK_SYMBOL_TRIANGLE_DOWN; + combo->sym_active = NK_SYMBOL_TRIANGLE_DOWN; + combo->content_padding = nk_vec2(4,4); + combo->button_padding = nk_vec2(0,4); + combo->spacing = nk_vec2(4,0); + combo->border = 1; + combo->rounding = 0; + combo->color_factor = 1.0f; + combo->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + /* combo button */ + button = &style->combo.button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_COMBO]); + button->hover = nk_style_item_color(table[NK_COLOR_COMBO]); + button->active = nk_style_item_color(table[NK_COLOR_COMBO]); + button->border_color = nk_rgba(0,0,0,0); + button->text_background = table[NK_COLOR_COMBO]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(2.0f,2.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 0.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + + /* tab */ + tab = &style->tab; + tab->background = nk_style_item_color(table[NK_COLOR_TAB_HEADER]); + tab->border_color = table[NK_COLOR_BORDER]; + tab->text = table[NK_COLOR_TEXT]; + tab->sym_minimize = NK_SYMBOL_TRIANGLE_RIGHT; + tab->sym_maximize = NK_SYMBOL_TRIANGLE_DOWN; + tab->padding = nk_vec2(4,4); + tab->spacing = nk_vec2(4,4); + tab->indent = 10.0f; + tab->border = 1; + tab->rounding = 0; + tab->color_factor = 1.0f; + tab->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + + /* tab button */ + button = &style->tab.tab_minimize_button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_TAB_HEADER]); + button->hover = nk_style_item_color(table[NK_COLOR_TAB_HEADER]); + button->active = nk_style_item_color(table[NK_COLOR_TAB_HEADER]); + button->border_color = nk_rgba(0,0,0,0); + button->text_background = table[NK_COLOR_TAB_HEADER]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(2.0f,2.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 0.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + style->tab.tab_maximize_button =*button; + + /* node button */ + button = &style->tab.node_minimize_button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]); + button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]); + button->active = nk_style_item_color(table[NK_COLOR_WINDOW]); + button->border_color = nk_rgba(0,0,0,0); + button->text_background = table[NK_COLOR_TAB_HEADER]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(2.0f,2.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 0.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + style->tab.node_maximize_button =*button; + + /* window header */ + win = &style->window; + win->header.align = NK_HEADER_RIGHT; + win->header.close_symbol = NK_SYMBOL_X; + win->header.minimize_symbol = NK_SYMBOL_MINUS; + win->header.maximize_symbol = NK_SYMBOL_PLUS; + win->header.normal = nk_style_item_color(table[NK_COLOR_HEADER]); + win->header.hover = nk_style_item_color(table[NK_COLOR_HEADER]); + win->header.active = nk_style_item_color(table[NK_COLOR_HEADER]); + win->header.label_normal = table[NK_COLOR_TEXT]; + win->header.label_hover = table[NK_COLOR_TEXT]; + win->header.label_active = table[NK_COLOR_TEXT]; + win->header.label_padding = nk_vec2(4,4); + win->header.padding = nk_vec2(4,4); + win->header.spacing = nk_vec2(0,0); + + /* window header close button */ + button = &style->window.header.close_button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_HEADER]); + button->hover = nk_style_item_color(table[NK_COLOR_HEADER]); + button->active = nk_style_item_color(table[NK_COLOR_HEADER]); + button->border_color = nk_rgba(0,0,0,0); + button->text_background = table[NK_COLOR_HEADER]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(0.0f,0.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 0.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + + /* window header minimize button */ + button = &style->window.header.minimize_button; + nk_zero_struct(*button); + button->normal = nk_style_item_color(table[NK_COLOR_HEADER]); + button->hover = nk_style_item_color(table[NK_COLOR_HEADER]); + button->active = nk_style_item_color(table[NK_COLOR_HEADER]); + button->border_color = nk_rgba(0,0,0,0); + button->text_background = table[NK_COLOR_HEADER]; + button->text_normal = table[NK_COLOR_TEXT]; + button->text_hover = table[NK_COLOR_TEXT]; + button->text_active = table[NK_COLOR_TEXT]; + button->padding = nk_vec2(0.0f,0.0f); + button->touch_padding = nk_vec2(0.0f,0.0f); + button->userdata = nk_handle_ptr(0); + button->text_alignment = NK_TEXT_CENTERED; + button->border = 0.0f; + button->rounding = 0.0f; + button->color_factor_text = 1.0f; + button->color_factor_background = 1.0f; + button->disabled_factor = NK_WIDGET_DISABLED_FACTOR; + button->draw_begin = 0; + button->draw_end = 0; + + /* window */ + win->background = table[NK_COLOR_WINDOW]; + win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]); + win->border_color = table[NK_COLOR_BORDER]; + win->popup_border_color = table[NK_COLOR_BORDER]; + win->combo_border_color = table[NK_COLOR_BORDER]; + win->contextual_border_color = table[NK_COLOR_BORDER]; + win->menu_border_color = table[NK_COLOR_BORDER]; + win->group_border_color = table[NK_COLOR_BORDER]; + win->tooltip_border_color = table[NK_COLOR_BORDER]; + win->scaler = nk_style_item_color(table[NK_COLOR_TEXT]); + + win->rounding = 0.0f; + win->spacing = nk_vec2(4,4); + win->scrollbar_size = nk_vec2(10,10); + win->min_size = nk_vec2(64,64); + + win->combo_border = 1.0f; + win->contextual_border = 1.0f; + win->menu_border = 1.0f; + win->group_border = 1.0f; + win->tooltip_border = 1.0f; + win->popup_border = 1.0f; + win->border = 2.0f; + win->min_row_height_padding = 8; + + win->padding = nk_vec2(4,4); + win->group_padding = nk_vec2(4,4); + win->popup_padding = nk_vec2(4,4); + win->combo_padding = nk_vec2(4,4); + win->contextual_padding = nk_vec2(4,4); + win->menu_padding = nk_vec2(4,4); + win->tooltip_padding = nk_vec2(4,4); +} +#endif + +static void +export_styles(struct nk_context* ctx) +{ + struct nk_style* style; + + NK_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; + +/* + export_text_style(&style->text); + + export_button_style(&style->button); + + // TODO reuse export_button_style? + export_contextual_button_style(&style->contextual_button); + export_menu_button_style(&style->menu_button); + + export_checkbox_style(&style->checkbox); + + export_option_style(&style->option); + + export_selectable_style(&style->selectable); + + // Sliders + export_slider_style(&style->slider); + // slider buttons inside slider? + export_button_style(&style->slider.inc_button); + export_button_style(&style->slider.dec_button); + + export_knob_style(&style->knob); + + export_progressbar_style(&style->progress); + + // TODO scroll bar stuff + export_scrollbar_style(&style->scrollh); + export_scrollbar_style(&style->scrollv); + + export_button_style(&style->scrollh.inc_button); + export_button_style(&style->scrollh.dec_button); + export_button_style(&style->scrollv.inc_button); + export_button_style(&style->scrollv.dec_button); + + + export_edit_style(&style->edit); + + // TODO property stuff + export_property_style(&style->property); + export_button_style(&style->property.dec_button); + export_button_style(&style->property.inc_button); + export_edit_style(&style->property.edit); + + + export_chart_style(&style->chart); + + // combo + export_combo_style(&style->combo); + export_button_style(&style->combo.button); + + // tab + export_tab_style(&style->tab); + export_button_style(&style->tab.tab_minimize_button); + export_button_style(&style->tab.tab_maximize_button); + + export_button_style(&style->tab.node_minimize_button); + export_button_style(&style->tab.node_maximize_button); + + // window + export_window_header_style(&style->window.header); + export_button_style(&style->window.header.close_button); + export_button_style(&style->window.header.minimize_button); + export_window_style(&style->window); + */ +} + static int style_rgb(struct nk_context* ctx, const char* name, struct nk_color* color) { @@ -128,6 +866,9 @@ style_text(struct nk_context* ctx, struct nk_style_text* out_style) style_vec2(ctx, "Padding:", &text.padding); + nk_property_float(ctx, "#Color Factor:", 0.0f, &text.color_factor, 1.0f, 0.1, 0.0025f); + nk_property_float(ctx, "#Disabled Factor:", 0.0f, &text.disabled_factor, 1.0f, 0.1, 0.0025f); + *out_style = text; } @@ -702,7 +1443,7 @@ style_window(struct nk_context* ctx, struct nk_style_window* out_style) } static int -style_configurator(struct nk_context *ctx, struct nk_color color_table[NK_COLOR_COUNT]) +style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT]) { /* window flags */ int border = nk_true; @@ -730,6 +1471,15 @@ style_configurator(struct nk_context *ctx, struct nk_color color_table[NK_COLOR_ { if (nk_tree_push(ctx, NK_TREE_TAB, "Global Colors", NK_MINIMIZED)) { style_global_colors(ctx, color_table); + + nk_layout_row_dynamic(ctx, 30, 1); + if (nk_button_label(ctx, "Export global color styles")) { + export_global_color_table(ctx, color_table, nk_false); + } + if (nk_button_label(ctx, "Export global color styles with labels")) { + export_global_color_table(ctx, color_table, nk_true); + } + nk_tree_pop(ctx); } @@ -847,6 +1597,9 @@ style_configurator(struct nk_context *ctx, struct nk_color color_table[NK_COLOR_ memcpy(color_table, nk_default_color_style, sizeof(nk_default_color_style)); nk_style_default(ctx); } + if (nk_button_label(ctx, "Export styles")) { + export_styles(ctx); + } } diff --git a/demo/sdl_renderer/main.c b/demo/sdl_renderer/main.c index 8193f5435..72dae1d0d 100644 --- a/demo/sdl_renderer/main.c +++ b/demo/sdl_renderer/main.c @@ -38,7 +38,7 @@ /*#define INCLUDE_CALCULATOR */ /*#define INCLUDE_CANVAS */ #define INCLUDE_OVERVIEW -/*#define INCLUDE_CONFIGURATOR */ +#define INCLUDE_CONFIGURATOR /*#define INCLUDE_NODE_EDITOR */ #ifdef INCLUDE_ALL @@ -90,7 +90,7 @@ main(void) #ifdef INCLUDE_CONFIGURATOR static struct nk_color color_table[NK_COLOR_COUNT]; - memcpy(color_table, nk_default_color_style, sizeof(color_table)); + memcpy(color_table, nk_get_default_color_table(), sizeof(color_table)); #endif /* SDL setup */ diff --git a/nuklear.h b/nuklear.h index 5939be954..d5d308c26 100644 --- a/nuklear.h +++ b/nuklear.h @@ -18588,6 +18588,16 @@ NK_GLOBAL const char *nk_color_names[NK_COLOR_COUNT] = { #undef NK_COLOR }; +NK_API const struct nk_color* +nk_get_default_color_table(void) +{ + return nk_default_color_style; +} +NK_API const char** +nk_get_color_names(void) +{ + return nk_color_names; +} NK_API const char* nk_style_get_color_by_name(enum nk_style_colors c) { diff --git a/src/nuklear_style.c b/src/nuklear_style.c index b241e7757..ea743e36e 100644 --- a/src/nuklear_style.c +++ b/src/nuklear_style.c @@ -53,6 +53,16 @@ NK_GLOBAL const char *nk_color_names[NK_COLOR_COUNT] = { #undef NK_COLOR }; +NK_API const struct nk_color* +nk_get_default_color_table(void) +{ + return nk_default_color_style; +} +NK_API const char** +nk_get_color_names(void) +{ + return nk_color_names; +} NK_API const char* nk_style_get_color_by_name(enum nk_style_colors c) { From cdbff2017f1d3b31d365ec6122e3dae1f8895d74 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Thu, 5 Mar 2026 19:33:47 -0700 Subject: [PATCH 2/4] Just getting stuff down first --- demo/common/style_configurator.c | 694 ++++++++++++++++++++++++++++--- 1 file changed, 634 insertions(+), 60 deletions(-) diff --git a/demo/common/style_configurator.c b/demo/common/style_configurator.c index 7b076bcbe..14c60f13b 100644 --- a/demo/common/style_configurator.c +++ b/demo/common/style_configurator.c @@ -26,7 +26,7 @@ static const char* symbols[NK_SYMBOL_MAX] = /*Not re-entrant/thread-safe*/ static const char* -export_color(struct nk_color c) +nk_color2str(struct nk_color c) { static char buf[32]; sprintf(buf, "{ %u, %u, %u, %u }", c.r, c.g, c.b, c.a); @@ -34,22 +34,28 @@ export_color(struct nk_color c) } static void -export_global_color_table(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT], nk_bool use_labels) +export_color(struct nk_color c, FILE* out) +{ + fprintf(out, "%s,\n", nk_color2str(c)); +} + +static void +export_global_color_table(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT], nk_bool use_labels, FILE* out) { int i; struct nk_color* c = color_table; const char** names = nk_get_color_names(); - puts("{"); + fputs("{\n", out); for ( i=0; istyle; +static void +export_float(float f, FILE* out) +{ + fprintf(out, "%f,\n", f); +} -/* - export_text_style(&style->text); +static void +export_int(int i, FILE* out) +{ + fprintf(out, "%d,\n", i); +} + +static void +export_text_style(struct nk_style_text* text, FILE* out) +{ + fputs("{\n", out); + export_color(text->color, out); + export_vec2(text->padding, out); + export_float(text->color_factor, out); + export_float(text->disabled_factor, out); + fputs("},\n", out); +} + +/*TODO image and 9-slice?*/ +static void +export_style_item_color(struct nk_style_item s, FILE* out) +{ + fputs("{\n", out); + fprintf(out, "%d,\n", NK_STYLE_ITEM_COLOR); + fprintf(out, "{ %s }\n", nk_color2str(s.data.color)); + fputs("},\n", out); +} + +static void +export_button_style(struct nk_style_button* button, FILE* out) +{ + /*TODO maybe just do normal color convert it to style_item on read?*/ + fputs("{\n", out); + export_style_item_color(button->normal, out); + export_style_item_color(button->hover, out); + export_style_item_color(button->active, out); + + export_color(button->border_color, out); + export_float(button->color_factor_background, out); + + export_color(button->text_background, out); + export_color(button->text_normal, out); + export_color(button->text_hover, out); + export_color(button->text_active, out); + /*TODO nk_flags text_alignment*/ + export_float(button->color_factor_text, out); + + export_float(button->border, out); + export_float(button->rounding, out); + export_vec2(button->padding, out); + export_vec2(button->image_padding, out); + export_vec2(button->touch_padding, out); + export_float(button->disabled_factor, out); + + /* unused but need to exist for C code */ + /* userdata, draw_begin, draw_end */ + fprintf(out, "{ 0 },\nNULL,\nNULL\n"); + + + fputs("},\n", out); +} + +static void +export_toggle_style(struct nk_style_toggle* toggle, FILE* out) +{ + fputs("{\n", out); + export_style_item_color(toggle->normal, out); + export_style_item_color(toggle->hover, out); + export_style_item_color(toggle->active, out); + + export_color(toggle->border_color, out); + + /* cursor_normal, cursor_hover */ + struct nk_style_item tmp = { 0 }; + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + + /* kind of annoying the order changes across structures */ + export_color(toggle->text_normal, out); + export_color(toggle->text_hover, out); + export_color(toggle->text_active, out); + export_color(toggle->text_background, out); + /*TODO nk_flags text_alignment*/ + + export_vec2(toggle->padding, out); + export_vec2(toggle->touch_padding, out); + export_float(toggle->spacing, out); + export_float(toggle->border, out); + export_float(toggle->color_factor, out); + export_float(toggle->disabled_factor, out); + + /* unused but need to exist for C code */ + /* userdata, draw_begin, draw_end */ + fprintf(out, "{ 0 },\nNULL,\nNULL\n"); + + fputs("},\n", out); +} - export_button_style(&style->button); +static void +export_selectable_style(struct nk_style_selectable* selectable, FILE* out) +{ + fputs("{\n", out); - // TODO reuse export_button_style? - export_contextual_button_style(&style->contextual_button); - export_menu_button_style(&style->menu_button); + /* background inactive */ + export_style_item_color(selectable->normal, out); + export_style_item_color(selectable->hover, out); + export_style_item_color(selectable->pressed, out); - export_checkbox_style(&style->checkbox); + /* background active */ + export_style_item_color(selectable->normal_active, out); + export_style_item_color(selectable->hover_active, out); + export_style_item_color(selectable->pressed_active, out); - export_option_style(&style->option); + /* text inactive */ + export_color(selectable->text_normal, out); + export_color(selectable->text_hover, out); + export_color(selectable->text_pressed, out); - export_selectable_style(&style->selectable); + /* text active */ + export_color(selectable->text_normal_active, out); + export_color(selectable->text_hover_active, out); + export_color(selectable->text_pressed_active, out); - // Sliders - export_slider_style(&style->slider); - // slider buttons inside slider? - export_button_style(&style->slider.inc_button); - export_button_style(&style->slider.dec_button); + export_color(selectable->text_background, out); - export_knob_style(&style->knob); + /*TODO nk_flags text_alignment*/ - export_progressbar_style(&style->progress); + /* properties */ + export_float(selectable->rounding, out); + export_vec2(selectable->padding, out); + export_vec2(selectable->touch_padding, out); + export_vec2(selectable->image_padding, out); + export_float(selectable->color_factor, out); + export_float(selectable->disabled_factor, out); - // TODO scroll bar stuff - export_scrollbar_style(&style->scrollh); - export_scrollbar_style(&style->scrollv); + /* unused but need to exist for C code */ + /* userdata, draw_begin, draw_end */ + fprintf(out, "{ 0 },\nNULL,\nNULL\n"); - export_button_style(&style->scrollh.inc_button); - export_button_style(&style->scrollh.dec_button); - export_button_style(&style->scrollv.inc_button); - export_button_style(&style->scrollv.dec_button); + fputs("},\n", out); +} +static void +export_slider_style(struct nk_style_slider* slider, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(slider->normal, out); + export_style_item_color(slider->hover, out); + export_style_item_color(slider->active, out); + export_color(slider->border_color, out); + + /* background bar */ + export_color(slider->bar_normal, out); + export_color(slider->bar_hover, out); + export_color(slider->bar_active, out); + export_color(slider->bar_filled, out); + + /* cursor_normal, cursor_hover, cursor_active */ + struct nk_style_item tmp = { 0 }; + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + + /* properties */ + export_float(slider->border, out); + export_float(slider->rounding, out); + export_float(slider->bar_height, out); + export_vec2(slider->padding, out); + export_vec2(slider->spacing, out); + export_vec2(slider->cursor_size, out); + export_float(slider->color_factor, out); + export_float(slider->disabled_factor, out); + + /* optional buttons */ + /* export_bool? */ + export_int(slider->show_buttons, out); + + export_button_style(&slider->inc_button, out); + export_button_style(&slider->dec_button, out); + /* int for enums too for now, enums later for better + * human readability */ + export_int(slider->inc_symbol, out); + export_int(slider->dec_symbol, out); + + /* unused but need to exist for C code */ + /* userdata, draw_begin, draw_end */ + fprintf(out, "{ 0 },\nNULL,\nNULL\n"); + + fputs("},\n", out); +} - export_edit_style(&style->edit); +static void +export_knob_style(struct nk_style_knob* knob, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(knob->normal, out); + export_style_item_color(knob->hover, out); + export_style_item_color(knob->active, out); + export_color(knob->border_color, out); + + /* knob */ + export_color(knob->knob_normal, out); + export_color(knob->knob_hover, out); + export_color(knob->knob_active, out); + export_color(knob->knob_border_color, out); + + /* cursor_normal, cursor_hover, cursor_active */ + struct nk_style_item tmp = { 0 }; + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + + /* properties */ + export_float(knob->border, out); + export_float(knob->knob_border, out); + export_vec2(knob->padding, out); + export_vec2(knob->spacing, out); + export_float(knob->cursor_width, out); + export_float(knob->color_factor, out); + export_float(knob->disabled_factor, out); + + /* unused but need to exist for C code */ + /* userdata, draw_begin, draw_end */ + fprintf(out, "{ 0 },\nNULL,\nNULL\n"); + + fputs("},\n", out); +} + +static void +export_progress_style(struct nk_style_progress* progress, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(progress->normal, out); + export_style_item_color(progress->hover, out); + export_style_item_color(progress->active, out); + export_color(progress->border_color, out); + + /* cursor_normal, cursor_hover, cursor_active */ + struct nk_style_item tmp = { 0 }; + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + + /* properties */ + export_float(progress->rounding, out); + export_float(progress->border, out); + export_float(progress->cursor_border, out); + export_float(progress->cursor_rounding, out); + export_vec2(progress->padding, out); + export_float(progress->color_factor, out); + export_float(progress->disabled_factor, out); + + /* unused but need to exist for C code */ + /* userdata, draw_begin, draw_end */ + fprintf(out, "{ 0 },\nNULL,\nNULL\n"); + + fputs("},\n", out); +} - // TODO property stuff - export_property_style(&style->property); - export_button_style(&style->property.dec_button); - export_button_style(&style->property.inc_button); - export_edit_style(&style->property.edit); +static void +export_scrollbar_style(struct nk_style_scrollbar* scroll, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(scroll->normal, out); + export_style_item_color(scroll->hover, out); + export_style_item_color(scroll->active, out); + export_color(scroll->border_color, out); + + /* cursor_normal, cursor_hover, cursor_active */ + struct nk_style_item tmp = { 0 }; + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + export_style_item_color(tmp, out); + + /* properties */ + export_float(scroll->border, out); + export_float(scroll->rounding, out); + export_float(scroll->border_cursor, out); + export_float(scroll->rounding_cursor, out); + export_vec2(scroll->padding, out); + export_float(scroll->color_factor, out); + export_float(scroll->disabled_factor, out); + + /* optional buttons */ + /* export_bool? */ + export_int(scroll->show_buttons, out); + + export_button_style(&scroll->inc_button, out); + export_button_style(&scroll->dec_button, out); + /* int for enums too for now, enums later for better + * human readability */ + export_int(scroll->inc_symbol, out); + export_int(scroll->dec_symbol, out); + + /* unused but need to exist for C code */ + /* userdata, draw_begin, draw_end */ + fprintf(out, "{ 0 },\nNULL,\nNULL\n"); + + fputs("},\n", out); +} - export_chart_style(&style->chart); +static void +export_edit_style(struct nk_style_edit* edit, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(edit->normal, out); + export_style_item_color(edit->hover, out); + export_style_item_color(edit->active, out); + export_color(edit->border_color, out); + export_scrollbar_style(&edit->scrollbar, out); + + /* cursor */ + export_color(edit->cursor_normal, out); + export_color(edit->cursor_hover, out); + export_color(edit->cursor_text_normal, out); + export_color(edit->cursor_text_hover, out); + + /* text unselected */ + export_color(edit->text_normal, out); + export_color(edit->text_hover, out); + export_color(edit->text_active, out); + + /* text selected */ + export_color(edit->selected_normal, out); + export_color(edit->selected_hover, out); + export_color(edit->selected_text_normal, out); + export_color(edit->selected_text_hover, out); + + /* properties */ + export_float(edit->border, out); + export_float(edit->rounding, out); + export_float(edit->cursor_size, out); + export_vec2(edit->scrollbar_size, out); + export_vec2(edit->padding, out); + export_float(edit->row_padding, out); + export_float(edit->color_factor, out); + export_float(edit->disabled_factor, out); + + fputs("},\n", out); +} - // combo - export_combo_style(&style->combo); - export_button_style(&style->combo.button); - // tab - export_tab_style(&style->tab); - export_button_style(&style->tab.tab_minimize_button); - export_button_style(&style->tab.tab_maximize_button); +static void +export_property_style(struct nk_style_property* property, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(property->normal, out); + export_style_item_color(property->hover, out); + export_style_item_color(property->active, out); + export_color(property->border_color, out); + + /* text */ + export_color(property->label_normal, out); + export_color(property->label_hover, out); + export_color(property->label_active, out); + + /* symbols */ + export_int(property->sym_left, out); + export_int(property->sym_right, out); + + /* properties */ + export_float(property->border, out); + export_float(property->rounding, out); + export_vec2(property->padding, out); + export_float(property->color_factor, out); + export_float(property->disabled_factor, out); + + /* TODO style_edit */ + export_edit_style(&property->edit, out); + export_button_style(&property->inc_button, out); + export_button_style(&property->dec_button, out); + + /* unused but need to exist for C code */ + /* userdata, draw_begin, draw_end */ + fprintf(out, "{ 0 },\nNULL,\nNULL\n"); + + fputs("},\n", out); +} - export_button_style(&style->tab.node_minimize_button); - export_button_style(&style->tab.node_maximize_button); +static void +export_chart_style(struct nk_style_chart* chart, FILE* out) +{ + fputs("{\n", out); + + /* colors */ + export_style_item_color(chart->background, out); + export_color(chart->border_color, out); + export_color(chart->selected_color, out); + export_color(chart->color, out); + + /* properties */ + export_float(chart->border, out); + export_float(chart->rounding, out); + export_vec2(chart->padding, out); + export_float(chart->color_factor, out); + export_float(chart->disabled_factor, out); + /* bool */ + export_int(chart->show_markers, out); + + fputs("},\n", out); +} - // window - export_window_header_style(&style->window.header); - export_button_style(&style->window.header.close_button); - export_button_style(&style->window.header.minimize_button); - export_window_style(&style->window); - */ +static void +export_tab_style(struct nk_style_tab* tab, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(tab->background, out); + export_color(tab->border_color, out); + export_color(tab->text, out); + + /* button */ + export_button_style(&tab->tab_maximize_button, out); + export_button_style(&tab->tab_minimize_button, out); + export_button_style(&tab->node_maximize_button, out); + export_button_style(&tab->node_minimize_button, out); + export_int(tab->sym_minimize, out); + export_int(tab->sym_maximize, out); + + /* properties */ + export_float(tab->border, out); + export_float(tab->rounding, out); + export_float(tab->indent, out); + export_vec2(tab->padding, out); + export_vec2(tab->spacing, out); + export_float(tab->color_factor, out); + export_float(tab->disabled_factor, out); + + fputs("},\n", out); } +static void +export_combo_style(struct nk_style_combo* combo, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(combo->normal, out); + export_style_item_color(combo->hover, out); + export_style_item_color(combo->active, out); + export_color(combo->border_color, out); + + /* label */ + export_color(combo->label_normal, out); + export_color(combo->label_hover, out); + export_color(combo->label_active, out); + + /* symbol */ + export_color(combo->symbol_normal, out); + export_color(combo->symbol_hover, out); + export_color(combo->symbol_active, out); + + /* button */ + export_button_style(&combo->button, out); + export_int(combo->sym_normal, out); + export_int(combo->sym_hover, out); + export_int(combo->sym_active, out); + + /* properties */ + export_float(combo->border, out); + export_float(combo->rounding, out); + export_vec2(combo->content_padding, out); + export_vec2(combo->button_padding, out); + export_vec2(combo->spacing, out); + export_float(combo->color_factor, out); + export_float(combo->disabled_factor, out); + + fputs("},\n", out); +} + +static void +export_window_header_style(struct nk_style_window_header* header, FILE* out) +{ + fputs("{\n", out); + + /* background */ + export_style_item_color(header->normal, out); + export_style_item_color(header->hover, out); + export_style_item_color(header->active, out); + + /* button */ + export_button_style(&header->close_button, out); + export_button_style(&header->minimize_button, out); + export_int(header->close_symbol, out); + export_int(header->minimize_symbol, out); + export_int(header->maximize_symbol, out); + + /* title */ + export_color(header->label_normal, out); + export_color(header->label_hover, out); + export_color(header->label_active, out); + + + /* properties */ + export_int(header->align, out); + export_vec2(header->padding, out); + export_vec2(header->label_padding, out); + export_vec2(header->spacing, out); + + fputs("},\n", out); +} + +static void +export_window_style(struct nk_style_window* win, FILE* out) +{ + fputs("{\n", out); + + export_window_header_style(&win->header, out); + export_style_item_color(win->fixed_background, out); + export_color(win->background, out); + + export_color(win->border_color, out); + export_color(win->popup_border_color, out); + export_color(win->combo_border_color, out); + export_color(win->contextual_border_color, out); + export_color(win->menu_border_color, out); + export_color(win->group_border_color, out); + export_color(win->tooltip_border_color, out); + export_style_item_color(win->scaler, out); + + /* properties */ + export_float(win->border, out); + export_float(win->combo_border, out); + export_float(win->contextual_border, out); + export_float(win->menu_border, out); + export_float(win->group_border, out); + export_float(win->tooltip_border, out); + export_float(win->popup_border, out); + export_float(win->min_row_height_padding, out); + + export_float(win->rounding, out); + export_vec2(win->spacing, out); + export_vec2(win->scrollbar_size, out); + export_vec2(win->min_size, out); + + export_vec2(win->padding, out); + export_vec2(win->group_padding, out); + export_vec2(win->popup_padding, out); + export_vec2(win->combo_padding, out); + export_vec2(win->contextual_padding, out); + export_vec2(win->menu_padding, out); + export_vec2(win->tooltip_padding, out); + + fputs("},\n", out); +} + +static void +export_styles(struct nk_context* ctx, FILE* out) +{ + struct nk_style* style; + + NK_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; + + if (!out) { + out = stdout; + } + + fputs("{\n", out); + + /* TODO handle un-saved members if we want to export full + * style struct directly as C code */ + + /* *font, *cursors[], *cursor_active, *cursor_last, int cursor visible */ + fprintf(out, "NULL, { NULL }, NULL, NULL, 0,\n"); + + export_text_style(&style->text, out); + + export_button_style(&style->button, out); + + export_button_style(&style->contextual_button, out); + export_button_style(&style->menu_button, out); + + export_toggle_style(&style->option, out); + export_toggle_style(&style->checkbox, out); + + export_selectable_style(&style->selectable, out); + export_slider_style(&style->slider, out); + export_knob_style(&style->knob, out); + export_progress_style(&style->progress, out); + + export_property_style(&style->property, out); + + export_edit_style(&style->edit, out); + export_chart_style(&style->chart, out); + + export_scrollbar_style(&style->scrollh, out); + export_scrollbar_style(&style->scrollv, out); + + export_tab_style(&style->tab, out); + export_combo_style(&style->combo, out); + + export_window_style(&style->window, out); + + fputs("};\n", out); +} + +/*TODO rgba options*/ static int -style_rgb(struct nk_context* ctx, const char* name, struct nk_color* color) +style_rgb_f(struct nk_context* ctx, const char* name, struct nk_color* color) { struct nk_colorf colorf; nk_label(ctx, name, NK_TEXT_LEFT); @@ -782,6 +1337,25 @@ style_rgb(struct nk_context* ctx, const char* name, struct nk_color* color) return 0; } +static int +style_rgb(struct nk_context* ctx, const char* name, struct nk_color* color) +{ + struct nk_colorf colorf; + nk_label(ctx, name, NK_TEXT_LEFT); + if (nk_combo_begin_color(ctx, *color, nk_vec2(nk_widget_width(ctx), 400))) { + nk_layout_row_dynamic(ctx, 120, 1); + colorf = nk_color_picker(ctx, nk_color_cf(*color), NK_RGB); + nk_layout_row_dynamic(ctx, 25, 1); + color->r = nk_propertyi(ctx, "#R:", 0, colorf.r*255, 255, 1, 1); + color->g = nk_propertyi(ctx, "#G:", 0, colorf.g*255, 255, 1, 1); + color->b = nk_propertyi(ctx, "#B:", 0, colorf.b*255, 255, 1, 1); + + nk_combo_end(ctx); + return 1; + } + return 0; +} + /* TODO style_style_item? how to handle images if at all? */ static void style_item_color(struct nk_context* ctx, const char* name, struct nk_style_item* item) @@ -1474,10 +2048,10 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ nk_layout_row_dynamic(ctx, 30, 1); if (nk_button_label(ctx, "Export global color styles")) { - export_global_color_table(ctx, color_table, nk_false); + export_global_color_table(ctx, color_table, nk_false, stdout); } if (nk_button_label(ctx, "Export global color styles with labels")) { - export_global_color_table(ctx, color_table, nk_true); + export_global_color_table(ctx, color_table, nk_true, stdout); } nk_tree_pop(ctx); @@ -1598,7 +2172,7 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ nk_style_default(ctx); } if (nk_button_label(ctx, "Export styles")) { - export_styles(ctx); + export_styles(ctx, stdout); } } From 24b7c26a0a7985b20e4a0cb95235fe75a74ccfca Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Sun, 8 Mar 2026 19:48:54 -0700 Subject: [PATCH 3/4] Keep the configurator window styling separate... ...So you can change things like padding and spacing without widgets jumping around on you. Particularly useful when dragging properties. --- demo/common/style_configurator.c | 13 +++++++++++-- demo/sdl_renderer/main.c | 9 +++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/demo/common/style_configurator.c b/demo/common/style_configurator.c index 14c60f13b..9acbc6786 100644 --- a/demo/common/style_configurator.c +++ b/demo/common/style_configurator.c @@ -1260,6 +1260,9 @@ export_window_style(struct nk_style_window* win, FILE* out) export_vec2(win->menu_padding, out); export_vec2(win->tooltip_padding, out); + export_int(win->tooltip_origin, out); + export_vec2(win->tooltip_offset, out); + fputs("},\n", out); } @@ -2017,7 +2020,7 @@ style_window(struct nk_context* ctx, struct nk_style_window* out_style) } static int -style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT]) +style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT], struct nk_style* used_style) { /* window flags */ int border = nk_true; @@ -2029,6 +2032,7 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ int minimizable = nk_true; struct nk_style *style = NULL; struct nk_style_button* dups[1]; + static struct nk_style tmp_style; /* window flags */ window_flags = 0; @@ -2039,7 +2043,10 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ if (scale_left) window_flags |= NK_WINDOW_SCALE_LEFT; if (minimizable) window_flags |= NK_WINDOW_MINIMIZABLE; - style = &ctx->style; + memcpy(&tmp_style, &ctx->style, sizeof(tmp_style)); + memcpy(&ctx->style, used_style, sizeof(tmp_style)); + + style = &tmp_style; if (nk_begin(ctx, "Configurator", nk_rect(10, 10, 400, 600), window_flags)) { @@ -2178,5 +2185,7 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ } nk_end(ctx); + + memcpy(&ctx->style, &tmp_style, sizeof(tmp_style)); return !nk_window_is_closed(ctx, "Configurator"); } diff --git a/demo/sdl_renderer/main.c b/demo/sdl_renderer/main.c index 72dae1d0d..38a979d37 100644 --- a/demo/sdl_renderer/main.c +++ b/demo/sdl_renderer/main.c @@ -90,7 +90,7 @@ main(void) #ifdef INCLUDE_CONFIGURATOR static struct nk_color color_table[NK_COLOR_COUNT]; - memcpy(color_table, nk_get_default_color_table(), sizeof(color_table)); + struct nk_style default_style; #endif /* SDL setup */ @@ -165,6 +165,11 @@ main(void) nk_style_set_font(ctx, &font->handle); } + #ifdef INCLUDE_CONFIGURATOR + memcpy(&default_style, &ctx->style, sizeof(default_style)); + memcpy(color_table, nk_get_default_color_table(), sizeof(color_table)); + #endif + bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f; while (running) { @@ -223,7 +228,7 @@ main(void) overview(ctx); #endif #ifdef INCLUDE_CONFIGURATOR - style_configurator(ctx, color_table); + style_configurator(ctx, color_table, &default_style); #endif #ifdef INCLUDE_NODE_EDITOR node_editor(ctx); From 253a8e50cb3de95f3e0195dc31f234cca8904afd Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Mon, 9 Mar 2026 20:52:17 -0700 Subject: [PATCH 4/4] Working rough exporting and loading --- demo/common/style_configurator.c | 673 ++++++++++++++++++++++++++++++- demo/sdl_renderer/Makefile | 2 +- demo/sdl_renderer/main.c | 10 +- 3 files changed, 666 insertions(+), 19 deletions(-) diff --git a/demo/common/style_configurator.c b/demo/common/style_configurator.c index 9acbc6786..2c3c7075c 100644 --- a/demo/common/style_configurator.c +++ b/demo/common/style_configurator.c @@ -39,6 +39,17 @@ export_color(struct nk_color c, FILE* out) fprintf(out, "%s,\n", nk_color2str(c)); } +static void +read_color(struct nk_color* c, FILE* in) +{ + int r, g, b, a; + fscanf(in, " { %d, %d, %d, %d },", &r, &g, &b, &a); + c->r = NK_CLAMP(0, r, 255); + c->g = NK_CLAMP(0, g, 255); + c->b = NK_CLAMP(0, b, 255); + c->a = NK_CLAMP(0, a, 255); +} + static void export_global_color_table(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT], nk_bool use_labels, FILE* out) { @@ -58,6 +69,19 @@ export_global_color_table(struct nk_context* ctx, struct nk_color color_table[NK fputs("};\n", out); } +static void load_global_color_table(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT], FILE* in) +{ + int i; + fscanf(in, " {"); + for (i=0; ix, &v->y); +} + static void export_float(float f, FILE* out) { fprintf(out, "%f,\n", f); } +static void +read_float(float* f, FILE* in) +{ + fscanf(in, " %f,\n", f); +} + static void export_int(int i, FILE* out) { fprintf(out, "%d,\n", i); } +static void +read_int(int* i, FILE* in) +{ + fscanf(in, " %d,\n", i); +} + static void export_text_style(struct nk_style_text* text, FILE* out) { @@ -1281,9 +1323,6 @@ export_styles(struct nk_context* ctx, FILE* out) fputs("{\n", out); - /* TODO handle un-saved members if we want to export full - * style struct directly as C code */ - /* *font, *cursors[], *cursor_active, *cursor_last, int cursor visible */ fprintf(out, "NULL, { NULL }, NULL, NULL, 0,\n"); @@ -1318,6 +1357,615 @@ export_styles(struct nk_context* ctx, FILE* out) fputs("};\n", out); } +/* Read style functions */ +/**********************/ +static void +read_text_style(struct nk_style_text* text, FILE* in) +{ + fscanf(in, " {\n"); + read_color(&text->color, in); + read_vec2(&text->padding, in); + read_float(&text->color_factor, in); + read_float(&text->disabled_factor, in); + fscanf(in, " },\n"); +} + +/*TODO image and 9-slice?*/ +static void +read_style_item_color(struct nk_style_item* s, FILE* in) +{ + fscanf(in, " {\n"); + /*s->type = NK_STYLE_ITEM_COLOR;*/ + fscanf(in, "%d,\n", &s->type); + fscanf(in, " {"); + read_color(&s->data.color, in); + fscanf(in, " }\n"); + fscanf(in, " },\n"); +} + +static void +read_button_style(struct nk_style_button* button, FILE* in) +{ + /*TODO maybe just do normal color convert it to style_item on read?*/ + fscanf(in, " {\n"); + read_style_item_color(&button->normal, in); + /*prouttf("%d %d %d %d\n", c->r, c->g, c->b, c->a);*/ + read_style_item_color(&button->hover, in); + read_style_item_color(&button->active, in); + + read_color(&button->border_color, in); + read_float(&button->color_factor_background, in); + + read_color(&button->text_background, in); + read_color(&button->text_normal, in); + read_color(&button->text_hover, in); + read_color(&button->text_active, in); + /*TODO nk_flags text_alignment*/ + read_float(&button->color_factor_text, in); + + read_float(&button->border, in); + read_float(&button->rounding, in); + read_vec2(&button->padding, in); + read_vec2(&button->image_padding, in); + read_vec2(&button->touch_padding, in); + read_float(&button->disabled_factor, in); + + /* unused but need to exist for C code */ + /* userdata, draw_begout, draw_end */ + fscanf(in, " { 0 },\nNULL,\nNULL\n"); + fscanf(in, " },\n"); +} + +static void +read_toggle_style(struct nk_style_toggle* toggle, FILE* in) +{ + fscanf(in, " {\n"); + read_style_item_color(&toggle->normal, in); + read_style_item_color(&toggle->hover, in); + read_style_item_color(&toggle->active, in); + + read_color(&toggle->border_color, in); + + /* cursor_normal, cursor_hover */ + struct nk_style_item tmp = { 0 }; + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + + /* koutd of annoying the order changes across structures */ + read_color(&toggle->text_normal, in); + read_color(&toggle->text_hover, in); + read_color(&toggle->text_active, in); + read_color(&toggle->text_background, in); + /*TODO nk_flags text_alignment*/ + + read_vec2(&toggle->padding, in); + read_vec2(&toggle->touch_padding, in); + read_float(&toggle->spacing, in); + read_float(&toggle->border, in); + read_float(&toggle->color_factor, in); + read_float(&toggle->disabled_factor, in); + + /* unused but need to exist for C code */ + /* userdata, draw_begout, draw_end */ + fscanf(in, " { 0 },\nNULL,\nNULL\n"); + fscanf(in, " },\n"); +} + +static void +read_selectable_style(struct nk_style_selectable* selectable, FILE* in) +{ + fscanf(in, " {\n"); + + /* background outactive */ + read_style_item_color(&selectable->normal, in); + read_style_item_color(&selectable->hover, in); + read_style_item_color(&selectable->pressed, in); + + /* background active */ + read_style_item_color(&selectable->normal_active, in); + read_style_item_color(&selectable->hover_active, in); + read_style_item_color(&selectable->pressed_active, in); + + /* text outactive */ + read_color(&selectable->text_normal, in); + read_color(&selectable->text_hover, in); + read_color(&selectable->text_pressed, in); + + /* text active */ + read_color(&selectable->text_normal_active, in); + read_color(&selectable->text_hover_active, in); + read_color(&selectable->text_pressed_active, in); + + read_color(&selectable->text_background, in); + + /*TODO nk_flags text_alignment*/ + + /* properties */ + read_float(&selectable->rounding, in); + read_vec2(&selectable->padding, in); + read_vec2(&selectable->touch_padding, in); + read_vec2(&selectable->image_padding, in); + read_float(&selectable->color_factor, in); + read_float(&selectable->disabled_factor, in); + + /* unused but need to exist for C code */ + /* userdata, draw_begout, draw_end */ + fscanf(in, " { 0 },\nNULL,\nNULL\n"); + fscanf(in, " },\n"); +} + +static void +read_slider_style(struct nk_style_slider* slider, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&slider->normal, in); + read_style_item_color(&slider->hover, in); + read_style_item_color(&slider->active, in); + read_color(&slider->border_color, in); + + /* background bar */ + read_color(&slider->bar_normal, in); + read_color(&slider->bar_hover, in); + read_color(&slider->bar_active, in); + read_color(&slider->bar_filled, in); + + /* cursor_normal, cursor_hover, cursor_active */ + struct nk_style_item tmp = { 0 }; + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + + /* properties */ + read_float(&slider->border, in); + read_float(&slider->rounding, in); + read_float(&slider->bar_height, in); + read_vec2(&slider->padding, in); + read_vec2(&slider->spacing, in); + read_vec2(&slider->cursor_size, in); + read_float(&slider->color_factor, in); + read_float(&slider->disabled_factor, in); + + /* optional buttons */ + /* read_bool? */ + read_int(&slider->show_buttons, in); + + read_button_style(&slider->inc_button, in); + read_button_style(&slider->dec_button, in); + /* outt for enums too for now, enums later for better + * human readability */ + read_int(&slider->inc_symbol, in); + read_int(&slider->dec_symbol, in); + + /* unused but need to exist for C code */ + /* userdata, draw_begout, draw_end */ + fscanf(in, " { 0 },\nNULL,\nNULL\n"); + fscanf(in, " },\n"); +} + +static void +read_knob_style(struct nk_style_knob* knob, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&knob->normal, in); + read_style_item_color(&knob->hover, in); + read_style_item_color(&knob->active, in); + read_color(&knob->border_color, in); + + /* knob */ + read_color(&knob->knob_normal, in); + read_color(&knob->knob_hover, in); + read_color(&knob->knob_active, in); + read_color(&knob->knob_border_color, in); + + /* cursor_normal, cursor_hover, cursor_active */ + struct nk_style_item tmp = { 0 }; + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + + /* properties */ + read_float(&knob->border, in); + read_float(&knob->knob_border, in); + read_vec2(&knob->padding, in); + read_vec2(&knob->spacing, in); + read_float(&knob->cursor_width, in); + read_float(&knob->color_factor, in); + read_float(&knob->disabled_factor, in); + + /* unused but need to exist for C code */ + /* userdata, draw_begout, draw_end */ + fscanf(in, " { 0 },\nNULL,\nNULL\n"); + fscanf(in, " },\n"); +} + +static void +read_progress_style(struct nk_style_progress* progress, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&progress->normal, in); + read_style_item_color(&progress->hover, in); + read_style_item_color(&progress->active, in); + read_color(&progress->border_color, in); + + /* cursor_normal, cursor_hover, cursor_active */ + struct nk_style_item tmp = { 0 }; + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + + /* properties */ + read_float(&progress->rounding, in); + read_float(&progress->border, in); + read_float(&progress->cursor_border, in); + read_float(&progress->cursor_rounding, in); + read_vec2(&progress->padding, in); + read_float(&progress->color_factor, in); + read_float(&progress->disabled_factor, in); + + /* unused but need to exist for C code */ + /* userdata, draw_begout, draw_end */ + fscanf(in, " { 0 },\nNULL,\nNULL\n"); + fscanf(in, " },\n"); +} + +static void +read_scrollbar_style(struct nk_style_scrollbar* scroll, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&scroll->normal, in); + read_style_item_color(&scroll->hover, in); + read_style_item_color(&scroll->active, in); + read_color(&scroll->border_color, in); + + /* cursor_normal, cursor_hover, cursor_active */ + struct nk_style_item tmp = { 0 }; + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + read_style_item_color(&tmp, in); + + /* properties */ + read_float(&scroll->border, in); + read_float(&scroll->rounding, in); + read_float(&scroll->border_cursor, in); + read_float(&scroll->rounding_cursor, in); + read_vec2(&scroll->padding, in); + read_float(&scroll->color_factor, in); + read_float(&scroll->disabled_factor, in); + + /* optional buttons */ + /* read_bool? */ + read_int(&scroll->show_buttons, in); + + read_button_style(&scroll->inc_button, in); + read_button_style(&scroll->dec_button, in); + /* outt for enums too for now, enums later for better + * human readability */ + read_int(&scroll->inc_symbol, in); + read_int(&scroll->dec_symbol, in); + + /* unused but need to exist for C code */ + /* userdata, draw_begout, draw_end */ + fscanf(in, " { 0 },\nNULL,\nNULL\n"); + fscanf(in, " },\n"); +} + + +static void +read_edit_style(struct nk_style_edit* edit, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&edit->normal, in); + read_style_item_color(&edit->hover, in); + read_style_item_color(&edit->active, in); + read_color(&edit->border_color, in); + read_scrollbar_style(&edit->scrollbar, in); + + /* cursor */ + read_color(&edit->cursor_normal, in); + read_color(&edit->cursor_hover, in); + read_color(&edit->cursor_text_normal, in); + read_color(&edit->cursor_text_hover, in); + + /* text unselected */ + read_color(&edit->text_normal, in); + read_color(&edit->text_hover, in); + read_color(&edit->text_active, in); + + /* text selected */ + read_color(&edit->selected_normal, in); + read_color(&edit->selected_hover, in); + read_color(&edit->selected_text_normal, in); + read_color(&edit->selected_text_hover, in); + + /* properties */ + read_float(&edit->border, in); + read_float(&edit->rounding, in); + read_float(&edit->cursor_size, in); + read_vec2(&edit->scrollbar_size, in); + read_vec2(&edit->padding, in); + read_float(&edit->row_padding, in); + read_float(&edit->color_factor, in); + read_float(&edit->disabled_factor, in); + + fscanf(in, " },\n"); +} + + +static void +read_property_style(struct nk_style_property* property, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&property->normal, in); + read_style_item_color(&property->hover, in); + read_style_item_color(&property->active, in); + read_color(&property->border_color, in); + + /* text */ + read_color(&property->label_normal, in); + read_color(&property->label_hover, in); + read_color(&property->label_active, in); + + /* symbols */ + read_int(&property->sym_left, in); + read_int(&property->sym_right, in); + + /* properties */ + read_float(&property->border, in); + read_float(&property->rounding, in); + read_vec2(&property->padding, in); + read_float(&property->color_factor, in); + read_float(&property->disabled_factor, in); + + /* TODO style_edit */ + read_edit_style(&property->edit, in); + read_button_style(&property->inc_button, in); + read_button_style(&property->dec_button, in); + + /* unused but need to exist for C code */ + /* userdata, draw_begout, draw_end */ + fscanf(in, " { 0 },\nNULL,\nNULL\n"); + fscanf(in, " },\n"); +} + +static void +read_chart_style(struct nk_style_chart* chart, FILE* in) +{ + fscanf(in, " {\n"); + + /* colors */ + read_style_item_color(&chart->background, in); + read_color(&chart->border_color, in); + read_color(&chart->selected_color, in); + read_color(&chart->color, in); + + /* properties */ + read_float(&chart->border, in); + read_float(&chart->rounding, in); + read_vec2(&chart->padding, in); + read_float(&chart->color_factor, in); + read_float(&chart->disabled_factor, in); + /* bool */ + read_int(&chart->show_markers, in); + + fscanf(in, " },\n"); +} + +static void +read_tab_style(struct nk_style_tab* tab, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&tab->background, in); + read_color(&tab->border_color, in); + read_color(&tab->text, in); + + /* button */ + read_button_style(&tab->tab_maximize_button, in); + read_button_style(&tab->tab_minimize_button, in); + read_button_style(&tab->node_maximize_button, in); + read_button_style(&tab->node_minimize_button, in); + read_int(&tab->sym_minimize, in); + read_int(&tab->sym_maximize, in); + + /* properties */ + read_float(&tab->border, in); + read_float(&tab->rounding, in); + read_float(&tab->indent, in); + read_vec2(&tab->padding, in); + read_vec2(&tab->spacing, in); + read_float(&tab->color_factor, in); + read_float(&tab->disabled_factor, in); + + fscanf(in, " },\n"); +} + +static void +read_combo_style(struct nk_style_combo* combo, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&combo->normal, in); + read_style_item_color(&combo->hover, in); + read_style_item_color(&combo->active, in); + read_color(&combo->border_color, in); + + /* label */ + read_color(&combo->label_normal, in); + read_color(&combo->label_hover, in); + read_color(&combo->label_active, in); + + /* symbol */ + read_color(&combo->symbol_normal, in); + read_color(&combo->symbol_hover, in); + read_color(&combo->symbol_active, in); + + /* button */ + read_button_style(&combo->button, in); + read_int(&combo->sym_normal, in); + read_int(&combo->sym_hover, in); + read_int(&combo->sym_active, in); + + /* properties */ + read_float(&combo->border, in); + read_float(&combo->rounding, in); + read_vec2(&combo->content_padding, in); + read_vec2(&combo->button_padding, in); + read_vec2(&combo->spacing, in); + read_float(&combo->color_factor, in); + read_float(&combo->disabled_factor, in); + + fscanf(in, " },\n"); +} + +static void +read_window_header_style(struct nk_style_window_header* header, FILE* in) +{ + fscanf(in, " {\n"); + + /* background */ + read_style_item_color(&header->normal, in); + read_style_item_color(&header->hover, in); + read_style_item_color(&header->active, in); + + /* button */ + read_button_style(&header->close_button, in); + read_button_style(&header->minimize_button, in); + read_int(&header->close_symbol, in); + read_int(&header->minimize_symbol, in); + read_int(&header->maximize_symbol, in); + + /* title */ + read_color(&header->label_normal, in); + read_color(&header->label_hover, in); + read_color(&header->label_active, in); + + + /* properties */ + read_int(&header->align, in); + read_vec2(&header->padding, in); + read_vec2(&header->label_padding, in); + read_vec2(&header->spacing, in); + + fscanf(in, " },\n"); +} + +static void +read_window_style(struct nk_style_window* win, FILE* in) +{ + fscanf(in, " {\n"); + + read_window_header_style(&win->header, in); + read_style_item_color(&win->fixed_background, in); + read_color(&win->background, in); + + read_color(&win->border_color, in); + read_color(&win->popup_border_color, in); + read_color(&win->combo_border_color, in); + read_color(&win->contextual_border_color, in); + read_color(&win->menu_border_color, in); + read_color(&win->group_border_color, in); + read_color(&win->tooltip_border_color, in); + read_style_item_color(&win->scaler, in); + + /* properties */ + read_float(&win->border, in); + read_float(&win->combo_border, in); + read_float(&win->contextual_border, in); + read_float(&win->menu_border, in); + read_float(&win->group_border, in); + read_float(&win->tooltip_border, in); + read_float(&win->popup_border, in); + read_float(&win->min_row_height_padding, in); + + read_float(&win->rounding, in); + read_vec2(&win->spacing, in); + read_vec2(&win->scrollbar_size, in); + read_vec2(&win->min_size, in); + + read_vec2(&win->padding, in); + read_vec2(&win->group_padding, in); + read_vec2(&win->popup_padding, in); + read_vec2(&win->combo_padding, in); + read_vec2(&win->contextual_padding, in); + read_vec2(&win->menu_padding, in); + read_vec2(&win->tooltip_padding, in); + + read_int(&win->tooltip_origin, in); + read_vec2(&win->tooltip_offset, in); + + fscanf(in, " },\n"); +} + +static void +read_styles(struct nk_context* ctx, FILE* in) +{ + struct nk_style* style; + + NK_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; + + if (!in) { + in = stdin; + } + + fscanf(in, " {\n"); + + /* *font, *cursors[], *cursor_active, *cursor_last, outt cursor visible */ + fscanf(in, " NULL, { NULL }, NULL, NULL, 0,\n"); + + read_text_style(&style->text, in); + + read_button_style(&style->button, in); + + read_button_style(&style->contextual_button, in); + read_button_style(&style->menu_button, in); + + read_toggle_style(&style->option, in); + read_toggle_style(&style->checkbox, in); + + read_selectable_style(&style->selectable, in); + read_slider_style(&style->slider, in); + read_knob_style(&style->knob, in); + read_progress_style(&style->progress, in); + + read_property_style(&style->property, in); + + read_edit_style(&style->edit, in); + read_chart_style(&style->chart, in); + + read_scrollbar_style(&style->scrollh, in); + read_scrollbar_style(&style->scrollv, in); + + read_tab_style(&style->tab, in); + read_combo_style(&style->combo, in); + + read_window_style(&style->window, in); + + fscanf(in, " };\n"); +} + + + + + + + + /*TODO rgba options*/ static int style_rgb_f(struct nk_context* ctx, const char* name, struct nk_color* color) @@ -2020,7 +2668,7 @@ style_window(struct nk_context* ctx, struct nk_style_window* out_style) } static int -style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT], struct nk_style* used_style) +style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_COUNT]) { /* window flags */ int border = nk_true; @@ -2032,7 +2680,6 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ int minimizable = nk_true; struct nk_style *style = NULL; struct nk_style_button* dups[1]; - static struct nk_style tmp_style; /* window flags */ window_flags = 0; @@ -2043,10 +2690,7 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ if (scale_left) window_flags |= NK_WINDOW_SCALE_LEFT; if (minimizable) window_flags |= NK_WINDOW_MINIMIZABLE; - memcpy(&tmp_style, &ctx->style, sizeof(tmp_style)); - memcpy(&ctx->style, used_style, sizeof(tmp_style)); - - style = &tmp_style; + style =&ctx->style; if (nk_begin(ctx, "Configurator", nk_rect(10, 10, 400, 600), window_flags)) { @@ -2060,6 +2704,9 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ if (nk_button_label(ctx, "Export global color styles with labels")) { export_global_color_table(ctx, color_table, nk_true, stdout); } + if (nk_button_label(ctx, "Load global color styles")) { + load_global_color_table(ctx, color_table, stdin); + } nk_tree_pop(ctx); } @@ -2178,14 +2825,18 @@ style_configurator(struct nk_context* ctx, struct nk_color color_table[NK_COLOR_ memcpy(color_table, nk_default_color_style, sizeof(nk_default_color_style)); nk_style_default(ctx); } - if (nk_button_label(ctx, "Export styles")) { + if (nk_button_label(ctx, "Export colors and styles")) { + export_global_color_table(ctx, color_table, nk_false, stdout); export_styles(ctx, stdout); } + if (nk_button_label(ctx, "Load colors and styles")) { + load_global_color_table(ctx, color_table, stdin); + read_styles(ctx, stdin); + } } nk_end(ctx); - memcpy(&ctx->style, &tmp_style, sizeof(tmp_style)); return !nk_window_is_closed(ctx, "Configurator"); } diff --git a/demo/sdl_renderer/Makefile b/demo/sdl_renderer/Makefile index b3efdb8c5..14146eec6 100644 --- a/demo/sdl_renderer/Makefile +++ b/demo/sdl_renderer/Makefile @@ -2,7 +2,7 @@ BIN = demo # Flags -CFLAGS += -std=c89 -Wall -Wextra -pedantic -O2 -DSDL_DISABLE_IMMINTRIN_H +CFLAGS += -std=c89 -Wall -Wextra -pedantic -g -DSDL_DISABLE_IMMINTRIN_H CFLAGS += `sdl2-config --cflags` SRC = main.c diff --git a/demo/sdl_renderer/main.c b/demo/sdl_renderer/main.c index 38a979d37..c3ce24cff 100644 --- a/demo/sdl_renderer/main.c +++ b/demo/sdl_renderer/main.c @@ -90,7 +90,8 @@ main(void) #ifdef INCLUDE_CONFIGURATOR static struct nk_color color_table[NK_COLOR_COUNT]; - struct nk_style default_style; + memcpy(color_table, nk_get_default_color_table(), sizeof(color_table)); + #endif /* SDL setup */ @@ -165,11 +166,6 @@ main(void) nk_style_set_font(ctx, &font->handle); } - #ifdef INCLUDE_CONFIGURATOR - memcpy(&default_style, &ctx->style, sizeof(default_style)); - memcpy(color_table, nk_get_default_color_table(), sizeof(color_table)); - #endif - bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f; while (running) { @@ -228,7 +224,7 @@ main(void) overview(ctx); #endif #ifdef INCLUDE_CONFIGURATOR - style_configurator(ctx, color_table, &default_style); + style_configurator(ctx, color_table); #endif #ifdef INCLUDE_NODE_EDITOR node_editor(ctx);