Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_gui_curve.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
27
28#ifdef HAVE_ALLEGRO
29
30#include <math.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include <allegro5/allegro_primitives.h>
35
36#include "nilorea/n_common.h"
37#include "nilorea/n_log.h"
38#include "nilorea/n_gui_curve.h"
39
42#define N_GUI_CURVE_SEGMENTS 64
45#define N_GUI_CURVE_MIN_GAP (1.0f / 1024.0f)
46
76
78static float _curve_clamp(float v) {
79 return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v);
80}
81
82float n_gui_curve_eval_points(const float* xs, const float* ys, int count, float x) {
83 int i = 0;
84 float h = 0.0f, t = 0.0f, m0 = 0.0f, m1 = 0.0f;
85
86 x = _curve_clamp(x);
87 if (!xs || !ys || count <= 0) return x;
88 if (count == 1 || x <= xs[0]) return _curve_clamp(ys[0]);
89 if (x >= xs[count - 1]) return _curve_clamp(ys[count - 1]);
90 for (i = 0; i < count - 2; i++)
91 if (x <= xs[i + 1]) break;
92 h = xs[i + 1] - xs[i];
93 if (h <= 1e-9f) return _curve_clamp(ys[i]);
94 t = (x - xs[i]) / h;
95 {
96 float secant = (ys[i + 1] - ys[i]) / h;
97 float prev = (i > 0 && xs[i] - xs[i - 1] > 1e-9f) ? (ys[i] - ys[i - 1]) / (xs[i] - xs[i - 1]) : secant;
98 float next = (i + 2 < count && xs[i + 2] - xs[i + 1] > 1e-9f) ? (ys[i + 2] - ys[i + 1]) / (xs[i + 2] - xs[i + 1]) : secant;
99
100 /* zero at a local extremum, the mean of the two slopes elsewhere */
101 m0 = (prev * secant <= 0.0f) ? 0.0f : (prev + secant) * 0.5f;
102 m1 = (secant * next <= 0.0f) ? 0.0f : (secant + next) * 0.5f;
103 if (fabsf(secant) > 1e-9f) {
104 /* Fritsch-Carlson: a tangent over three times the secant is what
105 * lets a cubic overshoot */
106 if (m0 / secant > 3.0f) m0 = 3.0f * secant;
107 if (m1 / secant > 3.0f) m1 = 3.0f * secant;
108 } else {
109 m0 = 0.0f;
110 m1 = 0.0f;
111 }
112 }
113 {
114 float t2 = t * t, t3 = t2 * t;
115 float h00 = 2.0f * t3 - 3.0f * t2 + 1.0f;
116 float h10 = t3 - 2.0f * t2 + t;
117 float h01 = -2.0f * t3 + 3.0f * t2;
118 float h11 = t3 - t2;
119
120 return _curve_clamp(h00 * ys[i] + h10 * h * m0 + h01 * ys[i + 1] + h11 * h * m1);
121 }
122}
123
125static float _curve_value(const _N_GUI_CURVE* cv, float x) {
126 if (cv->eval) return cv->eval(cv->xs, cv->ys, cv->count, _curve_clamp(x), cv->eval_user_data);
127 return n_gui_curve_eval_points(cv->xs, cv->ys, cv->count, x);
128}
129
131// cppcheck-suppress constParameterCallback ; callback signature must match N_GUI_CUSTOM_DRAW
132// cppcheck-suppress constParameter ; the same, under the id cppcheck 2.10 uses
133static void _curve_draw(N_GUI_WIDGET* widget, float x, float y, ALLEGRO_FONT* font, void* user_data) {
134 const _N_GUI_CURVE* cv = (const _N_GUI_CURVE*)user_data;
135 float w = 0.0f, h = 0.0f, px = 0.0f, py = 0.0f;
136 int i = 0;
137
138 (void)font;
139 if (!widget || !cv) return;
140 w = widget->w;
141 h = widget->h;
142 al_draw_filled_rectangle(x, y, x + w, y + h, widget->theme.bg_normal);
143 if (cv->background) cv->background(x, y, w, h, cv->background_user_data);
144 /* quarters: enough to judge where a point sits without competing with
145 * the curve */
146 for (i = 1; i < 4; i++) {
147 float t = (float)i / 4.0f;
148
149 al_draw_line(x + t * w, y, x + t * w, y + h, widget->theme.border_normal, 1.0f);
150 al_draw_line(x, y + t * h, x + w, y + t * h, widget->theme.border_normal, 1.0f);
151 }
152 al_draw_line(x, y + h, x + w, y, widget->theme.border_normal, 1.0f);
153 al_draw_rectangle(x, y, x + w, y + h, widget->theme.border_normal, 1.0f);
154
155 for (i = 0; i <= N_GUI_CURVE_SEGMENTS; i++) {
156 float t = (float)i / (float)N_GUI_CURVE_SEGMENTS;
157 float nx = x + t * w;
158 float ny = y + (1.0f - _curve_value(cv, t)) * h;
159
160 if (i > 0) al_draw_line(px, py, nx, ny, widget->theme.text_normal, cv->radius * 0.375f);
161 px = nx;
162 py = ny;
163 }
164 for (i = 0; i < cv->count; i++) {
165 float cx = x + cv->xs[i] * w;
166 float cy = y + (1.0f - cv->ys[i]) * h;
167
168 al_draw_filled_circle(cx, cy, cv->radius, widget->theme.text_normal);
169 al_draw_circle(cx, cy, cv->radius, widget->theme.bg_normal, 1.0f);
170 }
171}
172
174static int _curve_point_at(const _N_GUI_CURVE* cv, const N_GUI_WIDGET* wgt, float lx, float ly) {
175 float grab = cv->radius * 2.5f;
176 float best = grab * grab;
177 int i = 0, found = -1;
178
179 for (i = 0; i < cv->count; i++) {
180 float dx = cv->xs[i] * wgt->w - lx;
181 float dy = (1.0f - cv->ys[i]) * wgt->h - ly;
182 float d = dx * dx + dy * dy;
183
184 if (d <= best) {
185 best = d;
186 found = i;
187 }
188 }
189 return found;
190}
191
193static int _curve_insert(_N_GUI_CURVE* cv, float px, float py) {
194 int i = 0, at = 0;
195
196 if (cv->count >= cv->max) return -1;
197 while (at < cv->count && cv->xs[at] < px) at++;
198 /* never on top of a neighbour, and never outside the ends */
199 if (at == 0 || at == cv->count) return -1;
200 if (px - cv->xs[at - 1] < N_GUI_CURVE_MIN_GAP || cv->xs[at] - px < N_GUI_CURVE_MIN_GAP) return -1;
201 for (i = cv->count; i > at; i--) {
202 cv->xs[i] = cv->xs[i - 1];
203 cv->ys[i] = cv->ys[i - 1];
204 }
205 cv->xs[at] = px;
206 cv->ys[at] = py;
207 cv->count++;
208 return at;
209}
210
212static _N_GUI_CURVE* _curve_of(N_GUI_CTX* ctx, int widget_id) {
213 const N_GUI_WIDGET* wgt = ctx ? n_gui_get_widget(ctx, widget_id) : NULL;
214 const N_GUI_CUSTOM_DATA* cd = NULL;
215
216 if (!wgt || wgt->type != N_GUI_TYPE_CUSTOM || !wgt->data) return NULL;
217 cd = (const N_GUI_CUSTOM_DATA*)wgt->data;
218 /* the paint callback says what the widget is */
219 if (cd->draw != _curve_draw) return NULL;
220 return (_N_GUI_CURVE*)cd->user_data;
221}
222
231
233// cppcheck-suppress constParameterCallback ; callback signature must match N_GUI_CUSTOM_MOUSE
234// cppcheck-suppress constParameter ; the same, under the id cppcheck 2.10 uses
235static int _curve_mouse(int widget_id, const N_GUI_MOUSE_EVENT* ev, void* user_data) {
236 const _N_GUI_CURVE_MOUSE* cm = (const _N_GUI_CURVE_MOUSE*)user_data;
237 _N_GUI_CURVE* cv = cm ? cm->cv : NULL;
238 const N_GUI_WIDGET* wgt = cm ? n_gui_get_widget(cm->ctx, widget_id) : NULL;
239 float px = 0.0f, py = 0.0f;
240
241 if (!cv || !wgt || wgt->w <= 0.0f || wgt->h <= 0.0f) return 0;
242 px = _curve_clamp(ev->x / wgt->w);
243 py = _curve_clamp(1.0f - ev->y / wgt->h);
244 if (ev->type == N_GUI_MOUSE_DOWN && ev->button == 2) {
245 int at = _curve_point_at(cv, wgt, ev->x, ev->y);
246
247 /* removing an end would make the curve jump to whatever the next
248 * point says, which reads as the editor losing the curve */
249 if (at > 0 && at < cv->count - 1) {
250 memmove(cv->xs + at, cv->xs + at + 1, sizeof(float) * (size_t)(cv->count - at - 1));
251 memmove(cv->ys + at, cv->ys + at + 1, sizeof(float) * (size_t)(cv->count - at - 1));
252 cv->count--;
253 if (cv->on_change) cv->on_change(widget_id, 0, cv->user_data);
255 }
256 return 1;
257 }
258 if (ev->type == N_GUI_MOUSE_DOWN && ev->button == 1) {
259 int at = _curve_point_at(cv, wgt, ev->x, ev->y);
260
261 if (at < 0) {
262 at = _curve_insert(cv, px, py);
263 if (at >= 0 && cv->on_change) cv->on_change(widget_id, 0, cv->user_data);
264 }
265 cv->dragging = at;
267 return 1;
268 }
269 if (ev->type == N_GUI_MOUSE_DRAG && cv->dragging >= 0 && cv->dragging < cv->count) {
270 int d = cv->dragging;
271
272 /* the ends keep their x so the domain stays whole; the others stay
273 * between their neighbours so the points stay in order */
274 if (d > 0 && d < cv->count - 1) {
275 float lo = cv->xs[d - 1] + N_GUI_CURVE_MIN_GAP;
276 float hi = cv->xs[d + 1] - N_GUI_CURVE_MIN_GAP;
277
278 cv->xs[d] = px < lo ? lo : (px > hi ? hi : px);
279 }
280 cv->ys[d] = py;
281 if (cv->on_change) cv->on_change(widget_id, 1, cv->user_data);
283 return 1;
284 }
285 if (ev->type == N_GUI_MOUSE_UP) {
286 cv->dragging = -1;
287 return 1;
288 }
289 return 0;
290}
291
300
302static void _curve_block_destroy(void* user_data) {
303 _N_GUI_CURVE_BLOCK* block = (_N_GUI_CURVE_BLOCK*)user_data;
304
305 if (!block) return;
306 free(block->cv.xs);
307 free(block->cv.ys);
308 free(block);
309}
310
311int 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) {
312 _N_GUI_CURVE_BLOCK* block = NULL;
313 int id = -1;
314
315 __n_assert(ctx, return -1);
316 if (max_points < 2) {
317 n_log(LOG_ERR, "curve editor: max_points %d, needs 2 or more", max_points);
318 return -1;
319 }
320 block = (_N_GUI_CURVE_BLOCK*)calloc(1, sizeof(*block));
321 if (!block) return -1;
322 block->cv.xs = (float*)calloc((size_t)max_points, sizeof(float));
323 block->cv.ys = (float*)calloc((size_t)max_points, sizeof(float));
324 if (!block->cv.xs || !block->cv.ys) {
326 return -1;
327 }
328 block->cv.max = max_points;
329 block->cv.count = 2;
330 block->cv.xs[1] = 1.0f;
331 block->cv.ys[1] = 1.0f;
332 block->cv.dragging = -1;
333 block->cv.radius = 4.0f;
334 block->cv.on_change = on_change;
335 block->cv.user_data = user_data;
336 block->mouse.ctx = ctx;
337 block->mouse.cv = &block->cv;
338 /* the block's first member is the state, so the draw callback's pointer
339 * and the block's are the same pointer */
340 id = n_gui_add_custom(ctx, window_id, x, y, w, h, _curve_draw, &block->cv);
341 if (id < 0) {
343 return -1;
344 }
345 block->cv.id = id;
347 n_gui_custom_set_mouse(ctx, id, _curve_mouse, &block->mouse);
348 return id;
349}
350
351int n_gui_curve_set_points(N_GUI_CTX* ctx, int widget_id, const float* xs, const float* ys, int count) {
352 _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);
353 int i = 0;
354
355 if (!cv || !xs || !ys || count < 2 || count > cv->max) return FALSE;
356 if (cv->dragging >= 0) return FALSE;
357 for (i = 0; i < count; i++) {
358 cv->xs[i] = _curve_clamp(xs[i]);
359 cv->ys[i] = _curve_clamp(ys[i]);
360 }
361 /* insertion sort: a handful of points, usually already in order */
362 for (i = 1; i < count; i++) {
363 float kx = cv->xs[i], ky = cv->ys[i];
364 int j;
365
366 for (j = i - 1; j >= 0 && cv->xs[j] > kx; j--) {
367 cv->xs[j + 1] = cv->xs[j];
368 cv->ys[j + 1] = cv->ys[j];
369 }
370 cv->xs[j + 1] = kx;
371 cv->ys[j + 1] = ky;
372 }
373 cv->count = count;
374 n_gui_mark_dirty(ctx);
375 return TRUE;
376}
377
378int n_gui_curve_get_points(N_GUI_CTX* ctx, int widget_id, float* xs, float* ys, int max) {
379 const _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);
380 int i = 0;
381
382 if (!cv) return -1;
383 for (i = 0; i < cv->count && i < max; i++) {
384 if (xs) xs[i] = cv->xs[i];
385 if (ys) ys[i] = cv->ys[i];
386 }
387 return cv->count;
388}
389
390void n_gui_curve_set_eval(N_GUI_CTX* ctx, int widget_id, N_GUI_CURVE_EVAL eval, void* user_data) {
391 _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);
392
393 if (!cv) return;
394 cv->eval = eval;
395 cv->eval_user_data = user_data;
396 n_gui_mark_dirty(ctx);
397}
398
399void n_gui_curve_set_background(N_GUI_CTX* ctx, int widget_id, N_GUI_CURVE_BACKGROUND draw, void* user_data) {
400 _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);
401
402 if (!cv) return;
403 cv->background = draw;
404 cv->background_user_data = user_data;
405 n_gui_mark_dirty(ctx);
406}
407
408void n_gui_curve_set_point_radius(N_GUI_CTX* ctx, int widget_id, float radius) {
409 _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);
410
411 if (!cv || radius <= 0.0f) return;
412 cv->radius = radius;
413 n_gui_mark_dirty(ctx);
414}
415
416float n_gui_curve_eval(N_GUI_CTX* ctx, int widget_id, float x) {
417 const _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);
418
419 if (!cv) return x;
420 return _curve_value(cv, x);
421}
422
423#endif /* HAVE_ALLEGRO */
static void on_change(const float rgba[4], void *user_data)
the picker's callback
static void draw(N_GUI_CTX *gui)
draw the whole context into a memory bitmap
FONT * font
the default 8x8 font, valid after allegro_init()
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
float h
height
Definition n_gui.h:1104
float y
pointer y from the widget's top edge; outside 0..h during a DRAG
Definition n_gui.h:1208
N_GUI_THEME theme
color theme for this widget
Definition n_gui.h:1118
float w
width
Definition n_gui.h:1102
ALLEGRO_COLOR text_normal
text normal
Definition n_gui.h:398
int type
widget type (N_GUI_TYPE_*)
Definition n_gui.h:1096
ALLEGRO_COLOR border_normal
border normal
Definition n_gui.h:392
void * user_data
opaque user pointer handed back to the callback (not owned by n_gui)
Definition n_gui.h:1233
int button
1 left, 2 right for DOWN; 1 for UP and DRAG; 0 otherwise
Definition n_gui.h:1204
N_GUI_CUSTOM_DRAW draw
paint callback invoked during n_gui_draw (NULL = draw nothing)
Definition n_gui.h:1231
void * data
widget-specific data (union via void pointer)
Definition n_gui.h:1122
int type
N_GUI_MOUSE_DOWN, _UP, _MOVE, _DRAG or _WHEEL.
Definition n_gui.h:1202
ALLEGRO_COLOR bg_normal
background normal
Definition n_gui.h:386
float x
pointer x from the widget's left edge; outside 0..w during a DRAG
Definition n_gui.h:1206
#define N_GUI_MOUSE_DOWN
custom widget mouse event: a press inside it, button 1 or 2
Definition n_gui.h:1187
#define N_GUI_MOUSE_DRAG
custom widget mouse event: the pointer moved with button 1 held after a press in it; reported outside...
Definition n_gui.h:1195
void n_gui_custom_set_mouse(N_GUI_CTX *ctx, int widget_id, N_GUI_CUSTOM_MOUSE on_mouse, void *user_data)
give a custom widget mouse input
Definition n_gui.c:5730
N_GUI_WIDGET * n_gui_get_widget(N_GUI_CTX *ctx, int widget_id)
Get a widget pointer by id.
Definition n_gui.c:3701
#define N_GUI_MOUSE_UP
custom widget mouse event: the release of a button 1 press that began in it, wherever the pointer is ...
Definition n_gui.h:1190
#define N_GUI_TYPE_CUSTOM
widget type: owner-draw custom widget (rendering delegated to a callback)
Definition n_gui.h:140
void n_gui_custom_set_destroy(N_GUI_CTX *ctx, int widget_id, void(*destroy)(void *user_data))
hand a custom widget's paint user pointer to the widget
Definition n_gui.c:5745
void n_gui_mark_dirty(N_GUI_CTX *ctx)
Mark the GUI as needing a redraw.
Definition n_gui.c:1188
int n_gui_add_custom(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, N_GUI_CUSTOM_DRAW draw, void *user_data)
add an owner-draw custom widget; returns the widget id or -1
Definition n_gui.c:5698
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
Owner-draw custom widget data: a paint callback, an optional mouse callback, and their user pointers.
Definition n_gui.h:1229
A mouse event handed to a custom widget, in the widget's own coordinates.
Definition n_gui.h:1200
A single GUI widget.
Definition n_gui.h:1092
#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
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)(const float *xs, const float *ys, int count, float x, void *user_data)
the caller's curve through the points, for drawing.
Definition n_gui_curve.h:84
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.
void(* N_GUI_CURVE_BACKGROUND)(float x, float y, float w, float h, void *user_data)
drawn behind the grid and the curve, such as a histogram.
Definition n_gui_curve.h:92
void(* N_GUI_CURVE_CHANGE)(int widget_id, int dragging, void *user_data)
called on every change of the points.
Definition n_gui_curve.h:75
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.
float * xs
points' x, ascending
Definition n_gui_curve.c:52
N_GUI_CTX * ctx
context
static int _curve_insert(_N_GUI_CURVE *cv, float px, float py)
helper: insert a point in x order; returns its index or -1 when full
int id
the widget
Definition n_gui_curve.c:50
static float _curve_clamp(float v)
helper: clamp to [0,1]
Definition n_gui_curve.c:78
static int _curve_point_at(const _N_GUI_CURVE *cv, const N_GUI_WIDGET *wgt, float lx, float ly)
helper: the point within grab distance of a widget-local position, or -1
int count
how many points
Definition n_gui_curve.c:56
static float _curve_value(const _N_GUI_CURVE *cv, float x)
helper: the editor's curve at x, the caller's or the built-in one
static _N_GUI_CURVE * _curve_of(N_GUI_CTX *ctx, int widget_id)
helper: find the editor state behind a widget id, or NULL
void * eval_user_data
handed back to eval
Definition n_gui_curve.c:70
_N_GUI_CURVE * cv
editor
int dragging
the point being dragged, or -1
Definition n_gui_curve.c:60
static void _curve_block_destroy(void *user_data)
destroy callback for the block holding both
int max
room in xs and ys
Definition n_gui_curve.c:58
N_GUI_CURVE_CHANGE on_change
the caller's change callback
Definition n_gui_curve.c:64
float * ys
points' y
Definition n_gui_curve.c:54
#define N_GUI_CURVE_SEGMENTS
segments the drawn curve is made of: fine enough to look smooth at any size an editor has on a screen
Definition n_gui_curve.c:42
N_GUI_CURVE_EVAL eval
the caller's curve, or NULL for the built-in one
Definition n_gui_curve.c:68
_N_GUI_CURVE cv
what the draw callback sees
_N_GUI_CURVE_MOUSE mouse
what the mouse callback sees
N_GUI_CURVE_BACKGROUND background
drawn behind everything, or NULL
Definition n_gui_curve.c:72
void * user_data
handed back to on_change
Definition n_gui_curve.c:66
#define N_GUI_CURVE_MIN_GAP
how close in x two points may come, so neither is ever a zero width segment
Definition n_gui_curve.c:45
void * background_user_data
handed back to background
Definition n_gui_curve.c:74
float radius
drawn point radius, pixels
Definition n_gui_curve.c:62
static void _curve_draw(N_GUI_WIDGET *widget, float x, float y, ALLEGRO_FONT *font, void *user_data)
custom draw callback
static int _curve_mouse(int widget_id, const N_GUI_MOUSE_EVENT *ev, void *user_data)
custom mouse callback
an editor's state, owned by its custom widget
Definition n_gui_curve.c:48
an editor's state and its mouse callback's context in one allocation, because a custom widget owns on...
what an editor's mouse callback needs: the context, to find the widget and mark the GUI dirty,...
A curve editor for N_GUI: control points on a curve over [0,1].
Generic log system.