Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_curve.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 <allegro5/allegro_primitives.h>
24
25#include "nilorea/n_common.h"
26#include "nilorea/n_log.h"
27#include "nilorea/n_gui.h"
28#include "nilorea/n_gui_curve.h"
29
30static int log_level = LOG_ERR;
31static int failures = 0;
32
34static int changes = 0, drag_changes = 0;
36static int background_drawn = 0;
37
39static void expect_true(const char* what, int ok) {
40 if (!ok) {
41 n_log(LOG_ERR, "FAIL: %s", what);
42 failures++;
43 } else {
44 n_log(LOG_INFO, "ok: %s", what);
45 }
46}
47
49static int process_args(int argc, char** argv) {
50 int opt = 0;
51
52 while ((opt = getopt(argc, argv, "V:")) != -1) {
53 if (opt == 'V') {
54 log_level = atoi(optarg);
55 } else {
56 return -1;
57 }
58 }
59 return 0;
60}
61
63static void on_change(int widget_id, int dragging, void* user_data) {
64 (void)widget_id;
65 (void)user_data;
66 changes++;
67 if (dragging) drag_changes++;
68}
69
71static float flat(const float* xs, const float* ys, int count, float x, void* user_data) {
72 (void)xs;
73 (void)ys;
74 (void)count;
75 (void)x;
76 (void)user_data;
77 return 0.5f;
78}
79
81static void background(float x, float y, float w, float h, void* user_data) {
82 (void)user_data;
84 al_draw_filled_rectangle(x, y, x + w, y + h, al_map_rgba(40, 40, 40, 128));
85}
86
88static void feed(N_GUI_CTX* gui, int type, int x, int y, int button) {
89 ALLEGRO_EVENT ev;
90
91 memset(&ev, 0, sizeof(ev));
92 ev.type = (ALLEGRO_EVENT_TYPE)type;
93 ev.mouse.x = x;
94 ev.mouse.y = y;
95 ev.mouse.dx = type == ALLEGRO_EVENT_MOUSE_AXES ? 1 : 0;
96 ev.mouse.button = (unsigned int)button;
98}
99
102#define AT_X(cx) ((int)(50.0f + (cx) * 200.0f + 0.5f))
104#define AT_Y(cy) ((int)(50.0f + (1.0f - (cy)) * 100.0f + 0.5f))
105
107static void drag(N_GUI_CTX* gui, float fx, float fy, float tx, float ty) {
108 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(fx), AT_Y(fy), 0);
109 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(fx), AT_Y(fy), 1);
110 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(tx), AT_Y(ty), 0);
111 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(tx), AT_Y(ty), 1);
112}
113
115static void right(N_GUI_CTX* gui, float cx, float cy) {
116 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(cx), AT_Y(cy), 0);
117 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(cx), AT_Y(cy), 2);
118 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(cx), AT_Y(cy), 2);
119}
120
122static int near(float a, float b) {
123 return a - b < 0.011f && b - a < 0.011f;
124}
125
126int main(int argc, char** argv) {
127 float xs[8], ys[8];
128 int n = 0, i = 0, monotone = 1, bounded = 1;
129
131 if (process_args(argc, argv) != 0) {
132 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
133 return 2;
134 }
136 if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
137 n_log(LOG_ERR, "allegro init failed");
138 return 1;
139 }
140 ALLEGRO_FONT* font = al_create_builtin_font();
141 if (!font) return 1;
143 if (!gui) {
144 al_destroy_font(font);
145 return 1;
146 }
147 n_gui_set_display_size(gui, 400.0f, 300.0f);
148 int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 300.0f);
151
152 int ed = n_gui_curve_new(gui, win, 50.0f, 50.0f, 200.0f, 100.0f, 4, on_change, NULL);
153 expect_true("an editor is made", ed >= 0);
154 n = n_gui_curve_get_points(gui, ed, xs, ys, 8);
155 expect_true("it starts as the identity", n == 2 && xs[0] == 0.0f && ys[0] == 0.0f && xs[1] == 1.0f && ys[1] == 1.0f);
156 expect_true("which evaluates as a straight line", near(n_gui_curve_eval(gui, ed, 0.3f), 0.3f));
157
158 /* add, then drag */
159 drag(gui, 0.5f, 0.75f, 0.3f, 0.9f);
160 n = n_gui_curve_get_points(gui, ed, xs, ys, 8);
161 expect_true("a press off the points adds one", n == 3);
162 expect_true("the add is one change, then drag steps", changes == 2 && drag_changes == 1);
163 expect_true("the drag moved it", near(xs[1], 0.3f) && near(ys[1], 0.9f));
164
165 /* past its neighbour */
166 drag(gui, 0.3f, 0.9f, 1.0f, 0.5f);
167 n_gui_curve_get_points(gui, ed, xs, ys, 8);
168 expect_true("an inner point stops short of its neighbour", xs[1] < xs[2] && xs[1] > 0.99f);
169
170 /* an end keeps its x */
171 drag(gui, 0.0f, 0.0f, 0.4f, 0.5f);
172 n_gui_curve_get_points(gui, ed, xs, ys, 8);
173 expect_true("an end moves up and down only", xs[0] == 0.0f && near(ys[0], 0.5f));
174
175 /* removal */
176 right(gui, 0.0f, 0.5f);
177 expect_true("an end cannot be removed", n_gui_curve_get_points(gui, ed, NULL, NULL, 0) == 3);
178 n_gui_curve_get_points(gui, ed, xs, ys, 8);
179 right(gui, xs[1], ys[1]);
180 expect_true("an inner point can", n_gui_curve_get_points(gui, ed, NULL, NULL, 0) == 2);
181
182 /* set from code */
183 {
184 const float sx[4] = {1.0f, 0.25f, 0.0f, 0.75f};
185 const float sy[4] = {1.0f, 0.1f, 0.0f, 0.9f};
186
187 expect_true("points set from code", n_gui_curve_set_points(gui, ed, sx, sy, 4));
188 n = n_gui_curve_get_points(gui, ed, xs, ys, 8);
189 expect_true("come back sorted", n == 4 && xs[0] == 0.0f && xs[1] == 0.25f && ys[1] == 0.1f && xs[3] == 1.0f);
190 expect_true("more than the maximum is refused", n_gui_curve_set_points(gui, ed, sx, sy, 5) == FALSE);
191 }
192 /* the maximum, by the mouse */
193 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(0.5f), AT_Y(0.2f), 0);
194 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(0.5f), AT_Y(0.2f), 1);
195 expect_true("a press adds nothing past the maximum", n_gui_curve_get_points(gui, ed, NULL, NULL, 0) == 4);
196 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(0.5f), AT_Y(0.2f), 1);
197
198 /* no set while dragging */
199 feed(gui, ALLEGRO_EVENT_MOUSE_AXES, AT_X(0.25f), AT_Y(0.1f), 0);
200 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, AT_X(0.25f), AT_Y(0.1f), 1);
201 expect_true("set is refused during a drag", n_gui_curve_set_points(gui, ed, xs, ys, 2) == FALSE);
202 feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, AT_X(0.25f), AT_Y(0.1f), 1);
203 expect_true("and accepted after it", n_gui_curve_set_points(gui, ed, xs, ys, 4) == TRUE);
204
205 /* the caller's curve */
206 n_gui_curve_set_eval(gui, ed, flat, NULL);
207 expect_true("the caller's curve is the editor's", n_gui_curve_eval(gui, ed, 0.9f) == 0.5f);
208 n_gui_curve_set_eval(gui, ed, NULL, NULL);
209
210 /* the built-in spline */
211 {
212 const float px[3] = {0.0f, 0.5f, 1.0f};
213 const float py[3] = {0.0f, 1.0f, 1.0f};
214 float prev = 0.0f;
215
216 for (i = 0; i <= 200; i++) {
217 float v = n_gui_curve_eval_points(px, py, 3, (float)i / 200.0f);
218
219 if (v < prev - 1e-6f) monotone = 0;
220 if (v > 1.0f || v < 0.0f) bounded = 0;
221 prev = v;
222 }
223 expect_true("the spline through rising points never falls", monotone);
224 expect_true("and never overshoots", bounded);
225 expect_true("it holds its end value past the last point", n_gui_curve_eval_points(px, py, 2, 0.9f) == 1.0f);
226 }
227 {
228 int label = n_gui_add_label(gui, win, "not an editor", 10.0f, 200.0f, 100.0f, 20.0f, N_GUI_ALIGN_LEFT);
229
230 expect_true("a widget that is not an editor is refused", n_gui_curve_get_points(gui, label, NULL, NULL, 0) == -1);
231 }
232
233 /* drawn */
234 {
235 ALLEGRO_BITMAP* target = NULL;
236 int old = al_get_new_bitmap_flags();
237
240 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
241 target = al_create_bitmap(400, 300);
242 al_set_new_bitmap_flags(old);
243 al_set_target_bitmap(target);
244 al_clear_to_color(al_map_rgb(0, 0, 0));
246 al_set_target_bitmap(NULL);
247 al_destroy_bitmap(target);
248 expect_true("the background is drawn under it", background_drawn);
249 }
250
252 al_destroy_font(font);
253 if (failures) {
254 fprintf(stderr, "ex_gui_curve: %d failure(s)\n", failures);
255 return 1;
256 }
257 printf("ex_gui_curve: ok\n");
258 return 0;
259}
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...
#define AT_Y(v)
as AT_X, for v, which goes up
static void background(float x, float y, float w, float h, void *user_data)
a background
static int background_drawn
did the background draw
static void right(N_GUI_CTX *gui, float cx, float cy)
right press
static float flat(const float *xs, const float *ys, int count, float x, void *user_data)
a caller's own curve: flat
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
#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_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
int n_gui_curve_new(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int max_points, N_GUI_CURVE_CHANGE on_change, void *user_data)
Add a curve editor to a window.
int n_gui_curve_get_points(N_GUI_CTX *ctx, int widget_id, float *xs, float *ys, int max)
Read the points.
float n_gui_curve_eval(N_GUI_CTX *ctx, int widget_id, float x)
The editor's curve at x, as it is drawn.
void n_gui_curve_set_point_radius(N_GUI_CTX *ctx, int widget_id, float radius)
Size of the drawn points, for a display scale.
void n_gui_curve_set_eval(N_GUI_CTX *ctx, int widget_id, N_GUI_CURVE_EVAL eval, void *user_data)
Draw the caller's own curve through the points.
void n_gui_curve_set_background(N_GUI_CTX *ctx, int widget_id, N_GUI_CURVE_BACKGROUND draw, void *user_data)
Draw something behind the grid and the curve.
float n_gui_curve_eval_points(const float *xs, const float *ys, int count, float x)
A monotone cubic Hermite spline through points.
Definition n_gui_curve.c:82
int n_gui_curve_set_points(N_GUI_CTX *ctx, int widget_id, const float *xs, const float *ys, int count)
Replace the points.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
A curve editor for N_GUI: control points on a curve over [0,1].
Generic log system.