Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
41
42#define WIDTH 1280
43#define HEIGHT 900
44
45#define ALLEGRO_UNSTABLE 1
46#define HAVE_CJSON 1
47
48#include "nilorea/n_gui.h"
49#include <allegro5/allegro_audio.h>
50#include <allegro5/allegro_acodec.h>
51#include "cJSON.h"
52
53ALLEGRO_DISPLAY* display = NULL;
54
55int DONE = 0,
58
59static char theme_file[512] = "";
60
61/* application state driven by toggle buttons */
62static int player_mode = 0;
63static int height_mode = 0;
64static int show_grid = 0;
65static int smooth_height = 0;
66static int ghost_enabled = 0;
67static int current_proj = 0; /* 0=Classic, 1=Iso, 2=Military, 3=Staggered */
68
69static const char* proj_names[] = {"Classic 2:1", "True Isometric", "Military", "Staggered"};
70
71/* widget ids for status display */
72static int lbl_status = -1;
73static int lbl_proj = -1;
74
75/* projection toggle button ids for radio group */
76static int proj_btn_ids[4] = {-1, -1, -1, -1};
77
78/* window ids (needed for show/hide dropdown) */
79#define NUM_WINDOWS 18
80static int win_ids[NUM_WINDOWS];
81static const char* win_names[NUM_WINDOWS];
82
83/* dropdown menu widget id for show/hide */
84static int dropmenu_windows_id = -1;
85
86/* coordinate-helper readout label (updated each tick from the virtual
87 * mouse position via n_gui_virtual_to_screen). */
88static int lbl_coords = -1;
89
90/* Path used by the Layout I/O window's save/load buttons. Persists
91 * window geometry across runs via n_gui_save_layout_json /
92 * n_gui_load_layout_json. */
93#define EX_GUI_LAYOUT_FILE "ex_gui_layout.json"
94
95/* the Buttons window, whose title bar counts the clicks */
96static int buttons_window = -1;
97
98/* callback: button click. user_data is the GUI context. The count goes in a
99 * caption rather than the title: the title is the window's name in the
100 * saved layout, and changing it would lose the window's saved place. */
101void on_button_click(int widget_id, void* user_data) {
102 static int clicks = 0;
103 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
104 char caption[64];
105
106 n_log(LOG_NOTICE, "Button %d clicked!", widget_id);
107 clicks++;
108 if (gui && buttons_window >= 0) {
109 snprintf(caption, sizeof(caption), "Buttons - %d click%s", clicks, clicks == 1 ? "" : "s");
111 }
112}
113
114/* callback: slider change */
115void on_slider_change(int widget_id, double value, void* user_data) {
116 (void)user_data;
117 n_log(LOG_NOTICE, "Slider %d value: %.2f", widget_id, value);
118}
119
120/* callback: checkbox toggle */
121void on_checkbox_toggle(int widget_id, int checked, void* user_data) {
122 (void)user_data;
123 n_log(LOG_NOTICE, "Checkbox %d: %s", widget_id, checked ? "checked" : "unchecked");
124}
125
126/* callback: textarea change */
127void on_text_change(int widget_id, const char* text, void* user_data) {
128 (void)user_data;
129 n_log(LOG_NOTICE, "Textarea %d: \"%s\"", widget_id, text);
130}
131
132/* callback: scrollbar scroll */
133void on_scroll(int widget_id, double pos, void* user_data) {
134 (void)user_data;
135 n_log(LOG_NOTICE, "Scrollbar %d pos: %.2f", widget_id, pos);
136}
137
138/* callback: quit button */
139void on_quit_click(int widget_id, void* user_data) {
140 (void)widget_id;
141 int* done = (int*)user_data;
142 *done = 1;
143}
144
145/* callback: login button */
146void on_login_click(int widget_id, void* user_data) {
147 (void)user_data;
148 n_log(LOG_NOTICE, "Login button %d clicked!", widget_id);
149}
150
151/* callback: Save Layout button, persist current window geometry to JSON */
152void on_save_layout_click(int widget_id, void* user_data) {
153 (void)widget_id;
154 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
156 n_log(LOG_NOTICE, "Layout saved to %s", EX_GUI_LAYOUT_FILE);
157 } else {
158 n_log(LOG_ERR, "Failed to save layout to %s", EX_GUI_LAYOUT_FILE);
159 }
160}
161
162/* callback: Load Layout button, restore window geometry from JSON */
163void on_load_layout_click(int widget_id, void* user_data) {
164 (void)widget_id;
165 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
167 n_log(LOG_NOTICE, "Layout loaded from %s", EX_GUI_LAYOUT_FILE);
168 } else {
169 n_log(LOG_NOTICE, "No saved layout at %s yet (Save first)", EX_GUI_LAYOUT_FILE);
170 }
171}
172
173/* callback: listbox selection */
174void on_listbox_select(int widget_id, int index, int selected, void* user_data) {
175 (void)user_data;
176 n_log(LOG_NOTICE, "Listbox %d: item %d %s", widget_id, index, selected ? "selected" : "deselected");
177}
178
179/* callback: radiolist selection */
180void on_radiolist_select(int widget_id, int index, void* user_data) {
181 (void)user_data;
182 n_log(LOG_NOTICE, "Radiolist %d: selected item %d", widget_id, index);
183}
184
185/* callback: combobox selection */
186void on_combobox_select(int widget_id, int index, void* user_data) {
187 (void)user_data;
188 n_log(LOG_NOTICE, "Combobox %d: selected item %d", widget_id, index);
189}
190
191/* callback: label link click */
192void on_link_click(int widget_id, const char* link, void* user_data) {
193 (void)user_data;
194 n_log(LOG_NOTICE, "Link %d clicked: %s", widget_id, link);
195}
196
197/* toggle button callbacks */
198
199void on_player_toggle(int widget_id, void* user_data) {
200 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
202 n_log(LOG_NOTICE, "Player mode: %s", player_mode ? "ON" : "OFF");
203}
204
205void on_height_toggle(int widget_id, void* user_data) {
206 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
208 n_log(LOG_NOTICE, "Height mode: %s", height_mode ? "ON" : "OFF");
209}
210
211void on_grid_toggle(int widget_id, void* user_data) {
212 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
214 n_log(LOG_NOTICE, "Grid: %s", show_grid ? "ON" : "OFF");
215}
216
217void on_smooth_toggle(int widget_id, void* user_data) {
218 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
220 n_log(LOG_NOTICE, "Height rendering: %s", smooth_height ? "SMOOTH" : "CUT");
221}
222
223void on_ghost_toggle(int widget_id, void* user_data) {
224 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
226 n_log(LOG_NOTICE, "DR Ghost: %s", ghost_enabled ? "ON" : "OFF");
227}
228
229/* projection buttons: act as a radio group using toggle buttons */
230void on_proj_select(int widget_id, void* user_data) {
231 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
232 for (int i = 0; i < 4; i++) {
233 if (proj_btn_ids[i] == widget_id) {
234 current_proj = i;
236 } else {
238 }
239 }
240 n_log(LOG_NOTICE, "Projection: %s", proj_names[current_proj]);
241}
242
243/* dropdown menu: show/hide windows */
244
245/* callback when a window toggle entry is clicked in the dropdown */
246void on_window_toggle_click(int widget_id, int entry_index, int tag, void* user_data) {
247 (void)widget_id;
248 (void)entry_index;
249 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
251 n_log(LOG_NOTICE, "Toggled window id %d", tag);
252}
253
254/* callback to rebuild dynamic entries when the dropdown opens */
255void on_windows_menu_open(int widget_id, void* user_data) {
256 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
257
258 /* clear old dynamic entries */
260
261 /* add one dynamic entry per window showing its show/hide state */
262 for (int i = 0; i < NUM_WINDOWS; i++) {
263 if (win_ids[i] < 0) continue;
264 char buf[N_GUI_ID_MAX];
265 int is_open = n_gui_window_is_open(gui, win_ids[i]);
266 snprintf(buf, sizeof(buf), "[%s] %s", is_open ? "X" : " ", win_names[i]);
267 n_gui_dropmenu_add_dynamic_entry(gui, widget_id, buf, win_ids[i],
269 }
270}
271
272int main(int argc, char* argv[]) {
274
275 n_log(LOG_NOTICE, "%s is starting ...", argv[0]);
276
277 /* allegro 5 + addons loading */
278 if (!al_init()) {
279 n_abort("Could not init Allegro.\n");
280 }
281
282 /* The data is in DATAS/ beside the executable: found from wherever the
283 * example is started, not only from examples/. */
284 {
285 ALLEGRO_PATH* here = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
286
287 if (here) {
288 al_change_directory(al_path_cstr(here, ALLEGRO_NATIVE_PATH_SEP));
289 al_destroy_path(here);
290 }
291 }
292 if (!al_install_audio()) {
293 n_log(LOG_ERR, "Unable to initialize audio addon");
294 }
295 if (!al_init_acodec_addon()) {
296 n_log(LOG_ERR, "Unable to initialize audio codec addon");
297 }
298 if (!al_init_image_addon()) {
299 n_abort("Unable to initialize image addon\n");
300 }
301 if (!al_init_primitives_addon()) {
302 n_abort("Unable to initialize primitives addon\n");
303 }
304 if (!al_init_font_addon()) {
305 n_abort("Unable to initialize font addon\n");
306 }
307 if (!al_init_ttf_addon()) {
308 n_abort("Unable to initialize ttf_font addon\n");
309 }
310 if (!al_install_keyboard()) {
311 n_abort("Unable to initialize keyboard handler\n");
312 }
313 if (!al_install_mouse()) {
314 n_abort("Unable to initialize mouse handler\n");
315 }
316
317 /* parse command line */
318 char ver_str[128] = "";
319 while ((getoptret = getopt(argc, argv, "hvV:L:t:")) != EOF) {
320 switch (getoptret) {
321 case 'h':
322 n_log(LOG_NOTICE, "\n %s -h help -v version -V DEBUGLEVEL -L logfile -t theme.json", argv[0]);
323 exit(TRUE);
324 case 'v':
325 sprintf(ver_str, "%s %s", __DATE__, __TIME__);
326 exit(TRUE);
327 break;
328 case 'V':
329 if (!strncmp("NOTICE", optarg, 6)) {
331 } else if (!strncmp("VERBOSE", optarg, 7)) {
333 } else if (!strncmp("ERROR", optarg, 5)) {
335 } else if (!strncmp("DEBUG", optarg, 5)) {
337 } else {
338 n_log(LOG_ERR, "%s is not a valid log level", optarg);
339 exit(FALSE);
340 }
342 break;
343 case 'L':
344 set_log_file(optarg);
345 break;
346 case 't':
347 strncpy(theme_file, optarg, sizeof(theme_file) - 1);
348 theme_file[sizeof(theme_file) - 1] = '\0';
349 break;
350 default:
351 n_log(LOG_ERR, "\n %s -h help -v version -V DEBUGLEVEL -L logfile -t theme.json", argv[0]);
352 exit(FALSE);
353 }
354 }
355
356 ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
357 if (!event_queue) {
358 n_abort("Failed to create event queue!\n");
359 }
360
361 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
362 display = al_create_display(WIDTH, HEIGHT);
363 if (!display) {
364 n_abort("Unable to create display\n");
365 }
366 al_set_window_title(display, "Nilorea GUI Demo - All Widgets + Dropdown Menus");
367
368 ALLEGRO_TIMER* fps_timer = al_create_timer(1.0 / 60.0);
369
370 al_register_event_source(event_queue, al_get_display_event_source(display));
371 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
372 al_register_event_source(event_queue, al_get_keyboard_event_source());
373 al_register_event_source(event_queue, al_get_mouse_event_source());
374 al_hide_mouse_cursor(display);
375
376 /* load a built-in font */
377 ALLEGRO_FONT* font = al_create_builtin_font();
378 if (!font) {
379 n_abort("Unable to create builtin font\n");
380 }
381
382 /* create the GUI */
384
385 /* set display size for global scrollbars (shown when GUI exceeds display) */
386 n_gui_set_display_size(gui, (float)WIDTH, (float)HEIGHT);
387
388 /* set display pointer for clipboard operations (copy/paste in text areas) */
390
391 /* enable virtual canvas: widget coordinates stay in WIDTH x HEIGHT space,
392 * the GUI scales uniformly to fit the actual display size */
393 n_gui_set_virtual_size(gui, (float)WIDTH, (float)HEIGHT);
394
395 /* detect and apply DPI scale */
396 float dpi = n_gui_detect_dpi_scale(gui, display);
397 n_log(LOG_NOTICE, "DPI scale: %.2f", dpi);
398
399 /* load theme file if specified on command line */
400 if (theme_file[0]) {
402 n_log(LOG_NOTICE, "Loaded theme: %s", theme_file);
403 } else {
404 n_log(LOG_ERR, "Failed to load theme: %s", theme_file);
405 }
406 }
407
408 int win_idx = 0;
409
410 /* Window 0: Menu Bar (frameless, fixed position) */
411 win_ids[win_idx] = n_gui_add_window(gui, "Menu", 0, 0, (float)WIDTH, 28);
412 win_names[win_idx] = "Menu";
413 int win_menu = win_ids[win_idx];
415 win_idx++;
416
417 /* dropdown: Windows (show/hide) */
419 "Windows", 10, 2, 120, 22,
421
422 /* dropdown: File (static entries) */
423 int dm_file = n_gui_add_dropmenu(gui, win_menu,
424 "File", 140, 2, 100, 22, NULL, NULL);
425 n_gui_dropmenu_add_entry(gui, dm_file, "New", 0, NULL, NULL);
426 n_gui_dropmenu_add_entry(gui, dm_file, "Open", 1, NULL, NULL);
427 n_gui_dropmenu_add_entry(gui, dm_file, "Save", 2, NULL, NULL);
428
429 /* Window 1: Buttons */
430 win_ids[win_idx] = n_gui_add_window(gui, "Buttons", 20, 60, 300, 280);
431 win_names[win_idx] = "Buttons";
432 int win1 = win_ids[win_idx];
433 win_idx++;
434 buttons_window = win1;
435
436 n_gui_add_button(gui, win1, "Click Me!", 20, 20, 120, 36, N_GUI_SHAPE_RECT, on_button_click, gui);
437 n_gui_add_button(gui, win1, "Rounded", 160, 20, 120, 36, N_GUI_SHAPE_ROUNDED, on_button_click, gui);
438 n_gui_add_button(gui, win1, "Quit", 20, 80, 260, 40, N_GUI_SHAPE_ROUNDED, on_quit_click, &DONE);
439
440 /* custom themed button */
441 int btn_custom = n_gui_add_button(gui, win1, "Custom Theme", 20, 140, 260, 36, N_GUI_SHAPE_ROUNDED, on_button_click, NULL);
442 N_GUI_THEME red_theme = n_gui_make_theme(
443 al_map_rgba(120, 30, 30, 230), al_map_rgba(160, 40, 40, 240), al_map_rgba(200, 60, 60, 255),
444 al_map_rgba(200, 80, 80, 255), al_map_rgba(230, 100, 100, 255), al_map_rgba(255, 120, 120, 255),
445 al_map_rgba(255, 220, 220, 255), al_map_rgba(255, 255, 255, 255), al_map_rgba(255, 255, 255, 255),
446 2.0f, 8.0f, 8.0f);
447 n_gui_set_widget_theme(gui, btn_custom, red_theme);
448
449 {
450 int btn_dis = n_gui_add_button(gui, win1, "Disabled Button", 20, 196, 260, 30, N_GUI_SHAPE_RECT, on_button_click, NULL);
451 n_gui_set_widget_enabled(gui, btn_dis, 0);
452 }
453
454 /* Window 2: Sliders (resizable) */
455 win_ids[win_idx] = n_gui_add_window(gui, "Sliders (resizable)", 340, 60, 320, 200);
456 win_names[win_idx] = "Sliders (resizable)";
457 int win2 = win_ids[win_idx];
459 win_idx++;
460
461 n_gui_add_slider(gui, win2, 20, 20, 180, 24, 0.0, 100.0, 50.0, N_GUI_SLIDER_VALUE, on_slider_change, NULL);
462 n_gui_add_slider(gui, win2, 20, 60, 180, 24, 0.0, 100.0, 25.0, N_GUI_SLIDER_PERCENT, on_slider_change, NULL);
463 {
464 int sld_dis = n_gui_add_slider(gui, win2, 20, 100, 180, 24, -50.0, 50.0, 0.0, N_GUI_SLIDER_VALUE, on_slider_change, NULL);
465 n_gui_set_widget_enabled(gui, sld_dis, 0);
466 }
467
468 /* vertical sliders */
469 n_gui_add_vslider(gui, win2, 240, 10, 24, 120, 0.0, 100.0, 75.0, N_GUI_SLIDER_VALUE, on_slider_change, NULL);
470 n_gui_add_vslider(gui, win2, 275, 10, 24, 120, 0.0, 100.0, 40.0, N_GUI_SLIDER_PERCENT, on_slider_change, NULL);
471
472 /* Window 3: Text Areas */
473 win_ids[win_idx] = n_gui_add_window(gui, "Text Areas", 20, 360, 350, 280);
474 win_names[win_idx] = "Text Areas";
475 int win3 = win_ids[win_idx];
476 win_idx++;
477
478 n_gui_add_textarea(gui, win3, 20, 20, 310, 28, 0, 64, on_text_change, NULL);
479 int ta_multi = n_gui_add_textarea(gui, win3, 20, 60, 310, 160, 1, 512, on_text_change, NULL);
480 n_gui_textarea_set_text(gui, ta_multi, "Hello! This is a\nmultiline text area.\nType here...");
481
482 /* Window 4: Checkboxes (auto-sized) */
483 win_ids[win_idx] = n_gui_add_window_auto(gui, "Checkboxes (auto)", 680, 60);
484 win_names[win_idx] = "Checkboxes (auto)";
485 int win4 = win_ids[win_idx];
486 win_idx++;
487
488 n_gui_add_checkbox(gui, win4, "Enable sound", 20, 20, 260, 28, 1, on_checkbox_toggle, NULL);
489 n_gui_add_checkbox(gui, win4, "Fullscreen", 20, 56, 260, 28, 0, on_checkbox_toggle, NULL);
490 {
491 int cb_fps = n_gui_add_checkbox(gui, win4, "Show FPS (disabled)", 20, 92, 260, 28, 1, on_checkbox_toggle, NULL);
492 n_gui_set_widget_enabled(gui, cb_fps, 0);
493 }
494 {
495 int cb_vsync = n_gui_add_checkbox(gui, win4, "VSync (hidden)", 20, 128, 260, 28, 0, on_checkbox_toggle, NULL);
496 n_gui_set_widget_visible(gui, cb_vsync, 0);
497 }
498 /* auto-size to fit the widgets */
500
501 /* Window 5: Scrollbars */
502 win_ids[win_idx] = n_gui_add_window(gui, "Scrollbars", 390, 360, 320, 280);
503 win_names[win_idx] = "Scrollbars";
504 int win5 = win_ids[win_idx];
505 win_idx++;
506
508 1000.0, 200.0, on_scroll, NULL);
509 {
510 /* The vertical one is the window's wheel scrollbar: the wheel
511 * anywhere over the window moves it, five units a notch, where the
512 * others move only with the pointer on them. */
513 int sb_v = n_gui_add_scrollbar(gui, win5, 280, 20, 20, 220, N_GUI_SCROLLBAR_V, N_GUI_SHAPE_ROUNDED,
514 500.0, 100.0, on_scroll, NULL);
517 }
519 2000.0, 500.0, on_scroll, NULL);
521 100.0, 100.0, on_scroll, NULL);
522
523 /* Window 6: Listbox (single select) */
524 win_ids[win_idx] = n_gui_add_window(gui, "Listbox (Single)", 730, 360, 260, 240);
525 win_names[win_idx] = "Listbox (Single)";
526 int win6 = win_ids[win_idx];
527 win_idx++;
528
529 int lb_single = n_gui_add_listbox(gui, win6, 10, 10, 240, 180, N_GUI_SELECT_SINGLE, on_listbox_select, NULL);
530 n_gui_listbox_add_item(gui, lb_single, "Apple");
531 n_gui_listbox_add_item(gui, lb_single, "Banana");
532 n_gui_listbox_add_item(gui, lb_single, "Cherry");
533 n_gui_listbox_add_item(gui, lb_single, "Date");
534 n_gui_listbox_add_item(gui, lb_single, "Elderberry");
535 n_gui_listbox_add_item(gui, lb_single, "Fig");
536 n_gui_listbox_add_item(gui, lb_single, "Grape");
537 n_gui_listbox_add_item(gui, lb_single, "Honeydew");
538 n_gui_listbox_add_item(gui, lb_single, "Kiwi");
539 n_gui_listbox_add_item(gui, lb_single, "Lemon");
540 n_gui_listbox_add_item(gui, lb_single, "Mango");
541 n_gui_listbox_add_item(gui, lb_single, "Nectarine");
542 n_gui_listbox_add_item(gui, lb_single, "Orange");
543 n_gui_listbox_add_item(gui, lb_single, "Papaya");
544 n_gui_listbox_add_item(gui, lb_single, "Quince");
545 n_gui_listbox_set_selected(gui, lb_single, 2, 1);
546
547 /* Window 7: Listbox (multi select) */
548 win_ids[win_idx] = n_gui_add_window(gui, "Listbox (Multi)", 1000, 360, 260, 240);
549 win_names[win_idx] = "Listbox (Multi)";
550 int win7 = win_ids[win_idx];
551 win_idx++;
552
553 int lb_multi = n_gui_add_listbox(gui, win7, 10, 10, 240, 180, N_GUI_SELECT_MULTIPLE, on_listbox_select, NULL);
554 n_gui_listbox_add_item(gui, lb_multi, "Red");
555 n_gui_listbox_add_item(gui, lb_multi, "Green");
556 n_gui_listbox_add_item(gui, lb_multi, "Blue");
557 n_gui_listbox_add_item(gui, lb_multi, "Yellow");
558 n_gui_listbox_add_item(gui, lb_multi, "Cyan");
559 n_gui_listbox_add_item(gui, lb_multi, "Magenta");
560 n_gui_listbox_add_item(gui, lb_multi, "White");
561 n_gui_listbox_add_item(gui, lb_multi, "Black");
562 n_gui_listbox_add_item(gui, lb_multi, "Orange");
563 n_gui_listbox_add_item(gui, lb_multi, "Pink");
564 n_gui_listbox_add_item(gui, lb_multi, "Brown");
565 n_gui_listbox_add_item(gui, lb_multi, "Purple");
566 n_gui_listbox_set_selected(gui, lb_multi, 0, 1);
567 n_gui_listbox_set_selected(gui, lb_multi, 2, 1);
568
569 /* Window 8: Radiolist */
570 win_ids[win_idx] = n_gui_add_window(gui, "Radio List", 730, 60, 260, 230);
571 win_names[win_idx] = "Radio List";
572 int win8 = win_ids[win_idx];
573 win_idx++;
574
575 int radio1 = n_gui_add_radiolist(gui, win8, 10, 10, 240, 170, on_radiolist_select, NULL);
576 n_gui_radiolist_add_item(gui, radio1, "Option A");
577 n_gui_radiolist_add_item(gui, radio1, "Option B");
578 n_gui_radiolist_add_item(gui, radio1, "Option C");
579 n_gui_radiolist_add_item(gui, radio1, "Option D");
580 n_gui_radiolist_add_item(gui, radio1, "Option E");
581 n_gui_radiolist_add_item(gui, radio1, "Option F");
582 n_gui_radiolist_add_item(gui, radio1, "Option G");
583 n_gui_radiolist_add_item(gui, radio1, "Option H");
584 n_gui_radiolist_add_item(gui, radio1, "Option I");
585 n_gui_radiolist_add_item(gui, radio1, "Option J");
587
588 /* Window 9: Combobox + Labels + Image */
589 win_ids[win_idx] = n_gui_add_window(gui, "Combo / Labels / Image", 1000, 60, 260, 280);
590 win_names[win_idx] = "Combo / Labels / Image";
591 int win9 = win_ids[win_idx];
592 win_idx++;
593
594 /* combobox */
595 int combo1 = n_gui_add_combobox(gui, win9, 10, 10, 240, 24, on_combobox_select, NULL);
596 n_gui_combobox_add_item(gui, combo1, "Tiny");
597 n_gui_combobox_add_item(gui, combo1, "Small");
598 n_gui_combobox_add_item(gui, combo1, "Medium");
599 n_gui_combobox_add_item(gui, combo1, "Large");
600 n_gui_combobox_add_item(gui, combo1, "Extra Large");
601 n_gui_combobox_add_item(gui, combo1, "XXL");
602 n_gui_combobox_add_item(gui, combo1, "XXXL");
603 n_gui_combobox_add_item(gui, combo1, "Huge");
604 n_gui_combobox_add_item(gui, combo1, "Gigantic");
605 n_gui_combobox_add_item(gui, combo1, "Colossal");
607
608 /* static labels */
609 n_gui_add_label(gui, win9, "Left aligned label", 10, 50, 240, 20, N_GUI_ALIGN_LEFT);
610 n_gui_add_label(gui, win9, "Centered label", 10, 74, 240, 20, N_GUI_ALIGN_CENTER);
611 n_gui_add_label(gui, win9, "Right aligned label", 10, 98, 240, 20, N_GUI_ALIGN_RIGHT);
612 n_gui_add_label(gui, win9, "Justified: this text spreads across width evenly", 10, 122, 240, 20, N_GUI_ALIGN_JUSTIFIED);
613
614 /* hyperlink label */
616 "Click this link!", "https://example.com",
617 10, 150, 240, 20, N_GUI_ALIGN_LEFT,
618 on_link_click, NULL);
619
620 /* image widgets with different scale modes */
621 ALLEGRO_BITMAP* img_192 = al_load_bitmap("DATAS/img/android-chrome-192x192.png");
622 ALLEGRO_BITMAP* img_32 = al_load_bitmap("DATAS/img/favicon-32x32.png");
623 n_gui_add_label(gui, win9, "Image FIT:", 10, 180, 80, 16, N_GUI_ALIGN_LEFT);
624 n_gui_add_image(gui, win9, 10, 198, 80, 80, img_192, N_GUI_IMAGE_FIT);
625 n_gui_add_label(gui, win9, "STRETCH:", 100, 180, 70, 16, N_GUI_ALIGN_LEFT);
626 n_gui_add_image(gui, win9, 100, 198, 70, 80, img_192, N_GUI_IMAGE_STRETCH);
627 n_gui_add_label(gui, win9, "CENTER:", 180, 180, 70, 16, N_GUI_ALIGN_LEFT);
628 n_gui_add_image(gui, win9, 180, 198, 70, 80, img_32, N_GUI_IMAGE_CENTER);
629
630 /* Window 10: Mode Toggles (stays clicked/unclicked) */
631 win_ids[win_idx] = n_gui_add_window(gui, "Mode Toggles", 20, 660, 300, 230);
632 win_names[win_idx] = "Mode Toggles";
633 int win10 = win_ids[win_idx];
634 win_idx++;
635
636 n_gui_add_label(gui, win10, "Click or press key to toggle on/off:", 20, 10, 260, 20, N_GUI_ALIGN_LEFT);
637
638 {
639 int btn_id;
640 btn_id = n_gui_add_toggle_button(gui, win10, "Player Mode [Ctrl+P]",
641 20, 40, 260, 32, N_GUI_SHAPE_ROUNDED, 0,
643 n_gui_button_set_keycode(gui, btn_id, ALLEGRO_KEY_P, ALLEGRO_KEYMOD_CTRL);
644
645 btn_id = n_gui_add_toggle_button(gui, win10, "Height Mode [Ctrl+H]",
646 20, 80, 260, 32, N_GUI_SHAPE_ROUNDED, 0,
648 n_gui_button_set_keycode(gui, btn_id, ALLEGRO_KEY_H, ALLEGRO_KEYMOD_CTRL);
649
650 btn_id = n_gui_add_toggle_button(gui, win10, "Grid [Shift+G]",
651 20, 120, 260, 32, N_GUI_SHAPE_ROUNDED, 0,
653 n_gui_button_set_keycode(gui, btn_id, ALLEGRO_KEY_G, ALLEGRO_KEYMOD_SHIFT);
654
655 btn_id = n_gui_add_toggle_button(gui, win10, "Smooth Height [Shift+V]",
656 20, 160, 260, 32, N_GUI_SHAPE_ROUNDED, 0,
658 n_gui_button_set_keycode(gui, btn_id, ALLEGRO_KEY_V, ALLEGRO_KEYMOD_SHIFT);
659 }
660
661 /* Window 11: Projection Selection (toggle radio group) */
662 win_ids[win_idx] = n_gui_add_window(gui, "Projection", 340, 660, 280, 230);
663 win_names[win_idx] = "Projection";
664 int win11 = win_ids[win_idx];
665 win_idx++;
666
667 n_gui_add_label(gui, win11, "Select projection:", 20, 10, 240, 20, N_GUI_ALIGN_LEFT);
668
669 proj_btn_ids[0] = n_gui_add_toggle_button(gui, win11, "Classic 2:1 [Ctrl+F1]",
670 20, 40, 240, 30, N_GUI_SHAPE_ROUNDED, 1,
672 n_gui_button_set_keycode(gui, proj_btn_ids[0], ALLEGRO_KEY_F1, ALLEGRO_KEYMOD_CTRL);
673 proj_btn_ids[1] = n_gui_add_toggle_button(gui, win11, "True Isometric [Shift+F2]",
674 20, 78, 240, 30, N_GUI_SHAPE_ROUNDED, 0,
676 n_gui_button_set_keycode(gui, proj_btn_ids[1], ALLEGRO_KEY_F2, ALLEGRO_KEYMOD_SHIFT);
677 proj_btn_ids[2] = n_gui_add_toggle_button(gui, win11, "Military [Ctrl+F3]",
678 20, 116, 240, 30, N_GUI_SHAPE_ROUNDED, 0,
680 n_gui_button_set_keycode(gui, proj_btn_ids[2], ALLEGRO_KEY_F3, ALLEGRO_KEYMOD_CTRL);
681 proj_btn_ids[3] = n_gui_add_toggle_button(gui, win11, "Staggered [Shift+F4]",
682 20, 154, 240, 30, N_GUI_SHAPE_ROUNDED, 0,
684 n_gui_button_set_keycode(gui, proj_btn_ids[3], ALLEGRO_KEY_F4, ALLEGRO_KEYMOD_SHIFT);
685
686 /* custom theme: green for active/toggled state */
687 N_GUI_THEME green_theme = n_gui_make_theme(
688 al_map_rgba(40, 60, 40, 230), al_map_rgba(50, 80, 50, 240), al_map_rgba(30, 120, 30, 255),
689 al_map_rgba(60, 100, 60, 255), al_map_rgba(70, 140, 70, 255), al_map_rgba(40, 160, 40, 255),
690 al_map_rgba(200, 255, 200, 255), al_map_rgba(255, 255, 255, 255), al_map_rgba(255, 255, 255, 255),
691 2.0f, 8.0f, 8.0f);
692 for (int i = 0; i < 4; i++) {
693 n_gui_set_widget_theme(gui, proj_btn_ids[i], green_theme);
694 }
695
696 /* Window 12: Status Display */
697 win_ids[win_idx] = n_gui_add_window(gui, "Toggle Status", 640, 660, 620, 230);
698 win_names[win_idx] = "Toggle Status";
699 int win12 = win_ids[win_idx];
700 win_idx++;
701
702 lbl_status = n_gui_add_label(gui, win12, "Waiting...", 20, 10, 580, 20, N_GUI_ALIGN_LEFT);
703 lbl_proj = n_gui_add_label(gui, win12, "Projection: Classic 2:1", 20, 40, 580, 20, N_GUI_ALIGN_LEFT);
704 /* lbl_coords is updated each tick from the virtual mouse position
705 * via n_gui_virtual_to_screen (round-tripped against the cached
706 * screen position via n_gui_screen_to_virtual). */
707 lbl_coords = n_gui_add_label(gui, win12, "Coords: ?", 20, 60, 580, 20, N_GUI_ALIGN_LEFT);
708
709 n_gui_add_label(gui, win12, "Toggle buttons stay visually pressed when ON.", 20, 80, 580, 20, N_GUI_ALIGN_LEFT);
710 n_gui_add_label(gui, win12, "Click again to turn OFF. Projection uses radio-group pattern.", 20, 100, 580, 20, N_GUI_ALIGN_LEFT);
711 n_gui_add_label(gui, win12, "The state persists: no need to hold the mouse button down.", 20, 120, 580, 20, N_GUI_ALIGN_LEFT);
712
713 /* Window 13: Scrollable Content. AUTO_SCROLLBAR scrolls overflow; NO_HSCROLL makes
714 * this a vertical-only panel, so the content wider than the window is clipped rather
715 * than shown behind a horizontal scrollbar (the vertical scrollbar still appears). */
716 win_ids[win_idx] = n_gui_add_window(gui, "Scrollable (vertical only)", 390, 270, 280, 80);
717 win_names[win_idx] = "Scrollable (vertical only)";
718 int win_scroll = win_ids[win_idx];
720 win_idx++;
721
722 /* add many labels wider and taller than the window: vertical overflow scrolls, the
723 horizontal overflow is clipped (no horizontal scrollbar thanks to NO_HSCROLL) */
724 for (int i = 0; i < 20; i++) {
725 char buf[128];
726 snprintf(buf, sizeof(buf), "Scrollable line %d - wide content clipped, not h-scrolled", i + 1);
727 n_gui_add_label(gui, win_scroll, buf, 10, (float)(i * 22), 400, 20, N_GUI_ALIGN_LEFT);
728 }
729
730 /* Window 14: Bitmap Skinning Demo
731 * Demonstrates optional bitmap overlays on widgets and windows.
732 * Placeholder bitmaps are created programmatically as colored rectangles
733 * with gradients to show where the skinning takes effect.
734 * In a real application, use al_load_bitmap() with actual image files.
735 * Bitmaps are NOT owned by N_GUI, caller must keep them alive while
736 * the GUI uses them and destroy them AFTER n_gui_destroy_ctx(). */
737 ALLEGRO_BITMAP* skin_win_bg = NULL;
738 ALLEGRO_BITMAP* skin_win_tb = NULL;
739 ALLEGRO_BITMAP* skin_sld_track = NULL;
740 ALLEGRO_BITMAP* skin_sld_fill = NULL;
741 ALLEGRO_BITMAP* skin_sld_handle = NULL;
742 ALLEGRO_BITMAP* skin_sld_handle_h = NULL;
743 ALLEGRO_BITMAP* skin_chk_unchecked = NULL;
744 ALLEGRO_BITMAP* skin_chk_checked = NULL;
745
746 /* Window 16 ("Bitmap Skinning II") bitmaps. Declared at the outer
747 * scope so they live until the cleanup section runs after
748 * n_gui_destroy_ctx. N_GUI stores raw pointers; the caller owns
749 * the bitmaps and must outlive the GUI's use of them. */
750 ALLEGRO_BITMAP* skin_btn_normal = NULL;
751 ALLEGRO_BITMAP* skin_btn_hover = NULL;
752 ALLEGRO_BITMAP* skin_btn_active = NULL;
753 ALLEGRO_BITMAP* skin_list_bg = NULL;
754 ALLEGRO_BITMAP* skin_list_item = NULL;
755 ALLEGRO_BITMAP* skin_list_sel = NULL;
756 ALLEGRO_BITMAP* skin_drop_panel = NULL;
757 ALLEGRO_BITMAP* skin_drop_hover = NULL;
758 ALLEGRO_BITMAP* skin_text_bg = NULL;
759 ALLEGRO_BITMAP* skin_tb_close_n = NULL;
760 ALLEGRO_BITMAP* skin_tb_close_h = NULL;
761 ALLEGRO_BITMAP* skin_tb_close_a = NULL;
762
763 {
764 /* create placeholder skin bitmaps programmatically */
765 ALLEGRO_BITMAP* prev_target = al_get_target_bitmap();
766
767 /* window background: blue gradient */
768 skin_win_bg = al_create_bitmap(64, 64);
769 al_set_target_bitmap(skin_win_bg);
770 for (int row = 0; row < 64; row++) {
771 float t = (float)row / 63.0f;
772 al_draw_line(0, (float)row, 64, (float)row,
773 al_map_rgba((unsigned char)(20 + 40 * t), (unsigned char)(40 + 60 * t), (unsigned char)(80 + 80 * t), 230), 1.0f);
774 }
775
776 /* titlebar: dark gradient */
777 skin_win_tb = al_create_bitmap(64, 16);
778 al_set_target_bitmap(skin_win_tb);
779 for (int row = 0; row < 16; row++) {
780 float t = (float)row / 15.0f;
781 al_draw_line(0, (float)row, 64, (float)row,
782 al_map_rgba((unsigned char)(60 + 40 * t), (unsigned char)(30 + 30 * t), (unsigned char)(100 + 50 * t), 240), 1.0f);
783 }
784
785 /* slider track: dark rounded bar */
786 skin_sld_track = al_create_bitmap(32, 8);
787 al_set_target_bitmap(skin_sld_track);
788 al_clear_to_color(al_map_rgba(50, 50, 60, 200));
789
790 /* slider fill: bright bar */
791 skin_sld_fill = al_create_bitmap(32, 8);
792 al_set_target_bitmap(skin_sld_fill);
793 al_clear_to_color(al_map_rgba(100, 180, 255, 230));
794
795 /* slider handle: circle-ish */
796 skin_sld_handle = al_create_bitmap(16, 16);
797 al_set_target_bitmap(skin_sld_handle);
798 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
799 al_draw_filled_circle(8, 8, 7, al_map_rgba(200, 200, 220, 240));
800
801 /* slider handle hover */
802 skin_sld_handle_h = al_create_bitmap(16, 16);
803 al_set_target_bitmap(skin_sld_handle_h);
804 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
805 al_draw_filled_circle(8, 8, 7, al_map_rgba(150, 220, 255, 255));
806
807 /* checkbox bitmaps */
808 skin_chk_unchecked = al_create_bitmap(16, 16);
809 al_set_target_bitmap(skin_chk_unchecked);
810 al_clear_to_color(al_map_rgba(60, 60, 80, 220));
811 al_draw_rectangle(0.5f, 0.5f, 15.5f, 15.5f, al_map_rgba(150, 150, 200, 255), 1.0f);
812
813 skin_chk_checked = al_create_bitmap(16, 16);
814 al_set_target_bitmap(skin_chk_checked);
815 al_clear_to_color(al_map_rgba(80, 140, 255, 240));
816 al_draw_rectangle(0.5f, 0.5f, 15.5f, 15.5f, al_map_rgba(150, 150, 200, 255), 1.0f);
817 al_draw_line(3, 8, 7, 13, al_map_rgb(255, 255, 255), 2.0f);
818 al_draw_line(7, 13, 13, 3, al_map_rgb(255, 255, 255), 2.0f);
819
820 al_set_target_bitmap(prev_target);
821
822 /* create the skinned window */
823 win_ids[win_idx] = n_gui_add_window(gui, "Bitmap Skinning", 690, 60, 300, 240);
824 win_names[win_idx] = "Bitmap Skinning";
825 int win_skin = win_ids[win_idx];
826 win_idx++;
827
828 /* apply window bitmaps */
829 n_gui_window_set_bitmaps(gui, win_skin, skin_win_bg, skin_win_tb, N_GUI_IMAGE_STRETCH);
830
831 /* skinned slider */
832 int skin_slider = n_gui_add_slider(gui, win_skin, 20, 20, 200, 24, 0.0, 100.0, 60.0,
834 n_gui_slider_set_bitmaps(gui, skin_slider, skin_sld_track, skin_sld_fill, skin_sld_handle, skin_sld_handle_h, NULL);
835
836 /* skinned checkbox */
837 int skin_chk = n_gui_add_checkbox(gui, win_skin, "Skinned checkbox", 20, 60, 200, 24, 0,
838 on_checkbox_toggle, NULL);
839 n_gui_checkbox_set_bitmaps(gui, skin_chk, skin_chk_unchecked, skin_chk_checked, NULL);
840
841 /* label with background (reuses sld_track bitmap) */
842 int skin_label = n_gui_add_label(gui, win_skin, "Label with bg bitmap", 20, 100, 200, 24, N_GUI_ALIGN_CENTER);
843 n_gui_label_set_bitmap(gui, skin_label, skin_sld_track);
844
845 /* regular button for comparison */
846 n_gui_add_button(gui, win_skin, "Normal Button", 20, 140, 200, 30, N_GUI_SHAPE_ROUNDED, on_button_click, NULL);
847
848 n_gui_add_label(gui, win_skin, "Window bg, titlebar, slider, checkbox,", 20, 180, 260, 16, N_GUI_ALIGN_LEFT);
849 n_gui_add_label(gui, win_skin, "and label are all bitmap-skinned above.", 20, 198, 260, 16, N_GUI_ALIGN_LEFT);
850 }
851
852 /* Window 16: Bitmap Skinning II, Containers + Titlebar Buttons
853 * Demonstrates the bitmap-skinning APIs that "Bitmap Skinning"
854 * (window 14) does not exercise: button_bitmap, listbox_set_bitmaps,
855 * radiolist_set_bitmaps, combobox_set_bitmaps, dropmenu_set_bitmaps,
856 * textarea_set_bitmap, and window_set_tb_button_bitmaps for the
857 * titlebar Close button. */
858 {
859 ALLEGRO_BITMAP* prev_target = al_get_target_bitmap();
860
861 /* button: three states drawn as filled rounded-ish rects */
862 skin_btn_normal = al_create_bitmap(64, 32);
863 al_set_target_bitmap(skin_btn_normal);
864 al_clear_to_color(al_map_rgba(60, 100, 180, 230));
865 skin_btn_hover = al_create_bitmap(64, 32);
866 al_set_target_bitmap(skin_btn_hover);
867 al_clear_to_color(al_map_rgba(90, 140, 220, 240));
868 skin_btn_active = al_create_bitmap(64, 32);
869 al_set_target_bitmap(skin_btn_active);
870 al_clear_to_color(al_map_rgba(40, 80, 140, 255));
871
872 /* listbox/combobox/radiolist share these three bitmaps */
873 skin_list_bg = al_create_bitmap(32, 32);
874 al_set_target_bitmap(skin_list_bg);
875 al_clear_to_color(al_map_rgba(25, 35, 55, 240));
876 skin_list_item = al_create_bitmap(32, 16);
877 al_set_target_bitmap(skin_list_item);
878 al_clear_to_color(al_map_rgba(40, 50, 70, 220));
879 skin_list_sel = al_create_bitmap(32, 16);
880 al_set_target_bitmap(skin_list_sel);
881 al_clear_to_color(al_map_rgba(80, 140, 220, 255));
882
883 /* dropmenu panel + per-item hover overlay */
884 skin_drop_panel = al_create_bitmap(64, 64);
885 al_set_target_bitmap(skin_drop_panel);
886 al_clear_to_color(al_map_rgba(35, 25, 55, 245));
887 skin_drop_hover = al_create_bitmap(32, 16);
888 al_set_target_bitmap(skin_drop_hover);
889 al_clear_to_color(al_map_rgba(120, 80, 200, 230));
890
891 /* textarea background */
892 skin_text_bg = al_create_bitmap(64, 24);
893 al_set_target_bitmap(skin_text_bg);
894 al_clear_to_color(al_map_rgba(245, 245, 220, 255));
895
896 /* titlebar Close button: red dot variants */
897 skin_tb_close_n = al_create_bitmap(16, 16);
898 al_set_target_bitmap(skin_tb_close_n);
899 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
900 al_draw_filled_circle(8, 8, 6, al_map_rgba(200, 60, 60, 255));
901 skin_tb_close_h = al_create_bitmap(16, 16);
902 al_set_target_bitmap(skin_tb_close_h);
903 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
904 al_draw_filled_circle(8, 8, 7, al_map_rgba(255, 100, 100, 255));
905 skin_tb_close_a = al_create_bitmap(16, 16);
906 al_set_target_bitmap(skin_tb_close_a);
907 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
908 al_draw_filled_circle(8, 8, 6, al_map_rgba(140, 30, 30, 255));
909
910 al_set_target_bitmap(prev_target);
911
912 win_ids[win_idx] = n_gui_add_window(gui, "Bitmap Skinning II", 1000, 60, 280, 380);
913 win_names[win_idx] = "Bitmap Skinning II";
914 int win_skin2 = win_ids[win_idx];
915 win_idx++;
916
917 /* titlebar Close button overlay (n_gui_window_set_tb_button_bitmaps) */
919 skin_tb_close_n, skin_tb_close_h, skin_tb_close_a);
920
921 /* button_bitmap: normal/hover/active states drawn from bitmaps
922 * instead of the color theme */
923 n_gui_add_button_bitmap(gui, win_skin2, "Bitmap Button", 20, 20, 240, 32,
924 skin_btn_normal, skin_btn_hover, skin_btn_active,
925 on_button_click, NULL);
926
927 /* skinned single-line textarea */
928 int skin_text = n_gui_add_textarea(gui, win_skin2, 20, 60, 240, 24, 0, 64,
929 on_text_change, NULL);
930 n_gui_textarea_set_bitmap(gui, skin_text, skin_text_bg);
931
932 /* skinned listbox */
933 int skin_list = n_gui_add_listbox(gui, win_skin2, 20, 95, 110, 80,
934 0, on_listbox_select, NULL);
935 n_gui_listbox_add_item(gui, skin_list, "Alpha");
936 n_gui_listbox_add_item(gui, skin_list, "Beta");
937 n_gui_listbox_add_item(gui, skin_list, "Gamma");
938 n_gui_listbox_add_item(gui, skin_list, "Delta");
939 n_gui_listbox_set_bitmaps(gui, skin_list, skin_list_bg, skin_list_item, skin_list_sel);
940
941 /* skinned radiolist (reuses the same three bitmaps) */
942 int skin_radio = n_gui_add_radiolist(gui, win_skin2, 150, 95, 110, 80,
943 on_radiolist_select, NULL);
944 n_gui_radiolist_add_item(gui, skin_radio, "Red");
945 n_gui_radiolist_add_item(gui, skin_radio, "Green");
946 n_gui_radiolist_add_item(gui, skin_radio, "Blue");
947 n_gui_radiolist_set_bitmaps(gui, skin_radio, skin_list_bg, skin_list_item, skin_list_sel);
948
949 /* skinned combobox */
950 int skin_combo = n_gui_add_combobox(gui, win_skin2, 20, 190, 240, 24,
951 on_combobox_select, NULL);
952 n_gui_combobox_add_item(gui, skin_combo, "First");
953 n_gui_combobox_add_item(gui, skin_combo, "Second");
954 n_gui_combobox_add_item(gui, skin_combo, "Third");
955 n_gui_combobox_set_bitmaps(gui, skin_combo, skin_list_bg, skin_list_item, skin_list_sel);
956
957 /* skinned dropmenu (with three static entries) */
958 int skin_drop = n_gui_add_dropmenu(gui, win_skin2, "Drop Menu", 20, 220, 240, 24, NULL, NULL);
959 n_gui_dropmenu_add_entry(gui, skin_drop, "Entry one", 1, NULL, NULL);
960 n_gui_dropmenu_add_entry(gui, skin_drop, "Entry two", 2, NULL, NULL);
961 n_gui_dropmenu_add_entry(gui, skin_drop, "Entry three", 3, NULL, NULL);
962 n_gui_dropmenu_set_bitmaps(gui, skin_drop, skin_drop_panel, skin_drop_hover);
963
964 n_gui_add_label(gui, win_skin2, "Button, textarea, listbox, radiolist,", 20, 255, 250, 16, N_GUI_ALIGN_LEFT);
965 n_gui_add_label(gui, win_skin2, "combobox, dropmenu, all bitmap-skinned.", 20, 273, 250, 16, N_GUI_ALIGN_LEFT);
966 n_gui_add_label(gui, win_skin2, "Close (X) in titlebar uses bitmap states.", 20, 291, 250, 16, N_GUI_ALIGN_LEFT);
967 }
968
969 /* Window 17: Layout I/O, n_gui_save_layout_json / load_layout_json */
970 win_ids[win_idx] = n_gui_add_window(gui, "Layout I/O", 1000, 460, 240, 130);
971 win_names[win_idx] = "Layout I/O";
972 {
973 int win_layout = win_ids[win_idx];
974 win_idx++;
975 n_gui_add_label(gui, win_layout, "Persist window geometry to:", 10, 8, 220, 16, N_GUI_ALIGN_LEFT);
976 n_gui_add_label(gui, win_layout, EX_GUI_LAYOUT_FILE, 10, 26, 220, 16, N_GUI_ALIGN_LEFT);
977 n_gui_add_button(gui, win_layout, "Save Layout", 10, 50, 100, 30, N_GUI_SHAPE_ROUNDED, on_save_layout_click, gui);
978 n_gui_add_button(gui, win_layout, "Load Layout", 120, 50, 100, 30, N_GUI_SHAPE_ROUNDED, on_load_layout_click, gui);
979 n_gui_add_label(gui, win_layout, "Drag windows, click Save, then Load", 10, 90, 220, 16, N_GUI_ALIGN_LEFT);
980 }
981
982 /* Window 15: Login Dialog (autofit + centered + set_focus demo) */
983 win_ids[win_idx] = n_gui_add_window(gui, "Login", 640, 450, 1, 1);
984 win_names[win_idx] = "Login Dialog";
985 {
986 int win_login = win_ids[win_idx];
987 win_idx++;
988
989 /* configure auto-fit: adjust both W and H, centered on insertion point, 10px border */
991
992 /* add widgets */
993 n_gui_add_label(gui, win_login, "Username:", 10, 10, 80, 25, N_GUI_ALIGN_LEFT);
994 int txt_user = n_gui_add_textarea(gui, win_login, 100, 10, 200, 25, 0, 64, on_text_change, NULL);
995 n_gui_add_label(gui, win_login, "Password:", 10, 45, 80, 25, N_GUI_ALIGN_LEFT);
996 int txt_pass = n_gui_add_textarea(gui, win_login, 100, 45, 200, 25, 0, 64, on_text_change, NULL);
997 int btn_login = n_gui_add_button(gui, win_login, "Login", 100, 85, 100, 30, N_GUI_SHAPE_ROUNDED, on_login_click, NULL);
998
999 /* Bind ENTER on the Login button, but only when one of the two
1000 * textareas (or the button itself) has focus. The focused
1001 * variant avoids triggering Login when ENTER is pressed in an
1002 * unrelated textarea elsewhere in the application. */
1003 int login_sources[2] = {txt_user, txt_pass};
1004 n_gui_button_set_keycode_focused(gui, btn_login, ALLEGRO_KEY_ENTER, 0,
1005 login_sources, 2);
1006
1007 /* trigger autofit calculation */
1008 n_gui_window_apply_autofit(gui, win_login);
1009
1010 /* set initial focus to username field */
1011 n_gui_set_focus(gui, txt_user);
1012 }
1013
1014 /* enable adaptive resize mode */
1015 /* Switch from virtual canvas scaling to adaptive per-window resize.
1016 * Each window gets its own policy: SCALE (repositions + resizes),
1017 * MOVE (repositions only), or NONE (stays fixed). */
1019
1034
1035 /* main loop */
1036 al_start_timer(fps_timer);
1037 /* Redraw only when the GUI reports it needs it (n_gui_needs_redraw) or when
1038 * this example's own mouse-following overlay moved. An idle scene with no
1039 * input is then skipped entirely, which is the point of the redraw signal.
1040 * A host that paints no overlay of its own could gate purely on
1041 * n_gui_needs_redraw(gui). */
1042 int app_needs_redraw = 1; /* always paint the first frame */
1043 int mx = 0, my = 0;
1044 /* last text pushed into the status/projection/coord labels: the timer
1045 * refreshes a label (and marks the GUI dirty) only when it actually
1046 * changes, so a still, unchanged scene can idle instead of redrawing. */
1047 char last_status[256] = "", last_proj[128] = "", last_coord[160] = "";
1048
1049 al_clear_keyboard_state(NULL);
1050 al_flush_event_queue(event_queue);
1051
1052 do {
1053 ALLEGRO_EVENT ev;
1054 al_wait_for_event(event_queue, &ev);
1055
1056 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
1057 DONE = 1;
1058 }
1059 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
1060 DONE = 1;
1061 }
1062
1063 /* handle display resize: in adaptive mode, windows reposition/resize
1064 * according to their individual policies (NONE/MOVE/SCALE) */
1065 if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
1066 al_acknowledge_resize(display);
1067 int new_w = al_get_display_width(display);
1068 int new_h = al_get_display_height(display);
1069 n_gui_set_display_size(gui, (float)new_w, (float)new_h);
1070 }
1071
1072 /* pass events to the GUI, check if the event was consumed.
1073 * Use gui_handled to prevent mouse events from reaching game logic
1074 * when the cursor is over a GUI window or widget.
1075 * Example: if (!gui_handled && ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { ... } */
1076 int gui_handled = n_gui_process_event(gui, ev);
1077 (void)gui_handled;
1078
1079 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
1080 mx = ev.mouse.x;
1081 my = ev.mouse.y;
1082 app_needs_redraw = 1; /* the crosshair overlay follows the pointer */
1083 }
1084
1085 if (ev.type == ALLEGRO_EVENT_TIMER) {
1086 /* update toggle status labels */
1087 char status_buf[256];
1088 snprintf(status_buf, sizeof(status_buf),
1089 "Player:%s Height:%s Grid:%s Smooth:%s Ghost:%s",
1090 player_mode ? "ON" : "off",
1091 height_mode ? "ON" : "off",
1092 show_grid ? "ON" : "off",
1093 smooth_height ? "ON" : "off",
1094 ghost_enabled ? "ON" : "off");
1095 /* Push each label only when its text changed, and tell the GUI so:
1096 * n_gui_label_set_text is a mutation made outside n_gui_process_event
1097 * (from this timer), which the dirty bit would otherwise miss, so it
1098 * is paired with n_gui_mark_dirty. See the n_gui_mark_dirty docs. */
1099 if (strcmp(last_status, status_buf) != 0) {
1100 n_gui_label_set_text(gui, lbl_status, status_buf);
1101 snprintf(last_status, sizeof(last_status), "%s", status_buf);
1103 }
1104
1105 char proj_buf[128];
1106 snprintf(proj_buf, sizeof(proj_buf), "Projection: %s", proj_names[current_proj]);
1107 if (strcmp(last_proj, proj_buf) != 0) {
1108 n_gui_label_set_text(gui, lbl_proj, proj_buf);
1109 snprintf(last_proj, sizeof(last_proj), "%s", proj_buf);
1111 }
1112
1113 /* Display the screen->virtual coordinate conversion in real
1114 * time so the helper's effect (identity when virtual canvas
1115 * is disabled, scaled when enabled) is visible without
1116 * inspecting the cursor overlay. */
1117 float vmx_lbl = 0, vmy_lbl = 0;
1118 n_gui_screen_to_virtual(gui, (float)mx, (float)my, &vmx_lbl, &vmy_lbl);
1119 char coord_buf[160];
1120 snprintf(coord_buf, sizeof(coord_buf),
1121 "Coords: screen=(%d,%d) virtual=(%.1f,%.1f) "
1122 "(via n_gui_screen_to_virtual)",
1123 mx, my, (double)vmx_lbl, (double)vmy_lbl);
1124 if (strcmp(last_coord, coord_buf) != 0) {
1125 n_gui_label_set_text(gui, lbl_coords, coord_buf);
1126 snprintf(last_coord, sizeof(last_coord), "%s", coord_buf);
1128 }
1129 }
1130
1131 /* Redraw when the GUI changed or is animating (n_gui_needs_redraw), or
1132 * when our own overlay moved. Otherwise skip the frame: input drives the
1133 * redraws, so an untouched GUI costs nothing. */
1134 if ((n_gui_needs_redraw(gui) || app_needs_redraw) && al_is_event_queue_empty(event_queue)) {
1135 al_set_target_bitmap(al_get_backbuffer(display));
1136 al_clear_to_color(al_map_rgba(30, 30, 35, 255));
1137
1138 /* draw all GUI windows and widgets */
1139 n_gui_draw(gui);
1140
1141 /* mouse cursor crosshair (transform screen coords to virtual) */
1142 {
1143 float vmx, vmy;
1144 n_gui_screen_to_virtual(gui, (float)mx, (float)my, &vmx, &vmy);
1145 /* apply the virtual canvas transform for drawing */
1146 ALLEGRO_TRANSFORM overlay_tf;
1147 al_identity_transform(&overlay_tf);
1148 if (gui->virtual_w > 0 && gui->gui_scale > 0) {
1149 al_scale_transform(&overlay_tf, gui->gui_scale, gui->gui_scale);
1150 al_translate_transform(&overlay_tf, gui->gui_offset_x, gui->gui_offset_y);
1151 }
1152 al_use_transform(&overlay_tf);
1153 /* ensure crosshair is at least 1 physical pixel thick */
1154 float cross_thick = 1.0f;
1155 if (gui->gui_scale > 0.01f && gui->gui_scale < 1.0f) {
1156 cross_thick = 1.0f / gui->gui_scale;
1157 }
1158 al_draw_line(vmx - 6, vmy, vmx + 6, vmy, al_map_rgb(255, 100, 100), cross_thick);
1159 al_draw_line(vmx, vmy - 6, vmx, vmy + 6, al_map_rgb(255, 100, 100), cross_thick);
1160 }
1161
1162 /* instructions (drawn in virtual space) */
1163 {
1164 /* explicitly set virtual canvas transform so this block is
1165 * self-contained and does not depend on previous drawing state */
1166 ALLEGRO_TRANSFORM instr_tf;
1167 al_identity_transform(&instr_tf);
1168 if (gui->virtual_w > 0 && gui->virtual_h > 0 && gui->gui_scale > 0) {
1169 al_scale_transform(&instr_tf, gui->gui_scale, gui->gui_scale);
1170 al_translate_transform(&instr_tf, gui->gui_offset_x, gui->gui_offset_y);
1171 }
1172 al_use_transform(&instr_tf);
1173 float virt_h = (gui->virtual_h > 0) ? gui->virtual_h : (float)al_get_display_height(display);
1174 al_draw_text(font, al_map_rgb(180, 180, 180), 10, virt_h - 18, 0,
1175 "Drag windows | Click widgets | Scroll lists | Resize corners/display | 'Windows' dropdown | ESC to quit");
1176 /* restore identity transform */
1177 ALLEGRO_TRANSFORM identity;
1178 al_identity_transform(&identity);
1179 al_use_transform(&identity);
1180 }
1181
1182 al_flip_display();
1183 app_needs_redraw = 0;
1184 }
1185 } while (!DONE);
1186
1187 /* cleanup, destroy GUI context first, then bitmaps (N_GUI does not own them) */
1189 if (img_192) al_destroy_bitmap(img_192);
1190 if (img_32) al_destroy_bitmap(img_32);
1191 /* skin bitmaps: destroy after GUI context since N_GUI only stores pointers */
1192 if (skin_win_bg) al_destroy_bitmap(skin_win_bg);
1193 if (skin_win_tb) al_destroy_bitmap(skin_win_tb);
1194 if (skin_sld_track) al_destroy_bitmap(skin_sld_track);
1195 if (skin_sld_fill) al_destroy_bitmap(skin_sld_fill);
1196 if (skin_sld_handle) al_destroy_bitmap(skin_sld_handle);
1197 if (skin_sld_handle_h) al_destroy_bitmap(skin_sld_handle_h);
1198 if (skin_chk_unchecked) al_destroy_bitmap(skin_chk_unchecked);
1199 if (skin_chk_checked) al_destroy_bitmap(skin_chk_checked);
1200 /* Window 16 ("Bitmap Skinning II") + titlebar Close-button bitmaps */
1201 if (skin_btn_normal) al_destroy_bitmap(skin_btn_normal);
1202 if (skin_btn_hover) al_destroy_bitmap(skin_btn_hover);
1203 if (skin_btn_active) al_destroy_bitmap(skin_btn_active);
1204 if (skin_list_bg) al_destroy_bitmap(skin_list_bg);
1205 if (skin_list_item) al_destroy_bitmap(skin_list_item);
1206 if (skin_list_sel) al_destroy_bitmap(skin_list_sel);
1207 if (skin_drop_panel) al_destroy_bitmap(skin_drop_panel);
1208 if (skin_drop_hover) al_destroy_bitmap(skin_drop_hover);
1209 if (skin_text_bg) al_destroy_bitmap(skin_text_bg);
1210 if (skin_tb_close_n) al_destroy_bitmap(skin_tb_close_n);
1211 if (skin_tb_close_h) al_destroy_bitmap(skin_tb_close_h);
1212 if (skin_tb_close_a) al_destroy_bitmap(skin_tb_close_a);
1213 al_destroy_font(font);
1214 al_destroy_timer(fps_timer);
1215 al_destroy_event_queue(event_queue);
1216 al_destroy_display(display);
1217 al_uninstall_system();
1218
1219 return 0;
1220}
int main(void)
Definition ex_atlas.c:117
static int log_level
ALLEGRO_TIMER * fps_timer
Definition ex_fluid.c:65
int getoptret
Definition ex_fluid.c:59
int DONE
Definition ex_fluid.c:58
ALLEGRO_DISPLAY * display
Definition ex_fluid.c:54
void on_listbox_select(int widget_id, int index, int selected, void *user_data)
Definition ex_gui.c:174
static int proj_btn_ids[4]
Definition ex_gui.c:76
#define NUM_WINDOWS
Definition ex_gui.c:79
void on_link_click(int widget_id, const char *link, void *user_data)
Definition ex_gui.c:192
void on_grid_toggle(int widget_id, void *user_data)
Definition ex_gui.c:211
void on_player_toggle(int widget_id, void *user_data)
Definition ex_gui.c:199
void on_login_click(int widget_id, void *user_data)
Definition ex_gui.c:146
#define WIDTH
Definition ex_gui.c:42
void on_window_toggle_click(int widget_id, int entry_index, int tag, void *user_data)
Definition ex_gui.c:246
static int smooth_height
Definition ex_gui.c:65
void on_checkbox_toggle(int widget_id, int checked, void *user_data)
Definition ex_gui.c:121
void on_slider_change(int widget_id, double value, void *user_data)
Definition ex_gui.c:115
void on_button_click(int widget_id, void *user_data)
Definition ex_gui.c:101
static char theme_file[512]
Definition ex_gui.c:59
void on_windows_menu_open(int widget_id, void *user_data)
Definition ex_gui.c:255
void on_smooth_toggle(int widget_id, void *user_data)
Definition ex_gui.c:217
static int show_grid
Definition ex_gui.c:64
static int lbl_status
Definition ex_gui.c:72
void on_save_layout_click(int widget_id, void *user_data)
Definition ex_gui.c:152
void on_load_layout_click(int widget_id, void *user_data)
Definition ex_gui.c:163
void on_radiolist_select(int widget_id, int index, void *user_data)
Definition ex_gui.c:180
static int dropmenu_windows_id
Definition ex_gui.c:84
void on_combobox_select(int widget_id, int index, void *user_data)
Definition ex_gui.c:186
void on_ghost_toggle(int widget_id, void *user_data)
Definition ex_gui.c:223
static const char * proj_names[]
Definition ex_gui.c:69
void on_proj_select(int widget_id, void *user_data)
Definition ex_gui.c:230
static int player_mode
Definition ex_gui.c:62
void on_height_toggle(int widget_id, void *user_data)
Definition ex_gui.c:205
static int ghost_enabled
Definition ex_gui.c:66
static int lbl_proj
Definition ex_gui.c:73
void on_text_change(int widget_id, const char *text, void *user_data)
Definition ex_gui.c:127
void on_quit_click(int widget_id, void *user_data)
Definition ex_gui.c:139
static int height_mode
Definition ex_gui.c:63
static int lbl_coords
Definition ex_gui.c:88
void on_scroll(int widget_id, double pos, void *user_data)
Definition ex_gui.c:133
static int buttons_window
Definition ex_gui.c:96
static int win_ids[18]
Definition ex_gui.c:80
static int current_proj
Definition ex_gui.c:67
#define HEIGHT
Definition ex_gui.c:43
#define EX_GUI_LAYOUT_FILE
Definition ex_gui.c:93
static const char * win_names[18]
Definition ex_gui.c:81
static N_GUI_CTX * gui
static int clicks
static const char * row(N_GUI_TREE *tree, int index)
the text of a visible row, "" past the end
static N_HTTP_RESP * text(int status, const char *body)
text response
volatile sig_atomic_t done
FONT * font
the default 8x8 font, valid after allegro_init()
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:58
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
Definition n_gui.c:12348
void n_gui_combobox_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg, ALLEGRO_BITMAP *item_bg, ALLEGRO_BITMAP *item_selected)
Set optional bitmap overlays on a combobox widget.
Definition n_gui.c:6732
int n_gui_load_theme_json(N_GUI_CTX *ctx, const char *filepath)
load a theme and style from a JSON file
Definition n_gui.c:16796
int n_gui_button_is_toggled(N_GUI_CTX *ctx, int widget_id)
Check if a toggle button is currently in the "on" state.
Definition n_gui.c:2806
float n_gui_detect_dpi_scale(N_GUI_CTX *ctx, ALLEGRO_DISPLAY *display)
Detect and apply DPI scale from an Allegro display.
Definition n_gui.c:12420
void n_gui_toggle_window(N_GUI_CTX *ctx, int window_id)
Toggle window visibility (show if hidden, hide if shown).
Definition n_gui.c:2497
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
#define N_GUI_IMAGE_FIT
scale to fit within bounds, keep aspect ratio
Definition n_gui.h:204
int n_gui_add_slider(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, double min_val, double max_val, double initial, int mode, void(*on_change)(int, double, void *), void *user_data)
Add a slider widget.
Definition n_gui.c:3106
void n_gui_window_apply_autofit(N_GUI_CTX *ctx, int window_id)
Trigger auto-fit recalculation for a window.
Definition n_gui.c:2582
int n_gui_needs_redraw(N_GUI_CTX *ctx)
Return non-zero if the GUI must be redrawn this frame, i.e.
Definition n_gui.c:1192
void n_gui_textarea_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text content of a textarea widget
Definition n_gui.c:4210
int n_gui_save_layout_json(N_GUI_CTX *ctx, const char *filepath)
save the geometry + persistent state of every window to a JSON file
Definition n_gui.c:16950
#define N_GUI_SLIDER_PERCENT
slider uses 0-100 percentage
Definition n_gui.h:180
int n_gui_add_vslider(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, double min_val, double max_val, double initial, int mode, void(*on_change)(int, double, void *), void *user_data)
Add a vertical slider widget.
Definition n_gui.c:3148
void n_gui_set_display(N_GUI_CTX *ctx, ALLEGRO_DISPLAY *display)
Set the display pointer for clipboard operations (copy/paste).
Definition n_gui.c:12378
void n_gui_window_set_bitmaps(N_GUI_CTX *ctx, int window_id, ALLEGRO_BITMAP *bg, ALLEGRO_BITMAP *titlebar, int bg_scale_mode)
Set optional bitmap overlays on a window's body and titlebar.
Definition n_gui.c:6591
void n_gui_set_widget_theme(N_GUI_CTX *ctx, int widget_id, N_GUI_THEME theme)
Override the theme of a specific widget.
Definition n_gui.c:3795
int n_gui_radiolist_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a radiolist widget
Definition n_gui.c:5930
void n_gui_label_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set label text content
Definition n_gui.c:6184
int n_gui_add_listbox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int selection_mode, void(*on_select)(int, int, int, void *), void *user_data)
Add a listbox widget.
Definition n_gui.c:3285
#define N_GUI_WIN_RESIZE_SCALE
reposition AND resize proportionally, child widgets scale too
Definition n_gui.h:333
#define N_GUI_SLIDER_VALUE
slider uses raw start/end values
Definition n_gui.h:178
void n_gui_window_set_tb_button_bitmaps(N_GUI_CTX *ctx, int window_id, int btn_type, ALLEGRO_BITMAP *normal, ALLEGRO_BITMAP *hover, ALLEGRO_BITMAP *active)
Set optional bitmap overlays on a titlebar button.
Definition n_gui.c:1957
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:13611
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
Definition n_gui.c:3639
void n_gui_set_widget_enabled(N_GUI_CTX *ctx, int widget_id, int enabled)
Enable or disable a widget.
Definition n_gui.c:3887
int n_gui_add_radiolist(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, int, void *), void *user_data)
Add a radio list widget (single selection with radio bullets).
Definition n_gui.c:3512
#define N_GUI_WIN_FIXED_POSITION
disable window dragging (default:enable)
Definition n_gui.h:274
void n_gui_listbox_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg, ALLEGRO_BITMAP *item_bg, ALLEGRO_BITMAP *item_selected)
Set optional bitmap overlays on a listbox widget.
Definition n_gui.c:6700
void n_gui_window_set_resize_policy(N_GUI_CTX *ctx, int window_id, int policy)
Set per-window resize policy (N_GUI_WIN_RESIZE_NONE / _MOVE / _SCALE).
Definition n_gui.c:12132
void n_gui_button_set_keycode_focused(N_GUI_CTX *ctx, int widget_id, int keycode, int modifiers, const int *sources, int source_count)
Set a focused key binding on a button.
Definition n_gui.c:3085
void n_gui_listbox_set_selected(N_GUI_CTX *ctx, int widget_id, int index, int selected)
set the selection state of a listbox item
Definition n_gui.c:4491
int n_gui_add_window_auto(N_GUI_CTX *ctx, const char *title, float x, float y)
Add a window with automatic sizing (use n_gui_window_autosize after adding widgets).
Definition n_gui.c:2489
int n_gui_listbox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a listbox widget
Definition n_gui.c:4359
void n_gui_window_set_caption(N_GUI_CTX *ctx, int window_id, const char *caption)
Show a caption in a window's title bar in place of its title.
Definition n_gui.c:2442
void n_gui_set_virtual_size(N_GUI_CTX *ctx, float w, float h)
Set the virtual canvas size for resolution-independent scaling.
Definition n_gui.c:12027
#define N_GUI_WIN_RESIZE_NONE
no adaptation: absolute position and size unchanged
Definition n_gui.h:329
#define N_GUI_AUTOFIT_WH
auto-adjust both width and height (convenience: W|H)
Definition n_gui.h:341
#define N_GUI_SELECT_SINGLE
single item selection
Definition n_gui.h:198
#define N_GUI_TB_BTN_CLOSE
close titlebar button
Definition n_gui.h:319
int n_gui_combobox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a combo box
Definition n_gui.c:6012
void n_gui_window_autosize(N_GUI_CTX *ctx, int window_id)
Recompute and apply minimum-fit size for a window based on its current widgets.
Definition n_gui.c:2534
void n_gui_set_focus(N_GUI_CTX *ctx, int widget_id)
Set keyboard focus to a specific widget.
Definition n_gui.c:3908
#define N_GUI_SELECT_MULTIPLE
multiple item selection
Definition n_gui.h:200
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
Definition n_gui.h:276
int n_gui_add_combobox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, int, void *), void *user_data)
Add a combo box widget (dropdown selector).
Definition n_gui.c:3547
void n_gui_button_set_toggled(N_GUI_CTX *ctx, int widget_id, int toggled)
Set the toggle state of a button.
Definition n_gui.c:3043
int n_gui_add_toggle_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, int initial_state, void(*on_click)(int, void *), void *user_data)
Add a toggle button widget (stays clicked/unclicked on single click).
Definition n_gui.c:2790
int n_gui_add_label_link(N_GUI_CTX *ctx, int window_id, const char *text, const char *link, float x, float y, float w, float h, int align, void(*on_link_click)(int, const char *, void *), void *user_data)
Add a static text label with hyperlink.
Definition n_gui.c:3680
int n_gui_add_image(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, ALLEGRO_BITMAP *bitmap, int scale_mode)
Add an image display widget.
Definition n_gui.c:3610
void n_gui_label_set_bitmap(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg)
Set optional background bitmap on a label widget.
Definition n_gui.c:6763
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
Definition n_gui.c:11735
void n_gui_scrollbar_set_wheel_step(N_GUI_CTX *ctx, int widget_id, double step)
set how far one wheel notch moves a scrollbar
Definition n_gui.c:4323
#define N_GUI_AUTOFIT_CENTER
center the window on its insertion point after auto-fit (overrides EXPAND_LEFT/EXPAND_UP for centerin...
Definition n_gui.h:347
void n_gui_slider_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *track, ALLEGRO_BITMAP *fill, ALLEGRO_BITMAP *handle, ALLEGRO_BITMAP *handle_hover, ALLEGRO_BITMAP *handle_active)
Set optional bitmap overlays on a slider widget.
Definition n_gui.c:6606
#define N_GUI_SCROLLBAR_H
horizontal scrollbar
Definition n_gui.h:190
int n_gui_dropmenu_add_dynamic_entry(N_GUI_CTX *ctx, int widget_id, const char *text, int tag, void(*on_click)(int, int, int, void *), void *user_data)
Add a dynamic entry (rebuilt each time menu opens).
Definition n_gui.c:6398
void n_gui_button_set_keycode(N_GUI_CTX *ctx, int widget_id, int keycode, int modifiers)
Bind a keyboard key with optional modifier requirements to a button.
Definition n_gui.c:3075
int n_gui_window_is_open(N_GUI_CTX *ctx, int window_id)
Check if a window is currently visible.
Definition n_gui.c:2506
int n_gui_dropmenu_add_entry(N_GUI_CTX *ctx, int widget_id, const char *text, int tag, void(*on_click)(int, int, int, void *), void *user_data)
Add a static entry to a dropdown menu.
Definition n_gui.c:6375
#define N_GUI_IMAGE_STRETCH
stretch to fill bounds
Definition n_gui.h:206
void n_gui_window_set_wheel_scrollbar(N_GUI_CTX *ctx, int window_id, int scrollbar_id)
Make the wheel anywhere over a window move one of its scrollbars.
Definition n_gui.c:2458
void n_gui_dropmenu_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *panel, ALLEGRO_BITMAP *item_hover)
Set optional bitmap overlays on a dropmenu widget.
Definition n_gui.c:6748
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:1277
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
void n_gui_radiolist_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg, ALLEGRO_BITMAP *item_bg, ALLEGRO_BITMAP *item_selected)
Set optional bitmap overlays on a radiolist widget.
Definition n_gui.c:6716
int n_gui_add_button_bitmap(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, ALLEGRO_BITMAP *normal, ALLEGRO_BITMAP *hover, ALLEGRO_BITMAP *active, void(*on_click)(int, void *), void *user_data)
Add a bitmap-based button.
Definition n_gui.c:2762
#define N_GUI_IMAGE_CENTER
draw at original size, centered
Definition n_gui.h:208
void n_gui_set_resize_mode(N_GUI_CTX *ctx, int mode)
Set context-level resize mode: N_GUI_RESIZE_VIRTUAL (default) or N_GUI_RESIZE_ADAPTIVE.
Definition n_gui.c:12093
#define N_GUI_ALIGN_CENTER
center aligned text
Definition n_gui.h:214
void n_gui_textarea_set_bitmap(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg)
Set optional background bitmap on a textarea widget.
Definition n_gui.c:6657
int n_gui_add_scrollbar(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int orientation, int shape, double content_size, double viewport_size, void(*on_scroll)(int, double, void *), void *user_data)
Add a scrollbar widget.
Definition n_gui.c:3241
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
void n_gui_checkbox_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *box, ALLEGRO_BITMAP *box_checked, ALLEGRO_BITMAP *box_hover)
Set optional bitmap overlays on a checkbox widget.
Definition n_gui.c:6641
#define N_GUI_WIN_NO_HSCROLL
with N_GUI_WIN_AUTO_SCROLLBAR, never show the horizontal scrollbar (vertical-only scrolling): content...
Definition n_gui.h:287
#define N_GUI_WIN_RESIZE_MOVE
reposition proportionally, keep pixel size
Definition n_gui.h:331
int n_gui_load_layout_json(N_GUI_CTX *ctx, const char *filepath)
load a JSON window layout file and apply it to matching windows
Definition n_gui.c:17108
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
Definition n_gui.h:172
#define N_GUI_ALIGN_RIGHT
right aligned text
Definition n_gui.h:216
void n_gui_radiolist_set_selected(N_GUI_CTX *ctx, int widget_id, int index)
set the selected item in a radiolist widget
Definition n_gui.c:5981
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
Definition n_gui.c:2515
#define N_GUI_ALIGN_JUSTIFIED
justified text (spread words to fill width)
Definition n_gui.h:218
void n_gui_set_widget_visible(N_GUI_CTX *ctx, int widget_id, int visible)
Show or hide a widget.
Definition n_gui.c:3828
void n_gui_mark_dirty(N_GUI_CTX *ctx)
Mark the GUI as needing a redraw.
Definition n_gui.c:1188
#define N_GUI_ID_MAX
maximum length for widget id/name strings
Definition n_gui.h:86
#define N_GUI_WIN_AUTO_SCROLLBAR
enable automatic scrollbars when content exceeds window size
Definition n_gui.h:270
#define N_GUI_RESIZE_ADAPTIVE
virtual size tracks display; windows adapt per their resize_policy
Definition n_gui.h:325
int n_gui_add_textarea(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int multiline, size_t char_limit, void(*on_change)(int, const char *, void *), void *user_data)
Add a text area widget.
Definition n_gui.c:3162
int n_gui_add_checkbox(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int initial_checked, void(*on_toggle)(int, int, void *), void *user_data)
Add a checkbox widget.
Definition n_gui.c:3207
#define N_GUI_SHAPE_RECT
rectangle shape (default)
Definition n_gui.h:170
void n_gui_combobox_set_selected(N_GUI_CTX *ctx, int widget_id, int index)
set the selected item in a combobox widget
Definition n_gui.c:6116
#define N_GUI_SCROLLBAR_V
vertical scrollbar
Definition n_gui.h:192
N_GUI_THEME n_gui_make_theme(ALLEGRO_COLOR bg, ALLEGRO_COLOR bg_hover, ALLEGRO_COLOR bg_active, ALLEGRO_COLOR border, ALLEGRO_COLOR border_hover, ALLEGRO_COLOR border_active, ALLEGRO_COLOR text, ALLEGRO_COLOR text_hover, ALLEGRO_COLOR text_active, float border_thickness, float corner_rx, float corner_ry)
Create a custom theme with explicit colours.
Definition n_gui.c:825
int n_gui_add_dropmenu(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, void(*on_open)(int, void *), void *on_open_user_data)
Add a dropdown menu widget.
Definition n_gui.c:6297
void n_gui_screen_to_virtual(const N_GUI_CTX *ctx, float sx, float sy, float *vx, float *vy)
Convert physical screen coordinates to virtual canvas coordinates.
Definition n_gui.c:12072
int n_gui_add_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, void(*on_click)(int, void *), void *user_data)
Add a button widget to a window.
Definition n_gui.c:2720
#define N_GUI_WIN_RESIZABLE
enable user-resizable window with a drag handle at bottom-right
Definition n_gui.h:272
void n_gui_window_set_autofit(N_GUI_CTX *ctx, int window_id, int autofit_flags, float border)
Configure auto-fit behavior for a dialog window.
Definition n_gui.c:2563
void n_gui_dropmenu_clear_dynamic(N_GUI_CTX *ctx, int widget_id)
Remove all dynamic entries (keep static ones).
Definition n_gui.c:6423
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
Color theme for a widget.
Definition n_gui.h:384
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_DEBUG
debug-level messages
Definition n_log.h:108
#define LOG_ERR
error conditions
Definition n_log.h:100
int set_log_file(char *file)
Set the logging to a file instead of stderr.
Definition n_log.c:170
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_INFO
informational
Definition n_log.h:106
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.