Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_gui_curve.c File Reference

A curve editor for N_GUI: control points on a curve over [0,1]. More...

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <allegro5/allegro_primitives.h>
#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_gui_curve.h"
Include dependency graph for n_gui_curve.c:

Go to the source code of this file.

Data Structures

struct  _N_GUI_CURVE
 an editor's state, owned by its custom widget More...
struct  _N_GUI_CURVE_BLOCK
 an editor's state and its mouse callback's context in one allocation, because a custom widget owns one pointer More...
struct  _N_GUI_CURVE_MOUSE
 what an editor's mouse callback needs: the context, to find the widget and mark the GUI dirty, and the editor's state More...

Macros

#define N_GUI_CURVE_MIN_GAP   (1.0f / 1024.0f)
 how close in x two points may come, so neither is ever a zero width segment
#define N_GUI_CURVE_SEGMENTS   64
 segments the drawn curve is made of: fine enough to look smooth at any size an editor has on a screen

Functions

static void _curve_block_destroy (void *user_data)
 destroy callback for the block holding both
static float _curve_clamp (float v)
 helper: clamp to [0,1]
static void _curve_draw (N_GUI_WIDGET *widget, float x, float y, ALLEGRO_FONT *font, void *user_data)
 custom draw callback
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 int _curve_mouse (int widget_id, const N_GUI_MOUSE_EVENT *ev, void *user_data)
 custom mouse callback
static _N_GUI_CURVE * _curve_of (N_GUI_CTX *ctx, int widget_id)
 helper: find the editor state behind a widget id, or NULL
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
float n_gui_curve_eval (N_GUI_CTX *ctx, int widget_id, float x)
 The editor's curve at x, as it is drawn.
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_get_points (N_GUI_CTX *ctx, int widget_id, float *xs, float *ys, int max)
 Read the points.
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.
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_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_point_radius (N_GUI_CTX *ctx, int widget_id, float radius)
 Size of the drawn points, for a display scale.
int n_gui_curve_set_points (N_GUI_CTX *ctx, int widget_id, const float *xs, const float *ys, int count)
 Replace the points.

Detailed Description

A curve editor for N_GUI: control points on a curve over [0,1].

Author
Castagnier Mickael
Version
1.0
Date
24/09/2026

Definition in file n_gui_curve.c.


Data Structure Documentation

◆ _N_GUI_CURVE

struct _N_GUI_CURVE

an editor's state, owned by its custom widget

Definition at line 48 of file n_gui_curve.c.

Collaboration diagram for _N_GUI_CURVE:
Data Fields
N_GUI_CURVE_BACKGROUND background drawn behind everything, or NULL
void * background_user_data handed back to background
int count how many points
int dragging the point being dragged, or -1
N_GUI_CURVE_EVAL eval the caller's curve, or NULL for the built-in one
void * eval_user_data handed back to eval
int id the widget
int max room in xs and ys
N_GUI_CURVE_CHANGE on_change the caller's change callback
float radius drawn point radius, pixels
void * user_data handed back to on_change
float * xs points' x, ascending
float * ys points' y

◆ _N_GUI_CURVE_BLOCK

struct _N_GUI_CURVE_BLOCK

an editor's state and its mouse callback's context in one allocation, because a custom widget owns one pointer

Definition at line 294 of file n_gui_curve.c.

Collaboration diagram for _N_GUI_CURVE_BLOCK:
Data Fields
_N_GUI_CURVE cv what the draw callback sees
_N_GUI_CURVE_MOUSE mouse what the mouse callback sees

◆ _N_GUI_CURVE_MOUSE

struct _N_GUI_CURVE_MOUSE

what an editor's mouse callback needs: the context, to find the widget and mark the GUI dirty, and the editor's state

Definition at line 225 of file n_gui_curve.c.

Collaboration diagram for _N_GUI_CURVE_MOUSE:
Data Fields
N_GUI_CTX * ctx context
_N_GUI_CURVE * cv editor

