34#include <allegro5/allegro_primitives.h>
42#define N_GUI_CURVE_SEGMENTS 64
45#define N_GUI_CURVE_MIN_GAP (1.0f / 1024.0f)
79 return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v);
84 float h = 0.0f, t = 0.0f, m0 = 0.0f, m1 = 0.0f;
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];
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;
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) {
106 if (m0 / secant > 3.0f) m0 = 3.0f * secant;
107 if (m1 / secant > 3.0f) m1 = 3.0f * secant;
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;
120 return _curve_clamp(h00 * ys[i] + h10 * h * m0 + h01 * ys[i + 1] + h11 * h * m1);
135 float w = 0.0f, h = 0.0f, px = 0.0f, py = 0.0f;
139 if (!widget || !cv)
return;
142 al_draw_filled_rectangle(x, y, x + w, y + h, widget->
theme.
bg_normal);
146 for (i = 1; i < 4; i++) {
147 float t = (float)i / 4.0f;
157 float nx = x + t * w;
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;
175 float grab = cv->
radius * 2.5f;
176 float best = grab * grab;
177 int i = 0, found = -1;
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;
196 if (cv->
count >= cv->
max)
return -1;
197 while (at < cv->count && cv->
xs[at] < px) at++;
199 if (at == 0 || at == cv->
count)
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];
239 float px = 0.0f, py = 0.0f;
241 if (!cv || !wgt || wgt->
w <= 0.0f || wgt->
h <= 0.0f)
return 0;
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));
274 if (d > 0 && d < cv->count - 1) {
278 cv->
xs[d] = px < lo ? lo : (px > hi ? hi : px);
316 if (max_points < 2) {
317 n_log(
LOG_ERR,
"curve editor: max_points %d, needs 2 or more", max_points);
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) {
328 block->
cv.
max = max_points;
330 block->
cv.
xs[1] = 1.0f;
331 block->
cv.
ys[1] = 1.0f;
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++) {
362 for (i = 1; i < count; i++) {
363 float kx = cv->
xs[i], ky = cv->
ys[i];
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];
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];
411 if (!cv || radius <= 0.0f)
return;
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
float y
pointer y from the widget's top edge; outside 0..h during a DRAG
N_GUI_THEME theme
color theme for this widget
ALLEGRO_COLOR text_normal
text normal
int type
widget type (N_GUI_TYPE_*)
ALLEGRO_COLOR border_normal
border normal
void * user_data
opaque user pointer handed back to the callback (not owned by n_gui)
int button
1 left, 2 right for DOWN; 1 for UP and DRAG; 0 otherwise
N_GUI_CUSTOM_DRAW draw
paint callback invoked during n_gui_draw (NULL = draw nothing)
void * data
widget-specific data (union via void pointer)
int type
N_GUI_MOUSE_DOWN, _UP, _MOVE, _DRAG or _WHEEL.
ALLEGRO_COLOR bg_normal
background normal
float x
pointer x from the widget's left edge; outside 0..w during a DRAG
#define N_GUI_MOUSE_DOWN
custom widget mouse event: a press inside it, button 1 or 2
#define N_GUI_MOUSE_DRAG
custom widget mouse event: the pointer moved with button 1 held after a press in it; reported outside...
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
N_GUI_WIDGET * n_gui_get_widget(N_GUI_CTX *ctx, int widget_id)
Get a widget pointer by id.
#define N_GUI_MOUSE_UP
custom widget mouse event: the release of a button 1 press that began in it, wherever the pointer is ...
#define N_GUI_TYPE_CUSTOM
widget type: owner-draw custom widget (rendering delegated to a callback)
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
void n_gui_mark_dirty(N_GUI_CTX *ctx)
Mark the GUI as needing a redraw.
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
The top-level GUI context that holds all windows.
Owner-draw custom widget data: a paint callback, an optional mouse callback, and their user pointers.
A mouse event handed to a custom widget, in the widget's own coordinates.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
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.
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.
void(* N_GUI_CURVE_CHANGE)(int widget_id, int dragging, void *user_data)
called on every change of the points.
float n_gui_curve_eval_points(const float *xs, const float *ys, int count, float x)
A monotone cubic Hermite spline through points.
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
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
static float _curve_clamp(float v)
helper: clamp to [0,1]
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
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
int dragging
the point being dragged, or -1
static void _curve_block_destroy(void *user_data)
destroy callback for the block holding both
N_GUI_CURVE_CHANGE on_change
the caller's change callback
#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
N_GUI_CURVE_EVAL eval
the caller's curve, or NULL for the built-in one
_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
void * user_data
handed back to on_change
#define N_GUI_CURVE_MIN_GAP
how close in x two points may come, so neither is ever a zero width segment
void * background_user_data
handed back to background
float radius
drawn point radius, pixels
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
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].