Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_checkbox_release.c
Go to the documentation of this file.
1
15
16#define ALLEGRO_UNSTABLE 1
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22
23#include "nilorea/n_common.h"
24#include "nilorea/n_log.h"
25#include "nilorea/n_gui.h"
26
27static int log_level = LOG_ERR;
28static int failures = 0;
30static int toggles = 0;
31static int last_checked = -1;
32
34static void expect_true(const char* what, int ok) {
35 if (!ok) {
36 n_log(LOG_ERR, "FAIL: %s", what);
37 failures++;
38 } else {
39 n_log(LOG_INFO, "ok: %s", what);
40 }
41}
42
44static int process_args(int argc, char** argv) {
45 int opt = 0;
46
47 while ((opt = getopt(argc, argv, "V:")) != -1) {
48 if (opt == 'V') {
49 log_level = atoi(optarg);
50 } else {
51 return -1;
52 }
53 }
54 return 0;
55}
56
58static void on_toggle(int widget_id, int checked, void* user_data) {
59 (void)widget_id;
60 (void)user_data;
61 toggles++;
62 last_checked = checked;
63}
64
66static void feed(N_GUI_CTX* gui, int type, int x, int y) {
67 ALLEGRO_EVENT ev;
68 memset(&ev, 0, sizeof(ev));
69 ev.type = (ALLEGRO_EVENT_TYPE)type;
70 ev.mouse.x = x;
71 ev.mouse.y = y;
72 ev.mouse.dx = type == ALLEGRO_EVENT_MOUSE_AXES ? 1 : 0;
73 ev.mouse.button = type == ALLEGRO_EVENT_MOUSE_AXES ? 0u : 1u;
74 (void)n_gui_process_event(gui, ev);
75}
76
77int main(int argc, char** argv) {
79 if (process_args(argc, argv) != 0) {
80 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
81 return 2;
82 }
84
85 if (!al_init() || !al_init_font_addon()) {
86 n_log(LOG_ERR, "allegro init failed");
87 return 1;
88 }
89 ALLEGRO_FONT* font = al_create_builtin_font();
90 if (!font) return 1;
92 if (!gui) {
93 al_destroy_font(font);
94 return 1;
95 }
96 n_gui_set_display_size(gui, 400.0f, 300.0f);
97 int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 300.0f);
100 int box = n_gui_add_checkbox(gui, win, "Mute groups", 50.0f, 50.0f, 150.0f, 30.0f, 0, on_toggle, NULL);
101
102 /* a click: press and release on the box */
103 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 60);
104 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 60, 60);
105 expect_true("a press alone does not toggle", toggles == 0 && !n_gui_checkbox_is_checked(gui, box));
106 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 60, 60);
107 expect_true("the release over the box toggles it on", toggles == 1 && last_checked == 1 && n_gui_checkbox_is_checked(gui, box));
108
109 /* a release elsewhere on the box still counts */
110 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 60, 60);
111 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 190, 70);
112 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 190, 70);
113 expect_true("a release anywhere on the box toggles it off", toggles == 2 && last_checked == 0 && !n_gui_checkbox_is_checked(gui, box));
114
115 /* a swipe: press on the box, drag off it, release elsewhere */
116 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 60);
117 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 60, 60);
118 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 150);
119 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 250);
120 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 60, 250);
121 expect_true("a press dragged off the box changes nothing", toggles == 2 && !n_gui_checkbox_is_checked(gui, box));
122
123 /* the reverse: press elsewhere, release on the box */
124 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 300, 250);
125 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 300, 250);
126 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 60);
127 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 60, 60);
128 expect_true("a release on the box after a press elsewhere changes nothing", toggles == 2 && !n_gui_checkbox_is_checked(gui, box));
129
130 /* and a click still works after all that */
131 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 60, 60);
132 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 60, 60);
133 expect_true("a later click toggles it again", toggles == 3 && last_checked == 1 && n_gui_checkbox_is_checked(gui, box));
134
136 al_destroy_font(font);
137 if (failures) {
138 fprintf(stderr, "ex_gui_checkbox_release: %d failure(s)\n", failures);
139 return 1;
140 }
141 printf("ex_gui_checkbox_release: ok\n");
142 return 0;
143}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
static void feed(N_GUI_CTX *gui, int type, int x, int y)
feed one mouse event
static int last_checked
static int toggles
how many times on_toggle ran, and with what
static void on_toggle(int widget_id, int checked, void *user_data)
the checkbox callback
static N_GUI_CTX * gui
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
FONT * font
the default 8x8 font, valid after allegro_init()
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
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
#define N_GUI_WIN_FIXED_POSITION
disable window dragging (default:enable)
Definition n_gui.h:274
#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_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_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1436
int n_gui_checkbox_is_checked(N_GUI_CTX *ctx, int widget_id)
check if a checkbox widget is checked
Definition n_gui.c:4237
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
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
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
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_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.