6 n__gui__curve_8c
gullradriel edited this page 2026-10-09 00:55:18 +02:00

src/n_gui_curve.c

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

Structures

Name
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
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
struct _N_GUI_CURVE
an editor's state, owned by its custom widget

Functions

Name
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
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
_N_GUI_CURVE * _curve_of(N_GUI_CTX * ctx, int widget_id)
helper: find the editor state behind a widget id, or NULL
int _curve_mouse(int widget_id, const N_GUI_MOUSE_EVENT * ev, void * user_data)
custom mouse callback
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
void _curve_draw(N_GUI_WIDGET * widget, float x, float y, ALLEGRO_FONT * font, void * user_data)
custom draw callback
float _curve_clamp(float v)
helper: clamp to [0,1]
void _curve_block_destroy(void * user_data)
destroy callback for the block holding both

Defines

Name
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_MIN_GAP
how close in x two points may come, so neither is ever a zero width segment

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

Functions Documentation

function _curve_value

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

function _curve_point_at

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

function _curve_of

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

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

function _curve_mouse

static int _curve_mouse(
    int widget_id,
    const N_GUI_MOUSE_EVENT * ev,
    void * user_data
)

custom mouse callback

function _curve_insert

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

function _curve_draw

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

custom draw callback

function _curve_clamp

static float _curve_clamp(
    float v
)

helper: clamp to [0,1]

function _curve_block_destroy

static void _curve_block_destroy(
    void * user_data
)

destroy callback for the block holding both

Macros Documentation

define 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

define 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

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifdef HAVE_ALLEGRO

#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"

#define N_GUI_CURVE_SEGMENTS 64
#define N_GUI_CURVE_MIN_GAP (1.0f / 1024.0f)

typedef struct {
    int id;
    float* xs;
    float* ys;
    int count;
    int max;
    int dragging;
    float radius;
    N_GUI_CURVE_CHANGE on_change;
    void* user_data;
    N_GUI_CURVE_EVAL eval;
    void* eval_user_data;
    N_GUI_CURVE_BACKGROUND background;
    void* background_user_data;
} _N_GUI_CURVE;

static float _curve_clamp(float v) {
    return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v);
}

float n_gui_curve_eval_points(const float* xs, const float* ys, int count, float x) {
    int i = 0;
    float h = 0.0f, t = 0.0f, m0 = 0.0f, m1 = 0.0f;

    x = _curve_clamp(x);
    if (!xs || !ys || count <= 0) return x;
    if (count == 1 || x <= xs[0]) return _curve_clamp(ys[0]);
    if (x >= xs[count - 1]) return _curve_clamp(ys[count - 1]);
    for (i = 0; i < count - 2; i++)
        if (x <= xs[i + 1]) break;
    h = xs[i + 1] - xs[i];
    if (h <= 1e-9f) return _curve_clamp(ys[i]);
    t = (x - xs[i]) / h;
    {
        float secant = (ys[i + 1] - ys[i]) / h;
        float prev = (i > 0 && xs[i] - xs[i - 1] > 1e-9f) ? (ys[i] - ys[i - 1]) / (xs[i] - xs[i - 1]) : secant;
        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;

        /* zero at a local extremum, the mean of the two slopes elsewhere */
        m0 = (prev * secant <= 0.0f) ? 0.0f : (prev + secant) * 0.5f;
        m1 = (secant * next <= 0.0f) ? 0.0f : (secant + next) * 0.5f;
        if (fabsf(secant) > 1e-9f) {
            /* Fritsch-Carlson: a tangent over three times the secant is what
             * lets a cubic overshoot */
            if (m0 / secant > 3.0f) m0 = 3.0f * secant;
            if (m1 / secant > 3.0f) m1 = 3.0f * secant;
        } else {
            m0 = 0.0f;
            m1 = 0.0f;
        }
    }
    {
        float t2 = t * t, t3 = t2 * t;
        float h00 = 2.0f * t3 - 3.0f * t2 + 1.0f;
        float h10 = t3 - 2.0f * t2 + t;
        float h01 = -2.0f * t3 + 3.0f * t2;
        float h11 = t3 - t2;

        return _curve_clamp(h00 * ys[i] + h10 * h * m0 + h01 * ys[i + 1] + h11 * h * m1);
    }
}

static float _curve_value(const _N_GUI_CURVE* cv, float x) {
    if (cv->eval) return cv->eval(cv->xs, cv->ys, cv->count, _curve_clamp(x), cv->eval_user_data);
    return n_gui_curve_eval_points(cv->xs, cv->ys, cv->count, x);
}

// cppcheck-suppress constParameterCallback ; callback signature must match N_GUI_CUSTOM_DRAW
// cppcheck-suppress constParameter ; the same, under the id cppcheck 2.10 uses
static void _curve_draw(N_GUI_WIDGET* widget, float x, float y, ALLEGRO_FONT* font, void* user_data) {
    const _N_GUI_CURVE* cv = (const _N_GUI_CURVE*)user_data;
    float w = 0.0f, h = 0.0f, px = 0.0f, py = 0.0f;
    int i = 0;

    (void)font;
    if (!widget || !cv) return;
    w = widget->w;
    h = widget->h;
    al_draw_filled_rectangle(x, y, x + w, y + h, widget->theme.bg_normal);
    if (cv->background) cv->background(x, y, w, h, cv->background_user_data);
    /* quarters: enough to judge where a point sits without competing with
     * the curve */
    for (i = 1; i < 4; i++) {
        float t = (float)i / 4.0f;

        al_draw_line(x + t * w, y, x + t * w, y + h, widget->theme.border_normal, 1.0f);
        al_draw_line(x, y + t * h, x + w, y + t * h, widget->theme.border_normal, 1.0f);
    }
    al_draw_line(x, y + h, x + w, y, widget->theme.border_normal, 1.0f);
    al_draw_rectangle(x, y, x + w, y + h, widget->theme.border_normal, 1.0f);

    for (i = 0; i <= N_GUI_CURVE_SEGMENTS; i++) {
        float t = (float)i / (float)N_GUI_CURVE_SEGMENTS;
        float nx = x + t * w;
        float ny = y + (1.0f - _curve_value(cv, t)) * h;

        if (i > 0) al_draw_line(px, py, nx, ny, widget->theme.text_normal, cv->radius * 0.375f);
        px = nx;
        py = ny;
    }
    for (i = 0; i < cv->count; i++) {
        float cx = x + cv->xs[i] * w;
        float cy = y + (1.0f - cv->ys[i]) * h;

        al_draw_filled_circle(cx, cy, cv->radius, widget->theme.text_normal);
        al_draw_circle(cx, cy, cv->radius, widget->theme.bg_normal, 1.0f);
    }
}

static int _curve_point_at(const _N_GUI_CURVE* cv, const N_GUI_WIDGET* wgt, float lx, float ly) {
    float grab = cv->radius * 2.5f;
    float best = grab * grab;
    int i = 0, found = -1;

    for (i = 0; i < cv->count; i++) {
        float dx = cv->xs[i] * wgt->w - lx;
        float dy = (1.0f - cv->ys[i]) * wgt->h - ly;
        float d = dx * dx + dy * dy;

        if (d <= best) {
            best = d;
            found = i;
        }
    }
    return found;
}

static int _curve_insert(_N_GUI_CURVE* cv, float px, float py) {
    int i = 0, at = 0;

    if (cv->count >= cv->max) return -1;
    while (at < cv->count && cv->xs[at] < px) at++;
    /* never on top of a neighbour, and never outside the ends */
    if (at == 0 || at == cv->count) return -1;
    if (px - cv->xs[at - 1] < N_GUI_CURVE_MIN_GAP || cv->xs[at] - px < N_GUI_CURVE_MIN_GAP) return -1;
    for (i = cv->count; i > at; i--) {
        cv->xs[i] = cv->xs[i - 1];
        cv->ys[i] = cv->ys[i - 1];
    }
    cv->xs[at] = px;
    cv->ys[at] = py;
    cv->count++;
    return at;
}

static _N_GUI_CURVE* _curve_of(N_GUI_CTX* ctx, int widget_id) {
    const N_GUI_WIDGET* wgt = ctx ? n_gui_get_widget(ctx, widget_id) : NULL;
    const N_GUI_CUSTOM_DATA* cd = NULL;

    if (!wgt || wgt->type != N_GUI_TYPE_CUSTOM || !wgt->data) return NULL;
    cd = (const N_GUI_CUSTOM_DATA*)wgt->data;
    /* the paint callback says what the widget is */
    if (cd->draw != _curve_draw) return NULL;
    return (_N_GUI_CURVE*)cd->user_data;
}

typedef struct {
    N_GUI_CTX* ctx;
    _N_GUI_CURVE* cv;
} _N_GUI_CURVE_MOUSE;

// cppcheck-suppress constParameterCallback ; callback signature must match N_GUI_CUSTOM_MOUSE
// cppcheck-suppress constParameter ; the same, under the id cppcheck 2.10 uses
static int _curve_mouse(int widget_id, const N_GUI_MOUSE_EVENT* ev, void* user_data) {
    const _N_GUI_CURVE_MOUSE* cm = (const _N_GUI_CURVE_MOUSE*)user_data;
    _N_GUI_CURVE* cv = cm ? cm->cv : NULL;
    const N_GUI_WIDGET* wgt = cm ? n_gui_get_widget(cm->ctx, widget_id) : NULL;
    float px = 0.0f, py = 0.0f;

    if (!cv || !wgt || wgt->w <= 0.0f || wgt->h <= 0.0f) return 0;
    px = _curve_clamp(ev->x / wgt->w);
    py = _curve_clamp(1.0f - ev->y / wgt->h);
    if (ev->type == N_GUI_MOUSE_DOWN && ev->button == 2) {
        int at = _curve_point_at(cv, wgt, ev->x, ev->y);

        /* removing an end would make the curve jump to whatever the next
         * point says, which reads as the editor losing the curve */
        if (at > 0 && at < cv->count - 1) {
            memmove(cv->xs + at, cv->xs + at + 1, sizeof(float) * (size_t)(cv->count - at - 1));
            memmove(cv->ys + at, cv->ys + at + 1, sizeof(float) * (size_t)(cv->count - at - 1));
            cv->count--;
            if (cv->on_change) cv->on_change(widget_id, 0, cv->user_data);
            n_gui_mark_dirty(cm->ctx);
        }
        return 1;
    }
    if (ev->type == N_GUI_MOUSE_DOWN && ev->button == 1) {
        int at = _curve_point_at(cv, wgt, ev->x, ev->y);

        if (at < 0) {
            at = _curve_insert(cv, px, py);
            if (at >= 0 && cv->on_change) cv->on_change(widget_id, 0, cv->user_data);
        }
        cv->dragging = at;
        n_gui_mark_dirty(cm->ctx);
        return 1;
    }
    if (ev->type == N_GUI_MOUSE_DRAG && cv->dragging >= 0 && cv->dragging < cv->count) {
        int d = cv->dragging;

        /* the ends keep their x so the domain stays whole; the others stay
         * between their neighbours so the points stay in order */
        if (d > 0 && d < cv->count - 1) {
            float lo = cv->xs[d - 1] + N_GUI_CURVE_MIN_GAP;
            float hi = cv->xs[d + 1] - N_GUI_CURVE_MIN_GAP;

            cv->xs[d] = px < lo ? lo : (px > hi ? hi : px);
        }
        cv->ys[d] = py;
        if (cv->on_change) cv->on_change(widget_id, 1, cv->user_data);
        n_gui_mark_dirty(cm->ctx);
        return 1;
    }
    if (ev->type == N_GUI_MOUSE_UP) {
        cv->dragging = -1;
        return 1;
    }
    return 0;
}

typedef struct {
    _N_GUI_CURVE cv;
    _N_GUI_CURVE_MOUSE mouse;
} _N_GUI_CURVE_BLOCK;

static void _curve_block_destroy(void* user_data) {
    _N_GUI_CURVE_BLOCK* block = (_N_GUI_CURVE_BLOCK*)user_data;

    if (!block) return;
    free(block->cv.xs);
    free(block->cv.ys);
    free(block);
}

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) {
    _N_GUI_CURVE_BLOCK* block = NULL;
    int id = -1;

    __n_assert(ctx, return -1);
    if (max_points < 2) {
        n_log(LOG_ERR, "curve editor: max_points %d, needs 2 or more", max_points);
        return -1;
    }
    block = (_N_GUI_CURVE_BLOCK*)calloc(1, sizeof(*block));
    if (!block) return -1;
    block->cv.xs = (float*)calloc((size_t)max_points, sizeof(float));
    block->cv.ys = (float*)calloc((size_t)max_points, sizeof(float));
    if (!block->cv.xs || !block->cv.ys) {
        _curve_block_destroy(block);
        return -1;
    }
    block->cv.max = max_points;
    block->cv.count = 2;
    block->cv.xs[1] = 1.0f;
    block->cv.ys[1] = 1.0f;
    block->cv.dragging = -1;
    block->cv.radius = 4.0f;
    block->cv.on_change = on_change;
    block->cv.user_data = user_data;
    block->mouse.ctx = ctx;
    block->mouse.cv = &block->cv;
    /* the block's first member is the state, so the draw callback's pointer
     * and the block's are the same pointer */
    id = n_gui_add_custom(ctx, window_id, x, y, w, h, _curve_draw, &block->cv);
    if (id < 0) {
        _curve_block_destroy(block);
        return -1;
    }
    block->cv.id = id;
    n_gui_custom_set_destroy(ctx, id, _curve_block_destroy);
    n_gui_custom_set_mouse(ctx, id, _curve_mouse, &block->mouse);
    return id;
}

int n_gui_curve_set_points(N_GUI_CTX* ctx, int widget_id, const float* xs, const float* ys, int count) {
    _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);
    int i = 0;

    if (!cv || !xs || !ys || count < 2 || count > cv->max) return FALSE;
    if (cv->dragging >= 0) return FALSE;
    for (i = 0; i < count; i++) {
        cv->xs[i] = _curve_clamp(xs[i]);
        cv->ys[i] = _curve_clamp(ys[i]);
    }
    /* insertion sort: a handful of points, usually already in order */
    for (i = 1; i < count; i++) {
        float kx = cv->xs[i], ky = cv->ys[i];
        int j;

        for (j = i - 1; j >= 0 && cv->xs[j] > kx; j--) {
            cv->xs[j + 1] = cv->xs[j];
            cv->ys[j + 1] = cv->ys[j];
        }
        cv->xs[j + 1] = kx;
        cv->ys[j + 1] = ky;
    }
    cv->count = count;
    n_gui_mark_dirty(ctx);
    return TRUE;
}

int n_gui_curve_get_points(N_GUI_CTX* ctx, int widget_id, float* xs, float* ys, int max) {
    const _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);
    int i = 0;

    if (!cv) return -1;
    for (i = 0; i < cv->count && i < max; i++) {
        if (xs) xs[i] = cv->xs[i];
        if (ys) ys[i] = cv->ys[i];
    }
    return cv->count;
}

void n_gui_curve_set_eval(N_GUI_CTX* ctx, int widget_id, N_GUI_CURVE_EVAL eval, void* user_data) {
    _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);

    if (!cv) return;
    cv->eval = eval;
    cv->eval_user_data = user_data;
    n_gui_mark_dirty(ctx);
}

void n_gui_curve_set_background(N_GUI_CTX* ctx, int widget_id, N_GUI_CURVE_BACKGROUND draw, void* user_data) {
    _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);

    if (!cv) return;
    cv->background = draw;
    cv->background_user_data = user_data;
    n_gui_mark_dirty(ctx);
}

void n_gui_curve_set_point_radius(N_GUI_CTX* ctx, int widget_id, float radius) {
    _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);

    if (!cv || radius <= 0.0f) return;
    cv->radius = radius;
    n_gui_mark_dirty(ctx);
}

float n_gui_curve_eval(N_GUI_CTX* ctx, int widget_id, float x) {
    const _N_GUI_CURVE* cv = _curve_of(ctx, widget_id);

    if (!cv) return x;
    return _curve_value(cv, x);
}

#endif /* HAVE_ALLEGRO */