Macro Definition Documentation

◆ N_GUI_CURVE_MIN_GAP

#define N_GUI_CURVE_MIN_GAP   (1.0f / 1024.0f)

how close in x two points may come, so neither is ever a zero width segment

Definition at line 45 of file n_gui_curve.c.

Referenced by _curve_insert(), and _curve_mouse().

◆ N_GUI_CURVE_SEGMENTS

#define N_GUI_CURVE_SEGMENTS   64

segments the drawn curve is made of: fine enough to look smooth at any size an editor has on a screen

Definition at line 42 of file n_gui_curve.c.

Referenced by _curve_draw().

Function Documentation

◆ _curve_block_destroy()

void _curve_block_destroy ( void * user_data)
static

destroy callback for the block holding both

Definition at line 302 of file n_gui_curve.c.

References _N_GUI_CURVE_BLOCK::cv, _N_GUI_CURVE::xs, and _N_GUI_CURVE::ys.

Referenced by n_gui_curve_new().

Here is the caller graph for this function:

◆ _curve_clamp()

float _curve_clamp ( float v)
static

helper: clamp to [0,1]

Definition at line 78 of file n_gui_curve.c.

Referenced by _curve_mouse(), _curve_value(), n_gui_curve_eval_points(), and n_gui_curve_set_points().

Here is the caller graph for this function:

◆ _curve_draw()

void _curve_draw ( N_GUI_WIDGET * widget,
float x,
float y,
ALLEGRO_FONT * font,
void * user_data )
static

◆ _curve_insert()

int _curve_insert ( _N_GUI_CURVE * cv,
float px,
float py )
static

helper: insert a point in x order; returns its index or -1 when full

Definition at line 193 of file n_gui_curve.c.

References _N_GUI_CURVE::count, _N_GUI_CURVE::max, N_GUI_CURVE_MIN_GAP, _N_GUI_CURVE::xs, and _N_GUI_CURVE::ys.

Referenced by _curve_mouse().

Here is the caller graph for this function:

◆ _curve_mouse()

◆ _curve_of()

_N_GUI_CURVE * _curve_of ( N_GUI_CTX * ctx,
int widget_id )
static

helper: find the editor state behind a widget id, or NULL

Definition at line 212 of file n_gui_curve.c.

References _curve_draw(), N_GUI_WIDGET::data, N_GUI_CUSTOM_DATA::draw, n_gui_get_widget(), N_GUI_TYPE_CUSTOM, N_GUI_WIDGET::type, and N_GUI_CUSTOM_DATA::user_data.

Referenced by n_gui_curve_eval(), n_gui_curve_get_points(), n_gui_curve_set_background(), n_gui_curve_set_eval(), n_gui_curve_set_point_radius(), and n_gui_curve_set_points().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _curve_point_at()

int _curve_point_at ( const _N_GUI_CURVE * cv,
const N_GUI_WIDGET * wgt,
float lx,
float ly )
static

helper: the point within grab distance of a widget-local position, or -1

Definition at line 174 of file n_gui_curve.c.

References _N_GUI_CURVE::count, N_GUI_WIDGET::h, _N_GUI_CURVE::radius, N_GUI_WIDGET::w, _N_GUI_CURVE::xs, and _N_GUI_CURVE::ys.

Referenced by _curve_mouse().

Here is the caller graph for this function:

◆ _curve_value()

float _curve_value ( const _N_GUI_CURVE * cv,
float x )
static

helper: the editor's curve at x, the caller's or the built-in one

Definition at line 125 of file n_gui_curve.c.

References _curve_clamp(), _N_GUI_CURVE::count, _N_GUI_CURVE::eval, _N_GUI_CURVE::eval_user_data, n_gui_curve_eval_points(), _N_GUI_CURVE::xs, and _N_GUI_CURVE::ys.

Referenced by _curve_draw(), and n_gui_curve_eval().

Here is the call graph for this function:
Here is the caller graph for this function: