1010#include "../../nuklear_console.h"
1111
1212// Demo
13- static struct nk_console * console = NULL ;
1413static struct nk_gamepads gamepads ;
1514static nk_bool shouldClose = nk_false ;
1615
17- // Theme
18- static int theme = THEME_DRACULA ;
19-
20- // Progress
21- static nk_size progressValue = 50 ;
22-
23- // Combobox
24- static int weapon = 1 ;
25-
26- // Property
27- static int property_int_test = 20 ;
28- static float property_float_test = 0.4f ;
29-
30- // Slider
31- static int slider_int_test = 20 ;
32- static float slider_float_test = 0.4f ;
33-
34- // Radio
35- static int radio_option = 1 ;
36- static int radio_option2 = 0 ;
37-
38- // Checkbox
39- static nk_bool checkbox1 = nk_false ;
40- static nk_bool checkbox2 = nk_false ;
41- static nk_bool checkbox3 = nk_false ;
42- static nk_bool checkbox4 = nk_false ;
43- static nk_bool checkbox5 = nk_false ;
44- static nk_bool checkbox6 = nk_true ;
45-
46- // Messages
47- static int message_count = 0 ;
48-
49- // File
50- static char file_path_buffer [1024 ] = {0 };
51- static int file_path_buffer_size = 1024 ;
52-
53- // Textedit
54- static const int textedit_buffer_size = 256 ;
55- static char textedit_buffer [256 ] = "vurtun" ;
56-
57- // Input
58- static int gamepad_number = 0 ;
59- static enum nk_gamepad_button gamepad_button = NK_GAMEPAD_BUTTON_A ;
60-
61- // Color
62- static struct nk_colorf color = {0.31f , 1.0f , 0.48f , 1.0f };
16+ struct demo_console_state {
17+ // Theme
18+ int theme ;
19+
20+ // Progress
21+ nk_size progressValue ;
22+
23+ // Combobox
24+ int weapon ;
25+
26+ // Property
27+ int property_int_test ;
28+ float property_float_test ;
29+
30+ // Slider
31+ int slider_int_test ;
32+ float slider_float_test ;
33+
34+ // Radio
35+ int radio_option ;
36+ int radio_option2 ;
37+
38+ // Checkbox
39+ nk_bool checkbox1 ;
40+ nk_bool checkbox2 ;
41+ nk_bool checkbox3 ;
42+ nk_bool checkbox4 ;
43+ nk_bool checkbox5 ;
44+ nk_bool checkbox6 ;
45+
46+ // Messages
47+ int message_count ;
48+
49+ // File
50+ char file_path_buffer [1024 ];
51+ int file_path_buffer_size ;
52+
53+ // Textedit
54+ int textedit_buffer_size ;
55+ char textedit_buffer [256 ];
56+
57+ // Input
58+ int gamepad_number ;
59+ enum nk_gamepad_button gamepad_button ;
60+
61+ // Color
62+ struct nk_colorf color ;
63+ };
64+
65+ static struct demo_console_state demo_console_state_defaults (void ) {
66+ struct demo_console_state state = {
67+ .theme = THEME_DRACULA ,
68+ .progressValue = 50 ,
69+ .weapon = 1 ,
70+ .property_int_test = 20 ,
71+ .property_float_test = 0.4f ,
72+ .slider_int_test = 20 ,
73+ .slider_float_test = 0.4f ,
74+ .radio_option = 1 ,
75+ .radio_option2 = 0 ,
76+ .checkbox1 = nk_false ,
77+ .checkbox2 = nk_false ,
78+ .checkbox3 = nk_false ,
79+ .checkbox4 = nk_false ,
80+ .checkbox5 = nk_false ,
81+ .checkbox6 = nk_true ,
82+ .message_count = 0 ,
83+ .file_path_buffer_size = 1024 ,
84+ .textedit_buffer_size = 256 ,
85+ .color = {0.31f , 1.0f , 0.48f , 1.0f },
86+ .gamepad_number = 0 ,
87+ .gamepad_button = NK_GAMEPAD_BUTTON_A ,
88+ };
89+
90+ return state ;
91+ }
6392
6493void button_clicked (struct nk_console * button , void * user_data ) {
6594 NK_UNUSED (user_data );
@@ -69,8 +98,8 @@ void button_clicked(struct nk_console* button, void* user_data) {
6998}
7099
71100void theme_changed (struct nk_console * combobox , void * user_data ) {
72- NK_UNUSED ( user_data ) ;
73- set_style (combobox -> ctx , ( enum theme ) theme );
101+ enum theme t = * ( enum theme * ) user_data ;
102+ set_style (combobox -> ctx , t );
74103}
75104
76105void exclude_other_checkbox (nk_console * unused , void * user_data ) {
@@ -86,9 +115,9 @@ void toggle_visibility(nk_console* unused, void* user_data) {
86115}
87116
88117void nk_console_demo_show_message (struct nk_console * button , void * user_data ) {
89- NK_UNUSED ( user_data ) ;
118+ struct demo_console_state * state = ( struct demo_console_state * ) user_data ;
90119 char message [128 ];
91- snprintf (message , 128 , "This is message #%d!" , ++ message_count );
120+ snprintf (message , 128 , "This is message #%d!" , ++ state -> message_count );
92121 nk_console_show_message (button , message );
93122}
94123
@@ -97,9 +126,7 @@ void nk_console_radio_changed(struct nk_console* radio, void* user_data) {
97126 nk_console_show_message (radio , radio -> label );
98127}
99128
100- nk_console * nuklear_console_demo_init (struct nk_context * ctx , void * user_data , struct nk_image image ) {
101- console = nk_console_init (ctx );
102-
129+ 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 ) {
103130 nk_gamepad_init (& gamepads , ctx , user_data );
104131 nk_console_set_gamepads (console , & gamepads );
105132
@@ -138,22 +165,22 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s
138165
139166 nk_console * checkbox_button = nk_console_button (widgets , "Checkboxes" );
140167 {
141- nk_console_checkbox (checkbox_button , "Checkbox" , & checkbox1 )
168+ nk_console_checkbox (checkbox_button , "Checkbox" , & state -> checkbox1 )
142169 -> tooltip = "This is a checkbox!" ;
143- nk_console_checkbox (checkbox_button , "Right aligned" , & checkbox3 )
170+ nk_console_checkbox (checkbox_button , "Right aligned" , & state -> checkbox3 )
144171 -> alignment = NK_TEXT_RIGHT ;
145- nk_console_checkbox (checkbox_button , "Disabled Checkbox" , & checkbox2 )
172+ nk_console_checkbox (checkbox_button , "Disabled Checkbox" , & state -> checkbox2 )
146173 -> disabled = nk_true ;
147174
148175 // Onchange callbacks can be used to implement custom logic.
149176 // These two checkboxes disable each other when checked.
150- nk_console * exclude_a = nk_console_checkbox (checkbox_button , "Exclusive A (disables B)" , & checkbox4 );
151- nk_console * exclude_b = nk_console_checkbox (checkbox_button , "Exclusive B (disables A)" , & checkbox5 );
177+ nk_console * exclude_a = nk_console_checkbox (checkbox_button , "Exclusive A (disables B)" , & state -> checkbox4 );
178+ nk_console * exclude_b = nk_console_checkbox (checkbox_button , "Exclusive B (disables A)" , & state -> checkbox5 );
152179 nk_console_add_event_handler (exclude_a , NK_CONSOLE_EVENT_CHANGED , & exclude_other_checkbox , exclude_b , NULL );
153180 nk_console_add_event_handler (exclude_b , NK_CONSOLE_EVENT_CHANGED , & exclude_other_checkbox , exclude_a , NULL );
154181
155182 // Checkbox that will show/hide the below label.
156- nk_console * checkbox_show_label = nk_console_checkbox (checkbox_button , "Show Label" , & checkbox6 );
183+ nk_console * checkbox_show_label = nk_console_checkbox (checkbox_button , "Show Label" , & state -> checkbox6 );
157184 nk_console * label_to_show = nk_console_label (checkbox_button , "This label is only shown when the checkbox is checked." );
158185 nk_console_add_event_handler (checkbox_show_label , NK_CONSOLE_EVENT_CHANGED , & toggle_visibility , label_to_show , NULL );
159186
@@ -179,20 +206,20 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s
179206 nk_console * radios = nk_console_button (widgets , "Radios" );
180207 {
181208 nk_console_label (radios , "Option A:" );
182- nk_console_radio (radios , "Radio #1" , & radio_option );
183- nk_console_radio (radios , "Radio #2" , & radio_option );
184- nk_console_radio (radios , "Radio #3" , & radio_option );
185- nk_console_radio (radios , "Radio #4" , & radio_option );
209+ nk_console_radio (radios , "Radio #1" , & state -> radio_option );
210+ nk_console_radio (radios , "Radio #2" , & state -> radio_option );
211+ nk_console_radio (radios , "Radio #3" , & state -> radio_option );
212+ nk_console_radio (radios , "Radio #4" , & state -> radio_option );
186213
187214 nk_console_label (radios , "Option B:" );
188- nk_console_radio (radios , "Left Aligned #1" , & radio_option2 )-> alignment = NK_TEXT_LEFT ;
189- nk_console_radio (radios , "Left Aligned #2" , & radio_option2 )-> alignment = NK_TEXT_LEFT ;
190- nk_console_radio (radios , "Center Aligned #1" , & radio_option2 )-> alignment = NK_TEXT_CENTERED ;
191- nk_console_radio (radios , "Center Aligned #2" , & radio_option2 )-> alignment = NK_TEXT_CENTERED ;
192- nk_console_radio (radios , "Right Aligned #1" , & radio_option2 )-> alignment = NK_TEXT_RIGHT ;
193- nk_console_radio (radios , "Right Aligned #2" , & radio_option2 )-> alignment = NK_TEXT_RIGHT ;
194- nk_console_radio (radios , "Disabled" , & radio_option2 )-> disabled = nk_true ;
195- nk_console_radio (radios , "Invisible" , & radio_option2 )-> visible = nk_false ;
215+ nk_console_radio (radios , "Left Aligned #1" , & state -> radio_option2 )-> alignment = NK_TEXT_LEFT ;
216+ nk_console_radio (radios , "Left Aligned #2" , & state -> radio_option2 )-> alignment = NK_TEXT_LEFT ;
217+ nk_console_radio (radios , "Center Aligned #1" , & state -> radio_option2 )-> alignment = NK_TEXT_CENTERED ;
218+ nk_console_radio (radios , "Center Aligned #2" , & state -> radio_option2 )-> alignment = NK_TEXT_CENTERED ;
219+ nk_console_radio (radios , "Right Aligned #1" , & state -> radio_option2 )-> alignment = NK_TEXT_RIGHT ;
220+ nk_console_radio (radios , "Right Aligned #2" , & state -> radio_option2 )-> alignment = NK_TEXT_RIGHT ;
221+ nk_console_radio (radios , "Disabled" , & state -> radio_option2 )-> disabled = nk_true ;
222+ nk_console_radio (radios , "Invisible" , & state -> radio_option2 )-> visible = nk_false ;
196223
197224 nk_console_button_onclick (radios , "Back" , & nk_console_button_back );
198225 }
@@ -239,56 +266,56 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s
239266 // Progress Bar
240267 nk_console * progressbar = nk_console_button (widgets , "Progress Bar" );
241268 {
242- nk_console_progress (progressbar , "Progress" , & progressValue , 100 );
269+ nk_console_progress (progressbar , "Progress" , & state -> progressValue , 100 );
243270 nk_console_button_onclick (progressbar , "Back" , & nk_console_button_back );
244271 }
245272
246273 // Input: From any gamepad (-1)
247- nk_console_input (widgets , "Input Button" , -1 , & gamepad_number , & gamepad_button );
274+ nk_console_input (widgets , "Input Button" , -1 , & state -> gamepad_number , & state -> gamepad_button );
248275
249276 // Combobox
250- nk_console_combobox (widgets , "ComboBox" , "Fists;Chainsaw;Pistol;Shotgun;Chaingun" , ';' , & weapon )
277+ nk_console_combobox (widgets , "ComboBox" , "Fists;Chainsaw;Pistol;Shotgun;Chaingun" , ';' , & state -> weapon )
251278 -> tooltip = "Choose a weapon! The chainsaw is the best!" ;
252279
253280 // Property
254281 nk_console * properties = nk_console_button (widgets , "Property" );
255282 {
256- nk_console_property_int (properties , "Property Int" , 10 , & property_int_test , 30 , 1 , 1 );
257- nk_console_property_float (properties , "Property Float" , 0.0f , & property_float_test , 2.0f , 0.1f , 1 );
283+ nk_console_property_int (properties , "Property Int" , 10 , & state -> property_int_test , 30 , 1 , 1 );
284+ nk_console_property_float (properties , "Property Float" , 0.0f , & state -> property_float_test , 2.0f , 0.1f , 1 );
258285 nk_console_button_onclick (properties , "Back" , & nk_console_button_back );
259286 }
260287
261288 // Sliders
262289 nk_console * sliders = nk_console_button (widgets , "Sliders" );
263290 {
264- 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." ;
265- nk_console_slider_int (sliders , "Slider Int" , 0 , & slider_int_test , 20 , 1 );
291+ 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." ;
292+ nk_console_slider_int (sliders , "Slider Int" , 0 , & state -> slider_int_test , 20 , 1 );
266293 nk_console_button_onclick (sliders , "Back" , & nk_console_button_back );
267294 }
268295
269296 // Textedit
270- nk_console * textedit = nk_console_textedit (widgets , "Username" , textedit_buffer , textedit_buffer_size );
297+ nk_console * textedit = nk_console_textedit (widgets , "Username" , state -> textedit_buffer , state -> textedit_buffer_size );
271298 nk_console_set_tooltip (textedit , "Enter your username!" );
272299
273300 // Color
274- nk_console_color (widgets , "Select Color" , & color , NK_RGBA );
301+ nk_console_color (widgets , "Select Color" , & state -> color , NK_RGBA );
275302
276303 // File
277- nk_console_file (widgets , "File" , file_path_buffer , file_path_buffer_size );
304+ nk_console_file (widgets , "File" , state -> file_path_buffer , state -> file_path_buffer_size );
278305
279306 // Messages
280- nk_console_button_onclick (widgets , "Show Message" , & nk_console_demo_show_message );
307+ nk_console_button_onclick_handler (widgets , "Show Message" , & nk_console_demo_show_message , & state , NULL );
281308
282309 // Back Button
283310 nk_console_button_set_symbol (
284311 nk_console_button_onclick (widgets , "Back" , & nk_console_button_back ),
285312 NK_SYMBOL_TRIANGLE_LEFT );
286313 }
287314
288- nk_console * theme_options = nk_console_combobox (console , "Theme" , "Black;White;Red;Blue;Dark;Dracula;Default" , ';' , & theme );
289- nk_console_add_event (theme_options , NK_CONSOLE_EVENT_CHANGED , & theme_changed );
315+ nk_console * theme_options = nk_console_combobox (console , "Theme" , "Black;White;Red;Blue;Dark;Dracula;Default" , ';' , & state -> theme );
316+ nk_console_add_event_handler (theme_options , NK_CONSOLE_EVENT_CHANGED , & theme_changed , & state -> theme , NULL );
290317 theme_options -> tooltip = "Change the theme of the console!" ;
291- set_style (ctx , (enum theme )theme );
318+ set_style (ctx , (enum theme )state -> theme );
292319
293320 // Rows
294321 nk_console * calc = nk_console_button (console , "Calculator" );
@@ -342,17 +369,38 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s
342369
343370 nk_console_button (console , "Save Game" )-> disabled = nk_true ;
344371 nk_console_button_onclick (console , "Quit Game" , & button_clicked );
345-
346- return console ;
347372}
348373
349- nk_bool nuklear_console_demo_render () {
374+ nk_bool nuklear_console_demo_render (nk_console * console ) {
350375 nk_console_render (console );
351376
352- return shouldClose ;;
377+ return shouldClose ;
353378}
354379
355- void nuklear_console_demo_free () {
380+ nk_bool nuklear_console_demo_render_window (nk_console * console , int num , int width , int height , int flags ) {
381+ char title [18 ];
382+ snprintf (title , sizeof (title ), "nuklear_console_%d" , num );
383+
384+ int margin = 10 ;
385+
386+ nk_console_render_window (
387+ console ,
388+ title ,
389+ nk_rect (margin + width * (num - 1 ), margin , width - margin , height - margin * 2 ),
390+ flags );
391+
392+ return shouldClose ;
393+ }
394+
395+ #if CONSOLE_COUNT < 2
396+ void nuklear_console_demo_free (nk_console * console ) {
356397 nk_gamepad_free (nk_console_get_gamepads (console ));
357398 nk_console_free (console );
358399}
400+ #else
401+ void nuklear_console_demo_free (nk_consoles * consoles ) {
402+ nk_gamepad_free (& gamepads );
403+ nk_consoles_free (consoles );
404+ }
405+ #endif
406+
0 commit comments