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 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
36#define WIDTH 1280
37#define HEIGHT 900
38
39#define ALLEGRO_UNSTABLE 1
40#define HAVE_CJSON 1
41
42#include "nilorea/n_gui.h"
43#include "cJSON.h"
44
45ALLEGRO_DISPLAY* display = NULL;
46
47int DONE = 0,
50
51static char theme_file[512] = "";
52
53/* ---- application state driven by toggle buttons ---- */
54static int player_mode = 0;
55static int height_mode = 0;
56static int show_grid = 0;
57static int smooth_height = 0;
58static int ghost_enabled = 0;
59static int current_proj = 0; /* 0=Classic, 1=Iso, 2=Military, 3=Staggered */
60
61static const char* proj_names[] = {"Classic 2:1", "True Isometric", "Military", "Staggered"};
62
63/* widget ids for status display */
64static int lbl_status = -1;
65static int lbl_proj = -1;
66
67/* projection toggle button ids for radio group */
68static int proj_btn_ids[4] = {-1, -1, -1, -1};
69
70/* window ids (needed for show/hide dropdown) */
71#define NUM_WINDOWS 16
72static int win_ids[NUM_WINDOWS];
73static const char* win_names[NUM_WINDOWS];
74
75/* dropdown menu widget id for show/hide */
76static int dropmenu_windows_id = -1;
77
78/* callback: button click */
79void on_button_click(int widget_id, void* user_data) {
80 (void)user_data;
81 n_log(LOG_NOTICE, "Button %d clicked!", widget_id);
82}
83
84/* callback: slider change */
85void on_slider_change(int widget_id, double value, void* user_data) {
86 (void)user_data;
87 n_log(LOG_NOTICE, "Slider %d value: %.2f", widget_id, value);
88}
89
90/* callback: checkbox toggle */
91void on_checkbox_toggle(int widget_id, int checked, void* user_data) {
92 (void)user_data;
93 n_log(LOG_NOTICE, "Checkbox %d: %s", widget_id, checked ? "checked" : "unchecked");
94}
95
96/* callback: textarea change */
97void on_text_change(int widget_id, const char* text, void* user_data) {
98 (void)user_data;
99 n_log(LOG_NOTICE, "Textarea %d: \"%s\"", widget_id, text);
100}
101
102/* callback: scrollbar scroll */
103void on_scroll(int widget_id, double pos, void* user_data) {
104 (void)user_data;
105 n_log(LOG_NOTICE, "Scrollbar %d pos: %.2f", widget_id, pos);
106}
107
108/* callback: quit button */
109void on_quit_click(int widget_id, void* user_data) {
110 (void)widget_id;
111 int* done = (int*)user_data;
112 *done = 1;
113}
114
115/* callback: login button */
116void on_login_click(int widget_id, void* user_data) {
117 (void)user_data;
118 n_log(LOG_NOTICE, "Login button %d clicked!", widget_id);
119}
120
121/* callback: listbox selection */
122void on_listbox_select(int widget_id, int index, int selected, void* user_data) {
123 (void)user_data;
124 n_log(LOG_NOTICE, "Listbox %d: item %d %s", widget_id, index, selected ? "selected" : "deselected");
125}
126
127/* callback: radiolist selection */
128void on_radiolist_select(int widget_id, int index, void* user_data) {
129 (void)user_data;
130 n_log(LOG_NOTICE, "Radiolist %d: selected item %d", widget_id, index);
131}
132
133/* callback: combobox selection */
134void on_combobox_select(int widget_id, int index, void* user_data) {
135 (void)user_data;
136 n_log(LOG_NOTICE, "Combobox %d: selected item %d", widget_id, index);
137}
138
139/* callback: label link click */
140void on_link_click(int widget_id, const char* link, void* user_data) {
141 (void)user_data;
142 n_log(LOG_NOTICE, "Link %d clicked: %s", widget_id, link);
143}
144
145/* ---- toggle button callbacks ---- */
146
147void on_player_toggle(int widget_id, void* user_data) {
148 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
150 n_log(LOG_NOTICE, "Player mode: %s", player_mode ? "ON" : "OFF");
151}
152
153void on_height_toggle(int widget_id, void* user_data) {
154 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
156 n_log(LOG_NOTICE, "Height mode: %s", height_mode ? "ON" : "OFF");
157}
158
159void on_grid_toggle(int widget_id, void* user_data) {
160 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
162 n_log(LOG_NOTICE, "Grid: %s", show_grid ? "ON" : "OFF");
163}
164
165void on_smooth_toggle(int widget_id, void* user_data) {
166 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
168 n_log(LOG_NOTICE, "Height rendering: %s", smooth_height ? "SMOOTH" : "CUT");
169}
170
171void on_ghost_toggle(int widget_id, void* user_data) {
172 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
174 n_log(LOG_NOTICE, "DR Ghost: %s", ghost_enabled ? "ON" : "OFF");
175}
176
177/* projection buttons: act as a radio group using toggle buttons */
178void on_proj_select(int widget_id, void* user_data) {
179 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
180 for (int i = 0; i < 4; i++) {
181 if (proj_btn_ids[i] == widget_id) {
182 current_proj = i;
184 } else {
186 }
187 }
188 n_log(LOG_NOTICE, "Projection: %s", proj_names[current_proj]);
189}
190
191/* ---- dropdown menu: show/hide windows ---- */
192
193/* callback when a window toggle entry is clicked in the dropdown */
194void on_window_toggle_click(int widget_id, int entry_index, int tag, void* user_data) {
195 (void)widget_id;
196 (void)entry_index;
197 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
199 n_log(LOG_NOTICE, "Toggled window id %d", tag);
200}
201
202/* callback to rebuild dynamic entries when the dropdown opens */
203void on_windows_menu_open(int widget_id, void* user_data) {
204 N_GUI_CTX* gui = (N_GUI_CTX*)user_data;
205
206 /* clear old dynamic entries */
208
209 /* add one dynamic entry per window showing its show/hide state */
210 for (int i = 0; i < NUM_WINDOWS; i++) {
211 if (win_ids[i] < 0) continue;
212 char buf[N_GUI_ID_MAX];
213 int is_open = n_gui_window_is_open(gui, win_ids[i]);
214 snprintf(buf, sizeof(buf), "[%s] %s", is_open ? "X" : " ", win_names[i]);
215 n_gui_dropmenu_add_dynamic_entry(gui, widget_id, buf, win_ids[i],
217 }
218}
219
220int main(int argc, char* argv[]) {
222
223 n_log(LOG_NOTICE, "%s is starting ...", argv[0]);
224
225 /* allegro 5 + addons loading */
226 if (!al_init()) {
227 n_abort("Could not init Allegro.\n");
228 }
229 if (!al_install_audio()) {
230 n_log(LOG_ERR, "Unable to initialize audio addon");
231 }
232 if (!al_init_acodec_addon()) {
233 n_log(LOG_ERR, "Unable to initialize audio codec addon");
234 }
235 if (!al_init_image_addon()) {
236 n_abort("Unable to initialize image addon\n");
237 }
238 if (!al_init_primitives_addon()) {
239 n_abort("Unable to initialize primitives addon\n");
240 }
241 if (!al_init_font_addon()) {
242 n_abort("Unable to initialize font addon\n");
243 }
244 if (!al_init_ttf_addon()) {
245 n_abort("Unable to initialize ttf_font addon\n");
246 }
247 if (!al_install_keyboard()) {
248 n_abort("Unable to initialize keyboard handler\n");
249 }
250 if (!al_install_mouse()) {
251 n_abort("Unable to initialize mouse handler\n");
252 }
253
254 /* parse command line */
255 char ver_str[128] = "";
256 while ((getoptret = getopt(argc, argv, "hvV:L:t:")) != EOF) {
257 switch (getoptret) {
258 case 'h':
259 n_log(LOG_NOTICE, "\n %s -h help -v version -V DEBUGLEVEL -L logfile -t theme.json", argv[0]);
260 exit(TRUE);
261 case 'v':
262 sprintf(ver_str, "%s %s", __DATE__, __TIME__);
263 exit(TRUE);
264 break;
265 case 'V':
266 if (!strncmp("NOTICE", optarg, 6)) {
268 } else if (!strncmp("VERBOSE", optarg, 7)) {
270 } else if (!strncmp("ERROR", optarg, 5)) {
272 } else if (!strncmp("DEBUG", optarg, 5)) {
274 } else {
275 n_log(LOG_ERR, "%s is not a valid log level", optarg);
276 exit(FALSE);
277 }
279 break;
280 case 'L':
281 set_log_file(optarg);
282 break;
283 case 't':
284 strncpy(theme_file, optarg, sizeof(theme_file) - 1);
285 theme_file[sizeof(theme_file) - 1] = '\0';
286 break;
287 default:
288 n_log(LOG_ERR, "\n %s -h help -v version -V DEBUGLEVEL -L logfile -t theme.json", argv[0]);
289 exit(FALSE);
290 }
291 }
292
293 ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
294 if (!event_queue) {
295 n_abort("Failed to create event queue!\n");
296 }
297
298 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
299 display = al_create_display(WIDTH, HEIGHT);
300 if (!display) {
301 n_abort("Unable to create display\n");
302 }
303 al_set_window_title(display, "Nilorea GUI Demo - All Widgets + Dropdown Menus");
304
305 ALLEGRO_TIMER* fps_timer = al_create_timer(1.0 / 60.0);
306
307 al_register_event_source(event_queue, al_get_display_event_source(display));
308 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
309 al_register_event_source(event_queue, al_get_keyboard_event_source());
310 al_register_event_source(event_queue, al_get_mouse_event_source());
311 al_hide_mouse_cursor(display);
312
313 /* load a built-in font */
314 ALLEGRO_FONT* font = al_create_builtin_font();
315 if (!font) {
316 n_abort("Unable to create builtin font\n");
317 }
318
319 /* ---- create the GUI ---- */
320 N_GUI_CTX* gui = n_gui_new_ctx(font);
321
322 /* set display size for global scrollbars (shown when GUI exceeds display) */
323 n_gui_set_display_size(gui, (float)WIDTH, (float)HEIGHT);
324
325 /* set display pointer for clipboard operations (copy/paste in text areas) */
327
328 /* enable virtual canvas: widget coordinates stay in WIDTH x HEIGHT space,
329 * the GUI scales uniformly to fit the actual display size */
330 n_gui_set_virtual_size(gui, (float)WIDTH, (float)HEIGHT);
331
332 /* detect and apply DPI scale */
333 float dpi = n_gui_detect_dpi_scale(gui, display);
334 n_log(LOG_NOTICE, "DPI scale: %.2f", dpi);
335
336 /* load theme file if specified on command line */
337 if (theme_file[0]) {
339 n_log(LOG_NOTICE, "Loaded theme: %s", theme_file);
340 } else {
341 n_log(LOG_ERR, "Failed to load theme: %s", theme_file);
342 }
343 }
344
345 int win_idx = 0;
346
347 /* --- Window 0: Menu Bar (frameless, fixed position) --- */
348 win_ids[win_idx] = n_gui_add_window(gui, "Menu", 0, 0, (float)WIDTH, 28);
349 win_names[win_idx] = "Menu";
350 int win_menu = win_ids[win_idx];
352 win_idx++;
353
354 /* dropdown: Windows (show/hide) */
356 "Windows", 10, 2, 120, 22,
358
359 /* dropdown: File (static entries) */
360 int dm_file = n_gui_add_dropmenu(gui, win_menu,
361 "File", 140, 2, 100, 22, NULL, NULL);
362 n_gui_dropmenu_add_entry(gui, dm_file, "New", 0, NULL, NULL);
363 n_gui_dropmenu_add_entry(gui, dm_file, "Open", 1, NULL, NULL);
364 n_gui_dropmenu_add_entry(gui, dm_file, "Save", 2, NULL, NULL);
365
366 /* --- Window 1: Buttons --- */
367 win_ids[win_idx] = n_gui_add_window(gui, "Buttons", 20, 60, 300, 280);
368 win_names[win_idx] = "Buttons";
369 int win1 = win_ids[win_idx];
370 win_idx++;
371
372 n_gui_add_button(gui, win1, "Click Me!", 20, 20, 120, 36, N_GUI_SHAPE_RECT, on_button_click, NULL);
373 n_gui_add_button(gui, win1, "Rounded", 160, 20, 120, 36, N_GUI_SHAPE_ROUNDED, on_button_click, NULL);
374 n_gui_add_button(gui, win1, "Quit", 20, 80, 260, 40, N_GUI_SHAPE_ROUNDED, on_quit_click, &DONE);
375
376 /* custom themed button */
377 int btn_custom = n_gui_add_button(gui, win1, "Custom Theme", 20, 140, 260, 36, N_GUI_SHAPE_ROUNDED, on_button_click, NULL);
378 N_GUI_THEME red_theme = n_gui_make_theme(
379 al_map_rgba(120, 30, 30, 230), al_map_rgba(160, 40, 40, 240), al_map_rgba(200, 60, 60, 255),
380 al_map_rgba(200, 80, 80, 255), al_map_rgba(230, 100, 100, 255), al_map_rgba(255, 120, 120, 255),
381 al_map_rgba(255, 220, 220, 255), al_map_rgba(255, 255, 255, 255), al_map_rgba(255, 255, 255, 255),
382 2.0f, 8.0f, 8.0f);
383 n_gui_set_widget_theme(gui, btn_custom, red_theme);
384
385 {
386 int btn_dis = n_gui_add_button(gui, win1, "Disabled Button", 20, 196, 260, 30, N_GUI_SHAPE_RECT, on_button_click, NULL);
387 n_gui_set_widget_enabled(gui, btn_dis, 0);
388 }
389
390 /* --- Window 2: Sliders (resizable) --- */
391 win_ids[win_idx] = n_gui_add_window(gui, "Sliders (resizable)", 340, 60, 320, 200);
392 win_names[win_idx] = "Sliders (resizable)";
393 int win2 = win_ids[win_idx];
395 win_idx++;
396
397 n_gui_add_slider(gui, win2, 20, 20, 180, 24, 0.0, 100.0, 50.0, N_GUI_SLIDER_VALUE, on_slider_change, NULL);
398 n_gui_add_slider(gui, win2, 20, 60, 180, 24, 0.0, 100.0, 25.0, N_GUI_SLIDER_PERCENT, on_slider_change, NULL);
399 {
400 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);
401 n_gui_set_widget_enabled(gui, sld_dis, 0);
402 }
403
404 /* vertical sliders */
405 n_gui_add_vslider(gui, win2, 240, 10, 24, 120, 0.0, 100.0, 75.0, N_GUI_SLIDER_VALUE, on_slider_change, NULL);
406 n_gui_add_vslider(gui, win2, 275, 10, 24, 120, 0.0, 100.0, 40.0, N_GUI_SLIDER_PERCENT, on_slider_change, NULL);
407
408 /* --- Window 3: Text Areas --- */
409 win_ids[win_idx] = n_gui_add_window(gui, "Text Areas", 20, 360, 350, 280);
410 win_names[win_idx] = "Text Areas";
411 int win3 = win_ids[win_idx];
412 win_idx++;
413
414 n_gui_add_textarea(gui, win3, 20, 20, 310, 28, 0, 64, on_text_change, NULL);
415 int ta_multi = n_gui_add_textarea(gui, win3, 20, 60, 310, 160, 1, 512, on_text_change, NULL);
416 n_gui_textarea_set_text(gui, ta_multi, "Hello! This is a\nmultiline text area.\nType here...");
417
418 /* --- Window 4: Checkboxes (auto-sized) --- */
419 win_ids[win_idx] = n_gui_add_window_auto(gui, "Checkboxes (auto)", 680, 60);
420 win_names[win_idx] = "Checkboxes (auto)";
421 int win4 = win_ids[win_idx];
422 win_idx++;
423
424 n_gui_add_checkbox(gui, win4, "Enable sound", 20, 20, 260, 28, 1, on_checkbox_toggle, NULL);
425 n_gui_add_checkbox(gui, win4, "Fullscreen", 20, 56, 260, 28, 0, on_checkbox_toggle, NULL);
426 {
427 int cb_fps = n_gui_add_checkbox(gui, win4, "Show FPS (disabled)", 20, 92, 260, 28, 1, on_checkbox_toggle, NULL);
428 n_gui_set_widget_enabled(gui, cb_fps, 0);
429 }
430 {
431 int cb_vsync = n_gui_add_checkbox(gui, win4, "VSync (hidden)", 20, 128, 260, 28, 0, on_checkbox_toggle, NULL);
432 n_gui_set_widget_visible(gui, cb_vsync, 0);
433 }
434 /* auto-size to fit the widgets */
436
437 /* --- Window 5: Scrollbars --- */
438 win_ids[win_idx] = n_gui_add_window(gui, "Scrollbars", 390, 360, 320, 280);
439 win_names[win_idx] = "Scrollbars";
440 int win5 = win_ids[win_idx];
441 win_idx++;
442
444 1000.0, 200.0, on_scroll, NULL);
446 500.0, 100.0, on_scroll, NULL);
448 2000.0, 500.0, on_scroll, NULL);
450 100.0, 100.0, on_scroll, NULL);
451
452 /* --- Window 6: Listbox (single select) --- */
453 win_ids[win_idx] = n_gui_add_window(gui, "Listbox (Single)", 730, 360, 260, 240);
454 win_names[win_idx] = "Listbox (Single)";
455 int win6 = win_ids[win_idx];
456 win_idx++;
457
458 int lb_single = n_gui_add_listbox(gui, win6, 10, 10, 240, 180, N_GUI_SELECT_SINGLE, on_listbox_select, NULL);
459 n_gui_listbox_add_item(gui, lb_single, "Apple");
460 n_gui_listbox_add_item(gui, lb_single, "Banana");
461 n_gui_listbox_add_item(gui, lb_single, "Cherry");
462 n_gui_listbox_add_item(gui, lb_single, "Date");
463 n_gui_listbox_add_item(gui, lb_single, "Elderberry");
464 n_gui_listbox_add_item(gui, lb_single, "Fig");
465 n_gui_listbox_add_item(gui, lb_single, "Grape");
466 n_gui_listbox_add_item(gui, lb_single, "Honeydew");
467 n_gui_listbox_add_item(gui, lb_single, "Kiwi");
468 n_gui_listbox_add_item(gui, lb_single, "Lemon");
469 n_gui_listbox_add_item(gui, lb_single, "Mango");
470 n_gui_listbox_add_item(gui, lb_single, "Nectarine");
471 n_gui_listbox_add_item(gui, lb_single, "Orange");
472 n_gui_listbox_add_item(gui, lb_single, "Papaya");
473 n_gui_listbox_add_item(gui, lb_single, "Quince");
474 n_gui_listbox_set_selected(gui, lb_single, 2, 1);
475
476 /* --- Window 7: Listbox (multi select) --- */
477 win_ids[win_idx] = n_gui_add_window(gui, "Listbox (Multi)", 1000, 360, 260, 240);
478 win_names[win_idx] = "Listbox (Multi)";
479 int win7 = win_ids[win_idx];
480 win_idx++;
481
482 int lb_multi = n_gui_add_listbox(gui, win7, 10, 10, 240, 180, N_GUI_SELECT_MULTIPLE, on_listbox_select, NULL);
483 n_gui_listbox_add_item(gui, lb_multi, "Red");
484 n_gui_listbox_add_item(gui, lb_multi, "Green");
485 n_gui_listbox_add_item(gui, lb_multi, "Blue");
486 n_gui_listbox_add_item(gui, lb_multi, "Yellow");
487 n_gui_listbox_add_item(gui, lb_multi, "Cyan");
488 n_gui_listbox_add_item(gui, lb_multi, "Magenta");
489 n_gui_listbox_add_item(gui, lb_multi, "White");
490 n_gui_listbox_add_item(gui, lb_multi, "Black");
491 n_gui_listbox_add_item(gui, lb_multi, "Orange");
492 n_gui_listbox_add_item(gui, lb_multi, "Pink");
493 n_gui_listbox_add_item(gui, lb_multi, "Brown");
494 n_gui_listbox_add_item(gui, lb_multi, "Purple");
495 n_gui_listbox_set_selected(gui, lb_multi, 0, 1);
496 n_gui_listbox_set_selected(gui, lb_multi, 2, 1);
497
498 /* --- Window 8: Radiolist --- */
499 win_ids[win_idx] = n_gui_add_window(gui, "Radio List", 730, 60, 260, 230);
500 win_names[win_idx] = "Radio List";
501 int win8 = win_ids[win_idx];
502 win_idx++;
503
504 int radio1 = n_gui_add_radiolist(gui, win8, 10, 10, 240, 170, on_radiolist_select, NULL);
505 n_gui_radiolist_add_item(gui, radio1, "Option A");
506 n_gui_radiolist_add_item(gui, radio1, "Option B");
507 n_gui_radiolist_add_item(gui, radio1, "Option C");
508 n_gui_radiolist_add_item(gui, radio1, "Option D");
509 n_gui_radiolist_add_item(gui, radio1, "Option E");
510 n_gui_radiolist_add_item(gui, radio1, "Option F");
511 n_gui_radiolist_add_item(gui, radio1, "Option G");
512 n_gui_radiolist_add_item(gui, radio1, "Option H");
513 n_gui_radiolist_add_item(gui, radio1, "Option I");
514 n_gui_radiolist_add_item(gui, radio1, "Option J");
516
517 /* --- Window 9: Combobox + Labels + Image --- */
518 win_ids[win_idx] = n_gui_add_window(gui, "Combo / Labels / Image", 1000, 60, 260, 280);
519 win_names[win_idx] = "Combo / Labels / Image";
520 int win9 = win_ids[win_idx];
521 win_idx++;
522
523 /* combobox */
524 int combo1 = n_gui_add_combobox(gui, win9, 10, 10, 240, 24, on_combobox_select, NULL);
525 n_gui_combobox_add_item(gui, combo1, "Tiny");
526 n_gui_combobox_add_item(gui, combo1, "Small");
527 n_gui_combobox_add_item(gui, combo1, "Medium");
528 n_gui_combobox_add_item(gui, combo1, "Large");
529 n_gui_combobox_add_item(gui, combo1, "Extra Large");
530 n_gui_combobox_add_item(gui, combo1, "XXL");
531 n_gui_combobox_add_item(gui, combo1, "XXXL");
532 n_gui_combobox_add_item(gui, combo1, "Huge");
533 n_gui_combobox_add_item(gui, combo1, "Gigantic");
534 n_gui_combobox_add_item(gui, combo1, "Colossal");
536
537 /* static labels */
538 n_gui_add_label(gui, win9, "Left aligned label", 10, 50, 240, 20, N_GUI_ALIGN_LEFT);
539 n_gui_add_label(gui, win9, "Centered label", 10, 74, 240, 20, N_GUI_ALIGN_CENTER);
540 n_gui_add_label(gui, win9, "Right aligned label", 10, 98, 240, 20, N_GUI_ALIGN_RIGHT);
541 n_gui_add_label(gui, win9, "Justified: this text spreads across width evenly", 10, 122, 240, 20, N_GUI_ALIGN_JUSTIFIED);
542
543 /* hyperlink label */
545 "Click this link!", "https://example.com",
546 10, 150, 240, 20, N_GUI_ALIGN_LEFT,
547 on_link_click, NULL);
548
549 /* image widgets with different scale modes */
550 ALLEGRO_BITMAP* img_192 = al_load_bitmap("DATAS/img/android-chrome-192x192.png");
551 ALLEGRO_BITMAP* img_32 = al_load_bitmap("DATAS/img/favicon-32x32.png");
552 n_gui_add_label(gui, win9, "Image FIT:", 10, 180, 80, 16, N_GUI_ALIGN_LEFT);
553 n_gui_add_image(gui, win9, 10, 198, 80, 80, img_192, N_GUI_IMAGE_FIT);
554 n_gui_add_label(gui, win9, "STRETCH:", 100, 180, 70, 16, N_GUI_ALIGN_LEFT);
555 n_gui_add_image(gui, win9, 100, 198, 70, 80, img_192, N_GUI_IMAGE_STRETCH);
556 n_gui_add_label(gui, win9, "CENTER:", 180, 180, 70, 16, N_GUI_ALIGN_LEFT);
557 n_gui_add_image(gui, win9, 180, 198, 70, 80, img_32, N_GUI_IMAGE_CENTER);
558
559 /* --- Window 10: Mode Toggles (stays clicked/unclicked) --- */
560 win_ids[win_idx] = n_gui_add_window(gui, "Mode Toggles", 20, 660, 300, 230);
561 win_names[win_idx] = "Mode Toggles";
562 int win10 = win_ids[win_idx];
563 win_idx++;
564
565 n_gui_add_label(gui, win10, "Click or press key to toggle on/off:", 20, 10, 260, 20, N_GUI_ALIGN_LEFT);
566
567 {
568 int btn_id;
569 btn_id = n_gui_add_toggle_button(gui, win10, "Player Mode [Ctrl+P]",
570 20, 40, 260, 32, N_GUI_SHAPE_ROUNDED, 0,
572 n_gui_button_set_keycode(gui, btn_id, ALLEGRO_KEY_P, ALLEGRO_KEYMOD_CTRL);
573
574 btn_id = n_gui_add_toggle_button(gui, win10, "Height Mode [Ctrl+H]",
575 20, 80, 260, 32, N_GUI_SHAPE_ROUNDED, 0,
577 n_gui_button_set_keycode(gui, btn_id, ALLEGRO_KEY_H, ALLEGRO_KEYMOD_CTRL);
578
579 btn_id = n_gui_add_toggle_button(gui, win10, "Grid [Shift+G]",
580 20, 120, 260, 32, N_GUI_SHAPE_ROUNDED, 0,
582 n_gui_button_set_keycode(gui, btn_id, ALLEGRO_KEY_G, ALLEGRO_KEYMOD_SHIFT);
583
584 btn_id = n_gui_add_toggle_button(gui, win10, "Smooth Height [Shift+V]",
585 20, 160, 260, 32, N_GUI_SHAPE_ROUNDED, 0,
587 n_gui_button_set_keycode(gui, btn_id, ALLEGRO_KEY_V, ALLEGRO_KEYMOD_SHIFT);
588 }
589
590 /* --- Window 11: Projection Selection (toggle radio group) --- */
591 win_ids[win_idx] = n_gui_add_window(gui, "Projection", 340, 660, 280, 230);
592 win_names[win_idx] = "Projection";
593 int win11 = win_ids[win_idx];
594 win_idx++;
595
596 n_gui_add_label(gui, win11, "Select projection:", 20, 10, 240, 20, N_GUI_ALIGN_LEFT);
597
598 proj_btn_ids[0] = n_gui_add_toggle_button(gui, win11, "Classic 2:1 [Ctrl+F1]",
599 20, 40, 240, 30, N_GUI_SHAPE_ROUNDED, 1,
601 n_gui_button_set_keycode(gui, proj_btn_ids[0], ALLEGRO_KEY_F1, ALLEGRO_KEYMOD_CTRL);
602 proj_btn_ids[1] = n_gui_add_toggle_button(gui, win11, "True Isometric [Shift+F2]",
603 20, 78, 240, 30, N_GUI_SHAPE_ROUNDED, 0,
605 n_gui_button_set_keycode(gui, proj_btn_ids[1], ALLEGRO_KEY_F2, ALLEGRO_KEYMOD_SHIFT);
606 proj_btn_ids[2] = n_gui_add_toggle_button(gui, win11, "Military [Ctrl+F3]",
607 20, 116, 240, 30, N_GUI_SHAPE_ROUNDED, 0,
609 n_gui_button_set_keycode(gui, proj_btn_ids[2], ALLEGRO_KEY_F3, ALLEGRO_KEYMOD_CTRL);
610 proj_btn_ids[3] = n_gui_add_toggle_button(gui, win11, "Staggered [Shift+F4]",
611 20, 154, 240, 30, N_GUI_SHAPE_ROUNDED, 0,
613 n_gui_button_set_keycode(gui, proj_btn_ids[3], ALLEGRO_KEY_F4, ALLEGRO_KEYMOD_SHIFT);
614
615 /* custom theme: green for active/toggled state */
616 N_GUI_THEME green_theme = n_gui_make_theme(
617 al_map_rgba(40, 60, 40, 230), al_map_rgba(50, 80, 50, 240), al_map_rgba(30, 120, 30, 255),
618 al_map_rgba(60, 100, 60, 255), al_map_rgba(70, 140, 70, 255), al_map_rgba(40, 160, 40, 255),
619 al_map_rgba(200, 255, 200, 255), al_map_rgba(255, 255, 255, 255), al_map_rgba(255, 255, 255, 255),
620 2.0f, 8.0f, 8.0f);
621 for (int i = 0; i < 4; i++) {
622 n_gui_set_widget_theme(gui, proj_btn_ids[i], green_theme);
623 }
624
625 /* --- Window 12: Status Display --- */
626 win_ids[win_idx] = n_gui_add_window(gui, "Toggle Status", 640, 660, 620, 230);
627 win_names[win_idx] = "Toggle Status";
628 int win12 = win_ids[win_idx];
629 win_idx++;
630
631 lbl_status = n_gui_add_label(gui, win12, "Waiting...", 20, 10, 580, 20, N_GUI_ALIGN_LEFT);
632 lbl_proj = n_gui_add_label(gui, win12, "Projection: Classic 2:1", 20, 40, 580, 20, N_GUI_ALIGN_LEFT);
633
634 n_gui_add_label(gui, win12, "Toggle buttons stay visually pressed when ON.", 20, 80, 580, 20, N_GUI_ALIGN_LEFT);
635 n_gui_add_label(gui, win12, "Click again to turn OFF. Projection uses radio-group pattern.", 20, 100, 580, 20, N_GUI_ALIGN_LEFT);
636 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);
637
638 /* --- Window 13: Scrollable Content (auto scrollbar demo) --- */
639 win_ids[win_idx] = n_gui_add_window(gui, "Scrollable (auto scrollbar)", 390, 270, 280, 80);
640 win_names[win_idx] = "Scrollable (auto scrollbar)";
641 int win_scroll = win_ids[win_idx];
643 win_idx++;
644
645 /* add many labels to overflow the window height */
646 for (int i = 0; i < 20; i++) {
647 char buf[128];
648 snprintf(buf, sizeof(buf), "Scrollable line %d - content that extends beyond window", i + 1);
649 n_gui_add_label(gui, win_scroll, buf, 10, (float)(i * 22), 400, 20, N_GUI_ALIGN_LEFT);
650 }
651
652 /* --- Window 14: Bitmap Skinning Demo ---
653 * Demonstrates optional bitmap overlays on widgets and windows.
654 * Placeholder bitmaps are created programmatically as colored rectangles
655 * with gradients to show where the skinning takes effect.
656 * In a real application, use al_load_bitmap() with actual image files.
657 * Bitmaps are NOT owned by N_GUI — caller must keep them alive while
658 * the GUI uses them and destroy them AFTER n_gui_destroy_ctx(). */
659 ALLEGRO_BITMAP* skin_win_bg = NULL;
660 ALLEGRO_BITMAP* skin_win_tb = NULL;
661 ALLEGRO_BITMAP* skin_sld_track = NULL;
662 ALLEGRO_BITMAP* skin_sld_fill = NULL;
663 ALLEGRO_BITMAP* skin_sld_handle = NULL;
664 ALLEGRO_BITMAP* skin_sld_handle_h = NULL;
665 ALLEGRO_BITMAP* skin_chk_unchecked = NULL;
666 ALLEGRO_BITMAP* skin_chk_checked = NULL;
667 {
668 /* create placeholder skin bitmaps programmatically */
669 ALLEGRO_BITMAP* prev_target = al_get_target_bitmap();
670
671 /* window background: blue gradient */
672 skin_win_bg = al_create_bitmap(64, 64);
673 al_set_target_bitmap(skin_win_bg);
674 for (int row = 0; row < 64; row++) {
675 float t = (float)row / 63.0f;
676 al_draw_line(0, (float)row, 64, (float)row,
677 al_map_rgba((unsigned char)(20 + 40 * t), (unsigned char)(40 + 60 * t), (unsigned char)(80 + 80 * t), 230), 1.0f);
678 }
679
680 /* titlebar: dark gradient */
681 skin_win_tb = al_create_bitmap(64, 16);
682 al_set_target_bitmap(skin_win_tb);
683 for (int row = 0; row < 16; row++) {
684 float t = (float)row / 15.0f;
685 al_draw_line(0, (float)row, 64, (float)row,
686 al_map_rgba((unsigned char)(60 + 40 * t), (unsigned char)(30 + 30 * t), (unsigned char)(100 + 50 * t), 240), 1.0f);
687 }
688
689 /* slider track: dark rounded bar */
690 skin_sld_track = al_create_bitmap(32, 8);
691 al_set_target_bitmap(skin_sld_track);
692 al_clear_to_color(al_map_rgba(50, 50, 60, 200));
693
694 /* slider fill: bright bar */
695 skin_sld_fill = al_create_bitmap(32, 8);
696 al_set_target_bitmap(skin_sld_fill);
697 al_clear_to_color(al_map_rgba(100, 180, 255, 230));
698
699 /* slider handle: circle-ish */
700 skin_sld_handle = al_create_bitmap(16, 16);
701 al_set_target_bitmap(skin_sld_handle);
702 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
703 al_draw_filled_circle(8, 8, 7, al_map_rgba(200, 200, 220, 240));
704
705 /* slider handle hover */
706 skin_sld_handle_h = al_create_bitmap(16, 16);
707 al_set_target_bitmap(skin_sld_handle_h);
708 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
709 al_draw_filled_circle(8, 8, 7, al_map_rgba(150, 220, 255, 255));
710
711 /* checkbox bitmaps */
712 skin_chk_unchecked = al_create_bitmap(16, 16);
713 al_set_target_bitmap(skin_chk_unchecked);
714 al_clear_to_color(al_map_rgba(60, 60, 80, 220));
715 al_draw_rectangle(0.5f, 0.5f, 15.5f, 15.5f, al_map_rgba(150, 150, 200, 255), 1.0f);
716
717 skin_chk_checked = al_create_bitmap(16, 16);
718 al_set_target_bitmap(skin_chk_checked);
719 al_clear_to_color(al_map_rgba(80, 140, 255, 240));
720 al_draw_rectangle(0.5f, 0.5f, 15.5f, 15.5f, al_map_rgba(150, 150, 200, 255), 1.0f);
721 al_draw_line(3, 8, 7, 13, al_map_rgb(255, 255, 255), 2.0f);
722 al_draw_line(7, 13, 13, 3, al_map_rgb(255, 255, 255), 2.0f);
723
724 al_set_target_bitmap(prev_target);
725
726 /* create the skinned window */
727 win_ids[win_idx] = n_gui_add_window(gui, "Bitmap Skinning", 690, 60, 300, 240);
728 win_names[win_idx] = "Bitmap Skinning";
729 int win_skin = win_ids[win_idx];
730 win_idx++;
731
732 /* apply window bitmaps */
733 n_gui_window_set_bitmaps(gui, win_skin, skin_win_bg, skin_win_tb, N_GUI_IMAGE_STRETCH);
734
735 /* skinned slider */
736 int skin_slider = n_gui_add_slider(gui, win_skin, 20, 20, 200, 24, 0.0, 100.0, 60.0,
738 n_gui_slider_set_bitmaps(gui, skin_slider, skin_sld_track, skin_sld_fill, skin_sld_handle, skin_sld_handle_h, NULL);
739
740 /* skinned checkbox */
741 int skin_chk = n_gui_add_checkbox(gui, win_skin, "Skinned checkbox", 20, 60, 200, 24, 0,
742 on_checkbox_toggle, NULL);
743 n_gui_checkbox_set_bitmaps(gui, skin_chk, skin_chk_unchecked, skin_chk_checked, NULL);
744
745 /* label with background (reuses sld_track bitmap) */
746 int skin_label = n_gui_add_label(gui, win_skin, "Label with bg bitmap", 20, 100, 200, 24, N_GUI_ALIGN_CENTER);
747 n_gui_label_set_bitmap(gui, skin_label, skin_sld_track);
748
749 /* regular button for comparison */
750 n_gui_add_button(gui, win_skin, "Normal Button", 20, 140, 200, 30, N_GUI_SHAPE_ROUNDED, on_button_click, NULL);
751
752 n_gui_add_label(gui, win_skin, "Window bg, titlebar, slider, checkbox,", 20, 180, 260, 16, N_GUI_ALIGN_LEFT);
753 n_gui_add_label(gui, win_skin, "and label are all bitmap-skinned above.", 20, 198, 260, 16, N_GUI_ALIGN_LEFT);
754 }
755
756 /* --- Window 15: Login Dialog (autofit + centered + set_focus demo) --- */
757 win_ids[win_idx] = n_gui_add_window(gui, "Login", 640, 450, 1, 1);
758 win_names[win_idx] = "Login Dialog";
759 {
760 int win_login = win_ids[win_idx];
761 win_idx++;
762
763 /* configure auto-fit: adjust both W and H, centered on insertion point, 10px border */
765
766 /* add widgets */
767 n_gui_add_label(gui, win_login, "Username:", 10, 10, 80, 25, N_GUI_ALIGN_LEFT);
768 int txt_user = n_gui_add_textarea(gui, win_login, 100, 10, 200, 25, 0, 64, on_text_change, NULL);
769 n_gui_add_label(gui, win_login, "Password:", 10, 45, 80, 25, N_GUI_ALIGN_LEFT);
770 n_gui_add_textarea(gui, win_login, 100, 45, 200, 25, 0, 64, on_text_change, NULL);
771 n_gui_add_button(gui, win_login, "Login", 100, 85, 100, 30, N_GUI_SHAPE_ROUNDED, on_login_click, NULL);
772
773 /* trigger autofit calculation */
775
776 /* set initial focus to username field */
777 n_gui_set_focus(gui, txt_user);
778 }
779
780 /* ---- enable adaptive resize mode ---- */
781 /* Switch from virtual canvas scaling to adaptive per-window resize.
782 * Each window gets its own policy: SCALE (repositions + resizes),
783 * MOVE (repositions only), or NONE (stays fixed). */
785
800
801 /* ---- main loop ---- */
802 al_start_timer(fps_timer);
803 int do_draw = 0;
804 int mx = 0, my = 0;
805
806 al_clear_keyboard_state(NULL);
807 al_flush_event_queue(event_queue);
808
809 do {
810 ALLEGRO_EVENT ev;
811 al_wait_for_event(event_queue, &ev);
812
813 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
814 DONE = 1;
815 }
816 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
817 DONE = 1;
818 }
819
820 /* handle display resize: in adaptive mode, windows reposition/resize
821 * according to their individual policies (NONE/MOVE/SCALE) */
822 if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
823 al_acknowledge_resize(display);
824 int new_w = al_get_display_width(display);
825 int new_h = al_get_display_height(display);
826 n_gui_set_display_size(gui, (float)new_w, (float)new_h);
827 }
828
829 /* pass events to the GUI, check if the event was consumed.
830 * Use gui_handled to prevent mouse events from reaching game logic
831 * when the cursor is over a GUI window or widget.
832 * Example: if (!gui_handled && ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { ... } */
833 int gui_handled = n_gui_process_event(gui, ev);
834 (void)gui_handled;
835
836 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
837 mx = ev.mouse.x;
838 my = ev.mouse.y;
839 }
840
841 if (ev.type == ALLEGRO_EVENT_TIMER) {
842 /* update toggle status labels */
843 char status_buf[256];
844 snprintf(status_buf, sizeof(status_buf),
845 "Player:%s Height:%s Grid:%s Smooth:%s Ghost:%s",
846 player_mode ? "ON" : "off",
847 height_mode ? "ON" : "off",
848 show_grid ? "ON" : "off",
849 smooth_height ? "ON" : "off",
850 ghost_enabled ? "ON" : "off");
851 n_gui_label_set_text(gui, lbl_status, status_buf);
852
853 char proj_buf[128];
854 snprintf(proj_buf, sizeof(proj_buf), "Projection: %s", proj_names[current_proj]);
856
857 do_draw = 1;
858 }
859
860 if (do_draw && al_is_event_queue_empty(event_queue)) {
861 al_set_target_bitmap(al_get_backbuffer(display));
862 al_clear_to_color(al_map_rgba(30, 30, 35, 255));
863
864 /* draw all GUI windows and widgets */
866
867 /* mouse cursor crosshair (transform screen coords to virtual) */
868 {
869 float vmx, vmy;
870 n_gui_screen_to_virtual(gui, (float)mx, (float)my, &vmx, &vmy);
871 /* apply the virtual canvas transform for drawing */
872 ALLEGRO_TRANSFORM overlay_tf;
873 al_identity_transform(&overlay_tf);
874 if (gui->virtual_w > 0 && gui->gui_scale > 0) {
875 al_scale_transform(&overlay_tf, gui->gui_scale, gui->gui_scale);
876 al_translate_transform(&overlay_tf, gui->gui_offset_x, gui->gui_offset_y);
877 }
878 al_use_transform(&overlay_tf);
879 /* ensure crosshair is at least 1 physical pixel thick */
880 float cross_thick = 1.0f;
881 if (gui->gui_scale > 0.01f && gui->gui_scale < 1.0f) {
882 cross_thick = 1.0f / gui->gui_scale;
883 }
884 al_draw_line(vmx - 6, vmy, vmx + 6, vmy, al_map_rgb(255, 100, 100), cross_thick);
885 al_draw_line(vmx, vmy - 6, vmx, vmy + 6, al_map_rgb(255, 100, 100), cross_thick);
886 }
887
888 /* instructions (drawn in virtual space) */
889 {
890 /* explicitly set virtual canvas transform so this block is
891 * self-contained and does not depend on previous drawing state */
892 ALLEGRO_TRANSFORM instr_tf;
893 al_identity_transform(&instr_tf);
894 if (gui->virtual_w > 0 && gui->virtual_h > 0 && gui->gui_scale > 0) {
895 al_scale_transform(&instr_tf, gui->gui_scale, gui->gui_scale);
896 al_translate_transform(&instr_tf, gui->gui_offset_x, gui->gui_offset_y);
897 }
898 al_use_transform(&instr_tf);
899 float virt_h = (gui->virtual_h > 0) ? gui->virtual_h : (float)al_get_display_height(display);
900 al_draw_text(font, al_map_rgb(180, 180, 180), 10, virt_h - 18, 0,
901 "Drag windows | Click widgets | Scroll lists | Resize corners/display | 'Windows' dropdown | ESC to quit");
902 /* restore identity transform */
903 ALLEGRO_TRANSFORM identity;
904 al_identity_transform(&identity);
905 al_use_transform(&identity);
906 }
907
908 al_flip_display();
909 do_draw = 0;
910 }
911 } while (!DONE);
912
913 /* cleanup — destroy GUI context first, then bitmaps (N_GUI does not own them) */
915 if (img_192) al_destroy_bitmap(img_192);
916 if (img_32) al_destroy_bitmap(img_32);
917 /* skin bitmaps: destroy after GUI context since N_GUI only stores pointers */
918 if (skin_win_bg) al_destroy_bitmap(skin_win_bg);
919 if (skin_win_tb) al_destroy_bitmap(skin_win_tb);
920 if (skin_sld_track) al_destroy_bitmap(skin_sld_track);
921 if (skin_sld_fill) al_destroy_bitmap(skin_sld_fill);
922 if (skin_sld_handle) al_destroy_bitmap(skin_sld_handle);
923 if (skin_sld_handle_h) al_destroy_bitmap(skin_sld_handle_h);
924 if (skin_chk_unchecked) al_destroy_bitmap(skin_chk_unchecked);
925 if (skin_chk_checked) al_destroy_bitmap(skin_chk_checked);
926 al_destroy_font(font);
927 al_destroy_timer(fps_timer);
928 al_destroy_event_queue(event_queue);
929 al_destroy_display(display);
930 al_uninstall_system();
931
932 return 0;
933}
int main(void)
ALLEGRO_TIMER * fps_timer
Definition ex_fluid.c:66
int getoptret
Definition ex_fluid.c:60
int DONE
Definition ex_fluid.c:59
int log_level
Definition ex_fluid.c:61
ALLEGRO_DISPLAY * display
Definition ex_fluid.c:53
void on_listbox_select(int widget_id, int index, int selected, void *user_data)
Definition ex_gui.c:122
static int proj_btn_ids[4]
Definition ex_gui.c:68
#define NUM_WINDOWS
Definition ex_gui.c:71
void on_link_click(int widget_id, const char *link, void *user_data)
Definition ex_gui.c:140
void on_grid_toggle(int widget_id, void *user_data)
Definition ex_gui.c:159
void on_player_toggle(int widget_id, void *user_data)
Definition ex_gui.c:147
void on_login_click(int widget_id, void *user_data)
Definition ex_gui.c:116
#define WIDTH
Definition ex_gui.c:36
void on_window_toggle_click(int widget_id, int entry_index, int tag, void *user_data)
Definition ex_gui.c:194
static int smooth_height
Definition ex_gui.c:57
static int win_ids[16]
Definition ex_gui.c:72
void on_checkbox_toggle(int widget_id, int checked, void *user_data)
Definition ex_gui.c:91
void on_slider_change(int widget_id, double value, void *user_data)
Definition ex_gui.c:85
void on_button_click(int widget_id, void *user_data)
Definition ex_gui.c:79
static char theme_file[512]
Definition ex_gui.c:51
void on_windows_menu_open(int widget_id, void *user_data)
Definition ex_gui.c:203
void on_smooth_toggle(int widget_id, void *user_data)
Definition ex_gui.c:165
static int show_grid
Definition ex_gui.c:56
static const char * win_names[16]
Definition ex_gui.c:73
static int lbl_status
Definition ex_gui.c:64
void on_radiolist_select(int widget_id, int index, void *user_data)
Definition ex_gui.c:128
static int dropmenu_windows_id
Definition ex_gui.c:76
void on_combobox_select(int widget_id, int index, void *user_data)
Definition ex_gui.c:134
void on_ghost_toggle(int widget_id, void *user_data)
Definition ex_gui.c:171
static const char * proj_names[]
Definition ex_gui.c:61
void on_proj_select(int widget_id, void *user_data)
Definition ex_gui.c:178
static int player_mode
Definition ex_gui.c:54
void on_height_toggle(int widget_id, void *user_data)
Definition ex_gui.c:153
static int ghost_enabled
Definition ex_gui.c:58
static int lbl_proj
Definition ex_gui.c:65
void on_text_change(int widget_id, const char *text, void *user_data)
Definition ex_gui.c:97
void on_quit_click(int widget_id, void *user_data)
Definition ex_gui.c:109
static int height_mode
Definition ex_gui.c:55
void on_scroll(int widget_id, double pos, void *user_data)
Definition ex_gui.c:103
static int current_proj
Definition ex_gui.c:59
#define HEIGHT
Definition ex_gui.c:37
static N_GUI_CTX * gui
bool done
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:52
float gui_offset_x
horizontal letterbox offset for virtual canvas
Definition n_gui.h:932
float virtual_h
virtual canvas height (0 = disabled / identity transform)
Definition n_gui.h:928
float gui_offset_y
vertical letterbox offset for virtual canvas
Definition n_gui.h:934
float virtual_w
virtual canvas width (0 = disabled / identity transform)
Definition n_gui.h:926
float gui_scale
computed uniform scale factor for virtual canvas
Definition n_gui.h:930
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
Set the display (viewport) size for global scrollbar computation.
Definition n_gui.c:5176
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:7536
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:1086
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:5234
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:825
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:156
#define N_GUI_IMAGE_FIT
scale to fit within bounds, keep aspect ratio
Definition n_gui.h:148
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:1160
void n_gui_window_apply_autofit(N_GUI_CTX *ctx, int window_id)
Trigger auto-fit recalculation for a window.
Definition n_gui.c:910
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:1752
#define N_GUI_SLIDER_PERCENT
slider uses 0-100 percentage
Definition n_gui.h:124
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:1200
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:5192
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:2397
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:1575
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:2028
void n_gui_label_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text of a label widget
Definition n_gui.c:2178
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:1336
#define N_GUI_WIN_RESIZE_SCALE
reposition AND resize proportionally, child widgets scale too
Definition n_gui.h:212
#define N_GUI_SLIDER_VALUE
slider uses raw start/end values
Definition n_gui.h:122
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:5767
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:1497
void n_gui_set_widget_enabled(N_GUI_CTX *ctx, int widget_id, int enabled)
Enable or disable a widget.
Definition n_gui.c:1592
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:1371
#define N_GUI_WIN_FIXED_POSITION
disable window dragging (default:enable)
Definition n_gui.h:196
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:5081
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:1990
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:817
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:1860
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:4974
#define N_GUI_WIN_RESIZE_NONE
no adaptation: absolute position and size unchanged
Definition n_gui.h:208
#define N_GUI_AUTOFIT_WH
auto-adjust both width and height (convenience: W|H)
Definition n_gui.h:220
#define N_GUI_SELECT_SINGLE
single item selection
Definition n_gui.h:142
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:2109
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:862
void n_gui_set_focus(N_GUI_CTX *ctx, int widget_id)
Set keyboard focus to a specific widget.
Definition n_gui.c:1613
#define N_GUI_SELECT_MULTIPLE
multiple item selection
Definition n_gui.h:144
#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:198
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:1406
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:1097
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:1070
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:1538
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:1468
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:2553
void n_gui_draw(N_GUI_CTX *ctx)
Draw all visible windows and their widgets.
Definition n_gui.c:4800
#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:226
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:2412
#define N_GUI_SCROLLBAR_H
horizontal scrollbar
Definition n_gui.h:134
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:2301
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:1129
int n_gui_window_is_open(N_GUI_CTX *ctx, int window_id)
Check if a window is currently visible.
Definition n_gui.c:834
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:2278
#define N_GUI_IMAGE_STRETCH
stretch to fill bounds
Definition n_gui.h:150
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:581
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:466
#define N_GUI_IMAGE_CENTER
draw at original size, centered
Definition n_gui.h:152
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:5042
#define N_GUI_ALIGN_CENTER
center aligned text
Definition n_gui.h:158
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:1293
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:523
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:2447
#define N_GUI_WIN_RESIZE_MOVE
reposition proportionally, keep pixel size
Definition n_gui.h:210
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
Definition n_gui.h:116
#define N_GUI_ALIGN_RIGHT
right aligned text
Definition n_gui.h:160
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:2079
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:843
#define N_GUI_ALIGN_JUSTIFIED
justified text (spread words to fill width)
Definition n_gui.h:162
void n_gui_set_widget_visible(N_GUI_CTX *ctx, int widget_id, int visible)
Show or hide a widget.
Definition n_gui.c:1583
#define N_GUI_ID_MAX
maximum length for widget id/name strings
Definition n_gui.h:84
#define N_GUI_WIN_AUTO_SCROLLBAR
enable automatic scrollbars when content exceeds window size
Definition n_gui.h:192
#define N_GUI_RESIZE_ADAPTIVE
virtual size tracks display; windows adapt per their resize_policy
Definition n_gui.h:204
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:1214
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:1259
#define N_GUI_SHAPE_RECT
rectangle shape (default)
Definition n_gui.h:114
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:2145
#define N_GUI_SCROLLBAR_V
vertical scrollbar
Definition n_gui.h:136
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:328
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:2237
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:5019
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:1001
#define N_GUI_WIN_RESIZABLE
enable user-resizable window with a drag handle at bottom-right
Definition n_gui.h:194
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:891
void n_gui_dropmenu_clear_dynamic(N_GUI_CTX *ctx, int widget_id)
Remove all dynamic entries (keep static ones)
Definition n_gui.c:2326
The top-level GUI context that holds all windows.
Definition n_gui.h:882
Color theme for a widget.
Definition n_gui.h:256
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
int set_log_file(char *file)
Set the logging to a file instead of stderr.
Definition n_log.c:167
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_INFO
informational
Definition n_log.h:81
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.