Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_colorwheel.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
28
29#define ALLEGRO_UNSTABLE 1
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include <allegro5/allegro_primitives.h>
37
38#include "nilorea/n_common.h"
39#include "nilorea/n_log.h"
40#include "nilorea/n_gui.h"
42
44static int log_level = LOG_ERR;
46static int failures = 0;
48static int changes = 0, drag_changes = 0;
50static int colour_asked = 0;
51
55static void expect_true(const char* what, int ok) {
56 if (!ok) {
57 n_log(LOG_ERR, "FAIL: %s", what);
58 failures++;
59 } else {
60 n_log(LOG_INFO, "ok: %s", what);
61 }
62}
63
68static int process_args(int argc, char** argv) {
69 int opt = 0;
70
71 while ((opt = getopt(argc, argv, "V:")) != -1) {
72 if (opt == 'V') {
73 log_level = atoi(optarg);
74 } else {
75 return -1;
76 }
77 }
78 return 0;
79}
80
85static void on_change(int widget_id, int dragging, void* user_data) {
86 (void)widget_id;
87 (void)user_data;
88 changes++;
89 if (dragging) drag_changes++;
90}
91
97static ALLEGRO_COLOR grey(float u, float v, void* user_data) {
98 (void)u;
99 (void)v;
100 (void)user_data;
101 colour_asked = 1;
102 return al_map_rgb_f(0.5f, 0.5f, 0.5f);
103}
104
111static void feed(N_GUI_CTX* gui, int type, int x, int y, int button) {
112 ALLEGRO_EVENT ev;
113
114 memset(&ev, 0, sizeof(ev));
115 ev.type = (ALLEGRO_EVENT_TYPE)type;
116 ev.mouse.x = x;
117 ev.mouse.y = y;
118 ev.mouse.dx = type == ALLEGRO_EVENT_MOUSE_AXES ? 1 : 0;
119 ev.mouse.button = (unsigned int)button;
121}
122
125#define AT_X(u) ((int)(150.0f + (u) * 98.0f + 0.5f))
127#define AT_Y(v) ((int)(150.0f - (v) * 98.0f + 0.5f))
128
135static void drag(N_GUI_CTX* gui, float fu, float fv, float tu, float tv) {
136 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(fu), AT_Y(fv), 0);
137 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(fu), AT_Y(fv), 1);
138 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(tu), AT_Y(tv), 0);
139 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(tu), AT_Y(tv), 1);
140}
141
146static int near(float a, float b) {
147 return a - b < 0.02f && b - a < 0.02f;
148}
149
154int main(int argc, char** argv) {
155 float u = 0.0f, v = 0.0f;
156
158 if (process_args(argc, argv) != 0) {
159 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
160 return 2;
161 }
163 if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
164 n_log(LOG_ERR, "allegro init failed");
165 return 1;
166 }
167 ALLEGRO_FONT* font = al_create_builtin_font();
168 if (!font) return 1;
170 if (!gui) {
171 al_destroy_font(font);
172 return 1;
173 }
174 n_gui_set_display_size(gui, 400.0f, 300.0f);
175 int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 300.0f);
178
179 int wheel = n_gui_colorwheel_new(gui, win, 50.0f, 50.0f, 200.0f, 200.0f, on_change, NULL);
180 expect_true("a wheel is made", wheel >= 0);
181 expect_true("it starts in the centre", n_gui_colorwheel_get(gui, wheel, &u, &v) && u == 0.0f && v == 0.0f);
182
183 drag(gui, 0.0f, 0.0f, 0.5f, 0.5f);
185 expect_true("a drag moves the point", near(u, 0.5f) && near(v, 0.5f));
186 expect_true("with changes while dragging and one at the release", drag_changes >= 2 && changes == drag_changes + 1);
187
188 drag(gui, 0.5f, 0.5f, 1.2f, 0.0f);
190 expect_true("past the rim it stays on it", near(u, 1.0f) && near(v, 0.0f));
191
192 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(0.3f), AT_Y(-0.3f), 0);
193 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(0.3f), AT_Y(-0.3f), 2);
194 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(0.3f), AT_Y(-0.3f), 2);
196 expect_true("a right press puts it back in the centre", u == 0.0f && v == 0.0f);
197
198 /* Two quick presses at the same place: the first moves the point there,
199 * the second puts it back in the centre and starts no drag, so the
200 * release and a move after it leave it there. */
202 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(0.4f), AT_Y(0.2f), 0);
203 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(0.4f), AT_Y(0.2f), 1);
204 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(0.4f), AT_Y(0.2f), 1);
206 expect_true("a press moves the point", near(u, 0.4f) && near(v, 0.2f));
207 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(0.4f), AT_Y(0.2f), 1);
208 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(0.6f), AT_Y(0.1f), 0);
209 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(0.6f), AT_Y(0.1f), 1);
211 expect_true("a double click puts it back in the centre, and the drag after does not move it", u == 0.0f && v == 0.0f);
213
214 expect_true("set from code", n_gui_colorwheel_set(gui, wheel, -0.25f, 0.5f));
216 expect_true("reads back", u == -0.25f && v == 0.5f);
217 expect_true("set outside is kept on the rim", n_gui_colorwheel_set(gui, wheel, 3.0f, 4.0f));
219 expect_true("at the same angle", near(u, 0.6f) && near(v, 0.8f));
220
221 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(0.0f), AT_Y(0.0f), 0);
222 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(0.0f), AT_Y(0.0f), 1);
223 expect_true("set is refused during a drag", n_gui_colorwheel_set(gui, wheel, 0.1f, 0.1f) == FALSE);
224 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(0.0f), AT_Y(0.0f), 1);
225
226 {
227 ALLEGRO_COLOR c = n_gui_colorwheel_hue(1.0f, 0.0f, NULL);
228 float r = 0.0f, g = 0.0f, b = 0.0f;
229
230 al_unmap_rgb_f(c, &r, &g, &b);
231 expect_true("the default disc is red to the right", r > 0.99f && g < 0.01f && b < 0.01f);
232 c = n_gui_colorwheel_hue(0.0f, 0.0f, NULL);
233 al_unmap_rgb_f(c, &r, &g, &b);
234 expect_true("and white in the centre", r > 0.99f && g > 0.99f && b > 0.99f);
235 }
236 {
237 int label = n_gui_add_label(gui, win, "not a wheel", 10.0f, 270.0f, 100.0f, 20.0f, N_GUI_ALIGN_LEFT);
238
239 expect_true("a widget that is not a wheel is refused", n_gui_colorwheel_get(gui, label, &u, &v) == FALSE);
240 }
241 {
242 ALLEGRO_BITMAP* target = NULL;
243 int old = al_get_new_bitmap_flags();
244
246 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
247 target = al_create_bitmap(400, 300);
248 al_set_new_bitmap_flags(old);
249 al_set_target_bitmap(target);
250 al_clear_to_color(al_map_rgb(0, 0, 0));
252 al_set_target_bitmap(NULL);
253 al_destroy_bitmap(target);
254 expect_true("the caller's colours paint the disc", colour_asked);
255 }
256
258 al_destroy_font(font);
259 if (failures) {
260 fprintf(stderr, "ex_gui_colorwheel: %d failure(s)\n", failures);
261 return 1;
262 }
263 printf("ex_gui_colorwheel: ok\n");
264 return 0;
265}
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 int changes
how many times the picker called back, and with what
static void on_change(const float rgba[4], void *user_data)
the picker's callback
static void drag(N_GUI_CTX *gui, float fu, float fv, float tu, float tv)
press at one point, drag to another, release
static void feed(N_GUI_CTX *gui, int type, int x, int y, int button)
feed one mouse event at display coordinates
static int drag_changes
static int near(float a, float b)
is a near b
#define AT_X(u)
the wheel sits at 50,50 and is 200 by 200, its disc of radius 98 centred at 150,150: display position...
static int colour_asked
set when the caller's colour was asked for
#define AT_Y(v)
as AT_X, for v, which goes up
static ALLEGRO_COLOR grey(float u, float v, void *user_data)
a caller's colour: grey everywhere
static N_GUI_CTX * gui
static void wheel(N_GUI_CTX *gui, int dz)
turn the mouse wheel over the display
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
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
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
#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
void n_gui_set_double_click_time(N_GUI_CTX *ctx, double seconds)
set how long after a press a second one still counts as a double click
Definition n_gui.c:3776
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
Definition n_gui.c:11735
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
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
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
ALLEGRO_COLOR n_gui_colorwheel_hue(float u, float v, void *user_data)
The default disc colour: HSV, hue by angle with red to the right and going counter clockwise,...
int n_gui_colorwheel_get(N_GUI_CTX *ctx, int widget_id, float *u, float *v)
Read the point.
int n_gui_colorwheel_new(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, N_GUI_COLORWHEEL_CHANGE on_change, void *user_data)
Add a colour wheel, its point in the centre.
void n_gui_colorwheel_set_colour(N_GUI_CTX *ctx, int widget_id, N_GUI_COLORWHEEL_COLOUR colour, void *user_data)
Paint the disc with the caller's colours.
int n_gui_colorwheel_set(N_GUI_CTX *ctx, int widget_id, float u, float v)
Set the point.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
A colour wheel for N_GUI: one point in a disc, dragged.
Generic log system.