commit dfc020e6189e7e7ebaa2d0d42d2aed3b93037902 Author: Alpaq92 Date: Tue Oct 6 13:30:48 2026 +0200 Animation for Nuklear NukAnim is one C99 header: a port of ImAnim by Soufiane KHIAT, with ImAnimate's animate by Raidcore. It has tweens, easing, color spaces, clips and timelines, motion paths, procedural motion, and the Nuklear side: scrolling, style crossfades, text effects and an inspector. Co-Authored-By: Claude Opus 5.5 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..113ab27 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,29 @@ +name: ci + +on: + push: + pull_request: + +jobs: + test: + name: ${{ matrix.name }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + - { name: linux gcc, os: ubuntu-latest, cmake: -DCMAKE_C_COMPILER=gcc } + - { name: linux clang, os: ubuntu-latest, cmake: -DCMAKE_C_COMPILER=clang } + - { name: linux sanitizers, os: ubuntu-latest, cmake: -DCMAKE_C_COMPILER=clang -DNUKANIM_SANITIZE=ON } + - { name: macos clang, os: macos-latest, cmake: "" } + - { name: windows msvc, os: windows-latest, cmake: "" } + steps: + - uses: actions/checkout@v4 + with: + submodules: true + - name: Build + run: | + cmake -S . -B build -DCMAKE_BUILD_TYPE=Release ${{ matrix.cmake }} + cmake --build build --config Release + - name: Test + run: ctest --test-dir build -C Release --output-on-failure diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0155cb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build*/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..772a30a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/nuklear"] + path = external/nuklear + url = https://github.com/Immediate-Mode-UI/Nuklear.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4c3d082 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,54 @@ +cmake_minimum_required(VERSION 3.16) +project(NukAnim C) + +add_library(nukanim INTERFACE) +target_include_directories(nukanim INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) + +if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + return() +endif() + +option(NUKANIM_SANITIZE "Build the tests with AddressSanitizer and UBSan" OFF) +enable_testing() +set(NUKLEAR ${CMAKE_CURRENT_SOURCE_DIR}/external/nuklear) +if(NOT EXISTS ${NUKLEAR}/nuklear.h) + message(FATAL_ERROR "Nuklear is missing: git submodule update --init") +endif() + +if(MSVC) + set(STRICT /W4 /WX /wd5287) + set(QUIET /W1) +else() + set(STRICT -std=c99 -Wall -Wextra -pedantic -Werror) + set(QUIET -w) + find_library(MATH m) +endif() + +add_library(nuklear_tests STATIC tests/nuklear.c) +target_include_directories(nuklear_tests PUBLIC ${NUKLEAR}) +target_compile_options(nuklear_tests PRIVATE ${QUIET}) +if(MATH) + target_link_libraries(nuklear_tests PUBLIC ${MATH}) +endif() + +foreach(test core clip path fx nk) + add_executable(test_${test} tests/${test}.c) + target_link_libraries(test_${test} PRIVATE nukanim nuklear_tests) + target_compile_options(test_${test} PRIVATE ${STRICT}) + if(NUKANIM_SANITIZE) + target_compile_options(test_${test} PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer) + target_link_options(test_${test} PRIVATE -fsanitize=address,undefined) + endif() + add_test(NAME ${test} COMMAND test_${test}) +endforeach() + +add_library(nuklear_examples STATIC examples/nuklear.c) +target_include_directories(nuklear_examples PUBLIC ${NUKLEAR} examples) +target_compile_options(nuklear_examples PRIVATE ${QUIET}) +if(MATH) + target_link_libraries(nuklear_examples PUBLIC ${MATH}) +endif() + +add_executable(clips examples/clips.c) +target_link_libraries(clips PRIVATE nukanim nuklear_examples) +target_compile_options(clips PRIVATE ${STRICT}) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a76a843 --- /dev/null +++ b/LICENSE @@ -0,0 +1,23 @@ +MIT License + +Copyright (c) 2026 Alpaq92 +Copyright (c) 2025 SoufianeKHIAT (ImAnim) +Copyright (c) 2024-2025 Raidcore (ImAnimate) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..245b3a5 --- /dev/null +++ b/README.md @@ -0,0 +1,97 @@ +# NukAnim + +Animation for [Nuklear](https://github.com/Immediate-Mode-UI/Nuklear) in one C99 header, which includes +`` and nothing else (and `` only for Nuklear's default allocator). It is a port of +[ImAnim](https://github.com/soufianekhiat/ImAnim) by Soufiane Khiat, with the hover idiom of +[ImAnimate](https://github.com/RaidcoreGG/ImAnimate) by Raidcore. + +![Easing curves](docs/clips/easing.gif) + +## Use + +```c +#include "nuklear.h" +#define NUKANIM_IMPLEMENTATION /* in one C file */ +#include "nukanim.h" + +struct nka_context *anim = nka_create(NULL); /* malloc, under NK_INCLUDE_DEFAULT_ALLOCATOR */ + +/* Every frame, before the UI: */ +nka_update(anim, dt); + +float alpha = nka_tween_float(anim, nka_id("panel"), nka_id("alpha"), open ? 1.0f : 0.0f, 0.25f, + nka_ease(NKA_EASE_OUT_CUBIC), NKA_POLICY_CROSSFADE, 0); + +/* After it: an app that only draws on input keeps drawing while something moves. */ +if (nka_busy(anim)) request_frame(); +``` + +## What it does + +**Tweens** of floats, vectors, ints and colors, keyed by an id and a channel, with crossfade, cut, queue, +additive and multiply policies; targets relative to a window, computed by a callback, or eased per axis. + +**Easing**: the thirty classic curves, steps, cubic béziers, springs and your own functions. ImAnimate's +curves are among them, and so is its way of animating a value that keeps its own state: + +```c +static float size = 1; +if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds)) + nka_animate(anim, 1, 1.1f, 150, &size, NKA_EASE_OUT_CUBIC); +else + nka_animate(anim, 1.1f, 1, 250, &size, NKA_EASE_OUT_CUBIC); +``` + +![Hover](docs/clips/hover.gif) + +**Colors** blend in sRGB, linear sRGB, HSV, OKLAB or OKLCH. + +![Color spaces](docs/clips/colors.gif) + +**Clips**: keyframed timelines with loops, delays, markers, staggering across lists and grids, variation +from loop to loop, layering and chaining. They save to and load from memory. + +![Clips](docs/clips/timeline.gif) + +**Paths**: lines, béziers and Catmull-Rom splines to move along at constant speed, morph between, and set +text on. + +![Paths](docs/clips/paths.gif) + +**Procedural motion**: oscillators, shake, wiggle, noise, gradients, 2D transforms and drag snapping. + +![Procedural](docs/clips/procedural.gif) + +**Nuklear**: eased scrolling for windows and groups, whole-style crossfades, text that arrives glyph by +glyph, and an inspector window with clip timelines and a profiler. Tested with Nuklear 4.13.3. + +![Styles](docs/clips/styles.gif) + +![Text](docs/clips/stagger.gif) + +## Differences from ImAnim + +- A context you create holds the state, instead of globals; `nka_update(anim, dt)` once a frame replaces + the `dt` of every call, and `nka_busy` tells an app that only draws on input when to keep going. +- Nuklear cannot turn text: glyphs on a path stay upright, and the rotate effect fades. Scaled text needs + a backend that draws Nuklear's vertex output. +- Parametric curves are computed exactly rather than read from lookup tables. +- Clip files became buffers you read and write yourself, little-endian and checksummed. +- Bugs fixed on the way, among them: the queue policy only waited a frame, additive tweens added their + delta every frame, garbage collection dropped tweens in use, back and elastic curves ignored their + parameters, finite loops never ended, markers at the end of a clip never fired, finished gradient, + transform, morph and style tweens restarted forever, noise channels sampled the noise where it is + flattest, and shakes only shook one axis. + +## Tests + +```sh +git submodule update --init +cmake -S . -B build && cmake --build build && ctest --test-dir build +``` + +`build/clips` renders the clips above, run from the repository's root; `tools/gif.py` makes them GIFs. + +## License + +MIT. [LICENSE](LICENSE) keeps ImAnim's and ImAnimate's notices. diff --git a/docs/clips/colors.gif b/docs/clips/colors.gif new file mode 100644 index 0000000..592a224 Binary files /dev/null and b/docs/clips/colors.gif differ diff --git a/docs/clips/easing.gif b/docs/clips/easing.gif new file mode 100644 index 0000000..e48df88 Binary files /dev/null and b/docs/clips/easing.gif differ diff --git a/docs/clips/hover.gif b/docs/clips/hover.gif new file mode 100644 index 0000000..46d3743 Binary files /dev/null and b/docs/clips/hover.gif differ diff --git a/docs/clips/paths.gif b/docs/clips/paths.gif new file mode 100644 index 0000000..d20c171 Binary files /dev/null and b/docs/clips/paths.gif differ diff --git a/docs/clips/procedural.gif b/docs/clips/procedural.gif new file mode 100644 index 0000000..3c6b794 Binary files /dev/null and b/docs/clips/procedural.gif differ diff --git a/docs/clips/stagger.gif b/docs/clips/stagger.gif new file mode 100644 index 0000000..dfb1eed Binary files /dev/null and b/docs/clips/stagger.gif differ diff --git a/docs/clips/styles.gif b/docs/clips/styles.gif new file mode 100644 index 0000000..6657d25 Binary files /dev/null and b/docs/clips/styles.gif differ diff --git a/docs/clips/timeline.gif b/docs/clips/timeline.gif new file mode 100644 index 0000000..ab266d3 Binary files /dev/null and b/docs/clips/timeline.gif differ diff --git a/examples/clips.c b/examples/clips.c new file mode 100644 index 0000000..23ed774 --- /dev/null +++ b/examples/clips.c @@ -0,0 +1,611 @@ +/* The README's clips: Nuklear's anti-aliased vertex output, rasterized in + * software, written as raw RGBA frames for tools/gif.py to turn into GIFs. + * + * clips run from the repository's root + */ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include + +#include "nk.h" +#define NUKANIM_IMPLEMENTATION +#include "nukanim.h" + +#define DT 0.03f +#define FONTS "external/nuklear/extra_font/" + +struct vertex { float x, y, u, v; nk_byte c[4]; }; + +static struct nk_context ctx; +static struct nka_context *anim; +static struct nk_font *font, *bold, *small; +static struct nk_draw_null_texture null_tex; +static unsigned char *tex, *fb; +static int tex_w, tex_h, fb_w, fb_h; + +static const struct nk_color ink = { 230, 232, 236, 255 }, dim = { 138, 143, 152, 255 }; +static const struct nk_color ground = { 22, 24, 29, 255 }, panel = { 31, 34, 41, 255 }, line = { 48, 52, 62, 255 }; +static const struct nk_color cyan = { 91, 194, 231, 255 }, copper = { 204, 120, 88, 255 }; + +static void sample(float u, float v, float *out) +{ + float x = u * (float)tex_w - 0.5f, y = v * (float)tex_h - 0.5f; + int x0 = (int)floorf(x), y0 = (int)floorf(y), i, j, k; + float fx = x - (float)x0, fy = y - (float)y0; + out[0] = out[1] = out[2] = out[3] = 0; + for (j = 0; j < 2; ++j) + for (i = 0; i < 2; ++i) { + int tx = x0 + i < 0 ? 0 : (x0 + i >= tex_w ? tex_w - 1 : x0 + i); + int ty = y0 + j < 0 ? 0 : (y0 + j >= tex_h ? tex_h - 1 : y0 + j); + float w = (i ? fx : 1 - fx) * (j ? fy : 1 - fy); + const unsigned char *p = tex + ((size_t)ty * (size_t)tex_w + (size_t)tx) * 4; + for (k = 0; k < 4; ++k) out[k] += w * (float)p[k] / 255.0f; + } +} + +/* Edges in 1/256 pixel fixed point, so two triangles sharing an edge agree on + * every pixel; one on the edge goes to the triangle whose top or left edge it + * is, and nothing blends twice or not at all. */ +struct fixed { long long x, y; }; + +static struct fixed fix(const struct vertex *v) +{ + struct fixed f; + f.x = (long long)floorf(v->x * 256 + 0.5f); + f.y = (long long)floorf(v->y * 256 + 0.5f); + return f; +} + +static long long edge(struct fixed a, struct fixed b, long long x, long long y) +{ + return (b.x - a.x) * (y - a.y) - (b.y - a.y) * (x - a.x); +} + +static int top_left(struct fixed a, struct fixed b) +{ + return (b.y == a.y && b.x > a.x) || b.y < a.y; +} + +static int covers(long long w, int tl) { return w > 0 || (w == 0 && tl); } + +static void triangle(const struct vertex *v0, const struct vertex *v1, const struct vertex *v2, struct nk_rect clip) +{ + const struct vertex *v[3]; + struct fixed f[3]; + long long area; + int x0, x1, y0, y1, x, y, k, tl[3]; + v[0] = v0; + v[1] = v1; + v[2] = v2; + for (k = 0; k < 3; ++k) f[k] = fix(v[k]); + area = edge(f[0], f[1], f[2].x, f[2].y); + if (area == 0) return; + if (area < 0) { + struct fixed t = f[1]; + f[1] = f[2]; + f[2] = t; + v[1] = v2; + v[2] = v1; + area = -area; + } + x0 = (int)floorf(fminf(v0->x, fminf(v1->x, v2->x))); + x1 = (int)ceilf(fmaxf(v0->x, fmaxf(v1->x, v2->x))); + y0 = (int)floorf(fminf(v0->y, fminf(v1->y, v2->y))); + y1 = (int)ceilf(fmaxf(v0->y, fmaxf(v1->y, v2->y))); + if (x0 < (int)floorf(clip.x)) x0 = (int)floorf(clip.x); + if (y0 < (int)floorf(clip.y)) y0 = (int)floorf(clip.y); + if (x1 > (int)ceilf(clip.x + clip.w)) x1 = (int)ceilf(clip.x + clip.w); + if (y1 > (int)ceilf(clip.y + clip.h)) y1 = (int)ceilf(clip.y + clip.h); + if (x0 < 0) x0 = 0; + if (y0 < 0) y0 = 0; + if (x1 > fb_w) x1 = fb_w; + if (y1 > fb_h) y1 = fb_h; + tl[0] = top_left(f[1], f[2]); + tl[1] = top_left(f[2], f[0]); + tl[2] = top_left(f[0], f[1]); + for (y = y0; y < y1; ++y) + for (x = x0; x < x1; ++x) { + long long px = (long long)x * 256 + 128, py = (long long)y * 256 + 128, e[3]; + float w, t[4], s[4], u = 0, vv = 0; + unsigned char *d; + e[0] = edge(f[1], f[2], px, py); + e[1] = edge(f[2], f[0], px, py); + e[2] = edge(f[0], f[1], px, py); + if (!covers(e[0], tl[0]) || !covers(e[1], tl[1]) || !covers(e[2], tl[2])) continue; + s[0] = s[1] = s[2] = s[3] = 0; + for (k = 0; k < 3; ++k) { + int c; + w = (float)e[k] / (float)area; + u += w * v[k]->u; + vv += w * v[k]->v; + for (c = 0; c < 4; ++c) s[c] += w * (float)v[k]->c[c] / 255.0f; + } + sample(u, vv, t); + for (k = 0; k < 4; ++k) s[k] *= t[k]; + d = fb + ((size_t)y * (size_t)fb_w + (size_t)x) * 4; + for (k = 0; k < 3; ++k) d[k] = (unsigned char)((float)d[k] * (1 - s[3]) + s[k] * 255.0f * s[3] + 0.5f); + d[3] = 255; + } +} + +static void render(struct nk_color clear) +{ + static const struct nk_draw_vertex_layout_element layout[] = { + { NK_VERTEX_POSITION, NK_FORMAT_FLOAT, 0 }, + { NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, 8 }, + { NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, 16 }, + { NK_VERTEX_LAYOUT_END } + }; + struct nk_convert_config cfg; + struct nk_buffer cmds, verts, idx; + const struct nk_draw_command *cmd; + const nk_draw_index *offset; + const struct vertex *vtx; + int i; + for (i = 0; i < fb_w * fb_h; ++i) { + fb[i * 4 + 0] = clear.r; + fb[i * 4 + 1] = clear.g; + fb[i * 4 + 2] = clear.b; + fb[i * 4 + 3] = 255; + } + memset(&cfg, 0, sizeof cfg); + cfg.vertex_layout = layout; + cfg.vertex_size = sizeof(struct vertex); + cfg.vertex_alignment = 4; + cfg.tex_null = null_tex; + cfg.circle_segment_count = 48; + cfg.curve_segment_count = 32; + cfg.arc_segment_count = 32; + cfg.global_alpha = 1; + cfg.shape_AA = NK_ANTI_ALIASING_ON; + cfg.line_AA = NK_ANTI_ALIASING_ON; + nk_buffer_init_default(&cmds); + nk_buffer_init_default(&verts); + nk_buffer_init_default(&idx); + nk_convert(&ctx, &cmds, &verts, &idx, &cfg); + vtx = (const struct vertex *)nk_buffer_memory_const(&verts); + offset = (const nk_draw_index *)nk_buffer_memory_const(&idx); + nk_draw_foreach(cmd, &ctx, &cmds) { + unsigned int e; + for (e = 0; e + 2 < cmd->elem_count; e += 3) + triangle(&vtx[offset[e]], &vtx[offset[e + 1]], &vtx[offset[e + 2]], cmd->clip_rect); + offset += cmd->elem_count; + } + nk_buffer_free(&cmds); + nk_buffer_free(&verts); + nk_buffer_free(&idx); + nk_clear(&ctx); +} + +static void setup(int w, int h) +{ + struct nk_font_atlas atlas; + const void *image; + struct nk_color table[NK_COLOR_COUNT]; + int i; + fb_w = w; + fb_h = h; + fb = (unsigned char *)malloc((size_t)w * (size_t)h * 4); + nk_font_atlas_init_default(&atlas); + nk_font_atlas_begin(&atlas); + font = nk_font_atlas_add_from_file(&atlas, FONTS "Roboto-Regular.ttf", 17, 0); + bold = nk_font_atlas_add_from_file(&atlas, FONTS "Roboto-Bold.ttf", 18, 0); + small = nk_font_atlas_add_from_file(&atlas, FONTS "Roboto-Regular.ttf", 13, 0); + if (!font || !bold || !small) { + fprintf(stderr, "clips: run from the repository's root, the fonts are in " FONTS "\n"); + exit(1); + } + image = nk_font_atlas_bake(&atlas, &tex_w, &tex_h, NK_FONT_ATLAS_RGBA32); + tex = (unsigned char *)malloc((size_t)tex_w * (size_t)tex_h * 4); + memcpy(tex, image, (size_t)tex_w * (size_t)tex_h * 4); + nk_font_atlas_end(&atlas, nk_handle_id(0), &null_tex); + nk_init_default(&ctx, &font->handle); + for (i = 0; i < NK_COLOR_COUNT; ++i) table[i] = panel; + table[NK_COLOR_TEXT] = ink; + table[NK_COLOR_WINDOW] = ground; + table[NK_COLOR_BORDER] = line; + table[NK_COLOR_BUTTON_HOVER] = line; + table[NK_COLOR_BUTTON_ACTIVE] = line; + nk_style_from_table(&ctx, table); + ctx.style.window.padding = nk_vec2(0, 0); + ctx.style.window.spacing = nk_vec2(0, 0); + anim = nka_create(0); +} + +static struct nk_command_buffer *canvas(void) +{ + nk_begin(&ctx, "clip", nk_rect(0, 0, (float)fb_w, (float)fb_h), NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_BACKGROUND); + return nk_window_get_canvas(&ctx); +} + +static void text(struct nk_command_buffer *out, const struct nk_font *f, float x, float y, const char *s, + struct nk_color c) +{ + nk_draw_text(out, nk_rect(x, y, 400, f->handle.height + 4), s, (int)strlen(s), &f->handle, + nk_rgba(0, 0, 0, 0), c); +} + +static float text_width(const struct nk_font *f, const char *s) +{ + return f->handle.width(f->handle.userdata, f->handle.height, s, (int)strlen(s)); +} + +/* Scenes take the frame's time and draw into one window that fills the clip. */ + +static void easing(float time) +{ + static const char *names[] = { "out cubic", "in out sine", "out back", "out elastic", "out bounce", + "spring", "steps", "bezier" }; + struct nka_ease eases[8]; + struct nk_command_buffer *out = canvas(); + float target = fmodf(time, 4.4f) < 2.2f ? 1.0f : 0.0f; + int i; + eases[0] = nka_ease(NKA_EASE_OUT_CUBIC); + eases[1] = nka_ease(NKA_EASE_IN_OUT_SINE); + eases[2] = nka_ease(NKA_EASE_OUT_BACK); + eases[3] = nka_ease(NKA_EASE_OUT_ELASTIC); + eases[4] = nka_ease(NKA_EASE_OUT_BOUNCE); + eases[5] = nka_ease_spring(1, 180, 18, 0); + eases[6] = nka_ease_steps(6, 0); + eases[7] = nka_ease_bezier(0.68f, -0.55f, 0.27f, 1.55f); + for (i = 0; i < 8; ++i) { + float y = 26 + (float)i * 33, x0 = 170, x1 = (float)fb_w - 80; + float k = nka_tween_float(anim, nka_id(names[i]), 0, target, 1.4f, eases[i], NKA_POLICY_CROSSFADE, 0); + float x = x0 + (x1 - x0) * k; + text(out, font, 28, y - 10, names[i], dim); + nk_stroke_line(out, x0, y, x1, y, 2, line); + nk_fill_circle(out, nk_rect(x - 8, y - 8, 16, 16), i & 1 ? copper : cyan); + } + nk_end(&ctx); +} + +static void hover(float time) +{ + static const char *labels[] = { "Home", "Search", "Library", "Profile" }; + static float size[4] = { 1, 1, 1, 1 }; + const float pi = 3.14159265f; + struct nk_command_buffer *out = canvas(); + struct nk_vec2 mouse; + float sweep = fmodf(time, 4.0f) / 4.0f; + int i; + mouse.x = 40 + ((float)fb_w - 80) * (0.5f - 0.5f * cosf(sweep * 2 * pi)); + mouse.y = (float)fb_h * 0.5f + 34 * sinf(sweep * 4 * pi); + for (i = 0; i < 4; ++i) { + float w = 120, h = 64, cx = (float)fb_w * ((float)i + 0.5f) / 4, cy = (float)fb_h * 0.5f; + struct nk_rect r = nk_rect(cx - w / 2, cy - h / 2, w, h); + int over = mouse.x >= r.x && mouse.x <= r.x + r.w && mouse.y >= r.y && mouse.y <= r.y + r.h; + struct nk_colorf fill; + if (over) nka_animate(anim, 1, 1.12f, 180, &size[i], NKA_EASE_OUT_CUBIC); + else nka_animate(anim, 1.12f, 1, 260, &size[i], NKA_EASE_OUT_CUBIC); + fill = nka_tween_color(anim, nka_id(labels[i]), 1, nk_color_cf(over ? cyan : panel), 0.25f, + nka_ease(NKA_EASE_OUT_CUBIC), NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, nk_color_cf(panel)); + w *= size[i]; + h *= size[i]; + nk_fill_rect(out, nk_rect(cx - w / 2, cy - h / 2, w, h), 12 * size[i], nk_rgb_cf(fill)); + text(out, bold, cx - text_width(bold, labels[i]) / 2, cy - 11, labels[i], over ? ground : ink); + } + text(out, small, 24, (float)fb_h - 30, "nka_animate(a, 1, 1.12f, 180, &size, NKA_EASE_OUT_CUBIC)", dim); + nk_fill_circle(out, nk_rect(mouse.x - 6, mouse.y - 6, 12, 12), ink); + nk_end(&ctx); +} + +static struct nk_colorf rgb(float r, float g, float b) +{ + struct nk_colorf c; + c.r = r; c.g = g; c.b = b; c.a = 1; + return c; +} + +static void colors(float time) +{ + static const char *names[] = { "sRGB", "linear", "HSV", "OKLAB", "OKLCH" }; + static const int spaces[] = { NKA_COL_SRGB, NKA_COL_SRGB_LINEAR, NKA_COL_HSV, NKA_COL_OKLAB, NKA_COL_OKLCH }; + struct nk_colorf from = rgb(0.12f, 0.42f, 1.0f), to = rgb(1.0f, 0.82f, 0.12f); + struct nk_command_buffer *out = canvas(); + float k = nka_tween_float(anim, nka_id("blend"), 0, fmodf(time, 4.8f) < 2.4f ? 1.0f : 0.0f, 1.6f, + nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_POLICY_CROSSFADE, 0); + int i, s; + for (i = 0; i < 5; ++i) { + float y = 22 + (float)i * 46, x0 = 110, w = 400; + text(out, font, 24, y + 4, names[i], dim); + for (s = 0; s < 100; ++s) + nk_fill_rect(out, nk_rect(x0 + w * (float)s / 100, y, w / 100 + 1, 28), 0, + nk_rgb_cf(nka_color_blend(from, to, (float)s / 99, spaces[i]))); + nk_stroke_circle(out, nk_rect(x0 + w * k - 7, y + 7, 14, 14), 2, ink); + nk_fill_rect(out, nk_rect(x0 + w + 30, y - 2, 64, 32), 8, nk_rgb_cf(nka_color_blend(from, to, k, spaces[i]))); + } + nk_end(&ctx); +} + +static void paths(float time) +{ + nk_hash loop = nka_id("loop"), wave = nka_id("wave"); + struct nka_text_path_opts opts = nka_text_path_opts_default(); + struct nk_command_buffer *out = canvas(); + struct nk_vec2 p, prev; + float angle, progress; + int i, lap = (int)(time / 3.0f); + nka_path_begin(anim, loop, nk_vec2(40, 230)); + nka_path_cubic_to(anim, nk_vec2(60, 40), nk_vec2(250, 30), nk_vec2(270, 130)); + nka_path_catmull_to(anim, nk_vec2(200, 200), 0.5f); + nka_path_catmull_to(anim, nk_vec2(300, 250), 0.5f); + nka_path_end(anim); + if (!nka_path_has_arc_lut(anim, loop)) nka_path_build_arc_lut(anim, loop, 128); + nka_path_begin(anim, wave, nk_vec2(340, 170)); + nka_path_cubic_to(anim, nk_vec2(420, 60), nk_vec2(500, 280), nk_vec2(620, 150)); + nka_path_end(anim); + for (i = 0, prev = nka_path_evaluate(anim, loop, 0); i <= 120; ++i, prev = p) { + p = nka_path_evaluate(anim, loop, (float)i / 120); + if (i) nk_stroke_line(out, prev.x, prev.y, p.x, p.y, 2, line); + } + for (i = 0, prev = nka_path_evaluate(anim, wave, 0); i <= 120; ++i, prev = p) { + p = nka_path_evaluate(anim, wave, (float)i / 120); + if (i) nk_stroke_line(out, prev.x, prev.y, p.x, p.y, 1, line); + } + p = nka_tween_path(anim, 1, (nk_hash)lap, loop, 2.4f, nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_POLICY_CROSSFADE); + angle = nka_tween_path_angle(anim, 2, (nk_hash)lap, loop, 2.4f, nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_POLICY_CROSSFADE); + nk_stroke_line(out, p.x, p.y, p.x + 22 * cosf(angle), p.y + 22 * sinf(angle), 3, copper); + nk_fill_circle(out, nk_rect(p.x - 8, p.y - 8, 16, 16), cyan); + progress = fmodf(time, 3.0f) / 2.2f; + opts.color = ink; + opts.align = NKA_TEXT_ALIGN_CENTER; + opts.font = &bold->handle; + nka_text_path_animated(anim, &ctx, wave, "text follows paths too", progress, &opts); + nk_end(&ctx); +} + +static void stagger(float time) +{ + static const char *names[] = { "fade", "slide up", "bounce", "wave", "typewriter", "scale" }; + static const int effects[] = { NKA_TEXT_FX_FADE, NKA_TEXT_FX_SLIDE_UP, NKA_TEXT_FX_BOUNCE, + NKA_TEXT_FX_WAVE, NKA_TEXT_FX_TYPEWRITER, NKA_TEXT_FX_SCALE }; + struct nka_text_stagger_opts opts = nka_text_stagger_opts_default(); + struct nk_command_buffer *out = canvas(); + const char *line_text = "Text arrives glyph by glyph"; + float progress = fmodf(time, 4.5f) / 3.0f; + int i; + opts.char_delay = 0.035f; + opts.char_duration = 0.45f; + opts.effect_intensity = 14; + opts.color = ink; + opts.font = &bold->handle; + for (i = 0; i < 6; ++i) { + float y = 24 + (float)i * 42; + text(out, small, 24, y + 4, names[i], dim); + opts.effect = effects[i]; + opts.pos = nk_vec2(130, y); + nka_text_stagger(anim, &ctx, line_text, effects[i] == NKA_TEXT_FX_WAVE ? fmodf(time, 4.5f) / 4.5f + : (progress < 1 ? progress : 1), &opts); + } + nk_end(&ctx); +} + +static void styles(float time) +{ + static int registered, check = 1, option = 1; + static float slider = 0.6f; + static nk_size progress = 64; + nk_hash dark = nka_id("dark"), light = nka_id("light"); + if (!registered) { + struct nk_color table[NK_COLOR_COUNT]; + int i; + nka_style_register_current(anim, &ctx, dark); + for (i = 0; i < NK_COLOR_COUNT; ++i) table[i] = nk_rgb(236, 238, 242); + table[NK_COLOR_TEXT] = nk_rgb(32, 36, 44); + table[NK_COLOR_WINDOW] = nk_rgb(250, 250, 252); + table[NK_COLOR_BORDER] = nk_rgb(214, 218, 226); + table[NK_COLOR_BUTTON] = nk_rgb(226, 230, 238); + table[NK_COLOR_BUTTON_HOVER] = nk_rgb(214, 218, 228); + table[NK_COLOR_SLIDER] = nk_rgb(214, 218, 226); + table[NK_COLOR_SLIDER_CURSOR] = copper; + table[NK_COLOR_TOGGLE_CURSOR] = copper; + table[NK_COLOR_SELECT_ACTIVE] = copper; + table[NK_COLOR_PROPERTY] = nk_rgb(226, 230, 238); + nka_style_register_table(anim, &ctx, light, table); + ctx.style.slider.cursor_normal = nk_style_item_color(cyan); + ctx.style.checkbox.cursor_normal = nk_style_item_color(cyan); + ctx.style.option.cursor_normal = nk_style_item_color(cyan); + nka_style_register_current(anim, &ctx, dark); + registered = 1; + } + nka_style_tween(anim, &ctx, 1, fmodf(time, 5.0f) < 2.5f ? light : dark, 0.9f, + nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_COL_OKLAB); + ctx.style.window.padding = nk_vec2(28, 22); + ctx.style.window.spacing = nk_vec2(12, 12); + if (nk_begin(&ctx, "style", nk_rect(0, 0, (float)fb_w, (float)fb_h), NK_WINDOW_NO_SCROLLBAR)) { + nk_layout_row_dynamic(&ctx, 26, 1); + nk_label(&ctx, "nka_style_tween blends whole Nuklear styles", NK_TEXT_LEFT); + nk_layout_row_dynamic(&ctx, 28, 2); + nk_checkbox_label(&ctx, "Animate", &check); + nk_slider_float(&ctx, 0, &slider, 1, 0.01f); + nk_option_label(&ctx, "Color space: OKLAB", option); + nk_progress(&ctx, &progress, 100, NK_FIXED); + nk_layout_row_dynamic(&ctx, 36, 3); + nk_button_label(&ctx, "Cancel"); + nk_button_label(&ctx, "Apply"); + nk_button_label(&ctx, "OK"); + } + nk_end(&ctx); +} + +static struct nk_rect cell(struct nk_command_buffer *out, int i, const char *name) +{ + float w = ((float)fb_w - 16 * 4) / 3, h = ((float)fb_h - 16 * 3) / 2; + struct nk_rect r = nk_rect(16 + (float)(i % 3) * (w + 16), 16 + (float)(i / 3) * (h + 16), w, h); + nk_fill_rect(out, r, 10, panel); + text(out, small, r.x + 12, r.y + 8, name, dim); + return r; +} + +static void procedural(float time) +{ + static struct nk_vec2 trail[24]; + static int trail_n; + const float pi = 3.14159265f; + struct nk_command_buffer *out = canvas(); + struct nk_rect r; + struct nk_vec2 c, v; + struct nka_gradient g; + struct nka_transform x; + int i; + + r = cell(out, 0, "oscillate"); + c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8); + for (i = 0; i < 5; ++i) { + float y = nka_oscillate(anim, nka_id_mix(nka_id("osc"), (nk_hash)i), 26, 0.7f, NKA_WAVE_SINE, (float)i * 0.12f); + nk_fill_circle(out, nk_rect(c.x - 52 + (float)i * 26 - 7, c.y + y - 7, 14, 14), cyan); + } + + r = cell(out, 1, "wiggle"); + c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8); + v = nka_wiggle_vec2(anim, nka_id("wiggle"), nk_vec2(42, 28), 1.4f); + nk_fill_circle(out, nk_rect(c.x + v.x - 9, c.y + v.y - 9, 18, 18), copper); + + r = cell(out, 2, "shake"); + c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8); + if (fmodf(time, 1.5f) < DT) nka_trigger_shake(anim, nka_id("shake")); + v = nka_shake_vec2(anim, nka_id("shake"), nk_vec2(12, 5), 28, 0.55f); + nk_fill_rect(out, nk_rect(c.x - 44 + v.x, c.y - 18 + v.y, 88, 36), 8, line); + text(out, small, c.x - 20 + v.x, c.y - 9 + v.y, "wrong", ink); + + r = cell(out, 3, "noise"); + c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8); + v = nka_noise_channel_vec2(anim, nka_id("noise"), nk_vec2(0.9f, 0.7f), nk_vec2(170, 100), 0); + trail[trail_n++ % 24] = nk_vec2(c.x + v.x, c.y + v.y); + for (i = (trail_n < 24 ? trail_n : 24) - 1; i >= 0; --i) { + struct nk_vec2 p = trail[(trail_n - 1 - i) % 24]; + struct nk_color k = cyan; + k.a = (nk_byte)(255 - i * 10); + nk_fill_circle(out, nk_rect(p.x - 6 + (float)i * 0.15f, p.y - 6 + (float)i * 0.15f, + 12 - (float)i * 0.3f, 12 - (float)i * 0.3f), k); + } + + r = cell(out, 4, "gradient"); + { + struct nka_gradient a = nka_gradient_three_color(nk_color_cf(cyan), rgb(0.95f, 0.95f, 0.98f), nk_color_cf(copper)); + struct nka_gradient b = nka_gradient_two_color(rgb(0.36f, 0.20f, 0.62f), rgb(1.0f, 0.78f, 0.25f)); + g = nka_tween_gradient(anim, nka_id("gradient"), 0, fmodf(time, 4.0f) < 2.0f ? &a : &b, 1.2f, + nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_POLICY_CROSSFADE, NKA_COL_OKLAB); + for (i = 0; i < 64; ++i) + nk_fill_rect(out, nk_rect(r.x + 16 + (r.w - 32) * (float)i / 64, r.y + 44, (r.w - 32) / 64 + 1, 56), 0, + nk_rgb_cf(nka_gradient_sample(&g, (float)i / 63, NKA_COL_OKLAB))); + } + + r = cell(out, 5, "transform"); + c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8); + x = nka_tween_transform(anim, nka_id("square"), 0, + fmodf(time, 3.0f) < 1.5f ? nka_transform(c, 0, nk_vec2(1, 1)) + : nka_transform(c, pi / 2, nk_vec2(1.5f, 0.8f)), + 1.1f, nka_ease(NKA_EASE_OUT_BACK), NKA_POLICY_CROSSFADE, NKA_ROTATION_CW); + { + float pts[8]; + static const float corner[4][2] = { { -22, -22 }, { 22, -22 }, { 22, 22 }, { -22, 22 } }; + for (i = 0; i < 4; ++i) { + struct nk_vec2 p = nka_transform_apply(x, nk_vec2(corner[i][0], corner[i][1])); + pts[i * 2] = p.x; + pts[i * 2 + 1] = p.y; + } + nk_fill_polygon(out, pts, 4, cyan); + } + nk_end(&ctx); +} + +static void timeline(float time) +{ + static int made; + static float started = -1; + nk_hash clip = nka_id("tile"), scale = nka_id("scale"), alpha = nka_id("alpha"), lift = nka_id("lift"); + struct nka_stagger_grid_opts grid = nka_stagger_grid_opts_default(); + struct nk_command_buffer *out; + int i; + if (!made) { + nka_clip_begin(anim, clip); + nka_clip_key_float(anim, scale, 0, 0.4f, NKA_EASE_OUT_BACK, 0); + nka_clip_key_float(anim, scale, 0.6f, 1, NKA_EASE_LINEAR, 0); + nka_clip_key_float(anim, alpha, 0, 0, NKA_EASE_OUT_CUBIC, 0); + nka_clip_key_float(anim, alpha, 0.35f, 1, NKA_EASE_LINEAR, 0); + nka_clip_key_float(anim, lift, 0, 26, NKA_EASE_OUT_CUBIC, 0); + nka_clip_key_float(anim, lift, 0.5f, 0, NKA_EASE_LINEAR, 0); + nka_clip_end(anim); + made = 1; + } + grid.cols = 6; + grid.rows = 2; + grid.from = NKA_STAGGER_CENTER; + grid.delay = 0.09f; + if (started < 0 || time - started >= 3.0f) { + for (i = 0; i < 12; ++i) nka_play_with_delay(anim, clip, (nk_hash)(100 + i), nka_stagger_grid_delay_index(anim, i, &grid)); + started = time; + } + nk_style_push_vec2(&ctx, &ctx.style.window.padding, nk_vec2(16, 12)); + nk_begin(&ctx, "clip", nk_rect(0, 0, (float)fb_w, (float)fb_h), NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_BACKGROUND); + out = nk_window_get_canvas(&ctx); + for (i = 0; i < 12; ++i) { + float s = 1, k = 1, y = 0, cx = 16 + 52 + (float)(i % 6) * 101, cy = 52 + (float)(i / 6) * 80; + struct nk_color c = i % 2 ? copper : cyan; + nka_instance_get_float(anim, (nk_hash)(100 + i), scale, &s); + nka_instance_get_float(anim, (nk_hash)(100 + i), alpha, &k); + nka_instance_get_float(anim, (nk_hash)(100 + i), lift, &y); + c.a = (nk_byte)(255 * (k < 0 ? 0 : (k > 1 ? 1 : k))); + nk_fill_rect(out, nk_rect(cx - 44 * s, cy + y - 30 * s, 88 * s, 60 * s), 10 * s, c); + } + nk_layout_row_dynamic(&ctx, 150, 1); + nk_spacer(&ctx); + nka_show_debug_timeline(anim, &ctx, 100 + 2); + nk_end(&ctx); + nk_style_pop_vec2(&ctx); +} + +struct scene { const char *name; int w, h; float seconds; void (*draw)(float time); }; + +static const struct scene scenes[] = { + { "easing", 640, 290, 4.4f, easing }, + { "hover", 640, 220, 4.0f, hover }, + { "colors", 640, 250, 4.8f, colors }, + { "paths", 640, 290, 3.0f, paths }, + { "stagger", 640, 280, 4.5f, stagger }, + { "styles", 640, 230, 5.0f, styles }, + { "procedural", 640, 300, 6.0f, procedural }, + { "timeline", 640, 330, 3.0f, timeline }, +}; + +int main(int argc, char **argv) +{ + const struct scene *s = 0; + unsigned int header[4]; + FILE *f; + int i, frames; + for (i = 0; argc == 3 && i < (int)(sizeof scenes / sizeof *scenes); ++i) + if (!strcmp(argv[1], scenes[i].name)) s = &scenes[i]; + if (!s) { + fprintf(stderr, "usage: clips ; scenes:"); + for (i = 0; i < (int)(sizeof scenes / sizeof *scenes); ++i) fprintf(stderr, " %s", scenes[i].name); + fprintf(stderr, "\n"); + return 2; + } + setup(s->w, s->h); + f = fopen(argv[2], "wb"); + if (!f) return 1; + header[0] = 0x464b414e; /* NAKF */ + header[1] = (unsigned int)s->w; + header[2] = (unsigned int)s->h; + header[3] = (unsigned int)(DT * 1000 + 0.5f); + fwrite(header, sizeof header, 1, f); + frames = (int)(s->seconds / DT + 0.5f); + for (i = 0; i < frames; ++i) { + nka_update(anim, i ? DT : 0); + nk_input_begin(&ctx); + nk_input_end(&ctx); + s->draw((float)i * DT); + render(ground); + fwrite(fb, (size_t)fb_w * (size_t)fb_h * 4, 1, f); + } + fclose(f); + nka_destroy(anim); + nk_free(&ctx); + free(fb); + free(tex); + return 0; +} diff --git a/examples/nk.h b/examples/nk.h new file mode 100644 index 0000000..296e234 --- /dev/null +++ b/examples/nk.h @@ -0,0 +1,11 @@ +#ifndef EXAMPLES_NK_H +#define EXAMPLES_NK_H + +#define NK_INCLUDE_FIXED_TYPES +#define NK_INCLUDE_STANDARD_IO +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT +#define NK_INCLUDE_FONT_BAKING +#include "nuklear.h" + +#endif diff --git a/examples/nuklear.c b/examples/nuklear.c new file mode 100644 index 0000000..1eddd76 --- /dev/null +++ b/examples/nuklear.c @@ -0,0 +1,7 @@ +/* Nuklear's implementation on its own, so its warnings stay out of the examples. */ +#define _CRT_SECURE_NO_WARNINGS +#ifdef _MSC_VER +#pragma warning(disable: 4116 4127 4701 5287) +#endif +#define NK_IMPLEMENTATION +#include "nk.h" diff --git a/external/nuklear b/external/nuklear new file mode 160000 index 0000000..3a46e38 --- /dev/null +++ b/external/nuklear @@ -0,0 +1 @@ +Subproject commit 3a46e383c67740a444c6a049f35d87afa7d58b00 diff --git a/nukanim.h b/nukanim.h new file mode 100644 index 0000000..aebccaa --- /dev/null +++ b/nukanim.h @@ -0,0 +1,7019 @@ +/* nukanim.h - animation for Nuklear. v0.1.0, MIT, see the end of the file. + * + * A C99 port of ImAnim by Soufiane KHIAT, with ImAnimate's nka_animate() by + * K. Bieniek (Raidcore.GG). Include nuklear.h first; in one C file, also + * + * #define NUKANIM_IMPLEMENTATION + * #include "nukanim.h" + * + * The math comes from . To use your own float functions instead, + * name them with NKA_MATH: #define NKA_MATH(fn) my_##fn uses my_sin, my_pow... + */ +#ifndef NUKANIM_H +#define NUKANIM_H + +#define NKA_VERSION "0.1.0" + +#ifndef NKA_API +#ifdef NKA_PRIVATE +#define NKA_API static inline +#else +#define NKA_API extern +#endif +#endif + +enum nka_ease_type { + NKA_EASE_LINEAR, + NKA_EASE_IN_QUAD, NKA_EASE_OUT_QUAD, NKA_EASE_IN_OUT_QUAD, + NKA_EASE_IN_CUBIC, NKA_EASE_OUT_CUBIC, NKA_EASE_IN_OUT_CUBIC, + NKA_EASE_IN_QUART, NKA_EASE_OUT_QUART, NKA_EASE_IN_OUT_QUART, + NKA_EASE_IN_QUINT, NKA_EASE_OUT_QUINT, NKA_EASE_IN_OUT_QUINT, + NKA_EASE_IN_SINE, NKA_EASE_OUT_SINE, NKA_EASE_IN_OUT_SINE, + NKA_EASE_IN_EXPO, NKA_EASE_OUT_EXPO, NKA_EASE_IN_OUT_EXPO, + NKA_EASE_IN_CIRC, NKA_EASE_OUT_CIRC, NKA_EASE_IN_OUT_CIRC, + NKA_EASE_IN_BACK, NKA_EASE_OUT_BACK, NKA_EASE_IN_OUT_BACK, /* p0 overshoot */ + NKA_EASE_IN_ELASTIC, NKA_EASE_OUT_ELASTIC, NKA_EASE_IN_OUT_ELASTIC, /* p0 amplitude, p1 period */ + NKA_EASE_IN_BOUNCE, NKA_EASE_OUT_BOUNCE, NKA_EASE_IN_OUT_BOUNCE, + NKA_EASE_STEPS, /* p0 steps; p1 jumps at 0 the end, 1 the start, 2 both */ + NKA_EASE_CUBIC_BEZIER, /* p0 x1, p1 y1, p2 x2, p3 y2 */ + NKA_EASE_SPRING, /* p0 mass, p1 stiffness, p2 damping, p3 velocity */ + NKA_EASE_CUSTOM, /* p0 slot given to nka_register_ease */ + NKA_EASE_COUNT +}; + +enum nka_policy { + NKA_POLICY_CROSSFADE, /* ease from where it is to the new target */ + NKA_POLICY_CUT, /* jump to the new target */ + NKA_POLICY_QUEUE, /* finish, then head for the latest target */ + NKA_POLICY_ADDITIVE, /* a new target is added to the value */ + NKA_POLICY_MULTIPLY /* a new target multiplies the value */ +}; + +enum nka_color_space { + NKA_COL_SRGB, NKA_COL_SRGB_LINEAR, NKA_COL_HSV, NKA_COL_OKLAB, NKA_COL_OKLCH +}; + +enum nka_anchor_space { + NKA_ANCHOR_WINDOW_CONTENT, NKA_ANCHOR_WINDOW, NKA_ANCHOR_VIEWPORT, + NKA_ANCHOR_LAST_ITEM, NKA_ANCHOR_COUNT +}; + +struct nka_context; +struct nka_vec4 { float x, y, z, w; }; +struct nka_ease { int type; float p0, p1, p2, p3; }; +struct nka_ease_axes { struct nka_ease x, y, z, w; }; + +typedef float (*nka_ease_fn)(float t); +typedef float (*nka_float_resolver)(void *user); +typedef struct nk_vec2 (*nka_vec2_resolver)(void *user); +typedef struct nka_vec4 (*nka_vec4_resolver)(void *user); +typedef struct nk_colorf (*nka_color_resolver)(void *user); +typedef int (*nka_int_resolver)(void *user); + +/* A NULL allocator means malloc and free, under NK_INCLUDE_DEFAULT_ALLOCATOR. */ +NKA_API struct nka_context *nka_create(const struct nk_allocator *alloc); +NKA_API void nka_destroy(struct nka_context *a); + +/* Once a frame, before anything animates: advances time by dt seconds. */ +NKA_API void nka_update(struct nka_context *a, float dt); +/* Nonzero when something sampled this frame is still moving. */ +NKA_API int nka_busy(const struct nka_context *a); +NKA_API double nka_time(const struct nka_context *a); +/* Forgets whatever was not sampled in the last max_age frames. */ +NKA_API void nka_gc(struct nka_context *a, unsigned max_age); +NKA_API void nka_clear(struct nka_context *a); +NKA_API void nka_reserve(struct nka_context *a, int tweens); +NKA_API void nka_set_time_scale(struct nka_context *a, float scale); +NKA_API float nka_time_scale(const struct nka_context *a); +/* On by default: a tween asked for its init value is not stored. */ +NKA_API void nka_set_lazy_init(struct nka_context *a, int on); +NKA_API int nka_lazy_init(const struct nka_context *a); +/* The sizes that relative targets are fractions of. */ +NKA_API void nka_set_anchor(struct nka_context *a, int space, struct nk_vec2 size); +NKA_API struct nk_vec2 nka_anchor(const struct nka_context *a, int space); + +NKA_API nk_hash nka_id(const char *name); +NKA_API nk_hash nka_id_mix(nk_hash a, nk_hash b); + +NKA_API struct nka_ease nka_ease(int type); +NKA_API struct nka_ease nka_ease_bezier(float x1, float y1, float x2, float y2); +NKA_API struct nka_ease nka_ease_steps(int steps, int mode); +NKA_API struct nka_ease nka_ease_back(float overshoot); +NKA_API struct nka_ease nka_ease_elastic(float amplitude, float period); +NKA_API struct nka_ease nka_ease_spring(float mass, float stiffness, float damping, float velocity); +NKA_API struct nka_ease nka_ease_custom(int slot); +/* Slots 0 to 15. */ +NKA_API void nka_register_ease(struct nka_context *a, int slot, nka_ease_fn fn); +NKA_API nka_ease_fn nka_custom_ease(const struct nka_context *a, int slot); +NKA_API float nka_eval_preset(int type, float t); +NKA_API float nka_eval(const struct nka_context *a, struct nka_ease ease, float t); + +/* ImAnimate: moves *value from `from` toward `to` over ms milliseconds, + * from wherever *value stands. The pointer is the key. */ +NKA_API void nka_animate(struct nka_context *a, float from, float to, float ms, float *value, int curve); + +/* A tween is keyed by id and channel. init is where it starts. */ +NKA_API float nka_tween_float(struct nka_context *a, nk_hash id, nk_hash ch, float target, float dur, struct nka_ease ease, int policy, float init); +NKA_API struct nk_vec2 nka_tween_vec2(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_vec2 target, float dur, struct nka_ease ease, int policy, struct nk_vec2 init); +NKA_API struct nka_vec4 nka_tween_vec4(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_vec4 target, float dur, struct nka_ease ease, int policy, struct nka_vec4 init); +NKA_API int nka_tween_int(struct nka_context *a, nk_hash id, nk_hash ch, int target, float dur, struct nka_ease ease, int policy, int init); +NKA_API struct nk_colorf nka_tween_color(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_colorf target, float dur, struct nka_ease ease, int policy, int space, struct nk_colorf init); + +NKA_API struct nk_vec2 nka_tween_vec2_per_axis(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_vec2 target, float dur, struct nka_ease_axes ease, int policy); +NKA_API struct nka_vec4 nka_tween_vec4_per_axis(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_vec4 target, float dur, struct nka_ease_axes ease, int policy); +NKA_API struct nk_colorf nka_tween_color_per_axis(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_colorf target, float dur, struct nka_ease_axes ease, int policy, int space); + +/* Targets that are a fraction of an anchor plus pixels. Axis 0 is x, 1 is y; + * a vec4 takes z and w as they are. */ +NKA_API float nka_tween_float_rel(struct nka_context *a, nk_hash id, nk_hash ch, float percent, float px_bias, float dur, struct nka_ease ease, int policy, int anchor, int axis); +NKA_API struct nk_vec2 nka_tween_vec2_rel(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_vec2 percent, struct nk_vec2 px_bias, float dur, struct nka_ease ease, int policy, int anchor); +NKA_API struct nka_vec4 nka_tween_vec4_rel(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_vec4 percent, struct nka_vec4 px_bias, float dur, struct nka_ease ease, int policy, int anchor); + +/* Targets that a callback works out on every call. */ +NKA_API float nka_tween_float_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_float_resolver fn, void *user, float dur, struct nka_ease ease, int policy); +NKA_API struct nk_vec2 nka_tween_vec2_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_vec2_resolver fn, void *user, float dur, struct nka_ease ease, int policy); +NKA_API struct nka_vec4 nka_tween_vec4_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_vec4_resolver fn, void *user, float dur, struct nka_ease ease, int policy); +NKA_API struct nk_colorf nka_tween_color_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_color_resolver fn, void *user, float dur, struct nka_ease ease, int policy, int space); +NKA_API int nka_tween_int_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_int_resolver fn, void *user, float dur, struct nka_ease ease, int policy); + +/* Points a running tween somewhere else in the time it has left. */ +NKA_API void nka_rebase_float(struct nka_context *a, nk_hash id, nk_hash ch, float target); +NKA_API void nka_rebase_vec2(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_vec2 target); +NKA_API void nka_rebase_vec4(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_vec4 target); +NKA_API void nka_rebase_color(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_colorf target); +NKA_API void nka_rebase_int(struct nka_context *a, nk_hash id, nk_hash ch, int target); + +NKA_API struct nk_colorf nka_color_blend(struct nk_colorf a, struct nk_colorf b, float t, int space); + +/* Motion paths */ + +enum nka_path_segment_type { + NKA_SEG_LINE, NKA_SEG_QUADRATIC_BEZIER, NKA_SEG_CUBIC_BEZIER, NKA_SEG_CATMULL_ROM +}; + +/* The defaults are 64 samples and both flags on. Both ends always match, so + * match_endpoints changes nothing, as in ImAnim. */ +struct nka_morph_opts { int samples, match_endpoints, use_arc_length; }; + +NKA_API struct nk_vec2 nka_bezier_quadratic(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, float t); +NKA_API struct nk_vec2 nka_bezier_cubic(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, struct nk_vec2 p3, float t); +/* Through p1 at t 0 and p2 at t 1. Tension 0 is Catmull-Rom proper; ImAnim's default is 0.5. */ +NKA_API struct nk_vec2 nka_catmull_rom(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, struct nk_vec2 p3, float t, float tension); +NKA_API struct nk_vec2 nka_bezier_quadratic_deriv(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, float t); +NKA_API struct nk_vec2 nka_bezier_cubic_deriv(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, struct nk_vec2 p3, float t); +NKA_API struct nk_vec2 nka_catmull_rom_deriv(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, struct nk_vec2 p3, float t, float tension); + +/* nka_path_end puts the path in place of any other with its id. */ +NKA_API void nka_path_begin(struct nka_context *a, nk_hash path_id, struct nk_vec2 start); +NKA_API void nka_path_line_to(struct nka_context *a, struct nk_vec2 end); +NKA_API void nka_path_quadratic_to(struct nka_context *a, struct nk_vec2 ctrl, struct nk_vec2 end); +NKA_API void nka_path_cubic_to(struct nka_context *a, struct nk_vec2 ctrl1, struct nk_vec2 ctrl2, struct nk_vec2 end); +NKA_API void nka_path_catmull_to(struct nka_context *a, struct nk_vec2 end, float tension); +/* A line back to the start, unless the path is there already. */ +NKA_API void nka_path_close(struct nka_context *a); +NKA_API void nka_path_end(struct nka_context *a); + +NKA_API int nka_path_exists(const struct nka_context *a, nk_hash path_id); +/* Approximate: the arc-length table's measure once there is one. */ +NKA_API float nka_path_length(const struct nka_context *a, nk_hash path_id); +NKA_API struct nk_vec2 nka_path_evaluate(const struct nka_context *a, nk_hash path_id, float t); +NKA_API struct nk_vec2 nka_path_tangent(const struct nka_context *a, nk_hash path_id, float t); +NKA_API float nka_path_angle(const struct nka_context *a, nk_hash path_id, float t); + +/* From the start of the path to its end once, at constant speed when the path has an + * arc-length table. As in ImAnim, policy changes nothing. */ +NKA_API struct nk_vec2 nka_tween_path(struct nka_context *a, nk_hash id, nk_hash ch, nk_hash path_id, float dur, struct nka_ease ease, int policy); +NKA_API float nka_tween_path_angle(struct nka_context *a, nk_hash id, nk_hash ch, nk_hash path_id, float dur, struct nka_ease ease, int policy); + +/* Subdivisions below 1 mean 64. Rebuilding the path drops its table. */ +NKA_API void nka_path_build_arc_lut(struct nka_context *a, nk_hash path_id, int subdivisions); +NKA_API int nka_path_has_arc_lut(const struct nka_context *a, nk_hash path_id); +NKA_API float nka_path_distance_to_t(const struct nka_context *a, nk_hash path_id, float distance); +NKA_API struct nk_vec2 nka_path_evaluate_at_distance(const struct nka_context *a, nk_hash path_id, float distance); +NKA_API float nka_path_angle_at_distance(const struct nka_context *a, nk_hash path_id, float distance); +NKA_API struct nk_vec2 nka_path_tangent_at_distance(const struct nka_context *a, nk_hash path_id, float distance); + +NKA_API struct nka_morph_opts nka_morph_opts_default(void); +/* path_a at blend 0, path_b at 1; NULL opts are the defaults. With use_arc_length, t is a + * fraction of each path's length, and a path without an arc-length table gets one of 64. */ +NKA_API struct nk_vec2 nka_path_morph(struct nka_context *a, nk_hash path_a, nk_hash path_b, float t, float blend, const struct nka_morph_opts *opts); +NKA_API struct nk_vec2 nka_path_morph_tangent(struct nka_context *a, nk_hash path_a, nk_hash path_b, float t, float blend, const struct nka_morph_opts *opts); +NKA_API float nka_path_morph_angle(struct nka_context *a, nk_hash path_a, nk_hash path_b, float t, float blend, const struct nka_morph_opts *opts); +/* Along the morph once while the blend eases to target_blend. NKA_POLICY_CUT makes the + * blend jump; every other policy crossfades it. */ +NKA_API struct nk_vec2 nka_tween_path_morph(struct nka_context *a, nk_hash id, nk_hash ch, nk_hash path_a, nk_hash path_b, float target_blend, float dur, struct nka_ease path_ease, struct nka_ease morph_ease, int policy, const struct nka_morph_opts *opts); +NKA_API float nka_get_morph_blend(const struct nka_context *a, nk_hash id, nk_hash ch); + +/* Turns the four points about center, then moves them by translation. */ +NKA_API void nka_transform_quad(struct nk_vec2 *quad, struct nk_vec2 center, float angle, struct nk_vec2 translation); +/* Bottom-left, bottom-right, top-right, top-left of a glyph standing on pos, its baseline + * raised by baseline_offset, turned to angle. */ +NKA_API void nka_make_glyph_quad(struct nk_vec2 *quad, struct nk_vec2 pos, float angle, float glyph_width, float glyph_height, float baseline_offset); + +/* Clips */ + +enum nka_direction { NKA_DIR_NORMAL, NKA_DIR_REVERSE, NKA_DIR_ALTERNATE }; + +enum nka_channel_type { + NKA_CHAN_FLOAT, NKA_CHAN_VEC2, NKA_CHAN_VEC4, NKA_CHAN_INT, NKA_CHAN_COLOR, + NKA_CHAN_FLOAT_REL, NKA_CHAN_VEC2_REL, NKA_CHAN_VEC4_REL, NKA_CHAN_COLOR_REL +}; + +enum nka_result { NKA_OK, NKA_ERR_NOT_FOUND, NKA_ERR_BAD_ARG, NKA_ERR_NO_MEM }; + +/* How a keyed value changes from one loop to the next. */ +enum nka_variation_mode { + NKA_VAR_NONE, NKA_VAR_INCREMENT, NKA_VAR_DECREMENT, NKA_VAR_MULTIPLY, + NKA_VAR_RANDOM, /* within -amount and amount */ + NKA_VAR_RANDOM_ABS, /* within 0 and amount */ + NKA_VAR_PINGPONG, /* 0, +amount, 0, -amount, ... */ + NKA_VAR_CALLBACK +}; + +enum nka_stagger_from { NKA_STAGGER_FIRST, NKA_STAGGER_LAST, NKA_STAGGER_CENTER, NKA_STAGGER_INDEX }; +enum nka_stagger_axis { NKA_STAGGER_BOTH, NKA_STAGGER_X, NKA_STAGGER_Y }; + +struct nka_spring_params { float mass, stiffness, damping, initial_velocity; }; + +/* Called while values are worked out, with the loop index: they must not call nka_ functions. */ +typedef float (*nka_variation_float_fn)(int loop, void *user); +typedef int (*nka_variation_int_fn)(int loop, void *user); +typedef struct nk_vec2 (*nka_variation_vec2_fn)(int loop, void *user); +typedef struct nka_vec4 (*nka_variation_vec4_fn)(int loop, void *user); + +/* A seed of 0 draws from the instance; any other seed draws the same numbers every time. */ +struct nka_variation_float { + int mode; + float amount, min_clamp, max_clamp; + nk_uint seed; + nka_variation_float_fn callback; + void *user; +}; + +struct nka_variation_int { + int mode, amount, min_clamp, max_clamp; + nk_uint seed; + nka_variation_int_fn callback; + void *user; +}; + +/* With mode NKA_VAR_NONE, each axis varies on its own. */ +struct nka_variation_vec2 { + int mode; + struct nk_vec2 amount, min_clamp, max_clamp; + nk_uint seed; + nka_variation_vec2_fn callback; + void *user; + struct nka_variation_float x, y; +}; + +struct nka_variation_vec4 { + int mode; + struct nka_vec4 amount, min_clamp, max_clamp; + nk_uint seed; + nka_variation_vec4_fn callback; + void *user; + struct nka_variation_float x, y, z, w; +}; + +/* Varies the components of space and clamps there; the result, or the callback's, is sRGB. */ +struct nka_variation_color { + int mode; + struct nka_vec4 amount, min_clamp, max_clamp; + int space; + nk_uint seed; + nka_variation_vec4_fn callback; + void *user; + struct nka_variation_float r, g, b, a; +}; + +typedef void (*nka_clip_callback)(struct nka_context *a, nk_hash inst, void *user); +typedef void (*nka_loop_callback)(struct nka_context *a, nk_hash inst, int loop, void *user); +typedef void (*nka_marker_callback)(struct nka_context *a, nk_hash inst, nk_hash marker, float time, void *user); + +struct nka_stagger_grid_opts { + int cols, rows; + int from, from_index; /* nka_stagger_from; from_index with NKA_STAGGER_INDEX */ + int axis; /* nka_stagger_axis */ + float delay; /* the farthest element waits delay times the count less one */ + int ease; /* shapes the spread */ + float start_delay; +}; + +NKA_API struct nka_variation_float nka_varf_none(void); +NKA_API struct nka_variation_float nka_varf_inc(float amount); +NKA_API struct nka_variation_float nka_varf_dec(float amount); +NKA_API struct nka_variation_float nka_varf_mul(float factor); +NKA_API struct nka_variation_float nka_varf_rand(float range); +NKA_API struct nka_variation_float nka_varf_rand_abs(float range); +NKA_API struct nka_variation_float nka_varf_pingpong(float amount); +NKA_API struct nka_variation_float nka_varf_fn(nka_variation_float_fn fn, void *user); +NKA_API struct nka_variation_float nka_varf_clamp(struct nka_variation_float v, float lo, float hi); +NKA_API struct nka_variation_float nka_varf_seed(struct nka_variation_float v, nk_uint seed); + +NKA_API struct nka_variation_int nka_vari_none(void); +NKA_API struct nka_variation_int nka_vari_inc(int amount); +NKA_API struct nka_variation_int nka_vari_dec(int amount); +NKA_API struct nka_variation_int nka_vari_rand(int range); +NKA_API struct nka_variation_int nka_vari_fn(nka_variation_int_fn fn, void *user); +NKA_API struct nka_variation_int nka_vari_clamp(struct nka_variation_int v, int lo, int hi); +NKA_API struct nka_variation_int nka_vari_seed(struct nka_variation_int v, nk_uint seed); + +NKA_API struct nka_variation_vec2 nka_varv2_none(void); +NKA_API struct nka_variation_vec2 nka_varv2_inc(float x, float y); +NKA_API struct nka_variation_vec2 nka_varv2_dec(float x, float y); +NKA_API struct nka_variation_vec2 nka_varv2_mul(float factor); +NKA_API struct nka_variation_vec2 nka_varv2_rand(float x, float y); +NKA_API struct nka_variation_vec2 nka_varv2_fn(nka_variation_vec2_fn fn, void *user); +NKA_API struct nka_variation_vec2 nka_varv2_axis(struct nka_variation_float x, struct nka_variation_float y); +NKA_API struct nka_variation_vec2 nka_varv2_clamp(struct nka_variation_vec2 v, struct nk_vec2 lo, struct nk_vec2 hi); +NKA_API struct nka_variation_vec2 nka_varv2_seed(struct nka_variation_vec2 v, nk_uint seed); + +NKA_API struct nka_variation_vec4 nka_varv4_none(void); +NKA_API struct nka_variation_vec4 nka_varv4_inc(float x, float y, float z, float w); +NKA_API struct nka_variation_vec4 nka_varv4_dec(float x, float y, float z, float w); +NKA_API struct nka_variation_vec4 nka_varv4_mul(float factor); +NKA_API struct nka_variation_vec4 nka_varv4_rand(float x, float y, float z, float w); +NKA_API struct nka_variation_vec4 nka_varv4_fn(nka_variation_vec4_fn fn, void *user); +NKA_API struct nka_variation_vec4 nka_varv4_axis(struct nka_variation_float x, struct nka_variation_float y, + struct nka_variation_float z, struct nka_variation_float w); +NKA_API struct nka_variation_vec4 nka_varv4_clamp(struct nka_variation_vec4 v, struct nka_vec4 lo, struct nka_vec4 hi); +NKA_API struct nka_variation_vec4 nka_varv4_seed(struct nka_variation_vec4 v, nk_uint seed); + +/* In NKA_COL_OKLAB until nka_varc_space says otherwise. */ +NKA_API struct nka_variation_color nka_varc_none(void); +NKA_API struct nka_variation_color nka_varc_inc(float r, float g, float b, float alpha); +NKA_API struct nka_variation_color nka_varc_dec(float r, float g, float b, float alpha); +NKA_API struct nka_variation_color nka_varc_mul(float factor); +NKA_API struct nka_variation_color nka_varc_rand(float r, float g, float b, float alpha); +NKA_API struct nka_variation_color nka_varc_fn(nka_variation_vec4_fn fn, void *user); +NKA_API struct nka_variation_color nka_varc_channel(struct nka_variation_float r, struct nka_variation_float g, + struct nka_variation_float b, struct nka_variation_float alpha); +NKA_API struct nka_variation_color nka_varc_space(struct nka_variation_color v, int space); +NKA_API struct nka_variation_color nka_varc_clamp(struct nka_variation_color v, struct nka_vec4 lo, struct nka_vec4 hi); +NKA_API struct nka_variation_color nka_varc_seed(struct nka_variation_color v, nk_uint seed); + +/* One clip is built at a time; a begin drops a build that never ended. */ +NKA_API void nka_clip_begin(struct nka_context *a, nk_hash clip); +/* A key's ease shapes the way to the next key; bezier4, x1 y1 x2 y2, only with NKA_EASE_CUBIC_BEZIER. */ +NKA_API void nka_clip_key_float(struct nka_context *a, nk_hash ch, float time, float value, int ease, const float *bezier4); +NKA_API void nka_clip_key_vec2(struct nka_context *a, nk_hash ch, float time, struct nk_vec2 value, int ease, const float *bezier4); +NKA_API void nka_clip_key_vec4(struct nka_context *a, nk_hash ch, float time, struct nka_vec4 value, int ease, const float *bezier4); +NKA_API void nka_clip_key_int(struct nka_context *a, nk_hash ch, float time, int value, int ease); +NKA_API void nka_clip_key_color(struct nka_context *a, nk_hash ch, float time, struct nk_colorf value, int space, int ease, const float *bezier4); +NKA_API void nka_clip_key_float_var(struct nka_context *a, nk_hash ch, float time, float value, struct nka_variation_float var, + int ease, const float *bezier4); +NKA_API void nka_clip_key_vec2_var(struct nka_context *a, nk_hash ch, float time, struct nk_vec2 value, struct nka_variation_vec2 var, + int ease, const float *bezier4); +NKA_API void nka_clip_key_vec4_var(struct nka_context *a, nk_hash ch, float time, struct nka_vec4 value, struct nka_variation_vec4 var, + int ease, const float *bezier4); +NKA_API void nka_clip_key_int_var(struct nka_context *a, nk_hash ch, float time, int value, struct nka_variation_int var, int ease); +NKA_API void nka_clip_key_color_var(struct nka_context *a, nk_hash ch, float time, struct nk_colorf value, struct nka_variation_color var, + int space, int ease, const float *bezier4); +NKA_API void nka_clip_key_float_spring(struct nka_context *a, nk_hash ch, float time, float target, struct nka_spring_params spring); +/* Fractions of an anchor plus pixels, worked out when read. Axis 0 is x, 1 is y; a vec4 takes + * z and w as they are; a color scales r and b by the anchor's x, g and a by its y. */ +NKA_API void nka_clip_key_float_rel(struct nka_context *a, nk_hash ch, float time, float percent, float px_bias, int anchor, int axis, + int ease, const float *bezier4); +NKA_API void nka_clip_key_vec2_rel(struct nka_context *a, nk_hash ch, float time, struct nk_vec2 percent, struct nk_vec2 px_bias, + int anchor, int ease, const float *bezier4); +NKA_API void nka_clip_key_vec4_rel(struct nka_context *a, nk_hash ch, float time, struct nka_vec4 percent, struct nka_vec4 px_bias, + int anchor, int ease, const float *bezier4); +NKA_API void nka_clip_key_color_rel(struct nka_context *a, nk_hash ch, float time, struct nka_vec4 percent, struct nka_vec4 px_bias, + int space, int anchor, int ease, const float *bezier4); +/* Keys in a group take their times from where it began; after its end, from its last key. */ +NKA_API void nka_clip_seq_begin(struct nka_context *a); +NKA_API void nka_clip_seq_end(struct nka_context *a); +NKA_API void nka_clip_par_begin(struct nka_context *a); +NKA_API void nka_clip_par_end(struct nka_context *a); +/* Returns the marker's id, made up when id is 0. */ +NKA_API nk_hash nka_clip_marker(struct nka_context *a, float time, nk_hash id, nka_marker_callback cb, void *user); +/* A count of -1 loops forever, n repeats n times. */ +NKA_API void nka_clip_set_loop(struct nka_context *a, int loop, int direction, int count); +NKA_API void nka_clip_set_loop_delay(struct nka_context *a, float seconds); +NKA_API void nka_clip_set_delay(struct nka_context *a, float seconds); +/* center_bias from 0, a wave from the first element, to 1, from the middle out. */ +NKA_API void nka_clip_set_stagger(struct nka_context *a, int count, float each_delay, float center_bias); +NKA_API void nka_clip_set_stagger_ease(struct nka_context *a, int ease); +/* Each loop's length, the pause before it, and its speed. */ +NKA_API void nka_clip_set_duration_var(struct nka_context *a, struct nka_variation_float var); +NKA_API void nka_clip_set_delay_var(struct nka_context *a, struct nka_variation_float var); +NKA_API void nka_clip_set_timescale_var(struct nka_context *a, struct nka_variation_float var); +NKA_API void nka_clip_on_begin(struct nka_context *a, nka_clip_callback cb, void *user); +NKA_API void nka_clip_on_update(struct nka_context *a, nka_clip_callback cb, void *user); +NKA_API void nka_clip_on_complete(struct nka_context *a, nka_clip_callback cb, void *user); +NKA_API void nka_clip_on_loop(struct nka_context *a, nka_loop_callback cb, void *user); +NKA_API void nka_clip_on_pause(struct nka_context *a, nka_clip_callback cb, void *user); +/* NKA_OK, or an error with any clip of that id left as it was. */ +NKA_API int nka_clip_end(struct nka_context *a); + +NKA_API float nka_clip_duration(const struct nka_context *a, nk_hash clip); +NKA_API int nka_clip_exists(const struct nka_context *a, nk_hash clip); +NKA_API void nka_clip_reserve(struct nka_context *a, int clips, int instances); + +/* Starts clip on inst, made or restarted; returns inst, or 0 for an unknown clip or inst 0. */ +NKA_API nk_hash nka_play(struct nka_context *a, nk_hash clip, nk_hash inst); +/* inst when it exists, else 0. */ +NKA_API nk_hash nka_get_instance(struct nka_context *a, nk_hash inst); +NKA_API int nka_instance_valid(struct nka_context *a, nk_hash inst); +NKA_API void nka_instance_pause(struct nka_context *a, nk_hash inst); +NKA_API void nka_instance_resume(struct nka_context *a, nk_hash inst); +/* Stops at time 0 and keeps the values. */ +NKA_API void nka_instance_stop(struct nka_context *a, nk_hash inst); +NKA_API void nka_instance_restart(struct nka_context *a, nk_hash inst); +/* Goes back to the start of the pass and stops there. */ +NKA_API void nka_instance_reset(struct nka_context *a, nk_hash inst); +NKA_API void nka_instance_refresh(struct nka_context *a, nk_hash inst); +NKA_API void nka_instance_destroy(struct nka_context *a, nk_hash inst); +NKA_API void nka_instance_seek(struct nka_context *a, nk_hash inst, float time); +/* A scale of zero or below plays at normal speed. */ +NKA_API void nka_instance_set_time_scale(struct nka_context *a, nk_hash inst, float scale); +/* Multiplies the weight nka_layer_add gives the instance. */ +NKA_API void nka_instance_set_weight(struct nka_context *a, nk_hash inst, float weight); +/* When inst completes, next_clip plays on next_inst, made up when 0. Returns next_inst. */ +NKA_API nk_hash nka_instance_then(struct nka_context *a, nk_hash inst, nk_hash next_clip, nk_hash next_inst); +NKA_API void nka_instance_then_delay(struct nka_context *a, nk_hash inst, float delay); +NKA_API float nka_instance_time(struct nka_context *a, nk_hash inst); +NKA_API float nka_instance_duration(struct nka_context *a, nk_hash inst); +NKA_API int nka_instance_is_playing(struct nka_context *a, nk_hash inst); +NKA_API int nka_instance_is_paused(struct nka_context *a, nk_hash inst); +/* Zero, and *out zero (a color opaque black), when the clip has no such channel. */ +NKA_API int nka_instance_get_float(struct nka_context *a, nk_hash inst, nk_hash ch, float *out); +NKA_API int nka_instance_get_vec2(struct nka_context *a, nk_hash inst, nk_hash ch, struct nk_vec2 *out); +NKA_API int nka_instance_get_vec4(struct nka_context *a, nk_hash inst, nk_hash ch, struct nka_vec4 *out); +NKA_API int nka_instance_get_int(struct nka_context *a, nk_hash inst, nk_hash ch, int *out); +NKA_API int nka_instance_get_color(struct nka_context *a, nk_hash inst, nk_hash ch, struct nk_colorf *out); + +NKA_API float nka_stagger_delay(const struct nka_context *a, nk_hash clip, int index); +/* Plays with the clip's delay plus its stagger delay for index. */ +NKA_API nk_hash nka_play_stagger(struct nka_context *a, nk_hash clip, nk_hash inst, int index); +NKA_API struct nka_stagger_grid_opts nka_stagger_grid_opts_default(void); +/* NULL opts are the defaults. */ +NKA_API float nka_stagger_grid_delay(const struct nka_context *a, int col, int row, const struct nka_stagger_grid_opts *opts); +NKA_API float nka_stagger_grid_delay_index(const struct nka_context *a, int index, const struct nka_stagger_grid_opts *opts); +/* Plays with delay in place of the clip's. */ +NKA_API nk_hash nka_play_with_delay(struct nka_context *a, nk_hash clip, nk_hash inst, float delay); + +/* Weighted averages of instances' values per channel, kept under target; colors are left out. */ +NKA_API void nka_layer_begin(struct nka_context *a, nk_hash target); +NKA_API void nka_layer_add(struct nka_context *a, nk_hash inst, float weight); +NKA_API void nka_layer_end(struct nka_context *a, nk_hash target); +NKA_API int nka_get_blended_float(struct nka_context *a, nk_hash target, nk_hash ch, float *out); +NKA_API int nka_get_blended_vec2(struct nka_context *a, nk_hash target, nk_hash ch, struct nk_vec2 *out); +NKA_API int nka_get_blended_vec4(struct nka_context *a, nk_hash target, nk_hash ch, struct nka_vec4 *out); +NKA_API int nka_get_blended_int(struct nka_context *a, nk_hash target, nk_hash ch, int *out); + +/* Returns the size of clip saved, 0 for no such clip, and writes it to buf when cap allows. Callbacks are not saved. */ +NKA_API int nka_clip_save(const struct nka_context *a, nk_hash clip, void *buf, int cap); +/* Replaces the clip saved in buf, keeping the callbacks of the one it replaces, markers' by id. */ +NKA_API int nka_clip_load(struct nka_context *a, const void *buf, int len, nk_hash *out_id); + +/* Procedural */ + +enum nka_wave_type { NKA_WAVE_SINE, NKA_WAVE_TRIANGLE, NKA_WAVE_SAWTOOTH, NKA_WAVE_SQUARE }; +enum nka_noise_type { NKA_NOISE_PERLIN, NKA_NOISE_SIMPLEX, NKA_NOISE_VALUE, NKA_NOISE_WORLEY }; +/* CW only adds to the angle and CCW only takes from it; DIRECT lerps the two numbers. */ +enum nka_rotation_mode { + NKA_ROTATION_SHORTEST, NKA_ROTATION_LONGEST, NKA_ROTATION_CW, NKA_ROTATION_CCW, NKA_ROTATION_DIRECT +}; + +#ifndef NKA_GRADIENT_MAX +#define NKA_GRADIENT_MAX 16 +#endif + +/* Stops sorted by position. Zeroed, it is empty and samples white. */ +struct nka_gradient { + int count; + float positions[NKA_GRADIENT_MAX]; + struct nk_colorf colors[NKA_GRADIENT_MAX]; +}; + +/* Scales, then rotates by radians, then moves by position. */ +struct nka_transform { struct nk_vec2 position, scale; float rotation; }; + +struct nka_noise_opts { + int type, octaves; + float persistence, lacunarity; /* each octave's amplitude and frequency factors */ + int seed; +}; + +struct nka_drag_opts { + struct nk_vec2 snap_grid; /* 0 on an axis leaves it free */ + const struct nk_vec2 *snap_points; /* the nearest one wins over the grid */ + int snap_points_count; + float snap_duration; + float overshoot; /* 1 overshoots as much as NKA_EASE_OUT_BACK */ + int ease_type; +}; + +struct nka_drag_feedback { + struct nk_vec2 position, offset, velocity; + int is_dragging, is_snapping; + float snap_progress; +}; + +/* Between -amplitude and amplitude; phase is in cycles. */ +NKA_API float nka_oscillate(struct nka_context *a, nk_hash id, float amplitude, float frequency, int wave, float phase); +NKA_API int nka_oscillate_int(struct nka_context *a, nk_hash id, int amplitude, float frequency, int wave, float phase); +NKA_API struct nk_vec2 nka_oscillate_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 amplitude, struct nk_vec2 frequency, int wave, struct nk_vec2 phase); +NKA_API struct nka_vec4 nka_oscillate_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 amplitude, struct nka_vec4 frequency, int wave, struct nka_vec4 phase); +/* The amplitude moves the channels of the space, as hue, saturation, value and alpha in HSV. */ +NKA_API struct nk_colorf nka_oscillate_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 amplitude, float frequency, int wave, float phase, int space); + +/* A shake starts here and dies out over decay_time seconds. */ +NKA_API void nka_trigger_shake(struct nka_context *a, nk_hash id); +NKA_API float nka_shake(struct nka_context *a, nk_hash id, float intensity, float frequency, float decay_time); +NKA_API int nka_shake_int(struct nka_context *a, nk_hash id, int intensity, float frequency, float decay_time); +NKA_API struct nk_vec2 nka_shake_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 intensity, float frequency, float decay_time); +NKA_API struct nka_vec4 nka_shake_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 intensity, float frequency, float decay_time); +NKA_API struct nk_colorf nka_shake_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 intensity, float frequency, float decay_time, int space); +NKA_API float nka_wiggle(struct nka_context *a, nk_hash id, float amplitude, float frequency); +NKA_API int nka_wiggle_int(struct nka_context *a, nk_hash id, int amplitude, float frequency); +NKA_API struct nk_vec2 nka_wiggle_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 amplitude, float frequency); +NKA_API struct nka_vec4 nka_wiggle_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 amplitude, float frequency); +NKA_API struct nk_colorf nka_wiggle_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 amplitude, float frequency, int space); + +/* Perlin, 4 octaves, persistence 0.5, lacunarity 2, seed 0; a NULL opts means these. */ +NKA_API struct nka_noise_opts nka_noise_opts_default(void); +/* Between -1 and 1. */ +NKA_API float nka_noise_2d(float x, float y, const struct nka_noise_opts *opts); +NKA_API float nka_noise_3d(float x, float y, float z, const struct nka_noise_opts *opts); +NKA_API float nka_noise_channel_float(struct nka_context *a, nk_hash id, float frequency, float amplitude, const struct nka_noise_opts *opts); +NKA_API struct nk_vec2 nka_noise_channel_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 frequency, struct nk_vec2 amplitude, const struct nka_noise_opts *opts); +NKA_API struct nka_vec4 nka_noise_channel_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 frequency, struct nka_vec4 amplitude, const struct nka_noise_opts *opts); +NKA_API struct nk_colorf nka_noise_channel_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 amplitude, float frequency, const struct nka_noise_opts *opts, int space); +/* Two octaves of Perlin noise, speed being its frequency. */ +NKA_API float nka_smooth_noise_float(struct nka_context *a, nk_hash id, float amplitude, float speed); +NKA_API struct nk_vec2 nka_smooth_noise_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 amplitude, float speed); +NKA_API struct nka_vec4 nka_smooth_noise_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 amplitude, float speed); +NKA_API struct nk_colorf nka_smooth_noise_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 amplitude, float speed, int space); + +/* 0, adding nothing, when the gradient is full. */ +NKA_API int nka_gradient_add(struct nka_gradient *g, float position, struct nk_colorf color); +NKA_API int nka_gradient_add_rgba(struct nka_gradient *g, float position, struct nk_color color); +NKA_API struct nk_colorf nka_gradient_sample(const struct nka_gradient *g, float t, int space); +NKA_API struct nka_gradient nka_gradient_solid(struct nk_colorf color); +NKA_API struct nka_gradient nka_gradient_two_color(struct nk_colorf start, struct nk_colorf end); +NKA_API struct nka_gradient nka_gradient_three_color(struct nk_colorf start, struct nk_colorf mid, struct nk_colorf end); +/* Has a stop wherever either has one; past NKA_GRADIENT_MAX, the least visible go. */ +NKA_API struct nka_gradient nka_gradient_lerp(const struct nka_gradient *a, const struct nka_gradient *b, float t, int space); +/* NKA_POLICY_CUT jumps, every other policy crossfades. */ +NKA_API struct nka_gradient nka_tween_gradient(struct nka_context *a, nk_hash id, nk_hash ch, const struct nka_gradient *target, float dur, struct nka_ease ease, int policy, int space); + +NKA_API struct nka_transform nka_transform(struct nk_vec2 position, float rotation, struct nk_vec2 scale); +NKA_API struct nka_transform nka_transform_identity(void); +/* a after b: applying the result applies b, then a. */ +NKA_API struct nka_transform nka_transform_compose(struct nka_transform a, struct nka_transform b); +NKA_API struct nk_vec2 nka_transform_apply(struct nka_transform t, struct nk_vec2 point); +/* Exact under a uniform scale; compose(inverse(t), t) is the identity under any. */ +NKA_API struct nka_transform nka_transform_inverse(struct nka_transform t); +NKA_API struct nka_transform nka_transform_lerp(struct nka_transform a, struct nka_transform b, float t, int rotation_mode); +/* NKA_POLICY_CUT jumps, every other policy crossfades. */ +NKA_API struct nka_transform nka_tween_transform(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_transform target, float dur, struct nka_ease ease, int policy, int rotation_mode); +/* Row-major 3x2; out holds m00 m01 tx m10 m11 ty. */ +NKA_API struct nka_transform nka_transform_from_matrix(float m00, float m01, float m10, float m11, float tx, float ty); +NKA_API void nka_transform_to_matrix(struct nka_transform t, float *out); + +/* No snapping, 0.2 seconds, no overshoot, NKA_EASE_OUT_CUBIC; a NULL opts means these. */ +NKA_API struct nka_drag_opts nka_drag_opts_default(void); +NKA_API struct nka_drag_feedback nka_drag_begin(struct nka_context *a, nk_hash id, struct nk_vec2 pos); +NKA_API struct nka_drag_feedback nka_drag_update(struct nka_context *a, nk_hash id, struct nk_vec2 pos); +/* Once a frame until it stops snapping: the first call picks the target, the rest move there. */ +NKA_API struct nka_drag_feedback nka_drag_release(struct nka_context *a, nk_hash id, struct nk_vec2 pos, const struct nka_drag_opts *opts); +NKA_API void nka_drag_cancel(struct nka_context *a, nk_hash id); + +/* Nuklear */ + +/* Anchors from the current window: its content region and its size. The + * viewport and the last item are yours to set. */ +NKA_API void nka_anchor_update(struct nka_context *a, struct nk_context *ctx); + +/* In a window or group that scrolls this way, call nka_scroll last thing + * before nk_end or nk_group_end, every frame; the nka_scroll_to calls go + * anywhere inside it. Scrolling by hand stops a running one. */ +NKA_API void nka_scroll(struct nka_context *a, struct nk_context *ctx); +NKA_API void nka_scroll_to_x(struct nka_context *a, struct nk_context *ctx, float x, float dur, struct nka_ease ease); +NKA_API void nka_scroll_to_y(struct nka_context *a, struct nk_context *ctx, float y, float dur, struct nka_ease ease); +NKA_API void nka_scroll_to_top(struct nka_context *a, struct nk_context *ctx, float dur, struct nka_ease ease); +NKA_API void nka_scroll_to_bottom(struct nka_context *a, struct nk_context *ctx, float dur, struct nka_ease ease); + +/* Styles blend every color, size and color style item; the rest comes from + * whichever style is nearer. */ +NKA_API void nka_style_register(struct nka_context *a, nk_hash style_id, const struct nk_style *style); +NKA_API void nka_style_register_current(struct nka_context *a, const struct nk_context *ctx, nk_hash style_id); +/* The style nk_style_from_table makes from NK_COLOR_COUNT colors. */ +NKA_API void nka_style_register_table(struct nka_context *a, struct nk_context *ctx, nk_hash style_id, const struct nk_color *table); +NKA_API int nka_style_exists(const struct nka_context *a, nk_hash style_id); +NKA_API void nka_style_unregister(struct nka_context *a, nk_hash style_id); +NKA_API void nka_style_blend_to(struct nka_context *a, nk_hash style_a, nk_hash style_b, float t, struct nk_style *out, int space); +NKA_API void nka_style_blend(struct nka_context *a, struct nk_context *ctx, nk_hash style_a, nk_hash style_b, float t, int space); +/* Eases ctx->style from what it is to a registered style. */ +NKA_API void nka_style_tween(struct nka_context *a, struct nk_context *ctx, nk_hash id, nk_hash target_style, float dur, struct nka_ease ease, int space); + +enum nka_text_stagger_effect { + NKA_TEXT_FX_NONE, NKA_TEXT_FX_FADE, NKA_TEXT_FX_SCALE, + NKA_TEXT_FX_SLIDE_UP, NKA_TEXT_FX_SLIDE_DOWN, NKA_TEXT_FX_SLIDE_LEFT, NKA_TEXT_FX_SLIDE_RIGHT, + NKA_TEXT_FX_ROTATE, NKA_TEXT_FX_BOUNCE, NKA_TEXT_FX_WAVE, NKA_TEXT_FX_TYPEWRITER +}; + +struct nka_text_stagger_opts { + struct nk_vec2 pos; + int effect; + float char_delay, char_duration; + float effect_intensity; /* pixels for slides and waves */ + struct nka_ease ease; + struct nk_color color; + const struct nk_user_font *font; /* NULL is the style's */ + float font_scale; + float letter_spacing; +}; + +NKA_API struct nka_text_stagger_opts nka_text_stagger_opts_default(void); +/* Draws text into the current window, glyph by glyph as progress goes from + * 0 to 1. Scaling shows on backends that draw Nuklear's vertex output, from + * font copies kept until the next nka_update; Nuklear cannot turn text, so + * the rotate effect fades. */ +NKA_API void nka_text_stagger(struct nka_context *a, struct nk_context *ctx, const char *text, float progress, const struct nka_text_stagger_opts *opts); +NKA_API float nka_text_stagger_width(const struct nk_context *ctx, const char *text, const struct nka_text_stagger_opts *opts); +NKA_API float nka_text_stagger_duration(const char *text, const struct nka_text_stagger_opts *opts); + +enum nka_text_path_align { NKA_TEXT_ALIGN_START, NKA_TEXT_ALIGN_CENTER, NKA_TEXT_ALIGN_END }; + +struct nka_text_path_opts { + struct nk_vec2 origin; /* added to the path's points */ + float offset; /* along the path, in pixels */ + float letter_spacing; + int align; + int flip_y; /* glyphs hang below the path instead of standing on it */ + struct nk_color color; + const struct nk_user_font *font; /* NULL is the style's */ + float font_scale; +}; + +NKA_API struct nka_text_path_opts nka_text_path_opts_default(void); +/* Draws text along a path into the current window. Each glyph is centered on + * the path upright, as Nuklear's text cannot turn. The path gets an + * arc-length table if it has none. */ +NKA_API void nka_text_path(struct nka_context *a, struct nk_context *ctx, nk_hash path_id, const char *text, const struct nka_text_path_opts *opts); +/* The same, glyph by glyph as progress goes from 0 to 1. */ +NKA_API void nka_text_path_animated(struct nka_context *a, struct nk_context *ctx, nk_hash path_id, const char *text, float progress, const struct nka_text_path_opts *opts); +NKA_API float nka_text_path_width(const struct nk_context *ctx, const char *text, const struct nka_text_path_opts *opts); + +/* The profiler times named sections with the clock you give it, in seconds. */ +NKA_API void nka_profiler_set_clock(struct nka_context *a, double (*now)(void *user), void *user); +NKA_API void nka_profiler_enable(struct nka_context *a, int on); +NKA_API int nka_profiler_is_enabled(const struct nka_context *a); +NKA_API void nka_profiler_begin_frame(struct nka_context *a); +NKA_API void nka_profiler_end_frame(struct nka_context *a); +NKA_API void nka_profiler_begin(struct nka_context *a, const char *name); +NKA_API void nka_profiler_end(struct nka_context *a); + +/* A window with the time scale, what is animating, and the profiler. It + * closes by clearing *open; a NULL open keeps it open. */ +NKA_API void nka_show_unified_inspector(struct nka_context *a, struct nk_context *ctx, int *open); +/* A clip instance's tracks, keys and playhead, laid out in the current window. */ +NKA_API void nka_show_debug_timeline(struct nka_context *a, struct nk_context *ctx, nk_hash instance_id); + +#endif /* NUKANIM_H */ + +#ifdef NUKANIM_IMPLEMENTATION +#ifndef NKA__IMPLEMENTED +#define NKA__IMPLEMENTED + +#ifndef NKA_MATH +#include +#define NKA_MATH(fn) fn##f +#endif +#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR +#include +#endif + +#define nka__sin NKA_MATH(sin) +#define nka__cos NKA_MATH(cos) +#define nka__tan NKA_MATH(tan) +#define nka__asin NKA_MATH(asin) +#define nka__acos NKA_MATH(acos) +#define nka__atan2 NKA_MATH(atan2) +#define nka__pow NKA_MATH(pow) +#define nka__exp NKA_MATH(exp) +#define nka__log NKA_MATH(log) +#define nka__sqrt NKA_MATH(sqrt) +#define nka__cbrt NKA_MATH(cbrt) +#define nka__fabs NKA_MATH(fabs) +#define nka__fmod NKA_MATH(fmod) +#define nka__floor NKA_MATH(floor) +#define nka__ceil NKA_MATH(ceil) + +#define NKA__PI 3.14159265358979f +#define NKA__EPS 1e-6f +#define NKA__MIN_DUR 1e-6f + +/* Open addressing over power-of-two tables. A pointer into one lasts until + * the next put or del on that map. */ +struct nka__map { + nk_hash *keys; + char *vals; + int cap, len; + nk_size size; +}; + +enum { NKA__FLOAT, NKA__VEC2, NKA__VEC4, NKA__INT, NKA__COLOR }; + +/* Whatever nka__sweep collects starts with the frame it was last seen. */ +struct nka__chan { + nk_uint seen; + unsigned char kind, n, policy, space, sleeping, pending; + nk_hash id, ch; + float cur[4], from[4], to[4], req[4], pend[4]; + float dur; + double start; + struct nka_ease ease; +}; + +struct nka__anim { + nk_uint seen; + int running; + float from, to; + double start; +}; + +struct nka__path_seg { + int type; + float tension, length; + struct nk_vec2 p[4]; +}; + +/* seg_len, which maps t to a segment, stays the segments' sum; length becomes the table's measure. */ +struct nka__path_data { + struct nka__path_seg *segs; + float *lut; + int n, cap, lut_n, lut_cap; + float seg_len, length; + struct nk_vec2 start; +}; + +struct nka__path_pair { + const struct nka__path_data *a, *b; + int n, arc; +}; + +#define NKA__PATH_KEY 0x50415448u +#define NKA__PATH_POS 0x50504f53u +#define NKA__PATH_ANGLE 0x50414e47u +#define NKA__PATH_MORPH 0x504d5048u +#define NKA__PATH_BLEND 0x50424c44u + +#define NKA__CLIP_MAX_DT 1.0f +#define NKA__CLIP_MAX_WRAPS 1000 +#define NKA__CLIP_MIN_PASS 0.001f +#define NKA__CLIP_FLT_MAX 3.402823466e+38f +#define NKA__CLIP_INT_MAX ((int)(~0u >> 1)) +#define NKA__CLIP_MAGIC 0x43414b4eu +#define NKA__CLIP_VERSION 1u +#define NKA__CLIP_BEZIER 1 +#define NKA__CLIP_SPRING 2 +#define NKA__CLIP_VARIED 4 +#define NKA__CLIP_SALT_DUR 0xd1000001u +#define NKA__CLIP_SALT_DELAY 0xd1000002u +#define NKA__CLIP_SALT_SCALE 0xd1000003u +#define NKA__CLIP_SALT_INST 0x494e5354u +#define NKA__CLIP_SALT_MARK 0x4d41524bu + +union nka__clip_slot { float f; int i; nk_uint u; }; + +union nka__clip_var { + struct nka_variation_float f; + struct nka_variation_int i; + struct nka_variation_vec2 v2; + struct nka_variation_vec4 v4; + struct nka_variation_color c; +}; + +/* An int key or value lives in i, never read through f. */ +struct nka__clip_key { + nk_hash ch; + int type, space, anchor, axis; + float time; + int ease, flags, var; + float bezier[4], spring[4], ext[4]; + union nka__clip_slot v[4]; +}; + +struct nka__clip_track { + nk_hash ch; + int type, space, anchor, axis, first, count; +}; + +struct nka__clip_marker { + float time; + nk_hash id; + nka_marker_callback fn; + void *user; +}; + +struct nka__clip_clip { + nk_hash id; + nk_uint gen; + float duration, delay, loop_delay, stagger_delay, stagger_bias; + int loop_count, direction, stagger_count, stagger_ease; + int has_dur_var, has_delay_var, has_scale_var; + struct nka_variation_float dur_var, delay_var, scale_var; + nka_clip_callback on_begin, on_update, on_complete, on_pause; + nka_loop_callback on_loop; + void *begin_user, *update_user, *complete_user, *pause_user, *loop_user; + struct nka__clip_track *tracks; + struct nka__clip_key *keys; + union nka__clip_var *vars; + struct nka__clip_marker *markers; + int ntracks, nkeys, nvars, nmarkers; +}; + +struct nka__clip_group { float max; int seq; }; + +struct nka__clip_build { + struct nka__clip_clip clip; + struct nka__clip_key *keys; + union nka__clip_var *vars; + struct nka__clip_marker *markers; + struct nka__clip_group *groups; + int nkeys, nvars, nmarkers, ngroups, ckeys, cvars, cmarkers, cgroups; + float offset; + int open, failed; +}; + +/* vals holds 8 slots a track, then a fired flag a marker, sized for the clip of gen. */ +struct nka__clip_inst { + nk_uint seen, stepped, gen, epoch, rng; + nk_hash id, clip, chain_clip, chain_inst; + float time, scale, weight, delay, chain_delay; + int playing, paused, begun, dir, loops, loop; + union nka__clip_slot *vals; + unsigned char *fired; +}; + +struct nka__clip_blend { nk_hash ch; int kind; float v[4]; }; +struct nka__clip_layer { nk_uint seen; nk_hash id; int n; struct nka__clip_blend *e; }; +struct nka__clip_acc { nk_hash ch; int kind; float w, sum[4]; }; + +/* Off the lattice: along a whole-number row, gradient noise loses most of its range. */ +#define NKA__FX_ROW0 0.37f +#define NKA__FX_ROW1 100.71f +#define NKA__FX_ROW2 200.19f +#define NKA__FX_ROW3 300.53f + +enum { NKA__FX_OSC = 1, NKA__FX_SHAKE, NKA__FX_WIGGLE, NKA__FX_NOISE }; + +/* Its time passes only on frames where its id is sampled. */ +struct nka__fx_clock { + nk_uint seen, frame; + int triggered; + double time, since; +}; + +struct nka__fx_grad { + nk_uint seen; + unsigned char sleeping, policy, space; + float dur; + double start; + struct nka_ease ease; + struct nka_gradient from, to; +}; + +struct nka__fx_xform { + nk_uint seen; + unsigned char sleeping, policy, mode; + float dur; + double start; + struct nka_ease ease; + struct nka_transform from, to; +}; + +struct nka__fx_drag { + nk_uint seen, frame; + int dragging, snapping, ease; + float dur, overshoot, progress; + double t0, t_prev, t_cur; + struct nk_vec2 start, cur, prev, vel, from, to; +}; + +struct nka__nk_scroll { + nk_uint seen; + int active[2]; + float from[2], to[2], max[2], dur[2]; + nk_uint wrote[2]; + double start[2]; + struct nka_ease ease[2]; +}; + +struct nka__nk_style { + struct nk_style *style; +}; + +struct nka__nk_style_tween { + nk_uint seen; + int active, space; + nk_hash target; + struct nk_style *from; + double start; + float dur; + struct nka_ease ease; +}; + +#define NKA__NK_FONTS 32 +struct nka__nk_fonts { + struct nka__nk_fonts *next; + int used; + struct nk_user_font font[NKA__NK_FONTS]; +}; + +#define NKA__NK_SECTIONS 64 +#define NKA__NK_STACK 16 +#define NKA__NK_HISTORY 120 +struct nka__nk_section { + char name[32]; + double start, ms; + int calls; + float history[NKA__NK_HISTORY]; +}; + +struct nka__nk_profiler { + double (*now)(void *user); + void *user; + int enabled, count, depth, at; + double frame_start, frame_ms; + float frame[NKA__NK_HISTORY]; + int stack[NKA__NK_STACK]; + struct nka__nk_section sections[NKA__NK_SECTIONS]; +}; + +struct nka_context { + struct nk_allocator alloc; + double time; + float dt, scale; + nk_uint frame; + int lazy, busy; + nka_ease_fn custom[16]; + struct nk_vec2 anchors[NKA_ANCHOR_COUNT]; + struct nka__map chans, anims; + struct nka__map path_map; + struct nka__path_data path_new; + struct nk_vec2 path_cur, path_prev; + nk_hash path_id; + int path_building, path_failed; + struct nka__map clip_clips, clip_insts, clip_layers, clip_acc; + struct nka__clip_build clip_build; + nk_uint clip_gen, clip_epoch, clip_auto, clip_mod; + nk_hash clip_layer; + float clip_layer_weight; + int clip_updating; + struct nka__map fx_clocks, fx_grads, fx_xforms, fx_drags; + struct nka__map nk_scrolls, nk_styles, nk_style_tweens; + struct nka__nk_fonts *nk_fonts; + struct nka__nk_profiler *nk_profiler; +}; + +#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR +static void *nka__malloc(nk_handle h, void *old, nk_size n) { (void)h; (void)old; return malloc(n); } +static void nka__mfree(nk_handle h, void *p) { (void)h; free(p); } +#endif + +static void *nka__alloc(struct nka_context *a, nk_size n) { return a->alloc.alloc(a->alloc.userdata, 0, n); } +static void nka__free(struct nka_context *a, void *p) { if (p) a->alloc.free(a->alloc.userdata, p); } + +static void nka__zero(void *p, nk_size n) +{ + unsigned char *b = (unsigned char *)p; + while (n--) *b++ = 0; +} + +static void nka__copy(void *dst, const void *src, nk_size n) +{ + unsigned char *d = (unsigned char *)dst; + const unsigned char *s = (const unsigned char *)src; + while (n--) *d++ = *s++; +} + +static nk_hash nka__fmix(nk_hash h) +{ + h ^= h >> 16; h *= 0x85ebca6bu; + h ^= h >> 13; h *= 0xc2b2ae35u; + return h ^ (h >> 16); +} + +NKA_API nk_hash nka_id(const char *name) +{ + nk_hash h = 2166136261u; + while (name && *name) { h ^= (unsigned char)*name++; h *= 16777619u; } + return h; +} + +NKA_API nk_hash nka_id_mix(nk_hash a, nk_hash b) +{ + return nka__fmix(a ^ (b + 0x9e3779b9u + (a << 6) + (a >> 2))); +} + +static nk_hash nka__key(nk_hash id, nk_hash ch, int kind) +{ + return nka_id_mix(nka_id_mix(id, ch), (nk_hash)kind); +} + +static nk_hash nka__ptr_key(const void *p) +{ + nk_ptr v = (nk_ptr)p; + return nka_id_mix((nk_hash)v, (nk_hash)((v >> 16) >> 16)); +} + +static void *nka__at(const struct nka__map *m, int i) { return m->vals + (nk_size)i * m->size; } + +/* Keys are stored scrambled, so raw ids like 0 and 1 stay apart; 0 marks a free slot. */ +static nk_hash nka__mkey(nk_hash key) +{ + key = nka__fmix(key); + return key ? key : 1u; +} + +static int nka__home(const struct nka__map *m, nk_hash stored) { return (int)(stored & (nk_hash)(m->cap - 1)); } + +static int nka__slot(const struct nka__map *m, nk_hash key) +{ + int i; + if (!m->cap) return -1; + key = nka__mkey(key); + for (i = nka__home(m, key); m->keys[i]; i = (i + 1) & (m->cap - 1)) + if (m->keys[i] == key) return i; + return -1; +} + +static void *nka__get(const struct nka__map *m, nk_hash key) +{ + int i = nka__slot(m, key); + return i < 0 ? 0 : nka__at(m, i); +} + +static int nka__grow(struct nka_context *a, struct nka__map *m, int cap) +{ + struct nka__map n = *m; + int i; + n.keys = (nk_hash *)nka__alloc(a, (nk_size)cap * (sizeof(nk_hash) + m->size)); + if (!n.keys) return 0; + n.vals = (char *)(n.keys + cap); + n.cap = cap; + nka__zero(n.keys, (nk_size)cap * sizeof(nk_hash)); + for (i = 0; i < m->cap; ++i) { + int j; + if (!m->keys[i]) continue; + j = nka__home(&n, m->keys[i]); + while (n.keys[j]) j = (j + 1) & (cap - 1); + n.keys[j] = m->keys[i]; + nka__copy(nka__at(&n, j), nka__at(m, i), m->size); + } + nka__free(a, m->keys); + *m = n; + return 1; +} + +/* Zeroed when new. NULL when out of memory. */ +static void *nka__put(struct nka_context *a, struct nka__map *m, nk_hash key, int *fresh) +{ + int i; + if (fresh) *fresh = 0; + i = nka__slot(m, key); + if (i >= 0) return nka__at(m, i); + if ((m->len + 1) * 2 > m->cap && !nka__grow(a, m, m->cap ? m->cap * 2 : 16)) return 0; + key = nka__mkey(key); + i = nka__home(m, key); + while (m->keys[i]) i = (i + 1) & (m->cap - 1); + m->keys[i] = key; + m->len++; + nka__zero(nka__at(m, i), m->size); + if (fresh) *fresh = 1; + return nka__at(m, i); +} + +/* Shifts the rest of the cluster back, so slot i may hold another entry after. */ +static void nka__del_at(struct nka__map *m, int i) +{ + int mask = m->cap - 1, j = i; + for (;;) { + m->keys[i] = 0; + for (;;) { + int h; + j = (j + 1) & mask; + if (!m->keys[j]) { m->len--; return; } + h = nka__home(m, m->keys[j]); + if (!(i <= j ? (i < h && h <= j) : (i < h || h <= j))) break; + } + m->keys[i] = m->keys[j]; + nka__copy(nka__at(m, i), nka__at(m, j), m->size); + i = j; + } +} + +static void nka__del(struct nka__map *m, nk_hash key) +{ + int i = nka__slot(m, key); + if (i >= 0) nka__del_at(m, i); +} + +static void nka__sweep(struct nka__map *m, nk_uint frame, unsigned max_age) +{ + int i = 0; + while (i < m->cap) { + if (m->keys[i] && frame - *(const nk_uint *)nka__at(m, i) > max_age) nka__del_at(m, i); + else ++i; + } +} + +static void nka__map_free(struct nka_context *a, struct nka__map *m) +{ + nka__free(a, m->keys); + m->keys = 0; + m->vals = 0; + m->cap = m->len = 0; +} + +static float nka__clamp01(float t) { return t < 0 ? 0 : (t > 1 ? 1 : t); } +static float nka__lerp(float a, float b, float t) { return a + (b - a) * t; } + +static struct nk_vec2 nka__vec2(float x, float y) +{ + struct nk_vec2 v; + v.x = x; v.y = y; + return v; +} + +static struct nka_vec4 nka__vec4(float x, float y, float z, float w) +{ + struct nka_vec4 v; + v.x = x; v.y = y; v.z = z; v.w = w; + return v; +} + +static struct nka_vec4 nka__cv(struct nk_colorf c) { return nka__vec4(c.r, c.g, c.b, c.a); } + +static struct nk_colorf nka__vc(struct nka_vec4 v) +{ + struct nk_colorf c; + c.r = v.x; c.g = v.y; c.b = v.z; c.a = v.w; + return c; +} + +/* Easing */ + +NKA_API struct nka_ease nka_ease(int type) +{ + struct nka_ease e; + e.type = type; + e.p0 = e.p1 = e.p2 = e.p3 = 0; + return e; +} + +NKA_API struct nka_ease nka_ease_bezier(float x1, float y1, float x2, float y2) +{ + struct nka_ease e = nka_ease(NKA_EASE_CUBIC_BEZIER); + e.p0 = x1; e.p1 = y1; e.p2 = x2; e.p3 = y2; + return e; +} + +NKA_API struct nka_ease nka_ease_steps(int steps, int mode) +{ + struct nka_ease e = nka_ease(NKA_EASE_STEPS); + e.p0 = (float)steps; e.p1 = (float)mode; + return e; +} + +NKA_API struct nka_ease nka_ease_back(float overshoot) +{ + struct nka_ease e = nka_ease(NKA_EASE_OUT_BACK); + e.p0 = overshoot; + return e; +} + +NKA_API struct nka_ease nka_ease_elastic(float amplitude, float period) +{ + struct nka_ease e = nka_ease(NKA_EASE_OUT_ELASTIC); + e.p0 = amplitude; e.p1 = period; + return e; +} + +NKA_API struct nka_ease nka_ease_spring(float mass, float stiffness, float damping, float velocity) +{ + struct nka_ease e = nka_ease(NKA_EASE_SPRING); + e.p0 = mass; e.p1 = stiffness; e.p2 = damping; e.p3 = velocity; + return e; +} + +NKA_API struct nka_ease nka_ease_custom(int slot) +{ + struct nka_ease e = nka_ease(NKA_EASE_CUSTOM); + e.p0 = (float)slot; + return e; +} + +static float nka__bounce(float t) +{ + const float n = 7.5625f, d = 2.75f; + if (t < 1 / d) return n * t * t; + if (t < 2 / d) { t -= 1.5f / d; return n * t * t + 0.75f; } + if (t < 2.5f / d) { t -= 2.25f / d; return n * t * t + 0.9375f; } + t -= 2.625f / d; + return n * t * t + 0.984375f; +} + +static float nka__in(int family, float t) +{ + const float c1 = 1.70158f, c3 = c1 + 1, c4 = 2 * NKA__PI / 3; + switch (family) { + case 0: return t * t; + case 1: return t * t * t; + case 2: return t * t * t * t; + case 3: return t * t * t * t * t; + case 4: return 1 - nka__cos(t * NKA__PI / 2); + case 5: return t == 0 ? 0 : nka__pow(2, 10 * t - 10); + case 6: return 1 - nka__sqrt(1 - t * t); + case 7: return c3 * t * t * t - c1 * t * t; + case 8: return t == 0 || t == 1 ? t : -nka__pow(2, 10 * t - 10) * nka__sin((t * 10 - 10.75f) * c4); + case 9: return 1 - nka__bounce(1 - t); + default: return t; + } +} + +NKA_API float nka_eval_preset(int type, float t) +{ + int family = (type - 1) / 3; + t = nka__clamp01(t); + if (type <= NKA_EASE_LINEAR || type > NKA_EASE_IN_OUT_BOUNCE) return t; + switch ((type - 1) % 3) { + case 0: return nka__in(family, t); + case 1: return 1 - nka__in(family, 1 - t); + default: return t < 0.5f ? nka__in(family, 2 * t) / 2 : 1 - nka__in(family, 2 - 2 * t) / 2; + } +} + +static float nka__back(float t, float s) { return t * t * ((s + 1) * t - s); } + +static float nka__elastic(float t, float amp, float period) +{ + float s; + if (t <= 0 || t >= 1) return t; + if (amp < 1) { amp = 1; s = period / 4; } + else s = period / (2 * NKA__PI) * nka__asin(1 / amp); + return -(amp * nka__pow(2, 10 * (t - 1)) * nka__sin((t - 1 - s) * 2 * NKA__PI / period)); +} + +static float nka__steps(float t, int n, int mode) +{ + float k, y; + if (n < 1) n = 1; + k = nka__floor(t * (float)n + NKA__EPS); + if (mode == 1) y = (k + 1) / (float)n; + else if (mode == 2) y = (k + 1) / (float)(n + 1); + else y = k / (float)n; + return y > 1 ? 1 : y; +} + +static float nka__bezier(float x, float x1, float y1, float x2, float y2) +{ + float t = x, m; + int i; + for (i = 0; i < 8; ++i) { + float u = 1 - t; + float bx = 3 * u * u * t * x1 + 3 * u * t * t * x2 + t * t * t; + float dx = 3 * u * u * x1 + 6 * u * t * (x2 - x1) + 3 * t * t * (1 - x2); + if (dx != 0) t = nka__clamp01(t - (bx - x) / dx); + } + m = 1 - t; + return 3 * m * m * t * y1 + 3 * m * t * t * y2 + t * t * t; +} + +/* A unit step of a damped spring, t in seconds, v0 toward the target. */ +static float nka__spring(float t, float mass, float k, float c, float v0) +{ + float wn = nka__sqrt(k / mass), zeta = c / (2 * nka__sqrt(k * mass)); + if (zeta < 1) { + float wd = wn * nka__sqrt(1 - zeta * zeta); + float b = (zeta * wn - v0) / wd; + return 1 - nka__exp(-zeta * wn * t) * (nka__cos(wd * t) + b * nka__sin(wd * t)); + } + if (zeta == 1) return 1 - nka__exp(-wn * t) * (1 + (wn - v0) * t); + { + float wd = wn * nka__sqrt(zeta * zeta - 1); + float r1 = -zeta * wn + wd, r2 = -zeta * wn - wd; + float c1 = (-v0 - r2) / (r1 - r2); + return 1 - c1 * nka__exp(r1 * t) - (1 - c1) * nka__exp(r2 * t); + } +} + +NKA_API float nka_eval(const struct nka_context *a, struct nka_ease e, float t) +{ + t = nka__clamp01(t); + switch (e.type) { + case NKA_EASE_IN_BACK: case NKA_EASE_OUT_BACK: case NKA_EASE_IN_OUT_BACK: + if (e.p0 == 0) break; + if (e.type == NKA_EASE_IN_BACK) return nka__back(t, e.p0); + if (e.type == NKA_EASE_OUT_BACK) return 1 - nka__back(1 - t, e.p0); + return t < 0.5f ? nka__back(2 * t, e.p0) / 2 : 1 - nka__back(2 - 2 * t, e.p0) / 2; + case NKA_EASE_IN_ELASTIC: case NKA_EASE_OUT_ELASTIC: case NKA_EASE_IN_OUT_ELASTIC: { + float amp = e.p0 > 0 ? e.p0 : 1; + float period = e.p1 > 0 ? e.p1 : (e.type == NKA_EASE_IN_OUT_ELASTIC ? 0.45f : 0.3f); + if (e.p0 <= 0 && e.p1 <= 0) break; + if (e.type == NKA_EASE_IN_ELASTIC) return nka__elastic(t, amp, period); + if (e.type == NKA_EASE_OUT_ELASTIC) return 1 - nka__elastic(1 - t, amp, period); + return t < 0.5f ? nka__elastic(2 * t, amp, period) / 2 : 1 - nka__elastic(2 - 2 * t, amp, period) / 2; + } + case NKA_EASE_STEPS: return nka__steps(t, (int)e.p0, (int)e.p1); + case NKA_EASE_CUBIC_BEZIER: return nka__bezier(t, e.p0, e.p1, e.p2, e.p3); + case NKA_EASE_SPRING: + return nka__spring(t, e.p0 > 0 ? e.p0 : 1, e.p1 > 0 ? e.p1 : 120, e.p2 > 0 ? e.p2 : 20, e.p3); + case NKA_EASE_CUSTOM: { + int slot = (int)e.p0; + if (a && slot >= 0 && slot < 16 && a->custom[slot]) return a->custom[slot](t); + return t; + } + default: break; + } + return nka_eval_preset(e.type, t); +} + +NKA_API void nka_register_ease(struct nka_context *a, int slot, nka_ease_fn fn) +{ + if (a && slot >= 0 && slot < 16) a->custom[slot] = fn; +} + +NKA_API nka_ease_fn nka_custom_ease(const struct nka_context *a, int slot) +{ + return a && slot >= 0 && slot < 16 ? a->custom[slot] : 0; +} + +/* Color spaces. Everything outside them is sRGB, 0 to 1. */ + +static float nka__to_linear(float c) { return c <= 0.04045f ? c / 12.92f : nka__pow((c + 0.055f) / 1.055f, 2.4f); } +static float nka__to_srgb(float c) { return c <= 0.0031308f ? 12.92f * c : 1.055f * nka__pow(c, 1 / 2.4f) - 0.055f; } + +static struct nka_vec4 nka__hsv_to_srgb(struct nka_vec4 c) +{ + float h, f, p, q, t; + int i; + if (c.y <= 0) return nka__vec4(c.z, c.z, c.z, c.w); + h = nka__fmod(c.x, 1); + if (h < 0) h += 1; + h *= 6; + i = (int)nka__floor(h); + f = h - (float)i; + p = c.z * (1 - c.y); + q = c.z * (1 - c.y * f); + t = c.z * (1 - c.y * (1 - f)); + switch (i % 6) { + case 0: return nka__vec4(c.z, t, p, c.w); + case 1: return nka__vec4(q, c.z, p, c.w); + case 2: return nka__vec4(p, c.z, t, c.w); + case 3: return nka__vec4(p, q, c.z, c.w); + case 4: return nka__vec4(t, p, c.z, c.w); + default: return nka__vec4(c.z, p, q, c.w); + } +} + +static struct nka_vec4 nka__srgb_to_hsv(struct nka_vec4 c) +{ + float mx = c.x > c.y ? c.x : c.y, mn = c.x < c.y ? c.x : c.y, d, h = 0; + if (c.z > mx) mx = c.z; + if (c.z < mn) mn = c.z; + d = mx - mn; + if (d != 0) { + if (mx == c.x) h = nka__fmod((c.y - c.z) / d, 6); + else if (mx == c.y) h = (c.z - c.x) / d + 2; + else h = (c.x - c.y) / d + 4; + h /= 6; + if (h < 0) h += 1; + } + return nka__vec4(h, mx == 0 ? 0 : d / mx, mx, c.w); +} + +static struct nka_vec4 nka__srgb_to_oklab(struct nka_vec4 c) +{ + float r = nka__to_linear(c.x), g = nka__to_linear(c.y), b = nka__to_linear(c.z); + float l = nka__cbrt(0.4122214708f * r + 0.5363325363f * g + 0.0514459929f * b); + float m = nka__cbrt(0.2119034982f * r + 0.6806995451f * g + 0.1073969566f * b); + float s = nka__cbrt(0.0883024619f * r + 0.2817188376f * g + 0.6299787005f * b); + return nka__vec4(0.2104542553f * l + 0.7936177850f * m - 0.0040720468f * s, + 1.9779984951f * l - 2.4285922050f * m + 0.4505937099f * s, + 0.0259040371f * l + 0.7827717662f * m - 0.8086757660f * s, c.w); +} + +static struct nka_vec4 nka__oklab_to_srgb(struct nka_vec4 c) +{ + float l = c.x + 0.3963377774f * c.y + 0.2158037573f * c.z; + float m = c.x - 0.1055613458f * c.y - 0.0638541728f * c.z; + float s = c.x - 0.0894841775f * c.y - 1.2914855480f * c.z; + l = l * l * l; m = m * m * m; s = s * s * s; + return nka__vec4(nka__to_srgb(nka__clamp01(4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s)), + nka__to_srgb(nka__clamp01(-1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s)), + nka__to_srgb(nka__clamp01(-0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s)), c.w); +} + +static struct nka_vec4 nka__to_space(struct nka_vec4 c, int space) +{ + struct nka_vec4 v; + switch (space) { + case NKA_COL_SRGB_LINEAR: return nka__vec4(nka__to_linear(c.x), nka__to_linear(c.y), nka__to_linear(c.z), c.w); + case NKA_COL_HSV: return nka__srgb_to_hsv(c); + case NKA_COL_OKLAB: return nka__srgb_to_oklab(c); + case NKA_COL_OKLCH: + v = nka__srgb_to_oklab(c); + c.x = v.x; + c.y = nka__sqrt(v.y * v.y + v.z * v.z); + c.z = nka__atan2(v.z, v.y) / (2 * NKA__PI); + if (c.z < 0) c.z += 1; + return c; + default: return c; + } +} + +static struct nka_vec4 nka__from_space(struct nka_vec4 c, int space) +{ + switch (space) { + case NKA_COL_SRGB_LINEAR: return nka__vec4(nka__to_srgb(c.x), nka__to_srgb(c.y), nka__to_srgb(c.z), c.w); + case NKA_COL_HSV: return nka__hsv_to_srgb(c); + case NKA_COL_OKLAB: return nka__oklab_to_srgb(c); + case NKA_COL_OKLCH: + return nka__oklab_to_srgb(nka__vec4(c.x, c.y * nka__cos(c.z * 2 * NKA__PI), c.y * nka__sin(c.z * 2 * NKA__PI), c.w)); + default: return c; + } +} + +static float nka__hue_lerp(float a, float b, float t) +{ + float h = a + (nka__fmod(b - a + 1.5f, 1) - 0.5f) * t; + return h < 0 ? h + 1 : (h > 1 ? h - 1 : h); +} + +static struct nka_vec4 nka__lerp4(struct nka_vec4 a, struct nka_vec4 b, float t) +{ + return nka__vec4(nka__lerp(a.x, b.x, t), nka__lerp(a.y, b.y, t), nka__lerp(a.z, b.z, t), nka__lerp(a.w, b.w, t)); +} + +static struct nka_vec4 nka__lerp_color(struct nka_vec4 a, struct nka_vec4 b, float t, int space) +{ + struct nka_vec4 x, y, r; + if (t == 0) return a; + if (t == 1) return b; + x = nka__to_space(a, space); + y = nka__to_space(b, space); + r = nka__lerp4(x, y, t); + if (space == NKA_COL_HSV) r.x = nka__hue_lerp(x.x, y.x, t); + if (space == NKA_COL_OKLCH) r.z = nka__hue_lerp(x.z, y.z, t); + return nka__from_space(r, space); +} + +NKA_API struct nk_colorf nka_color_blend(struct nk_colorf a, struct nk_colorf b, float t, int space) +{ + return nka__vc(nka__lerp_color(nka__cv(a), nka__cv(b), t, space)); +} + +/* Tweens */ + +static void nka__chan_eval(struct nka_context *a, struct nka__chan *c) +{ + float t, k; + int i; + if (c->sleeping) return; + t = (float)((a->time - c->start) / c->dur); + if (t >= 1) { + for (i = 0; i < 4; ++i) c->cur[i] = c->to[i]; + c->sleeping = 1; + return; + } + k = nka_eval(a, c->ease, t); + if (c->kind == NKA__COLOR) { + struct nka_vec4 v = nka__lerp_color(nka__vec4(c->from[0], c->from[1], c->from[2], c->from[3]), + nka__vec4(c->to[0], c->to[1], c->to[2], c->to[3]), k, c->space); + c->cur[0] = v.x; c->cur[1] = v.y; c->cur[2] = v.z; c->cur[3] = v.w; + } else { + for (i = 0; i < c->n; ++i) { + c->cur[i] = nka__lerp(c->from[i], c->to[i], k); + if (c->kind == NKA__INT) c->cur[i] = nka__floor(c->cur[i] + 0.5f); + } + } + a->busy = 1; +} + +static void nka__chan_set(struct nka_context *a, struct nka__chan *c, const float *to, float dur, + struct nka_ease e, int policy, int space) +{ + int i; + for (i = 0; i < 4; ++i) { c->from[i] = c->cur[i]; c->to[i] = to[i]; } + c->dur = dur > NKA__MIN_DUR ? dur : NKA__MIN_DUR; + c->start = a->time; + c->ease = e; + c->policy = (unsigned char)policy; + c->space = (unsigned char)space; + c->sleeping = 0; +} + +static int nka__near(const float *x, const float *y, int n) +{ + float d = 0; + int i; + for (i = 0; i < n; ++i) d += nka__fabs(x[i] - y[i]); + return d <= NKA__EPS; +} + +static int nka__ease_eq(struct nka_ease x, struct nka_ease y) +{ + return x.type == y.type && x.p0 == y.p0 && x.p1 == y.p1 && x.p2 == y.p2 && x.p3 == y.p3; +} + +/* Every array is four floats; a kind reads the first one, two or four. */ +static void nka__tween(struct nka_context *a, nk_hash id, nk_hash ch, int kind, const float *target, float dur, + struct nka_ease e, int policy, int space, const float *init, float *out) +{ + static const unsigned char width[] = { 1, 2, 4, 1, 4 }; + int n = width[kind], i, same, cfg, done; + nk_hash key = nka__key(id, ch, kind); + struct nka__chan *c = (struct nka__chan *)nka__get(&a->chans, key); + if (!c) { + if (a->lazy && policy < NKA_POLICY_ADDITIVE && nka__near(target, init, n)) c = 0; + else c = (struct nka__chan *)nka__put(a, &a->chans, key, 0); + if (!c) { + for (i = 0; i < 4; ++i) out[i] = target[i]; + return; + } + for (i = 0; i < 4; ++i) c->cur[i] = c->from[i] = c->to[i] = c->req[i] = init[i]; + c->id = id; + c->ch = ch; + c->kind = (unsigned char)kind; + c->n = (unsigned char)n; + c->policy = (unsigned char)policy; + c->space = (unsigned char)space; + c->ease = e; + c->dur = NKA__MIN_DUR; + c->sleeping = 1; + } + c->seen = a->frame; + same = nka__near(c->req, target, n); + cfg = c->policy == policy && c->space == space && nka__ease_eq(c->ease, e); + done = c->sleeping || a->time - c->start >= c->dur; + if (c->pending && done) { + c->pending = 0; + nka__chan_eval(a, c); + for (i = 0; i < 4; ++i) c->req[i] = c->pend[i]; + nka__chan_set(a, c, c->pend, dur, e, policy, space); + } else if (policy == NKA_POLICY_QUEUE && !done) { + c->pending = (unsigned char)!same; + for (i = 0; i < 4; ++i) c->pend[i] = target[i]; + } else if (!same || (!cfg && policy < NKA_POLICY_ADDITIVE)) { + float to[4]; + nka__chan_eval(a, c); + for (i = 0; i < 4; ++i) { + c->req[i] = target[i]; + to[i] = policy == NKA_POLICY_ADDITIVE ? c->cur[i] + target[i] + : policy == NKA_POLICY_MULTIPLY ? c->cur[i] * target[i] : target[i]; + } + nka__chan_set(a, c, to, dur, e, policy, space); + if (policy == NKA_POLICY_CUT) { + for (i = 0; i < 4; ++i) c->cur[i] = c->from[i] = to[i]; + c->sleeping = 1; + } + } + nka__chan_eval(a, c); + for (i = 0; i < 4; ++i) out[i] = c->cur[i]; +} + +NKA_API float nka_tween_float(struct nka_context *a, nk_hash id, nk_hash ch, float target, float dur, + struct nka_ease ease, int policy, float init) +{ + float t[4] = { 0 }, s[4] = { 0 }, o[4]; + if (!a) return target; + t[0] = target; + s[0] = init; + nka__tween(a, id, ch, NKA__FLOAT, t, dur, ease, policy, 0, s, o); + return o[0]; +} + +NKA_API struct nk_vec2 nka_tween_vec2(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_vec2 target, + float dur, struct nka_ease ease, int policy, struct nk_vec2 init) +{ + float t[4] = { 0 }, s[4] = { 0 }, o[4]; + if (!a) return target; + t[0] = target.x; t[1] = target.y; + s[0] = init.x; s[1] = init.y; + nka__tween(a, id, ch, NKA__VEC2, t, dur, ease, policy, 0, s, o); + return nka__vec2(o[0], o[1]); +} + +NKA_API struct nka_vec4 nka_tween_vec4(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_vec4 target, + float dur, struct nka_ease ease, int policy, struct nka_vec4 init) +{ + float t[4], s[4], o[4]; + if (!a) return target; + t[0] = target.x; t[1] = target.y; t[2] = target.z; t[3] = target.w; + s[0] = init.x; s[1] = init.y; s[2] = init.z; s[3] = init.w; + nka__tween(a, id, ch, NKA__VEC4, t, dur, ease, policy, 0, s, o); + return nka__vec4(o[0], o[1], o[2], o[3]); +} + +NKA_API int nka_tween_int(struct nka_context *a, nk_hash id, nk_hash ch, int target, float dur, + struct nka_ease ease, int policy, int init) +{ + float t[4] = { 0 }, s[4] = { 0 }, o[4]; + if (!a) return target; + t[0] = (float)target; + s[0] = (float)init; + nka__tween(a, id, ch, NKA__INT, t, dur, ease, policy, 0, s, o); + return (int)nka__floor(o[0] + 0.5f); +} + +NKA_API struct nk_colorf nka_tween_color(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_colorf target, + float dur, struct nka_ease ease, int policy, int space, struct nk_colorf init) +{ + float t[4], s[4], o[4]; + if (!a) return target; + t[0] = target.r; t[1] = target.g; t[2] = target.b; t[3] = target.a; + s[0] = init.r; s[1] = init.g; s[2] = init.b; s[3] = init.a; + nka__tween(a, id, ch, NKA__COLOR, t, dur, ease, policy, space, s, o); + return nka__vc(nka__vec4(o[0], o[1], o[2], o[3])); +} + +static nk_hash nka__axis(nk_hash ch, int axis) { return nka_id_mix(ch, 0x41584953u + (nk_hash)axis); } + +NKA_API struct nk_vec2 nka_tween_vec2_per_axis(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_vec2 target, + float dur, struct nka_ease_axes ease, int policy) +{ + struct nk_vec2 v; + v.x = nka_tween_float(a, id, nka__axis(ch, 0), target.x, dur, ease.x, policy, 0); + v.y = nka_tween_float(a, id, nka__axis(ch, 1), target.y, dur, ease.y, policy, 0); + return v; +} + +NKA_API struct nka_vec4 nka_tween_vec4_per_axis(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_vec4 target, + float dur, struct nka_ease_axes ease, int policy) +{ + struct nka_vec4 v; + v.x = nka_tween_float(a, id, nka__axis(ch, 0), target.x, dur, ease.x, policy, 0); + v.y = nka_tween_float(a, id, nka__axis(ch, 1), target.y, dur, ease.y, policy, 0); + v.z = nka_tween_float(a, id, nka__axis(ch, 2), target.z, dur, ease.z, policy, 0); + v.w = nka_tween_float(a, id, nka__axis(ch, 3), target.w, dur, ease.w, policy, 0); + return v; +} + +/* Each component eases on its own, in the given space. */ +NKA_API struct nk_colorf nka_tween_color_per_axis(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_colorf target, + float dur, struct nka_ease_axes ease, int policy, int space) +{ + struct nka_vec4 w = nka__to_space(nka__cv(target), space), v; + v.x = nka_tween_float(a, id, nka__axis(ch, 4), w.x, dur, ease.x, policy, 0); + v.y = nka_tween_float(a, id, nka__axis(ch, 5), w.y, dur, ease.y, policy, 0); + v.z = nka_tween_float(a, id, nka__axis(ch, 6), w.z, dur, ease.z, policy, 0); + v.w = nka_tween_float(a, id, nka__axis(ch, 7), w.w, dur, ease.w, policy, 0); + return nka__vc(nka__from_space(v, space)); +} + +NKA_API void nka_set_anchor(struct nka_context *a, int space, struct nk_vec2 size) +{ + if (a && space >= 0 && space < NKA_ANCHOR_COUNT) a->anchors[space] = size; +} + +NKA_API struct nk_vec2 nka_anchor(const struct nka_context *a, int space) +{ + return a && space >= 0 && space < NKA_ANCHOR_COUNT ? a->anchors[space] : nka__vec2(0, 0); +} + +NKA_API float nka_tween_float_rel(struct nka_context *a, nk_hash id, nk_hash ch, float percent, float px_bias, + float dur, struct nka_ease ease, int policy, int anchor, int axis) +{ + struct nk_vec2 s = nka_anchor(a, anchor); + return nka_tween_float(a, id, ch, (axis ? s.y : s.x) * percent + px_bias, dur, ease, policy, 0); +} + +NKA_API struct nk_vec2 nka_tween_vec2_rel(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_vec2 percent, + struct nk_vec2 px_bias, float dur, struct nka_ease ease, int policy, int anchor) +{ + struct nk_vec2 s = nka_anchor(a, anchor); + return nka_tween_vec2(a, id, ch, nka__vec2(s.x * percent.x + px_bias.x, s.y * percent.y + px_bias.y), + dur, ease, policy, nka__vec2(0, 0)); +} + +NKA_API struct nka_vec4 nka_tween_vec4_rel(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_vec4 percent, + struct nka_vec4 px_bias, float dur, struct nka_ease ease, int policy, int anchor) +{ + struct nk_vec2 s = nka_anchor(a, anchor); + return nka_tween_vec4(a, id, ch, nka__vec4(s.x * percent.x + px_bias.x, s.y * percent.y + px_bias.y, + percent.z + px_bias.z, percent.w + px_bias.w), + dur, ease, policy, nka__vec4(0, 0, 0, 0)); +} + +NKA_API float nka_tween_float_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_float_resolver fn, + void *user, float dur, struct nka_ease ease, int policy) +{ + return nka_tween_float(a, id, ch, fn ? fn(user) : 0, dur, ease, policy, 0); +} + +NKA_API struct nk_vec2 nka_tween_vec2_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_vec2_resolver fn, + void *user, float dur, struct nka_ease ease, int policy) +{ + return nka_tween_vec2(a, id, ch, fn ? fn(user) : nka__vec2(0, 0), dur, ease, policy, nka__vec2(0, 0)); +} + +NKA_API struct nka_vec4 nka_tween_vec4_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_vec4_resolver fn, + void *user, float dur, struct nka_ease ease, int policy) +{ + struct nka_vec4 zero = nka__vec4(0, 0, 0, 0); + return nka_tween_vec4(a, id, ch, fn ? fn(user) : zero, dur, ease, policy, zero); +} + +NKA_API struct nk_colorf nka_tween_color_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_color_resolver fn, + void *user, float dur, struct nka_ease ease, int policy, int space) +{ + struct nk_colorf white = nka__vc(nka__vec4(1, 1, 1, 1)); + return nka_tween_color(a, id, ch, fn ? fn(user) : white, dur, ease, policy, space, white); +} + +NKA_API int nka_tween_int_resolved(struct nka_context *a, nk_hash id, nk_hash ch, nka_int_resolver fn, + void *user, float dur, struct nka_ease ease, int policy) +{ + return nka_tween_int(a, id, ch, fn ? fn(user) : 0, dur, ease, policy, 0); +} + +static void nka__rebase(struct nka_context *a, nk_hash id, nk_hash ch, int kind, const float *target) +{ + struct nka__chan *c; + float t, left; + int i; + if (!a) return; + c = (struct nka__chan *)nka__get(&a->chans, nka__key(id, ch, kind)); + if (!c) return; + t = c->sleeping ? 1 : nka__clamp01((float)((a->time - c->start) / c->dur)); + left = (1 - t) * c->dur; + nka__chan_eval(a, c); + for (i = 0; i < 4; ++i) { + c->from[i] = left > NKA__MIN_DUR ? c->cur[i] : target[i]; + c->cur[i] = c->from[i]; + c->to[i] = c->req[i] = target[i]; + } + c->start = a->time; + c->dur = left > NKA__MIN_DUR ? left : NKA__MIN_DUR; + c->sleeping = (unsigned char)(left <= NKA__MIN_DUR); +} + +NKA_API void nka_rebase_float(struct nka_context *a, nk_hash id, nk_hash ch, float target) +{ + float t[4] = { 0 }; + t[0] = target; + nka__rebase(a, id, ch, NKA__FLOAT, t); +} + +NKA_API void nka_rebase_vec2(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_vec2 target) +{ + float t[4] = { 0 }; + t[0] = target.x; t[1] = target.y; + nka__rebase(a, id, ch, NKA__VEC2, t); +} + +NKA_API void nka_rebase_vec4(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_vec4 target) +{ + float t[4]; + t[0] = target.x; t[1] = target.y; t[2] = target.z; t[3] = target.w; + nka__rebase(a, id, ch, NKA__VEC4, t); +} + +NKA_API void nka_rebase_color(struct nka_context *a, nk_hash id, nk_hash ch, struct nk_colorf target) +{ + float t[4]; + t[0] = target.r; t[1] = target.g; t[2] = target.b; t[3] = target.a; + nka__rebase(a, id, ch, NKA__COLOR, t); +} + +NKA_API void nka_rebase_int(struct nka_context *a, nk_hash id, nk_hash ch, int target) +{ + float t[4] = { 0 }; + t[0] = (float)target; + nka__rebase(a, id, ch, NKA__INT, t); +} + +/* ImAnimate. The state lives in the value: a change of direction or range + * picks up at the time where the curve passes through it. */ + +static float nka__unease(const struct nka_context *a, struct nka_ease e, float y) +{ + float lo = 0, hi = 1; + int i; + for (i = 0; i < 24; ++i) { + float mid = (lo + hi) / 2; + if (nka_eval(a, e, mid) < y) lo = mid; + else hi = mid; + } + return (lo + hi) / 2; +} + +NKA_API void nka_animate(struct nka_context *a, float from, float to, float ms, float *value, int curve) +{ + struct nka__anim *s; + struct nka_ease e = nka_ease(curve); + float lo = from < to ? from : to, hi = from < to ? to : from, v, p; + double now; + if (!a || !value) return; + v = *value; + s = (struct nka__anim *)nka__put(a, &a->anims, nka__ptr_key(value), 0); + if (!s) { *value = to; return; } + s->seen = a->frame; + if (v < lo || v > hi) { s->running = 0; *value = v < lo ? lo : hi; return; } + if (v == to || ms <= 0) { s->running = 0; *value = to; return; } + now = a->time * 1000; + if (!s->running || s->from != from || s->to != to) { + s->start = now - ms * nka__unease(a, e, (v - from) / (to - from)); + s->from = from; + s->to = to; + s->running = 1; + } + p = (float)((now - s->start) / ms); + if (p >= 1) { s->running = 0; *value = to; return; } + v = from + (to - from) * nka_eval(a, e, p); + *value = v < lo ? lo : (v > hi ? hi : v); + a->busy = 1; +} + +/* Motion paths */ + +NKA_API struct nk_vec2 nka_bezier_quadratic(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, float t) +{ + float u = 1 - t, uu = u * u, ut2 = 2 * u * t, tt = t * t; + return nka__vec2(uu * p0.x + ut2 * p1.x + tt * p2.x, uu * p0.y + ut2 * p1.y + tt * p2.y); +} + +NKA_API struct nk_vec2 nka_bezier_quadratic_deriv(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, float t) +{ + float u = 1 - t; + return nka__vec2(2 * u * (p1.x - p0.x) + 2 * t * (p2.x - p1.x), 2 * u * (p1.y - p0.y) + 2 * t * (p2.y - p1.y)); +} + +NKA_API struct nk_vec2 nka_bezier_cubic(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, struct nk_vec2 p3, float t) +{ + float u = 1 - t, uu = u * u, tt = t * t; + float k0 = uu * u, k1 = 3 * uu * t, k2 = 3 * u * tt, k3 = tt * t; + return nka__vec2(k0 * p0.x + k1 * p1.x + k2 * p2.x + k3 * p3.x, k0 * p0.y + k1 * p1.y + k2 * p2.y + k3 * p3.y); +} + +NKA_API struct nk_vec2 nka_bezier_cubic_deriv(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, struct nk_vec2 p3, float t) +{ + float u = 1 - t, uu = u * u, tt = t * t; + float k0 = 3 * uu, k1 = 6 * u * t, k2 = 3 * tt; + return nka__vec2(k0 * (p1.x - p0.x) + k1 * (p2.x - p1.x) + k2 * (p3.x - p2.x), + k0 * (p1.y - p0.y) + k1 * (p2.y - p1.y) + k2 * (p3.y - p2.y)); +} + +NKA_API struct nk_vec2 nka_catmull_rom(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, struct nk_vec2 p3, + float t, float tension) +{ + float t2 = t * t, t3 = t2 * t, s = (1 - tension) / 2; + float h1 = -s * t3 + 2 * s * t2 - s * t; + float h2 = (2 - s) * t3 + (s - 3) * t2 + 1; + float h3 = (s - 2) * t3 + (3 - 2 * s) * t2 + s * t; + float h4 = s * t3 - s * t2; + return nka__vec2(h1 * p0.x + h2 * p1.x + h3 * p2.x + h4 * p3.x, h1 * p0.y + h2 * p1.y + h3 * p2.y + h4 * p3.y); +} + +NKA_API struct nk_vec2 nka_catmull_rom_deriv(struct nk_vec2 p0, struct nk_vec2 p1, struct nk_vec2 p2, struct nk_vec2 p3, + float t, float tension) +{ + float t2 = t * t, s = (1 - tension) / 2; + float d1 = -3 * s * t2 + 4 * s * t - s; + float d2 = 3 * (2 - s) * t2 + 2 * (s - 3) * t; + float d3 = 3 * (s - 2) * t2 + 2 * (3 - 2 * s) * t + s; + float d4 = 3 * s * t2 - 2 * s * t; + return nka__vec2(d1 * p0.x + d2 * p1.x + d3 * p2.x + d4 * p3.x, d1 * p0.y + d2 * p1.y + d3 * p2.y + d4 * p3.y); +} + +/* A bijection, so ids 0 and 1 stay apart in a map that reads key 0 as 1. */ +static nk_hash nka__path_key(nk_hash id) { return nka__fmix(id ^ NKA__PATH_KEY); } + +static struct nka__path_data *nka__path_get(const struct nka_context *a, nk_hash id) +{ + return a ? (struct nka__path_data *)nka__get(&a->path_map, nka__path_key(id)) : 0; +} + +static struct nk_vec2 nka__path_seg_at(const struct nka__path_seg *s, float t) +{ + switch (s->type) { + case NKA_SEG_LINE: return nka__vec2(nka__lerp(s->p[0].x, s->p[1].x, t), nka__lerp(s->p[0].y, s->p[1].y, t)); + case NKA_SEG_QUADRATIC_BEZIER: return nka_bezier_quadratic(s->p[0], s->p[1], s->p[2], t); + case NKA_SEG_CUBIC_BEZIER: return nka_bezier_cubic(s->p[0], s->p[1], s->p[2], s->p[3], t); + default: return nka_catmull_rom(s->p[0], s->p[1], s->p[2], s->p[3], t, s->tension); + } +} + +static struct nk_vec2 nka__path_seg_deriv(const struct nka__path_seg *s, float t) +{ + switch (s->type) { + case NKA_SEG_LINE: return nka__vec2(s->p[1].x - s->p[0].x, s->p[1].y - s->p[0].y); + case NKA_SEG_QUADRATIC_BEZIER: return nka_bezier_quadratic_deriv(s->p[0], s->p[1], s->p[2], t); + case NKA_SEG_CUBIC_BEZIER: return nka_bezier_cubic_deriv(s->p[0], s->p[1], s->p[2], s->p[3], t); + default: return nka_catmull_rom_deriv(s->p[0], s->p[1], s->p[2], s->p[3], t, s->tension); + } +} + +static float nka__path_dist(struct nk_vec2 p, struct nk_vec2 q) +{ + float dx = q.x - p.x, dy = q.y - p.y; + return nka__sqrt(dx * dx + dy * dy); +} + +static float nka__path_seg_len(const struct nka__path_seg *s) +{ + struct nk_vec2 prev = nka__path_seg_at(s, 0), cur; + float len = 0; + int i; + for (i = 1; i <= 16; ++i) { + cur = nka__path_seg_at(s, (float)i / 16); + len += nka__path_dist(prev, cur); + prev = cur; + } + return len; +} + +static int nka__path_find(const struct nka__path_data *p, float t, float *local) +{ + float target = t * p->seg_len, acc = 0; + int i; + *local = 0; + if (!p->n) return -1; + if (t <= 0) return 0; + if (t < 1) { + for (i = 0; i < p->n; ++i) { + float len = p->segs[i].length; + if (acc + len >= target) { + if (len > 0) *local = (target - acc) / len; + return i; + } + acc += len; + } + } + *local = 1; + return p->n - 1; +} + +static struct nk_vec2 nka__path_at(const struct nka__path_data *p, float t) +{ + float lt; + int i = nka__path_find(p, t, <); + return i < 0 ? p->start : nka__path_seg_at(&p->segs[i], lt); +} + +/* Where the derivative vanishes, as at a control point lying on its end point, the + * direction is the derivative's just inside the segment. */ +static struct nk_vec2 nka__path_dir(const struct nka__path_data *p, float t) +{ + struct nk_vec2 d = nka__vec2(1, 0); + float lt, h, len = 1; + int i = nka__path_find(p, t, <); + if (i >= 0) { + d = nka__path_seg_deriv(&p->segs[i], lt); + len = nka__sqrt(d.x * d.x + d.y * d.y); + for (h = 1e-3f; len <= 1e-6f && h < 0.5f; h *= 10) { + d = nka__path_seg_deriv(&p->segs[i], lt < 0.5f ? lt + h : lt - h); + len = nka__sqrt(d.x * d.x + d.y * d.y); + } + } + return len > 1e-6f ? nka__vec2(d.x / len, d.y / len) : nka__vec2(1, 0); +} + +static void nka__path_lut(struct nka_context *a, struct nka__path_data *p, int sub) +{ + struct nk_vec2 prev, cur; + float acc = 0; + int i; + if (sub < 1) sub = 64; + if (sub > 1 << 24) sub = 1 << 24; + p->lut_n = 0; + p->length = p->seg_len; + if (!p->n || p->seg_len <= 0) return; + if (sub + 1 > p->lut_cap) { + float *lut = (float *)nka__alloc(a, (nk_size)(sub + 1) * sizeof(float)); + if (!lut) return; + nka__free(a, p->lut); + p->lut = lut; + p->lut_cap = sub + 1; + } + p->lut[0] = 0; + prev = nka__path_at(p, 0); + for (i = 1; i <= sub; ++i) { + cur = nka__path_at(p, (float)i / (float)sub); + acc += nka__path_dist(prev, cur); + p->lut[i] = acc; + prev = cur; + } + if (acc <= 0) return; + p->lut_n = sub + 1; + p->length = acc; +} + +static float nka__path_d2t(const struct nka__path_data *p, float d) +{ + int lo = 0, hi = p->lut_n - 1; + float d0, d1, t0, t1; + if (p->lut_n < 2) return p->length > 0 ? nka__clamp01(d / p->length) : 0; + if (d <= 0) return 0; + if (d >= p->length) return 1; + while (lo < hi - 1) { + int mid = (lo + hi) / 2; + if (p->lut[mid] < d) lo = mid; + else hi = mid; + } + d0 = p->lut[lo]; + d1 = p->lut[hi]; + t0 = (float)lo / (float)(p->lut_n - 1); + t1 = (float)hi / (float)(p->lut_n - 1); + if (d1 - d0 <= 0) return t0; + return t0 + (t1 - t0) * ((d - d0) / (d1 - d0)); +} + +static struct nka__path_seg *nka__path_push(struct nka_context *a, int type, struct nk_vec2 end) +{ + struct nka__path_data *b; + struct nka__path_seg *s; + if (!a || !a->path_building || a->path_failed) return 0; + b = &a->path_new; + if (b->n == b->cap) { + int cap = b->cap ? b->cap * 2 : 8; + s = (struct nka__path_seg *)nka__alloc(a, (nk_size)cap * sizeof *s); + if (!s) { + a->path_failed = 1; + return 0; + } + if (b->n) nka__copy(s, b->segs, (nk_size)b->n * sizeof *s); + nka__free(a, b->segs); + b->segs = s; + b->cap = cap; + } + s = &b->segs[b->n++]; + nka__zero(s, sizeof *s); + s->type = type; + s->p[0] = a->path_cur; + a->path_prev = a->path_cur; + a->path_cur = end; + return s; +} + +NKA_API void nka_path_begin(struct nka_context *a, nk_hash path_id, struct nk_vec2 start) +{ + if (!a) return; + a->path_building = 1; + a->path_failed = 0; + a->path_id = path_id; + a->path_new.n = 0; + a->path_new.start = start; + a->path_cur = a->path_prev = start; +} + +NKA_API void nka_path_line_to(struct nka_context *a, struct nk_vec2 end) +{ + struct nka__path_seg *s = nka__path_push(a, NKA_SEG_LINE, end); + if (s) s->p[1] = end; +} + +NKA_API void nka_path_quadratic_to(struct nka_context *a, struct nk_vec2 ctrl, struct nk_vec2 end) +{ + struct nka__path_seg *s = nka__path_push(a, NKA_SEG_QUADRATIC_BEZIER, end); + if (!s) return; + s->p[1] = ctrl; + s->p[2] = end; +} + +NKA_API void nka_path_cubic_to(struct nka_context *a, struct nk_vec2 ctrl1, struct nk_vec2 ctrl2, struct nk_vec2 end) +{ + struct nka__path_seg *s = nka__path_push(a, NKA_SEG_CUBIC_BEZIER, end); + if (!s) return; + s->p[1] = ctrl1; + s->p[2] = ctrl2; + s->p[3] = end; +} + +NKA_API void nka_path_catmull_to(struct nka_context *a, struct nk_vec2 end, float tension) +{ + struct nka__path_seg *s; + struct nk_vec2 before; + if (!a) return; + before = a->path_new.n ? a->path_prev : a->path_cur; + s = nka__path_push(a, NKA_SEG_CATMULL_ROM, end); + if (!s) return; + s->p[1] = s->p[0]; + s->p[0] = before; + s->p[2] = s->p[3] = end; + s->tension = tension; + if (a->path_new.n >= 2 && a->path_new.segs[a->path_new.n - 2].type == NKA_SEG_CATMULL_ROM) + a->path_new.segs[a->path_new.n - 2].p[3] = end; +} + +NKA_API void nka_path_close(struct nka_context *a) +{ + if (!a || !a->path_building) return; + if (a->path_cur.x != a->path_new.start.x || a->path_cur.y != a->path_new.start.y) + nka_path_line_to(a, a->path_new.start); +} + +/* The path takes the builder's array and leaves its old one for the next build. */ +NKA_API void nka_path_end(struct nka_context *a) +{ + struct nka__path_data *b, *p; + struct nka__path_seg *segs; + int i, cap; + if (!a || !a->path_building) return; + a->path_building = 0; + b = &a->path_new; + p = a->path_failed ? 0 : (struct nka__path_data *)nka__put(a, &a->path_map, nka__path_key(a->path_id), 0); + if (!p) { + b->n = 0; + return; + } + b->seg_len = 0; + for (i = 0; i < b->n; ++i) { + b->segs[i].length = nka__path_seg_len(&b->segs[i]); + b->seg_len += b->segs[i].length; + } + segs = p->segs; + cap = p->cap; + p->segs = b->segs; + p->cap = b->cap; + p->n = b->n; + p->start = b->start; + p->seg_len = p->length = b->seg_len; + p->lut_n = 0; + b->segs = segs; + b->cap = cap; + b->n = 0; +} + +NKA_API int nka_path_exists(const struct nka_context *a, nk_hash path_id) { return nka__path_get(a, path_id) != 0; } + +NKA_API float nka_path_length(const struct nka_context *a, nk_hash path_id) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + return p ? p->length : 0; +} + +NKA_API struct nk_vec2 nka_path_evaluate(const struct nka_context *a, nk_hash path_id, float t) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + return p ? nka__path_at(p, t) : nka__vec2(0, 0); +} + +NKA_API struct nk_vec2 nka_path_tangent(const struct nka_context *a, nk_hash path_id, float t) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + return p ? nka__path_dir(p, t) : nka__vec2(1, 0); +} + +NKA_API float nka_path_angle(const struct nka_context *a, nk_hash path_id, float t) +{ + struct nk_vec2 d = nka_path_tangent(a, path_id, t); + return nka__atan2(d.y, d.x); +} + +NKA_API void nka_path_build_arc_lut(struct nka_context *a, nk_hash path_id, int subdivisions) +{ + struct nka__path_data *p = nka__path_get(a, path_id); + if (p) nka__path_lut(a, p, subdivisions); +} + +NKA_API int nka_path_has_arc_lut(const struct nka_context *a, nk_hash path_id) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + return p && p->lut_n >= 2; +} + +NKA_API float nka_path_distance_to_t(const struct nka_context *a, nk_hash path_id, float distance) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + return p ? nka__path_d2t(p, distance) : 0; +} + +NKA_API struct nk_vec2 nka_path_evaluate_at_distance(const struct nka_context *a, nk_hash path_id, float distance) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + return p ? nka__path_at(p, nka__path_d2t(p, distance)) : nka__vec2(0, 0); +} + +NKA_API struct nk_vec2 nka_path_tangent_at_distance(const struct nka_context *a, nk_hash path_id, float distance) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + return p ? nka__path_dir(p, nka__path_d2t(p, distance)) : nka__vec2(1, 0); +} + +NKA_API float nka_path_angle_at_distance(const struct nka_context *a, nk_hash path_id, float distance) +{ + struct nk_vec2 d = nka_path_tangent_at_distance(a, path_id, distance); + return nka__atan2(d.y, d.x); +} + +static nk_hash nka__path_ch(nk_hash ch, nk_hash salt) { return nka_id_mix(ch, salt); } + +/* Additive from 0 adds 1 a single time and, unlike a crossfade, does not start over + * when the ease changes: ImAnim's progress along a path. */ +static float nka__path_progress(struct nka_context *a, nk_hash id, nk_hash ch, float dur, struct nka_ease ease) +{ + return nka_tween_float(a, id, ch, 1, dur, ease, NKA_POLICY_ADDITIVE, 0); +} + +static float nka__path_arc_t(const struct nka__path_data *p, float t, int arc) +{ + return arc && p->lut_n >= 2 ? nka__path_d2t(p, t * p->length) : t; +} + +NKA_API struct nk_vec2 nka_tween_path(struct nka_context *a, nk_hash id, nk_hash ch, nk_hash path_id, float dur, + struct nka_ease ease, int policy) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + float k; + (void)policy; + if (!p || !p->n) return nka__vec2(0, 0); + k = nka__path_progress(a, id, nka__path_ch(ch, NKA__PATH_POS), dur, ease); + return nka__path_at(p, nka__path_arc_t(p, k, 1)); +} + +NKA_API float nka_tween_path_angle(struct nka_context *a, nk_hash id, nk_hash ch, nk_hash path_id, float dur, + struct nka_ease ease, int policy) +{ + const struct nka__path_data *p = nka__path_get(a, path_id); + struct nk_vec2 d; + float k; + (void)policy; + if (!p || !p->n) return 0; + k = nka__path_progress(a, id, nka__path_ch(ch, NKA__PATH_ANGLE), dur, ease); + d = nka__path_dir(p, nka__path_arc_t(p, k, 1)); + return nka__atan2(d.y, d.x); +} + +/* Morphing */ + +NKA_API struct nka_morph_opts nka_morph_opts_default(void) +{ + struct nka_morph_opts o; + o.samples = 64; + o.match_endpoints = 1; + o.use_arc_length = 1; + return o; +} + +/* 0 to 1, and 0 for NaN, which reaches a float to int conversion below. */ +static float nka__path_sat(float v) { return v > 0 ? (v < 1 ? v : 1) : 0; } + +static struct nk_vec2 nka__path_mix(struct nk_vec2 p, struct nk_vec2 q, float k) +{ + return nka__vec2(p.x + (q.x - p.x) * k, p.y + (q.y - p.y) * k); +} + +static struct nka__path_data *nka__path_morph_get(struct nka_context *a, nk_hash id, int arc) +{ + struct nka__path_data *p = nka__path_get(a, id); + if (p && arc && p->lut_n < 2) nka__path_lut(a, p, 64); + return p; +} + +/* Point i of the n that ImAnim resamples a path to; a missing or empty path is the origin. */ +static struct nk_vec2 nka__path_sample(const struct nka__path_data *p, int i, int n, int arc) +{ + if (!p || !p->n) return nka__vec2(0, 0); + return nka__path_at(p, nka__path_arc_t(p, (float)i / (float)(n - 1), arc)); +} + +/* Only the paths the blend shows. Alone at blend 0 or 1, a path is still timed by arc + * length when asked, or the morph would jump as the blend leaves either end. */ +static void nka__path_pair_of(struct nka_context *a, nk_hash path_a, nk_hash path_b, float blend, + const struct nka_morph_opts *opts, struct nka__path_pair *m) +{ + struct nka_morph_opts o = opts ? *opts : nka_morph_opts_default(); + m->n = o.samples > 2 ? o.samples : 2; + m->arc = o.use_arc_length != 0; + m->a = blend < 1 ? nka__path_morph_get(a, path_a, m->arc) : 0; + m->b = blend > 0 ? nka__path_morph_get(a, path_b, m->arc) : 0; +} + +static struct nk_vec2 nka__path_morph_at(const struct nka__path_pair *m, float t, float blend) +{ + float s, f; + int i, n = m->n; + if (t <= 0) return nka__path_mix(nka__path_sample(m->a, 0, n, m->arc), nka__path_sample(m->b, 0, n, m->arc), blend); + if (t >= 1) + return nka__path_mix(nka__path_sample(m->a, n - 1, n, m->arc), nka__path_sample(m->b, n - 1, n, m->arc), blend); + s = t * (float)(n - 1); + i = (int)s; + f = s - (float)i; + if (i >= n - 1) { + i = n - 2; + f = 1; + } + return nka__path_mix(nka__path_mix(nka__path_sample(m->a, i, n, m->arc), nka__path_sample(m->a, i + 1, n, m->arc), f), + nka__path_mix(nka__path_sample(m->b, i, n, m->arc), nka__path_sample(m->b, i + 1, n, m->arc), f), + blend); +} + +NKA_API struct nk_vec2 nka_path_morph(struct nka_context *a, nk_hash path_a, nk_hash path_b, float t, float blend, + const struct nka_morph_opts *opts) +{ + struct nka__path_pair m; + const struct nka__path_data *p; + t = nka__path_sat(t); + blend = nka__path_sat(blend); + nka__path_pair_of(a, path_a, path_b, blend, opts, &m); + if (blend > 0 && blend < 1) return nka__path_morph_at(&m, t, blend); + p = blend <= 0 ? m.a : m.b; + return p ? nka__path_at(p, nka__path_arc_t(p, t, m.arc)) : nka__vec2(0, 0); +} + +NKA_API struct nk_vec2 nka_path_morph_tangent(struct nka_context *a, nk_hash path_a, nk_hash path_b, float t, float blend, + const struct nka_morph_opts *opts) +{ + struct nka__path_pair m; + const struct nka__path_data *p; + struct nk_vec2 p0, p1, d; + float len; + t = nka__path_sat(t); + blend = nka__path_sat(blend); + nka__path_pair_of(a, path_a, path_b, blend, opts, &m); + if (blend <= 0 || blend >= 1) { + p = blend <= 0 ? m.a : m.b; + return p ? nka__path_dir(p, nka__path_arc_t(p, t, m.arc)) : nka__vec2(1, 0); + } + p0 = nka__path_morph_at(&m, t - 0.001f > 0 ? t - 0.001f : 0, blend); + p1 = nka__path_morph_at(&m, t + 0.001f < 1 ? t + 0.001f : 1, blend); + d = nka__vec2(p1.x - p0.x, p1.y - p0.y); + len = nka__sqrt(d.x * d.x + d.y * d.y); + return len > 1e-6f ? nka__vec2(d.x / len, d.y / len) : nka__vec2(1, 0); +} + +NKA_API float nka_path_morph_angle(struct nka_context *a, nk_hash path_a, nk_hash path_b, float t, float blend, + const struct nka_morph_opts *opts) +{ + struct nk_vec2 d = nka_path_morph_tangent(a, path_a, path_b, t, blend, opts); + return nka__atan2(d.y, d.x); +} + +NKA_API struct nk_vec2 nka_tween_path_morph(struct nka_context *a, nk_hash id, nk_hash ch, nk_hash path_a, nk_hash path_b, + float target_blend, float dur, struct nka_ease path_ease, + struct nka_ease morph_ease, int policy, const struct nka_morph_opts *opts) +{ + float k, blend; + if (!a) return nka__vec2(0, 0); + k = nka__path_progress(a, id, nka__path_ch(ch, NKA__PATH_MORPH), dur, path_ease); + blend = nka_tween_float(a, id, nka__path_ch(ch, NKA__PATH_BLEND), target_blend, dur, morph_ease, + policy == NKA_POLICY_CUT ? NKA_POLICY_CUT : NKA_POLICY_CROSSFADE, 0); + return nka_path_morph(a, path_a, path_b, k, blend, opts); +} + +NKA_API float nka_get_morph_blend(const struct nka_context *a, nk_hash id, nk_hash ch) +{ + const struct nka__chan *c; + if (!a) return 0; + c = (const struct nka__chan *)nka__get(&a->chans, nka__key(id, nka__path_ch(ch, NKA__PATH_BLEND), NKA__FLOAT)); + return c ? c->cur[0] : 0; +} + +NKA_API void nka_transform_quad(struct nk_vec2 *quad, struct nk_vec2 center, float angle, struct nk_vec2 translation) +{ + float c = nka__cos(angle), s = nka__sin(angle); + int i; + if (!quad) return; + for (i = 0; i < 4; ++i) { + float x = quad[i].x - center.x, y = quad[i].y - center.y; + quad[i].x = x * c - y * s + center.x + translation.x; + quad[i].y = x * s + y * c + center.y + translation.y; + } +} + +/* Up, away from the path, is (sin, -cos) on a y-down screen. */ +NKA_API void nka_make_glyph_quad(struct nk_vec2 *quad, struct nk_vec2 pos, float angle, float glyph_width, + float glyph_height, float baseline_offset) +{ + float c = nka__cos(angle), s = nka__sin(angle), hw = glyph_width * 0.5f; + float ox = s * baseline_offset, oy = -c * baseline_offset, ux = s * glyph_height, uy = -c * glyph_height; + if (!quad) return; + quad[0] = nka__vec2(pos.x - c * hw + ox, pos.y - s * hw + oy); + quad[1] = nka__vec2(pos.x + c * hw + ox, pos.y + s * hw + oy); + quad[2] = nka__vec2(pos.x + c * hw + ox + ux, pos.y + s * hw + oy + uy); + quad[3] = nka__vec2(pos.x - c * hw + ox + ux, pos.y - s * hw + oy + uy); +} + +static void nka__path_free(struct nka_context *a) +{ + int i; + for (i = 0; i < a->path_map.cap; ++i) { + struct nka__path_data *p = (struct nka__path_data *)nka__at(&a->path_map, i); + if (!a->path_map.keys[i]) continue; + nka__free(a, p->segs); + nka__free(a, p->lut); + } + nka__map_free(a, &a->path_map); + nka__free(a, a->path_new.segs); +} + +/* Clips */ + +static float nka__clip_clampf(float v, float lo, float hi) { return v < lo ? lo : (v > hi ? hi : v); } + +static int nka__clip_toint(float f) +{ + if (f != f) return 0; + if (f >= 2147483520.0f) return 2147483520; + if (f <= -2147483648.0f) return -NKA__CLIP_INT_MAX - 1; + return (int)f; +} + +static int nka__clip_s32(nk_uint u) { return u <= 0x7fffffffu ? (int)u : -(int)~u - 1; } + +/* x to y rounded half up, in doubles so ints of any size land exactly on their keys. */ +static int nka__clip_lerpi(int x, int y, float w) +{ + double d = ((double)y - (double)x) * (double)w + 0.5; + long long v; + if (!(d > -1e17 && d < 1e17)) return w > 0.5f ? y : x; + v = (long long)d; + if ((double)v > d) --v; + v += x; + return (int)(v > NKA__CLIP_INT_MAX ? NKA__CLIP_INT_MAX : (v < -NKA__CLIP_INT_MAX - 1 ? -NKA__CLIP_INT_MAX - 1 : v)); +} + +NKA_API struct nka_variation_float nka_varf_none(void) +{ + struct nka_variation_float v; + v.mode = NKA_VAR_NONE; + v.amount = 0; + v.min_clamp = -NKA__CLIP_FLT_MAX; + v.max_clamp = NKA__CLIP_FLT_MAX; + v.seed = 0; + v.callback = 0; + v.user = 0; + return v; +} + +static struct nka_variation_float nka__clip_varf_of(int mode, float amount) +{ + struct nka_variation_float v = nka_varf_none(); + v.mode = mode; + v.amount = amount; + return v; +} + +NKA_API struct nka_variation_float nka_varf_inc(float amount) { return nka__clip_varf_of(NKA_VAR_INCREMENT, amount); } +NKA_API struct nka_variation_float nka_varf_dec(float amount) { return nka__clip_varf_of(NKA_VAR_DECREMENT, amount); } +NKA_API struct nka_variation_float nka_varf_mul(float factor) { return nka__clip_varf_of(NKA_VAR_MULTIPLY, factor); } +NKA_API struct nka_variation_float nka_varf_rand(float range) { return nka__clip_varf_of(NKA_VAR_RANDOM, range); } +NKA_API struct nka_variation_float nka_varf_rand_abs(float range) { return nka__clip_varf_of(NKA_VAR_RANDOM_ABS, range); } +NKA_API struct nka_variation_float nka_varf_pingpong(float amount) { return nka__clip_varf_of(NKA_VAR_PINGPONG, amount); } + +NKA_API struct nka_variation_float nka_varf_fn(nka_variation_float_fn fn, void *user) +{ + struct nka_variation_float v = nka__clip_varf_of(NKA_VAR_CALLBACK, 0); + v.callback = fn; + v.user = user; + return v; +} + +NKA_API struct nka_variation_float nka_varf_clamp(struct nka_variation_float v, float lo, float hi) +{ + v.min_clamp = lo; + v.max_clamp = hi; + return v; +} + +NKA_API struct nka_variation_float nka_varf_seed(struct nka_variation_float v, nk_uint seed) { v.seed = seed; return v; } + +NKA_API struct nka_variation_int nka_vari_none(void) +{ + struct nka_variation_int v; + v.mode = NKA_VAR_NONE; + v.amount = 0; + v.min_clamp = -NKA__CLIP_INT_MAX - 1; + v.max_clamp = NKA__CLIP_INT_MAX; + v.seed = 0; + v.callback = 0; + v.user = 0; + return v; +} + +static struct nka_variation_int nka__clip_vari_of(int mode, int amount) +{ + struct nka_variation_int v = nka_vari_none(); + v.mode = mode; + v.amount = amount; + return v; +} + +NKA_API struct nka_variation_int nka_vari_inc(int amount) { return nka__clip_vari_of(NKA_VAR_INCREMENT, amount); } +NKA_API struct nka_variation_int nka_vari_dec(int amount) { return nka__clip_vari_of(NKA_VAR_DECREMENT, amount); } +NKA_API struct nka_variation_int nka_vari_rand(int range) { return nka__clip_vari_of(NKA_VAR_RANDOM, range); } + +NKA_API struct nka_variation_int nka_vari_fn(nka_variation_int_fn fn, void *user) +{ + struct nka_variation_int v = nka__clip_vari_of(NKA_VAR_CALLBACK, 0); + v.callback = fn; + v.user = user; + return v; +} + +NKA_API struct nka_variation_int nka_vari_clamp(struct nka_variation_int v, int lo, int hi) +{ + v.min_clamp = lo; + v.max_clamp = hi; + return v; +} + +NKA_API struct nka_variation_int nka_vari_seed(struct nka_variation_int v, nk_uint seed) { v.seed = seed; return v; } + +NKA_API struct nka_variation_vec2 nka_varv2_none(void) +{ + struct nka_variation_vec2 v; + v.mode = NKA_VAR_NONE; + v.amount = nka__vec2(0, 0); + v.min_clamp = nka__vec2(-NKA__CLIP_FLT_MAX, -NKA__CLIP_FLT_MAX); + v.max_clamp = nka__vec2(NKA__CLIP_FLT_MAX, NKA__CLIP_FLT_MAX); + v.seed = 0; + v.callback = 0; + v.user = 0; + v.x = v.y = nka_varf_none(); + return v; +} + +static struct nka_variation_vec2 nka__clip_varv2_of(int mode, float x, float y) +{ + struct nka_variation_vec2 v = nka_varv2_none(); + v.mode = mode; + v.amount = nka__vec2(x, y); + return v; +} + +NKA_API struct nka_variation_vec2 nka_varv2_inc(float x, float y) { return nka__clip_varv2_of(NKA_VAR_INCREMENT, x, y); } +NKA_API struct nka_variation_vec2 nka_varv2_dec(float x, float y) { return nka__clip_varv2_of(NKA_VAR_DECREMENT, x, y); } +NKA_API struct nka_variation_vec2 nka_varv2_mul(float factor) { return nka__clip_varv2_of(NKA_VAR_MULTIPLY, factor, factor); } +NKA_API struct nka_variation_vec2 nka_varv2_rand(float x, float y) { return nka__clip_varv2_of(NKA_VAR_RANDOM, x, y); } + +NKA_API struct nka_variation_vec2 nka_varv2_fn(nka_variation_vec2_fn fn, void *user) +{ + struct nka_variation_vec2 v = nka__clip_varv2_of(NKA_VAR_CALLBACK, 0, 0); + v.callback = fn; + v.user = user; + return v; +} + +NKA_API struct nka_variation_vec2 nka_varv2_axis(struct nka_variation_float x, struct nka_variation_float y) +{ + struct nka_variation_vec2 v = nka_varv2_none(); + v.x = x; + v.y = y; + return v; +} + +NKA_API struct nka_variation_vec2 nka_varv2_clamp(struct nka_variation_vec2 v, struct nk_vec2 lo, struct nk_vec2 hi) +{ + v.min_clamp = lo; + v.max_clamp = hi; + return v; +} + +NKA_API struct nka_variation_vec2 nka_varv2_seed(struct nka_variation_vec2 v, nk_uint seed) { v.seed = seed; return v; } + +NKA_API struct nka_variation_vec4 nka_varv4_none(void) +{ + struct nka_variation_vec4 v; + v.mode = NKA_VAR_NONE; + v.amount = nka__vec4(0, 0, 0, 0); + v.min_clamp = nka__vec4(-NKA__CLIP_FLT_MAX, -NKA__CLIP_FLT_MAX, -NKA__CLIP_FLT_MAX, -NKA__CLIP_FLT_MAX); + v.max_clamp = nka__vec4(NKA__CLIP_FLT_MAX, NKA__CLIP_FLT_MAX, NKA__CLIP_FLT_MAX, NKA__CLIP_FLT_MAX); + v.seed = 0; + v.callback = 0; + v.user = 0; + v.x = v.y = v.z = v.w = nka_varf_none(); + return v; +} + +static struct nka_variation_vec4 nka__clip_varv4_of(int mode, struct nka_vec4 amount) +{ + struct nka_variation_vec4 v = nka_varv4_none(); + v.mode = mode; + v.amount = amount; + return v; +} + +NKA_API struct nka_variation_vec4 nka_varv4_inc(float x, float y, float z, float w) +{ + return nka__clip_varv4_of(NKA_VAR_INCREMENT, nka__vec4(x, y, z, w)); +} + +NKA_API struct nka_variation_vec4 nka_varv4_dec(float x, float y, float z, float w) +{ + return nka__clip_varv4_of(NKA_VAR_DECREMENT, nka__vec4(x, y, z, w)); +} + +NKA_API struct nka_variation_vec4 nka_varv4_mul(float factor) +{ + return nka__clip_varv4_of(NKA_VAR_MULTIPLY, nka__vec4(factor, factor, factor, factor)); +} + +NKA_API struct nka_variation_vec4 nka_varv4_rand(float x, float y, float z, float w) +{ + return nka__clip_varv4_of(NKA_VAR_RANDOM, nka__vec4(x, y, z, w)); +} + +NKA_API struct nka_variation_vec4 nka_varv4_fn(nka_variation_vec4_fn fn, void *user) +{ + struct nka_variation_vec4 v = nka__clip_varv4_of(NKA_VAR_CALLBACK, nka__vec4(0, 0, 0, 0)); + v.callback = fn; + v.user = user; + return v; +} + +NKA_API struct nka_variation_vec4 nka_varv4_axis(struct nka_variation_float x, struct nka_variation_float y, + struct nka_variation_float z, struct nka_variation_float w) +{ + struct nka_variation_vec4 v = nka_varv4_none(); + v.x = x; + v.y = y; + v.z = z; + v.w = w; + return v; +} + +NKA_API struct nka_variation_vec4 nka_varv4_clamp(struct nka_variation_vec4 v, struct nka_vec4 lo, struct nka_vec4 hi) +{ + v.min_clamp = lo; + v.max_clamp = hi; + return v; +} + +NKA_API struct nka_variation_vec4 nka_varv4_seed(struct nka_variation_vec4 v, nk_uint seed) { v.seed = seed; return v; } + +NKA_API struct nka_variation_color nka_varc_none(void) +{ + struct nka_variation_color v; + v.mode = NKA_VAR_NONE; + v.amount = nka__vec4(0, 0, 0, 0); + v.min_clamp = nka__vec4(-NKA__CLIP_FLT_MAX, -NKA__CLIP_FLT_MAX, -NKA__CLIP_FLT_MAX, -NKA__CLIP_FLT_MAX); + v.max_clamp = nka__vec4(NKA__CLIP_FLT_MAX, NKA__CLIP_FLT_MAX, NKA__CLIP_FLT_MAX, NKA__CLIP_FLT_MAX); + v.space = NKA_COL_OKLAB; + v.seed = 0; + v.callback = 0; + v.user = 0; + v.r = v.g = v.b = v.a = nka_varf_none(); + return v; +} + +static struct nka_variation_color nka__clip_varc_of(int mode, struct nka_vec4 amount) +{ + struct nka_variation_color v = nka_varc_none(); + v.mode = mode; + v.amount = amount; + return v; +} + +NKA_API struct nka_variation_color nka_varc_inc(float r, float g, float b, float alpha) +{ + return nka__clip_varc_of(NKA_VAR_INCREMENT, nka__vec4(r, g, b, alpha)); +} + +NKA_API struct nka_variation_color nka_varc_dec(float r, float g, float b, float alpha) +{ + return nka__clip_varc_of(NKA_VAR_DECREMENT, nka__vec4(r, g, b, alpha)); +} + +NKA_API struct nka_variation_color nka_varc_mul(float factor) +{ + return nka__clip_varc_of(NKA_VAR_MULTIPLY, nka__vec4(factor, factor, factor, 1)); +} + +NKA_API struct nka_variation_color nka_varc_rand(float r, float g, float b, float alpha) +{ + return nka__clip_varc_of(NKA_VAR_RANDOM, nka__vec4(r, g, b, alpha)); +} + +NKA_API struct nka_variation_color nka_varc_fn(nka_variation_vec4_fn fn, void *user) +{ + struct nka_variation_color v = nka__clip_varc_of(NKA_VAR_CALLBACK, nka__vec4(0, 0, 0, 0)); + v.callback = fn; + v.user = user; + return v; +} + +NKA_API struct nka_variation_color nka_varc_channel(struct nka_variation_float r, struct nka_variation_float g, + struct nka_variation_float b, struct nka_variation_float alpha) +{ + struct nka_variation_color v = nka_varc_none(); + v.r = r; + v.g = g; + v.b = b; + v.a = alpha; + return v; +} + +NKA_API struct nka_variation_color nka_varc_space(struct nka_variation_color v, int space) { v.space = space; return v; } + +NKA_API struct nka_variation_color nka_varc_clamp(struct nka_variation_color v, struct nka_vec4 lo, struct nka_vec4 hi) +{ + v.min_clamp = lo; + v.max_clamp = hi; + return v; +} + +NKA_API struct nka_variation_color nka_varc_seed(struct nka_variation_color v, nk_uint seed) { v.seed = seed; return v; } + +/* Variations. A draw depends on the instance, the loop and the key, so it holds for a loop. */ + +static nk_uint nka__clip_xorshift(nk_uint *s) +{ + nk_uint x = *s; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + *s = x; + return x; +} + +static float nka__clip_unit(nk_uint *s) { return (float)(nka__clip_xorshift(s) & 0x7fffffffu) / (float)0x7fffffff; } +static float nka__clip_signed(nk_uint *s) { return nka__clip_unit(s) * 2 - 1; } + +static nk_uint nka__clip_rng(nk_uint seed, int loop, nk_uint inst, nk_uint salt) +{ + return seed ? seed + (nk_uint)loop * 1664525u : nka_id_mix(nka_id_mix(inst, (nk_hash)loop), salt); +} + +static float nka__clip_varf(const struct nka_variation_float *v, float base, int loop, nk_uint inst, nk_uint salt) +{ + nk_uint s; + float d = 0; + if (v->mode == NKA_VAR_NONE) return base; + if (v->mode == NKA_VAR_CALLBACK && v->callback) + return nka__clip_clampf(v->callback(loop, v->user), v->min_clamp, v->max_clamp); + if (v->mode == NKA_VAR_MULTIPLY) + return nka__clip_clampf(base * nka__pow(v->amount, (float)loop), v->min_clamp, v->max_clamp); + s = nka__clip_rng(v->seed, loop, inst, salt); + switch (v->mode) { + case NKA_VAR_INCREMENT: d = v->amount * (float)loop; break; + case NKA_VAR_DECREMENT: d = -v->amount * (float)loop; break; + case NKA_VAR_RANDOM: d = nka__clip_signed(&s) * v->amount; break; + case NKA_VAR_RANDOM_ABS: d = nka__clip_unit(&s) * v->amount; break; + case NKA_VAR_PINGPONG: + d = loop % 2 == 0 ? 0 : v->amount; + if (loop % 4 >= 2) d = -d; + break; + default: break; + } + return nka__clip_clampf(base + d, v->min_clamp, v->max_clamp); +} + +static int nka__clip_vari(const struct nka_variation_int *v, int base, int loop, nk_uint inst, nk_uint salt) +{ + nk_uint s; + long long r = base; + if (v->mode == NKA_VAR_NONE) return base; + if (v->mode == NKA_VAR_CALLBACK && v->callback) r = v->callback(loop, v->user); + else { + s = nka__clip_rng(v->seed, loop, inst, salt); + switch (v->mode) { + case NKA_VAR_INCREMENT: r += (long long)v->amount * loop; break; + case NKA_VAR_DECREMENT: r -= (long long)v->amount * loop; break; + case NKA_VAR_MULTIPLY: r = nka__clip_toint((float)base * nka__pow((float)v->amount, (float)loop)); break; + case NKA_VAR_RANDOM: r += nka__clip_toint(nka__clip_signed(&s) * (float)v->amount); break; + case NKA_VAR_RANDOM_ABS: r += nka__clip_toint(nka__clip_unit(&s) * (float)v->amount); break; + case NKA_VAR_PINGPONG: + if (loop % 2) r += loop % 4 >= 2 ? -(long long)v->amount : v->amount; + break; + default: return base; + } + } + return (int)(r < v->min_clamp ? v->min_clamp : (r > v->max_clamp ? v->max_clamp : r)); +} + +/* The modes shared by vec2, vec4 and color, on n components. */ +static void nka__clip_varn(int mode, const float *amount, nk_uint seed, int loop, nk_uint inst, nk_uint salt, float *r, int n) +{ + nk_uint s = nka__clip_rng(seed, loop, inst, salt); + float k = loop % 2 == 0 ? 0.0f : (loop % 4 >= 2 ? -1.0f : 1.0f); + int i; + for (i = 0; i < n; ++i) { + switch (mode) { + case NKA_VAR_INCREMENT: r[i] += amount[i] * (float)loop; break; + case NKA_VAR_DECREMENT: r[i] -= amount[i] * (float)loop; break; + case NKA_VAR_MULTIPLY: r[i] *= nka__pow(amount[i], (float)loop); break; + case NKA_VAR_RANDOM: r[i] += nka__clip_signed(&s) * amount[i]; break; + case NKA_VAR_RANDOM_ABS: r[i] += nka__clip_unit(&s) * amount[i]; break; + case NKA_VAR_PINGPONG: r[i] += amount[i] * k; break; + default: break; + } + } +} + +static struct nk_vec2 nka__clip_varv2(const struct nka_variation_vec2 *v, struct nk_vec2 base, int loop, nk_uint inst, nk_uint salt) +{ + float r[2], amount[2]; + if (v->mode == NKA_VAR_NONE) { + if (v->x.mode == NKA_VAR_NONE && v->y.mode == NKA_VAR_NONE) return base; + r[0] = nka__clip_varf(&v->x, base.x, loop, inst, nka_id_mix(salt, 1u)); + r[1] = nka__clip_varf(&v->y, base.y, loop, inst, nka_id_mix(salt, 2u)); + return nka__vec2(r[0], r[1]); + } + if (v->mode == NKA_VAR_CALLBACK && v->callback) base = v->callback(loop, v->user); + else { + r[0] = base.x; + r[1] = base.y; + amount[0] = v->amount.x; + amount[1] = v->amount.y; + nka__clip_varn(v->mode, amount, v->seed, loop, inst, salt, r, 2); + base = nka__vec2(r[0], r[1]); + } + return nka__vec2(nka__clip_clampf(base.x, v->min_clamp.x, v->max_clamp.x), + nka__clip_clampf(base.y, v->min_clamp.y, v->max_clamp.y)); +} + +static void nka__clip_v4f(struct nka_vec4 v, float *f) { f[0] = v.x; f[1] = v.y; f[2] = v.z; f[3] = v.w; } + +static struct nka_vec4 nka__clip_clamp4(struct nka_vec4 v, struct nka_vec4 lo, struct nka_vec4 hi) +{ + return nka__vec4(nka__clip_clampf(v.x, lo.x, hi.x), nka__clip_clampf(v.y, lo.y, hi.y), + nka__clip_clampf(v.z, lo.z, hi.z), nka__clip_clampf(v.w, lo.w, hi.w)); +} + +static struct nka_vec4 nka__clip_axes(const struct nka_variation_float *axes, struct nka_vec4 v, int loop, nk_uint inst, nk_uint salt) +{ + v.x = nka__clip_varf(&axes[0], v.x, loop, inst, nka_id_mix(salt, 1u)); + v.y = nka__clip_varf(&axes[1], v.y, loop, inst, nka_id_mix(salt, 2u)); + v.z = nka__clip_varf(&axes[2], v.z, loop, inst, nka_id_mix(salt, 3u)); + v.w = nka__clip_varf(&axes[3], v.w, loop, inst, nka_id_mix(salt, 4u)); + return v; +} + +static int nka__clip_still(const struct nka_variation_float *a, const struct nka_variation_float *b, + const struct nka_variation_float *c, const struct nka_variation_float *d) +{ + return a->mode == NKA_VAR_NONE && b->mode == NKA_VAR_NONE && c->mode == NKA_VAR_NONE && d->mode == NKA_VAR_NONE; +} + +static struct nka_vec4 nka__clip_varv4(const struct nka_variation_vec4 *v, struct nka_vec4 base, int loop, nk_uint inst, nk_uint salt) +{ + struct nka_variation_float axes[4]; + float r[4], amount[4]; + if (v->mode == NKA_VAR_NONE) { + if (nka__clip_still(&v->x, &v->y, &v->z, &v->w)) return base; + axes[0] = v->x; + axes[1] = v->y; + axes[2] = v->z; + axes[3] = v->w; + return nka__clip_axes(axes, base, loop, inst, salt); + } + if (v->mode == NKA_VAR_CALLBACK && v->callback) base = v->callback(loop, v->user); + else { + nka__clip_v4f(base, r); + nka__clip_v4f(v->amount, amount); + nka__clip_varn(v->mode, amount, v->seed, loop, inst, salt, r, 4); + base = nka__vec4(r[0], r[1], r[2], r[3]); + } + return nka__clip_clamp4(base, v->min_clamp, v->max_clamp); +} + +static struct nka_vec4 nka__clip_varc(const struct nka_variation_color *v, struct nka_vec4 base, int loop, nk_uint inst, nk_uint salt) +{ + struct nka_variation_float axes[4]; + float r[4], amount[4]; + if (v->mode == NKA_VAR_NONE && nka__clip_still(&v->r, &v->g, &v->b, &v->a)) return base; + if (v->mode == NKA_VAR_CALLBACK && v->callback) base = nka__clip_clamp4(v->callback(loop, v->user), v->min_clamp, v->max_clamp); + else if (v->mode == NKA_VAR_NONE) { + axes[0] = v->r; + axes[1] = v->g; + axes[2] = v->b; + axes[3] = v->a; + base = nka__from_space(nka__clip_axes(axes, nka__to_space(base, v->space), loop, inst, salt), v->space); + } else { + nka__clip_v4f(nka__to_space(base, v->space), r); + nka__clip_v4f(v->amount, amount); + nka__clip_varn(v->mode, amount, v->seed, loop, inst, salt, r, 4); + base = nka__from_space(nka__clip_clamp4(nka__vec4(r[0], r[1], r[2], r[3]), v->min_clamp, v->max_clamp), v->space); + } + return nka__vec4(nka__clamp01(base.x), nka__clamp01(base.y), nka__clamp01(base.z), nka__clamp01(base.w)); +} + +/* Authoring */ + +static struct nka__clip_clip *nka__clip_get(const struct nka_context *a, nk_hash id) +{ + return a ? (struct nka__clip_clip *)nka__get(&a->clip_clips, id) : 0; +} + +static struct nka__clip_build *nka__clip_building(struct nka_context *a) +{ + return a && a->clip_build.open ? &a->clip_build : 0; +} + +/* Room for one more of size bytes after n: the array, moved if it grew, or NULL. */ +static void *nka__clip_room(struct nka_context *a, void *p, int n, int *cap, nk_size size) +{ + void *q; + int c; + if (n < *cap) return p; + c = *cap ? *cap * 2 : 8; + q = nka__alloc(a, (nk_size)c * size); + if (!q) return 0; + if (n) nka__copy(q, p, (nk_size)n * size); + nka__free(a, p); + *cap = c; + return q; +} + +NKA_API void nka_clip_begin(struct nka_context *a, nk_hash clip) +{ + struct nka__clip_build *b; + if (!a) return; + b = &a->clip_build; + b->nkeys = b->nvars = b->nmarkers = b->ngroups = 0; + b->offset = 0; + b->failed = 0; + b->open = 1; + nka__zero(&b->clip, sizeof b->clip); + b->clip.id = clip; +} + +static struct nka__clip_key *nka__clip_key(struct nka_context *a, nk_hash ch, float time, int type, int ease, const float *bezier4) +{ + struct nka__clip_build *b = nka__clip_building(a); + struct nka__clip_key *k; + void *q; + int i; + if (!b) return 0; + q = nka__clip_room(a, b->keys, b->nkeys, &b->ckeys, sizeof *k); + if (!q) { + b->failed = 1; + return 0; + } + b->keys = (struct nka__clip_key *)q; + k = &b->keys[b->nkeys++]; + nka__zero(k, sizeof *k); + k->ch = ch; + k->type = type; + k->ease = ease; + k->var = -1; + k->time = time + b->offset; + if (b->ngroups && k->time > b->groups[b->ngroups - 1].max) b->groups[b->ngroups - 1].max = k->time; + if (k->time > b->clip.duration) b->clip.duration = k->time; + if (bezier4) { + k->flags |= NKA__CLIP_BEZIER; + for (i = 0; i < 4; ++i) k->bezier[i] = bezier4[i]; + } + return k; +} + +static void nka__clip_key_var(struct nka_context *a, struct nka__clip_key *k, const union nka__clip_var *v) +{ + struct nka__clip_build *b = &a->clip_build; + void *q = nka__clip_room(a, b->vars, b->nvars, &b->cvars, sizeof *v); + if (!q) { + b->failed = 1; + return; + } + b->vars = (union nka__clip_var *)q; + b->vars[b->nvars] = *v; + k->var = b->nvars++; +} + +static void nka__clip_set4(struct nka__clip_key *k, struct nka_vec4 v) +{ + k->v[0].f = v.x; + k->v[1].f = v.y; + k->v[2].f = v.z; + k->v[3].f = v.w; +} + +NKA_API void nka_clip_key_float(struct nka_context *a, nk_hash ch, float time, float value, int ease, const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_FLOAT, ease, bezier4); + if (k) k->v[0].f = value; +} + +NKA_API void nka_clip_key_vec2(struct nka_context *a, nk_hash ch, float time, struct nk_vec2 value, int ease, const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_VEC2, ease, bezier4); + if (!k) return; + k->v[0].f = value.x; + k->v[1].f = value.y; +} + +NKA_API void nka_clip_key_vec4(struct nka_context *a, nk_hash ch, float time, struct nka_vec4 value, int ease, const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_VEC4, ease, bezier4); + if (k) nka__clip_set4(k, value); +} + +NKA_API void nka_clip_key_int(struct nka_context *a, nk_hash ch, float time, int value, int ease) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_INT, ease, 0); + if (k) k->v[0].i = value; +} + +NKA_API void nka_clip_key_color(struct nka_context *a, nk_hash ch, float time, struct nk_colorf value, int space, int ease, + const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_COLOR, ease, bezier4); + if (!k) return; + k->space = space; + nka__clip_set4(k, nka__cv(value)); +} + +NKA_API void nka_clip_key_float_var(struct nka_context *a, nk_hash ch, float time, float value, struct nka_variation_float var, + int ease, const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_FLOAT, ease, bezier4); + union nka__clip_var v; + if (!k) return; + k->v[0].f = value; + nka__zero(&v, sizeof v); + v.f = var; + nka__clip_key_var(a, k, &v); +} + +NKA_API void nka_clip_key_vec2_var(struct nka_context *a, nk_hash ch, float time, struct nk_vec2 value, struct nka_variation_vec2 var, + int ease, const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_VEC2, ease, bezier4); + union nka__clip_var v; + if (!k) return; + k->v[0].f = value.x; + k->v[1].f = value.y; + nka__zero(&v, sizeof v); + v.v2 = var; + nka__clip_key_var(a, k, &v); +} + +NKA_API void nka_clip_key_vec4_var(struct nka_context *a, nk_hash ch, float time, struct nka_vec4 value, struct nka_variation_vec4 var, + int ease, const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_VEC4, ease, bezier4); + union nka__clip_var v; + if (!k) return; + nka__clip_set4(k, value); + nka__zero(&v, sizeof v); + v.v4 = var; + nka__clip_key_var(a, k, &v); +} + +NKA_API void nka_clip_key_int_var(struct nka_context *a, nk_hash ch, float time, int value, struct nka_variation_int var, int ease) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_INT, ease, 0); + union nka__clip_var v; + if (!k) return; + k->v[0].i = value; + nka__zero(&v, sizeof v); + v.i = var; + nka__clip_key_var(a, k, &v); +} + +NKA_API void nka_clip_key_color_var(struct nka_context *a, nk_hash ch, float time, struct nk_colorf value, struct nka_variation_color var, + int space, int ease, const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_COLOR, ease, bezier4); + union nka__clip_var v; + if (!k) return; + k->space = space; + nka__clip_set4(k, nka__cv(value)); + nka__zero(&v, sizeof v); + v.c = var; + nka__clip_key_var(a, k, &v); +} + +NKA_API void nka_clip_key_float_spring(struct nka_context *a, nk_hash ch, float time, float target, struct nka_spring_params spring) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, NKA_CHAN_FLOAT, NKA_EASE_SPRING, 0); + if (!k) return; + k->flags |= NKA__CLIP_SPRING; + k->spring[0] = spring.mass; + k->spring[1] = spring.stiffness; + k->spring[2] = spring.damping; + k->spring[3] = spring.initial_velocity; + k->v[0].f = target; +} + +static void nka__clip_key_rel(struct nka_context *a, nk_hash ch, float time, int type, struct nka_vec4 percent, struct nka_vec4 bias, + int space, int anchor, int axis, int ease, const float *bezier4) +{ + struct nka__clip_key *k = nka__clip_key(a, ch, time, type, ease, bezier4); + if (!k) return; + k->space = space; + k->anchor = anchor; + k->axis = axis; + nka__clip_set4(k, percent); + nka__clip_v4f(bias, k->ext); +} + +NKA_API void nka_clip_key_float_rel(struct nka_context *a, nk_hash ch, float time, float percent, float px_bias, int anchor, int axis, + int ease, const float *bezier4) +{ + nka__clip_key_rel(a, ch, time, NKA_CHAN_FLOAT_REL, nka__vec4(percent, 0, 0, 0), nka__vec4(px_bias, 0, 0, 0), + 0, anchor, axis, ease, bezier4); +} + +NKA_API void nka_clip_key_vec2_rel(struct nka_context *a, nk_hash ch, float time, struct nk_vec2 percent, struct nk_vec2 px_bias, + int anchor, int ease, const float *bezier4) +{ + nka__clip_key_rel(a, ch, time, NKA_CHAN_VEC2_REL, nka__vec4(percent.x, percent.y, 0, 0), nka__vec4(px_bias.x, px_bias.y, 0, 0), + 0, anchor, 0, ease, bezier4); +} + +NKA_API void nka_clip_key_vec4_rel(struct nka_context *a, nk_hash ch, float time, struct nka_vec4 percent, struct nka_vec4 px_bias, + int anchor, int ease, const float *bezier4) +{ + nka__clip_key_rel(a, ch, time, NKA_CHAN_VEC4_REL, percent, px_bias, 0, anchor, 0, ease, bezier4); +} + +NKA_API void nka_clip_key_color_rel(struct nka_context *a, nk_hash ch, float time, struct nka_vec4 percent, struct nka_vec4 px_bias, + int space, int anchor, int ease, const float *bezier4) +{ + nka__clip_key_rel(a, ch, time, NKA_CHAN_COLOR_REL, percent, px_bias, space, anchor, 0, ease, bezier4); +} + +static void nka__clip_group(struct nka_context *a, int seq) +{ + struct nka__clip_build *b = nka__clip_building(a); + void *q; + if (!b) return; + q = nka__clip_room(a, b->groups, b->ngroups, &b->cgroups, sizeof *b->groups); + if (!q) { + b->failed = 1; + return; + } + b->groups = (struct nka__clip_group *)q; + b->groups[b->ngroups].max = b->offset; + b->groups[b->ngroups].seq = seq; + b->ngroups++; +} + +static void nka__clip_group_end(struct nka_context *a, int seq) +{ + struct nka__clip_build *b = nka__clip_building(a); + struct nka__clip_group g; + if (!b || !b->ngroups) return; + g = b->groups[--b->ngroups]; + if (!seq || g.seq) b->offset = g.max; + if (b->ngroups && g.max > b->groups[b->ngroups - 1].max) b->groups[b->ngroups - 1].max = g.max; +} + +NKA_API void nka_clip_seq_begin(struct nka_context *a) { nka__clip_group(a, 1); } +NKA_API void nka_clip_seq_end(struct nka_context *a) { nka__clip_group_end(a, 1); } +NKA_API void nka_clip_par_begin(struct nka_context *a) { nka__clip_group(a, 0); } +NKA_API void nka_clip_par_end(struct nka_context *a) { nka__clip_group_end(a, 0); } + +static nk_hash nka__clip_auto(struct nka_context *a, nk_hash salt, int instance) +{ + nk_hash id; + do { + id = nka_id_mix(++a->clip_auto, salt); + } while (!id || (instance && nka__get(&a->clip_insts, id))); + return id; +} + +NKA_API nk_hash nka_clip_marker(struct nka_context *a, float time, nk_hash id, nka_marker_callback cb, void *user) +{ + struct nka__clip_build *b = nka__clip_building(a); + struct nka__clip_marker *m; + void *q; + if (!b) return 0; + q = nka__clip_room(a, b->markers, b->nmarkers, &b->cmarkers, sizeof *m); + if (!q) { + b->failed = 1; + return 0; + } + b->markers = (struct nka__clip_marker *)q; + if (!id) id = nka__clip_auto(a, NKA__CLIP_SALT_MARK, 0); + m = &b->markers[b->nmarkers++]; + m->time = time + b->offset; + m->id = id; + m->fn = cb; + m->user = user; + if (m->time > b->clip.duration) b->clip.duration = m->time; + return id; +} + +NKA_API void nka_clip_set_loop(struct nka_context *a, int loop, int direction, int count) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.direction = direction; + b->clip.loop_count = loop ? count : 0; +} + +NKA_API void nka_clip_set_loop_delay(struct nka_context *a, float seconds) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (b) b->clip.loop_delay = seconds; +} + +NKA_API void nka_clip_set_delay(struct nka_context *a, float seconds) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (b) b->clip.delay = seconds; +} + +NKA_API void nka_clip_set_stagger(struct nka_context *a, int count, float each_delay, float center_bias) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.stagger_count = count > 0 ? count : 1; + b->clip.stagger_delay = each_delay; + b->clip.stagger_bias = nka__clamp01(center_bias); +} + +NKA_API void nka_clip_set_stagger_ease(struct nka_context *a, int ease) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (b) b->clip.stagger_ease = ease; +} + +NKA_API void nka_clip_set_duration_var(struct nka_context *a, struct nka_variation_float var) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.has_dur_var = 1; + b->clip.dur_var = var; +} + +NKA_API void nka_clip_set_delay_var(struct nka_context *a, struct nka_variation_float var) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.has_delay_var = 1; + b->clip.delay_var = var; +} + +NKA_API void nka_clip_set_timescale_var(struct nka_context *a, struct nka_variation_float var) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.has_scale_var = 1; + b->clip.scale_var = var; +} + +NKA_API void nka_clip_on_begin(struct nka_context *a, nka_clip_callback cb, void *user) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.on_begin = cb; + b->clip.begin_user = user; +} + +NKA_API void nka_clip_on_update(struct nka_context *a, nka_clip_callback cb, void *user) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.on_update = cb; + b->clip.update_user = user; +} + +NKA_API void nka_clip_on_complete(struct nka_context *a, nka_clip_callback cb, void *user) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.on_complete = cb; + b->clip.complete_user = user; +} + +NKA_API void nka_clip_on_loop(struct nka_context *a, nka_loop_callback cb, void *user) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.on_loop = cb; + b->clip.loop_user = user; +} + +NKA_API void nka_clip_on_pause(struct nka_context *a, nka_clip_callback cb, void *user) +{ + struct nka__clip_build *b = nka__clip_building(a); + if (!b) return; + b->clip.on_pause = cb; + b->clip.pause_user = user; +} + +static void nka__clip_release(struct nka_context *a, struct nka__clip_clip *c) +{ + nka__free(a, c->tracks); + nka__free(a, c->keys); + nka__free(a, c->vars); + nka__free(a, c->markers); + c->tracks = 0; + c->keys = 0; + c->vars = 0; + c->markers = 0; + c->ntracks = c->nkeys = c->nvars = c->nmarkers = 0; +} + +static void nka__clip_keep_callbacks(struct nka__clip_clip *c, const struct nka__clip_clip *old) +{ + int i, j; + c->on_begin = old->on_begin; + c->on_update = old->on_update; + c->on_complete = old->on_complete; + c->on_pause = old->on_pause; + c->on_loop = old->on_loop; + c->begin_user = old->begin_user; + c->update_user = old->update_user; + c->complete_user = old->complete_user; + c->pause_user = old->pause_user; + c->loop_user = old->loop_user; + for (i = 0; i < c->nmarkers; ++i) + for (j = 0; j < old->nmarkers; ++j) + if (old->markers[j].id == c->markers[i].id) { + c->markers[i].fn = old->markers[j].fn; + c->markers[i].user = old->markers[j].user; + break; + } +} + +/* Sorts keys into tracks and markers by time, stably, and puts c in place of the clip with its id. */ +static int nka__clip_install(struct nka_context *a, struct nka__clip_clip *c, const struct nka__clip_key *keys, int nk, + const union nka__clip_var *vars, int nv, const struct nka__clip_marker *marks, int nm, int loaded) +{ + struct nka__clip_clip *old; + int *tmp = 0, *order, *tix, *head, i, j, nt = 0; + c->tracks = 0; + c->keys = nk ? (struct nka__clip_key *)nka__alloc(a, (nk_size)nk * sizeof *keys) : 0; + c->vars = nv ? (union nka__clip_var *)nka__alloc(a, (nk_size)nv * sizeof *vars) : 0; + c->markers = nm ? (struct nka__clip_marker *)nka__alloc(a, (nk_size)nm * sizeof *marks) : 0; + c->ntracks = 0; + c->nkeys = nk; + c->nvars = nv; + c->nmarkers = nm; + if (nk) tmp = (int *)nka__alloc(a, (nk_size)nk * 3 * sizeof(int)); + if ((nk && (!c->keys || !tmp)) || (nv && !c->vars) || (nm && !c->markers)) { + nka__free(a, tmp); + nka__clip_release(a, c); + return NKA_ERR_NO_MEM; + } + order = tmp; + tix = tmp + nk; + head = tmp + 2 * nk; + for (i = 0; i < nk; ++i) { + for (j = i; j > 0 && keys[order[j - 1]].time > keys[i].time; --j) order[j] = order[j - 1]; + order[j] = i; + } + for (i = 0; i < nk; ++i) { + const struct nka__clip_key *k = &keys[order[i]]; + for (j = 0; j < nt; ++j) + if (keys[head[j]].ch == k->ch && keys[head[j]].type == k->type) break; + if (j == nt) head[nt++] = order[i]; + tix[i] = j; + } + if (nt) { + c->tracks = (struct nka__clip_track *)nka__alloc(a, (nk_size)nt * sizeof *c->tracks); + if (!c->tracks) { + nka__free(a, tmp); + nka__clip_release(a, c); + return NKA_ERR_NO_MEM; + } + c->ntracks = nt; + } + for (j = 0; j < nt; ++j) { + const struct nka__clip_key *k = &keys[head[j]]; + struct nka__clip_track *tr = &c->tracks[j]; + tr->ch = k->ch; + tr->type = k->type; + tr->space = k->space; + tr->anchor = k->anchor; + tr->axis = k->axis; + tr->first = tr->count = 0; + } + for (i = 0; i < nk; ++i) c->tracks[tix[i]].count++; + for (j = 1; j < nt; ++j) c->tracks[j].first = c->tracks[j - 1].first + c->tracks[j - 1].count; + for (j = 0; j < nt; ++j) head[j] = 0; + for (i = 0; i < nk; ++i) { + const struct nka__clip_track *tr = &c->tracks[tix[i]]; + struct nka__clip_key *k = &c->keys[tr->first + head[tix[i]]]; + head[tix[i]]++; + *k = keys[order[i]]; + k->space = tr->space; + k->anchor = tr->anchor; + k->axis = tr->axis; + } + nka__free(a, tmp); + for (i = 0; i < nv; ++i) c->vars[i] = vars[i]; + for (i = 0; i < nm; ++i) { + struct nka__clip_marker m = marks[i]; + for (j = i; j > 0 && c->markers[j - 1].time > m.time; --j) c->markers[j] = c->markers[j - 1]; + c->markers[j] = m; + } + c->gen = ++a->clip_gen; + old = nka__clip_get(a, c->id); + if (old) { + if (loaded) nka__clip_keep_callbacks(c, old); + nka__clip_release(a, old); + *old = *c; + return NKA_OK; + } + old = (struct nka__clip_clip *)nka__put(a, &a->clip_clips, c->id, 0); + if (!old) { + nka__clip_release(a, c); + return NKA_ERR_NO_MEM; + } + *old = *c; + return NKA_OK; +} + +NKA_API int nka_clip_end(struct nka_context *a) +{ + struct nka__clip_build *b = nka__clip_building(a); + struct nka__clip_clip c; + if (!b) return NKA_ERR_BAD_ARG; + b->open = 0; + if (b->failed) return NKA_ERR_NO_MEM; + c = b->clip; + return nka__clip_install(a, &c, b->keys, b->nkeys, b->vars, b->nvars, b->markers, b->nmarkers, 0); +} + +NKA_API float nka_clip_duration(const struct nka_context *a, nk_hash clip) +{ + const struct nka__clip_clip *c = nka__clip_get(a, clip); + return c ? c->duration : 0; +} + +NKA_API int nka_clip_exists(const struct nka_context *a, nk_hash clip) { return nka__clip_get(a, clip) != 0; } + +NKA_API void nka_clip_reserve(struct nka_context *a, int clips, int instances) +{ + int cap = 16; + if (!a) return; + while (cap < clips * 2) cap *= 2; + if (cap > a->clip_clips.cap) nka__grow(a, &a->clip_clips, cap); + cap = 16; + while (cap < instances * 2) cap *= 2; + if (cap > a->clip_insts.cap && nka__grow(a, &a->clip_insts, cap)) a->clip_mod++; +} + +/* Instances */ + +static struct nka__clip_inst *nka__clip_inst(struct nka_context *a, nk_hash id) +{ + struct nka__clip_inst *in; + if (!a || !id) return 0; + in = (struct nka__clip_inst *)nka__get(&a->clip_insts, id); + if (in) in->seen = a->frame; + return in; +} + +static void nka__clip_find(const struct nka__clip_key *k, int n, float t, int *i0, int *i1) +{ + int lo = 1, hi = n - 1; + if (n == 1 || t <= k[0].time) { + *i0 = *i1 = 0; + return; + } + if (t >= k[n - 1].time) { + *i0 = *i1 = n - 1; + return; + } + while (lo < hi) { + int mid = (lo + hi) / 2; + if (k[mid].time >= t) hi = mid; + else lo = mid + 1; + } + *i0 = lo - 1; + *i1 = lo; +} + +static float nka__clip_weight(const struct nka_context *a, const struct nka__clip_key *k, int type, float u) +{ + if ((k->flags & NKA__CLIP_SPRING) && type == NKA_CHAN_FLOAT) + return nka_eval(a, nka_ease_spring(k->spring[0], k->spring[1], k->spring[2], k->spring[3]), u); + if (k->ease == NKA_EASE_CUBIC_BEZIER) { + if (!(k->flags & NKA__CLIP_BEZIER)) return nka__clamp01(u); + return nka_eval(a, nka_ease_bezier(k->bezier[0], k->bezier[1], k->bezier[2], k->bezier[3]), u); + } + return nka_eval(a, nka_ease(k->ease), u); +} + +static struct nka_vec4 nka__clip_key4(const struct nka__clip_key *k) +{ + return nka__vec4(k->v[0].f, k->v[1].f, k->v[2].f, k->v[3].f); +} + +static void nka__clip_eval_track(struct nka_context *a, const struct nka__clip_clip *c, int ti, struct nka__clip_inst *in) +{ + const struct nka__clip_track *tr = &c->tracks[ti]; + const struct nka__clip_key *k0, *k1; + union nka__clip_slot *out = in->vals + ti * 8; + float t = in->time, u, w; + nk_uint s0, s1; + int i0, i1, i, n; + nka__clip_find(c->keys + tr->first, tr->count, t, &i0, &i1); + k0 = c->keys + tr->first + i0; + k1 = c->keys + tr->first + i1; + s0 = (nk_uint)(tr->first + i0); + s1 = (nk_uint)(tr->first + i1); + u = k1->time == k0->time ? 1 : (t - k0->time) / (k1->time - k0->time); + w = nka__clip_weight(a, k0, tr->type, u); + switch (tr->type) { + case NKA_CHAN_FLOAT: { + float x = k0->v[0].f, y = k1->v[0].f; + if (k0->var >= 0) x = nka__clip_varf(&c->vars[k0->var].f, x, in->loop, in->rng, s0); + if (k1->var >= 0) y = nka__clip_varf(&c->vars[k1->var].f, y, in->loop, in->rng, s1); + out[0].f = nka__lerp(x, y, w); + break; + } + case NKA_CHAN_VEC2: { + struct nk_vec2 x = nka__vec2(k0->v[0].f, k0->v[1].f), y = nka__vec2(k1->v[0].f, k1->v[1].f); + if (k0->var >= 0) x = nka__clip_varv2(&c->vars[k0->var].v2, x, in->loop, in->rng, s0); + if (k1->var >= 0) y = nka__clip_varv2(&c->vars[k1->var].v2, y, in->loop, in->rng, s1); + out[0].f = nka__lerp(x.x, y.x, w); + out[1].f = nka__lerp(x.y, y.y, w); + break; + } + case NKA_CHAN_VEC4: case NKA_CHAN_COLOR: { + struct nka_vec4 x = nka__clip_key4(k0), y = nka__clip_key4(k1); + if (tr->type == NKA_CHAN_VEC4) { + if (k0->var >= 0) x = nka__clip_varv4(&c->vars[k0->var].v4, x, in->loop, in->rng, s0); + if (k1->var >= 0) y = nka__clip_varv4(&c->vars[k1->var].v4, y, in->loop, in->rng, s1); + x = nka__lerp4(x, y, w); + } else { + if (k0->var >= 0) x = nka__clip_varc(&c->vars[k0->var].c, x, in->loop, in->rng, s0); + if (k1->var >= 0) y = nka__clip_varc(&c->vars[k1->var].c, y, in->loop, in->rng, s1); + x = nka__lerp_color(x, y, w, tr->space); + } + out[0].f = x.x; + out[1].f = x.y; + out[2].f = x.z; + out[3].f = x.w; + break; + } + case NKA_CHAN_INT: { + int x = k0->v[0].i, y = k1->v[0].i; + if (k0->var >= 0) x = nka__clip_vari(&c->vars[k0->var].i, x, in->loop, in->rng, s0); + if (k1->var >= 0) y = nka__clip_vari(&c->vars[k1->var].i, y, in->loop, in->rng, s1); + out[0].i = nka__clip_lerpi(x, y, w); + break; + } + default: + n = tr->type == NKA_CHAN_FLOAT_REL ? 1 : (tr->type == NKA_CHAN_VEC2_REL ? 2 : 4); + for (i = 0; i < n; ++i) { + out[i].f = nka__lerp(k0->v[i].f, k1->v[i].f, w); + out[4 + i].f = nka__lerp(k0->ext[i], k1->ext[i], w); + } + break; + } +} + +static void nka__clip_eval(struct nka_context *a, const struct nka__clip_clip *c, struct nka__clip_inst *in) +{ + int i; + if (in->gen != c->gen) return; + for (i = 0; i < c->ntracks; ++i) nka__clip_eval_track(a, c, i, in); +} + +/* Markers ahead of the time, in the way the instance plays, may fire again. */ +static void nka__clip_arm(struct nka__clip_inst *in, const struct nka__clip_clip *c) +{ + int m; + for (m = 0; m < c->nmarkers; ++m) + in->fired[m] = (unsigned char)(in->dir > 0 ? c->markers[m].time < in->time : c->markers[m].time > in->time); +} + +/* Fits the instance to its clip, which may have been authored again; force re-arms and re-evaluates. */ +static int nka__clip_sync(struct nka_context *a, struct nka__clip_inst *in, const struct nka__clip_clip *c, int force) +{ + if (in->gen != c->gen) { + nk_size size = (nk_size)c->ntracks * 8 * sizeof(union nka__clip_slot) + (nk_size)c->nmarkers; + union nka__clip_slot *p = 0; + if (size) { + p = (union nka__clip_slot *)nka__alloc(a, size); + if (!p) return 0; + nka__zero(p, size); + } + nka__free(a, in->vals); + in->vals = p; + in->fired = p ? (unsigned char *)(p + c->ntracks * 8) : 0; + in->gen = c->gen; + in->time = nka__clip_clampf(in->time, 0, c->duration); + force = 1; + } + if (force) { + nka__clip_arm(in, c); + nka__clip_eval(a, c, in); + } + return 1; +} + +static void nka__clip_rewind(struct nka_context *a, struct nka__clip_inst *in, const struct nka__clip_clip *c) +{ + in->delay = c->delay; + in->playing = 1; + in->paused = 0; + in->begun = 0; + in->dir = c->direction == NKA_DIR_REVERSE ? -1 : 1; + in->loops = c->loop_count; + in->loop = 0; + in->rng = 12345u + in->id; + in->time = in->dir > 0 ? 0 : c->duration; + in->chain_clip = in->chain_inst = 0; + in->chain_delay = 0; + in->seen = in->stepped = a->frame; + in->epoch = ++a->clip_epoch; + nka__clip_sync(a, in, c, 1); + a->busy = 1; +} + +/* An instance being stepped, with what user code may change under it. */ +struct nka__clip_run { + struct nka__clip_inst *in; + struct nka__clip_clip *c; + nk_hash id; + nk_uint epoch, gen; +}; + +/* After user code: refetches both, and says whether the step may go on. */ +static int nka__clip_ok(struct nka_context *a, struct nka__clip_run *r) +{ + r->in = (struct nka__clip_inst *)nka__get(&a->clip_insts, r->id); + if (!r->in || r->in->epoch != r->epoch || r->in->gen != r->gen) return 0; + r->c = nka__clip_get(a, r->in->clip); + return r->c && r->c->gen == r->gen; +} + +/* Fires the markers from from to to that have not fired this pass, in the order passed. */ +static int nka__clip_fire(struct nka_context *a, struct nka__clip_run *r, float from, float to) +{ + float lo = from < to ? from : to, hi = from < to ? to : from; + int n = r->c->nmarkers, i; + for (i = 0; i < n; ++i) { + int m = to < from ? n - 1 - i : i; + const struct nka__clip_marker *mk = &r->c->markers[m]; + if (r->in->fired[m] || mk->time < lo || mk->time > hi) continue; + r->in->fired[m] = 1; + if (!mk->fn) continue; + mk->fn(a, r->id, mk->id, mk->time, mk->user); + if (!nka__clip_ok(a, r)) return 0; + } + return 1; +} + +static void nka__clip_complete(struct nka_context *a, struct nka__clip_run *r) +{ + nk_hash next, inst; + float delay; + r->in->playing = 0; + nka__clip_eval(a, r->c, r->in); + if (r->c->on_complete) { + r->c->on_complete(a, r->id, r->c->complete_user); + r->in = (struct nka__clip_inst *)nka__get(&a->clip_insts, r->id); + if (!r->in || r->in->epoch != r->epoch) return; + } + next = r->in->chain_clip; + inst = r->in->chain_inst; + delay = r->in->chain_delay; + r->in->chain_clip = r->in->chain_inst = 0; + r->in->chain_delay = 0; + if (next && nka_play(a, next, inst) && delay > 0) { + struct nka__clip_inst *n = (struct nka__clip_inst *)nka__get(&a->clip_insts, inst); + if (n) n->delay += delay; + } +} + +static void nka__clip_step(struct nka_context *a, struct nka__clip_inst *in, float dt) +{ + struct nka__clip_run r; + float left = dt; + int wraps = 0, m; + r.id = in->id; + r.in = in; + r.c = nka__clip_get(a, in->clip); + if (!in->playing || in->paused || !r.c || !nka__clip_sync(a, in, r.c, 0)) return; + in->seen = a->frame; + r.epoch = in->epoch; + r.gen = in->gen; + if (in->delay > 0) { + in->delay -= left; + if (in->delay > 0) { + a->busy = 1; + return; + } + left = -in->delay; + } + in->delay = 0; + if (!in->begun) { + in->begun = 1; + if (r.c->on_begin) { + r.c->on_begin(a, r.id, r.c->begin_user); + if (!nka__clip_ok(a, &r)) return; + } + } + if (r.c->duration <= 0) { + if (nka__clip_fire(a, &r, r.in->time, r.in->time)) nka__clip_complete(a, &r); + return; + } + for (;;) { + float dur = r.c->duration, from = r.in->time, rate = 1, speed = r.in->scale > 0 ? r.in->scale : 1, step, room, hold; + if (r.c->has_dur_var) { + float v = nka__clip_varf(&r.c->dur_var, dur, r.in->loop, r.in->rng, NKA__CLIP_SALT_DUR); + rate = dur / (v > NKA__CLIP_MIN_PASS ? v : NKA__CLIP_MIN_PASS); + } + step = left * speed * rate; + room = r.in->dir > 0 ? dur - from : from; + if (room < 0) room = 0; + if (step <= room || wraps >= NKA__CLIP_MAX_WRAPS) { + if (step > room) step = room; + r.in->time = nka__clip_clampf(r.in->dir > 0 ? from + step : from - step, 0, dur); + if (!nka__clip_fire(a, &r, from, r.in->time)) return; + break; + } + left -= room / (speed * rate); + if (left < 0) left = 0; + r.in->time = r.in->dir > 0 ? dur : 0; + if (!nka__clip_fire(a, &r, from, r.in->time)) return; + if (r.in->loops == 0) { + nka__clip_complete(a, &r); + return; + } + if (r.in->loops > 0) r.in->loops--; + r.in->loop++; + wraps++; + if (r.c->direction == NKA_DIR_ALTERNATE) r.in->dir = -r.in->dir; + else r.in->time = r.in->dir > 0 ? 0 : dur; + for (m = 0; m < r.c->nmarkers; ++m) + r.in->fired[m] = (unsigned char)(r.c->direction == NKA_DIR_ALTERNATE && r.c->markers[m].time == r.in->time); + if (r.c->on_loop) { + r.c->on_loop(a, r.id, r.in->loop, r.c->loop_user); + if (!nka__clip_ok(a, &r)) return; + } + hold = r.c->loop_delay > 0 ? r.c->loop_delay : 0; + if (r.c->has_scale_var) { + float v = nka__clip_varf(&r.c->scale_var, 1, r.in->loop, r.in->rng, NKA__CLIP_SALT_SCALE); + r.in->scale = v > 0 ? v : 1; + } + if (r.c->has_delay_var) { + float v = nka__clip_varf(&r.c->delay_var, 0, r.in->loop, r.in->rng, NKA__CLIP_SALT_DELAY); + if (v > 0) hold += v; + } + if (hold > left) { + r.in->delay = hold - left; + break; + } + left -= hold; + } + nka__clip_eval(a, r.c, r.in); + if (r.in->playing && !r.in->paused) a->busy = 1; + if (r.c->on_update) r.c->on_update(a, r.id, r.c->update_user); +} + +static void nka__clip_update(struct nka_context *a) +{ + float dt = a->dt < NKA__CLIP_MAX_DT ? a->dt : NKA__CLIP_MAX_DT; + int i = 0; + if (a->clip_updating) return; + a->clip_updating = 1; + while (i < a->clip_insts.cap) { + struct nka__clip_inst *in; + nk_uint mod = a->clip_mod; + if (!a->clip_insts.keys[i]) { + ++i; + continue; + } + in = (struct nka__clip_inst *)nka__at(&a->clip_insts, i); + if (in->stepped == a->frame) { + ++i; + continue; + } + in->stepped = a->frame; + nka__clip_step(a, in, dt); + if (a->clip_mod != mod) i = 0; + else ++i; + } + a->clip_updating = 0; +} + +static void nka__clip_gc(struct nka_context *a, unsigned max_age) +{ + struct nka__map *m = &a->clip_insts; + int i = 0; + while (i < m->cap) { + if (m->keys[i] && a->frame - ((const struct nka__clip_inst *)nka__at(m, i))->seen > max_age) { + nka__free(a, ((struct nka__clip_inst *)nka__at(m, i))->vals); + nka__del_at(m, i); + a->clip_mod++; + } else ++i; + } + m = &a->clip_layers; + i = 0; + while (i < m->cap) { + if (m->keys[i] && a->frame - ((const struct nka__clip_layer *)nka__at(m, i))->seen > max_age) { + nka__free(a, ((struct nka__clip_layer *)nka__at(m, i))->e); + nka__del_at(m, i); + } else ++i; + } +} + +static void nka__clip_clear(struct nka_context *a) +{ + int i; + for (i = 0; i < a->clip_insts.cap; ++i) + if (a->clip_insts.keys[i]) nka__free(a, ((struct nka__clip_inst *)nka__at(&a->clip_insts, i))->vals); + for (i = 0; i < a->clip_layers.cap; ++i) + if (a->clip_layers.keys[i]) nka__free(a, ((struct nka__clip_layer *)nka__at(&a->clip_layers, i))->e); + nka__map_free(a, &a->clip_insts); + nka__map_free(a, &a->clip_layers); + nka__map_free(a, &a->clip_acc); + a->clip_layer = 0; + a->clip_mod++; +} + +static void nka__clip_destroy(struct nka_context *a) +{ + struct nka__clip_build *b = &a->clip_build; + int i; + nka__clip_clear(a); + for (i = 0; i < a->clip_clips.cap; ++i) + if (a->clip_clips.keys[i]) nka__clip_release(a, (struct nka__clip_clip *)nka__at(&a->clip_clips, i)); + nka__map_free(a, &a->clip_clips); + nka__free(a, b->keys); + nka__free(a, b->vars); + nka__free(a, b->markers); + nka__free(a, b->groups); +} + +NKA_API nk_hash nka_play(struct nka_context *a, nk_hash clip, nk_hash inst) +{ + const struct nka__clip_clip *c = nka__clip_get(a, clip); + struct nka__clip_inst *in; + int fresh; + if (!c || !inst) return 0; + in = (struct nka__clip_inst *)nka__put(a, &a->clip_insts, inst, &fresh); + if (!in) return 0; + if (fresh) { + in->id = inst; + a->clip_mod++; + } + in->clip = clip; + in->scale = 1; + in->weight = 1; + nka__clip_rewind(a, in, c); + return inst; +} + +NKA_API nk_hash nka_get_instance(struct nka_context *a, nk_hash inst) { return nka__clip_inst(a, inst) ? inst : 0; } +NKA_API int nka_instance_valid(struct nka_context *a, nk_hash inst) { return nka__clip_inst(a, inst) != 0; } + +NKA_API void nka_instance_pause(struct nka_context *a, nk_hash inst) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + const struct nka__clip_clip *c; + if (!in || in->paused) return; + in->paused = 1; + c = nka__clip_get(a, in->clip); + if (c && c->on_pause) c->on_pause(a, inst, c->pause_user); +} + +NKA_API void nka_instance_resume(struct nka_context *a, nk_hash inst) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + if (!in) return; + in->paused = 0; + if (in->playing) a->busy = 1; +} + +NKA_API void nka_instance_stop(struct nka_context *a, nk_hash inst) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + if (!in) return; + in->playing = 0; + in->time = 0; + in->epoch = ++a->clip_epoch; +} + +NKA_API void nka_instance_restart(struct nka_context *a, nk_hash inst) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + const struct nka__clip_clip *c = in ? nka__clip_get(a, in->clip) : 0; + if (c) nka__clip_rewind(a, in, c); +} + +NKA_API void nka_instance_reset(struct nka_context *a, nk_hash inst) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + const struct nka__clip_clip *c = in ? nka__clip_get(a, in->clip) : 0; + if (!c) return; + in->playing = in->paused = 0; + in->delay = 0; + in->epoch = ++a->clip_epoch; + if (!nka__clip_sync(a, in, c, 0)) return; + in->time = in->dir > 0 ? 0 : c->duration; + nka__clip_eval(a, c, in); +} + +NKA_API void nka_instance_refresh(struct nka_context *a, nk_hash inst) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + const struct nka__clip_clip *c = in ? nka__clip_get(a, in->clip) : 0; + if (c && nka__clip_sync(a, in, c, 0)) nka__clip_eval(a, c, in); +} + +NKA_API void nka_instance_destroy(struct nka_context *a, nk_hash inst) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + if (!in) return; + nka__free(a, in->vals); + nka__del(&a->clip_insts, inst); + a->clip_mod++; +} + +NKA_API void nka_instance_seek(struct nka_context *a, nk_hash inst, float time) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + const struct nka__clip_clip *c = in ? nka__clip_get(a, in->clip) : 0; + if (!c) return; + in->time = nka__clip_clampf(time, 0, c->duration); + in->epoch = ++a->clip_epoch; + nka__clip_sync(a, in, c, 1); +} + +NKA_API void nka_instance_set_time_scale(struct nka_context *a, nk_hash inst, float scale) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + if (in) in->scale = scale; +} + +NKA_API void nka_instance_set_weight(struct nka_context *a, nk_hash inst, float weight) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + if (in) in->weight = weight; +} + +NKA_API nk_hash nka_instance_then(struct nka_context *a, nk_hash inst, nk_hash next_clip, nk_hash next_inst) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + if (!in) return 0; + if (!next_inst) next_inst = nka__clip_auto(a, NKA__CLIP_SALT_INST, 1); + in->chain_clip = next_clip; + in->chain_inst = next_inst; + return next_inst; +} + +NKA_API void nka_instance_then_delay(struct nka_context *a, nk_hash inst, float delay) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + if (in) in->chain_delay = delay; +} + +NKA_API float nka_instance_time(struct nka_context *a, nk_hash inst) +{ + const struct nka__clip_inst *in = nka__clip_inst(a, inst); + return in ? in->time : 0; +} + +NKA_API float nka_instance_duration(struct nka_context *a, nk_hash inst) +{ + const struct nka__clip_inst *in = nka__clip_inst(a, inst); + const struct nka__clip_clip *c = in ? nka__clip_get(a, in->clip) : 0; + return c ? c->duration : 0; +} + +NKA_API int nka_instance_is_playing(struct nka_context *a, nk_hash inst) +{ + const struct nka__clip_inst *in = nka__clip_inst(a, inst); + return in ? in->playing : 0; +} + +NKA_API int nka_instance_is_paused(struct nka_context *a, nk_hash inst) +{ + const struct nka__clip_inst *in = nka__clip_inst(a, inst); + return in ? in->paused : 0; +} + +static int nka__clip_track_of(const struct nka__clip_clip *c, nk_hash ch, int type) +{ + int i; + for (i = 0; i < c->ntracks; ++i) + if (c->tracks[i].ch == ch && c->tracks[i].type == type) return i; + return -1; +} + +/* The values of the track a getter reads for ch: its relative kind first. */ +static const union nka__clip_slot *nka__clip_lookup(struct nka_context *a, nk_hash inst, nk_hash ch, int type, int rel, + const struct nka__clip_track **tr) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + const struct nka__clip_clip *c = in ? nka__clip_get(a, in->clip) : 0; + int t; + if (!c || !nka__clip_sync(a, in, c, 0)) return 0; + t = rel >= 0 ? nka__clip_track_of(c, ch, rel) : -1; + if (t < 0) t = nka__clip_track_of(c, ch, type); + if (t < 0) return 0; + *tr = &c->tracks[t]; + return in->vals + t * 8; +} + +/* A float track's values, relative ones worked out against the anchors as they are now. */ +static void nka__clip_resolve(const struct nka_context *a, const struct nka__clip_track *tr, const union nka__clip_slot *v, float *out) +{ + struct nk_vec2 s = nka_anchor(a, tr->anchor); + int i; + for (i = 0; i < 4; ++i) out[i] = v[i].f; + switch (tr->type) { + case NKA_CHAN_FLOAT_REL: + out[0] = (tr->axis ? s.y : s.x) * v[0].f + v[4].f; + break; + case NKA_CHAN_VEC2_REL: case NKA_CHAN_VEC4_REL: case NKA_CHAN_COLOR_REL: + out[0] = s.x * v[0].f + v[4].f; + out[1] = s.y * v[1].f + v[5].f; + out[2] = (tr->type == NKA_CHAN_COLOR_REL ? s.x * v[2].f : v[2].f) + v[6].f; + out[3] = (tr->type == NKA_CHAN_COLOR_REL ? s.y * v[3].f : v[3].f) + v[7].f; + break; + default: break; + } +} + +static int nka__clip_read4(struct nka_context *a, nk_hash inst, nk_hash ch, int type, int rel, float *out) +{ + const struct nka__clip_track *tr = 0; + const union nka__clip_slot *v = nka__clip_lookup(a, inst, ch, type, rel, &tr); + if (!v) return 0; + nka__clip_resolve(a, tr, v, out); + return 1; +} + +NKA_API int nka_instance_get_float(struct nka_context *a, nk_hash inst, nk_hash ch, float *out) +{ + float v[4] = { 0 }; + int ok = out && nka__clip_read4(a, inst, ch, NKA_CHAN_FLOAT, NKA_CHAN_FLOAT_REL, v); + if (out) *out = v[0]; + return ok; +} + +NKA_API int nka_instance_get_vec2(struct nka_context *a, nk_hash inst, nk_hash ch, struct nk_vec2 *out) +{ + float v[4] = { 0 }; + int ok = out && nka__clip_read4(a, inst, ch, NKA_CHAN_VEC2, NKA_CHAN_VEC2_REL, v); + if (out) *out = nka__vec2(v[0], v[1]); + return ok; +} + +NKA_API int nka_instance_get_vec4(struct nka_context *a, nk_hash inst, nk_hash ch, struct nka_vec4 *out) +{ + float v[4] = { 0 }; + int ok = out && nka__clip_read4(a, inst, ch, NKA_CHAN_VEC4, NKA_CHAN_VEC4_REL, v); + if (out) *out = nka__vec4(v[0], v[1], v[2], v[3]); + return ok; +} + +NKA_API int nka_instance_get_int(struct nka_context *a, nk_hash inst, nk_hash ch, int *out) +{ + const struct nka__clip_track *tr = 0; + const union nka__clip_slot *v = out ? nka__clip_lookup(a, inst, ch, NKA_CHAN_INT, -1, &tr) : 0; + if (out) *out = v ? v[0].i : 0; + return v != 0; +} + +NKA_API int nka_instance_get_color(struct nka_context *a, nk_hash inst, nk_hash ch, struct nk_colorf *out) +{ + float v[4] = { 0, 0, 0, 1 }; + int ok = out && nka__clip_read4(a, inst, ch, NKA_CHAN_COLOR, NKA_CHAN_COLOR_REL, v); + if (out) *out = nka__vc(nka__vec4(v[0], v[1], v[2], v[3])); + return ok; +} + +/* Stagger */ + +NKA_API float nka_stagger_delay(const struct nka_context *a, nk_hash clip, int index) +{ + const struct nka__clip_clip *c = nka__clip_get(a, clip); + float delay, bias, raw = 0; + int count; + if (!c || c->stagger_count <= 1) return 0; + count = c->stagger_count; + delay = c->stagger_delay; + bias = c->stagger_bias; + if (bias <= 0) raw = (float)index * delay; + else { + float center = (float)(count - 1) * 0.5f; + if (center > 0) { + float lin = (float)index * delay; + float mid = nka__fabs((float)index - center) * delay * 2 / (float)count * (float)(count - 1); + raw = lin * (1 - bias) + mid * bias; + } + } + if (c->stagger_ease != NKA_EASE_LINEAR) { + float most = (float)(count - 1) * delay; + if (most > 0) raw = nka_eval(a, nka_ease(c->stagger_ease), raw / most) * most; + } + return raw; +} + +NKA_API nk_hash nka_play_stagger(struct nka_context *a, nk_hash clip, nk_hash inst, int index) +{ + struct nka__clip_inst *in; + const struct nka__clip_clip *c; + if (!nka_play(a, clip, inst)) return 0; + c = nka__clip_get(a, clip); + in = nka__clip_inst(a, inst); + in->delay = c->delay + nka_stagger_delay(a, clip, index); + return inst; +} + +NKA_API struct nka_stagger_grid_opts nka_stagger_grid_opts_default(void) +{ + struct nka_stagger_grid_opts o; + o.cols = o.rows = 1; + o.from = NKA_STAGGER_FIRST; + o.from_index = 0; + o.axis = NKA_STAGGER_BOTH; + o.delay = 0.05f; + o.ease = NKA_EASE_LINEAR; + o.start_delay = 0; + return o; +} + +static float nka__clip_grid_dist(int axis, float dx, float dy) +{ + if (axis == NKA_STAGGER_X) return nka__fabs(dx); + if (axis == NKA_STAGGER_Y) return nka__fabs(dy); + return nka__sqrt(dx * dx + dy * dy); +} + +NKA_API float nka_stagger_grid_delay(const struct nka_context *a, int col, int row, const struct nka_stagger_grid_opts *opts) +{ + struct nka_stagger_grid_opts o = opts ? *opts : nka_stagger_grid_opts_default(); + int cols = o.cols > 0 ? o.cols : 1, rows = o.rows > 0 ? o.rows : 1, total = cols * rows, i; + float ox = 0, oy = 0, most = 0, t; + if (total <= 1) return o.start_delay; + if (o.from == NKA_STAGGER_LAST) { + ox = (float)(cols - 1); + oy = (float)(rows - 1); + } else if (o.from == NKA_STAGGER_CENTER) { + ox = (float)(cols - 1) * 0.5f; + oy = (float)(rows - 1) * 0.5f; + } else if (o.from == NKA_STAGGER_INDEX) { + int at = o.from_index < 0 ? 0 : (o.from_index > total - 1 ? total - 1 : o.from_index); + ox = (float)(at % cols); + oy = (float)(at / cols); + } + for (i = 0; i < 4; ++i) { + float d = nka__clip_grid_dist(o.axis, (float)(i & 1 ? cols - 1 : 0) - ox, (float)(i & 2 ? rows - 1 : 0) - oy); + if (d > most) most = d; + } + t = most > 0 ? nka__clip_grid_dist(o.axis, (float)col - ox, (float)row - oy) / most : 0; + if (o.ease != NKA_EASE_LINEAR) t = nka_eval(a, nka_ease(o.ease), t); + return o.start_delay + t * o.delay * (float)(total - 1); +} + +NKA_API float nka_stagger_grid_delay_index(const struct nka_context *a, int index, const struct nka_stagger_grid_opts *opts) +{ + int cols = opts && opts->cols > 0 ? opts->cols : 1; + return nka_stagger_grid_delay(a, index % cols, index / cols, opts); +} + +NKA_API nk_hash nka_play_with_delay(struct nka_context *a, nk_hash clip, nk_hash inst, float delay) +{ + struct nka__clip_inst *in; + if (!nka_play(a, clip, inst)) return 0; + in = nka__clip_inst(a, inst); + in->delay = delay; + return inst; +} + +/* Layers */ + +static int nka__clip_kind(int type) +{ + switch (type) { + case NKA_CHAN_FLOAT: case NKA_CHAN_FLOAT_REL: return NKA_CHAN_FLOAT; + case NKA_CHAN_VEC2: case NKA_CHAN_VEC2_REL: return NKA_CHAN_VEC2; + case NKA_CHAN_VEC4: case NKA_CHAN_VEC4_REL: return NKA_CHAN_VEC4; + case NKA_CHAN_INT: return NKA_CHAN_INT; + default: return -1; + } +} + +static void nka__clip_map_reset(struct nka__map *m) +{ + if (m->keys) nka__zero(m->keys, (nk_size)m->cap * sizeof(nk_hash)); + m->len = 0; +} + +NKA_API void nka_layer_begin(struct nka_context *a, nk_hash target) +{ + if (!a) return; + nka__clip_map_reset(&a->clip_acc); + a->clip_layer = target; + a->clip_layer_weight = 0; +} + +NKA_API void nka_layer_add(struct nka_context *a, nk_hash inst, float weight) +{ + struct nka__clip_inst *in = nka__clip_inst(a, inst); + const struct nka__clip_clip *c = in ? nka__clip_get(a, in->clip) : 0; + int t, i; + if (!c || !a->clip_layer || !nka__clip_sync(a, in, c, 0)) return; + weight *= in->weight; + if (!(weight > 0)) return; + a->clip_layer_weight += weight; + for (t = 0; t < c->ntracks; ++t) { + const struct nka__clip_track *tr = &c->tracks[t]; + int kind = nka__clip_kind(tr->type); + struct nka__clip_acc *acc; + float v[4]; + if (kind < 0 || (tr->type <= NKA_CHAN_VEC4 && nka__clip_track_of(c, tr->ch, tr->type + NKA_CHAN_FLOAT_REL) >= 0)) continue; + nka__clip_resolve(a, tr, in->vals + t * 8, v); + if (kind == NKA_CHAN_INT) v[0] = (float)in->vals[t * 8].i; + acc = (struct nka__clip_acc *)nka__put(a, &a->clip_acc, nka_id_mix(tr->ch, (nk_hash)kind), 0); + if (!acc) return; + acc->ch = tr->ch; + acc->kind = kind; + acc->w += weight; + for (i = 0; i < 4; ++i) acc->sum[i] += v[i] * weight; + } +} + +NKA_API void nka_layer_end(struct nka_context *a, nk_hash target) +{ + struct nka__clip_layer *ly; + int i, k, n = 0, len; + if (!a || !target || target != a->clip_layer) return; + a->clip_layer = 0; + if (!(a->clip_layer_weight > 0)) return; + ly = (struct nka__clip_layer *)nka__put(a, &a->clip_layers, target, 0); + if (!ly) return; + ly->id = target; + ly->seen = a->frame; + len = a->clip_acc.len; + if (ly->n != len) { + struct nka__clip_blend *e = 0; + if (len) { + e = (struct nka__clip_blend *)nka__alloc(a, (nk_size)len * sizeof *e); + if (!e) return; + } + nka__free(a, ly->e); + ly->e = e; + ly->n = len; + } + for (i = 0; i < a->clip_acc.cap; ++i) { + const struct nka__clip_acc *acc; + struct nka__clip_blend *b; + if (!a->clip_acc.keys[i]) continue; + acc = (const struct nka__clip_acc *)nka__at(&a->clip_acc, i); + b = &ly->e[n++]; + b->ch = acc->ch; + b->kind = acc->kind; + for (k = 0; k < 4; ++k) b->v[k] = acc->sum[k] / acc->w; + if (b->kind == NKA_CHAN_INT) b->v[0] = nka__floor(b->v[0] + 0.5f); + } +} + +static const float *nka__clip_blended(struct nka_context *a, nk_hash target, nk_hash ch, int kind) +{ + struct nka__clip_layer *ly; + int i; + if (!a || !target) return 0; + ly = (struct nka__clip_layer *)nka__get(&a->clip_layers, target); + if (!ly) return 0; + ly->seen = a->frame; + for (i = 0; i < ly->n; ++i) + if (ly->e[i].ch == ch && ly->e[i].kind == kind) return ly->e[i].v; + return 0; +} + +NKA_API int nka_get_blended_float(struct nka_context *a, nk_hash target, nk_hash ch, float *out) +{ + const float *v = nka__clip_blended(a, target, ch, NKA_CHAN_FLOAT); + if (!v || !out) return 0; + *out = v[0]; + return 1; +} + +NKA_API int nka_get_blended_vec2(struct nka_context *a, nk_hash target, nk_hash ch, struct nk_vec2 *out) +{ + const float *v = nka__clip_blended(a, target, ch, NKA_CHAN_VEC2); + if (!v || !out) return 0; + *out = nka__vec2(v[0], v[1]); + return 1; +} + +NKA_API int nka_get_blended_vec4(struct nka_context *a, nk_hash target, nk_hash ch, struct nka_vec4 *out) +{ + const float *v = nka__clip_blended(a, target, ch, NKA_CHAN_VEC4); + if (!v || !out) return 0; + *out = nka__vec4(v[0], v[1], v[2], v[3]); + return 1; +} + +NKA_API int nka_get_blended_int(struct nka_context *a, nk_hash target, nk_hash ch, int *out) +{ + const float *v = nka__clip_blended(a, target, ch, NKA_CHAN_INT); + if (!v || !out) return 0; + *out = nka__clip_toint(v[0]); + return 1; +} + +/* Saving. Words of four bytes, low byte first: "NKAC", version, size, clip id, the clip, + * its tracks and keys, markers, and an FNV-1a sum of everything before it. */ + +struct nka__clip_out { unsigned char *p; int n, cap; }; +struct nka__clip_in { const unsigned char *p; int n, at, bad; }; + +static nk_uint nka__clip_fnv(const unsigned char *p, int n) +{ + nk_uint h = 2166136261u; + int i; + for (i = 0; i < n; ++i) { + h ^= p[i]; + h *= 16777619u; + } + return h; +} + +static void nka__clip_w(struct nka__clip_out *o, nk_uint v) +{ + if (o->p && o->n + 4 <= o->cap) { + o->p[o->n] = (unsigned char)(v & 0xffu); + o->p[o->n + 1] = (unsigned char)((v >> 8) & 0xffu); + o->p[o->n + 2] = (unsigned char)((v >> 16) & 0xffu); + o->p[o->n + 3] = (unsigned char)(v >> 24); + } + o->n += 4; +} + +static void nka__clip_wi(struct nka__clip_out *o, int v) { nka__clip_w(o, (nk_uint)v); } + +static void nka__clip_wf(struct nka__clip_out *o, float v) +{ + union nka__clip_slot s; + s.f = v; + nka__clip_w(o, s.u); +} + +static void nka__clip_wf4(struct nka__clip_out *o, const float *v, int n) +{ + int i; + for (i = 0; i < n; ++i) nka__clip_wf(o, v[i]); +} + +static nk_uint nka__clip_r(struct nka__clip_in *r) +{ + const unsigned char *p = r->p + r->at; + if (r->bad || r->n - r->at < 4) { + r->bad = 1; + return 0; + } + r->at += 4; + return (nk_uint)p[0] | ((nk_uint)p[1] << 8) | ((nk_uint)p[2] << 16) | ((nk_uint)p[3] << 24); +} + +static int nka__clip_ri(struct nka__clip_in *r) { return nka__clip_s32(nka__clip_r(r)); } + +static float nka__clip_rf(struct nka__clip_in *r) +{ + union nka__clip_slot s; + s.u = nka__clip_r(r); + return s.f; +} + +/* A float that steers time: NaN means the buffer is corrupt. */ +static float nka__clip_rt(struct nka__clip_in *r) +{ + float f = nka__clip_rf(r); + if (f != f) r->bad = 1; + return f; +} + +static void nka__clip_rf4(struct nka__clip_in *r, float *v, int n) +{ + int i; + for (i = 0; i < n; ++i) v[i] = nka__clip_rf(r); +} + +static void nka__clip_wvarf(struct nka__clip_out *o, const struct nka_variation_float *v) +{ + nka__clip_wi(o, v->mode); + nka__clip_wf(o, v->amount); + nka__clip_wf(o, v->min_clamp); + nka__clip_wf(o, v->max_clamp); + nka__clip_w(o, v->seed); +} + +static void nka__clip_rvarf(struct nka__clip_in *r, struct nka_variation_float *v) +{ + v->mode = nka__clip_ri(r); + v->amount = nka__clip_rf(r); + v->min_clamp = nka__clip_rf(r); + v->max_clamp = nka__clip_rf(r); + v->seed = nka__clip_r(r); + v->callback = 0; + v->user = 0; +} + +/* A vec4 or color variation's numbers: mode, amount, clamps, then space for colors, seed. */ +static void nka__clip_wvar4(struct nka__clip_out *o, int mode, struct nka_vec4 amount, struct nka_vec4 lo, struct nka_vec4 hi, + const int *space, nk_uint seed, const struct nka_variation_float *axes) +{ + float f[12]; + int i; + nka__clip_v4f(amount, f); + nka__clip_v4f(lo, f + 4); + nka__clip_v4f(hi, f + 8); + nka__clip_wi(o, mode); + nka__clip_wf4(o, f, 12); + if (space) nka__clip_wi(o, *space); + nka__clip_w(o, seed); + for (i = 0; i < 4; ++i) nka__clip_wvarf(o, &axes[i]); +} + +static void nka__clip_rvar4(struct nka__clip_in *r, int *mode, struct nka_vec4 *amount, struct nka_vec4 *lo, struct nka_vec4 *hi, + int *space, nk_uint *seed, struct nka_variation_float *axes) +{ + float f[12]; + int i; + *mode = nka__clip_ri(r); + nka__clip_rf4(r, f, 12); + *amount = nka__vec4(f[0], f[1], f[2], f[3]); + *lo = nka__vec4(f[4], f[5], f[6], f[7]); + *hi = nka__vec4(f[8], f[9], f[10], f[11]); + if (space) *space = nka__clip_ri(r); + *seed = nka__clip_r(r); + for (i = 0; i < 4; ++i) nka__clip_rvarf(r, &axes[i]); +} + +static void nka__clip_wvar(struct nka__clip_out *o, int type, const union nka__clip_var *v) +{ + struct nka_variation_float axes[4]; + float f[6]; + switch (type) { + case NKA_CHAN_FLOAT: + nka__clip_wvarf(o, &v->f); + break; + case NKA_CHAN_INT: + nka__clip_wi(o, v->i.mode); + nka__clip_wi(o, v->i.amount); + nka__clip_wi(o, v->i.min_clamp); + nka__clip_wi(o, v->i.max_clamp); + nka__clip_w(o, v->i.seed); + break; + case NKA_CHAN_VEC2: + f[0] = v->v2.amount.x; + f[1] = v->v2.amount.y; + f[2] = v->v2.min_clamp.x; + f[3] = v->v2.min_clamp.y; + f[4] = v->v2.max_clamp.x; + f[5] = v->v2.max_clamp.y; + nka__clip_wi(o, v->v2.mode); + nka__clip_wf4(o, f, 6); + nka__clip_w(o, v->v2.seed); + nka__clip_wvarf(o, &v->v2.x); + nka__clip_wvarf(o, &v->v2.y); + break; + case NKA_CHAN_VEC4: + axes[0] = v->v4.x; + axes[1] = v->v4.y; + axes[2] = v->v4.z; + axes[3] = v->v4.w; + nka__clip_wvar4(o, v->v4.mode, v->v4.amount, v->v4.min_clamp, v->v4.max_clamp, 0, v->v4.seed, axes); + break; + default: + axes[0] = v->c.r; + axes[1] = v->c.g; + axes[2] = v->c.b; + axes[3] = v->c.a; + nka__clip_wvar4(o, v->c.mode, v->c.amount, v->c.min_clamp, v->c.max_clamp, &v->c.space, v->c.seed, axes); + break; + } +} + +static void nka__clip_rvar(struct nka__clip_in *r, int type, union nka__clip_var *v) +{ + struct nka_variation_float axes[4]; + float f[6]; + nka__zero(v, sizeof *v); + switch (type) { + case NKA_CHAN_FLOAT: + nka__clip_rvarf(r, &v->f); + break; + case NKA_CHAN_INT: + v->i.mode = nka__clip_ri(r); + v->i.amount = nka__clip_ri(r); + v->i.min_clamp = nka__clip_ri(r); + v->i.max_clamp = nka__clip_ri(r); + v->i.seed = nka__clip_r(r); + break; + case NKA_CHAN_VEC2: + v->v2.mode = nka__clip_ri(r); + nka__clip_rf4(r, f, 6); + v->v2.amount = nka__vec2(f[0], f[1]); + v->v2.min_clamp = nka__vec2(f[2], f[3]); + v->v2.max_clamp = nka__vec2(f[4], f[5]); + v->v2.seed = nka__clip_r(r); + nka__clip_rvarf(r, &v->v2.x); + nka__clip_rvarf(r, &v->v2.y); + break; + case NKA_CHAN_VEC4: + nka__clip_rvar4(r, &v->v4.mode, &v->v4.amount, &v->v4.min_clamp, &v->v4.max_clamp, 0, &v->v4.seed, axes); + v->v4.x = axes[0]; + v->v4.y = axes[1]; + v->v4.z = axes[2]; + v->v4.w = axes[3]; + break; + case NKA_CHAN_COLOR: + nka__clip_rvar4(r, &v->c.mode, &v->c.amount, &v->c.min_clamp, &v->c.max_clamp, &v->c.space, &v->c.seed, axes); + v->c.r = axes[0]; + v->c.g = axes[1]; + v->c.b = axes[2]; + v->c.a = axes[3]; + break; + default: + r->bad = 1; + break; + } +} + +static void nka__clip_write(struct nka__clip_out *o, const struct nka__clip_clip *c, int size) +{ + int t, k, i; + nka__clip_w(o, NKA__CLIP_MAGIC); + nka__clip_w(o, NKA__CLIP_VERSION); + nka__clip_wi(o, size); + nka__clip_w(o, c->id); + nka__clip_wf(o, c->duration); + nka__clip_wf(o, c->delay); + nka__clip_wf(o, c->loop_delay); + nka__clip_wi(o, c->loop_count); + nka__clip_wi(o, c->direction); + nka__clip_wi(o, c->stagger_count); + nka__clip_wf(o, c->stagger_delay); + nka__clip_wf(o, c->stagger_bias); + nka__clip_wi(o, c->stagger_ease); + nka__clip_wi(o, (c->has_dur_var ? 1 : 0) | (c->has_delay_var ? 2 : 0) | (c->has_scale_var ? 4 : 0)); + nka__clip_wvarf(o, &c->dur_var); + nka__clip_wvarf(o, &c->delay_var); + nka__clip_wvarf(o, &c->scale_var); + nka__clip_wi(o, c->ntracks); + for (t = 0; t < c->ntracks; ++t) { + const struct nka__clip_track *tr = &c->tracks[t]; + nka__clip_w(o, tr->ch); + nka__clip_wi(o, tr->type); + nka__clip_wi(o, tr->space); + nka__clip_wi(o, tr->anchor); + nka__clip_wi(o, tr->axis); + nka__clip_wi(o, tr->count); + for (k = tr->first; k < tr->first + tr->count; ++k) { + const struct nka__clip_key *key = &c->keys[k]; + nka__clip_wf(o, key->time); + nka__clip_wi(o, key->ease); + nka__clip_wi(o, key->flags | (key->var >= 0 ? NKA__CLIP_VARIED : 0)); + nka__clip_wf4(o, key->bezier, 4); + nka__clip_wf4(o, key->spring, 4); + for (i = 0; i < 4; ++i) nka__clip_w(o, key->v[i].u); + nka__clip_wf4(o, key->ext, 4); + if (key->var >= 0) nka__clip_wvar(o, tr->type, &c->vars[key->var]); + } + } + nka__clip_wi(o, c->nmarkers); + for (i = 0; i < c->nmarkers; ++i) { + nka__clip_wf(o, c->markers[i].time); + nka__clip_w(o, c->markers[i].id); + } + nka__clip_w(o, 0); +} + +NKA_API int nka_clip_save(const struct nka_context *a, nk_hash clip, void *buf, int cap) +{ + const struct nka__clip_clip *c = nka__clip_get(a, clip); + struct nka__clip_out o; + int size; + if (!c) return 0; + o.p = 0; + o.n = o.cap = 0; + nka__clip_write(&o, c, 0); + size = o.n; + if (buf && cap >= size) { + o.p = (unsigned char *)buf; + o.n = 0; + o.cap = cap; + nka__clip_write(&o, c, size); + o.n = size - 4; + nka__clip_w(&o, nka__clip_fnv(o.p, size - 4)); + } + return size; +} + +/* Reads a clip after the header. With keys, vars and markers NULL it only counts them. */ +static void nka__clip_read(struct nka__clip_in *r, struct nka__clip_clip *c, struct nka__clip_key *keys, union nka__clip_var *vars, + struct nka__clip_marker *markers, int *nkeys, int *nvars, int *nmarkers) +{ + int nt, t, k, i, nk = 0, nv = 0, nm, flags; + nka__zero(c, sizeof *c); + r->at = 12; + c->id = nka__clip_r(r); + c->duration = nka__clip_rt(r); + c->delay = nka__clip_rt(r); + c->loop_delay = nka__clip_rt(r); + c->loop_count = nka__clip_ri(r); + c->direction = nka__clip_ri(r); + c->stagger_count = nka__clip_ri(r); + c->stagger_delay = nka__clip_rt(r); + c->stagger_bias = nka__clip_rt(r); + c->stagger_ease = nka__clip_ri(r); + flags = nka__clip_ri(r); + if ((flags & ~7) || c->duration < 0) r->bad = 1; + c->has_dur_var = flags & 1; + c->has_delay_var = (flags >> 1) & 1; + c->has_scale_var = (flags >> 2) & 1; + nka__clip_rvarf(r, &c->dur_var); + nka__clip_rvarf(r, &c->delay_var); + nka__clip_rvarf(r, &c->scale_var); + nt = nka__clip_ri(r); + if (nt < 0 || nt > (r->n - r->at) / 24) r->bad = 1; + for (t = 0; t < nt && !r->bad; ++t) { + nk_hash ch = nka__clip_r(r); + int type = nka__clip_ri(r), space = nka__clip_ri(r), anchor = nka__clip_ri(r), axis = nka__clip_ri(r); + int count = nka__clip_ri(r); + if (type < NKA_CHAN_FLOAT || type > NKA_CHAN_COLOR_REL || count < 1 || count > (r->n - r->at) / 76) r->bad = 1; + for (k = 0; k < count && !r->bad; ++k) { + struct nka__clip_key tmp, *key = keys ? &keys[nk] : &tmp; + nka__zero(key, sizeof *key); + key->ch = ch; + key->type = type; + key->space = space; + key->anchor = anchor; + key->axis = axis; + key->time = nka__clip_rt(r); + key->ease = nka__clip_ri(r); + flags = nka__clip_ri(r); + if ((flags & ~7) || ((flags & NKA__CLIP_VARIED) && type >= NKA_CHAN_FLOAT_REL)) r->bad = 1; + key->flags = flags & (NKA__CLIP_BEZIER | NKA__CLIP_SPRING); + nka__clip_rf4(r, key->bezier, 4); + nka__clip_rf4(r, key->spring, 4); + for (i = 0; i < 4; ++i) key->v[i].u = nka__clip_r(r); + nka__clip_rf4(r, key->ext, 4); + key->var = -1; + if (flags & NKA__CLIP_VARIED) { + union nka__clip_var tv; + nka__clip_rvar(r, type, vars ? &vars[nv] : &tv); + key->var = nv++; + } + nk++; + } + } + nm = nka__clip_ri(r); + if (nm < 0 || nm > (r->n - r->at) / 8) r->bad = 1; + for (i = 0; i < nm && !r->bad; ++i) { + struct nka__clip_marker tmp, *mk = markers ? &markers[i] : &tmp; + mk->time = nka__clip_rt(r); + mk->id = nka__clip_r(r); + mk->fn = 0; + mk->user = 0; + } + if (r->at != r->n) r->bad = 1; + *nkeys = nk; + *nvars = nv; + *nmarkers = nm; +} + +NKA_API int nka_clip_load(struct nka_context *a, const void *buf, int len, nk_hash *out_id) +{ + struct nka__clip_in r; + struct nka__clip_clip c; + struct nka__clip_key *keys = 0; + union nka__clip_var *vars = 0; + struct nka__clip_marker *markers = 0; + nk_uint size, sum; + int nk, nv, nm, res = NKA_ERR_NO_MEM; + if (!a || !buf || len < 20) return NKA_ERR_BAD_ARG; + r.p = (const unsigned char *)buf; + r.n = len; + r.at = 0; + r.bad = 0; + if (nka__clip_r(&r) != NKA__CLIP_MAGIC || nka__clip_r(&r) != NKA__CLIP_VERSION) return NKA_ERR_BAD_ARG; + size = nka__clip_r(&r); + if (size < 20 || size > (nk_uint)len || size % 4) return NKA_ERR_BAD_ARG; + r.at = (int)size - 4; + sum = nka__clip_r(&r); + if (sum != nka__clip_fnv(r.p, (int)size - 4)) return NKA_ERR_BAD_ARG; + r.n = (int)size - 4; + nka__clip_read(&r, &c, 0, 0, 0, &nk, &nv, &nm); + if (r.bad) return NKA_ERR_BAD_ARG; + if (nk) keys = (struct nka__clip_key *)nka__alloc(a, (nk_size)nk * sizeof *keys); + if (nv) vars = (union nka__clip_var *)nka__alloc(a, (nk_size)nv * sizeof *vars); + if (nm) markers = (struct nka__clip_marker *)nka__alloc(a, (nk_size)nm * sizeof *markers); + if ((!nk || keys) && (!nv || vars) && (!nm || markers)) { + nka__clip_read(&r, &c, keys, vars, markers, &nk, &nv, &nm); + res = nka__clip_install(a, &c, keys, nk, vars, nv, markers, nm, 1); + } + nka__free(a, keys); + nka__free(a, vars); + nka__free(a, markers); + if (res == NKA_OK && out_id) *out_id = c.id; + return res; +} + +/* Oscillators, shakes and wiggles */ + +static double nka__fx_floor(double x) +{ + double i; + if (!(x > -4e18 && x < 4e18)) return x; + i = (double)(long long)x; + return i > x ? i - 1 : i; +} + +static struct nka__fx_clock *nka__fx_tick(struct nka_context *a, nk_hash id, int kind) +{ + struct nka__fx_clock *c; + if (!a) return 0; + c = (struct nka__fx_clock *)nka__put(a, &a->fx_clocks, nka_id_mix(id, (nk_hash)kind), 0); + if (!c) return 0; + c->seen = a->frame; + if (c->frame != a->frame) { + c->frame = a->frame; + c->time += a->dt; + if (c->triggered) c->since += a->dt; + } + return c; +} + +static double nka__fx_time(struct nka_context *a, nk_hash id, int kind) +{ + struct nka__fx_clock *c = nka__fx_tick(a, id, kind); + if (!a) return 0; + a->busy = 1; + return c ? c->time : a->time; +} + +static float nka__fx_wave(int wave, double x) +{ + float t = (float)(x - nka__fx_floor(x)); + if (t >= 1) t = 0; + switch (wave) { + case NKA_WAVE_TRIANGLE: return t < 0.5f ? 4 * t - 1 : 3 - 4 * t; + case NKA_WAVE_SAWTOOTH: return 2 * t - 1; + case NKA_WAVE_SQUARE: return t < 0.5f ? 1.0f : -1.0f; + default: return nka__sin(t * 2 * NKA__PI); + } +} + +static int nka__fx_round(float v) { return (int)(v > 0 ? v + 0.5f : v - 0.5f); } + +static struct nk_colorf nka__fx_offset(struct nk_colorf base, struct nka_vec4 d, int space) +{ + struct nka_vec4 c = nka__to_space(nka__cv(base), space); + c.x += d.x; c.y += d.y; c.z += d.z; c.w += d.w; + if (space == NKA_COL_HSV) { + c.x = nka__fmod(c.x + 1, 1); + c.y = nka__clamp01(c.y); + c.z = nka__clamp01(c.z); + } else if (space == NKA_COL_OKLCH) { + c.z = nka__fmod(c.z + 1, 1); + } + c = nka__from_space(c, space); + return nka__vc(nka__vec4(nka__clamp01(c.x), nka__clamp01(c.y), nka__clamp01(c.z), nka__clamp01(c.w))); +} + +NKA_API float nka_oscillate(struct nka_context *a, nk_hash id, float amplitude, float frequency, int wave, float phase) +{ + return amplitude * nka__fx_wave(wave, nka__fx_time(a, id, NKA__FX_OSC) * frequency + phase); +} + +NKA_API int nka_oscillate_int(struct nka_context *a, nk_hash id, int amplitude, float frequency, int wave, float phase) +{ + return nka__fx_round(nka_oscillate(a, id, (float)amplitude, frequency, wave, phase)); +} + +NKA_API struct nk_vec2 nka_oscillate_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 amplitude, + struct nk_vec2 frequency, int wave, struct nk_vec2 phase) +{ + double t = nka__fx_time(a, id, NKA__FX_OSC); + return nka__vec2(amplitude.x * nka__fx_wave(wave, t * frequency.x + phase.x), + amplitude.y * nka__fx_wave(wave, t * frequency.y + phase.y)); +} + +NKA_API struct nka_vec4 nka_oscillate_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 amplitude, + struct nka_vec4 frequency, int wave, struct nka_vec4 phase) +{ + double t = nka__fx_time(a, id, NKA__FX_OSC); + return nka__vec4(amplitude.x * nka__fx_wave(wave, t * frequency.x + phase.x), + amplitude.y * nka__fx_wave(wave, t * frequency.y + phase.y), + amplitude.z * nka__fx_wave(wave, t * frequency.z + phase.z), + amplitude.w * nka__fx_wave(wave, t * frequency.w + phase.w)); +} + +NKA_API struct nk_colorf nka_oscillate_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 amplitude, + float frequency, int wave, float phase, int space) +{ + float w = nka_oscillate(a, id, 1, frequency, wave, phase); + return nka__fx_offset(base, nka__vec4(amplitude.x * w, amplitude.y * w, amplitude.z * w, amplitude.w * w), space); +} + +static const nk_hash nka__fx_salt[4] = { 0, 0x12345678u, 0x23456789u, 0x3456789au }; + +static float nka__fx_hash(nk_hash s) +{ + s = (s ^ 61u) ^ (s >> 16); + s += s << 3; + s ^= s >> 4; + s *= 0x27d4eb2du; + s ^= s >> 15; + return (float)(s & 0xffffu) / 32768.0f - 1; +} + +static float nka__fx_jitter(nk_hash seed, double x, int smooth) +{ + double i = nka__fx_floor(x); + float f = (float)(x - i), n0, n1; + nk_hash k = 0; + if (i > -4e18 && i < 4e18) k = (nk_hash)(long long)i; + if (smooth) f = f * f * (3 - 2 * f); + n0 = nka__fx_hash(seed + k); + n1 = nka__fx_hash(seed + k + 1); + return n0 + (n1 - n0) * f; +} + +static void nka__fx_shake(struct nka_context *a, nk_hash id, float frequency, float decay_time, float *out, int n) +{ + struct nka__fx_clock *c = nka__fx_tick(a, id, NKA__FX_SHAKE); + float decay; + int i; + for (i = 0; i < n; ++i) out[i] = 0; + if (!c || !c->triggered) return; + if (c->since >= decay_time) { + c->triggered = 0; + return; + } + decay = 1 - (float)(c->since / decay_time); + for (i = 0; i < n; ++i) out[i] = nka__fx_jitter(id ^ nka__fx_salt[i], c->time * frequency, 0) * decay * decay; + a->busy = 1; +} + +static void nka__fx_wiggle(struct nka_context *a, nk_hash id, float frequency, float *out, int n) +{ + double t = nka__fx_time(a, id, NKA__FX_WIGGLE); + int i; + for (i = 0; i < n; ++i) out[i] = nka__fx_jitter(id ^ nka__fx_salt[i], t * frequency, 1); +} + +NKA_API void nka_trigger_shake(struct nka_context *a, nk_hash id) +{ + struct nka__fx_clock *c; + if (!a) return; + c = (struct nka__fx_clock *)nka__put(a, &a->fx_clocks, nka_id_mix(id, (nk_hash)NKA__FX_SHAKE), 0); + if (!c) return; + c->seen = a->frame; + c->triggered = 1; + c->since = 0; +} + +NKA_API float nka_shake(struct nka_context *a, nk_hash id, float intensity, float frequency, float decay_time) +{ + float v; + nka__fx_shake(a, id, frequency, decay_time, &v, 1); + return v * intensity; +} + +NKA_API int nka_shake_int(struct nka_context *a, nk_hash id, int intensity, float frequency, float decay_time) +{ + return nka__fx_round(nka_shake(a, id, (float)intensity, frequency, decay_time)); +} + +NKA_API struct nk_vec2 nka_shake_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 intensity, float frequency, float decay_time) +{ + float v[2]; + nka__fx_shake(a, id, frequency, decay_time, v, 2); + return nka__vec2(v[0] * intensity.x, v[1] * intensity.y); +} + +NKA_API struct nka_vec4 nka_shake_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 intensity, float frequency, float decay_time) +{ + float v[4]; + nka__fx_shake(a, id, frequency, decay_time, v, 4); + return nka__vec4(v[0] * intensity.x, v[1] * intensity.y, v[2] * intensity.z, v[3] * intensity.w); +} + +NKA_API struct nk_colorf nka_shake_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 intensity, + float frequency, float decay_time, int space) +{ + return nka__fx_offset(base, nka_shake_vec4(a, id, intensity, frequency, decay_time), space); +} + +NKA_API float nka_wiggle(struct nka_context *a, nk_hash id, float amplitude, float frequency) +{ + float v; + nka__fx_wiggle(a, id, frequency, &v, 1); + return v * amplitude; +} + +NKA_API int nka_wiggle_int(struct nka_context *a, nk_hash id, int amplitude, float frequency) +{ + return nka__fx_round(nka_wiggle(a, id, (float)amplitude, frequency)); +} + +NKA_API struct nk_vec2 nka_wiggle_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 amplitude, float frequency) +{ + float v[2]; + nka__fx_wiggle(a, id, frequency, v, 2); + return nka__vec2(v[0] * amplitude.x, v[1] * amplitude.y); +} + +NKA_API struct nka_vec4 nka_wiggle_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 amplitude, float frequency) +{ + float v[4]; + nka__fx_wiggle(a, id, frequency, v, 4); + return nka__vec4(v[0] * amplitude.x, v[1] * amplitude.y, v[2] * amplitude.z, v[3] * amplitude.w); +} + +NKA_API struct nk_colorf nka_wiggle_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 amplitude, + float frequency, int space) +{ + return nka__fx_offset(base, nka_wiggle_vec4(a, id, amplitude, frequency), space); +} + +/* Noise */ + +static const unsigned char nka__fx_perm[256] = { + 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, + 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, + 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, + 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, + 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, + 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, + 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, + 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, + 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, + 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, + 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, + 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, + 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, + 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, + 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, + 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180 +}; + +static int nka__fx_p(int i) { return nka__fx_perm[i & 255]; } +static float nka__fx_fade(float t) { return t * t * t * (t * (t * 6 - 15) + 10); } + +static float nka__fx_grad2(int h, float x, float y) +{ + float u = (h & 7) < 4 ? x : y, v = (h & 7) < 4 ? y : x; + return ((h & 1) ? -u : u) + ((h & 2) ? -2 * v : 2 * v); +} + +static float nka__fx_grad3(int h, float x, float y, float z) +{ + float u, v; + h &= 15; + u = h < 8 ? x : y; + v = h < 4 ? y : (h == 12 || h == 14 ? x : z); + return ((h & 1) ? -u : u) + ((h & 2) ? -v : v); +} + +static float nka__fx_perlin2(float x, float y, int seed) +{ + float fx, fy, u, v; + int X, Y, A, B; + x += (float)seed * 12.9898f; + y += (float)seed * 78.233f; + fx = nka__floor(x); + fy = nka__floor(y); + X = (int)fx & 255; + Y = (int)fy & 255; + x -= fx; + y -= fy; + u = nka__fx_fade(x); + v = nka__fx_fade(y); + A = nka__fx_p(X) + Y; + B = nka__fx_p(X + 1) + Y; + return nka__lerp(nka__lerp(nka__fx_grad2(nka__fx_p(A), x, y), nka__fx_grad2(nka__fx_p(B), x - 1, y), u), + nka__lerp(nka__fx_grad2(nka__fx_p(A + 1), x, y - 1), nka__fx_grad2(nka__fx_p(B + 1), x - 1, y - 1), u), + v) * 0.5f; +} + +static float nka__fx_perlin3(float x, float y, float z, int seed) +{ + float fx, fy, fz, u, v, w, n0, n1; + int X, Y, Z, A, AA, AB, B, BA, BB; + x += (float)seed * 12.9898f; + y += (float)seed * 78.233f; + z += (float)seed * 37.719f; + fx = nka__floor(x); + fy = nka__floor(y); + fz = nka__floor(z); + X = (int)fx & 255; + Y = (int)fy & 255; + Z = (int)fz & 255; + x -= fx; + y -= fy; + z -= fz; + u = nka__fx_fade(x); + v = nka__fx_fade(y); + w = nka__fx_fade(z); + A = nka__fx_p(X) + Y; + AA = nka__fx_p(A) + Z; + AB = nka__fx_p(A + 1) + Z; + B = nka__fx_p(X + 1) + Y; + BA = nka__fx_p(B) + Z; + BB = nka__fx_p(B + 1) + Z; + n0 = nka__lerp(nka__lerp(nka__fx_grad3(nka__fx_p(AA), x, y, z), nka__fx_grad3(nka__fx_p(BA), x - 1, y, z), u), + nka__lerp(nka__fx_grad3(nka__fx_p(AB), x, y - 1, z), nka__fx_grad3(nka__fx_p(BB), x - 1, y - 1, z), u), v); + n1 = nka__lerp(nka__lerp(nka__fx_grad3(nka__fx_p(AA + 1), x, y, z - 1), nka__fx_grad3(nka__fx_p(BA + 1), x - 1, y, z - 1), u), + nka__lerp(nka__fx_grad3(nka__fx_p(AB + 1), x, y - 1, z - 1), + nka__fx_grad3(nka__fx_p(BB + 1), x - 1, y - 1, z - 1), u), v); + return nka__lerp(n0, n1, w) * 0.5f; +} + +static float nka__fx_corner2(int h, float x, float y) +{ + static const float g[8][2] = { + { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }, + { 0.70710678f, 0.70710678f }, { -0.70710678f, 0.70710678f }, + { 0.70710678f, -0.70710678f }, { -0.70710678f, -0.70710678f } + }; + float t = 0.5f - x * x - y * y; + if (t < 0) return 0; + t *= t; + return t * t * (g[h & 7][0] * x + g[h & 7][1] * y); +} + +static float nka__fx_simplex2(float x, float y, int seed) +{ + const float f2 = 0.366025403f, g2 = 0.211324865f; + float s, t, x0, y0; + int i, j, i1; + x += (float)seed * 12.9898f; + y += (float)seed * 78.233f; + s = (x + y) * f2; + i = (int)nka__floor(x + s); + j = (int)nka__floor(y + s); + t = (float)(i + j) * g2; + x0 = x - ((float)i - t); + y0 = y - ((float)j - t); + i1 = x0 > y0; + return 45.23065f * (nka__fx_corner2(nka__fx_p(i + nka__fx_p(j)), x0, y0) + + nka__fx_corner2(nka__fx_p(i + i1 + nka__fx_p(j + !i1)), x0 - (float)i1 + g2, y0 - (float)!i1 + g2) + + nka__fx_corner2(nka__fx_p(i + 1 + nka__fx_p(j + 1)), x0 - 1 + 2 * g2, y0 - 1 + 2 * g2)); +} + +static float nka__fx_corner3(int h, float x, float y, float z) +{ + static const signed char g[12][3] = { + { 1, 1, 0 }, { -1, 1, 0 }, { 1, -1, 0 }, { -1, -1, 0 }, { 1, 0, 1 }, { -1, 0, 1 }, + { 1, 0, -1 }, { -1, 0, -1 }, { 0, 1, 1 }, { 0, -1, 1 }, { 0, 1, -1 }, { 0, -1, -1 } + }; + float t = 0.6f - x * x - y * y - z * z; + if (t < 0) return 0; + t *= t; + h %= 12; + return t * t * ((float)g[h][0] * x + (float)g[h][1] * y + (float)g[h][2] * z); +} + +static float nka__fx_simplex3(float x, float y, float z, int seed) +{ + const float g3 = 1.0f / 6; + float s, t, x0, y0, z0; + int i, j, k, i1, j1, k1, i2, j2, k2; + x += (float)seed * 12.9898f; + y += (float)seed * 78.233f; + z += (float)seed * 37.719f; + s = (x + y + z) / 3; + i = (int)nka__floor(x + s); + j = (int)nka__floor(y + s); + k = (int)nka__floor(z + s); + t = (float)(i + j + k) * g3; + x0 = x - ((float)i - t); + y0 = y - ((float)j - t); + z0 = z - ((float)k - t); + if (x0 >= y0) { + if (y0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } + else if (x0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } + else { i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } + } else { + if (y0 < z0) { i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1; } + else if (x0 < z0) { i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1; } + else { i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } + } + return 32 * (nka__fx_corner3(nka__fx_p(i + nka__fx_p(j + nka__fx_p(k))), x0, y0, z0) + + nka__fx_corner3(nka__fx_p(i + i1 + nka__fx_p(j + j1 + nka__fx_p(k + k1))), + x0 - (float)i1 + g3, y0 - (float)j1 + g3, z0 - (float)k1 + g3) + + nka__fx_corner3(nka__fx_p(i + i2 + nka__fx_p(j + j2 + nka__fx_p(k + k2))), + x0 - (float)i2 + 2 * g3, y0 - (float)j2 + 2 * g3, z0 - (float)k2 + 2 * g3) + + nka__fx_corner3(nka__fx_p(i + 1 + nka__fx_p(j + 1 + nka__fx_p(k + 1))), + x0 - 1 + 3 * g3, y0 - 1 + 3 * g3, z0 - 1 + 3 * g3)); +} + +static float nka__fx_lattice2(int x, int y, int seed) { return (float)nka__fx_p(nka__fx_p(x + seed) + y) / 255.0f * 2 - 1; } + +static float nka__fx_lattice3(int x, int y, int z, int seed) +{ + return (float)nka__fx_p(nka__fx_p(nka__fx_p(x + seed) + y) + z) / 255.0f * 2 - 1; +} + +static float nka__fx_value2(float x, float y, int seed) +{ + float fx = nka__floor(x), fy = nka__floor(y), u = nka__fx_fade(x - fx), v = nka__fx_fade(y - fy); + int xi = (int)fx, yi = (int)fy; + return nka__lerp(nka__lerp(nka__fx_lattice2(xi, yi, seed), nka__fx_lattice2(xi + 1, yi, seed), u), + nka__lerp(nka__fx_lattice2(xi, yi + 1, seed), nka__fx_lattice2(xi + 1, yi + 1, seed), u), v); +} + +static float nka__fx_value3(float x, float y, float z, int seed) +{ + float fx = nka__floor(x), fy = nka__floor(y), fz = nka__floor(z), n[2]; + float u = nka__fx_fade(x - fx), v = nka__fx_fade(y - fy), w = nka__fx_fade(z - fz); + int xi = (int)fx, yi = (int)fy, zi = (int)fz, c; + for (c = 0; c < 2; ++c) + n[c] = nka__lerp(nka__lerp(nka__fx_lattice3(xi, yi, zi + c, seed), nka__fx_lattice3(xi + 1, yi, zi + c, seed), u), + nka__lerp(nka__fx_lattice3(xi, yi + 1, zi + c, seed), nka__fx_lattice3(xi + 1, yi + 1, zi + c, seed), u), v); + return nka__lerp(n[0], n[1], w); +} + +static float nka__fx_worley2(float x, float y, int seed) +{ + float best = 1e10f; + int xi, yi, dx, dy; + x += (float)seed * 12.9898f; + y += (float)seed * 78.233f; + xi = (int)nka__floor(x); + yi = (int)nka__floor(y); + for (dy = -1; dy <= 1; ++dy) + for (dx = -1; dx <= 1; ++dx) { + int cx = xi + dx, cy = yi + dy, h = nka__fx_p(cx + nka__fx_p(cy)); + float px = x - ((float)cx + (float)nka__fx_p(h) / 255.0f); + float py = y - ((float)cy + (float)nka__fx_p(h + 1) / 255.0f); + float d = px * px + py * py; + if (d < best) best = d; + } + return nka__sqrt(best) * 1.4f - 1; +} + +static float nka__fx_worley3(float x, float y, float z, int seed) +{ + float best = 1e10f; + int xi, yi, zi, dx, dy, dz; + x += (float)seed * 12.9898f; + y += (float)seed * 78.233f; + z += (float)seed * 37.719f; + xi = (int)nka__floor(x); + yi = (int)nka__floor(y); + zi = (int)nka__floor(z); + for (dz = -1; dz <= 1; ++dz) + for (dy = -1; dy <= 1; ++dy) + for (dx = -1; dx <= 1; ++dx) { + int cx = xi + dx, cy = yi + dy, cz = zi + dz, h = nka__fx_p(cx + nka__fx_p(cy + nka__fx_p(cz))); + float px = x - ((float)cx + (float)nka__fx_p(h) / 255.0f); + float py = y - ((float)cy + (float)nka__fx_p(h + 1) / 255.0f); + float pz = z - ((float)cz + (float)nka__fx_p(h + 2) / 255.0f); + float d = px * px + py * py + pz * pz; + if (d < best) best = d; + } + return nka__sqrt(best) * 1.1547005f - 1; +} + +static float nka__fx_octave(int type, int dims, float x, float y, float z, int seed) +{ + switch (type) { + case NKA_NOISE_SIMPLEX: return dims == 3 ? nka__fx_simplex3(x, y, z, seed) : nka__fx_simplex2(x, y, seed); + case NKA_NOISE_VALUE: return dims == 3 ? nka__fx_value3(x, y, z, seed) : nka__fx_value2(x, y, seed); + case NKA_NOISE_WORLEY: return dims == 3 ? nka__fx_worley3(x, y, z, seed) : nka__fx_worley2(x, y, seed); + default: return dims == 3 ? nka__fx_perlin3(x, y, z, seed) : nka__fx_perlin2(x, y, seed); + } +} + +static float nka__fx_fractal(int dims, float x, float y, float z, const struct nka_noise_opts *opts) +{ + struct nka_noise_opts o = opts ? *opts : nka_noise_opts_default(); + float total = 0, norm = 0, amp = 1, freq = 1, v; + int i; + if (o.octaves < 1) o.octaves = 1; + for (i = 0; i < o.octaves; ++i) { + total += nka__fx_octave(o.type, dims, x * freq, y * freq, z * freq, o.seed) * amp; + norm += nka__fabs(amp); + amp *= o.persistence; + freq *= o.lacunarity; + } + v = total / norm; + return v < -1 ? -1.0f : (v > 1 ? 1.0f : v); +} + +NKA_API struct nka_noise_opts nka_noise_opts_default(void) +{ + struct nka_noise_opts o; + o.type = NKA_NOISE_PERLIN; + o.octaves = 4; + o.persistence = 0.5f; + o.lacunarity = 2; + o.seed = 0; + return o; +} + +NKA_API float nka_noise_2d(float x, float y, const struct nka_noise_opts *opts) { return nka__fx_fractal(2, x, y, 0, opts); } +NKA_API float nka_noise_3d(float x, float y, float z, const struct nka_noise_opts *opts) { return nka__fx_fractal(3, x, y, z, opts); } + +NKA_API float nka_noise_channel_float(struct nka_context *a, nk_hash id, float frequency, float amplitude, + const struct nka_noise_opts *opts) +{ + return nka_noise_2d((float)(nka__fx_time(a, id, NKA__FX_NOISE) * frequency), NKA__FX_ROW0, opts) * amplitude; +} + +NKA_API struct nk_vec2 nka_noise_channel_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 frequency, struct nk_vec2 amplitude, + const struct nka_noise_opts *opts) +{ + double t = nka__fx_time(a, id, NKA__FX_NOISE); + return nka__vec2(nka_noise_2d((float)(t * frequency.x), NKA__FX_ROW0, opts) * amplitude.x, + nka_noise_2d((float)(t * frequency.y), NKA__FX_ROW1, opts) * amplitude.y); +} + +NKA_API struct nka_vec4 nka_noise_channel_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 frequency, struct nka_vec4 amplitude, + const struct nka_noise_opts *opts) +{ + double t = nka__fx_time(a, id, NKA__FX_NOISE); + return nka__vec4(nka_noise_2d((float)(t * frequency.x), NKA__FX_ROW0, opts) * amplitude.x, + nka_noise_2d((float)(t * frequency.y), NKA__FX_ROW1, opts) * amplitude.y, + nka_noise_2d((float)(t * frequency.z), NKA__FX_ROW2, opts) * amplitude.z, + nka_noise_2d((float)(t * frequency.w), NKA__FX_ROW3, opts) * amplitude.w); +} + +NKA_API struct nk_colorf nka_noise_channel_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 amplitude, + float frequency, const struct nka_noise_opts *opts, int space) +{ + struct nka_vec4 f = nka__vec4(frequency, frequency, frequency, frequency); + return nka__fx_offset(base, nka_noise_channel_vec4(a, id, f, amplitude, opts), space); +} + +static struct nka_noise_opts nka__fx_smooth(void) +{ + struct nka_noise_opts o = nka_noise_opts_default(); + o.octaves = 2; + return o; +} + +NKA_API float nka_smooth_noise_float(struct nka_context *a, nk_hash id, float amplitude, float speed) +{ + struct nka_noise_opts o = nka__fx_smooth(); + return nka_noise_channel_float(a, id, speed, amplitude, &o); +} + +NKA_API struct nk_vec2 nka_smooth_noise_vec2(struct nka_context *a, nk_hash id, struct nk_vec2 amplitude, float speed) +{ + struct nka_noise_opts o = nka__fx_smooth(); + return nka_noise_channel_vec2(a, id, nka__vec2(speed, speed), amplitude, &o); +} + +NKA_API struct nka_vec4 nka_smooth_noise_vec4(struct nka_context *a, nk_hash id, struct nka_vec4 amplitude, float speed) +{ + struct nka_noise_opts o = nka__fx_smooth(); + return nka_noise_channel_vec4(a, id, nka__vec4(speed, speed, speed, speed), amplitude, &o); +} + +NKA_API struct nk_colorf nka_smooth_noise_color(struct nka_context *a, nk_hash id, struct nk_colorf base, struct nka_vec4 amplitude, + float speed, int space) +{ + struct nka_noise_opts o = nka__fx_smooth(); + return nka_noise_channel_color(a, id, base, amplitude, speed, &o, space); +} + +/* Gradients */ + +static int nka__fx_stops(const struct nka_gradient *g) +{ + if (!g || g->count < 0) return 0; + return g->count < NKA_GRADIENT_MAX ? g->count : NKA_GRADIENT_MAX; +} + +static struct nk_colorf nka__fx_mix(struct nk_colorf x, struct nk_colorf y, float t, int space) +{ + if (t == 0) return x; + if (t == 1) return y; + return nka__vc(nka__lerp_color(nka__cv(x), nka__cv(y), t, space)); +} + +NKA_API int nka_gradient_add(struct nka_gradient *g, float position, struct nk_colorf color) +{ + int n = nka__fx_stops(g), i; + if (!g || n >= NKA_GRADIENT_MAX) return 0; + for (i = n; i > 0 && position < g->positions[i - 1]; --i) { + g->positions[i] = g->positions[i - 1]; + g->colors[i] = g->colors[i - 1]; + } + g->positions[i] = position; + g->colors[i] = color; + g->count = n + 1; + return 1; +} + +NKA_API int nka_gradient_add_rgba(struct nka_gradient *g, float position, struct nk_color color) +{ + struct nka_vec4 c = nka__vec4((float)color.r / 255, (float)color.g / 255, (float)color.b / 255, (float)color.a / 255); + return nka_gradient_add(g, position, nka__vc(c)); +} + +NKA_API struct nk_colorf nka_gradient_sample(const struct nka_gradient *g, float t, int space) +{ + int n = nka__fx_stops(g), i; + if (!n) return nka__vc(nka__vec4(1, 1, 1, 1)); + if (n == 1 || t <= g->positions[0]) return g->colors[0]; + if (t >= g->positions[n - 1]) return g->colors[n - 1]; + for (i = 0; i < n - 1; ++i) + if (t >= g->positions[i] && t <= g->positions[i + 1]) { + float range = g->positions[i + 1] - g->positions[i]; + return nka__fx_mix(g->colors[i], g->colors[i + 1], range > NKA__EPS ? (t - g->positions[i]) / range : 0, space); + } + return g->colors[n - 1]; +} + +NKA_API struct nka_gradient nka_gradient_two_color(struct nk_colorf start, struct nk_colorf end) +{ + struct nka_gradient g; + nka__zero(&g, sizeof g); + nka_gradient_add(&g, 0, start); + nka_gradient_add(&g, 1, end); + return g; +} + +NKA_API struct nka_gradient nka_gradient_solid(struct nk_colorf color) { return nka_gradient_two_color(color, color); } + +NKA_API struct nka_gradient nka_gradient_three_color(struct nk_colorf start, struct nk_colorf mid, struct nk_colorf end) +{ + struct nka_gradient g = nka_gradient_two_color(start, end); + nka_gradient_add(&g, 0.5f, mid); + return g; +} + +/* The colors just before and after p, and whether g has more than one stop there. */ +static int nka__fx_sides(const struct nka_gradient *g, int n, int *at, float p, int space, + struct nk_colorf *left, struct nk_colorf *right) +{ + int first = *at, i = *at; + while (i < n && g->positions[i] - p <= NKA__EPS) ++i; + *at = i; + if (i == first) { + *left = *right = nka_gradient_sample(g, p, space); + return 0; + } + *left = g->colors[first]; + *right = g->colors[i - 1]; + return i - first > 1; +} + +NKA_API struct nka_gradient nka_gradient_lerp(const struct nka_gradient *a, const struct nka_gradient *b, float t, int space) +{ + float pos[2 * NKA_GRADIENT_MAX]; + struct nk_colorf col[2 * NKA_GRADIENT_MAX]; + struct nka_gradient r; + int na = nka__fx_stops(a), nb = nka__fx_stops(b), i = 0, j = 0, n = 0, k; + while (i < na || j < nb) { + struct nk_colorf al, ar, bl, br; + float p = j >= nb || (i < na && a->positions[i] <= b->positions[j]) ? a->positions[i] : b->positions[j]; + int split = nka__fx_sides(a, na, &i, p, space, &al, &ar); + split |= nka__fx_sides(b, nb, &j, p, space, &bl, &br); + pos[n] = p; + col[n++] = nka__fx_mix(al, bl, t, space); + if (split) { + pos[n] = p; + col[n++] = nka__fx_mix(ar, br, t, space); + } + } + while (n > NKA_GRADIENT_MAX && n > 2) { + float least = -1; + int best = 1; + for (k = 1; k < n - 1; ++k) { + float span = pos[k + 1] - pos[k - 1], w = span > NKA__EPS ? (pos[k] - pos[k - 1]) / span : 0; + float cost = span * (nka__fabs(col[k].r - nka__lerp(col[k - 1].r, col[k + 1].r, w)) + + nka__fabs(col[k].g - nka__lerp(col[k - 1].g, col[k + 1].g, w)) + + nka__fabs(col[k].b - nka__lerp(col[k - 1].b, col[k + 1].b, w)) + + nka__fabs(col[k].a - nka__lerp(col[k - 1].a, col[k + 1].a, w))); + if (least < 0 || cost < least) { + least = cost; + best = k; + } + } + for (k = best; k < n - 1; ++k) { + pos[k] = pos[k + 1]; + col[k] = col[k + 1]; + } + --n; + } + if (n > NKA_GRADIENT_MAX) n = NKA_GRADIENT_MAX; + nka__zero(&r, sizeof r); + r.count = n; + for (k = 0; k < n; ++k) { + r.positions[k] = pos[k]; + r.colors[k] = col[k]; + } + return r; +} + +static int nka__fx_grad_eq(const struct nka_gradient *x, const struct nka_gradient *y) +{ + int n = nka__fx_stops(x), i; + if (n != nka__fx_stops(y)) return 0; + for (i = 0; i < n; ++i) + if (nka__fabs(x->positions[i] - y->positions[i]) > NKA__EPS + || nka__fabs(x->colors[i].r - y->colors[i].r) > NKA__EPS || nka__fabs(x->colors[i].g - y->colors[i].g) > NKA__EPS + || nka__fabs(x->colors[i].b - y->colors[i].b) > NKA__EPS || nka__fabs(x->colors[i].a - y->colors[i].a) > NKA__EPS) + return 0; + return 1; +} + +static struct nka_gradient nka__fx_grad_now(struct nka_context *a, struct nka__fx_grad *c) +{ + float t; + if (c->sleeping) return c->to; + t = (float)((a->time - c->start) / c->dur); + if (t >= 1) { + c->sleeping = 1; + return c->to; + } + a->busy = 1; + return nka_gradient_lerp(&c->from, &c->to, nka_eval(a, c->ease, t), c->space); +} + +NKA_API struct nka_gradient nka_tween_gradient(struct nka_context *a, nk_hash id, nk_hash ch, const struct nka_gradient *target, + float dur, struct nka_ease ease, int policy, int space) +{ + struct nka__fx_grad *c; + struct nka_gradient g; + int fresh; + nka__zero(&g, sizeof g); + if (target) g = *target; + if (!a) return g; + c = (struct nka__fx_grad *)nka__put(a, &a->fx_grads, nka_id_mix(id, ch), &fresh); + if (!c) return g; + c->seen = a->frame; + if (fresh) { + c->from = c->to = g; + c->sleeping = 1; + c->dur = NKA__MIN_DUR; + c->ease = ease; + c->policy = (unsigned char)policy; + c->space = (unsigned char)space; + } + if (!nka__fx_grad_eq(&c->to, &g) + || (!c->sleeping && (c->policy != policy || c->space != space || !nka__ease_eq(c->ease, ease)))) { + if (policy == NKA_POLICY_CUT) { + c->from = c->to = g; + c->sleeping = 1; + } else { + struct nka_gradient now = nka__fx_grad_now(a, c); + c->from = now; + c->to = g; + c->start = a->time; + c->dur = dur > NKA__MIN_DUR ? dur : NKA__MIN_DUR; + c->sleeping = 0; + } + c->ease = ease; + c->policy = (unsigned char)policy; + c->space = (unsigned char)space; + } + return nka__fx_grad_now(a, c); +} + +/* Transforms */ + +NKA_API struct nka_transform nka_transform(struct nk_vec2 position, float rotation, struct nk_vec2 scale) +{ + struct nka_transform t; + t.position = position; + t.scale = scale; + t.rotation = rotation; + return t; +} + +NKA_API struct nka_transform nka_transform_identity(void) { return nka_transform(nka__vec2(0, 0), 0, nka__vec2(1, 1)); } + +NKA_API struct nk_vec2 nka_transform_apply(struct nka_transform t, struct nk_vec2 point) +{ + float c = nka__cos(t.rotation), s = nka__sin(t.rotation), x = point.x * t.scale.x, y = point.y * t.scale.y; + return nka__vec2(t.position.x + x * c - y * s, t.position.y + x * s + y * c); +} + +NKA_API struct nka_transform nka_transform_compose(struct nka_transform a, struct nka_transform b) +{ + return nka_transform(nka_transform_apply(a, b.position), a.rotation + b.rotation, + nka__vec2(a.scale.x * b.scale.x, a.scale.y * b.scale.y)); +} + +NKA_API struct nka_transform nka_transform_inverse(struct nka_transform t) +{ + struct nk_vec2 s = nka__vec2(nka__fabs(t.scale.x) > NKA__EPS ? 1 / t.scale.x : 1, + nka__fabs(t.scale.y) > NKA__EPS ? 1 / t.scale.y : 1); + struct nka_transform r = nka_transform(nka__vec2(0, 0), -t.rotation, s); + struct nk_vec2 p = nka_transform_apply(r, t.position); + r.position = nka__vec2(-p.x, -p.y); + return r; +} + +static float nka__fx_turn(float from, float to, int mode) +{ + const float tau = 2 * NKA__PI; + float x = nka__fmod(from, tau), y = nka__fmod(to, tau), d; + if (x < 0) x += tau; + if (y < 0) y += tau; + d = y - x; + switch (mode) { + case NKA_ROTATION_SHORTEST: + if (d > NKA__PI) d -= tau; + else if (d < -NKA__PI) d += tau; + return d; + case NKA_ROTATION_LONGEST: + if (d > 0 && d < NKA__PI) d -= tau; + else if (d < 0 && d > -NKA__PI) d += tau; + return d; + case NKA_ROTATION_CW: return d < 0 ? d + tau : d; + case NKA_ROTATION_CCW: return d > 0 ? d - tau : d; + default: return to - from; + } +} + +NKA_API struct nka_transform nka_transform_lerp(struct nka_transform a, struct nka_transform b, float t, int rotation_mode) +{ + return nka_transform(nka__vec2(nka__lerp(a.position.x, b.position.x, t), nka__lerp(a.position.y, b.position.y, t)), + a.rotation + nka__fx_turn(a.rotation, b.rotation, rotation_mode) * t, + nka__vec2(nka__lerp(a.scale.x, b.scale.x, t), nka__lerp(a.scale.y, b.scale.y, t))); +} + +NKA_API struct nka_transform nka_transform_from_matrix(float m00, float m01, float m10, float m11, float tx, float ty) +{ + float sign = m00 * m11 - m01 * m10 < 0 ? -1.0f : 1.0f; + return nka_transform(nka__vec2(tx, ty), nka__atan2(sign * m10, sign * m00), + nka__vec2(sign * nka__sqrt(m00 * m00 + m10 * m10), nka__sqrt(m01 * m01 + m11 * m11))); +} + +NKA_API void nka_transform_to_matrix(struct nka_transform t, float *out) +{ + float c = nka__cos(t.rotation), s = nka__sin(t.rotation); + if (!out) return; + out[0] = c * t.scale.x; + out[1] = -s * t.scale.y; + out[2] = t.position.x; + out[3] = s * t.scale.x; + out[4] = c * t.scale.y; + out[5] = t.position.y; +} + +static int nka__fx_xform_eq(struct nka_transform x, struct nka_transform y) +{ + return nka__fabs(x.position.x - y.position.x) + nka__fabs(x.position.y - y.position.y) <= NKA__EPS + && nka__fabs(nka__fx_turn(x.rotation, y.rotation, NKA_ROTATION_SHORTEST)) <= NKA__EPS + && nka__fabs(x.scale.x - y.scale.x) + nka__fabs(x.scale.y - y.scale.y) <= NKA__EPS; +} + +static struct nka_transform nka__fx_xform_now(struct nka_context *a, struct nka__fx_xform *c) +{ + float t; + if (c->sleeping) return c->to; + t = (float)((a->time - c->start) / c->dur); + if (t >= 1) { + c->sleeping = 1; + return c->to; + } + a->busy = 1; + return nka_transform_lerp(c->from, c->to, nka_eval(a, c->ease, t), c->mode); +} + +NKA_API struct nka_transform nka_tween_transform(struct nka_context *a, nk_hash id, nk_hash ch, struct nka_transform target, + float dur, struct nka_ease ease, int policy, int rotation_mode) +{ + struct nka__fx_xform *c; + int fresh; + if (!a) return target; + c = (struct nka__fx_xform *)nka__put(a, &a->fx_xforms, nka_id_mix(id, ch), &fresh); + if (!c) return target; + c->seen = a->frame; + if (fresh) { + c->from = c->to = target; + c->sleeping = 1; + c->dur = NKA__MIN_DUR; + c->ease = ease; + c->policy = (unsigned char)policy; + c->mode = (unsigned char)rotation_mode; + } + if (!nka__fx_xform_eq(c->to, target) + || (!c->sleeping && (c->policy != policy || c->mode != rotation_mode || !nka__ease_eq(c->ease, ease)))) { + if (policy == NKA_POLICY_CUT) { + c->from = c->to = target; + c->sleeping = 1; + } else { + struct nka_transform now = nka__fx_xform_now(a, c); + c->from = now; + c->to = target; + c->start = a->time; + c->dur = dur > NKA__MIN_DUR ? dur : NKA__MIN_DUR; + c->sleeping = 0; + } + c->ease = ease; + c->policy = (unsigned char)policy; + c->mode = (unsigned char)rotation_mode; + } + return nka__fx_xform_now(a, c); +} + +/* Drag feedback */ + +NKA_API struct nka_drag_opts nka_drag_opts_default(void) +{ + struct nka_drag_opts o; + o.snap_grid = nka__vec2(0, 0); + o.snap_points = 0; + o.snap_points_count = 0; + o.snap_duration = 0.2f; + o.overshoot = 0; + o.ease_type = NKA_EASE_OUT_CUBIC; + return o; +} + +static struct nka_drag_feedback nka__fx_loose(struct nk_vec2 pos, int dragging, float progress) +{ + struct nka_drag_feedback fb; + fb.position = pos; + fb.offset = fb.velocity = nka__vec2(0, 0); + fb.is_dragging = dragging; + fb.is_snapping = 0; + fb.snap_progress = progress; + return fb; +} + +static struct nka_drag_feedback nka__fx_feedback(const struct nka__fx_drag *s, struct nk_vec2 pos) +{ + struct nka_drag_feedback fb; + fb.position = pos; + fb.offset = nka__vec2(pos.x - s->start.x, pos.y - s->start.y); + fb.velocity = s->vel; + fb.is_dragging = s->dragging; + fb.is_snapping = s->snapping; + fb.snap_progress = s->progress; + return fb; +} + +static struct nka__fx_drag *nka__fx_drag_get(struct nka_context *a, nk_hash id) +{ + return a ? (struct nka__fx_drag *)nka__get(&a->fx_drags, id) : 0; +} + +NKA_API struct nka_drag_feedback nka_drag_begin(struct nka_context *a, nk_hash id, struct nk_vec2 pos) +{ + struct nka__fx_drag *s = 0; + if (a) s = (struct nka__fx_drag *)nka__put(a, &a->fx_drags, id, 0); + if (!s) return nka__fx_loose(pos, 1, 0); + s->seen = s->frame = a->frame; + s->t_prev = s->t_cur = a->time; + s->start = s->cur = s->prev = s->to = pos; + s->vel = nka__vec2(0, 0); + s->dragging = 1; + s->snapping = 0; + s->progress = 0; + return nka__fx_feedback(s, pos); +} + +NKA_API struct nka_drag_feedback nka_drag_update(struct nka_context *a, nk_hash id, struct nk_vec2 pos) +{ + struct nka__fx_drag *s = nka__fx_drag_get(a, id); + float dt; + if (!s) return nka__fx_loose(pos, 0, 0); + s->seen = a->frame; + if (s->frame != a->frame) { + s->frame = a->frame; + s->prev = s->cur; + s->t_prev = s->t_cur; + } + dt = (float)(a->time - s->t_prev); + if (dt > 0) s->vel = nka__vec2((pos.x - s->prev.x) / dt, (pos.y - s->prev.y) / dt); + s->cur = pos; + s->t_cur = a->time; + return nka__fx_feedback(s, pos); +} + +static int nka__fx_snap(const struct nka_drag_opts *o, struct nk_vec2 pos, struct nk_vec2 *to) +{ + int snap = 0, i; + *to = pos; + if (o->snap_grid.x > 0 || o->snap_grid.y > 0) { + if (o->snap_grid.x > 0) to->x = nka__floor(pos.x / o->snap_grid.x + 0.5f) * o->snap_grid.x; + if (o->snap_grid.y > 0) to->y = nka__floor(pos.y / o->snap_grid.y + 0.5f) * o->snap_grid.y; + snap = 1; + } + if (o->snap_points && o->snap_points_count > 0) { + float best = 0; + for (i = 0; i < o->snap_points_count; ++i) { + float dx = o->snap_points[i].x - pos.x, dy = o->snap_points[i].y - pos.y; + if (i == 0 || dx * dx + dy * dy < best) { + best = dx * dx + dy * dy; + *to = o->snap_points[i]; + } + } + snap = 1; + } + return snap; +} + +NKA_API struct nka_drag_feedback nka_drag_release(struct nka_context *a, nk_hash id, struct nk_vec2 pos, + const struct nka_drag_opts *opts) +{ + struct nka__fx_drag *s = nka__fx_drag_get(a, id); + struct nka_drag_opts d; + float t, k; + if (!s) return nka__fx_loose(pos, 0, 1); + if (!opts) { + d = nka_drag_opts_default(); + opts = &d; + } + s->seen = a->frame; + if (!s->snapping) { + int snap = nka__fx_snap(opts, pos, &s->to); + s->dragging = 0; + s->progress = 1; + if (snap && opts->snap_duration > 0 && (s->to.x != pos.x || s->to.y != pos.y)) { + s->snapping = 1; + s->progress = 0; + s->from = pos; + s->t0 = a->time; + s->dur = opts->snap_duration; + s->overshoot = opts->overshoot; + s->ease = opts->ease_type; + } + } + if (!s->snapping) return nka__fx_feedback(s, s->to); + t = (float)((a->time - s->t0) / s->dur); + if (t >= 1) { + s->snapping = 0; + s->progress = 1; + return nka__fx_feedback(s, s->to); + } + s->progress = t; + k = nka_eval(a, nka_ease(s->ease), t) + s->overshoot * 1.70158f * t * (1 - t) * (1 - t); + a->busy = 1; + return nka__fx_feedback(s, nka__vec2(nka__lerp(s->from.x, s->to.x, k), nka__lerp(s->from.y, s->to.y, k))); +} + +NKA_API void nka_drag_cancel(struct nka_context *a, nk_hash id) +{ + struct nka__fx_drag *s = nka__fx_drag_get(a, id); + if (!s) return; + s->dragging = 0; + s->snapping = 0; +} + +/* Nuklear */ + +NKA_API void nka_anchor_update(struct nka_context *a, struct nk_context *ctx) +{ + if (!a || !ctx || !ctx->current) return; + nka_set_anchor(a, NKA_ANCHOR_WINDOW_CONTENT, nk_window_get_content_region_size(ctx)); + nka_set_anchor(a, NKA_ANCHOR_WINDOW, nk_window_get_size(ctx)); +} + +static struct nk_panel *nka__nk_panel(struct nk_context *ctx) +{ + return ctx && ctx->current && ctx->current->layout ? ctx->current->layout : 0; +} + +/* The range nk_panel_end clamps a scroll offset to, from the layout so far. */ +static void nka__nk_range(const struct nk_panel *p, float *max) +{ + float h = (float)(int)(p->at_y + p->row.height - p->bounds.y) - p->bounds.h; + float w = (float)(int)(p->max_x - p->bounds.x) - p->bounds.w; + max[0] = w > 0 ? w : 0; + max[1] = h > 0 ? h : 0; +} + +static void nka__nk_scroll_to(struct nka_context *a, struct nk_context *ctx, int axis, float to, int bottom, + float dur, struct nka_ease ease) +{ + struct nk_panel *p = nka__nk_panel(ctx); + struct nka__nk_scroll *s; + int fresh; + nk_uint *offset; + if (!a || !p) return; + s = (struct nka__nk_scroll *)nka__put(a, &a->nk_scrolls, nka__ptr_key(p->offset_y), &fresh); + if (!s) return; + if (fresh) nka__nk_range(p, s->max); + offset = axis ? p->offset_y : p->offset_x; + s->seen = a->frame; + s->from[axis] = (float)*offset; + s->to[axis] = bottom ? s->max[axis] : (to > 0 ? to : 0); + s->dur[axis] = dur; + s->ease[axis] = ease; + s->start[axis] = a->time; + s->wrote[axis] = *offset; + s->active[axis] = 1; +} + +NKA_API void nka_scroll_to_x(struct nka_context *a, struct nk_context *ctx, float x, float dur, struct nka_ease ease) +{ + nka__nk_scroll_to(a, ctx, 0, x, 0, dur, ease); +} + +NKA_API void nka_scroll_to_y(struct nka_context *a, struct nk_context *ctx, float y, float dur, struct nka_ease ease) +{ + nka__nk_scroll_to(a, ctx, 1, y, 0, dur, ease); +} + +NKA_API void nka_scroll_to_top(struct nka_context *a, struct nk_context *ctx, float dur, struct nka_ease ease) +{ + nka__nk_scroll_to(a, ctx, 1, 0, 0, dur, ease); +} + +NKA_API void nka_scroll_to_bottom(struct nka_context *a, struct nk_context *ctx, float dur, struct nka_ease ease) +{ + nka__nk_scroll_to(a, ctx, 1, 0, 1, dur, ease); +} + +NKA_API void nka_scroll(struct nka_context *a, struct nk_context *ctx) +{ + struct nk_panel *p = nka__nk_panel(ctx); + struct nka__nk_scroll *s; + int axis; + if (!a || !p) return; + s = (struct nka__nk_scroll *)nka__put(a, &a->nk_scrolls, nka__ptr_key(p->offset_y), 0); + if (!s) return; + s->seen = a->frame; + nka__nk_range(p, s->max); + for (axis = 0; axis < 2; ++axis) { + nk_uint *offset = axis ? p->offset_y : p->offset_x; + nk_uint now = *offset; + float t, v; + if (!s->active[axis]) continue; + if ((now > s->wrote[axis] ? now - s->wrote[axis] : s->wrote[axis] - now) > 1) { + s->active[axis] = 0; + continue; + } + t = s->dur[axis] > 0 ? (float)((a->time - s->start[axis]) / s->dur[axis]) : 1; + v = nka__lerp(s->from[axis], s->to[axis], nka_eval(a, s->ease[axis], t)); + v = v < 0 ? 0 : (v > s->max[axis] ? s->max[axis] : v); + *offset = (nk_uint)(v + 0.5f); + s->wrote[axis] = *offset; + if (t >= 1) s->active[axis] = 0; + else a->busy = 1; + } +} + +static nk_byte nka__nk_byte(float v) { return (nk_byte)(nka__clamp01(v) * 255 + 0.5f); } + +static struct nk_color nka__nk_mix(struct nk_color x, struct nk_color y, float t, int space) +{ + struct nka_vec4 v; + struct nk_color c; + if (x.r == y.r && x.g == y.g && x.b == y.b && x.a == y.a) return x; + v = nka__lerp_color(nka__vec4(x.r / 255.0f, x.g / 255.0f, x.b / 255.0f, x.a / 255.0f), + nka__vec4(y.r / 255.0f, y.g / 255.0f, y.b / 255.0f, y.a / 255.0f), t, space); + c.r = nka__nk_byte(v.x); + c.g = nka__nk_byte(v.y); + c.b = nka__nk_byte(v.z); + c.a = nka__nk_byte(v.w); + return c; +} + +static struct nk_vec2 nka__nk_vmix(struct nk_vec2 x, struct nk_vec2 y, float t) +{ + return nka__vec2(nka__lerp(x.x, y.x, t), nka__lerp(x.y, y.y, t)); +} + +static void nka__nk_item(struct nk_style_item *o, const struct nk_style_item *x, const struct nk_style_item *y, + float t, int space) +{ + if (x->type != NK_STYLE_ITEM_COLOR || y->type != NK_STYLE_ITEM_COLOR) return; + o->type = NK_STYLE_ITEM_COLOR; + o->data.color = nka__nk_mix(x->data.color, y->data.color, t, space); +} + +#define NKA__NK_C(f) o->f = nka__nk_mix(x->f, y->f, t, space) +#define NKA__NK_F(f) o->f = nka__lerp(x->f, y->f, t) +#define NKA__NK_V(f) o->f = nka__nk_vmix(x->f, y->f, t) +#define NKA__NK_I(f) nka__nk_item(&o->f, &x->f, &y->f, t, space) +#define NKA__NK_S(fn, f) fn(&o->f, &x->f, &y->f, t, space) + +static void nka__nk_button(struct nk_style_button *o, const struct nk_style_button *x, + const struct nk_style_button *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); + NKA__NK_C(border_color); NKA__NK_F(color_factor_background); + NKA__NK_C(text_background); NKA__NK_C(text_normal); NKA__NK_C(text_hover); NKA__NK_C(text_active); + NKA__NK_F(color_factor_text); NKA__NK_F(border); NKA__NK_F(rounding); + NKA__NK_V(padding); NKA__NK_V(image_padding); NKA__NK_V(touch_padding); NKA__NK_F(disabled_factor); +} + +static void nka__nk_toggle(struct nk_style_toggle *o, const struct nk_style_toggle *x, + const struct nk_style_toggle *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); NKA__NK_C(border_color); + NKA__NK_I(cursor_normal); NKA__NK_I(cursor_hover); + NKA__NK_C(text_normal); NKA__NK_C(text_hover); NKA__NK_C(text_active); NKA__NK_C(text_background); + NKA__NK_V(padding); NKA__NK_V(touch_padding); NKA__NK_F(spacing); NKA__NK_F(border); + NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); +} + +static void nka__nk_selectable(struct nk_style_selectable *o, const struct nk_style_selectable *x, + const struct nk_style_selectable *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(pressed); + NKA__NK_I(normal_active); NKA__NK_I(hover_active); NKA__NK_I(pressed_active); + NKA__NK_C(text_normal); NKA__NK_C(text_hover); NKA__NK_C(text_pressed); + NKA__NK_C(text_normal_active); NKA__NK_C(text_hover_active); NKA__NK_C(text_pressed_active); + NKA__NK_C(text_background); NKA__NK_F(rounding); + NKA__NK_V(padding); NKA__NK_V(touch_padding); NKA__NK_V(image_padding); + NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); +} + +static void nka__nk_slider(struct nk_style_slider *o, const struct nk_style_slider *x, + const struct nk_style_slider *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); NKA__NK_C(border_color); + NKA__NK_C(bar_normal); NKA__NK_C(bar_hover); NKA__NK_C(bar_active); NKA__NK_C(bar_filled); + NKA__NK_I(cursor_normal); NKA__NK_I(cursor_hover); NKA__NK_I(cursor_active); + NKA__NK_F(border); NKA__NK_F(rounding); NKA__NK_F(bar_height); + NKA__NK_V(padding); NKA__NK_V(spacing); NKA__NK_V(cursor_size); + NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); + NKA__NK_S(nka__nk_button, inc_button); NKA__NK_S(nka__nk_button, dec_button); +} + +static void nka__nk_knob(struct nk_style_knob *o, const struct nk_style_knob *x, + const struct nk_style_knob *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); NKA__NK_C(border_color); + NKA__NK_C(knob_normal); NKA__NK_C(knob_hover); NKA__NK_C(knob_active); NKA__NK_C(knob_border_color); + NKA__NK_C(cursor_normal); NKA__NK_C(cursor_hover); NKA__NK_C(cursor_active); + NKA__NK_F(border); NKA__NK_F(knob_border); NKA__NK_V(padding); NKA__NK_V(spacing); + NKA__NK_F(cursor_width); NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); +} + +static void nka__nk_progress(struct nk_style_progress *o, const struct nk_style_progress *x, + const struct nk_style_progress *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); NKA__NK_C(border_color); + NKA__NK_I(cursor_normal); NKA__NK_I(cursor_hover); NKA__NK_I(cursor_active); NKA__NK_C(cursor_border_color); + NKA__NK_F(rounding); NKA__NK_F(border); NKA__NK_F(cursor_border); NKA__NK_F(cursor_rounding); + NKA__NK_V(padding); NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); +} + +static void nka__nk_scrollbar(struct nk_style_scrollbar *o, const struct nk_style_scrollbar *x, + const struct nk_style_scrollbar *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); NKA__NK_C(border_color); + NKA__NK_I(cursor_normal); NKA__NK_I(cursor_hover); NKA__NK_I(cursor_active); NKA__NK_C(cursor_border_color); + NKA__NK_F(border); NKA__NK_F(rounding); NKA__NK_F(border_cursor); NKA__NK_F(rounding_cursor); + NKA__NK_V(padding); NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); + NKA__NK_S(nka__nk_button, inc_button); NKA__NK_S(nka__nk_button, dec_button); +} + +static void nka__nk_edit(struct nk_style_edit *o, const struct nk_style_edit *x, + const struct nk_style_edit *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); NKA__NK_C(border_color); + NKA__NK_S(nka__nk_scrollbar, scrollbar); + NKA__NK_C(cursor_normal); NKA__NK_C(cursor_hover); NKA__NK_C(cursor_text_normal); NKA__NK_C(cursor_text_hover); + NKA__NK_C(text_normal); NKA__NK_C(text_hover); NKA__NK_C(text_active); + NKA__NK_C(selected_normal); NKA__NK_C(selected_hover); + NKA__NK_C(selected_text_normal); NKA__NK_C(selected_text_hover); + NKA__NK_F(border); NKA__NK_F(rounding); NKA__NK_F(cursor_size); + NKA__NK_V(scrollbar_size); NKA__NK_V(padding); NKA__NK_F(row_padding); + NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); +} + +static void nka__nk_property(struct nk_style_property *o, const struct nk_style_property *x, + const struct nk_style_property *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); NKA__NK_C(border_color); + NKA__NK_C(label_normal); NKA__NK_C(label_hover); NKA__NK_C(label_active); + NKA__NK_F(border); NKA__NK_F(rounding); NKA__NK_V(padding); + NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); + NKA__NK_S(nka__nk_edit, edit); + NKA__NK_S(nka__nk_button, inc_button); NKA__NK_S(nka__nk_button, dec_button); +} + +static void nka__nk_chart(struct nk_style_chart *o, const struct nk_style_chart *x, + const struct nk_style_chart *y, float t, int space) +{ + NKA__NK_I(background); NKA__NK_C(border_color); NKA__NK_C(selected_color); NKA__NK_C(color); + NKA__NK_F(border); NKA__NK_F(rounding); NKA__NK_V(padding); + NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); +} + +static void nka__nk_combo(struct nk_style_combo *o, const struct nk_style_combo *x, + const struct nk_style_combo *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); NKA__NK_C(border_color); + NKA__NK_C(label_normal); NKA__NK_C(label_hover); NKA__NK_C(label_active); + NKA__NK_C(symbol_normal); NKA__NK_C(symbol_hover); NKA__NK_C(symbol_active); + NKA__NK_S(nka__nk_button, button); + NKA__NK_F(border); NKA__NK_F(rounding); + NKA__NK_V(content_padding); NKA__NK_V(button_padding); NKA__NK_V(spacing); + NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); +} + +static void nka__nk_tab(struct nk_style_tab *o, const struct nk_style_tab *x, + const struct nk_style_tab *y, float t, int space) +{ + NKA__NK_I(background); NKA__NK_C(border_color); NKA__NK_C(text); + NKA__NK_S(nka__nk_button, tab_maximize_button); NKA__NK_S(nka__nk_button, tab_minimize_button); + NKA__NK_S(nka__nk_button, node_maximize_button); NKA__NK_S(nka__nk_button, node_minimize_button); + NKA__NK_F(border); NKA__NK_F(rounding); NKA__NK_F(indent); + NKA__NK_V(padding); NKA__NK_V(spacing); NKA__NK_F(color_factor); NKA__NK_F(disabled_factor); +} + +static void nka__nk_header(struct nk_style_window_header *o, const struct nk_style_window_header *x, + const struct nk_style_window_header *y, float t, int space) +{ + NKA__NK_I(normal); NKA__NK_I(hover); NKA__NK_I(active); + NKA__NK_S(nka__nk_button, close_button); NKA__NK_S(nka__nk_button, minimize_button); + NKA__NK_C(label_normal); NKA__NK_C(label_hover); NKA__NK_C(label_active); + NKA__NK_V(padding); NKA__NK_V(label_padding); NKA__NK_V(spacing); +} + +static void nka__nk_window(struct nk_style_window *o, const struct nk_style_window *x, + const struct nk_style_window *y, float t, int space) +{ + NKA__NK_S(nka__nk_header, header); + NKA__NK_I(fixed_background); NKA__NK_C(background); NKA__NK_C(border_color); + NKA__NK_C(popup_border_color); NKA__NK_C(combo_border_color); NKA__NK_C(contextual_border_color); + NKA__NK_C(menu_border_color); NKA__NK_C(group_border_color); NKA__NK_C(tooltip_border_color); + NKA__NK_I(scaler); + NKA__NK_F(border); NKA__NK_F(combo_border); NKA__NK_F(contextual_border); NKA__NK_F(menu_border); + NKA__NK_F(group_border); NKA__NK_F(tooltip_border); NKA__NK_F(popup_border); + NKA__NK_F(min_row_height_padding); NKA__NK_F(rounding); + NKA__NK_V(spacing); NKA__NK_V(scrollbar_size); NKA__NK_V(min_size); NKA__NK_V(padding); + NKA__NK_V(group_padding); NKA__NK_V(popup_padding); NKA__NK_V(combo_padding); + NKA__NK_V(contextual_padding); NKA__NK_V(menu_padding); NKA__NK_V(tooltip_padding); + NKA__NK_V(tooltip_offset); NKA__NK_F(tooltip_delay); +} + +/* t stays within 0 and 1: an overshoot would make sizes negative. */ +static void nka__nk_blend(const struct nk_style *x, const struct nk_style *y, float t, int space, struct nk_style *o) +{ + *o = t < 0.5f ? *x : *y; + if (t <= 0 || t >= 1) return; + NKA__NK_C(text.color); NKA__NK_V(text.padding); NKA__NK_F(text.color_factor); NKA__NK_F(text.disabled_factor); + NKA__NK_S(nka__nk_button, button); + NKA__NK_S(nka__nk_button, contextual_button); + NKA__NK_S(nka__nk_button, menu_button); + NKA__NK_S(nka__nk_toggle, option); + NKA__NK_S(nka__nk_toggle, checkbox); + NKA__NK_S(nka__nk_selectable, selectable); + NKA__NK_S(nka__nk_slider, slider); + NKA__NK_S(nka__nk_knob, knob); + NKA__NK_S(nka__nk_progress, progress); + NKA__NK_S(nka__nk_property, property); + NKA__NK_S(nka__nk_edit, edit); + NKA__NK_S(nka__nk_chart, chart); + NKA__NK_S(nka__nk_scrollbar, scrollh); + NKA__NK_S(nka__nk_scrollbar, scrollv); + NKA__NK_S(nka__nk_tab, tab); + NKA__NK_S(nka__nk_combo, combo); + NKA__NK_S(nka__nk_window, window); +} + +#undef NKA__NK_C +#undef NKA__NK_F +#undef NKA__NK_V +#undef NKA__NK_I +#undef NKA__NK_S + +static const struct nk_style *nka__nk_find(const struct nka_context *a, nk_hash style_id) +{ + const struct nka__nk_style *s = (const struct nka__nk_style *)nka__get(&a->nk_styles, style_id); + return s ? s->style : 0; +} + +NKA_API void nka_style_register(struct nka_context *a, nk_hash style_id, const struct nk_style *style) +{ + struct nka__nk_style *s; + struct nk_style *copy; + if (!a || !style) return; + copy = (struct nk_style *)nka__nk_find(a, style_id); + if (!copy) { + copy = (struct nk_style *)nka__alloc(a, sizeof *copy); + if (!copy) return; + s = (struct nka__nk_style *)nka__put(a, &a->nk_styles, style_id, 0); + if (!s) { nka__free(a, copy); return; } + s->style = copy; + } + *copy = *style; +} + +NKA_API void nka_style_register_current(struct nka_context *a, const struct nk_context *ctx, nk_hash style_id) +{ + if (ctx) nka_style_register(a, style_id, &ctx->style); +} + +NKA_API void nka_style_register_table(struct nka_context *a, struct nk_context *ctx, nk_hash style_id, + const struct nk_color *table) +{ + struct nk_style keep; + if (!a || !ctx) return; + keep = ctx->style; + nk_style_from_table(ctx, table); + nka_style_register(a, style_id, &ctx->style); + ctx->style = keep; +} + +NKA_API int nka_style_exists(const struct nka_context *a, nk_hash style_id) +{ + return a && nka__nk_find(a, style_id) != 0; +} + +NKA_API void nka_style_unregister(struct nka_context *a, nk_hash style_id) +{ + struct nk_style *style; + if (!a) return; + style = (struct nk_style *)nka__nk_find(a, style_id); + if (!style) return; + nka__free(a, style); + nka__del(&a->nk_styles, style_id); +} + +NKA_API void nka_style_blend_to(struct nka_context *a, nk_hash style_a, nk_hash style_b, float t, + struct nk_style *out, int space) +{ + const struct nk_style *x, *y; + if (!a || !out) return; + x = nka__nk_find(a, style_a); + y = nka__nk_find(a, style_b); + if (x && y) nka__nk_blend(x, y, t, space, out); +} + +NKA_API void nka_style_blend(struct nka_context *a, struct nk_context *ctx, nk_hash style_a, nk_hash style_b, + float t, int space) +{ + if (ctx) nka_style_blend_to(a, style_a, style_b, t, &ctx->style, space); +} + +NKA_API void nka_style_tween(struct nka_context *a, struct nk_context *ctx, nk_hash id, nk_hash target_style, + float dur, struct nka_ease ease, int space) +{ + struct nka__nk_style_tween *s; + const struct nk_style *to; + int fresh; + float t; + if (!a || !ctx) return; + to = nka__nk_find(a, target_style); + if (!to) return; + s = (struct nka__nk_style_tween *)nka__put(a, &a->nk_style_tweens, id, &fresh); + if (!s) return; + s->seen = a->frame; + if (fresh || s->target != target_style) { + if (!s->from) s->from = (struct nk_style *)nka__alloc(a, sizeof *s->from); + if (!s->from) return; + *s->from = ctx->style; + s->target = target_style; + s->start = a->time; + s->dur = dur; + s->ease = ease; + s->space = space; + s->active = 1; + } + if (!s->active) return; + t = s->dur > 0 ? (float)((a->time - s->start) / s->dur) : 1; + if (t >= 1) s->active = 0; + else a->busy = 1; + nka__nk_blend(s->from, to, nka_eval(a, s->ease, t), s->space, &ctx->style); +} + +static void nka__nk_free_tweens(struct nka_context *a) +{ + int i; + for (i = 0; i < a->nk_style_tweens.cap; ++i) + if (a->nk_style_tweens.keys[i]) + nka__free(a, ((struct nka__nk_style_tween *)nka__at(&a->nk_style_tweens, i))->from); + nka__map_free(a, &a->nk_style_tweens); +} + +static void nka__nk_gc(struct nka_context *a, unsigned max_age) +{ + int i = 0; + nka__sweep(&a->nk_scrolls, a->frame, max_age); + while (i < a->nk_style_tweens.cap) { + struct nka__nk_style_tween *s = (struct nka__nk_style_tween *)nka__at(&a->nk_style_tweens, i); + if (a->nk_style_tweens.keys[i] && a->frame - s->seen > max_age) { + nka__free(a, s->from); + nka__del_at(&a->nk_style_tweens, i); + } else ++i; + } +} + +static void nka__nk_free(struct nka_context *a) +{ + int i; + struct nka__nk_fonts *f = a->nk_fonts; + nka__free(a, a->nk_profiler); + a->nk_profiler = 0; + while (f) { + struct nka__nk_fonts *next = f->next; + nka__free(a, f); + f = next; + } + a->nk_fonts = 0; + for (i = 0; i < a->nk_styles.cap; ++i) + if (a->nk_styles.keys[i]) nka__free(a, ((struct nka__nk_style *)nka__at(&a->nk_styles, i))->style); + nka__map_free(a, &a->nk_styles); + nka__nk_free_tweens(a); + nka__map_free(a, &a->nk_scrolls); +} + +/* Lives until the next nka_update, as long as Nuklear's text commands need it. */ +static const struct nk_user_font *nka__nk_scaled(struct nka_context *a, const struct nk_user_font *font, float scale) +{ + struct nka__nk_fonts *f = a->nk_fonts; + struct nk_user_font *copy; + while (f && f->used == NKA__NK_FONTS) f = f->next; + if (!f) { + f = (struct nka__nk_fonts *)nka__alloc(a, sizeof *f); + if (!f) return font; + f->used = 0; + f->next = a->nk_fonts; + a->nk_fonts = f; + } + copy = &f->font[f->used++]; + *copy = *font; + copy->height = font->height * scale; + return copy; +} + +NKA_API struct nka_text_stagger_opts nka_text_stagger_opts_default(void) +{ + struct nka_text_stagger_opts o; + nka__zero(&o, sizeof o); + o.effect = NKA_TEXT_FX_FADE; + o.char_delay = 0.05f; + o.char_duration = 0.3f; + o.effect_intensity = 20; + o.ease = nka_ease(NKA_EASE_OUT_CUBIC); + o.color.r = o.color.g = o.color.b = o.color.a = 255; + o.font_scale = 1; + return o; +} + +static int nka__nk_glyph(const char *text, int left) +{ + nk_rune rune; + return nk_utf_decode(text, &rune, left); +} + +static int nka__nk_strlen(const char *text) +{ + int n = 0; + while (text[n]) ++n; + return n; +} + +NKA_API float nka_text_stagger_duration(const char *text, const struct nka_text_stagger_opts *opts) +{ + struct nka_text_stagger_opts d = nka_text_stagger_opts_default(); + int count = 0, i = 0, len; + if (!text) return 0; + if (!opts) opts = &d; + len = nka__nk_strlen(text); + while (i < len) { + int g = nka__nk_glyph(text + i, len - i); + if (!g) break; + i += g; + ++count; + } + return count ? (float)(count - 1) * opts->char_delay + opts->char_duration : 0; +} + +NKA_API float nka_text_stagger_width(const struct nk_context *ctx, const char *text, + const struct nka_text_stagger_opts *opts) +{ + struct nka_text_stagger_opts d = nka_text_stagger_opts_default(); + const struct nk_user_font *font; + float w = 0; + int i = 0, len; + if (!text) return 0; + if (!opts) opts = &d; + font = opts->font ? opts->font : (ctx ? ctx->style.font : 0); + if (!font) return 0; + len = nka__nk_strlen(text); + while (i < len) { + int g = nka__nk_glyph(text + i, len - i); + if (!g) break; + w += font->width(font->userdata, font->height, text + i, g) * opts->font_scale; + i += g; + if (i < len) w += opts->letter_spacing; + } + return w; +} + +NKA_API void nka_text_stagger(struct nka_context *a, struct nk_context *ctx, const char *text, float progress, + const struct nka_text_stagger_opts *opts) +{ + struct nka_text_stagger_opts d = nka_text_stagger_opts_default(); + const struct nk_user_font *font; + struct nk_command_buffer *out; + struct nk_color clear = { 0, 0, 0, 0 }; + float x, now, h; + int i = 0, index = 0, len; + if (!a || !ctx || !ctx->current || !text || !*text) return; + if (!opts) opts = &d; + font = opts->font ? opts->font : ctx->style.font; + if (!font) return; + out = nk_window_get_canvas(ctx); + len = nka__nk_strlen(text); + now = progress * nka_text_stagger_duration(text, opts); + h = font->height * opts->font_scale; + x = opts->pos.x; + while (i < len) { + int g = nka__nk_glyph(text + i, len - i); + float w, begin, k = 0, alpha = 1, scale = 1, dx = 0, dy = 0; + if (!g) break; + w = font->width(font->userdata, font->height, text + i, g) * opts->font_scale; + begin = (float)index * opts->char_delay; + if (now >= begin + opts->char_duration) k = 1; + else if (now > begin) k = nka_eval(a, opts->ease, (now - begin) / opts->char_duration); + if (k > 0 || opts->effect == NKA_TEXT_FX_WAVE) { + switch (opts->effect) { + case NKA_TEXT_FX_NONE: case NKA_TEXT_FX_TYPEWRITER: alpha = k > 0 ? 1.0f : 0.0f; break; + case NKA_TEXT_FX_SCALE: alpha = k; scale = k; break; + case NKA_TEXT_FX_SLIDE_UP: alpha = k; dy = (1 - k) * opts->effect_intensity; break; + case NKA_TEXT_FX_SLIDE_DOWN: alpha = k; dy = -(1 - k) * opts->effect_intensity; break; + case NKA_TEXT_FX_SLIDE_LEFT: alpha = k; dx = (1 - k) * opts->effect_intensity; break; + case NKA_TEXT_FX_SLIDE_RIGHT: alpha = k; dx = -(1 - k) * opts->effect_intensity; break; + case NKA_TEXT_FX_BOUNCE: alpha = k; scale = 1 - nka__back(1 - k, 1.70158f); break; + case NKA_TEXT_FX_WAVE: + dy = nka__sin(progress * 2 * NKA__PI + (float)index * 0.3f) * opts->effect_intensity * 0.5f; + break; + default: alpha = k; break; + } + if (alpha > 0 && scale > 0) { + struct nk_color c = opts->color; + const struct nk_user_font *f = font; + float s = scale * opts->font_scale; + c.a = (nk_byte)((float)c.a * (alpha < 1 ? alpha : 1) + 0.5f); + if (s != 1) f = nka__nk_scaled(a, font, s); + dx += w * (1 - scale) / 2; + dy += h * (1 - scale) / 2; + nk_draw_text(out, nk_rect(x + dx, opts->pos.y + dy, w * scale + h, h * scale), text + i, g, f, clear, c); + } + } + x += w + opts->letter_spacing; + i += g; + ++index; + } +} + +NKA_API struct nka_text_path_opts nka_text_path_opts_default(void) +{ + struct nka_text_path_opts o; + nka__zero(&o, sizeof o); + o.color.r = o.color.g = o.color.b = o.color.a = 255; + o.font_scale = 1; + return o; +} + +NKA_API float nka_text_path_width(const struct nk_context *ctx, const char *text, const struct nka_text_path_opts *opts) +{ + struct nka_text_stagger_opts s = nka_text_stagger_opts_default(); + struct nka_text_path_opts d = nka_text_path_opts_default(); + if (!opts) opts = &d; + s.font = opts->font; + s.font_scale = opts->font_scale; + s.letter_spacing = opts->letter_spacing; + return nka_text_stagger_width(ctx, text, &s); +} + +static void nka__nk_text_path(struct nka_context *a, struct nk_context *ctx, nk_hash path_id, const char *text, + float progress, const struct nka_text_path_opts *opts) +{ + struct nka_text_path_opts d = nka_text_path_opts_default(); + const struct nk_user_font *font, *drawn; + struct nk_command_buffer *out; + struct nk_color clear = { 0, 0, 0, 0 }; + float len, at, h, shown = 0; + int i = 0, index = 0, n, count = 0; + if (!a || !ctx || !ctx->current || !text || !*text) return; + if (!opts) opts = &d; + font = opts->font ? opts->font : ctx->style.font; + if (!font || !nka_path_exists(a, path_id)) return; + if (!nka_path_has_arc_lut(a, path_id)) nka_path_build_arc_lut(a, path_id, 64); + len = nka_path_length(a, path_id); + if (len <= 0) return; + n = nka__nk_strlen(text); + if (progress < 1) { + while (i < n) { + int g = nka__nk_glyph(text + i, n - i); + if (!g) break; + i += g; + ++count; + } + shown = (progress > 0 ? progress : 0) * (float)count; + i = 0; + } + drawn = opts->font_scale != 1 ? nka__nk_scaled(a, font, opts->font_scale) : font; + out = nk_window_get_canvas(ctx); + h = font->height * opts->font_scale; + at = opts->offset; + if (opts->align == NKA_TEXT_ALIGN_CENTER) at += (len - nka_text_path_width(ctx, text, opts)) / 2; + else if (opts->align == NKA_TEXT_ALIGN_END) at += len - nka_text_path_width(ctx, text, opts); + while (i < n) { + int g = nka__nk_glyph(text + i, n - i); + float w, mid; + struct nk_color c = opts->color; + if (!g || (progress < 1 && (float)index >= shown)) break; + w = font->width(font->userdata, font->height, text + i, g) * opts->font_scale; + mid = at + w / 2; + if (progress < 1 && shown - (float)index < 1) + c.a = (nk_byte)((float)c.a * (shown - (float)index) + 0.5f); + if (mid >= 0 && mid <= len) { + struct nk_vec2 p = nka_path_evaluate_at_distance(a, path_id, mid); + p.x += opts->origin.x; + p.y += opts->origin.y; + nk_draw_text(out, nk_rect(p.x - w / 2, opts->flip_y ? p.y : p.y - h, w + h, h), text + i, g, drawn, clear, c); + } + at += w + opts->letter_spacing; + i += g; + ++index; + } +} + +NKA_API void nka_text_path(struct nka_context *a, struct nk_context *ctx, nk_hash path_id, const char *text, + const struct nka_text_path_opts *opts) +{ + nka__nk_text_path(a, ctx, path_id, text, 1, opts); +} + +NKA_API void nka_text_path_animated(struct nka_context *a, struct nk_context *ctx, nk_hash path_id, const char *text, + float progress, const struct nka_text_path_opts *opts) +{ + nka__nk_text_path(a, ctx, path_id, text, progress < 1 ? progress : 1, opts); +} + +static struct nka__nk_profiler *nka__nk_prof(struct nka_context *a) +{ + if (!a->nk_profiler) { + a->nk_profiler = (struct nka__nk_profiler *)nka__alloc(a, sizeof *a->nk_profiler); + if (a->nk_profiler) nka__zero(a->nk_profiler, sizeof *a->nk_profiler); + } + return a->nk_profiler; +} + +static int nka__nk_ready(const struct nka_context *a) +{ + return a && a->nk_profiler && a->nk_profiler->enabled && a->nk_profiler->now; +} + +NKA_API void nka_profiler_set_clock(struct nka_context *a, double (*now)(void *user), void *user) +{ + struct nka__nk_profiler *p = a ? nka__nk_prof(a) : 0; + if (!p) return; + p->now = now; + p->user = user; +} + +NKA_API void nka_profiler_enable(struct nka_context *a, int on) +{ + struct nka__nk_profiler *p = a ? nka__nk_prof(a) : 0; + if (p) p->enabled = on != 0; +} + +NKA_API int nka_profiler_is_enabled(const struct nka_context *a) +{ + return a && a->nk_profiler && a->nk_profiler->enabled; +} + +NKA_API void nka_profiler_begin_frame(struct nka_context *a) +{ + struct nka__nk_profiler *p; + int i; + if (!nka__nk_ready(a)) return; + p = a->nk_profiler; + p->frame_start = p->now(p->user); + p->depth = 0; + for (i = 0; i < p->count; ++i) { + p->sections[i].ms = 0; + p->sections[i].calls = 0; + } +} + +NKA_API void nka_profiler_end_frame(struct nka_context *a) +{ + struct nka__nk_profiler *p; + int i; + if (!nka__nk_ready(a)) return; + p = a->nk_profiler; + p->frame_ms = (p->now(p->user) - p->frame_start) * 1000; + p->frame[p->at] = (float)p->frame_ms; + for (i = 0; i < p->count; ++i) p->sections[i].history[p->at] = (float)p->sections[i].ms; + p->at = (p->at + 1) % NKA__NK_HISTORY; +} + +/* Names are kept to 31 characters, so longer ones match on those. */ +static int nka__nk_named(const char *name, const char *kept) +{ + int k = 0; + while (k < 31 && name[k] && name[k] == kept[k]) ++k; + return k == 31 || name[k] == kept[k]; +} + +NKA_API void nka_profiler_begin(struct nka_context *a, const char *name) +{ + struct nka__nk_profiler *p; + int i, k; + if (!nka__nk_ready(a) || !name) return; + p = a->nk_profiler; + for (i = 0; i < p->count && !nka__nk_named(name, p->sections[i].name); ++i) continue; + if (i == p->count) { + if (p->count == NKA__NK_SECTIONS) return; + for (k = 0; k < 31 && name[k]; ++k) p->sections[i].name[k] = name[k]; + p->sections[i].name[k] = 0; + ++p->count; + } + p->sections[i].start = p->now(p->user); + p->sections[i].calls++; + if (p->depth < NKA__NK_STACK) p->stack[p->depth++] = i; +} + +NKA_API void nka_profiler_end(struct nka_context *a) +{ + struct nka__nk_profiler *p; + struct nka__nk_section *s; + if (!nka__nk_ready(a) || a->nk_profiler->depth <= 0) return; + p = a->nk_profiler; + s = &p->sections[p->stack[--p->depth]]; + s->ms += (p->now(p->user) - s->start) * 1000; +} + +/* Label text built without the varargs Nuklear may be compiled without. */ +struct nka__nk_text { char s[96]; int n; }; + +static void nka__nk_put(struct nka__nk_text *t, const char *s) +{ + while (*s && t->n < 95) t->s[t->n++] = *s++; + t->s[t->n] = 0; +} + +static void nka__nk_hex(struct nka__nk_text *t, nk_hash v) +{ + static const char digits[] = "0123456789ABCDEF"; + char s[11]; + int k; + s[0] = '0'; + s[1] = 'x'; + for (k = 0; k < 8; ++k) s[2 + k] = digits[(v >> ((7 - k) * 4)) & 15]; + s[10] = 0; + nka__nk_put(t, s); +} + +static void nka__nk_num(struct nka__nk_text *t, float v, int decimals) +{ + char s[24], digits[16]; + int i = 0, n = 0, k; + unsigned long scale = 1, x; + for (k = 0; k < decimals; ++k) scale *= 10; + if (v < 0) { s[i++] = '-'; v = -v; } + if (v > 1e6f) v = 1e6f; + x = (unsigned long)(v * (float)scale + 0.5f); + do { digits[n++] = (char)('0' + x % 10); x /= 10; } while (x || n <= decimals); + while (n > 0) { + s[i++] = digits[--n]; + if (n == decimals && n) s[i++] = '.'; + } + s[i] = 0; + nka__nk_put(t, s); +} + +static void nka__nk_ease(struct nka__nk_text *t, int type) +{ + static const char *families[] = { "quad", "cubic", "quart", "quint", "sine", "expo", "circ", "back", "elastic", "bounce" }; + static const char *variants[] = { "in ", "out ", "in-out " }; + static const char *others[] = { "steps", "cubic bezier", "spring", "custom" }; + if (type == NKA_EASE_LINEAR) nka__nk_put(t, "linear"); + else if (type > 0 && type <= NKA_EASE_IN_OUT_BOUNCE) { + nka__nk_put(t, variants[(type - 1) % 3]); + nka__nk_put(t, families[(type - 1) / 3]); + } else if (type >= NKA_EASE_STEPS && type < NKA_EASE_COUNT) nka__nk_put(t, others[type - NKA_EASE_STEPS]); + else nka__nk_put(t, "?"); +} + +static void nka__nk_value(struct nk_context *ctx, const char *prefix, float v, int decimals) +{ + struct nka__nk_text t; + t.n = 0; + nka__nk_put(&t, prefix); + nka__nk_put(&t, ": "); + nka__nk_num(&t, v, decimals); + nk_label(ctx, t.s, NK_TEXT_LEFT); +} + +static void nka__nk_plot(struct nk_context *ctx, const float *values, int at, float height) +{ + float top = 0.01f; + int i; + for (i = 0; i < NKA__NK_HISTORY; ++i) if (values[i] > top) top = values[i]; + nk_layout_row_dynamic(ctx, height, 1); + if (nk_chart_begin(ctx, NK_CHART_LINES, NKA__NK_HISTORY, 0, top * 1.2f)) { + for (i = 0; i < NKA__NK_HISTORY; ++i) nk_chart_push(ctx, values[(at + i) % NKA__NK_HISTORY]); + nk_chart_end(ctx); + } +} + +static void nka__nk_tweens(struct nka_context *a, struct nk_context *ctx) +{ + static const char *kinds[] = { "Float", "Vec2", "Vec4", "Int", "Color" }; + int count[5] = { 0, 0, 0, 0, 0 }, moving = 0, shown = 0, i; + for (i = 0; i < a->chans.cap; ++i) { + const struct nka__chan *c = (const struct nka__chan *)nka__at(&a->chans, i); + if (!a->chans.keys[i]) continue; + ++count[c->kind]; + moving += !c->sleeping; + } + nk_layout_row_dynamic(ctx, 18, 2); + for (i = 0; i < 5; ++i) nka__nk_value(ctx, kinds[i], (float)count[i], 0); + nka__nk_value(ctx, "Animate", (float)a->anims.len, 0); + nka__nk_value(ctx, "Total", (float)(a->chans.len + a->anims.len), 0); + nka__nk_value(ctx, "Moving", (float)moving, 0); + for (i = 0; i < a->chans.cap && shown < 12; ++i) { + const struct nka__chan *c = (const struct nka__chan *)nka__at(&a->chans, i); + struct nka__nk_text id, ch; + nk_size progress; + if (!a->chans.keys[i] || c->sleeping) continue; + progress = (nk_size)(nka__clamp01((float)((a->time - c->start) / c->dur)) * 100); + id.n = ch.n = 0; + nka__nk_hex(&id, c->id); + nka__nk_hex(&ch, c->ch); + nk_layout_row_dynamic(ctx, 18, 4); + nk_label(ctx, kinds[c->kind], NK_TEXT_LEFT); + nk_label(ctx, id.s, NK_TEXT_LEFT); + nk_label(ctx, ch.s, NK_TEXT_LEFT); + nk_progress(ctx, &progress, 100, NK_FIXED); + ++shown; + } +} + +static void nka__nk_performance(struct nka_context *a, struct nk_context *ctx) +{ + struct nka__nk_profiler *p = nka__nk_prof(a); + nk_bool on; + int i; + if (!p) return; + on = p->enabled; + nk_layout_row_dynamic(ctx, 22, 1); + if (nk_checkbox_label(ctx, "Profiler", &on)) p->enabled = on != 0; + if (!p->now) { + nk_label(ctx, "Give it a clock with nka_profiler_set_clock.", NK_TEXT_LEFT); + return; + } + if (!p->enabled) return; + { + float sum = 0, top = 0; + for (i = 0; i < NKA__NK_HISTORY; ++i) { + sum += p->frame[i]; + if (p->frame[i] > top) top = p->frame[i]; + } + nk_layout_row_dynamic(ctx, 18, 3); + nka__nk_value(ctx, "Frame ms", (float)p->frame_ms, 2); + nka__nk_value(ctx, "Avg", sum / NKA__NK_HISTORY, 2); + nka__nk_value(ctx, "Max", top, 2); + nka__nk_plot(ctx, p->frame, p->at, 60); + } + if (!p->count) { + nk_layout_row_dynamic(ctx, 18, 1); + nk_label(ctx, "Time sections with nka_profiler_begin and _end.", NK_TEXT_LEFT); + } + for (i = 0; i < p->count; ++i) { + nk_layout_row_dynamic(ctx, 18, 3); + nk_label(ctx, p->sections[i].name, NK_TEXT_LEFT); + nka__nk_value(ctx, "ms", (float)p->sections[i].ms, 2); + nka__nk_value(ctx, "calls", (float)p->sections[i].calls, 0); + nka__nk_plot(ctx, p->sections[i].history, p->at, 30); + } +} + +static void nka__nk_say(struct nk_command_buffer *out, const struct nk_user_font *font, struct nk_rect r, + const char *text, int len, struct nk_color color) +{ + struct nk_color clear = { 0, 0, 0, 0 }; + nk_draw_text(out, r, text, len, font, clear, color); +} + +NKA_API void nka_show_debug_timeline(struct nka_context *a, struct nk_context *ctx, nk_hash instance_id) +{ + static const char *types[] = { "float", "vec2", "vec4", "int", "color", "float rel", "vec2 rel", "vec4 rel", "color rel" }; + static const struct nk_color c1 = { 91, 194, 231, 255 }, c2 = { 204, 120, 88, 255 }; + static const struct nk_color c1_dim = { 91, 194, 231, 80 }, c2_dim = { 204, 120, 88, 80 }; + static const struct nk_color c1_lit = { 120, 210, 240, 255 }, c2_lit = { 230, 140, 110, 255 }; + static const struct nk_color ground = { 30, 32, 40, 255 }, lane = { 40, 44, 55, 255 }, grid = { 60, 65, 80, 255 }; + static const struct nk_color ink = { 180, 185, 195, 255 }, head = { 255, 255, 255, 220 }; + static const struct nk_color wait = { 50, 55, 70, 255 }, hatch = { 70, 75, 90, 255 }; + const struct nka__clip_inst *in; + const struct nka__clip_clip *c; + const struct nk_user_font *font; + struct nk_command_buffer *out; + struct nka__nk_text t; + struct nk_rect r; + float lh, label, x0, w, y0, dur, total, ry, step, g, at; + int tracks, i, k; + if (!a || !ctx || !ctx->current || !ctx->style.font) return; + font = ctx->style.font; + lh = font->height; + in = nka__clip_inst(a, instance_id); + c = in ? nka__clip_get(a, in->clip) : 0; + t.n = 0; + if (!c) { + nka__nk_put(&t, in ? "No clip " : "No instance "); + nka__nk_hex(&t, in ? in->clip : instance_id); + nk_layout_row_dynamic(ctx, lh + 6, 1); + nk_label(ctx, t.s, NK_TEXT_LEFT); + return; + } + tracks = c->ntracks > 0 ? c->ntracks : 1; + nk_layout_row_dynamic(ctx, 44 + (float)tracks * 22 + 2 + 36 + 4, 1); + if (nk_widget(&r, ctx) == NK_WIDGET_INVALID) return; + out = nk_window_get_canvas(ctx); + nk_fill_rect(out, r, 4, ground); + + nka__nk_put(&t, "Clip "); + nka__nk_hex(&t, c->id); + nka__nk_put(&t, " "); + if (in->delay > 0) { + nka__nk_put(&t, "delay "); + nka__nk_num(&t, in->delay, 2); + nka__nk_put(&t, "s "); + } else { + nka__nk_num(&t, in->time, 2); + nka__nk_put(&t, "s / "); + nka__nk_num(&t, c->duration, 2); + nka__nk_put(&t, "s "); + } + nka__nk_put(&t, in->paused ? "paused" : !in->playing ? "stopped" : in->delay > 0 ? "waiting" : "playing"); + nka__nk_say(out, font, nk_rect(r.x + 4, r.y + 4, r.w - 8, lh + 2), t.s, t.n, in->playing ? c1 : ink); + + label = font->width(font->userdata, font->height, "color rel", 9) + 8; + x0 = r.x + 4 + label; + w = r.w - label - 8; + if (w < 100) w = 100; + y0 = r.y + 44; + dur = c->duration > 0 ? c->duration : 1; + total = c->delay + dur; + step = dur > 10 ? 2.0f : dur > 5 ? 1.0f : 0.5f; + for (i = 0; i < c->ntracks; ++i) { + const struct nka__clip_track *tr = &c->tracks[i]; + float ty = y0 + (float)i * 22; + const char *name = tr->type >= 0 && tr->type < 9 ? types[tr->type] : "?"; + nk_fill_rect(out, nk_rect(x0, ty, w, 20), 2, lane); + nka__nk_say(out, font, nk_rect(r.x + 4, ty + (20 - lh) / 2, label, lh + 2), name, nka__nk_strlen(name), ink); + if (c->delay > 0) { + float end = x0 + c->delay / total * w; + nk_fill_rect(out, nk_rect(x0, ty + 2, end - x0, 16), 2, wait); + for (g = x0 + 4; g < end - 4; g += 12) nk_stroke_line(out, g, ty + 4, g + 6, ty + 16, 1, hatch); + } + for (k = 0; k < tr->count; ++k) { + const struct nka__clip_key *key = &c->keys[tr->first + k]; + float next = k + 1 < tr->count ? c->keys[tr->first + k + 1].time : dur; + float x1 = x0 + (c->delay + key->time) / total * w, x2 = x0 + (c->delay + next) / total * w; + int on = in->delay <= 0 && in->time >= key->time && in->time < next; + struct nk_rect seg = nk_rect(x1, ty + 2, x2 - x1, 16), dot = nk_rect(x1 - 8, ty + 2, 16, 16); + if (on) nk_fill_rect(out, nk_rect(x1 - 1, ty + 1, x2 - x1 + 2, 18), 3, i & 1 ? c2_lit : c1_lit); + nk_fill_rect(out, seg, 2, on ? (i & 1 ? c2 : c1) : (i & 1 ? c2_dim : c1_dim)); + nk_fill_circle(out, dot, on ? head : (i & 1 ? c2 : c1)); + t.n = 0; + if (nk_input_is_mouse_hovering_rect(&ctx->input, dot)) { + nk_stroke_circle(out, nk_rect(x1 - 11, ty - 1, 22, 22), 2, head); + nka__nk_num(&t, key->time, 3); + nka__nk_put(&t, "s "); + nka__nk_ease(&t, key->ease); + nk_tooltip(ctx, t.s); + } else if (nk_input_is_mouse_hovering_rect(&ctx->input, seg)) { + nk_stroke_rect(out, seg, 2, 2, head); + nka__nk_num(&t, key->time, 2); + nka__nk_put(&t, "s - "); + nka__nk_num(&t, next, 2); + nka__nk_put(&t, "s "); + nka__nk_ease(&t, key->ease); + nk_tooltip(ctx, t.s); + } + } + for (g = step; g < dur; g += step) { + float x = x0 + (c->delay + g) / total * w; + nk_stroke_line(out, x, ty, x, ty + 20, 1, grid); + } + } + if (!c->ntracks) { + nk_fill_rect(out, nk_rect(x0, y0, w, 20), 2, lane); + nka__nk_say(out, font, nk_rect(x0 + 10, y0 + (20 - lh) / 2, w - 10, lh + 2), "No tracks", 9, ink); + } + + ry = y0 + (float)tracks * 22 + 2; + nk_fill_rect(out, nk_rect(x0, ry, w, 36), 0, lane); + step = total > 10 ? 2.0f : total > 5 ? 1.0f : total < 1 ? 0.1f : 0.5f; + for (g = 0; g <= total + 0.001f; g += step) { + float x = x0 + g / total * w, tw; + nk_stroke_line(out, x, ry, x, ry + 6, 1, grid); + t.n = 0; + nka__nk_num(&t, g, 1); + nka__nk_put(&t, "s"); + tw = font->width(font->userdata, font->height, t.s, t.n); + if (x + tw / 2 < x0 + w) nka__nk_say(out, font, nk_rect(x - tw / 2, ry + 6, tw + 2, lh + 2), t.s, t.n, ink); + } + if (c->delay > 0) { + float x = x0 + c->delay / total * w; + nk_stroke_line(out, x, ry, x, ry + 36, 2, c1_dim); + nka__nk_say(out, font, nk_rect(x + 2, ry + 2, w, lh + 2), "delay end", 9, c1_dim); + } + + at = in->delay > 0 ? c->delay - in->delay : c->delay + in->time; + if (at >= 0 && at <= total) { + float x = x0 + at / total * w; + nk_stroke_line(out, x, y0, x, ry + 36, 2, head); + nk_fill_triangle(out, x, y0, x - 5, y0 - 8, x + 5, y0 - 8, head); + } +} + +static void nka__nk_clips(struct nka_context *a, struct nk_context *ctx) +{ + int i, shown = 0; + nk_layout_row_dynamic(ctx, 18, 2); + nka__nk_value(ctx, "Clips", (float)a->clip_clips.len, 0); + nka__nk_value(ctx, "Instances", (float)a->clip_insts.len, 0); + for (i = 0; i < a->clip_insts.cap && shown < 16; ++i) { + const struct nka__clip_inst *in = (const struct nka__clip_inst *)nka__at(&a->clip_insts, i); + struct nka__nk_text t; + nk_hash id; + if (!a->clip_insts.keys[i]) continue; + id = in->id; + t.n = 0; + nka__nk_put(&t, "Instance "); + nka__nk_hex(&t, id); + if (nk_tree_push_hashed(ctx, NK_TREE_NODE, t.s, NK_MINIMIZED, (const char *)&id, (int)sizeof id, 0)) { + nk_layout_row_dynamic(ctx, 18, 2); + t.n = 0; + nka__nk_put(&t, "Clip: "); + nka__nk_hex(&t, in->clip); + nk_label(ctx, t.s, NK_TEXT_LEFT); + nka__nk_value(ctx, "Time", in->time, 2); + nk_label(ctx, in->paused ? "Paused" : in->playing ? "Playing" : "Stopped", NK_TEXT_LEFT); + nka__nk_value(ctx, "Loops left", (float)in->loops, 0); + nka_show_debug_timeline(a, ctx, id); + nk_tree_pop(ctx); + } + ++shown; + } +} + +NKA_API void nka_show_unified_inspector(struct nka_context *a, struct nk_context *ctx, int *open) +{ + static const char *name = "NukAnim"; + nk_flags flags = NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | NK_WINDOW_TITLE; + if (!a || !ctx || (open && !*open)) return; + if (open) { + flags |= NK_WINDOW_CLOSABLE; + nk_window_show(ctx, name, NK_SHOWN); + } + if (nk_begin(ctx, name, nk_rect(20, 20, 380, 520), flags)) { + if (nk_tree_push(ctx, NK_TREE_TAB, "Time scale", NK_MAXIMIZED)) { + float scale = a->scale; + nk_layout_row_dynamic(ctx, 22, 1); + nka__nk_value(ctx, "Time scale", scale, 2); + nk_slider_float(ctx, 0, &scale, 2, 0.05f); + nk_layout_row_dynamic(ctx, 22, 5); + if (nk_button_label(ctx, "0.1x")) scale = 0.1f; + if (nk_button_label(ctx, "0.25x")) scale = 0.25f; + if (nk_button_label(ctx, "0.5x")) scale = 0.5f; + if (nk_button_label(ctx, "1x")) scale = 1; + if (nk_button_label(ctx, "2x")) scale = 2; + nka_set_time_scale(a, scale); + nk_tree_pop(ctx); + } + if (nk_tree_push(ctx, NK_TREE_TAB, "Tweens", NK_MAXIMIZED)) { + nka__nk_tweens(a, ctx); + nk_tree_pop(ctx); + } + if (nk_tree_push(ctx, NK_TREE_TAB, "Clips", NK_MAXIMIZED)) { + nka__nk_clips(a, ctx); + nk_tree_pop(ctx); + } + if (nk_tree_push(ctx, NK_TREE_TAB, "Performance", NK_MINIMIZED)) { + nka__nk_performance(a, ctx); + nk_tree_pop(ctx); + } + } else if (open) { + *open = 0; + } + nk_end(ctx); +} + +/* Lifetime and frames */ + +NKA_API struct nka_context *nka_create(const struct nk_allocator *alloc) +{ + struct nk_allocator al; + struct nka_context *a; + if (alloc) al = *alloc; + else { +#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR + al.userdata.ptr = 0; + al.alloc = nka__malloc; + al.free = nka__mfree; +#else + return 0; +#endif + } + a = (struct nka_context *)al.alloc(al.userdata, 0, sizeof *a); + if (!a) return 0; + nka__zero(a, sizeof *a); + a->alloc = al; + a->scale = 1; + a->lazy = 1; + a->chans.size = sizeof(struct nka__chan); + a->anims.size = sizeof(struct nka__anim); + a->path_map.size = sizeof(struct nka__path_data); + a->clip_clips.size = sizeof(struct nka__clip_clip); + a->clip_insts.size = sizeof(struct nka__clip_inst); + a->clip_layers.size = sizeof(struct nka__clip_layer); + a->clip_acc.size = sizeof(struct nka__clip_acc); + a->fx_clocks.size = sizeof(struct nka__fx_clock); + a->fx_grads.size = sizeof(struct nka__fx_grad); + a->fx_xforms.size = sizeof(struct nka__fx_xform); + a->fx_drags.size = sizeof(struct nka__fx_drag); + a->nk_scrolls.size = sizeof(struct nka__nk_scroll); + a->nk_styles.size = sizeof(struct nka__nk_style); + a->nk_style_tweens.size = sizeof(struct nka__nk_style_tween); + return a; +} + +NKA_API void nka_destroy(struct nka_context *a) +{ + struct nk_allocator al; + if (!a) return; + nka__path_free(a); + nka__clip_destroy(a); + nka__map_free(a, &a->fx_clocks); + nka__map_free(a, &a->fx_grads); + nka__map_free(a, &a->fx_xforms); + nka__map_free(a, &a->fx_drags); + nka__nk_free(a); + nka__map_free(a, &a->chans); + nka__map_free(a, &a->anims); + al = a->alloc; + al.free(al.userdata, a); +} + +NKA_API void nka_update(struct nka_context *a, float dt) +{ + if (!a) return; + a->frame++; + a->dt = (dt > 0 ? dt : 0) * a->scale; + a->time += a->dt; + a->busy = 0; + nka__clip_update(a); + { + struct nka__nk_fonts *f; + for (f = a->nk_fonts; f; f = f->next) f->used = 0; + } +} + +NKA_API int nka_busy(const struct nka_context *a) { return a ? a->busy : 0; } +NKA_API double nka_time(const struct nka_context *a) { return a ? a->time : 0; } + +NKA_API void nka_gc(struct nka_context *a, unsigned max_age) +{ + if (!a) return; + nka__sweep(&a->chans, a->frame, max_age); + nka__sweep(&a->anims, a->frame, max_age); + nka__clip_gc(a, max_age); + nka__sweep(&a->fx_clocks, a->frame, max_age); + nka__sweep(&a->fx_grads, a->frame, max_age); + nka__sweep(&a->fx_xforms, a->frame, max_age); + nka__sweep(&a->fx_drags, a->frame, max_age); + nka__nk_gc(a, max_age); +} + +NKA_API void nka_clear(struct nka_context *a) +{ + if (!a) return; + nka__map_free(a, &a->chans); + nka__map_free(a, &a->anims); + nka__clip_clear(a); + nka__map_free(a, &a->fx_clocks); + nka__map_free(a, &a->fx_grads); + nka__map_free(a, &a->fx_xforms); + nka__map_free(a, &a->fx_drags); + nka__nk_free_tweens(a); + nka__map_free(a, &a->nk_scrolls); +} + +NKA_API void nka_reserve(struct nka_context *a, int tweens) +{ + int cap = 16; + if (!a) return; + while (cap < tweens * 2) cap *= 2; + if (cap > a->chans.cap) nka__grow(a, &a->chans, cap); +} + +NKA_API void nka_set_time_scale(struct nka_context *a, float scale) { if (a) a->scale = scale > 0 ? scale : 0; } +NKA_API float nka_time_scale(const struct nka_context *a) { return a ? a->scale : 1; } +NKA_API void nka_set_lazy_init(struct nka_context *a, int on) { if (a) a->lazy = on != 0; } +NKA_API int nka_lazy_init(const struct nka_context *a) { return a ? a->lazy : 1; } + +#endif /* NKA__IMPLEMENTED */ +#endif /* NUKANIM_IMPLEMENTATION */ + +/* +MIT License + +Copyright (c) 2026 Alpaq92 +Copyright (c) 2025 SoufianeKHIAT (ImAnim) +Copyright (c) 2024-2025 Raidcore (ImAnimate) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ diff --git a/tests/clip.c b/tests/clip.c new file mode 100644 index 0000000..8790bfb --- /dev/null +++ b/tests/clip.c @@ -0,0 +1,1841 @@ +#include +#include +#include + +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#include "nuklear.h" +#define NUKANIM_IMPLEMENTATION +#include "nukanim.h" + +static int failures; + +static void check(int ok, const char *what, int line) +{ + if (ok) return; + printf("clip.c:%d: %s\n", line, what); + failures++; +} + +#define CHECK(c) check((c) != 0, #c, __LINE__) +#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps)) + +static nk_hash H(const char *s) { return nka_id(s); } + +static struct nk_vec2 v2(float x, float y) +{ + struct nk_vec2 v; + v.x = x; v.y = y; + return v; +} + +static struct nka_vec4 v4(float x, float y, float z, float w) +{ + struct nka_vec4 v; + v.x = x; v.y = y; v.z = z; v.w = w; + return v; +} + +static struct nk_colorf rgba(float r, float g, float b, float al) +{ + struct nk_colorf c; + c.r = r; c.g = g; c.b = b; c.a = al; + return c; +} + +static float getf(struct nka_context *a, nk_hash inst, const char *ch) +{ + float v = -12345; + nka_instance_get_float(a, inst, H(ch), &v); + return v; +} + +static int geti(struct nka_context *a, nk_hash inst, const char *ch) +{ + int v = -12345; + nka_instance_get_int(a, inst, H(ch), &v); + return v; +} + +static struct nka__clip_inst *inst_of(struct nka_context *a, nk_hash inst) +{ + return (struct nka__clip_inst *)nka__get(&a->clip_insts, inst); +} + +static void key2(struct nka_context *a, const char *ch, float t0, float x0, float t1, float x1) +{ + nka_clip_key_float(a, H(ch), t0, x0, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H(ch), t1, x1, NKA_EASE_LINEAR, 0); +} + +static void steps(struct nka_context *a, int n, float dt) +{ + while (n-- > 0) nka_update(a, dt); +} + +/* Callback logs */ + +static char events[256]; +static int nevents, loops_seen[64], nloops; + +static void ev(char c) +{ + if (nevents < 255) events[nevents++] = c; + events[nevents] = 0; +} + +static void reset_events(void) +{ + nevents = 0; + events[0] = 0; + nloops = 0; +} + +static void on_begin(struct nka_context *a, nk_hash inst, void *user) { (void)a; (void)inst; (void)user; ev('B'); } +static void on_update(struct nka_context *a, nk_hash inst, void *user) { (void)a; (void)inst; (void)user; ev('U'); } +static void on_complete(struct nka_context *a, nk_hash inst, void *user) { (void)a; (void)inst; (void)user; ev('C'); } +static void on_pause(struct nka_context *a, nk_hash inst, void *user) { (void)a; (void)inst; (void)user; ev('P'); } + +static void on_loop(struct nka_context *a, nk_hash inst, int loop, void *user) +{ + (void)a; (void)inst; (void)user; + ev('L'); + if (nloops < 64) loops_seen[nloops++] = loop; +} + +static struct { + nk_hash inst[64], id[64]; + float time[64]; + int n; +} marks; + +static void on_mark(struct nka_context *a, nk_hash inst, nk_hash id, float time, void *user) +{ + (void)a; (void)user; + if (marks.n >= 64) return; + marks.inst[marks.n] = inst; + marks.id[marks.n] = id; + marks.time[marks.n] = time; + marks.n++; +} + +static int marks_are(nk_hash a0, nk_hash a1, nk_hash a2, nk_hash a3) +{ + nk_hash want[4]; + int i; + want[0] = a0; want[1] = a1; want[2] = a2; want[3] = a3; + for (i = 0; i < marks.n && i < 4; ++i) + if (marks.id[i] != want[i]) return 0; + return 1; +} + +/* Keys and channels */ + +static void test_keys(void) +{ + struct nka_context *a = nka_create(0); + nk_hash clip = H("keys"), inst = H("k"); + const float bz[4] = { 0.25f, 0.1f, 0.25f, 1 }; + struct nk_colorf red = rgba(1, 0, 0, 1), blue = rgba(0, 0, 1, 0.5f), c, want; + struct nk_vec2 p; + struct nka_vec4 q; + int k, s; + + nka_clip_begin(a, clip); + nka_clip_key_float(a, H("f"), 2, 30, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("f"), 1, 10, NKA_EASE_OUT_QUAD, 0); + nka_clip_key_float(a, H("f"), 0, 0, NKA_EASE_LINEAR, 0); + nka_clip_key_vec2(a, H("v2"), 0, v2(0, 0), NKA_EASE_IN_QUAD, 0); + nka_clip_key_vec2(a, H("v2"), 2, v2(10, 20), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4(a, H("v4"), 0, v4(1, 2, 3, 4), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4(a, H("v4"), 2, v4(5, 6, 7, 8), NKA_EASE_LINEAR, 0); + nka_clip_key_int(a, H("i"), 0, 10, NKA_EASE_LINEAR); + nka_clip_key_int(a, H("i"), 2, 0, NKA_EASE_LINEAR); + for (s = NKA_COL_SRGB; s <= NKA_COL_OKLCH; ++s) { + nka_clip_key_color(a, nka_id_mix(H("c"), (nk_hash)s), 0, red, s, NKA_EASE_LINEAR, 0); + nka_clip_key_color(a, nka_id_mix(H("c"), (nk_hash)s), 2, blue, s, NKA_EASE_LINEAR, 0); + } + nka_clip_key_float(a, H("bz"), 0, 0, NKA_EASE_CUBIC_BEZIER, bz); + nka_clip_key_float(a, H("bz"), 2, 100, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("nobz"), 0, 0, NKA_EASE_LINEAR, bz); + nka_clip_key_float(a, H("nobz"), 2, 100, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("late"), 1, 5, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("late"), 1.5f, 7, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("step"), 2, 3, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("step"), 1, 1, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("step"), 0, 0, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("step"), 1, 2, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("steps"), 0, 0, NKA_EASE_STEPS, 0); + nka_clip_key_float(a, H("steps"), 2, 1, NKA_EASE_LINEAR, 0); + nka_clip_key_int(a, H("big"), 0, -2147483647 - 1, NKA_EASE_LINEAR); + nka_clip_key_int(a, H("big"), 2, 2147483647, NKA_EASE_LINEAR); + nka_clip_key_int(a, H("near"), 0, 150, NKA_EASE_LINEAR); + nka_clip_key_int(a, H("near"), 1, 2147483647, NKA_EASE_IN_BACK); + nka_clip_key_int(a, H("near"), 2, 2147483000, NKA_EASE_LINEAR); + CHECK(nka_clip_end(a) == NKA_OK); + CHECK(nka_clip_exists(a, clip) && nka_clip_duration(a, clip) == 2); + CHECK(nka_play(a, clip, inst) == inst && nka_instance_duration(a, inst) == 2); + + for (k = 0; k <= 16; ++k) { + float t = (float)k / 8, u = t / 2; + nka_instance_seek(a, inst, t); + CHECK(nka_instance_time(a, inst) == t); + CHECK(NEAR(getf(a, inst, "f"), t <= 1 ? 10 * t : 10 + 20 * nka_eval_preset(NKA_EASE_OUT_QUAD, t - 1), 1e-5f)); + CHECK(nka_instance_get_vec2(a, inst, H("v2"), &p)); + CHECK(NEAR(p.x, 10 * u * u, 1e-5f) && NEAR(p.y, 20 * u * u, 1e-5f)); + CHECK(nka_instance_get_vec4(a, inst, H("v4"), &q)); + CHECK(NEAR(q.x, 1 + 4 * u, 1e-5f) && NEAR(q.y, 2 + 4 * u, 1e-5f) && NEAR(q.z, 3 + 4 * u, 1e-5f) && NEAR(q.w, 4 + 4 * u, 1e-5f)); + CHECK(geti(a, inst, "i") == 10 + (int)floorf(-10 * u + 0.5f)); + for (s = NKA_COL_SRGB; s <= NKA_COL_OKLCH; ++s) { + CHECK(nka_instance_get_color(a, inst, nka_id_mix(H("c"), (nk_hash)s), &c)); + want = nka_color_blend(red, blue, u, s); + CHECK(NEAR(c.r, want.r, 1e-5f) && NEAR(c.g, want.g, 1e-5f) && NEAR(c.b, want.b, 1e-5f) && NEAR(c.a, want.a, 1e-5f)); + } + CHECK(NEAR(getf(a, inst, "bz"), 100 * nka_eval(a, nka_ease_bezier(bz[0], bz[1], bz[2], bz[3]), u), 1e-4f)); + CHECK(NEAR(getf(a, inst, "nobz"), 100 * u, 1e-4f)); + CHECK(NEAR(getf(a, inst, "late"), t <= 1 ? 5 : (t >= 1.5f ? 7 : 5 + 4 * (t - 1)), 1e-5f)); + CHECK(NEAR(getf(a, inst, "step"), t <= 1 ? t : 1 + t, 1e-5f)); + CHECK(getf(a, inst, "steps") == (t < 2 ? 0 : 1)); + } + /* Ints round half up on the way down too: upstream sat one above until the last key. */ + nka_instance_seek(a, inst, 1.9f); + CHECK(geti(a, inst, "i") == 1); + nka_instance_seek(a, inst, 1.96f); + CHECK(geti(a, inst, "i") == 0); + nka_instance_seek(a, inst, 0.5f); + CHECK(geti(a, inst, "i") == 8); + /* Ints of any size land exactly on their keys, and overshoot saturates. */ + nka_instance_seek(a, inst, 0); + CHECK(geti(a, inst, "big") == -2147483647 - 1 && geti(a, inst, "near") == 150); + nka_instance_seek(a, inst, 1); + CHECK(geti(a, inst, "big") == 0 && geti(a, inst, "near") == 2147483647); + nka_instance_seek(a, inst, 1.25f); + CHECK(geti(a, inst, "near") == 2147483647); + nka_instance_seek(a, inst, 1.95f); + CHECK(geti(a, inst, "near") < 2147483647 - 400 && geti(a, inst, "near") > 2147483000); + nka_instance_seek(a, inst, 2); + CHECK(geti(a, inst, "big") == 2147483647 && geti(a, inst, "near") == 2147483000); + + nka_instance_restart(a, inst); + CHECK(nka_instance_time(a, inst) == 0 && getf(a, inst, "f") == 0); + nka_update(a, 0.5f); + CHECK(getf(a, inst, "f") == 5); + nka_update(a, 1); + CHECK(NEAR(getf(a, inst, "f"), 25, 1e-5f)); + nka_update(a, 0.5f); + CHECK(getf(a, inst, "f") == 30 && nka_instance_is_playing(a, inst)); + nka_update(a, 0.25f); + CHECK(getf(a, inst, "f") == 30 && !nka_instance_is_playing(a, inst)); + + /* A channel the clip lacks reads zero. Upstream said yes for floats and ints. */ + { + float f = 5; + int i = 5; + CHECK(!nka_instance_get_float(a, inst, H("none"), &f) && f == 0); + CHECK(!nka_instance_get_int(a, inst, H("none"), &i) && i == 0); + CHECK(!nka_instance_get_color(a, inst, H("none"), &c) && c.r == 0 && c.a == 1); + CHECK(!nka_instance_get_vec2(a, inst, H("f"), &p) && p.x == 0); + CHECK(!nka_instance_get_float(a, inst, H("f"), 0)); + CHECK(!nka_instance_get_float(a, H("nobody"), H("f"), &f)); + } + nka_destroy(a); +} + +static void test_groups(void) +{ + struct nka_context *a = nka_create(0); + nk_hash inst = H("g"); + + nka_clip_begin(a, H("seq")); + nka_clip_seq_begin(a); + key2(a, "x", 0, 0, 0.5f, 100); + nka_clip_seq_end(a); + nka_clip_seq_begin(a); + key2(a, "y", 0, 0, 0.5f, 50); + nka_clip_seq_end(a); + nka_clip_seq_begin(a); + key2(a, "s", 0, 1, 0.5f, 1.5f); + nka_clip_marker(a, 0.25f, H("in"), on_mark, 0); + nka_clip_seq_end(a); + nka_clip_end(a); + CHECK(nka_clip_duration(a, H("seq")) == 1.5f); + nka_play(a, H("seq"), inst); + nka_instance_seek(a, inst, 0.75f); + CHECK(getf(a, inst, "x") == 100 && getf(a, inst, "y") == 25 && getf(a, inst, "s") == 1); + nka_instance_seek(a, inst, 1.25f); + CHECK(getf(a, inst, "s") == 1.25f); + marks.n = 0; + nka_instance_seek(a, inst, 1); + nka_update(a, 0.5f); + CHECK(marks.n == 1 && marks.time[0] == 1.25f); + + nka_clip_begin(a, H("par")); + nka_clip_par_begin(a); + key2(a, "x", 0, 0, 0.6f, 100); + key2(a, "y", 0, 0, 0.6f, 50); + nka_clip_par_end(a); + key2(a, "a", 0, 0, 0.3f, 1); + nka_clip_end(a); + CHECK(NEAR(nka_clip_duration(a, H("par")), 0.9f, 1e-6f)); + nka_play(a, H("par"), inst); + nka_instance_seek(a, inst, 0.75f); + CHECK(NEAR(getf(a, inst, "a"), 0.5f, 1e-5f) && getf(a, inst, "x") == 100); + + /* A group that ends with a nested one keeps its keys: upstream went back to its start. */ + nka_clip_begin(a, H("nest")); + nka_clip_seq_begin(a); + nka_clip_par_begin(a); + key2(a, "x", 0, 0, 0.3f, 30); + nka_clip_par_end(a); + nka_clip_seq_end(a); + key2(a, "z", 0, 0, 0.2f, 20); + nka_clip_end(a); + CHECK(NEAR(nka_clip_duration(a, H("nest")), 0.5f, 1e-6f)); + nka_play(a, H("nest"), inst); + nka_instance_seek(a, inst, 0.3f); + CHECK(getf(a, inst, "z") == 0); + nka_instance_seek(a, inst, 0.4f); + CHECK(NEAR(getf(a, inst, "z"), 10, 1e-4f)); + + nka_clip_begin(a, H("doc")); + nka_clip_seq_begin(a); + nka_clip_par_begin(a); + key2(a, "x", 0, 0, 0.3f, 50); + key2(a, "y", 0, 0, 0.3f, 50); + nka_clip_par_end(a); + key2(a, "s", 0, 1, 0.2f, 1.5f); + nka_clip_seq_end(a); + key2(a, "a", 0, 1, 0.3f, 0); + nka_clip_end(a); + CHECK(NEAR(nka_clip_duration(a, H("doc")), 0.8f, 1e-6f)); + nka_play(a, H("doc"), inst); + nka_instance_seek(a, inst, 0.4f); + CHECK(NEAR(getf(a, inst, "s"), 1.25f, 1e-5f) && getf(a, inst, "a") == 1); + + /* An unmatched end and keys outside any build are ignored. */ + nka_clip_seq_end(a); + nka_clip_key_float(a, H("x"), 0, 1, NKA_EASE_LINEAR, 0); + CHECK(nka_clip_end(a) == NKA_ERR_BAD_ARG); + nka_destroy(a); +} + +static void test_delay(void) +{ + struct nka_context *a = nka_create(0); + nk_hash clip = H("delayed"), inst = H("d"); + nka_clip_begin(a, clip); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_delay(a, 1); + nka_clip_on_begin(a, on_begin, 0); + nka_clip_end(a); + reset_events(); + nka_play(a, clip, inst); + CHECK(getf(a, inst, "f") == 0 && nka_busy(a)); + nka_update(a, 0.5f); + CHECK(getf(a, inst, "f") == 0 && nka_instance_time(a, inst) == 0 && nka_busy(a) && nevents == 0); + nka_update(a, 0.75f); + CHECK(strcmp(events, "B") == 0); + CHECK(nka_instance_time(a, inst) == 0.25f && getf(a, inst, "f") == 2.5f); + nka_update(a, 1); + CHECK(!nka_instance_is_playing(a, inst) && getf(a, inst, "f") == 10); + + CHECK(nka_play_with_delay(a, clip, inst, 0.25f) == inst); + nka_update(a, 0.5f); + CHECK(nka_instance_time(a, inst) == 0.25f); + nka_play_with_delay(a, clip, inst, -1); + nka_update(a, 0.5f); + CHECK(nka_instance_time(a, inst) == 0.5f); + CHECK(nka_play_with_delay(a, H("nope"), inst, 1) == 0); + nka_destroy(a); +} + +static void test_loops(void) +{ + struct nka_context *a = nka_create(0); + nk_hash inst = H("l"); + + nka_clip_begin(a, H("n2")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, 2); + nka_clip_on_loop(a, on_loop, 0); + nka_clip_on_complete(a, on_complete, 0); + nka_clip_end(a); + reset_events(); + nka_play(a, H("n2"), inst); + steps(a, 4, 0.25f); + CHECK(nka_instance_time(a, inst) == 1 && getf(a, inst, "f") == 10 && nloops == 0); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.25f && nloops == 1 && loops_seen[0] == 1); + steps(a, 7, 0.25f); + CHECK(nka_instance_is_playing(a, inst) && nloops == 2 && loops_seen[1] == 2 && nka_instance_time(a, inst) == 1); + nka_update(a, 0.25f); + CHECK(!nka_instance_is_playing(a, inst) && strcmp(events, "LLC") == 0 && getf(a, inst, "f") == 10); + + nka_clip_begin(a, H("inf")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, -1); + nka_clip_end(a); + nka_play(a, H("inf"), inst); + steps(a, 400, 0.25f); + CHECK(nka_instance_is_playing(a, inst) && inst_of(a, inst)->loop == 99); + + /* Reverse starts at the end; upstream finished on the first frame. */ + nka_clip_begin(a, H("rev")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 0, NKA_DIR_REVERSE, 0); + nka_clip_end(a); + nka_play(a, H("rev"), inst); + CHECK(nka_instance_time(a, inst) == 1 && getf(a, inst, "f") == 10); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.75f && getf(a, inst, "f") == 7.5f && nka_instance_is_playing(a, inst)); + steps(a, 3, 0.25f); + CHECK(nka_instance_time(a, inst) == 0 && nka_instance_is_playing(a, inst)); + nka_update(a, 0.25f); + CHECK(!nka_instance_is_playing(a, inst) && getf(a, inst, "f") == 0); + + nka_clip_begin(a, H("rev1")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_REVERSE, 1); + nka_clip_on_loop(a, on_loop, 0); + nka_clip_end(a); + reset_events(); + nka_play(a, H("rev1"), inst); + nka_update(a, 0.25f); + CHECK(nloops == 0 && nka_instance_time(a, inst) == 0.75f); + steps(a, 4, 0.25f); + CHECK(nloops == 1 && loops_seen[0] == 1 && nka_instance_time(a, inst) == 0.75f); + steps(a, 3, 0.25f); + CHECK(nka_instance_is_playing(a, inst) && nka_instance_time(a, inst) == 0); + nka_update(a, 0.25f); + CHECK(!nka_instance_is_playing(a, inst) && nka_instance_time(a, inst) == 0); + + nka_clip_begin(a, H("alt")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_ALTERNATE, 2); + nka_clip_end(a); + nka_play(a, H("alt"), inst); + steps(a, 5, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.75f && getf(a, inst, "f") == 7.5f); + steps(a, 4, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.25f && inst_of(a, inst)->dir == 1); + steps(a, 3, 0.25f); + CHECK(nka_instance_time(a, inst) == 1 && nka_instance_is_playing(a, inst)); + nka_update(a, 0.25f); + CHECK(!nka_instance_is_playing(a, inst) && nka_instance_time(a, inst) == 1 && getf(a, inst, "f") == 10); + + /* The pause between loops starts at the boundary and takes the rest of the frame. */ + nka_clip_begin(a, H("ld")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, 1); + nka_clip_set_loop_delay(a, 0.5f); + nka_clip_end(a); + nka_play(a, H("ld"), inst); + steps(a, 5, 0.25f); + CHECK(nka_instance_time(a, inst) == 0 && getf(a, inst, "f") == 0 && nka_busy(a)); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0 && nka_busy(a)); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.25f); + nka_play(a, H("ld"), inst); + steps(a, 3, 0.4f); + CHECK(nka_instance_time(a, inst) == 0 && NEAR(inst_of(a, inst)->delay, 0.3f, 1e-6f)); + nka_update(a, 0.4f); + CHECK(NEAR(nka_instance_time(a, inst), 0.1f, 1e-6f) && NEAR(getf(a, inst, "f"), 1, 1e-5f)); + + /* Without loop, set_loop still sets the direction. */ + nka_clip_begin(a, H("once")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 0, NKA_DIR_ALTERNATE, 5); + nka_clip_end(a); + nka_play(a, H("once"), inst); + steps(a, 5, 0.25f); + CHECK(!nka_instance_is_playing(a, inst) && nka_instance_time(a, inst) == 1); + nka_destroy(a); +} + +static void test_markers(void) +{ + struct nka_context *a = nka_create(0); + nk_hash clip = H("marks"), inst = H("m"), m0, mhalf, mid, end = H("end"); + int i; + + nka_clip_begin(a, clip); + key2(a, "f", 0, 0, 3, 1); + m0 = nka_clip_marker(a, 0, 0, on_mark, 0); + mhalf = nka_clip_marker(a, 0.5f, 0, on_mark, 0); + mid = nka_clip_marker(a, 1.5f, H("mid"), on_mark, 0); + nka_clip_marker(a, 3, end, on_mark, 0); + nka_clip_end(a); + CHECK(m0 && mhalf && m0 != mhalf && mid == H("mid")); + CHECK(nka_clip_marker(a, 1, 0, on_mark, 0) == 0); + + marks.n = 0; + nka_play(a, clip, inst); + nka_update(a, 0.4f); + CHECK(marks.n == 1 && marks.id[0] == m0 && marks.time[0] == 0 && marks.inst[0] == inst); + steps(a, 7, 0.4f); + CHECK(marks.n == 4 && marks_are(m0, mhalf, mid, end) && marks.time[3] == 3); + CHECK(!nka_instance_is_playing(a, inst)); + + nka_clip_begin(a, H("marks2")); + key2(a, "f", 0, 0, 3, 1); + nka_clip_marker(a, 0, m0, on_mark, 0); + nka_clip_marker(a, 0.5f, mhalf, on_mark, 0); + nka_clip_marker(a, 1.5f, mid, on_mark, 0); + nka_clip_marker(a, 3, end, on_mark, 0); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, 1); + nka_clip_end(a); + marks.n = 0; + nka_play(a, H("marks2"), inst); + steps(a, 20, 0.4f); + CHECK(marks.n == 8 && !nka_instance_is_playing(a, inst)); + for (i = 0; i < 8 && i < marks.n; ++i) CHECK(marks.id[i] == marks.id[i % 4] && marks.id[i] == (i % 4 == 0 ? m0 : i % 4 == 1 ? mhalf : i % 4 == 2 ? mid : end)); + + /* A seek back fires the markers again; a seek ahead skips what it jumps over. */ + marks.n = 0; + nka_play(a, clip, inst); + steps(a, 5, 0.4f); + CHECK(marks.n == 3); + nka_instance_seek(a, inst, 1); + steps(a, 3, 0.4f); + CHECK(marks.n == 4 && marks.id[3] == mid); + nka_instance_seek(a, inst, 0.2f); + nka_instance_seek(a, inst, 2.6f); + steps(a, 2, 0.4f); + CHECK(marks.n == 5 && marks.id[4] == end && !nka_instance_is_playing(a, inst)); + + marks.n = 0; + nka_play(a, clip, inst); + nka_instance_pause(a, inst); + nka_instance_seek(a, inst, 1.5f); + nka_update(a, 0.1f); + CHECK(marks.n == 0); + nka_instance_resume(a, inst); + nka_update(a, 0.1f); + CHECK(marks.n == 1 && marks.id[0] == mid); + + /* Played backward, markers fire in the order they are passed. */ + nka_clip_begin(a, H("rmarks")); + key2(a, "f", 0, 0, 3, 1); + nka_clip_marker(a, 0, H("a0"), on_mark, 0); + nka_clip_marker(a, 0.5f, H("a1"), on_mark, 0); + nka_clip_marker(a, 1.5f, H("a2"), on_mark, 0); + nka_clip_set_loop(a, 0, NKA_DIR_REVERSE, 0); + nka_clip_end(a); + marks.n = 0; + nka_play(a, H("rmarks"), inst); + steps(a, 3, 1); + CHECK(marks.n == 3 && marks_are(H("a2"), H("a1"), H("a0"), 0)); + + /* Ping-pong fires a marker at a turn once, not as the end of one pass and the start of the next. */ + nka_clip_begin(a, H("amarks")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_marker(a, 0, H("lo"), on_mark, 0); + nka_clip_marker(a, 1, H("hi"), on_mark, 0); + nka_clip_set_loop(a, 1, NKA_DIR_ALTERNATE, 2); + nka_clip_end(a); + marks.n = 0; + nka_play(a, H("amarks"), inst); + steps(a, 20, 0.25f); + CHECK(!nka_instance_is_playing(a, inst) && marks.n == 4 && marks_are(H("lo"), H("hi"), H("lo"), H("hi"))); + + /* A marker past the keys makes the clip longer. */ + nka_clip_begin(a, H("long")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_marker(a, 2, H("tail"), 0, 0); + nka_clip_end(a); + CHECK(nka_clip_duration(a, H("long")) == 2); + nka_destroy(a); +} + +/* Callbacks that change things under the update */ + +static void destroy_self(struct nka_context *a, nk_hash inst, void *user) +{ + (void)user; + nka_instance_destroy(a, inst); + ev('D'); +} + +static void replay_self(struct nka_context *a, nk_hash inst, void *user) +{ + nka_play(a, *(const nk_hash *)user, inst); + ev('R'); +} + +static void mark_destroy(struct nka_context *a, nk_hash inst, nk_hash id, float time, void *user) +{ + (void)id; (void)time; (void)user; + nka_instance_destroy(a, inst); +} + +static void rebuild(struct nka_context *a, nk_hash inst, nk_hash id, float time, void *user) +{ + (void)inst; (void)id; (void)time; + nka_clip_begin(a, *(const nk_hash *)user); + key2(a, "g", 0, 5, 1, 6); + nka_clip_end(a); +} + +static void clear_all(struct nka_context *a, nk_hash inst, void *user) +{ + (void)inst; (void)user; + nka_clear(a); +} + +static int nested; + +static void update_inside(struct nka_context *a, nk_hash inst, void *user) +{ + (void)inst; (void)user; + if (nested++) return; + nka_update(a, 0.1f); +} + +static nk_hash counted_ids[20]; +static int counted[20], stray, spawned, killed; + +static void count_update(struct nka_context *a, nk_hash inst, void *user) +{ + int i, at = -1; + for (i = 0; i < 20; ++i) + if (counted_ids[i] == inst) at = i; + if (at < 0) { + stray++; + return; + } + counted[at]++; + if (at == 7 && !spawned) { + spawned = 1; + for (i = 0; i < 200; ++i) nka_play(a, *(const nk_hash *)user, nka_id_mix(H("spawn"), (nk_hash)i)); + } + if (at == 3 && !killed) { + killed = 1; + for (i = 10; i < 20; ++i) nka_instance_destroy(a, counted_ids[i]); + } +} + +static void test_callbacks(void) +{ + struct nka_context *a = nka_create(0); + nk_hash inst = H("c"), again = H("again"), rb = H("rb"), counted_clip = H("counted"); + float f; + int i, ok; + + nka_clip_begin(a, H("cb")); + key2(a, "f", 0, 0, 0.5f, 1); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, 1); + nka_clip_on_begin(a, on_begin, 0); + nka_clip_on_update(a, on_update, 0); + nka_clip_on_complete(a, on_complete, 0); + nka_clip_on_loop(a, on_loop, 0); + nka_clip_on_pause(a, on_pause, 0); + nka_clip_end(a); + reset_events(); + nka_play(a, H("cb"), inst); + CHECK(nevents == 0); + steps(a, 6, 0.25f); + CHECK(strcmp(events, "BUULUUC") == 0 && nloops == 1 && loops_seen[0] == 1); + + /* Pause tells once per pause; upstream told every call. */ + reset_events(); + nka_play(a, H("cb"), inst); + nka_instance_pause(a, inst); + nka_instance_pause(a, inst); + CHECK(strcmp(events, "P") == 0 && nka_instance_is_paused(a, inst)); + nka_update(a, 0.25f); + CHECK(strcmp(events, "P") == 0 && nka_instance_time(a, inst) == 0 && !nka_busy(a)); + nka_instance_resume(a, inst); + CHECK(nka_busy(a) && !nka_instance_is_paused(a, inst)); + nka_update(a, 0.25f); + CHECK(strcmp(events, "PBU") == 0); + nka_instance_pause(a, inst); + CHECK(strcmp(events, "PBUP") == 0); + + nka_clip_begin(a, H("selfdestruct")); + key2(a, "f", 0, 0, 0.25f, 1); + nka_clip_on_complete(a, destroy_self, 0); + nka_clip_end(a); + reset_events(); + nka_play(a, H("selfdestruct"), inst); + nka_instance_then(a, inst, H("cb"), H("next")); + nka_update(a, 0.5f); + CHECK(strcmp(events, "D") == 0 && !nka_instance_valid(a, inst) && !nka_instance_valid(a, H("next"))); + + nka_clip_begin(a, again); + key2(a, "f", 0, 0, 0.25f, 1); + nka_clip_on_complete(a, replay_self, &again); + nka_clip_end(a); + reset_events(); + nka_play(a, again, inst); + nka_update(a, 0.5f); + CHECK(strcmp(events, "R") == 0 && nka_instance_is_playing(a, inst) && nka_instance_time(a, inst) == 0); + nka_update(a, 0.25f); + CHECK(strcmp(events, "R") == 0 && nka_instance_time(a, inst) == 0.25f); + + nka_clip_begin(a, H("md")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_marker(a, 0.5f, 0, mark_destroy, 0); + nka_clip_on_update(a, on_update, 0); + nka_clip_end(a); + reset_events(); + nka_play(a, H("md"), inst); + nka_update(a, 0.25f); + nka_update(a, 0.5f); + CHECK(strcmp(events, "U") == 0 && !nka_instance_valid(a, inst)); + + /* Authoring the playing clip again from its marker: the step stops, the next read catches up. */ + nka_clip_begin(a, rb); + key2(a, "f", 0, 0, 1, 1); + nka_clip_marker(a, 0.5f, 0, rebuild, &rb); + nka_clip_end(a); + nka_play(a, rb, inst); + nka_update(a, 0.75f); + CHECK(NEAR(getf(a, inst, "g"), 5.75f, 1e-5f) && !nka_instance_get_float(a, inst, H("f"), &f) && f == 0); + nka_update(a, 0.5f); + CHECK(!nka_instance_is_playing(a, inst) && getf(a, inst, "g") == 6); + + /* Instances made and destroyed by callbacks during an update: each other one steps once. */ + nka_clip_begin(a, counted_clip); + key2(a, "f", 0, 0, 10, 1); + nka_clip_on_update(a, count_update, &counted_clip); + nka_clip_end(a); + for (i = 0; i < 20; ++i) { + counted_ids[i] = nka_id_mix(H("counted"), (nk_hash)i); + counted[i] = 0; + nka_play(a, counted_clip, counted_ids[i]); + } + nka_update(a, 0.1f); + ok = stray == 0 && spawned && killed; + for (i = 0; i < 10; ++i) ok = ok && counted[i] == 1; + for (i = 10; i < 20; ++i) ok = ok && counted[i] <= 1 && !nka_instance_valid(a, counted_ids[i]); + CHECK(ok); + CHECK(nka_instance_time(a, nka_id_mix(H("spawn"), 5)) == 0); + nka_update(a, 0.1f); + ok = stray == 200; + for (i = 0; i < 10; ++i) ok = ok && counted[i] == 2; + CHECK(ok); + + nested = 0; + nka_clip_begin(a, H("nested")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_on_update(a, update_inside, 0); + nka_clip_end(a); + nka_play(a, H("nested"), H("n")); + nka_update(a, 0.25f); + CHECK(nested == 1 && nka_instance_time(a, H("n")) == 0.25f); + + nka_clip_begin(a, H("clr")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_on_update(a, clear_all, 0); + nka_clip_end(a); + for (i = 0; i < 3; ++i) nka_play(a, H("clr"), nka_id_mix(H("clr"), (nk_hash)i)); + nka_update(a, 0.25f); + CHECK(a->clip_insts.len == 0 && nka_clip_exists(a, H("clr"))); + nka_destroy(a); +} + +static void test_chain(void) +{ + struct nka_context *a = nka_create(0); + nk_hash A = H("ca"), B = H("cb"), ia = H("ia"), ib = H("ib"), made; + nka_clip_begin(a, A); + key2(a, "f", 0, 0, 0.5f, 1); + nka_clip_end(a); + nka_clip_begin(a, B); + key2(a, "g", 0, 0, 0.5f, 1); + nka_clip_end(a); + + nka_play(a, A, ia); + CHECK(nka_instance_then(a, ia, B, ib) == ib); + nka_instance_then_delay(a, ia, 0.25f); + steps(a, 2, 0.25f); + CHECK(!nka_instance_valid(a, ib) && nka_instance_time(a, ia) == 0.5f); + nka_update(a, 0.25f); + CHECK(!nka_instance_is_playing(a, ia) && nka_instance_is_playing(a, ib) && nka_instance_time(a, ib) == 0 && nka_busy(a)); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, ib) == 0 && nka_busy(a)); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, ib) == 0.25f && getf(a, ib, "g") == 0.5f); + + nka_play(a, A, ia); + made = nka_instance_then(a, ia, B, 0); + CHECK(made && made != ia && made != ib && !nka_instance_valid(a, made)); + steps(a, 3, 0.25f); + CHECK(nka_instance_is_playing(a, made) && nka_instance_time(a, made) == 0); + + nka_play(a, A, ia); + nka_instance_then(a, ia, B, H("ic")); + nka_instance_destroy(a, ia); + steps(a, 4, 0.25f); + CHECK(!nka_instance_valid(a, H("ic"))); + + nka_play(a, A, ia); + nka_instance_then(a, ia, B, H("id")); + nka_play(a, A, ia); + steps(a, 4, 0.25f); + CHECK(!nka_instance_valid(a, H("id"))); + + nka_play(a, A, ia); + nka_instance_then(a, ia, H("unknown"), H("ie")); + steps(a, 4, 0.25f); + CHECK(!nka_instance_valid(a, H("ie"))); + CHECK(nka_instance_then(a, H("nobody"), B, 0) == 0); + nka_destroy(a); +} + +static void test_control(void) +{ + struct nka_context *a = nka_create(0); + nk_hash inst = H("s"); + nka_clip_begin(a, H("sk")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_end(a); + nka_clip_begin(a, H("rsk")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 0, NKA_DIR_REVERSE, 0); + nka_clip_end(a); + + CHECK(nka_get_instance(a, inst) == 0); + nka_play(a, H("sk"), inst); + CHECK(nka_get_instance(a, inst) == inst); + nka_update(a, 0.25f); + nka_instance_pause(a, inst); + nka_instance_seek(a, inst, 0.75f); + CHECK(nka_instance_time(a, inst) == 0.75f && getf(a, inst, "f") == 7.5f); + nka_instance_seek(a, inst, 5); + CHECK(nka_instance_time(a, inst) == 1 && getf(a, inst, "f") == 10); + nka_instance_seek(a, inst, -1); + CHECK(nka_instance_time(a, inst) == 0 && getf(a, inst, "f") == 0); + nka_instance_resume(a, inst); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.25f && getf(a, inst, "f") == 2.5f); + + nka_instance_stop(a, inst); + CHECK(!nka_instance_is_playing(a, inst) && nka_instance_time(a, inst) == 0 && getf(a, inst, "f") == 2.5f); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0); + + nka_instance_set_time_scale(a, inst, 2); + nka_instance_restart(a, inst); + CHECK(nka_instance_is_playing(a, inst) && getf(a, inst, "f") == 0); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.5f); + nka_instance_refresh(a, inst); + CHECK(getf(a, inst, "f") == 5); + + nka_instance_reset(a, inst); + CHECK(!nka_instance_is_playing(a, inst) && nka_instance_time(a, inst) == 0 && getf(a, inst, "f") == 0); + + /* Reset of a reversed clip goes to its start, the end; upstream said time 0 there. */ + nka_play(a, H("rsk"), inst); + nka_update(a, 0.5f); + nka_instance_reset(a, inst); + CHECK(nka_instance_time(a, inst) == 1 && getf(a, inst, "f") == 10 && !nka_instance_is_playing(a, inst)); + + /* Playing another clip on an instance drops the channels of the old one. */ + nka_clip_begin(a, H("other")); + key2(a, "g", 0, 3, 1, 4); + nka_clip_end(a); + nka_play(a, H("other"), inst); + CHECK(getf(a, inst, "g") == 3 && getf(a, inst, "f") == 0 && !nka_instance_get_float(a, inst, H("f"), 0)); + CHECK(nka_play(a, H("other"), 0) == 0 && nka_play(a, H("missing"), inst) == 0); + + nka_instance_destroy(a, inst); + CHECK(!nka_instance_valid(a, inst) && nka_instance_time(a, inst) == 0 && !nka_instance_is_playing(a, inst)); + nka_instance_destroy(a, inst); + nka_instance_seek(a, inst, 1); + nka_instance_reset(a, inst); + nka_instance_restart(a, inst); + nka_instance_refresh(a, inst); + nka_instance_pause(a, inst); + nka_instance_resume(a, inst); + nka_instance_stop(a, inst); + CHECK(!nka_instance_valid(a, inst)); + nka_destroy(a); +} + +static void test_time_scale(void) +{ + struct nka_context *a = nka_create(0); + nk_hash inst = H("t"); + nka_clip_begin(a, H("ts")); + key2(a, "f", 0, 0, 4, 10); + nka_clip_end(a); + nka_play(a, H("ts"), inst); + nka_instance_set_time_scale(a, inst, 2); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.5f); + nka_instance_set_time_scale(a, inst, 0); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 0.75f); + nka_instance_set_time_scale(a, inst, -3); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, inst) == 1); + nka_instance_set_time_scale(a, inst, 1); + nka_set_time_scale(a, 0.5f); + nka_update(a, 0.5f); + CHECK(nka_instance_time(a, inst) == 1.25f); + nka_set_time_scale(a, 0); + nka_update(a, 0.5f); + CHECK(nka_instance_time(a, inst) == 1.25f && nka_busy(a)); + nka_set_time_scale(a, 1); + nka_update(a, 3); + CHECK(nka_instance_time(a, inst) == 2.25f); + nka_destroy(a); +} + +static void test_layers(void) +{ + struct nka_context *a = nka_create(0); + nk_hash ia = H("la"), ib = H("lb"), target = H("blend"); + struct nk_vec2 p; + struct nka_vec4 q; + float f; + int i; + nka_set_anchor(a, NKA_ANCHOR_WINDOW, v2(200, 100)); + nka_clip_begin(a, H("ca")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_key_vec2(a, H("v"), 0, v2(0, 0), NKA_EASE_LINEAR, 0); + nka_clip_key_vec2(a, H("v"), 1, v2(10, 20), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4(a, H("q"), 0, v4(1, 2, 3, 4), NKA_EASE_LINEAR, 0); + nka_clip_key_int(a, H("i"), 0, 0, NKA_EASE_LINEAR); + nka_clip_key_int(a, H("i"), 1, 10, NKA_EASE_LINEAR); + nka_clip_key_float(a, H("only"), 0, 42, NKA_EASE_LINEAR, 0); + nka_clip_key_color(a, H("c"), 0, rgba(1, 0, 0, 1), NKA_COL_SRGB, NKA_EASE_LINEAR, 0); + nka_clip_key_float_rel(a, H("x"), 0, 0.5f, 0, NKA_ANCHOR_WINDOW, 0, NKA_EASE_LINEAR, 0); + nka_clip_end(a); + nka_clip_begin(a, H("cb")); + nka_clip_key_float(a, H("f"), 0, 20, NKA_EASE_LINEAR, 0); + nka_clip_key_vec2(a, H("v"), 0, v2(100, 100), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4(a, H("q"), 0, v4(5, 6, 7, 8), NKA_EASE_LINEAR, 0); + nka_clip_key_int(a, H("i"), 0, 20, NKA_EASE_LINEAR); + nka_clip_key_float(a, H("x"), 0, 300, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("f"), 1, 20, NKA_EASE_LINEAR, 0); + nka_clip_end(a); + nka_play(a, H("ca"), ia); + nka_play(a, H("cb"), ib); + nka_instance_seek(a, ia, 0.5f); + + /* The target need not be an instance; upstream wrote nowhere unless it was. */ + CHECK(!nka_get_blended_float(a, target, H("f"), &f)); + nka_layer_begin(a, target); + nka_layer_add(a, ia, 1); + nka_layer_add(a, ib, 3); + nka_layer_end(a, target); + CHECK(nka_get_blended_float(a, target, H("f"), &f) && f == 16.25f); + CHECK(nka_get_blended_vec2(a, target, H("v"), &p) && p.x == 76.25f && p.y == 77.5f); + CHECK(nka_get_blended_vec4(a, target, H("q"), &q) && q.x == 4 && q.w == 7); + CHECK(nka_get_blended_int(a, target, H("i"), &i) && i == 16); + CHECK(nka_get_blended_float(a, target, H("only"), &f) && f == 42); + CHECK(nka_get_blended_float(a, target, H("x"), &f) && f == 250); + CHECK(!nka_get_blended_float(a, target, H("c"), &f) && !nka_get_blended_vec4(a, target, H("c"), &q)); + CHECK(!nka_get_blended_float(a, target, H("v"), &f) && !nka_get_blended_float(a, H("other"), H("f"), &f)); + CHECK(!nka_get_blended_float(a, target, H("f"), 0)); + + /* The instance's weight multiplies the layer's. */ + nka_instance_set_weight(a, ib, 0.5f); + nka_layer_begin(a, target); + nka_layer_add(a, ia, 1); + nka_layer_add(a, ib, 2); + nka_layer_add(a, ib, 0); + nka_layer_add(a, H("nobody"), 1); + nka_layer_end(a, target); + CHECK(nka_get_blended_float(a, target, H("f"), &f) && f == 12.5f); + CHECK(nka_get_blended_int(a, target, H("i"), &i) && i == 13); + + /* An end for another target is ignored; an end with no weight keeps what was there. */ + nka_layer_begin(a, target); + nka_layer_add(a, ia, 1); + nka_layer_end(a, H("wrong")); + CHECK(nka_get_blended_float(a, target, H("f"), &f) && f == 12.5f && !nka_get_blended_float(a, H("wrong"), H("f"), &f)); + nka_layer_end(a, target); + CHECK(nka_get_blended_float(a, target, H("f"), &f) && f == 5); + nka_layer_begin(a, target); + nka_layer_add(a, ia, 0); + nka_layer_end(a, target); + CHECK(nka_get_blended_float(a, target, H("f"), &f) && f == 5); + nka_layer_add(a, ib, 1); + nka_layer_end(a, target); + CHECK(nka_get_blended_float(a, target, H("f"), &f) && f == 5); + + /* The instance itself can be the target. */ + nka_layer_begin(a, ia); + nka_layer_add(a, ib, 1); + nka_layer_end(a, ia); + CHECK(nka_get_blended_float(a, ia, H("f"), &f) && f == 20); + CHECK(getf(a, ia, "f") == 5); + nka_destroy(a); +} + +/* Variations */ + +static nk_uint xs(nk_uint *s) +{ + nk_uint x = *s; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + *s = x; + return x; +} + +static float unit_of(nk_uint *s) { return (float)(xs(s) & 0x7fffffffu) / (float)0x7fffffff; } + +static float tens(int loop, void *user) { (void)user; return (float)loop * 10; } +static int hundreds(int loop, void *user) { (void)user; return loop * 100; } +static struct nk_vec2 pairs(int loop, void *user) { (void)user; return v2((float)loop, (float)-loop); } +static struct nka_vec4 grey(int loop, void *user) { (void)user; return v4(0.1f * (float)loop, 0.2f, 0.3f, 0.4f); } + +/* Loops 0 to n-1 of a clip whose "v" carries one varied key, read halfway through each. */ +static void build_var(struct nka_context *a, nk_hash clip) +{ + nka_clip_key_float(a, H("t"), 1, 0, NKA_EASE_LINEAR, 0); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, -1); + nka_clip_end(a); + nka_play(a, clip, H("vi")); + nka_update(a, 0.5f); +} + +static void next_loop(struct nka_context *a) { steps(a, 2, 0.5f); } + +static void varf_run(struct nka_variation_float var, float base, const float *want, int n, int line) +{ + struct nka_context *a = nka_create(0); + int k; + nka_clip_begin(a, H("vf")); + nka_clip_key_float_var(a, H("v"), 0, base, var, NKA_EASE_LINEAR, 0); + build_var(a, H("vf")); + for (k = 0; k < n; ++k) { + float v = getf(a, H("vi"), "v"); + check(inst_of(a, H("vi"))->loop == k && NEAR(v, want[k], 1e-5f), "varf", line); + next_loop(a); + } + nka_destroy(a); +} + +static void test_variations(void) +{ + struct nka_context *a; + float want[8]; + nk_uint s; + int k; + { + const float inc[4] = { 1, 1.5f, 2, 2.5f }, dec[4] = { 1, 0.5f, 0, -0.5f }, mul[4] = { 4, 2, 1, 0.5f }; + const float pp[6] = { 0, 1, 0, -1, 0, 1 }, cl[4] = { 1, 2, 2, 2 }, fn[3] = { 0, 10, 20 }, none[2] = { 3, 3 }; + varf_run(nka_varf_inc(0.5f), 1, inc, 4, __LINE__); + varf_run(nka_varf_dec(0.5f), 1, dec, 4, __LINE__); + varf_run(nka_varf_mul(0.5f), 4, mul, 4, __LINE__); + varf_run(nka_varf_pingpong(1), 0, pp, 6, __LINE__); + varf_run(nka_varf_clamp(nka_varf_inc(1), 0, 2), 1, cl, 4, __LINE__); + varf_run(nka_varf_fn(tens, 0), 7, fn, 3, __LINE__); + varf_run(nka_varf_none(), 3, none, 2, __LINE__); + } + for (k = 0; k < 6; ++k) { + s = 1234u + (nk_uint)k * 1664525u; + want[k] = 5 + (unit_of(&s) * 2 - 1) * 10; + } + varf_run(nka_varf_seed(nka_varf_rand(10), 1234), 5, want, 6, __LINE__); + for (k = 0; k < 6; ++k) { + s = 77u + (nk_uint)k * 1664525u; + want[k] = 5 + unit_of(&s) * 2; + } + varf_run(nka_varf_seed(nka_varf_rand_abs(2), 77), 5, want, 6, __LINE__); + + /* Unseeded draws hold for a loop, and repeat for the same instance; upstream drew every frame. */ + { + struct nka_context *b = nka_create(0); + float first[3], v, w; + a = nka_create(0); + nka_clip_begin(a, H("vf")); + nka_clip_key_float_var(a, H("v"), 0, 0, nka_varf_rand(100), NKA_EASE_LINEAR, 0); + build_var(a, H("vf")); + nka_clip_begin(b, H("vf")); + nka_clip_key_float_var(b, H("v"), 0, 0, nka_varf_rand(100), NKA_EASE_LINEAR, 0); + build_var(b, H("vf")); + for (k = 0; k < 3; ++k) { + v = getf(a, H("vi"), "v"); + nka_update(a, 0.25f); + w = getf(a, H("vi"), "v"); + CHECK(v == w && v >= -100 && v <= 100 && v == getf(b, H("vi"), "v")); + first[k] = v; + nka_update(a, 0.25f); + nka_update(a, 0.5f); + next_loop(b); + } + CHECK(first[0] != first[1] && first[1] != first[2]); + nka_destroy(b); + nka_destroy(a); + } + + a = nka_create(0); + nka_clip_begin(a, H("many")); + nka_clip_key_int_var(a, H("i"), 0, 1, nka_vari_inc(3), NKA_EASE_LINEAR); + nka_clip_key_int_var(a, H("j"), 0, 1, nka_vari_clamp(nka_vari_dec(5), -6, 100), NKA_EASE_LINEAR); + nka_clip_key_int_var(a, H("k"), 0, 1, nka_vari_seed(nka_vari_rand(50), 9), NKA_EASE_LINEAR); + nka_clip_key_int_var(a, H("l"), 0, 1, nka_vari_fn(hundreds, 0), NKA_EASE_LINEAR); + nka_clip_key_vec2_var(a, H("p"), 0, v2(0, 0), nka_varv2_inc(1, 2), NKA_EASE_LINEAR, 0); + nka_clip_key_vec2_var(a, H("pa"), 0, v2(0, 0), nka_varv2_axis(nka_varf_inc(1), nka_varf_dec(1)), NKA_EASE_LINEAR, 0); + nka_clip_key_vec2_var(a, H("pm"), 0, v2(1, 3), nka_varv2_clamp(nka_varv2_mul(2), v2(0, 0), v2(100, 20)), NKA_EASE_LINEAR, 0); + nka_clip_key_vec2_var(a, H("pr"), 0, v2(0, 0), nka_varv2_seed(nka_varv2_rand(1, 2), 31), NKA_EASE_LINEAR, 0); + nka_clip_key_vec2_var(a, H("pf"), 0, v2(0, 0), nka_varv2_fn(pairs, 0), NKA_EASE_LINEAR, 0); + nka_clip_key_vec2_var(a, H("pd"), 0, v2(0, 0), nka_varv2_dec(1, 1), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_var(a, H("q"), 0, v4(0, 0, 0, 0), nka_varv4_inc(1, 2, 3, 4), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_var(a, H("qa"), 0, v4(0, 0, 0, 0), + nka_varv4_axis(nka_varf_inc(1), nka_varf_none(), nka_varf_dec(1), nka_varf_mul(2)), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_var(a, H("qm"), 0, v4(1, 1, 1, 1), nka_varv4_clamp(nka_varv4_mul(3), v4(0, 0, 0, 0), v4(5, 5, 5, 5)), + NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_var(a, H("qr"), 0, v4(0, 0, 0, 0), nka_varv4_seed(nka_varv4_rand(1, 1, 1, 1), 5), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_var(a, H("qd"), 0, v4(0, 0, 0, 0), nka_varv4_dec(1, 1, 1, 1), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_var(a, H("qf"), 0, v4(0, 0, 0, 0), nka_varv4_fn(grey, 0), NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("cs"), 0, rgba(0.25f, 0, 0, 1), nka_varc_space(nka_varc_inc(0.25f, 0, 0, 0), NKA_COL_SRGB), + NKA_COL_SRGB, NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("cb"), 0, rgba(0, 0, 1, 1), nka_varc_dec(0, 0, 0, 0.25f), NKA_COL_OKLAB, NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("ch"), 0, rgba(1, 0, 0, 1), + nka_varc_space(nka_varc_channel(nka_varf_inc(0.5f), nka_varf_none(), nka_varf_none(), nka_varf_none()), NKA_COL_HSV), + NKA_COL_SRGB, NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("cf"), 0, rgba(1, 1, 1, 1), nka_varc_fn(grey, 0), NKA_COL_SRGB, NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("cm"), 0, rgba(0.5f, 0.5f, 0.5f, 0.5f), nka_varc_space(nka_varc_mul(0.5f), NKA_COL_SRGB_LINEAR), + NKA_COL_SRGB, NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("cr"), 0, rgba(0.5f, 0.5f, 0.5f, 1), + nka_varc_clamp(nka_varc_seed(nka_varc_rand(0.1f, 0.1f, 0.1f, 0), 3), v4(0, -1, -1, 0), v4(1, 1, 1, 1)), + NKA_COL_OKLAB, NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("cd"), 0, rgba(0.5f, 0.5f, 0.5f, 1), nka_varc_space(nka_varc_dec(0.5f, 0, 0, 0), NKA_COL_SRGB), + NKA_COL_SRGB, NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("cn"), 0, rgba(0.3f, 0.6f, 0.9f, 1), nka_varc_none(), NKA_COL_SRGB, NKA_EASE_LINEAR, 0); + build_var(a, H("many")); + for (k = 0; k < 4; ++k) { + struct nk_vec2 p; + struct nka_vec4 q; + struct nk_colorf c; + nk_uint s1; + float kf = (float)k; + CHECK(geti(a, H("vi"), "i") == 1 + 3 * k); + CHECK(geti(a, H("vi"), "j") == (1 - 5 * k < -6 ? -6 : 1 - 5 * k)); + s1 = 9u + (nk_uint)k * 1664525u; + CHECK(geti(a, H("vi"), "k") == 1 + (int)((unit_of(&s1) * 2 - 1) * 50)); + CHECK(geti(a, H("vi"), "l") == 100 * k); + nka_instance_get_vec2(a, H("vi"), H("p"), &p); + CHECK(p.x == kf && p.y == 2 * kf); + nka_instance_get_vec2(a, H("vi"), H("pa"), &p); + CHECK(p.x == kf && p.y == -kf); + nka_instance_get_vec2(a, H("vi"), H("pm"), &p); + CHECK(p.x == powf(2, kf) && p.y == (3 * powf(2, kf) > 20 ? 20 : 3 * powf(2, kf))); + nka_instance_get_vec2(a, H("vi"), H("pr"), &p); + s1 = 31u + (nk_uint)k * 1664525u; + CHECK(NEAR(p.x, (unit_of(&s1) * 2 - 1) * 1, 1e-6f)); + CHECK(NEAR(p.y, (unit_of(&s1) * 2 - 1) * 2, 1e-6f)); + nka_instance_get_vec2(a, H("vi"), H("pf"), &p); + CHECK(p.x == kf && p.y == -kf); + nka_instance_get_vec2(a, H("vi"), H("pd"), &p); + CHECK(p.x == -kf && p.y == -kf); + nka_instance_get_vec4(a, H("vi"), H("q"), &q); + CHECK(q.x == kf && q.y == 2 * kf && q.z == 3 * kf && q.w == 4 * kf); + nka_instance_get_vec4(a, H("vi"), H("qa"), &q); + CHECK(q.x == kf && q.y == 0 && q.z == -kf && q.w == 0); + nka_instance_get_vec4(a, H("vi"), H("qm"), &q); + CHECK(q.x == (k < 2 ? powf(3, kf) : 5)); + nka_instance_get_vec4(a, H("vi"), H("qr"), &q); + s1 = 5u + (nk_uint)k * 1664525u; + CHECK(NEAR(q.x, unit_of(&s1) * 2 - 1, 1e-6f) && NEAR(q.y, unit_of(&s1) * 2 - 1, 1e-6f)); + nka_instance_get_vec4(a, H("vi"), H("qd"), &q); + CHECK(q.x == -kf && q.w == -kf); + nka_instance_get_vec4(a, H("vi"), H("qf"), &q); + CHECK(NEAR(q.x, 0.1f * kf, 1e-6f) && q.w == 0.4f); + nka_instance_get_color(a, H("vi"), H("cs"), &c); + CHECK(NEAR(c.r, k < 3 ? 0.25f * (kf + 1) : 1, 1e-6f) && c.g == 0 && c.a == 1); + /* Blue keeps its hue while its alpha fades: upstream clamped OKLAB's b to 0 and lost it. */ + nka_instance_get_color(a, H("vi"), H("cb"), &c); + CHECK(NEAR(c.b, 1, 1e-3f) && c.r < 1e-3f && c.g < 1e-3f && NEAR(c.a, 1 - 0.25f * kf, 1e-5f)); + nka_instance_get_color(a, H("vi"), H("ch"), &c); + CHECK(k % 2 ? (NEAR(c.r, 0, 1e-5f) && NEAR(c.g, 1, 1e-5f)) : (NEAR(c.r, 1, 1e-5f) && NEAR(c.g, 0, 1e-5f))); + nka_instance_get_color(a, H("vi"), H("cf"), &c); + CHECK(NEAR(c.r, 0.1f * kf, 1e-6f) && c.g == 0.2f && c.a == 0.4f); + nka_instance_get_color(a, H("vi"), H("cm"), &c); + CHECK(NEAR(c.a, 0.5f, 1e-6f) && (k == 0 ? NEAR(c.r, 0.5f, 1e-5f) : c.r < 0.5f)); + nka_instance_get_color(a, H("vi"), H("cr"), &c); + CHECK(c.r >= 0 && c.r <= 1 && c.g >= 0 && c.g <= 1 && c.a == 1); + nka_instance_get_color(a, H("vi"), H("cd"), &c); + CHECK(NEAR(c.r, k ? 0 : 0.5f, 1e-6f)); + nka_instance_get_color(a, H("vi"), H("cn"), &c); + CHECK(c.r == 0.3f && c.g == 0.6f && c.b == 0.9f); + next_loop(a); + } + nka_destroy(a); + + /* Duration variation changes the speed of a loop; upstream cut the loop short. */ + a = nka_create(0); + nka_clip_begin(a, H("dv")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, 2); + nka_clip_set_duration_var(a, nka_varf_mul(0.5f)); + nka_clip_end(a); + nka_play(a, H("dv"), H("d")); + steps(a, 5, 0.25f); + CHECK(nka_instance_time(a, H("d")) == 0.5f && getf(a, H("d"), "f") == 5); + steps(a, 2, 0.25f); + CHECK(nka_instance_time(a, H("d")) == 1 && nka_instance_is_playing(a, H("d")) && inst_of(a, H("d"))->loop == 2); + nka_update(a, 0.25f); + CHECK(!nka_instance_is_playing(a, H("d")) && getf(a, H("d"), "f") == 10); + + nka_clip_begin(a, H("delv")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, 2); + nka_clip_set_delay_var(a, nka_varf_inc(0.5f)); + nka_clip_end(a); + nka_play(a, H("delv"), H("d")); + steps(a, 5, 0.25f); + CHECK(nka_instance_time(a, H("d")) == 0 && NEAR(inst_of(a, H("d"))->delay, 0.25f, 1e-6f)); + steps(a, 2, 0.25f); + CHECK(nka_instance_time(a, H("d")) == 0.25f); + steps(a, 4, 0.25f); + CHECK(nka_instance_time(a, H("d")) == 0 && NEAR(inst_of(a, H("d"))->delay, 0.75f, 1e-6f)); + + nka_clip_begin(a, H("negv")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, 2); + nka_clip_set_delay_var(a, nka_varf_dec(0.5f)); + nka_clip_end(a); + nka_play(a, H("negv"), H("d")); + steps(a, 5, 0.25f); + CHECK(nka_instance_time(a, H("d")) == 0.25f); + + nka_clip_begin(a, H("tsv")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, 2); + nka_clip_set_timescale_var(a, nka_varf_mul(2)); + nka_clip_end(a); + nka_play(a, H("tsv"), H("d")); + nka_instance_set_time_scale(a, H("d"), 0.5f); + steps(a, 8, 0.25f); + CHECK(nka_instance_time(a, H("d")) == 1 && inst_of(a, H("d"))->loop == 0); + nka_update(a, 0.25f); + CHECK(nka_instance_time(a, H("d")) == 0.5f && inst_of(a, H("d"))->scale == 2); + nka_destroy(a); +} + +static void test_stagger(void) +{ + struct nka_context *a = nka_create(0); + struct nka_stagger_grid_opts o = nka_stagger_grid_opts_default(); + int k, from, axis, ease, col, row, ok = 1; + + nka_clip_begin(a, H("st")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_set_stagger(a, 5, 0.1f, 0); + nka_clip_set_delay(a, 0.05f); + nka_clip_end(a); + for (k = 0; k < 5; ++k) CHECK(NEAR(nka_stagger_delay(a, H("st"), k), 0.1f * (float)k, 1e-6f)); + CHECK(nka_play_stagger(a, H("st"), H("s3"), 3) == H("s3") && NEAR(inst_of(a, H("s3"))->delay, 0.35f, 1e-6f)); + CHECK(nka_play_stagger(a, H("missing"), H("s4"), 3) == 0 && !nka_instance_valid(a, H("s4"))); + + nka_clip_begin(a, H("stc")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_set_stagger(a, 5, 0.1f, 1); + nka_clip_end(a); + CHECK(NEAR(nka_stagger_delay(a, H("stc"), 0), 0.32f, 1e-6f) && nka_stagger_delay(a, H("stc"), 2) == 0); + CHECK(NEAR(nka_stagger_delay(a, H("stc"), 4), 0.32f, 1e-6f)); + + nka_clip_begin(a, H("sth")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_set_stagger(a, 5, 0.1f, 0.5f); + nka_clip_end(a); + CHECK(NEAR(nka_stagger_delay(a, H("sth"), 0), 0.16f, 1e-6f) && NEAR(nka_stagger_delay(a, H("sth"), 4), 0.36f, 1e-6f)); + + nka_clip_begin(a, H("ste")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_set_stagger(a, 5, 0.1f, -3); + nka_clip_set_stagger_ease(a, NKA_EASE_IN_QUAD); + nka_clip_end(a); + CHECK(NEAR(nka_stagger_delay(a, H("ste"), 2), 0.1f, 1e-6f) && NEAR(nka_stagger_delay(a, H("ste"), 9), 0.4f, 1e-6f)); + + nka_clip_begin(a, H("st1")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_set_stagger(a, 0, 0.1f, 0); + nka_clip_end(a); + CHECK(nka_stagger_delay(a, H("st1"), 3) == 0 && nka_stagger_delay(a, H("none"), 3) == 0); + + /* Against upstream's search of every cell for the farthest one. */ + o.cols = 4; + o.rows = 3; + o.delay = 0.1f; + for (from = NKA_STAGGER_FIRST; from <= NKA_STAGGER_INDEX + 1; ++from) + for (axis = NKA_STAGGER_BOTH; axis <= NKA_STAGGER_Y; ++axis) + for (ease = 0; ease < 2; ++ease) + for (row = 0; row < 3; ++row) + for (col = 0; col < 4; ++col) { + float ox = 0, oy = 0, most = 0, d, t, want; + int r, c, at = 13; + o.from = from; + o.from_index = 13; + o.axis = axis; + o.ease = ease ? NKA_EASE_IN_QUAD : NKA_EASE_LINEAR; + o.start_delay = 0.5f; + if (from == NKA_STAGGER_LAST) { ox = 3; oy = 2; } + if (from == NKA_STAGGER_CENTER) { ox = 1.5f; oy = 1; } + if (from == NKA_STAGGER_INDEX) { at = at > 11 ? 11 : at; ox = (float)(at % 4); oy = (float)(at / 4); } + for (r = 0; r < 3; ++r) + for (c = 0; c < 4; ++c) { + float dx = (float)c - ox, dy = (float)r - oy; + d = axis == NKA_STAGGER_X ? fabsf(dx) : axis == NKA_STAGGER_Y ? fabsf(dy) : sqrtf(dx * dx + dy * dy); + if (d > most) most = d; + } + { + float dx = (float)col - ox, dy = (float)row - oy; + d = axis == NKA_STAGGER_X ? fabsf(dx) : axis == NKA_STAGGER_Y ? fabsf(dy) : sqrtf(dx * dx + dy * dy); + } + t = most > 0 ? d / most : 0; + if (ease) t = nka_eval_preset(NKA_EASE_IN_QUAD, t); + want = 0.5f + t * 0.1f * 11; + ok = ok && nka_stagger_grid_delay(a, col, row, &o) == want; + ok = ok && nka_stagger_grid_delay_index(a, row * 4 + col, &o) == want; + } + CHECK(ok); + o = nka_stagger_grid_opts_default(); + o.cols = 4; + o.rows = 3; + o.delay = 0.1f; + CHECK(nka_stagger_grid_delay(a, 0, 0, &o) == 0 && NEAR(nka_stagger_grid_delay(a, 3, 2, &o), 1.1f, 1e-6f)); + o.from = NKA_STAGGER_LAST; + CHECK(NEAR(nka_stagger_grid_delay(a, 0, 0, &o), 1.1f, 1e-6f) && nka_stagger_grid_delay(a, 3, 2, &o) == 0); + o.from = NKA_STAGGER_INDEX; + o.from_index = 5; + CHECK(nka_stagger_grid_delay(a, 1, 1, &o) == 0 && NEAR(nka_stagger_grid_delay(a, 3, 0, &o), 1.1f, 1e-6f)); + o.from = NKA_STAGGER_FIRST; + o.axis = NKA_STAGGER_X; + CHECK(NEAR(nka_stagger_grid_delay(a, 2, 2, &o), 1.1f * 2 / 3, 1e-6f)); + CHECK(nka_stagger_grid_delay(a, 5, 5, 0) == 0 && nka_stagger_grid_delay_index(0, 7, 0) == 0); + o.cols = 0; + o.rows = -2; + o.start_delay = 0.25f; + CHECK(nka_stagger_grid_delay(a, 1, 1, &o) == 0.25f); + nka_destroy(a); +} + +static void test_spring(void) +{ + struct nka_context *a = nka_create(0); + struct nka_spring_params sp = { 1, 100, 20, 0 }; + nk_hash inst = H("sp"); + int k; + nka_clip_begin(a, H("spring")); + nka_clip_key_float_spring(a, H("f"), 0, 0, sp); + nka_clip_key_float(a, H("f"), 1, 10, NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("g"), 0, 0, NKA_EASE_LINEAR, 0); + nka_clip_key_float_spring(a, H("g"), 1, 10, sp); + nka_clip_end(a); + nka_play(a, H("spring"), inst); + for (k = 0; k < 10; ++k) { + float u = (float)k / 10; + nka_instance_seek(a, inst, u); + CHECK(NEAR(getf(a, inst, "f"), 10 * (1 - expf(-10 * u) * (1 + 10 * u)), 1e-4f)); + CHECK(NEAR(getf(a, inst, "g"), 10 * u, 1e-5f)); + } + nka_instance_seek(a, inst, 1); + CHECK(getf(a, inst, "f") == 10 && getf(a, inst, "g") == 10); + nka_instance_seek(a, inst, 0.5f); + CHECK(NEAR(getf(a, inst, "f"), 10 * (1 - expf(-5) * 6), 1e-4f)); + nka_destroy(a); +} + +static void test_relative(void) +{ + struct nka_context *a = nka_create(0); + nk_hash inst = H("r"); + struct nk_vec2 p; + struct nka_vec4 q; + struct nk_colorf c; + nka_set_anchor(a, NKA_ANCHOR_WINDOW, v2(200, 100)); + nka_set_anchor(a, NKA_ANCHOR_VIEWPORT, v2(1000, 500)); + nka_clip_begin(a, H("rel")); + nka_clip_key_float_rel(a, H("x"), 0, 0, 35, NKA_ANCHOR_WINDOW, 0, NKA_EASE_LINEAR, 0); + nka_clip_key_float_rel(a, H("x"), 2, 1, -35, NKA_ANCHOR_WINDOW, 0, NKA_EASE_LINEAR, 0); + nka_clip_key_float_rel(a, H("y"), 0, 0.5f, 0, NKA_ANCHOR_WINDOW, 1, NKA_EASE_LINEAR, 0); + nka_clip_key_vec2_rel(a, H("p"), 0, v2(0.5f, 0.5f), v2(10, -10), NKA_ANCHOR_VIEWPORT, NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_rel(a, H("q"), 0, v4(0.5f, 0.5f, 2, 3), v4(1, 2, 3, 4), NKA_ANCHOR_WINDOW, NKA_EASE_LINEAR, 0); + nka_clip_key_color_rel(a, H("c"), 0, v4(0.001f, 0.002f, 0.003f, 0.004f), v4(0, 0, 0.1f, 0), NKA_COL_SRGB, NKA_ANCHOR_WINDOW, + NKA_EASE_LINEAR, 0); + nka_clip_key_float(a, H("x"), 0, 1000, NKA_EASE_LINEAR, 0); + nka_clip_end(a); + nka_play(a, H("rel"), inst); + CHECK(getf(a, inst, "x") == 35 && getf(a, inst, "y") == 50); + nka_instance_seek(a, inst, 1); + CHECK(getf(a, inst, "x") == 100); + nka_set_anchor(a, NKA_ANCHOR_WINDOW, v2(400, 300)); + CHECK(getf(a, inst, "x") == 200 && getf(a, inst, "y") == 150); + CHECK(nka_instance_get_vec2(a, inst, H("p"), &p) && p.x == 510 && p.y == 240); + CHECK(nka_instance_get_vec4(a, inst, H("q"), &q) && q.x == 201 && q.y == 152 && q.z == 5 && q.w == 7); + CHECK(nka_instance_get_color(a, inst, H("c"), &c) && NEAR(c.r, 0.4f, 1e-6f) && NEAR(c.g, 0.6f, 1e-6f) + && NEAR(c.b, 1.3f, 1e-6f) && NEAR(c.a, 1.2f, 1e-6f)); + nka_update(a, 0.5f); + CHECK(getf(a, inst, "x") == 1.5f * 400 / 2 + 35 - 35 * 1.5f); + nka_destroy(a); +} + +/* Saving */ + +static nk_hash rich(struct nka_context *a) +{ + const float bz[4] = { 0.4f, 0, 0.2f, 1 }; + struct nka_spring_params sp = { 1, 150, 12, 2 }; + nk_hash clip = H("rich"); + nka_clip_begin(a, clip); + nka_clip_key_float(a, H("f"), 0, 1, NKA_EASE_OUT_BACK, 0); + nka_clip_key_float(a, H("f"), 0.8f, -2, NKA_EASE_CUBIC_BEZIER, bz); + nka_clip_key_float(a, H("f"), 1.6f, 3, NKA_EASE_LINEAR, 0); + nka_clip_key_float_spring(a, H("s"), 0, 0, sp); + nka_clip_key_float(a, H("s"), 1, 1, NKA_EASE_LINEAR, 0); + nka_clip_key_vec2_var(a, H("v2"), 0, v2(1, 2), nka_varv2_seed(nka_varv2_rand(3, 4), 99), NKA_EASE_IN_OUT_SINE, 0); + nka_clip_key_vec2_var(a, H("v2"), 1.2f, v2(5, 6), nka_varv2_axis(nka_varf_inc(1), nka_varf_rand(2)), NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_var(a, H("v4"), 0, v4(1, 2, 3, 4), nka_varv4_clamp(nka_varv4_inc(1, 1, 1, 1), v4(0, 0, 0, 0), v4(5, 5, 5, 5)), + NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_var(a, H("v4"), 2, v4(4, 3, 2, 1), nka_varv4_axis(nka_varf_none(), nka_varf_mul(2), nka_varf_none(), nka_varf_dec(1)), + NKA_EASE_IN_QUAD, 0); + nka_clip_key_int_var(a, H("i"), 0, 3, nka_vari_seed(nka_vari_rand(7), 5), NKA_EASE_LINEAR); + nka_clip_key_int(a, H("i"), 1, -40, NKA_EASE_OUT_CUBIC); + nka_clip_key_int_var(a, H("i"), 2, 2147483000, nka_vari_clamp(nka_vari_inc(1000), 0, 2147483600), NKA_EASE_LINEAR); + nka_clip_key_color_var(a, H("c"), 0, rgba(1, 0.5f, 0, 1), nka_varc_space(nka_varc_rand(0.1f, 0.1f, 0.1f, 0), NKA_COL_HSV), + NKA_COL_OKLCH, NKA_EASE_LINEAR, 0); + nka_clip_key_color_var(a, H("c"), 1.5f, rgba(0, 0.2f, 1, 0.5f), + nka_varc_channel(nka_varf_none(), nka_varf_inc(0.01f), nka_varf_none(), nka_varf_dec(0.1f)), + NKA_COL_OKLCH, NKA_EASE_IN_OUT_CUBIC, 0); + nka_clip_key_float_rel(a, H("x"), 0, 0, 35, NKA_ANCHOR_WINDOW, 0, NKA_EASE_LINEAR, 0); + nka_clip_key_float_rel(a, H("x"), 2, 1, -35, NKA_ANCHOR_WINDOW, 0, NKA_EASE_OUT_QUAD, 0); + nka_clip_key_vec2_rel(a, H("p"), 0.5f, v2(0.5f, 0.5f), v2(1, 2), NKA_ANCHOR_VIEWPORT, NKA_EASE_LINEAR, 0); + nka_clip_key_vec4_rel(a, H("r"), 0, v4(0.1f, 0.2f, 1, 2), v4(1, 2, 3, 4), NKA_ANCHOR_LAST_ITEM, NKA_EASE_LINEAR, 0); + nka_clip_key_color_rel(a, H("cr"), 0, v4(0.001f, 0.002f, 0.003f, 0.004f), v4(0, 0, 0, 0), NKA_COL_SRGB, NKA_ANCHOR_WINDOW, + NKA_EASE_LINEAR, 0); + nka_clip_key_float_var(a, H("u"), 0, 2, nka_varf_rand(1), NKA_EASE_LINEAR, 0); + nka_clip_key_float_var(a, H("u"), 1, 4, nka_varf_clamp(nka_varf_pingpong(1.5f), 0, 5), NKA_EASE_LINEAR, 0); + nka_clip_seq_begin(a); + key2(a, "q", 0, 0, 0.5f, 1); + nka_clip_seq_end(a); + nka_clip_marker(a, 0.25f, 0, on_mark, 0); + nka_clip_marker(a, 0, H("m1"), on_mark, 0); + nka_clip_marker(a, -0.1f, H("m2"), on_mark, 0); + nka_clip_set_loop(a, 1, NKA_DIR_ALTERNATE, 3); + nka_clip_set_loop_delay(a, 0.1f); + nka_clip_set_delay(a, 0.05f); + nka_clip_set_stagger(a, 4, 0.1f, 0.5f); + nka_clip_set_stagger_ease(a, NKA_EASE_OUT_QUAD); + nka_clip_set_duration_var(a, nka_varf_seed(nka_varf_rand(0.2f), 7)); + nka_clip_set_delay_var(a, nka_varf_inc(0.05f)); + nka_clip_set_timescale_var(a, nka_varf_pingpong(0.25f)); + nka_clip_on_begin(a, on_begin, 0); + nka_clip_on_complete(a, on_complete, 0); + nka_clip_end(a); + return clip; +} + +static int same_values(struct nka_context *a, struct nka_context *b, nk_hash inst) +{ + static const char *const floats[] = { "f", "s", "x", "u", "q" }; + struct nk_vec2 pa, pb; + struct nka_vec4 qa, qb; + struct nk_colorf ca, cb; + int i, ok = nka_instance_time(a, inst) == nka_instance_time(b, inst); + ok = ok && nka_instance_is_playing(a, inst) == nka_instance_is_playing(b, inst); + for (i = 0; i < 5; ++i) ok = ok && getf(a, inst, floats[i]) == getf(b, inst, floats[i]); + ok = ok && geti(a, inst, "i") == geti(b, inst, "i"); + nka_instance_get_vec2(a, inst, H("v2"), &pa); + nka_instance_get_vec2(b, inst, H("v2"), &pb); + ok = ok && pa.x == pb.x && pa.y == pb.y; + nka_instance_get_vec2(a, inst, H("p"), &pa); + nka_instance_get_vec2(b, inst, H("p"), &pb); + ok = ok && pa.x == pb.x && pa.y == pb.y; + nka_instance_get_vec4(a, inst, H("v4"), &qa); + nka_instance_get_vec4(b, inst, H("v4"), &qb); + ok = ok && qa.x == qb.x && qa.y == qb.y && qa.z == qb.z && qa.w == qb.w; + nka_instance_get_vec4(a, inst, H("r"), &qa); + nka_instance_get_vec4(b, inst, H("r"), &qb); + ok = ok && qa.x == qb.x && qa.w == qb.w; + nka_instance_get_color(a, inst, H("c"), &ca); + nka_instance_get_color(b, inst, H("c"), &cb); + ok = ok && ca.r == cb.r && ca.g == cb.g && ca.b == cb.b && ca.a == cb.a; + nka_instance_get_color(a, inst, H("cr"), &ca); + nka_instance_get_color(b, inst, H("cr"), &cb); + ok = ok && ca.r == cb.r && ca.a == cb.a; + return ok; +} + +static void put32(unsigned char *p, nk_uint v) +{ + p[0] = (unsigned char)(v & 0xff); + p[1] = (unsigned char)((v >> 8) & 0xff); + p[2] = (unsigned char)((v >> 16) & 0xff); + p[3] = (unsigned char)(v >> 24); +} + +static nk_uint get32(const unsigned char *p) +{ + return (nk_uint)p[0] | ((nk_uint)p[1] << 8) | ((nk_uint)p[2] << 16) | ((nk_uint)p[3] << 24); +} + +static void reseal(unsigned char *p, int size) { put32(p + size - 4, nka__clip_fnv(p, size - 4)); } + +static void test_persist(void) +{ + struct nka_context *a = nka_create(0), *b = nka_create(0); + nk_hash clip = rich(a), out = 0, inst = H("p"); + unsigned char *buf, *copy, *again; + int size, i, bad = 0, ok = 1, frame; + + CHECK(nka_clip_exists(a, clip) && nka_clip_save(a, H("nothing"), 0, 0) == 0); + size = nka_clip_save(a, clip, 0, 0); + CHECK(size > 128 && size % 4 == 0); + buf = (unsigned char *)malloc((size_t)size + 8); + copy = (unsigned char *)malloc((size_t)size + 8); + again = (unsigned char *)malloc((size_t)size + 8); + memset(buf, 0xab, (size_t)size + 8); + CHECK(nka_clip_save(a, clip, buf, size - 1) == size); + for (i = 0; i < size + 8; ++i) ok = ok && buf[i] == 0xab; + CHECK(ok); + CHECK(nka_clip_save(a, clip, buf, size) == size && buf[size] == 0xab); + CHECK(buf[0] == 'N' && buf[1] == 'K' && buf[2] == 'A' && buf[3] == 'C'); + CHECK(get32(buf + 4) == 1 && get32(buf + 8) == (nk_uint)size && get32(buf + 12) == clip); + + CHECK(nka_clip_load(b, buf, size, &out) == NKA_OK && out == clip); + CHECK(nka_clip_duration(b, clip) == nka_clip_duration(a, clip) && nka_clip_duration(b, clip) == 2); + CHECK(nka_clip_save(b, clip, again, size) == size && memcmp(buf, again, (size_t)size) == 0); + CHECK(nka_stagger_delay(a, clip, 3) == nka_stagger_delay(b, clip, 3) && nka_stagger_delay(a, clip, 3) > 0); + + /* Played side by side, the copy matches bit for bit. */ + nka_set_anchor(a, NKA_ANCHOR_WINDOW, v2(300, 200)); + nka_set_anchor(b, NKA_ANCHOR_WINDOW, v2(300, 200)); + nka_set_anchor(a, NKA_ANCHOR_VIEWPORT, v2(800, 600)); + nka_set_anchor(b, NKA_ANCHOR_VIEWPORT, v2(800, 600)); + nka_set_anchor(a, NKA_ANCHOR_LAST_ITEM, v2(40, 20)); + nka_set_anchor(b, NKA_ANCHOR_LAST_ITEM, v2(40, 20)); + nka_play(a, clip, inst); + nka_play(b, clip, inst); + ok = same_values(a, b, inst); + marks.n = 0; + reset_events(); + for (frame = 0; frame < 300; ++frame) { + nka_update(a, 0.037f); + nka_update(b, 0.037f); + ok = ok && same_values(a, b, inst); + } + CHECK(ok && !nka_instance_is_playing(a, inst) && inst_of(a, inst)->loop == 3); + CHECK(marks.n == 12 && strcmp(events, "BC") == 0); + + /* Loading over a clip keeps its callbacks and its markers' by id. */ + CHECK(nka_clip_load(a, buf, size, 0) == NKA_OK); + CHECK(nka_clip_save(a, clip, again, size) == size && memcmp(buf, again, (size_t)size) == 0); + marks.n = 0; + reset_events(); + nka_play(a, clip, inst); + steps(a, 300, 0.037f); + CHECK(marks.n == 12 && strcmp(events, "BC") == 0); + + /* Every short read, every flipped byte, and trailing bytes. */ + for (i = 0; i < size; ++i) bad += nka_clip_load(b, buf, i, &out) == NKA_ERR_BAD_ARG; + CHECK(bad == size); + bad = 0; + for (i = 0; i < size; ++i) { + memcpy(copy, buf, (size_t)size); + copy[i] ^= 0x5a; + bad += nka_clip_load(b, copy, size, &out) == NKA_ERR_BAD_ARG; + } + CHECK(bad == size); + CHECK(nka_clip_load(b, buf, size + 8, &out) == NKA_OK); + + /* A sum that fits does not let a broken clip through. */ + memcpy(copy, buf, (size_t)size); + put32(copy + 4, 2); + reseal(copy, size); + CHECK(nka_clip_load(b, copy, size, &out) == NKA_ERR_BAD_ARG); + memcpy(copy, buf, (size_t)size); + put32(copy + 124, 99); + reseal(copy, size); + CHECK(nka_clip_load(b, copy, size, &out) == NKA_ERR_BAD_ARG); + memcpy(copy, buf, (size_t)size); + put32(copy + 116, 1000000); + reseal(copy, size); + CHECK(nka_clip_load(b, copy, size, &out) == NKA_ERR_BAD_ARG); + memcpy(copy, buf, (size_t)size); + put32(copy + 144, 0x7fc00000u); + reseal(copy, size); + CHECK(nka_clip_load(b, copy, size, &out) == NKA_ERR_BAD_ARG); + memcpy(copy, buf, (size_t)size); + put32(copy + 8, (nk_uint)size + 4); + put32(copy + size, 0); + reseal(copy, size + 4); + CHECK(nka_clip_load(b, copy, size + 4, &out) == NKA_ERR_BAD_ARG); + memcpy(copy, buf, (size_t)size); + put32(copy + 8, 12); + CHECK(nka_clip_load(b, copy, size, &out) == NKA_ERR_BAD_ARG); + CHECK(nka_clip_load(b, 0, size, &out) == NKA_ERR_BAD_ARG && nka_clip_load(0, buf, size, &out) == NKA_ERR_BAD_ARG); + CHECK(nka_clip_load(b, buf, -5, &out) == NKA_ERR_BAD_ARG); + + /* A clip with nothing in it, under id 0. */ + nka_clip_begin(a, 0); + CHECK(nka_clip_end(a) == NKA_OK && nka_clip_exists(a, 0) && nka_clip_duration(a, 0) == 0); + size = nka_clip_save(a, 0, again, 128); + CHECK(size == 128 && nka_clip_load(b, again, size, &out) == NKA_OK && out == 0 && nka_clip_exists(b, 0)); + + free(buf); + free(copy); + free(again); + nka_destroy(a); + nka_destroy(b); +} + +static void test_gc(void) +{ + struct nka_context *a = nka_create(0); + float f; + int i; + nka_clip_begin(a, H("short")); + key2(a, "f", 0, 0, 0.25f, 1); + nka_clip_end(a); + nka_clip_begin(a, H("forever")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, -1); + nka_clip_end(a); + nka_play(a, H("short"), H("done_untouched")); + nka_play(a, H("short"), H("done_read")); + nka_play(a, H("forever"), H("playing")); + nka_play(a, H("forever"), H("paused_read")); + nka_play(a, H("forever"), H("paused_untouched")); + nka_instance_pause(a, H("paused_read")); + nka_instance_pause(a, H("paused_untouched")); + nka_layer_begin(a, H("lt")); + nka_layer_add(a, H("playing"), 1); + nka_layer_end(a, H("lt")); + nka_layer_begin(a, H("lt_read")); + nka_layer_add(a, H("playing"), 1); + nka_layer_end(a, H("lt_read")); + for (i = 0; i < 20; ++i) { + nka_update(a, 0.1f); + getf(a, H("done_read"), "f"); + nka_instance_is_paused(a, H("paused_read")); + nka_get_blended_float(a, H("lt_read"), H("f"), &f); + } + nka_gc(a, 10); + CHECK(a->clip_insts.len == 3 && a->clip_layers.len == 1); + CHECK(!nka_instance_valid(a, H("done_untouched")) && !nka_instance_valid(a, H("paused_untouched"))); + CHECK(nka_instance_valid(a, H("done_read")) && nka_instance_valid(a, H("playing")) && nka_instance_valid(a, H("paused_read"))); + CHECK(!nka_get_blended_float(a, H("lt"), H("f"), &f) && nka_get_blended_float(a, H("lt_read"), H("f"), &f)); + nka_clip_reserve(a, 100, 1000); + CHECK(a->clip_clips.cap == 256 && a->clip_insts.cap == 2048 && nka_instance_valid(a, H("playing"))); + nka_clear(a); + CHECK(a->clip_insts.len == 0 && a->clip_layers.len == 0 && !nka_instance_valid(a, H("playing"))); + CHECK(nka_clip_exists(a, H("forever")) && nka_play(a, H("forever"), H("again")) == H("again")); + nka_update(a, 0.5f); + CHECK(getf(a, H("again"), "f") == 0.5f); + nka_destroy(a); +} + +static void test_busy(void) +{ + struct nka_context *a = nka_create(0); + nka_clip_begin(a, H("b")); + key2(a, "f", 0, 0, 0.5f, 1); + nka_clip_end(a); + nka_update(a, 0.25f); + CHECK(!nka_busy(a)); + nka_play(a, H("b"), H("i")); + CHECK(nka_busy(a)); + nka_update(a, 0.25f); + CHECK(nka_busy(a)); + nka_update(a, 0.25f); + CHECK(nka_busy(a) && nka_instance_is_playing(a, H("i"))); + nka_update(a, 0.25f); + CHECK(!nka_busy(a) && !nka_instance_is_playing(a, H("i"))); + nka_update(a, 0.25f); + CHECK(!nka_busy(a)); + nka_play_with_delay(a, H("b"), H("i"), 1); + nka_update(a, 0.25f); + CHECK(nka_busy(a)); + nka_instance_pause(a, H("i")); + nka_update(a, 0.25f); + CHECK(!nka_busy(a)); + nka_destroy(a); +} + +static void test_misc(void) +{ + struct nka_context *a = nka_create(0); + unsigned char buf[8] = { 0 }; + struct nk_vec2 p; + float f; + int i; + + /* A clip with no length completes at once; upstream played forever. */ + nka_clip_begin(a, H("zero")); + nka_clip_key_float(a, H("f"), 0, 7, NKA_EASE_LINEAR, 0); + nka_clip_marker(a, 0, H("z"), on_mark, 0); + nka_clip_set_loop(a, 1, NKA_DIR_NORMAL, -1); + nka_clip_on_complete(a, on_complete, 0); + nka_clip_end(a); + reset_events(); + marks.n = 0; + nka_play(a, H("zero"), H("z")); + CHECK(getf(a, H("z"), "f") == 7 && nka_instance_is_playing(a, H("z"))); + nka_update(a, 0.1f); + CHECK(!nka_instance_is_playing(a, H("z")) && marks.n == 1 && strcmp(events, "C") == 0 && !nka_busy(a)); + + /* A clip authored again while it plays. */ + nka_clip_begin(a, H("rb")); + key2(a, "f", 0, 0, 1, 10); + nka_clip_end(a); + nka_play(a, H("rb"), H("r")); + nka_update(a, 0.5f); + nka_clip_begin(a, H("rb")); + key2(a, "g", 0, 0, 0.25f, 1); + key2(a, "f", 0, 100, 0.25f, 100); + CHECK(getf(a, H("r"), "f") == 5); + nka_clip_end(a); + CHECK(getf(a, H("r"), "f") == 100 && getf(a, H("r"), "g") == 1 && nka_instance_time(a, H("r")) == 0.25f); + nka_update(a, 0.1f); + CHECK(!nka_instance_is_playing(a, H("r"))); + + /* A second begin drops the first build. */ + nka_clip_begin(a, H("first")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_begin(a, H("second")); + key2(a, "f", 0, 0, 1, 1); + CHECK(nka_clip_end(a) == NKA_OK && !nka_clip_exists(a, H("first")) && nka_clip_exists(a, H("second"))); + CHECK(nka_clip_end(a) == NKA_ERR_BAD_ARG); + + for (i = 0; i < 300; ++i) { + nka_clip_begin(a, nka_id_mix(H("bulk"), (nk_hash)i)); + key2(a, "f", 0, (float)i, 1, 0); + nka_clip_end(a); + } + for (i = 0; i < 300; ++i) CHECK(nka_play(a, nka_id_mix(H("bulk"), (nk_hash)i), nka_id_mix(H("bi"), (nk_hash)i))); + for (i = 0; i < 300; ++i) CHECK(getf(a, nka_id_mix(H("bi"), (nk_hash)i), "f") == (float)i); + nka_destroy(a); + + nka_clip_begin(0, 1); + nka_clip_key_float(0, 1, 0, 1, NKA_EASE_LINEAR, 0); + nka_clip_key_int(0, 1, 0, 1, NKA_EASE_LINEAR); + nka_clip_seq_begin(0); + nka_clip_par_end(0); + nka_clip_set_loop(0, 1, 0, 1); + nka_clip_on_begin(0, on_begin, 0); + CHECK(nka_clip_marker(0, 0, 0, 0, 0) == 0 && nka_clip_end(0) == NKA_ERR_BAD_ARG); + CHECK(nka_play(0, 1, 1) == 0 && nka_play_stagger(0, 1, 1, 1) == 0 && nka_play_with_delay(0, 1, 1, 1) == 0); + CHECK(nka_get_instance(0, 1) == 0 && !nka_instance_valid(0, 1) && nka_instance_then(0, 1, 1, 0) == 0); + nka_instance_pause(0, 1); + nka_instance_seek(0, 1, 1); + nka_instance_destroy(0, 1); + nka_instance_set_weight(0, 1, 1); + nka_instance_then_delay(0, 1, 1); + CHECK(nka_instance_time(0, 1) == 0 && nka_instance_duration(0, 1) == 0 && !nka_instance_is_playing(0, 1)); + CHECK(!nka_instance_get_float(0, 1, 1, &f) && f == 0 && !nka_instance_get_vec2(0, 1, 1, &p)); + CHECK(nka_clip_duration(0, 1) == 0 && !nka_clip_exists(0, 1) && nka_stagger_delay(0, 1, 2) == 0); + CHECK(nka_stagger_grid_delay(0, 1, 1, 0) == 0); + nka_layer_begin(0, 1); + nka_layer_add(0, 1, 1); + nka_layer_end(0, 1); + CHECK(!nka_get_blended_float(0, 1, 1, &f) && !nka_get_blended_int(0, 1, 1, &i)); + CHECK(nka_clip_save(0, 1, buf, 8) == 0 && nka_clip_load(0, buf, 8, 0) == NKA_ERR_BAD_ARG); + nka_clip_reserve(0, 1, 1); +} + +/* Memory */ + +static int live, allocs, fail_at = -1; + +static void *count_alloc(nk_handle h, void *old, nk_size n) +{ + (void)h; (void)old; + if (allocs++ == fail_at) return 0; + live++; + return malloc(n); +} + +static void count_free(nk_handle h, void *p) +{ + (void)h; + live--; + free(p); +} + +static void scenario(struct nka_context *a) +{ + static unsigned char buf[16384]; + nk_hash clip = rich(a), out; + float f; + int i, size; + nka_clip_begin(a, H("plain")); + key2(a, "f", 0, 0, 1, 1); + nka_clip_end(a); + for (i = 0; i < 40; ++i) nka_play(a, i & 1 ? clip : H("plain"), nka_id_mix(H("o"), (nk_hash)i)); + nka_instance_then(a, nka_id_mix(H("o"), 0), clip, 0); + nka_layer_begin(a, H("l")); + for (i = 0; i < 40; ++i) nka_layer_add(a, nka_id_mix(H("o"), (nk_hash)i), 1); + nka_layer_end(a, H("l")); + nka_get_blended_float(a, H("l"), H("f"), &f); + size = nka_clip_save(a, clip, buf, (int)sizeof buf); + nka_clip_load(a, buf, size, &out); + nka_clip_begin(a, H("plain")); + key2(a, "g", 0, 0, 1, 1); + nka_clip_end(a); + steps(a, 40, 0.05f); + for (i = 0; i < 40; ++i) getf(a, nka_id_mix(H("o"), (nk_hash)i), "f"); + steps(a, 10, 0.05f); + nka_gc(a, 5); + CHECK(a->clip_insts.len < 41 && a->clip_layers.len == 0); + nka_clear(a); + nka_play(a, clip, H("after")); + steps(a, 3, 0.05f); +} + +static void test_alloc(void) +{ + struct nk_allocator al; + struct nka_context *a; + int n, total, ok = 1; + al.userdata.ptr = 0; + al.alloc = count_alloc; + al.free = count_free; + live = allocs = 0; + fail_at = -1; + a = nka_create(&al); + scenario(a); + CHECK(live > 1); + nka_destroy(a); + CHECK(live == 0); + total = allocs; + /* Every allocation failing in turn: nothing leaks and nothing breaks. */ + for (n = 0; n < total; ++n) { + live = allocs = 0; + fail_at = n; + a = nka_create(&al); + if (a) { + scenario(a); + nka_destroy(a); + } + ok = ok && live == 0; + } + CHECK(ok && total > 50); + fail_at = -1; +} + +int main(void) +{ + test_keys(); + test_groups(); + test_delay(); + test_loops(); + test_markers(); + test_callbacks(); + test_chain(); + test_control(); + test_time_scale(); + test_layers(); + test_variations(); + test_stagger(); + test_spring(); + test_relative(); + test_persist(); + test_gc(); + test_busy(); + test_misc(); + test_alloc(); + if (failures) printf("%d failed\n", failures); + else printf("clip: ok\n"); + return failures != 0; +} diff --git a/tests/core.c b/tests/core.c new file mode 100644 index 0000000..94bdb22 --- /dev/null +++ b/tests/core.c @@ -0,0 +1,462 @@ +#include + +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#include "nuklear.h" +#define NUKANIM_IMPLEMENTATION +#include "nukanim.h" + +static int failures; + +static void check(int ok, const char *what, int line) +{ + if (ok) return; + printf("core.c:%d: %s\n", line, what); + failures++; +} + +#define CHECK(c) check((c) != 0, #c, __LINE__) +#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps)) + +static const struct nka_ease linear = { NKA_EASE_LINEAR, 0, 0, 0, 0 }; + +/* ImAnimate's curves, as ImAnimate.cpp writes them, in its ECurve order. */ +static float imanimate(int curve, float p) +{ + const float pi = 3.14159f; + switch (curve) { + case 0: return p; + case 1: return 1 - cosf((p * pi) / 2); + case 2: return sinf((p * pi) / 2); + case 3: return -(cosf(pi * p) - 1) / 2; + case 4: return p * p; + case 5: return 1 - (1 - p) * (1 - p); + case 6: return p < 0.5f ? 2 * p * p : 1 - powf(-2 * p + 2, 2) / 2.0f; + case 7: return p * p * p; + case 8: return 1 - powf(1 - p, 3); + case 9: return p < 0.5f ? 4 * p * p * p : 1 - powf(-2 * p + 2, 3) / 2.0f; + case 10: return p * p * p * p; + case 11: return 1 - powf(1 - p, 4); + case 12: return p < 0.5f ? 8 * p * p * p * p : 1 - powf(-2 * p + 2, 4) / 2.0f; + case 13: return p * p * p * p * p; + case 14: return 1 - powf(1 - p, 5); + case 15: return p < 0.5f ? 16 * p * p * p * p * p : 1 - powf(-2 * p + 2, 5) / 2.0f; + case 16: return p == 0 ? 0 : powf(2, 10 * p - 10); + case 17: return p == 1 ? 1 : 1 - powf(2, -10 * p); + case 18: return p == 0 ? 0 : p == 1 ? 1 : p < 0.5f ? powf(2, 20 * p - 10) / 2.0f : (2 - powf(2, -20 * p + 10)) / 2.0f; + case 19: return 1 - sqrtf(1 - powf(p, 2)); + case 20: return sqrtf(1 - powf(p - 1, 2)); + default: return p < 0.5f ? (1 - sqrtf(1 - powf(2 * p, 2))) / 2.0f : (sqrtf(1 - powf(-2 * p + 2, 2)) + 1) / 2.0f; + } +} + +static const int imanimate_curves[22] = { + NKA_EASE_LINEAR, + NKA_EASE_IN_SINE, NKA_EASE_OUT_SINE, NKA_EASE_IN_OUT_SINE, + NKA_EASE_IN_QUAD, NKA_EASE_OUT_QUAD, NKA_EASE_IN_OUT_QUAD, + NKA_EASE_IN_CUBIC, NKA_EASE_OUT_CUBIC, NKA_EASE_IN_OUT_CUBIC, + NKA_EASE_IN_QUART, NKA_EASE_OUT_QUART, NKA_EASE_IN_OUT_QUART, + NKA_EASE_IN_QUINT, NKA_EASE_OUT_QUINT, NKA_EASE_IN_OUT_QUINT, + NKA_EASE_IN_EXPO, NKA_EASE_OUT_EXPO, NKA_EASE_IN_OUT_EXPO, + NKA_EASE_IN_CIRC, NKA_EASE_OUT_CIRC, NKA_EASE_IN_OUT_CIRC +}; + +static float square(float t) { return t * t; } + +static void test_easing(void) +{ + struct nka_context *a = nka_create(0); + int type, i, k; + for (type = NKA_EASE_LINEAR; type <= NKA_EASE_IN_OUT_BOUNCE; ++type) { + CHECK(NEAR(nka_eval_preset(type, 0), 0, 1e-6f)); + CHECK(NEAR(nka_eval_preset(type, 1), 1, 1e-5f)); + } + for (type = NKA_EASE_IN_QUAD; type <= NKA_EASE_IN_BOUNCE; type += 3) { + CHECK(NEAR(nka_eval_preset(type + 2, 0.5f), 0.5f, 1e-5f)); + for (k = 0; k <= 20; ++k) { + float t = (float)k / 20; + CHECK(NEAR(nka_eval_preset(type + 1, t), 1 - nka_eval_preset(type, 1 - t), 1e-5f)); + } + } + for (i = 0; i < 22; ++i) + for (k = 0; k <= 64; ++k) { + float t = (float)k / 64; + CHECK(NEAR(nka_eval_preset(imanimate_curves[i], t), imanimate(i, t), 1e-5f)); + } + + for (k = 0; k <= 20; ++k) { + float t = (float)k / 20; + struct nka_ease in_back = nka_ease(NKA_EASE_IN_BACK), in_el = nka_ease(NKA_EASE_IN_ELASTIC); + in_back.p0 = 1.70158f; + in_el.p0 = 1; + in_el.p1 = 0.3f; + CHECK(NEAR(nka_eval(a, nka_ease_back(1.70158f), t), nka_eval_preset(NKA_EASE_OUT_BACK, t), 1e-5f)); + CHECK(NEAR(nka_eval(a, in_back, t), nka_eval_preset(NKA_EASE_IN_BACK, t), 1e-5f)); + CHECK(NEAR(nka_eval(a, nka_ease_elastic(1, 0.3f), t), nka_eval_preset(NKA_EASE_OUT_ELASTIC, t), 1e-5f)); + CHECK(NEAR(nka_eval(a, in_el, t), nka_eval_preset(NKA_EASE_IN_ELASTIC, t), 1e-5f)); + CHECK(nka_eval(a, nka_ease_elastic(0.5f, 0.3f), t) == nka_eval(a, nka_ease_elastic(0.5f, 0.3f), t)); + CHECK(NEAR(nka_eval(a, nka_ease_bezier(0, 0, 1, 1), t), t, 1e-4f)); + } + CHECK(NEAR(nka_eval(a, nka_ease_back(3), 0.8f), 1 - nka__back(0.2f, 3), 1e-6f)); + CHECK(nka_eval(a, nka_ease_back(3), 0.8f) > 1); + + CHECK(nka_eval(a, nka_ease_steps(4, 0), 0) == 0); + CHECK(nka_eval(a, nka_ease_steps(4, 0), 0.3f) == 0.25f); + CHECK(nka_eval(a, nka_ease_steps(4, 0), 0.99f) == 0.75f); + CHECK(nka_eval(a, nka_ease_steps(4, 0), 1) == 1); + CHECK(nka_eval(a, nka_ease_steps(4, 1), 0) == 0.25f); + CHECK(nka_eval(a, nka_ease_steps(4, 1), 0.3f) == 0.5f); + CHECK(nka_eval(a, nka_ease_steps(4, 1), 1) == 1); + CHECK(NEAR(nka_eval(a, nka_ease_steps(4, 2), 0), 0.2f, 1e-6f)); + CHECK(NEAR(nka_eval(a, nka_ease_steps(4, 2), 0.3f), 0.4f, 1e-6f)); + CHECK(NEAR(nka_eval(a, nka_ease_steps(4, 2), 0.99f), 0.8f, 1e-6f)); + CHECK(nka_eval(a, nka_ease_steps(4, 2), 1) == 1); + + /* CSS ease at the middle */ + CHECK(NEAR(nka_eval(a, nka_ease_bezier(0.25f, 0.1f, 0.25f, 1), 0.5f), 0.8024f, 1e-3f)); + + CHECK(NEAR(nka_eval(a, nka_ease_spring(0, 0, 0, 0), 0), 0, 1e-6f)); + CHECK(NEAR(nka_eval(a, nka_ease_spring(0, 0, 0, 0), 1), 1, 1e-3f)); + CHECK(nka_eval(a, nka_ease_spring(1, 120, 20, 8), 0.01f) > nka_eval(a, nka_ease_spring(1, 120, 20, 0), 0.01f)); + { + float prev = 0; + for (k = 1; k <= 50; ++k) { + float v = nka_eval(a, nka_ease_spring(1, 120, 100, 0), (float)k / 50); + CHECK(v > prev && v < 1); + prev = v; + } + CHECK(NEAR(nka_eval(a, nka_ease_spring(1, 100, 20, 0), 0.5f), 1 - expf(-5) * (1 + 5), 1e-5f)); + } + + CHECK(nka_eval(a, nka_ease_custom(3), 0.5f) == 0.5f); + nka_register_ease(a, 3, square); + CHECK(nka_custom_ease(a, 3) == square); + CHECK(nka_eval(a, nka_ease_custom(3), 0.5f) == 0.25f); + nka_register_ease(a, 16, square); + CHECK(nka_custom_ease(a, 16) == 0); + nka_destroy(a); +} + +static void test_tweens(void) +{ + struct nka_context *a = nka_create(0); + nk_hash id = nka_id("box"), x = nka_id("x"); + float v; + + nka_update(a, 0.016f); + CHECK(nka_tween_float(a, id, x, 0, 1, linear, NKA_POLICY_CROSSFADE, 0) == 0); + CHECK(a->chans.len == 0); + CHECK(nka_tween_float(a, id, x, 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 0); + CHECK(a->chans.len == 1 && nka_busy(a)); + nka_update(a, 0.25f); + CHECK(NEAR(nka_tween_float(a, id, x, 10, 1, linear, NKA_POLICY_CROSSFADE, 0), 2.5f, 1e-4f)); + nka_update(a, 0.5f); + CHECK(NEAR(nka_tween_float(a, id, x, 10, 1, linear, NKA_POLICY_CROSSFADE, 0), 7.5f, 1e-4f)); + CHECK(NEAR(nka_tween_float(a, id, x, 0, 1, linear, NKA_POLICY_CROSSFADE, 0), 7.5f, 1e-4f)); + nka_update(a, 0.5f); + CHECK(NEAR(nka_tween_float(a, id, x, 0, 1, linear, NKA_POLICY_CROSSFADE, 0), 3.75f, 1e-4f)); + CHECK(nka_busy(a)); + nka_update(a, 0.6f); + CHECK(nka_tween_float(a, id, x, 0, 1, linear, NKA_POLICY_CROSSFADE, 0) == 0); + CHECK(!nka_busy(a)); + + nka_set_lazy_init(a, 0); + CHECK(nka_tween_float(a, id, nka_id("eager"), 3, 1, linear, NKA_POLICY_CROSSFADE, 3) == 3); + CHECK(a->chans.len == 2 && !nka_busy(a)); + nka_set_lazy_init(a, 1); + + CHECK(nka_tween_float(a, id, nka_id("cut"), 5, 1, linear, NKA_POLICY_CUT, 0) == 5); + CHECK(!nka_busy(a)); + + v = nka_tween_float(a, id, nka_id("q"), 10, 1, linear, NKA_POLICY_QUEUE, 0); + CHECK(v == 0); + nka_update(a, 0.5f); + CHECK(NEAR(nka_tween_float(a, id, nka_id("q"), 20, 1, linear, NKA_POLICY_QUEUE, 0), 5, 1e-4f)); + nka_update(a, 0.25f); + CHECK(NEAR(nka_tween_float(a, id, nka_id("q"), 20, 1, linear, NKA_POLICY_QUEUE, 0), 7.5f, 1e-4f)); + nka_update(a, 0.25f); + CHECK(NEAR(nka_tween_float(a, id, nka_id("q"), 20, 1, linear, NKA_POLICY_QUEUE, 0), 10, 1e-4f)); + nka_update(a, 0.5f); + CHECK(NEAR(nka_tween_float(a, id, nka_id("q"), 20, 1, linear, NKA_POLICY_QUEUE, 0), 15, 1e-4f)); + + CHECK(nka_tween_float(a, id, nka_id("add"), 5, 1, linear, NKA_POLICY_ADDITIVE, 0) == 0); + nka_update(a, 1); + CHECK(nka_tween_float(a, id, nka_id("add"), 5, 1, linear, NKA_POLICY_ADDITIVE, 0) == 5); + nka_update(a, 1); + CHECK(nka_tween_float(a, id, nka_id("add"), 5, 1, linear, NKA_POLICY_ADDITIVE, 0) == 5); + CHECK(nka_tween_float(a, id, nka_id("add"), 3, 1, linear, NKA_POLICY_ADDITIVE, 0) == 5); + nka_update(a, 1); + CHECK(nka_tween_float(a, id, nka_id("add"), 3, 1, linear, NKA_POLICY_ADDITIVE, 0) == 8); + + CHECK(nka_tween_float(a, id, nka_id("mul"), 3, 1, linear, NKA_POLICY_MULTIPLY, 2) == 2); + nka_update(a, 0.5f); + CHECK(NEAR(nka_tween_float(a, id, nka_id("mul"), 3, 1, linear, NKA_POLICY_MULTIPLY, 2), 4, 1e-4f)); + nka_update(a, 0.5f); + CHECK(nka_tween_float(a, id, nka_id("mul"), 3, 1, linear, NKA_POLICY_MULTIPLY, 2) == 6); + + nka_tween_float(a, id, nka_id("re"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0); + nka_update(a, 0.5f); + CHECK(NEAR(nka_tween_float(a, id, nka_id("re"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0), 5, 1e-4f)); + nka_rebase_float(a, id, nka_id("re"), 20); + nka_update(a, 0.25f); + CHECK(NEAR(nka_tween_float(a, id, nka_id("re"), 20, 1, linear, NKA_POLICY_CROSSFADE, 0), 12.5f, 1e-4f)); + nka_update(a, 0.25f); + CHECK(nka_tween_float(a, id, nka_id("re"), 20, 1, linear, NKA_POLICY_CROSSFADE, 0) == 20); + nka_rebase_float(a, id, nka_id("none"), 1); + + CHECK(nka_tween_int(a, id, nka_id("i"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 0); + nka_update(a, 0.25f); + CHECK(nka_tween_int(a, id, nka_id("i"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 3); + nka_update(a, 0.04f); + CHECK(nka_tween_int(a, id, nka_id("i"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 3); + nka_update(a, 1); + CHECK(nka_tween_int(a, id, nka_id("i"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 10); + nka_rebase_int(a, id, nka_id("i"), -4); + CHECK(nka_tween_int(a, id, nka_id("i"), -4, 1, linear, NKA_POLICY_CROSSFADE, 0) == -4); + + { + struct nk_vec2 init = { 0, 0 }, to = { 100, 50 }, r; + struct nka_vec4 z4 = { 0, 0, 0, 0 }, t4 = { 1, 2, 3, 4 }, r4; + struct nka_ease_axes axes; + axes.x = linear; + axes.y = nka_ease(NKA_EASE_IN_QUAD); + axes.z = linear; + axes.w = linear; + r = nka_tween_vec2(a, id, nka_id("v2"), to, 1, linear, NKA_POLICY_CROSSFADE, init); + CHECK(r.x == 0 && r.y == 0); + nka_tween_vec2_per_axis(a, id, nka_id("pa"), to, 1, axes, NKA_POLICY_CROSSFADE); + r4 = nka_tween_vec4(a, id, nka_id("v4"), t4, 1, linear, NKA_POLICY_CROSSFADE, z4); + CHECK(r4.w == 0); + nka_update(a, 0.5f); + r = nka_tween_vec2(a, id, nka_id("v2"), to, 1, linear, NKA_POLICY_CROSSFADE, init); + CHECK(NEAR(r.x, 50, 1e-3f) && NEAR(r.y, 25, 1e-3f)); + r = nka_tween_vec2_per_axis(a, id, nka_id("pa"), to, 1, axes, NKA_POLICY_CROSSFADE); + CHECK(NEAR(r.x, 50, 1e-3f) && NEAR(r.y, 12.5f, 1e-3f)); + r4 = nka_tween_vec4(a, id, nka_id("v4"), t4, 1, linear, NKA_POLICY_CROSSFADE, z4); + CHECK(NEAR(r4.x, 0.5f, 1e-4f) && NEAR(r4.w, 2, 1e-4f)); + nka_rebase_vec2(a, id, nka_id("v2"), init); + nka_rebase_vec4(a, id, nka_id("v4"), z4); + nka_update(a, 0.5f); + r = nka_tween_vec2(a, id, nka_id("v2"), init, 1, linear, NKA_POLICY_CROSSFADE, init); + CHECK(r.x == 0 && r.y == 0); + r4 = nka_tween_vec4(a, id, nka_id("v4"), z4, 1, linear, NKA_POLICY_CROSSFADE, z4); + CHECK(r4.x == 0 && r4.w == 0); + } + { + struct nk_vec2 half = { 0.5f, 0.5f }, bias = { 10, 0 }, size = { 200, 100 }, r; + nka_set_anchor(a, NKA_ANCHOR_WINDOW, size); + CHECK(nka_anchor(a, NKA_ANCHOR_WINDOW).x == 200 && nka_anchor(a, 9).x == 0); + r = nka_tween_vec2_rel(a, id, nka_id("rel"), half, bias, 1, linear, NKA_POLICY_CUT, NKA_ANCHOR_WINDOW); + CHECK(r.x == 110 && r.y == 50); + CHECK(nka_tween_float_rel(a, id, nka_id("relf"), 0.25f, 1, 1, linear, NKA_POLICY_CUT, NKA_ANCHOR_WINDOW, 1) == 26); + } + nka_destroy(a); +} + +static float forty_two(void *user) { (void)user; return 42; } + +static void test_time(void) +{ + struct nka_context *a = nka_create(0); + nk_hash id = nka_id("t"); + nka_tween_float(a, id, 0, 1, 1, linear, NKA_POLICY_CROSSFADE, 0); + nka_set_time_scale(a, 0.5f); + CHECK(nka_time_scale(a) == 0.5f); + nka_update(a, 1); + CHECK(nka_time(a) == 0.5); + CHECK(NEAR(nka_tween_float(a, id, 0, 1, 1, linear, NKA_POLICY_CROSSFADE, 0), 0.5f, 1e-5f)); + nka_set_time_scale(a, -1); + CHECK(nka_time_scale(a) == 0); + nka_update(a, 1); + CHECK(nka_time(a) == 0.5); + CHECK(nka_tween_float_resolved(a, id, 1, forty_two, 0, 1, linear, NKA_POLICY_CUT) == 42); + nka_destroy(a); +} + +static void test_colors(void) +{ + struct nka_context *a = nka_create(0); + struct nk_colorf black = { 0, 0, 0, 1 }, white = { 1, 1, 1, 1 }, red = { 1, 0, 0, 1 }, pink = { 1, 0, 0.6f, 1 }, c; + int space, r, g, b; + for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) + for (r = 0; r <= 4; ++r) + for (g = 0; g <= 4; ++g) + for (b = 0; b <= 4; ++b) { + struct nka_vec4 in = { (float)r / 4, (float)g / 4, (float)b / 4, 0.5f }; + struct nka_vec4 out = nka__from_space(nka__to_space(in, space), space); + CHECK(NEAR(in.x, out.x, 1e-4f) && NEAR(in.y, out.y, 1e-4f) && NEAR(in.z, out.z, 1e-4f) && out.w == 0.5f); + } + c = nka_color_blend(red, pink, 0.5f, NKA_COL_HSV); + CHECK(NEAR(c.r, 1, 1e-4f) && NEAR(c.g, 0, 1e-4f) && NEAR(c.b, 0.3f, 1e-4f)); + for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) { + c = nka_color_blend(red, pink, 0, space); + CHECK(NEAR(c.r, 1, 1e-4f) && NEAR(c.b, 0, 1e-4f)); + c = nka_color_blend(red, pink, 1, space); + CHECK(NEAR(c.r, 1, 1e-4f) && NEAR(c.b, 0.6f, 1e-4f)); + } + c = nka_color_blend(black, white, 0.5f, NKA_COL_SRGB_LINEAR); + CHECK(NEAR(c.r, 0.7354f, 1e-3f)); + + c = nka_tween_color(a, 1, 2, white, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, black); + CHECK(c.r == 0 && c.a == 1); + nka_update(a, 0.5f); + c = nka_tween_color(a, 1, 2, white, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, black); + CHECK(c.r > 0.3f && c.r < 0.6f && NEAR(c.r, c.g, 1e-4f)); + nka_update(a, 0.5f); + c = nka_tween_color(a, 1, 2, white, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, black); + CHECK(c.r == 1 && c.g == 1 && c.b == 1); + nka_rebase_color(a, 1, 2, red); + nka_update(a, 0.1f); + c = nka_tween_color(a, 1, 2, red, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, black); + CHECK(c.r == 1 && c.g == 0); + { + struct nka_ease_axes axes; + axes.x = axes.y = axes.z = axes.w = linear; + c = nka_tween_color_per_axis(a, 1, 3, red, 0, axes, NKA_POLICY_CUT, NKA_COL_OKLCH); + CHECK(NEAR(c.r, 1, 1e-4f) && NEAR(c.g, 0, 1e-4f)); + } + nka_destroy(a); +} + +static void test_animate(void) +{ + struct nka_context *a = nka_create(0); + float size = 1, other = 0.5f; + nka_update(a, 0); + nka_animate(a, 1, 1.1f, 500, &size, NKA_EASE_IN_CUBIC); + CHECK(size == 1 && nka_busy(a)); + nka_update(a, 0.25f); + nka_animate(a, 1, 1.1f, 500, &size, NKA_EASE_IN_CUBIC); + CHECK(NEAR(size, 1.0125f, 1e-5f)); + nka_animate(a, 1.1f, 1, 500, &size, NKA_EASE_OUT_CUBIC); + CHECK(NEAR(size, 1.0125f, 1e-5f)); + nka_update(a, 0.125f); + nka_animate(a, 1.1f, 1, 500, &size, NKA_EASE_OUT_CUBIC); + CHECK(NEAR(size, 1.1f - 0.1f * (1 - 0.25f * 0.25f * 0.25f), 1e-5f)); + nka_update(a, 0.125f); + nka_animate(a, 1.1f, 1, 500, &size, NKA_EASE_OUT_CUBIC); + CHECK(size == 1); + nka_update(a, 0.1f); + nka_animate(a, 1.1f, 1, 500, &size, NKA_EASE_OUT_CUBIC); + CHECK(size == 1 && !nka_busy(a)); + + nka_animate(a, 0, 1, 1000, &other, NKA_EASE_LINEAR); + CHECK(NEAR(other, 0.5f, 1e-6f)); + nka_update(a, 0.25f); + nka_animate(a, 0, 1, 1000, &other, NKA_EASE_LINEAR); + CHECK(NEAR(other, 0.75f, 1e-5f)); + other = 3; + nka_animate(a, 0, 1, 1000, &other, NKA_EASE_LINEAR); + CHECK(other == 1); + other = 0.2f; + nka_animate(a, 0, 1, 0, &other, NKA_EASE_LINEAR); + CHECK(other == 1); + nka_destroy(a); +} + +static void test_gc_and_map(void) +{ + struct nka_context *a = nka_create(0); + int i, frame; + for (i = 0; i < 1000; ++i) nka_tween_float(a, (nk_hash)i, 0, 1, 1, linear, NKA_POLICY_CUT, 0); + CHECK(a->chans.len == 1000); + for (frame = 0; frame < 10; ++frame) { + nka_update(a, 0.016f); + for (i = 0; i < 1000; i += 2) nka_tween_float(a, (nk_hash)i, 0, 1, 1, linear, NKA_POLICY_CUT, 0); + } + nka_gc(a, 5); + CHECK(a->chans.len == 500); + for (i = 0; i < 1000; ++i) { + struct nka__chan *c = (struct nka__chan *)nka__get(&a->chans, nka__key((nk_hash)i, 0, NKA__FLOAT)); + CHECK((c != 0) == !(i & 1)); + if (c) CHECK(c->cur[0] == 1 && c->id == (nk_hash)i); + } + nka_clear(a); + CHECK(a->chans.len == 0 && a->chans.cap == 0); + nka_reserve(a, 100); + CHECK(a->chans.cap == 256); + + { + static unsigned char in[40000]; + struct nka__map m = { 0, 0, 0, 0, sizeof(int) }; + nk_hash s = 7; + for (i = 0; i < 40000; ++i) { + int *v = (int *)nka__put(a, &m, (nk_hash)i * 2654435761u, 0); + *v = i; + in[i] = 1; + } + for (i = 0; i < 60000; ++i) { + int k; + s = s * 1103515245u + 12345u; + k = (int)((s >> 8) % 40000); + nka__del(&m, (nk_hash)k * 2654435761u); + in[k] = 0; + } + frame = 0; + for (i = 0; i < 40000; ++i) { + int *v = (int *)nka__get(&m, (nk_hash)i * 2654435761u); + frame += in[i]; + CHECK((v != 0) == (in[i] != 0)); + if (v) CHECK(*v == i); + } + CHECK(m.len == frame); + nka__map_free(a, &m); + *(int *)nka__put(a, &m, 0, 0) = 10; + *(int *)nka__put(a, &m, 1, 0) = 11; + CHECK(m.len == 2 && *(int *)nka__get(&m, 0) == 10 && *(int *)nka__get(&m, 1) == 11); + nka__del(&m, 0); + CHECK(!nka__get(&m, 0) && *(int *)nka__get(&m, 1) == 11); + nka__map_free(a, &m); + } + CHECK(nka_id("") == 2166136261u && nka_id("a") == 0xe40c292cu && nka_id(0) == 2166136261u); + nka_destroy(a); +} + +static int live; + +static void *count_alloc(nk_handle h, void *old, nk_size n) +{ + (void)h; (void)old; + live++; + return malloc(n); +} + +static void count_free(nk_handle h, void *p) +{ + (void)h; + live--; + free(p); +} + +static void test_allocator(void) +{ + struct nk_allocator al; + struct nka_context *a; + float v = 0; + int i; + al.userdata.ptr = 0; + al.alloc = count_alloc; + al.free = count_free; + a = nka_create(&al); + for (i = 0; i < 300; ++i) nka_tween_float(a, (nk_hash)i, 1, 1, 1, linear, NKA_POLICY_CROSSFADE, 0); + nka_animate(a, 0, 1, 100, &v, NKA_EASE_LINEAR); + CHECK(live > 1); + nka_destroy(a); + CHECK(live == 0); + CHECK(nka_tween_float(0, 1, 1, 3, 1, linear, NKA_POLICY_CROSSFADE, 0) == 3); + nka_update(0, 1); + nka_destroy(0); +} + +int main(void) +{ + test_easing(); + test_tweens(); + test_time(); + test_colors(); + test_animate(); + test_gc_and_map(); + test_allocator(); + if (failures) printf("%d failed\n", failures); + else printf("core: ok\n"); + return failures != 0; +} diff --git a/tests/fx.c b/tests/fx.c new file mode 100644 index 0000000..afcb3c0 --- /dev/null +++ b/tests/fx.c @@ -0,0 +1,951 @@ +#include + +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#include "nuklear.h" +#define NUKANIM_IMPLEMENTATION +#include "nukanim.h" + +static int failures; + +static void check(int ok, const char *what, int line) +{ + if (ok) return; + printf("fx.c:%d: %s\n", line, what); + failures++; +} + +#define CHECK(c) check((c) != 0, #c, __LINE__) +#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps)) + +static const struct nka_ease linear = { NKA_EASE_LINEAR, 0, 0, 0, 0 }; +static const float tau = 6.2831853f; + +static struct nk_colorf rgba(float r, float g, float b, float a) +{ + struct nk_colorf c; + c.r = r; c.g = g; c.b = b; c.a = a; + return c; +} + +static struct nk_colorf average(struct nk_colorf x, struct nk_colorf y) +{ + return rgba((x.r + y.r) / 2, (x.g + y.g) / 2, (x.b + y.b) / 2, (x.a + y.a) / 2); +} + +static int same_color(struct nk_colorf x, struct nk_colorf y, float eps) +{ + return NEAR(x.r, y.r, eps) && NEAR(x.g, y.g, eps) && NEAR(x.b, y.b, eps) && NEAR(x.a, y.a, eps); +} + +static int unit_color(struct nk_colorf c) +{ + return c.r >= 0 && c.r <= 1 && c.g >= 0 && c.g <= 1 && c.b >= 0 && c.b <= 1 && c.a >= 0 && c.a <= 1; +} + +static int same_vec2(struct nk_vec2 x, struct nk_vec2 y, float eps) { return NEAR(x.x, y.x, eps) && NEAR(x.y, y.y, eps); } + +static int same_gradient(const struct nka_gradient *x, const struct nka_gradient *y) +{ + int i; + if (x->count != y->count) return 0; + for (i = 0; i < x->count; ++i) + if (x->positions[i] != y->positions[i] || !same_color(x->colors[i], y->colors[i], 0)) return 0; + return 1; +} + +static struct nka_transform xf(float x, float y, float rotation, float sx, float sy) +{ + return nka_transform(nka__vec2(x, y), rotation, nka__vec2(sx, sy)); +} + +static int same_xf(struct nka_transform x, struct nka_transform y) +{ + return x.position.x == y.position.x && x.position.y == y.position.y && x.rotation == y.rotation + && x.scale.x == y.scale.x && x.scale.y == y.scale.y; +} + +static void test_waves(void) +{ + static const float phase[5] = { 0, 0.25f, 0.5f, 0.75f, 0.125f }; + static const float shape[4][5] = { + { 0, 1, 0, -1, 0.70710678f }, + { -1, 0, 1, 0, -0.5f }, + { -1, -0.5f, 0, 0.5f, -0.75f }, + { 1, 1, -1, -1, 1 } + }; + struct nka_context *a = nka_create(0); + struct nk_vec2 amp2 = { 2, 3 }, f2 = { 1, 2 }, p2 = { 0.25f, 0.125f }, v2; + struct nka_vec4 amp4 = { 1, 2, 3, 4 }, f4 = { 1, 1, 1, 1 }, p4 = { 0, 0.25f, 0.5f, 0.75f }, v4; + int wave, k; + + for (wave = NKA_WAVE_SINE; wave <= NKA_WAVE_SQUARE; ++wave) + for (k = 0; k < 5; ++k) { + CHECK(NEAR(nka_oscillate(a, 1, 3, 1, wave, phase[k]), 3 * shape[wave][k], 1e-5f)); + CHECK(NEAR(nka_oscillate(a, 1, 3, 1, wave, phase[k] + 2), 3 * shape[wave][k], 1e-5f)); + CHECK(NEAR(nka_oscillate(a, 1, 3, 1, wave, phase[k] - 1), 3 * shape[wave][k], 1e-5f)); + } + CHECK(nka_busy(a)); + v2 = nka_oscillate_vec2(a, 2, amp2, f2, NKA_WAVE_SINE, p2); + CHECK(NEAR(v2.x, 2, 1e-5f) && NEAR(v2.y, 3 * 0.70710678f, 1e-5f)); + v4 = nka_oscillate_vec4(a, 3, amp4, f4, NKA_WAVE_TRIANGLE, p4); + CHECK(NEAR(v4.x, -1, 1e-5f) && NEAR(v4.y, 0, 1e-5f) && NEAR(v4.z, 3, 1e-5f) && NEAR(v4.w, 0, 1e-5f)); + CHECK(nka_oscillate_int(a, 4, 5, 1, NKA_WAVE_SAWTOOTH, 0.75f) == 3); + CHECK(nka_oscillate_int(a, 4, 5, 1, NKA_WAVE_SAWTOOTH, 0.25f) == -3); + CHECK(nka_oscillate_int(a, 4, 5, 1, NKA_WAVE_SAWTOOTH, 0.5f) == 0); + CHECK(nka_oscillate_int(a, 4, 5, 1, NKA_WAVE_SINE, 0.25f) == 5); + nka_destroy(a); + + for (wave = NKA_WAVE_SINE; wave <= NKA_WAVE_SQUARE; ++wave) { + a = nka_create(0); + for (k = 1; k <= 8; ++k) { + nka_update(a, 0.125f); + CHECK(NEAR(nka_oscillate(a, 7, 2, 2, wave, 0), 2 * shape[wave][k % 4], 1e-5f)); + } + nka_destroy(a); + } + + a = nka_create(0); + nka_update(a, 0.1f); + v2 = nka_oscillate_vec2(a, 2, amp2, f2, NKA_WAVE_SINE, p2); + CHECK(NEAR(v2.x, 2 * sinf(tau * (0.1f + 0.25f)), 1e-5f) && NEAR(v2.y, 3 * sinf(tau * (0.2f + 0.125f)), 1e-5f)); + nka_destroy(a); + + for (wave = NKA_WAVE_SINE; wave <= NKA_WAVE_SQUARE; ++wave) { + struct nk_vec2 f2b = { 1.3f, 0.7f }, p2b = { 0.1f, 0.6f }; + struct nka_vec4 f4b = { 0.9f, 1.7f, 2.3f, 0.4f }, p4b = { 0, 0.3f, 0.5f, 0.9f }; + float lo = 0, hi = 0; + int out = 0; + a = nka_create(0); + for (k = 0; k < 400; ++k) { + float v; + int iv; + nka_update(a, 0.0137f); + v = nka_oscillate(a, 1, 4, 1.1f, wave, 0); + iv = nka_oscillate_int(a, 2, 7, 0.9f, wave, 0.2f); + v2 = nka_oscillate_vec2(a, 3, amp2, f2b, wave, p2b); + v4 = nka_oscillate_vec4(a, 4, amp4, f4b, wave, p4b); + out += fabsf(v) > 4 + 1e-5f || iv < -7 || iv > 7 || !nka_busy(a); + out += fabsf(v2.x) > 2 + 1e-5f || fabsf(v2.y) > 3 + 1e-5f; + out += fabsf(v4.x) > 1 + 1e-5f || fabsf(v4.y) > 2 + 1e-5f || fabsf(v4.z) > 3 + 1e-5f || fabsf(v4.w) > 4 + 1e-5f; + if (v < lo) lo = v; + if (v > hi) hi = v; + } + CHECK(out == 0 && lo < -3.8f && hi > 3.8f); + nka_destroy(a); + } + + { + struct nk_colorf gray = rgba(0.5f, 0.5f, 0.5f, 1), red = rgba(1, 0, 0, 1), sky = rgba(0.2f, 0.5f, 1, 1); + struct nk_colorf beige = rgba(0.6f, 0.5f, 0.4f, 0.8f), c; + struct nka_vec4 d = { 0.25f, 0.25f, 0.25f, 0 }, hue = { 0.5f, 0, 0, 0 }, dim = { 0, 0, -0.5f, 0 }; + struct nka_vec4 turn = { 0, 0, 0.5f, 0 }, big = { 2, 2, 2, 2 }, lch, rot; + int space, out = 0; + a = nka_create(0); + c = nka_oscillate_color(a, 5, gray, d, 1, NKA_WAVE_SINE, 0.25f, NKA_COL_SRGB); + CHECK(same_color(c, rgba(0.75f, 0.75f, 0.75f, 1), 1e-5f)); + c = nka_oscillate_color(a, 5, red, hue, 1, NKA_WAVE_SINE, 0.25f, NKA_COL_HSV); + CHECK(same_color(c, rgba(0, 1, 1, 1), 1e-4f)); + c = nka_oscillate_color(a, 5, sky, dim, 1, NKA_WAVE_SINE, 0.25f, NKA_COL_HSV); + CHECK(same_color(c, rgba(0.1f, 0.25f, 0.5f, 1), 1e-4f)); + c = nka_oscillate_color(a, 5, beige, turn, 1, NKA_WAVE_SINE, 0.25f, NKA_COL_OKLCH); + lch = nka__to_space(nka__cv(beige), NKA_COL_OKLCH); + rot = nka__to_space(nka__cv(c), NKA_COL_OKLCH); + CHECK(NEAR(rot.x, lch.x, 1e-3f) && NEAR(rot.y, lch.y, 1e-3f) && NEAR(fmodf(rot.z - lch.z + 1, 1), 0.5f, 1e-3f)); + CHECK(c.a == 0.8f); + for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) + for (k = 0; k < 100; ++k) { + nka_update(a, 0.05f); + out += !unit_color(nka_oscillate_color(a, 6, sky, big, 1.3f, k & 3, 0, space)); + } + CHECK(out == 0); + nka_destroy(a); + } +} + +static void test_frames(void) +{ + struct nka_context *a = nka_create(0); + struct nk_vec2 one = { 1, 1 }, half = { 0.5f, 0.5f }, zero = { 0, 0 }; + struct nka__fx_clock *c; + float v; + int k; + + nka_update(a, 0.25f); + v = nka_oscillate(a, 9, 1, 0.5f, NKA_WAVE_SAWTOOTH, 0); + CHECK(v == -0.75f && v == nka_oscillate(a, 9, 1, 0.5f, NKA_WAVE_SAWTOOTH, 0)); + CHECK(nka_oscillate_vec2(a, 9, one, half, NKA_WAVE_SAWTOOTH, zero).x == v); + nka_update(a, 0.25f); + CHECK(nka_oscillate(a, 9, 1, 0.5f, NKA_WAVE_SAWTOOTH, 0) == -0.5f); + c = (struct nka__fx_clock *)nka__get(&a->fx_clocks, nka_id_mix(9, (nk_hash)NKA__FX_OSC)); + CHECK(c && c->time == 0.5); + if (c) c->time = 86400; + for (k = 0; k < 15; ++k) { + nka_update(a, 1.0f / 60); + v = nka_oscillate(a, 9, 1, 4, NKA_WAVE_SAWTOOTH, 0.5f); + } + CHECK(NEAR(v, 0, 1e-4f)); + + nka_update(a, 0.25f); + v = nka_wiggle(a, 9, 10, 3); + CHECK(v == nka_wiggle(a, 9, 10, 3)); + v = nka_noise_channel_float(a, 9, 1.3f, 10, 0); + CHECK(v == nka_noise_channel_float(a, 9, 1.3f, 10, 0)); + nka_smooth_noise_float(a, 9, 10, 1.3f); + c = (struct nka__fx_clock *)nka__get(&a->fx_clocks, nka_id_mix(9, (nk_hash)NKA__FX_WIGGLE)); + CHECK(c && c->time == 0.25); + c = (struct nka__fx_clock *)nka__get(&a->fx_clocks, nka_id_mix(9, (nk_hash)NKA__FX_NOISE)); + CHECK(c && c->time == 0.25); + + nka_trigger_shake(a, 9); + nka_update(a, 0.25f); + v = nka_shake(a, 9, 10, 20, 1); + CHECK(v != 0 && v == nka_shake(a, 9, 10, 20, 1)); + c = (struct nka__fx_clock *)nka__get(&a->fx_clocks, nka_id_mix(9, (nk_hash)NKA__FX_SHAKE)); + CHECK(c && c->since == 0.25); + nka_destroy(a); +} + +static void test_shake(void) +{ + struct nka_context *a = nka_create(0); + const float dt = 1.0f / 64; + struct nk_vec2 i2 = { 10, 10 }, v2; + struct nka_vec4 i4 = { 1, 2, 3, 4 }, ci = { 0.3f, 0.3f, 0.3f, 0 }, v4; + struct nk_colorf gray = rgba(0.5f, 0.5f, 0.5f, 1), c = gray; + int frame, moved = 0, out = 0, axes[4] = { 0, 0, 0, 0 }, k; + float v; + + nka_update(a, dt); + CHECK(nka_shake(a, 1, 10, 20, 0.5f) == 0 && !nka_busy(a)); + nka_trigger_shake(a, 1); + for (frame = 1; frame <= 40; ++frame) { + float env = 1 - (float)frame * dt / 0.5f; + nka_update(a, dt); + v = nka_shake(a, 1, 10, 20, 0.5f); + if (env > 0) { + out += fabsf(v) > 10 * env * env + 1e-4f || !nka_busy(a); + moved += fabsf(v) > 0.5f; + } else { + out += v != 0 || nka_busy(a); + } + } + CHECK(out == 0 && moved > 5); + nka_trigger_shake(a, 1); + nka_update(a, dt); + v = nka_shake(a, 1, 10, 20, 0.5f); + CHECK(v != 0 && nka_busy(a)); + + moved = 0; + nka_trigger_shake(a, 2); + for (frame = 1; frame <= 40; ++frame) { + nka_update(a, dt); + v2 = nka_shake_vec2(a, 2, i2, 20, 0.5f); + v4 = nka_shake_vec4(a, 2, i4, 20, 0.5f); + k = nka_shake_int(a, 2, 10, 20, 0.5f); + c = nka_shake_color(a, 2, gray, ci, 20, 0.5f, NKA_COL_OKLAB); + moved += fabsf(v2.y) > 0.5f; + axes[0] += fabsf(v4.x) > 0.05f; + axes[1] += fabsf(v4.y) > 0.1f; + axes[2] += fabsf(v4.z) > 0.15f; + axes[3] += fabsf(v4.w) > 0.2f; + out += fabsf(v2.x) > 10 || fabsf(v2.y) > 10 || k < -10 || k > 10 || !unit_color(c); + out += fabsf(v4.x) > 1 || fabsf(v4.y) > 2 || fabsf(v4.z) > 3 || fabsf(v4.w) > 4; + } + CHECK(out == 0 && moved > 5 && axes[0] > 5 && axes[1] > 5 && axes[2] > 5 && axes[3] > 5); + CHECK(same_color(c, gray, 1e-4f)); + nka_trigger_shake(a, 2); + nka_update(a, dt); + v2 = nka_shake_vec2(a, 2, i2, 20, 0.5f); + CHECK(v2.x != 0 && v2.x == nka_shake(a, 2, 10, 20, 0.5f)); + + for (frame = 0; frame < 4; ++frame) { + nka_update(a, dt); + c = nka_shake_color(a, 3, gray, ci, 20, 0.5f, NKA_COL_SRGB); + } + CHECK(same_color(c, gray, 0) && !nka_busy(a)); + nka_trigger_shake(a, 4); + nka_update(a, dt); + CHECK(nka_shake(a, 4, 10, 20, 0) == 0 && !nka_busy(a)); + nka_trigger_shake(a, 5); + nka_update(a, dt); + v = nka_shake(a, 5, 10, 0, 1); + CHECK(v == v && fabsf(v) <= 10); + nka_destroy(a); +} + +static void test_wiggle(void) +{ + struct nka_context *a = nka_create(0); + const float dt = 1.0f / 60; + struct nk_vec2 amp2 = { 3, 7 }, v2; + struct nka_vec4 amp4 = { 1, 2, 3, 4 }, v4; + struct nk_colorf sky = rgba(0.2f, 0.5f, 1, 1); + float prev = 0, v; + int frame, changed = 0, out = 0, k, space; + for (frame = 0; frame < 600; ++frame) { + nka_update(a, dt); + v = nka_wiggle(a, 1, 10, 3); + out += fabsf(v) > 10 || !nka_busy(a); + if (frame) { + out += fabsf(v - prev) > 10 * 2 * 1.5f * 3 * dt + 1e-4f; + changed += fabsf(v - prev) > 0.01f; + } + prev = v; + v2 = nka_wiggle_vec2(a, 2, amp2, 2); + v4 = nka_wiggle_vec4(a, 3, amp4, 5); + k = nka_wiggle_int(a, 4, 6, 1); + out += fabsf(v2.x) > 3 || fabsf(v2.y) > 7 || k < -6 || k > 6; + out += fabsf(v4.x) > 1 || fabsf(v4.y) > 2 || fabsf(v4.z) > 3 || fabsf(v4.w) > 4; + for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) + out += !unit_color(nka_wiggle_color(a, (nk_hash)(5 + space), sky, amp4, 2, space)); + } + CHECK(out == 0 && changed > 200); + nka_destroy(a); +} + +static float roughness(int type, int octaves, int dims) +{ + struct nka_noise_opts o = nka_noise_opts_default(); + float sum = 0, h = 0.02f; + int i; + o.type = type; + o.octaves = octaves; + for (i = 1; i < 400; ++i) { + float x = (float)i * 0.0371f, y = 0.37f; + if (dims == 3) + sum += fabsf(nka_noise_3d(x + h, y, 0.5f, &o) - 2 * nka_noise_3d(x, y, 0.5f, &o) + nka_noise_3d(x - h, y, 0.5f, &o)); + else + sum += fabsf(nka_noise_2d(x + h, y, &o) - 2 * nka_noise_2d(x, y, &o) + nka_noise_2d(x - h, y, &o)); + } + return sum; +} + +static void test_noise(void) +{ + struct nka_noise_opts o = nka_noise_opts_default(); + int type, octaves, i, j; + float v; + + CHECK(o.type == NKA_NOISE_PERLIN && o.octaves == 4 && o.persistence == 0.5f && o.lacunarity == 2 && o.seed == 0); + CHECK(nka_noise_2d(1.3f, 2.7f, 0) == nka_noise_2d(1.3f, 2.7f, &o)); + CHECK(nka_noise_3d(1.3f, 2.7f, 0.1f, 0) == nka_noise_3d(1.3f, 2.7f, 0.1f, &o)); + for (type = NKA_NOISE_PERLIN; type <= NKA_NOISE_WORLEY; ++type) { + int differs2 = 0, differs3 = 0, same = 1; + for (octaves = 1; octaves <= 6; octaves += 5) { + float lo2 = 2, hi2 = -2, lo3 = 2, hi3 = -2; + o.type = type; + o.octaves = octaves; + o.seed = 3; + for (i = 0; i < 64; ++i) + for (j = 0; j < 64; ++j) { + float x = (float)i * 0.173f - 5, y = (float)j * 0.191f - 6, z = (float)((i * 7 + j) % 13) * 0.37f; + float n2 = nka_noise_2d(x, y, &o), n3 = nka_noise_3d(x, y, z, &o); + if (n2 < lo2) lo2 = n2; + if (n2 > hi2) hi2 = n2; + if (n3 < lo3) lo3 = n3; + if (n3 > hi3) hi3 = n3; + } + CHECK(lo2 >= -1 && hi2 <= 1 && hi2 - lo2 > 0.4f); + CHECK(lo3 >= -1 && hi3 <= 1 && hi3 - lo3 > 0.4f); + } + o.octaves = 3; + for (i = 0; i < 50; ++i) { + float x = (float)i * 0.731f - 9, y = (float)i * 0.317f + 2, z = (float)i * 0.113f; + o.seed = 7; + v = nka_noise_2d(x, y, &o); + same &= v == nka_noise_2d(x, y, &o); + o.seed = 8; + differs2 += v != nka_noise_2d(x, y, &o); + o.seed = 7; + v = nka_noise_3d(x, y, z, &o); + same &= v == nka_noise_3d(x, y, z, &o); + o.seed = 8; + differs3 += v != nka_noise_3d(x, y, z, &o); + } + CHECK(same && differs2 > 40 && differs3 > 40); + CHECK(roughness(type, 4, 2) > 1.5f * roughness(type, 1, 2)); + CHECK(roughness(type, 4, 3) > 1.5f * roughness(type, 1, 3)); + } + + for (type = NKA_NOISE_SIMPLEX; type <= NKA_NOISE_WORLEY; ++type) { + struct nka_noise_opts perlin = nka_noise_opts_default(); + int differs = 0; + o = perlin; + o.type = type; + for (i = 0; i < 50; ++i) { + float x = (float)i * 0.531f - 7, y = (float)i * 0.277f + 1, z = (float)i * 0.193f - 3; + differs += nka_noise_3d(x, y, z, &o) != nka_noise_3d(x, y, z, &perlin); + } + CHECK(differs > 40); + } + + o = nka_noise_opts_default(); + o.octaves = 0; + v = nka_noise_2d(0.3f, 0.4f, &o); + o.octaves = 1; + CHECK(v == nka_noise_2d(0.3f, 0.4f, &o)); + { + float first = nka_noise_2d(0.3f, 0.4f, &o), second = nka_noise_2d(0.6f, 0.8f, &o); + o.octaves = 2; + o.persistence = -1; + v = nka_noise_2d(0.3f, 0.4f, &o); + CHECK(NEAR(v, (first - second) / 2, 1e-6f)); + } + + { + struct nka_context *a = nka_create(0); + struct nk_vec2 f2 = { 0.5f, 0.7f }, a2 = { 30, 20 }, v2; + struct nka_vec4 f4 = { 0.3f, 0.4f, 0.5f, 0.6f }, a4 = { 1, 2, 3, 4 }, v4, big = { 1, 1, 1, 1 }; + struct nk_colorf sky = rgba(0.2f, 0.5f, 1, 1); + float prev = 0, sprev = 0, s; + int frame, changed = 0, schanged = 0, out = 0, space; + o = nka_noise_opts_default(); + o.type = NKA_NOISE_SIMPLEX; + for (frame = 1; frame <= 128; ++frame) { + nka_update(a, 1.0f / 64); + v = nka_noise_channel_float(a, 1, 1.5f, 5, &o); + out += v != nka_noise_2d((float)frame / 64 * 1.5f, NKA__FX_ROW0, &o) * 5; + out += fabsf(v) > 5 || !nka_busy(a); + changed += v != prev; + prev = v; + v2 = nka_noise_channel_vec2(a, 2, f2, a2, &o); + out += fabsf(v2.x) > 30 || fabsf(v2.y) > 20; + v4 = nka_noise_channel_vec4(a, 3, f4, a4, &o); + out += fabsf(v4.x) > 1 || fabsf(v4.y) > 2 || fabsf(v4.z) > 3 || fabsf(v4.w) > 4; + s = nka_smooth_noise_float(a, 4, 10, 2); + schanged += s != sprev; + sprev = s; + out += fabsf(s) > 10; + v2 = nka_smooth_noise_vec2(a, 5, a2, 1); + out += fabsf(v2.x) > 30 || fabsf(v2.y) > 20; + v4 = nka_smooth_noise_vec4(a, 6, a4, 1); + out += fabsf(v4.x) > 1 || fabsf(v4.y) > 2 || fabsf(v4.z) > 3 || fabsf(v4.w) > 4; + for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) { + out += !unit_color(nka_noise_channel_color(a, (nk_hash)(7 + space), sky, big, 2, &o, space)); + out += !unit_color(nka_smooth_noise_color(a, (nk_hash)(20 + space), sky, big, 2, space)); + } + } + CHECK(out == 0 && changed > 100 && schanged > 100); + nka_destroy(a); + } +} + +static void test_gradients(void) +{ + struct nk_colorf red = rgba(1, 0, 0, 1), green = rgba(0, 1, 0, 1), blue = rgba(0, 0, 1, 1); + struct nk_colorf white = rgba(1, 1, 1, 1), black = rgba(0, 0, 0, 1); + struct nk_color byte_red = { 255, 0, 0, 255 }; + struct nka_gradient g = { 0 }, h = { 0 }, x, y, r; + int i, space, sorted = 1, inside = 1, same = 1; + + CHECK(nka_gradient_add(&g, 1, blue) && nka_gradient_add(&g, 0, red) && nka_gradient_add(&g, 0.5f, green)); + CHECK(g.count == 3 && g.positions[0] == 0 && g.positions[1] == 0.5f && g.positions[2] == 1); + CHECK(same_color(g.colors[0], red, 0) && same_color(g.colors[1], green, 0) && same_color(g.colors[2], blue, 0)); + for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) { + CHECK(same_color(nka_gradient_sample(&g, 0, space), red, 0)); + CHECK(same_color(nka_gradient_sample(&g, 0.5f, space), green, 0)); + CHECK(same_color(nka_gradient_sample(&g, 1, space), blue, 0)); + CHECK(same_color(nka_gradient_sample(&g, -3, space), red, 0)); + CHECK(same_color(nka_gradient_sample(&g, 7, space), blue, 0)); + CHECK(same_color(nka_gradient_sample(&g, 0.25f, space), nka_color_blend(red, green, 0.5f, space), 1e-6f)); + CHECK(same_color(nka_gradient_sample(&g, 0.875f, space), nka_color_blend(green, blue, 0.75f, space), 1e-6f)); + } + CHECK(same_color(nka_gradient_sample(&g, 0.25f, NKA_COL_SRGB), rgba(0.5f, 0.5f, 0, 1), 1e-6f)); + CHECK(!same_color(nka_gradient_sample(&g, 0.25f, NKA_COL_OKLAB), rgba(0.5f, 0.5f, 0, 1), 0.05f)); + + CHECK(same_color(nka_gradient_sample(&h, 0.3f, NKA_COL_OKLAB), white, 0)); + CHECK(same_color(nka_gradient_sample(0, 0.3f, NKA_COL_OKLAB), white, 0)); + CHECK(nka_gradient_add(&h, 0.4f, red) && same_color(nka_gradient_sample(&h, 0.9f, NKA_COL_OKLAB), red, 0)); + CHECK(!nka_gradient_add(0, 0, red)); + + h = nka_gradient_two_color(black, white); + CHECK(nka_gradient_add(&h, 0.5f, red) && nka_gradient_add(&h, 0.5f, blue)); + CHECK(h.count == 4 && same_color(h.colors[1], red, 0) && same_color(h.colors[2], blue, 0)); + CHECK(same_color(nka_gradient_sample(&h, 0.5f, NKA_COL_SRGB), red, 0)); + CHECK(same_color(nka_gradient_sample(&h, 0.499f, NKA_COL_SRGB), red, 0.01f)); + CHECK(same_color(nka_gradient_sample(&h, 0.501f, NKA_COL_SRGB), blue, 0.01f)); + + nka__zero(&x, sizeof x); + for (i = 0; i < NKA_GRADIENT_MAX; ++i) + CHECK(nka_gradient_add(&x, (float)(NKA_GRADIENT_MAX - 1 - i) / (NKA_GRADIENT_MAX - 1), i & 1 ? white : black)); + CHECK(!nka_gradient_add(&x, 0.5f, red) && x.count == NKA_GRADIENT_MAX); + for (i = 1; i < x.count; ++i) sorted &= x.positions[i] > x.positions[i - 1]; + CHECK(sorted); + + nka__zero(&y, sizeof y); + CHECK(nka_gradient_add_rgba(&y, 0.3f, byte_red) && same_color(y.colors[0], red, 0) && y.positions[0] == 0.3f); + r = nka_gradient_solid(green); + CHECK(r.count == 2 && r.positions[0] == 0 && r.positions[1] == 1); + CHECK(same_color(r.colors[0], green, 0) && same_color(r.colors[1], green, 0)); + CHECK(same_color(nka_gradient_sample(&r, 0.37f, NKA_COL_OKLAB), green, 1e-4f)); + r = nka_gradient_two_color(red, blue); + CHECK(r.count == 2 && same_color(r.colors[0], red, 0) && same_color(r.colors[1], blue, 0)); + r = nka_gradient_three_color(red, green, blue); + CHECK(r.count == 3 && r.positions[1] == 0.5f && same_color(r.colors[1], green, 0) && same_color(r.colors[2], blue, 0)); + + x = nka_gradient_two_color(red, blue); + y = nka_gradient_three_color(black, white, black); + for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) { + r = nka_gradient_lerp(&x, &y, 0, space); + CHECK(r.count == 3); + for (i = 0; i < 3; ++i) CHECK(same_color(r.colors[i], nka_gradient_sample(&x, r.positions[i], space), 0)); + r = nka_gradient_lerp(&x, &y, 1, space); + for (i = 0; i < 3; ++i) CHECK(same_color(r.colors[i], nka_gradient_sample(&y, r.positions[i], space), 0)); + } + r = nka_gradient_lerp(&x, &y, 0.5f, NKA_COL_SRGB); + for (i = 0; i <= 20; ++i) { + float t = (float)i / 20; + same &= same_color(nka_gradient_sample(&r, t, NKA_COL_SRGB), + average(nka_gradient_sample(&x, t, NKA_COL_SRGB), nka_gradient_sample(&y, t, NKA_COL_SRGB)), 1e-6f); + } + CHECK(same && same_color(nka_gradient_sample(&r, 0.5f, NKA_COL_SRGB), rgba(0.75f, 0.5f, 0.75f, 1), 1e-6f)); + + r = nka_gradient_lerp(&h, &h, 0.5f, NKA_COL_SRGB); + CHECK(r.count == 4); + for (i = 0; i <= 1000; ++i) + same &= same_color(nka_gradient_sample(&r, (float)i / 1000, NKA_COL_SRGB), nka_gradient_sample(&h, (float)i / 1000, NKA_COL_SRGB), 1e-6f); + CHECK(same && same_color(nka_gradient_sample(&r, 0.501f, NKA_COL_SRGB), blue, 0.01f)); + r = nka_gradient_lerp(&h, &g, 0.5f, NKA_COL_SRGB); + CHECK(r.count == 4); + for (i = 0; i <= 1000; ++i) { + float t = (float)i / 1000; + same &= same_color(nka_gradient_sample(&r, t, NKA_COL_SRGB), + average(nka_gradient_sample(&h, t, NKA_COL_SRGB), nka_gradient_sample(&g, t, NKA_COL_SRGB)), 1e-6f); + } + CHECK(same); + + nka__zero(&x, sizeof x); + nka__zero(&y, sizeof y); + for (i = 0; i < NKA_GRADIENT_MAX; ++i) { + nka_gradient_add(&x, (float)i / (NKA_GRADIENT_MAX - 1), i & 1 ? red : blue); + nka_gradient_add(&y, ((float)i + 0.5f) / NKA_GRADIENT_MAX, i & 1 ? green : black); + } + r = nka_gradient_lerp(&x, &y, 0.5f, NKA_COL_OKLAB); + CHECK(r.count == NKA_GRADIENT_MAX && r.positions[0] == 0 && r.positions[NKA_GRADIENT_MAX - 1] == 1); + for (i = 1; i < r.count; ++i) sorted &= r.positions[i] >= r.positions[i - 1]; + for (i = 0; i < r.count; ++i) inside &= unit_color(r.colors[i]); + CHECK(sorted && inside); + + nka__zero(&y, sizeof y); + nka_gradient_add(&y, 0, black); + nka_gradient_add(&y, 1, white); + for (i = 0; i < NKA_GRADIENT_MAX - 2; ++i) { + float p = ((float)i + 0.5f) / (NKA_GRADIENT_MAX - 1); + nka_gradient_add(&y, p, rgba(p, p, p, 1)); + } + r = nka_gradient_lerp(&x, &y, 0.5f, NKA_COL_SRGB); + CHECK(r.count == NKA_GRADIENT_MAX); + for (i = 0; i <= 300; ++i) { + float t = (float)i / 300; + same &= same_color(nka_gradient_sample(&r, t, NKA_COL_SRGB), + average(nka_gradient_sample(&x, t, NKA_COL_SRGB), nka_gradient_sample(&y, t, NKA_COL_SRGB)), 1e-5f); + } + CHECK(same); + r = nka_gradient_lerp(0, 0, 0.5f, NKA_COL_OKLAB); + CHECK(r.count == 0); +} + +static void test_gradient_tween(void) +{ + struct nka_context *a = nka_create(0); + struct nka_gradient sunset = nka_gradient_three_color(rgba(1, 0.3f, 0, 1), rgba(1, 0.6f, 0.2f, 1), rgba(0.4f, 0.1f, 0.3f, 1)); + struct nka_gradient ocean = nka_gradient_two_color(rgba(0, 0.3f, 0.6f, 1), rgba(0, 0.9f, 0.9f, 1)); + struct nka_gradient forest = nka_gradient_solid(rgba(0.1f, 0.3f, 0.1f, 1)), r, r2, mid; + int i, same = 1; + + nka_update(a, 0.125f); + r = nka_tween_gradient(a, 1, 2, &sunset, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB); + CHECK(same_gradient(&r, &sunset) && !nka_busy(a)); + nka_update(a, 0.125f); + r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB); + CHECK(nka_busy(a)); + for (i = 0; i <= 20; ++i) + same &= same_color(nka_gradient_sample(&r, (float)i / 20, NKA_COL_SRGB), nka_gradient_sample(&sunset, (float)i / 20, NKA_COL_SRGB), 1e-6f); + nka_update(a, 0.5f); + r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB); + r2 = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB); + CHECK(same_gradient(&r, &r2) && nka_busy(a)); + for (i = 0; i <= 20; ++i) { + float t = (float)i / 20; + same &= same_color(nka_gradient_sample(&r, t, NKA_COL_SRGB), + average(nka_gradient_sample(&sunset, t, NKA_COL_SRGB), nka_gradient_sample(&ocean, t, NKA_COL_SRGB)), 1e-6f); + } + CHECK(same); + nka_update(a, 0.5f); + r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB); + CHECK(same_gradient(&r, &ocean) && !nka_busy(a)); + nka_update(a, 0.5f); + r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB); + CHECK(same_gradient(&r, &ocean) && !nka_busy(a)); + + nka_update(a, 0.125f); + nka_tween_gradient(a, 1, 2, &forest, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB); + nka_update(a, 0.25f); + mid = nka_tween_gradient(a, 1, 2, &forest, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB); + r = nka_tween_gradient(a, 1, 2, &sunset, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB); + for (i = 0; i <= 20; ++i) + same &= same_color(nka_gradient_sample(&r, (float)i / 20, NKA_COL_OKLAB), nka_gradient_sample(&mid, (float)i / 20, NKA_COL_OKLAB), 1e-5f); + CHECK(same && nka_busy(a)); + + r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CUT, NKA_COL_OKLAB); + CHECK(same_gradient(&r, &ocean)); + nka_update(a, 0.125f); + r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CUT, NKA_COL_OKLAB); + CHECK(same_gradient(&r, &ocean) && !nka_busy(a)); + nka_destroy(a); +} + +static float turned(float from, float to, int mode, float t) +{ + return nka_transform_lerp(xf(0, 0, from, 1, 1), xf(0, 0, to, 1, 1), t, mode).rotation; +} + +static int same_angle(float x, float y) { return NEAR(cosf(x), cosf(y), 1e-4f) && NEAR(sinf(x), sinf(y), 1e-4f); } + +static void test_transforms(void) +{ + struct nka_transform id = nka_transform_identity(), t1 = xf(10, 20, 0.5f, 2, 2), t2 = xf(-3, 4, -1.2f, 0.5f, 0.5f), r; + struct nka_transform ts[4]; + struct nk_vec2 p = { 7, -2 }, q; + float m[6], m2[6], late = tau - 0.1f; + int i, k, mode; + + CHECK(id.position.x == 0 && id.position.y == 0 && id.scale.x == 1 && id.scale.y == 1 && id.rotation == 0); + q = nka_transform_apply(id, p); + CHECK(q.x == 7 && q.y == -2); + q = nka_transform_apply(xf(1, 2, 0, 3, 4), p); + CHECK(q.x == 22 && q.y == -6); + CHECK(same_vec2(nka_transform_apply(xf(0, 0, NKA__PI / 2, 1, 1), nka__vec2(1, 0)), nka__vec2(0, 1), 1e-6f)); + + r = nka_transform_compose(t1, t2); + CHECK(same_vec2(nka_transform_apply(r, p), nka_transform_apply(t1, nka_transform_apply(t2, p)), 1e-4f)); + CHECK(NEAR(r.rotation, -0.7f, 1e-6f) && r.scale.x == 1 && r.scale.y == 1); + t1.scale = nka__vec2(2, 0.5f); + r = nka_transform_compose(t1, t2); + CHECK(same_vec2(r.position, nka_transform_apply(t1, t2.position), 1e-5f) && r.scale.x == 1 && r.scale.y == 0.25f); + t1.scale = nka__vec2(2, 2); + r = nka_transform_inverse(t1); + CHECK(same_vec2(nka_transform_apply(r, nka_transform_apply(t1, p)), p, 1e-4f)); + CHECK(same_vec2(nka_transform_apply(t1, nka_transform_apply(r, p)), p, 1e-4f)); + t1.scale = nka__vec2(3, 0.25f); + r = nka_transform_compose(nka_transform_inverse(t1), t1); + CHECK(same_vec2(r.position, nka__vec2(0, 0), 1e-4f) && NEAR(r.rotation, 0, 1e-6f)); + CHECK(NEAR(r.scale.x, 1, 1e-6f) && NEAR(r.scale.y, 1, 1e-6f)); + r = nka_transform_inverse(xf(0, 0, 0, 0, 2)); + CHECK(r.scale.x == 1 && r.scale.y == 0.5f); + + CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_SHORTEST, 1), -0.1f, 1e-5f)); + CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_SHORTEST, 0.5f), 0, 1e-5f)); + CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_LONGEST, 1), late, 1e-5f)); + CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_LONGEST, 0.5f), NKA__PI, 1e-5f)); + CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_CW, 1), late, 1e-5f)); + CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_CCW, 1), -0.1f, 1e-5f)); + CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_DIRECT, 0.5f), NKA__PI, 1e-5f)); + CHECK(NEAR(turned(late, 0.1f, NKA_ROTATION_SHORTEST, 1), tau + 0.1f, 1e-5f)); + CHECK(NEAR(turned(late, 0.1f, NKA_ROTATION_CW, 1), tau + 0.1f, 1e-5f)); + CHECK(NEAR(turned(late, 0.1f, NKA_ROTATION_CCW, 1), 0.1f, 1e-5f)); + CHECK(NEAR(turned(late, 0.1f, NKA_ROTATION_DIRECT, 1), 0.1f, 1e-5f)); + CHECK(NEAR(turned(3, -3, NKA_ROTATION_SHORTEST, 1), tau - 3, 1e-5f)); + CHECK(NEAR(turned(3, -3, NKA_ROTATION_SHORTEST, 0.5f), NKA__PI, 1e-5f)); + CHECK(NEAR(turned(3, -3, NKA_ROTATION_CW, 1), tau - 3, 1e-5f)); + CHECK(NEAR(turned(3, -3, NKA_ROTATION_CCW, 1), -3, 1e-5f)); + CHECK(NEAR(turned(3, -3, NKA_ROTATION_LONGEST, 1), -3, 1e-5f)); + CHECK(NEAR(turned(3, -3, NKA_ROTATION_DIRECT, 0.5f), 0, 1e-6f)); + CHECK(NEAR(turned(-3, 3, NKA_ROTATION_SHORTEST, 1), 3 - tau, 1e-5f)); + CHECK(NEAR(turned(-3, 3, NKA_ROTATION_SHORTEST, 0.5f), -NKA__PI, 1e-5f)); + CHECK(NEAR(turned(-3, 3, NKA_ROTATION_CW, 1), 3, 1e-5f)); + CHECK(NEAR(turned(-3, 3, NKA_ROTATION_CCW, 1), 3 - tau, 1e-5f)); + CHECK(NEAR(turned(-3, 3, NKA_ROTATION_LONGEST, 1), 3, 1e-5f)); + for (mode = NKA_ROTATION_SHORTEST; mode <= NKA_ROTATION_DIRECT; ++mode) { + CHECK(same_angle(turned(3, -3, mode, 1), -3) && same_angle(turned(0.1f, late, mode, 1), late)); + CHECK(turned(1, 2, mode, 0) == 1); + } + for (k = 1; k <= 10; ++k) { + CHECK(turned(3, -3, NKA_ROTATION_CW, (float)k / 10) > turned(3, -3, NKA_ROTATION_CW, (float)(k - 1) / 10)); + CHECK(turned(-3, 3, NKA_ROTATION_CCW, (float)k / 10) < turned(-3, 3, NKA_ROTATION_CCW, (float)(k - 1) / 10)); + } + + ts[0] = xf(5, -7, 0.7f, 2, 3); + ts[1] = xf(1, 2, -2.5f, -1.5f, 0.5f); + ts[2] = xf(0, 0, 3, 1, -2); + ts[3] = id; + for (i = 0; i < 4; ++i) { + nka_transform_to_matrix(ts[i], m); + r = nka_transform_from_matrix(m[0], m[1], m[3], m[4], m[2], m[5]); + nka_transform_to_matrix(r, m2); + for (k = 0; k < 6; ++k) CHECK(NEAR(m[k], m2[k], 1e-5f)); + CHECK(same_vec2(nka_transform_apply(r, p), nka_transform_apply(ts[i], p), 1e-4f)); + } + nka_transform_to_matrix(ts[0], m); + r = nka_transform_from_matrix(m[0], m[1], m[3], m[4], m[2], m[5]); + CHECK(NEAR(r.rotation, 0.7f, 1e-6f) && NEAR(r.scale.x, 2, 1e-5f) && NEAR(r.scale.y, 3, 1e-5f)); + CHECK(r.position.x == 5 && r.position.y == -7); + nka_transform_to_matrix(ts[1], m); + r = nka_transform_from_matrix(m[0], m[1], m[3], m[4], m[2], m[5]); + CHECK(NEAR(r.rotation, -2.5f, 1e-5f) && NEAR(r.scale.x, -1.5f, 1e-5f) && NEAR(r.scale.y, 0.5f, 1e-5f)); + r = nka_transform_from_matrix(0, -1, 1, 0, 4, 6); + CHECK(NEAR(r.rotation, NKA__PI / 2, 1e-6f) && NEAR(r.scale.x, 1, 1e-6f) && NEAR(r.scale.y, 1, 1e-6f)); + nka_transform_to_matrix(id, 0); +} + +static void test_transform_tween(void) +{ + struct nka_context *a = nka_create(0); + struct nka_transform t0 = xf(10, 10, 3, 1, 1), t1 = xf(30, -10, -3, 2, 0.5f), r, r2; + + nka_update(a, 0.125f); + r = nka_tween_transform(a, 1, 2, t0, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST); + CHECK(same_xf(r, t0) && !nka_busy(a)); + nka_update(a, 0.125f); + r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST); + CHECK(same_xf(r, t0) && nka_busy(a)); + nka_update(a, 0.5f); + r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST); + r2 = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST); + CHECK(same_xf(r, r2) && nka_busy(a)); + CHECK(NEAR(r.position.x, 20, 1e-4f) && NEAR(r.position.y, 0, 1e-4f) && NEAR(r.scale.x, 1.5f, 1e-5f) && NEAR(r.scale.y, 0.75f, 1e-5f)); + CHECK(NEAR(r.rotation, NKA__PI, 1e-4f)); + nka_update(a, 0.5f); + r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST); + CHECK(same_xf(r, t1) && !nka_busy(a)); + nka_update(a, 0.5f); + r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST); + CHECK(same_xf(r, t1) && !nka_busy(a)); + r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_CW); + CHECK(same_xf(r, t1) && !nka_busy(a)); + + nka_update(a, 0.125f); + nka_tween_transform(a, 1, 2, t0, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_CW); + nka_update(a, 0.5f); + r = nka_tween_transform(a, 1, 2, t0, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_CW); + CHECK(NEAR(r.rotation, 0, 1e-4f) && nka_busy(a)); + + r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CUT, NKA_ROTATION_SHORTEST); + CHECK(same_xf(r, t1)); + nka_update(a, 0.125f); + r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CUT, NKA_ROTATION_SHORTEST); + CHECK(same_xf(r, t1) && !nka_busy(a)); + nka_destroy(a); +} + +static void test_drag(void) +{ + struct nka_context *a = nka_create(0); + struct nka_drag_opts o = nka_drag_opts_default(); + struct nka_drag_feedback fb; + struct nk_vec2 pts[3]; + int i, over = 0; + + CHECK(o.snap_grid.x == 0 && o.snap_grid.y == 0 && o.snap_points == 0 && o.snap_points_count == 0); + CHECK(o.snap_duration == 0.2f && o.overshoot == 0 && o.ease_type == NKA_EASE_OUT_CUBIC); + fb = nka_drag_update(a, 1, nka__vec2(5, 5)); + CHECK(!fb.is_dragging && fb.position.x == 5 && fb.velocity.x == 0); + fb = nka_drag_release(a, 1, nka__vec2(5, 5), &o); + CHECK(!fb.is_snapping && fb.snap_progress == 1 && fb.position.x == 5); + + nka_update(a, 0.125f); + fb = nka_drag_begin(a, 1, nka__vec2(100, 100)); + CHECK(fb.is_dragging && !fb.is_snapping && fb.position.x == 100 && fb.offset.x == 0 && fb.velocity.x == 0); + fb = nka_drag_update(a, 1, nka__vec2(100, 100)); + CHECK(fb.is_dragging && fb.velocity.x == 0 && fb.velocity.y == 0); + nka_update(a, 0.125f); + fb = nka_drag_update(a, 1, nka__vec2(110, 100)); + CHECK(fb.velocity.x == 80 && fb.velocity.y == 0 && fb.offset.x == 10 && fb.offset.y == 0); + nka_update(a, 0.125f); + fb = nka_drag_update(a, 1, nka__vec2(115, 102)); + CHECK(fb.velocity.x == 40 && fb.velocity.y == 16); + fb = nka_drag_update(a, 1, nka__vec2(115, 102)); + CHECK(fb.velocity.x == 40 && fb.velocity.y == 16 && fb.is_dragging); + + o.snap_grid = nka__vec2(32, 32); + o.snap_duration = 0.5f; + o.ease_type = NKA_EASE_LINEAR; + nka_update(a, 0.125f); + fb = nka_drag_release(a, 1, nka__vec2(115, 102), &o); + CHECK(!fb.is_dragging && fb.is_snapping && fb.snap_progress == 0 && nka_busy(a)); + CHECK(fb.position.x == 115 && fb.position.y == 102 && fb.velocity.x == 40); + for (i = 1; i <= 4; ++i) { + nka_update(a, 0.125f); + fb = nka_drag_release(a, 1, fb.position, &o); + if (i < 4) + CHECK(fb.is_snapping && nka_busy(a) && fb.snap_progress == (float)i / 4 + && NEAR(fb.position.x, 115 + 13 * (float)i / 4, 1e-4f) && NEAR(fb.position.y, 102 - 6 * (float)i / 4, 1e-4f)); + } + CHECK(!fb.is_snapping && !nka_busy(a) && fb.snap_progress == 1 && fb.position.x == 128 && fb.position.y == 96); + CHECK(fb.offset.x == 28 && fb.offset.y == -4); + nka_update(a, 0.125f); + fb = nka_drag_release(a, 1, fb.position, &o); + CHECK(!fb.is_snapping && !nka_busy(a) && fb.position.x == 128 && fb.position.y == 96); + + pts[0] = nka__vec2(0, 0); + pts[1] = nka__vec2(50, 50); + pts[2] = nka__vec2(100, 0); + o = nka_drag_opts_default(); + o.snap_grid = nka__vec2(32, 32); + o.snap_points = pts; + o.snap_points_count = 3; + o.snap_duration = 0.25f; + nka_update(a, 0.125f); + nka_drag_begin(a, 2, nka__vec2(10, 10)); + nka_update(a, 0.125f); + nka_drag_update(a, 2, nka__vec2(40, 45)); + nka_update(a, 0.125f); + fb = nka_drag_release(a, 2, nka__vec2(40, 45), &o); + CHECK(fb.is_snapping && nka_busy(a)); + nka_update(a, 0.125f); + fb = nka_drag_release(a, 2, fb.position, &o); + CHECK(fb.is_snapping && NEAR(fb.position.x, 48.75f, 1e-4f) && NEAR(fb.position.y, 49.375f, 1e-4f)); + nka_update(a, 0.125f); + fb = nka_drag_release(a, 2, fb.position, &o); + CHECK(!fb.is_snapping && fb.position.x == 50 && fb.position.y == 50 && !nka_busy(a)); + + o = nka_drag_opts_default(); + pts[0] = nka__vec2(100, 0); + o.snap_points = pts; + o.snap_points_count = 1; + o.snap_duration = 1; + o.overshoot = 1; + nka_update(a, 0.125f); + nka_drag_begin(a, 3, nka__vec2(0, 0)); + fb = nka_drag_release(a, 3, nka__vec2(0, 0), &o); + for (i = 1; i <= 8; ++i) { + nka_update(a, 0.125f); + fb = nka_drag_release(a, 3, fb.position, &o); + if (i < 8) CHECK(NEAR(fb.position.x, 100 * nka_eval_preset(NKA_EASE_OUT_BACK, (float)i / 8), 2e-3f)); + over += fb.position.x > 100.5f; + } + CHECK(over > 0 && fb.position.x == 100 && !fb.is_snapping); + o.overshoot = 0; + over = 0; + nka_drag_begin(a, 4, nka__vec2(0, 0)); + fb = nka_drag_release(a, 4, nka__vec2(0, 0), &o); + for (i = 1; i <= 8; ++i) { + nka_update(a, 0.125f); + fb = nka_drag_release(a, 4, fb.position, &o); + over += fb.position.x > 100; + } + CHECK(over == 0 && fb.position.x == 100); + + o.snap_duration = 0; + nka_drag_begin(a, 5, nka__vec2(30, 0)); + fb = nka_drag_release(a, 5, nka__vec2(30, 0), &o); + CHECK(!fb.is_snapping && fb.position.x == 100 && fb.snap_progress == 1); + o.snap_duration = 1; + nka_drag_begin(a, 6, nka__vec2(100, 0)); + nka_update(a, 0.125f); + fb = nka_drag_release(a, 6, nka__vec2(100, 0), &o); + CHECK(!fb.is_snapping && fb.position.x == 100 && !nka_busy(a)); + + nka_update(a, 0.125f); + nka_drag_begin(a, 7, nka__vec2(1, 1)); + nka_drag_cancel(a, 7); + fb = nka_drag_update(a, 7, nka__vec2(2, 2)); + CHECK(!fb.is_dragging && !fb.is_snapping); + o = nka_drag_opts_default(); + o.snap_grid = nka__vec2(32, 32); + nka_drag_begin(a, 8, nka__vec2(10, 10)); + fb = nka_drag_release(a, 8, nka__vec2(10, 10), &o); + CHECK(fb.is_snapping); + nka_drag_cancel(a, 8); + nka_update(a, 0.125f); + fb = nka_drag_release(a, 8, nka__vec2(10, 10), 0); + CHECK(!fb.is_snapping && fb.position.x == 10 && fb.position.y == 10 && !nka_busy(a)); + + nka_drag_begin(a, 0, nka__vec2(1, 1)); + nka_drag_begin(a, 1, nka__vec2(9, 9)); + fb = nka_drag_update(a, 0, nka__vec2(2, 2)); + CHECK(fb.is_dragging && fb.offset.x == 1 && fb.offset.y == 1); + nka_destroy(a); +} + +static int live; + +static void *count_alloc(nk_handle h, void *old, nk_size n) +{ + (void)h; (void)old; + live++; + return malloc(n); +} + +static void count_free(nk_handle h, void *p) +{ + (void)h; + live--; + free(p); +} + +static void touch(struct nka_context *a, nk_hash id, const struct nka_gradient *g, int frame) +{ + nka_oscillate(a, id, 1, 1, NKA_WAVE_SINE, 0); + nka_trigger_shake(a, id); + nka_wiggle(a, id, 1, 1); + nka_noise_channel_float(a, id, 1, 1, 0); + nka_tween_gradient(a, id, 0, g, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB); + nka_tween_transform(a, id, 0, nka_transform_identity(), 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST); + if (frame) nka_drag_update(a, id, nka__vec2(1, 1)); + else nka_drag_begin(a, id, nka__vec2(0, 0)); +} + +static void test_memory(void) +{ + struct nk_allocator al; + struct nka_context *a; + struct nka_gradient g = nka_gradient_two_color(rgba(1, 0, 0, 1), rgba(0, 0, 1, 1)), r; + struct nka_drag_feedback fb; + int i, frame; + + al.userdata.ptr = 0; + al.alloc = count_alloc; + al.free = count_free; + a = nka_create(&al); + nka_update(a, 0.1f); + for (i = 0; i < 40; ++i) touch(a, (nk_hash)i, &g, 0); + CHECK(a->fx_clocks.len == 160 && a->fx_grads.len == 40 && a->fx_xforms.len == 40 && a->fx_drags.len == 40); + for (frame = 1; frame <= 10; ++frame) { + nka_update(a, 0.1f); + for (i = 0; i < 40; i += 2) touch(a, (nk_hash)i, &g, frame); + } + nka_gc(a, 5); + CHECK(a->fx_clocks.len == 80 && a->fx_grads.len == 20 && a->fx_xforms.len == 20 && a->fx_drags.len == 20); + CHECK(live > 1); + nka_clear(a); + CHECK(!a->fx_clocks.cap && !a->fx_grads.cap && !a->fx_xforms.cap && !a->fx_drags.cap); + touch(a, 1, &g, 0); + CHECK(a->fx_clocks.len == 4 && a->fx_drags.len == 1); + nka_destroy(a); + CHECK(live == 0); + + CHECK(nka_oscillate(0, 1, 2, 1, NKA_WAVE_SINE, 0.25f) == 2); + CHECK(nka_shake(0, 1, 2, 1, 1) == 0 && nka_wiggle(0, 1, 0, 1) == 0 && nka_noise_channel_float(0, 1, 1, 0, 0) == 0); + nka_trigger_shake(0, 1); + r = nka_tween_gradient(0, 1, 2, &g, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB); + CHECK(same_gradient(&r, &g)); + r = nka_tween_gradient(0, 1, 2, 0, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB); + CHECK(r.count == 0); + CHECK(nka_tween_transform(0, 1, 2, nka_transform_identity(), 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST).scale.x == 1); + fb = nka_drag_begin(0, 1, nka__vec2(3, 4)); + CHECK(fb.position.x == 3 && fb.is_dragging); + fb = nka_drag_update(0, 1, nka__vec2(3, 4)); + CHECK(!fb.is_dragging); + fb = nka_drag_release(0, 1, nka__vec2(3, 4), 0); + CHECK(fb.position.y == 4 && !fb.is_snapping); + nka_drag_cancel(0, 1); +} + +int main(void) +{ + test_waves(); + test_frames(); + test_shake(); + test_wiggle(); + test_noise(); + test_gradients(); + test_gradient_tween(); + test_transforms(); + test_transform_tween(); + test_drag(); + test_memory(); + if (failures) printf("%d failed\n", failures); + else printf("fx: ok\n"); + return failures != 0; +} diff --git a/tests/nk.c b/tests/nk.c new file mode 100644 index 0000000..f0ad5c6 --- /dev/null +++ b/tests/nk.c @@ -0,0 +1,531 @@ +#include +#include + +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#include "nuklear.h" +#define NUKANIM_IMPLEMENTATION +#include "nukanim.h" + +static int failures; + +static void check(int ok, const char *what, int line) +{ + if (ok) return; + printf("nk.c:%d: %s\n", line, what); + failures++; +} + +#define CHECK(c) check((c) != 0, #c, __LINE__) +#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps)) + +static const struct nka_ease linear = { NKA_EASE_LINEAR, 0, 0, 0, 0 }; +static struct nk_context ctx; +static struct nk_user_font font; + +static float text_width(nk_handle h, float height, const char *s, int len) +{ + (void)h; (void)s; + return (float)len * height * 0.5f; +} + +/* One frame of a 200x200 window holding 50 rows, or a group of them. */ +static nk_uint scroll_frame(struct nka_context *a, int group, int action, const struct nk_vec2 *wheel) +{ + nk_uint x = 0, y = 0; + int i; + nk_input_begin(&ctx); + if (wheel) { + nk_input_motion(&ctx, 50, 50); + nk_input_scroll(&ctx, *wheel); + } + nk_input_end(&ctx); + if (nk_begin(&ctx, "list", nk_rect(0, 0, 200, 200), 0)) { + if (group) { + nk_layout_row_dynamic(&ctx, 150, 1); + if (nk_group_begin(&ctx, "g", 0)) { + nk_layout_row_dynamic(&ctx, 20, 1); + for (i = 0; i < 50; ++i) nk_label(&ctx, "row", NK_TEXT_LEFT); + if (action == 1) nka_scroll_to_y(a, &ctx, 300, 1, linear); + if (action == 2) nka_scroll_to_bottom(a, &ctx, 1, linear); + nka_scroll(a, &ctx); + nk_group_end(&ctx); + } + nk_group_get_scroll(&ctx, "g", &x, &y); + } else { + nk_layout_row_dynamic(&ctx, 20, 1); + for (i = 0; i < 50; ++i) nk_label(&ctx, "row", NK_TEXT_LEFT); + if (action == 1) nka_scroll_to_y(a, &ctx, 300, 1, linear); + if (action == 2) nka_scroll_to_bottom(a, &ctx, 1, linear); + if (action == 3) nka_scroll_to_top(a, &ctx, 1, linear); + nka_scroll(a, &ctx); + } + } + nk_end(&ctx); + if (!group) y = nk_window_find(&ctx, "list")->scrollbar.y; + nk_clear(&ctx); + return y; +} + +static struct nka__nk_scroll *last_scroll(struct nka_context *a) +{ + int i; + for (i = 0; i < a->nk_scrolls.cap; ++i) + if (a->nk_scrolls.keys[i]) return (struct nka__nk_scroll *)nka__at(&a->nk_scrolls, i); + return 0; +} + +static void test_scroll(int group) +{ + struct nka_context *a = nka_create(0); + struct nk_vec2 up = { 0, 1 }; + nk_uint max, y; + nka_update(a, 0.016f); + CHECK(scroll_frame(a, group, 0, 0) == 0); + nka_update(a, 0); + CHECK(scroll_frame(a, group, 1, 0) == 0); + CHECK(nka_busy(a)); + nka_update(a, 0.25f); + CHECK(scroll_frame(a, group, 0, 0) == 75); + nka_update(a, 0.25f); + CHECK(scroll_frame(a, group, 0, 0) == 150); + nka_update(a, 0.5f); + CHECK(scroll_frame(a, group, 0, 0) == 300); + CHECK(!nka_busy(a)); + nka_update(a, 0.5f); + CHECK(scroll_frame(a, group, 0, 0) == 300); + + nka_update(a, 0); + CHECK(scroll_frame(a, group, 2, 0) == 300); + max = (nk_uint)last_scroll(a)->max[1]; + CHECK(max > 300 && max < 50 * 30); + nka_update(a, 0.5f); + y = scroll_frame(a, group, 0, 0); + CHECK(y > 300 && y < max); + nka_update(a, 0.5f); + CHECK(scroll_frame(a, group, 0, 0) == max); + nka_update(a, 0.5f); + CHECK(scroll_frame(a, group, 0, 0) == max); + + if (!group) { + nka_update(a, 0); + scroll_frame(a, group, 3, 0); + nka_update(a, 0.25f); + y = scroll_frame(a, group, 0, &up); + CHECK(last_scroll(a)->active[1]); + nka_update(a, 0.25f); + y = scroll_frame(a, group, 0, 0); + CHECK(!last_scroll(a)->active[1]); + nka_update(a, 0.25f); + CHECK(scroll_frame(a, group, 0, 0) == y); + } + nka_destroy(a); +} + +static void test_style(void) +{ + struct nka_context *a = nka_create(0); + struct nk_color table[NK_COLOR_COUNT]; + struct nk_style keep = ctx.style, base, wide, out; + struct nk_user_font other = font; + nk_hash d = nka_id("default"), red = nka_id("red"), big = nka_id("big"); + int i; + + for (i = 0; i < NK_COLOR_COUNT; ++i) table[i] = nk_rgba(200, 30, 30, 255); + nka_style_register_current(a, &ctx, d); + nka_style_register_table(a, &ctx, red, table); + CHECK(memcmp(&keep, &ctx.style, sizeof keep) == 0); + CHECK(nka_style_exists(a, d) && nka_style_exists(a, red) && !nka_style_exists(a, big)); + + base = ctx.style; + wide = ctx.style; + wide.window.rounding = base.window.rounding + 10; + wide.window.padding = nk_vec2(base.window.padding.x + 20, base.window.padding.y); + wide.text.color = nk_rgba(0, 0, 0, 255); + wide.font = &other; + wide.button.normal = nk_style_item_hide(); + nka_style_register(a, big, &wide); + + nka_style_blend_to(a, d, big, 0, &out, NKA_COL_SRGB); + CHECK(memcmp(&out, &base, sizeof out) == 0); + nka_style_blend_to(a, d, big, 1, &out, NKA_COL_SRGB); + CHECK(memcmp(&out, &wide, sizeof out) == 0); + nka_style_blend_to(a, d, big, 0.4f, &out, NKA_COL_SRGB); + CHECK(NEAR(out.window.rounding, base.window.rounding + 4, 1e-4f)); + CHECK(NEAR(out.window.padding.x, base.window.padding.x + 8, 1e-4f)); + CHECK(out.text.color.r == (nk_byte)(base.text.color.r * 0.6f + 0.5f)); + CHECK(out.font == base.font && out.button.normal.type == NK_STYLE_ITEM_COLOR); + nka_style_blend_to(a, d, big, 0.6f, &out, NKA_COL_SRGB); + CHECK(out.font == &other && out.button.normal.type == wide.button.normal.type); + + nka_style_blend_to(a, d, red, 0.5f, &out, NKA_COL_OKLAB); + CHECK(out.window.background.r > base.window.background.r && out.window.background.r < 200); + CHECK(out.window.rounding == base.window.rounding); + + nka_update(a, 0.016f); + nka_style_tween(a, &ctx, 1, red, 1, linear, NKA_COL_SRGB); + CHECK(memcmp(&keep, &ctx.style, sizeof keep) == 0 && nka_busy(a)); + nka_update(a, 0.5f); + nka_style_tween(a, &ctx, 1, red, 1, linear, NKA_COL_SRGB); + CHECK(ctx.style.window.background.r > keep.window.background.r && ctx.style.window.background.r < 200); + nka_update(a, 0.5f); + nka_style_tween(a, &ctx, 1, red, 1, linear, NKA_COL_SRGB); + CHECK(!nka_busy(a)); + nka_style_blend_to(a, d, red, 1, &out, NKA_COL_SRGB); + CHECK(memcmp(&out, &ctx.style, sizeof out) == 0); + nka_update(a, 0.5f); + nka_style_tween(a, &ctx, 1, red, 1, linear, NKA_COL_SRGB); + CHECK(!nka_busy(a) && memcmp(&out, &ctx.style, sizeof out) == 0); + nka_style_tween(a, &ctx, 1, d, 1, linear, NKA_COL_SRGB); + CHECK(nka_busy(a)); + nka_update(a, 1); + nka_style_tween(a, &ctx, 1, d, 1, linear, NKA_COL_SRGB); + CHECK(memcmp(&keep, &ctx.style, sizeof keep) == 0); + + nka_style_unregister(a, red); + CHECK(!nka_style_exists(a, red)); + out = keep; + nka_style_blend_to(a, d, red, 0.5f, &out, NKA_COL_SRGB); + CHECK(memcmp(&keep, &out, sizeof out) == 0); + nka_destroy(a); + ctx.style = keep; +} + +struct glyph { float x, y, height; nk_byte alpha; char c; const struct nk_user_font *font; }; + +/* Draws text at (20, 40) in a fresh window and collects the glyphs it drew. */ +static int stagger(struct nka_context *a, const char *text, float progress, struct nka_text_stagger_opts *o, + struct glyph *out) +{ + const struct nk_command *cmd; + int n = 0; + nk_input_begin(&ctx); + nk_input_end(&ctx); + o->pos = nk_vec2(20, 40); + if (nk_begin(&ctx, "text", nk_rect(0, 0, 300, 200), NK_WINDOW_NO_SCROLLBAR)) + nka_text_stagger(a, &ctx, text, progress, o); + nk_end(&ctx); + nk_foreach(cmd, &ctx) { + const struct nk_command_text *t = (const struct nk_command_text *)cmd; + if (cmd->type != NK_COMMAND_TEXT || n == 16) continue; + out[n].x = t->x; + out[n].y = t->y; + out[n].height = t->height; + out[n].alpha = t->foreground.a; + out[n].c = t->string[0]; + out[n].font = t->font; + ++n; + } + nk_clear(&ctx); + return n; +} + +static void test_text(void) +{ + struct nka_context *a = nka_create(0); + struct nka_text_stagger_opts o = nka_text_stagger_opts_default(); + struct glyph g[16]; + int n, i; + + CHECK(NEAR(nka_text_stagger_duration("Hello", &o), 0.5f, 1e-6f)); + CHECK(nka_text_stagger_duration("", &o) == 0 && nka_text_stagger_duration("h\xc3\xa9llo", 0) == nka_text_stagger_duration("hello", 0)); + o.letter_spacing = 2; + CHECK(nka_text_stagger_width(&ctx, "Hello", &o) == 5 * 8 + 4 * 2); + o.letter_spacing = 0; + + o.ease = linear; + nka_update(a, 0.016f); + n = stagger(a, "Hello", 0.3f, &o, g); + CHECK(n == 3); + for (i = 0; i < n; ++i) CHECK(g[i].x == 20 + i * 8 && g[i].y == 40 && g[i].c == "Hello"[i]); + CHECK(NEAR(g[0].alpha, 128, 1) && NEAR(g[1].alpha, 85, 1) && NEAR(g[2].alpha, 43, 1)); + n = stagger(a, "Hello", 0.8f, &o, g); + CHECK(n == 5 && g[2].alpha == 255 && NEAR(g[3].alpha, 213, 1) && NEAR(g[4].alpha, 170, 1)); + n = stagger(a, "Hello", 1, &o, g); + CHECK(n == 5 && g[4].alpha == 255 && g[4].font == &font); + CHECK(stagger(a, "Hello", 0, &o, g) == 0); + CHECK(stagger(a, "h\xc3\xa9llo", 1, &o, g) == 5); + + o.effect = NKA_TEXT_FX_SLIDE_UP; + n = stagger(a, "Hello", 0.8f, &o, g); + CHECK(g[0].y == 40 && g[3].y > 40 && g[4].y > g[3].y); + o.effect = NKA_TEXT_FX_SLIDE_RIGHT; + n = stagger(a, "Hello", 0.8f, &o, g); + CHECK(g[0].x == 20 && g[3].x < 44); + + o.effect = NKA_TEXT_FX_SCALE; + n = stagger(a, "Hello", 0.8f, &o, g); + CHECK(n == 5 && g[2].height == 16 && g[2].font == &font); + CHECK(g[3].height < 16 && g[3].font != &font && g[3].font->height == g[3].height && g[3].font->width == font.width); + CHECK(g[4].height < g[3].height && g[4].y > 40); + CHECK(a->nk_fonts && a->nk_fonts->used == 2); + nka_update(a, 0.016f); + CHECK(a->nk_fonts->used == 0); + + o.effect = NKA_TEXT_FX_BOUNCE; + n = stagger(a, "Hello", 0.8f, &o, g); + CHECK(n == 5 && g[3].height > 16); + + o.effect = NKA_TEXT_FX_ROTATE; + n = stagger(a, "Hello", 0.8f, &o, g); + CHECK(n == 5 && g[3].height == 16 && g[3].alpha < 255); + + o.effect = NKA_TEXT_FX_TYPEWRITER; + n = stagger(a, "Hello", 0.3f, &o, g); + CHECK(n == 3 && g[2].alpha == 255); + + o.effect = NKA_TEXT_FX_WAVE; + n = stagger(a, "Hello", 0, &o, g); + CHECK(n == 5 && g[0].y == 40 && g[1].y != 40); + + o.effect = NKA_TEXT_FX_FADE; + o.font_scale = 2; + n = stagger(a, "Hello", 1, &o, g); + CHECK(n == 5 && g[1].x == 20 + 16 && g[1].height == 32); + nka_destroy(a); +} + +static int path_text(struct nka_context *a, nk_hash path, const char *text, float progress, + const struct nka_text_path_opts *o, struct glyph *out) +{ + const struct nk_command *cmd; + int n = 0; + nk_input_begin(&ctx); + nk_input_end(&ctx); + if (nk_begin(&ctx, "text", nk_rect(0, 0, 300, 200), NK_WINDOW_NO_SCROLLBAR)) + nka_text_path_animated(a, &ctx, path, text, progress, o); + nk_end(&ctx); + nk_foreach(cmd, &ctx) { + const struct nk_command_text *t = (const struct nk_command_text *)cmd; + if (cmd->type != NK_COMMAND_TEXT || n == 16) continue; + out[n].x = t->x; + out[n].y = t->y; + out[n].height = t->height; + out[n].alpha = t->foreground.a; + out[n].c = t->string[0]; + out[n].font = t->font; + ++n; + } + nk_clear(&ctx); + return n; +} + +static void test_text_path(void) +{ + struct nka_context *a = nka_create(0); + struct nka_text_path_opts o = nka_text_path_opts_default(); + struct glyph g[16]; + nk_hash line = nka_id("line"), down = nka_id("down"); + int n; + nka_path_begin(a, line, nk_vec2(10, 100)); + nka_path_line_to(a, nk_vec2(210, 100)); + nka_path_end(a); + nka_path_begin(a, down, nk_vec2(150, 20)); + nka_path_line_to(a, nk_vec2(150, 180)); + nka_path_end(a); + nka_update(a, 0.016f); + CHECK(nka_text_path_width(&ctx, "abcd", &o) == 32); + n = path_text(a, line, "abcd", 1, &o, g); + CHECK(n == 4 && g[0].x == 10 && g[1].x == 18 && g[3].x == 34 && g[0].y == 84 && g[2].c == 'c'); + CHECK(nka_path_has_arc_lut(a, line)); + o.align = NKA_TEXT_ALIGN_CENTER; + n = path_text(a, line, "abcd", 1, &o, g); + CHECK(n == 4 && g[0].x == 94); + o.align = NKA_TEXT_ALIGN_END; + n = path_text(a, line, "abcd", 1, &o, g); + CHECK(n == 4 && g[0].x == 178 && g[3].x == 202); + o.align = NKA_TEXT_ALIGN_START; + o.offset = -12; + n = path_text(a, line, "abcd", 1, &o, g); + CHECK(n == 3 && g[0].c == 'b' && g[0].x == 6); + o.offset = 0; + o.flip_y = 1; + n = path_text(a, line, "abcd", 1, &o, g); + CHECK(n == 4 && g[0].y == 100); + o.flip_y = 0; + n = path_text(a, line, "abcd", 0.5f, &o, g); + CHECK(n == 2 && g[1].alpha == 255); + n = path_text(a, line, "abcd", 0.625f, &o, g); + CHECK(n == 3 && NEAR(g[2].alpha, 128, 1)); + CHECK(path_text(a, line, "abcd", 0, &o, g) == 0); + n = path_text(a, down, "ab", 1, &o, g); + CHECK(n == 2 && g[0].x == 146 && g[0].y == 8 && g[1].y == 16); + o.font_scale = 2; + n = path_text(a, line, "abcd", 1, &o, g); + CHECK(n == 4 && g[1].x == 26 && g[0].height == 32 && g[0].font != &font); + CHECK(path_text(a, nka_id("missing"), "abcd", 1, &o, g) == 0); + nka_destroy(a); +} + +static double clock_now; + +static double fake_clock(void *user) +{ + (void)user; + return clock_now; +} + +static int window_texts(const char *needle) +{ + const struct nk_command *cmd; + int n = 0, len = (int)strlen(needle); + nk_foreach(cmd, &ctx) { + const struct nk_command_text *t = (const struct nk_command_text *)cmd; + if (cmd->type == NK_COMMAND_TEXT && t->length >= len && !strncmp(t->string, needle, (size_t)len)) ++n; + } + return n; +} + +static void test_inspector(void) +{ + struct nka_context *a = nka_create(0); + struct nka__nk_profiler *p; + int open = 1, i; + nka_profiler_begin(a, "ignored"); + CHECK(!nka_profiler_is_enabled(a)); + nka_profiler_enable(a, 1); + nka_profiler_begin_frame(a); + nka_profiler_begin(a, "nothing without a clock"); + nka_profiler_end(a); + p = a->nk_profiler; + CHECK(p && p->count == 0); + nka_profiler_set_clock(a, fake_clock, 0); + for (i = 0; i < 2; ++i) { + nka_profiler_begin_frame(a); + nka_profiler_begin(a, "layout"); + clock_now += 0.002; + nka_profiler_begin(a, "a section with a name longer than thirty-one characters"); + clock_now += 0.001; + nka_profiler_end(a); + nka_profiler_end(a); + nka_profiler_begin(a, "a section with a name longer than thirty-one characters, again"); + clock_now += 0.001; + nka_profiler_end(a); + nka_profiler_end_frame(a); + } + CHECK(p->count == 2 && p->at == 2); + CHECK(NEAR(p->sections[0].ms, 3, 1e-3f) && p->sections[0].calls == 1); + CHECK(NEAR(p->sections[1].ms, 2, 1e-3f) && p->sections[1].calls == 2); + CHECK(NEAR(p->frame_ms, 4, 1e-3f) && NEAR(p->frame[1], 4, 1e-3f) && NEAR(p->sections[1].history[1], 2, 1e-3f)); + + nka_update(a, 0.016f); + nka_tween_float(a, 7, 8, 1, 1, linear, NKA_POLICY_CROSSFADE, 0); + nk_input_begin(&ctx); + nk_input_end(&ctx); + nka_show_unified_inspector(a, &ctx, &open); + CHECK(open && nk_window_find(&ctx, "NukAnim") != 0); + CHECK(window_texts("Time scale") == 2 && window_texts("Moving") == 1 && window_texts("0x00000007") == 1); + nk_clear(&ctx); + open = 0; + nk_input_begin(&ctx); + nk_input_end(&ctx); + nka_show_unified_inspector(a, &ctx, &open); + CHECK(window_texts("Time scale") == 0); + nk_clear(&ctx); + nka_destroy(a); +} + +static int count_commands(enum nk_command_type type) +{ + const struct nk_command *cmd; + int n = 0; + nk_foreach(cmd, &ctx) n += cmd->type == type; + return n; +} + +/* The timeline's background, the first filled rect in its ground color. */ +static struct nk_rect timeline_rect(void) +{ + const struct nk_command *cmd; + nk_foreach(cmd, &ctx) { + const struct nk_command_rect_filled *f = (const struct nk_command_rect_filled *)cmd; + if (cmd->type == NK_COMMAND_RECT_FILLED && f->color.r == 30 && f->color.g == 32 && f->color.b == 40) + return nk_rect(f->x, f->y, f->w, f->h); + } + return nk_rect(0, 0, 0, 0); +} + +static void test_timeline(void) +{ + struct nka_context *a = nka_create(0); + nk_hash clip = nka_id("fade in"), inst; + struct nk_rect r; + float label, x0, w; + nka_clip_begin(a, clip); + nka_clip_key_float(a, nka_id("alpha"), 0, 0, NKA_EASE_OUT_CUBIC, 0); + nka_clip_key_float(a, nka_id("alpha"), 1, 1, NKA_EASE_LINEAR, 0); + nka_clip_key_vec2(a, nka_id("pos"), 0, nk_vec2(0, 0), NKA_EASE_IN_OUT_SINE, 0); + nka_clip_key_vec2(a, nka_id("pos"), 2, nk_vec2(10, 0), NKA_EASE_LINEAR, 0); + nka_clip_set_delay(a, 0.5f); + CHECK(nka_clip_end(a) == NKA_OK); + nka_update(a, 0.016f); + inst = nka_play(a, clip, 9); + CHECK(inst == 9); + nka_update(a, 1.0f); + + nk_input_begin(&ctx); + nk_input_end(&ctx); + if (nk_begin(&ctx, "timeline", nk_rect(0, 0, 400, 300), NK_WINDOW_NO_SCROLLBAR)) { + nka_show_debug_timeline(a, &ctx, inst); + nka_show_debug_timeline(a, &ctx, 12345); + } + nk_end(&ctx); + CHECK(window_texts("Clip 0x") == 1 && window_texts("No instance 0x00003039") == 1); + CHECK(window_texts("float") == 1 && window_texts("vec2") == 1 && window_texts("delay end") == 1); + CHECK(window_texts("0.0s") == 1 && window_texts("2.0s") == 1 && window_texts("2.5s") == 0); + CHECK(count_commands(NK_COMMAND_TRIANGLE_FILLED) == 1 && count_commands(NK_COMMAND_CIRCLE_FILLED) == 4); + r = timeline_rect(); + CHECK(r.w > 300 && r.h == 130); + nk_clear(&ctx); + + label = font.width(font.userdata, font.height, "color rel", 9) + 8; + x0 = r.x + 4 + label; + w = r.w - label - 8; + nk_input_begin(&ctx); + nk_input_motion(&ctx, (int)(x0 + 0.5f / 2.5f * w), (int)(r.y + 44 + 10)); + nk_input_end(&ctx); + if (nk_begin(&ctx, "timeline", nk_rect(0, 0, 400, 300), NK_WINDOW_NO_SCROLLBAR)) + nka_show_debug_timeline(a, &ctx, inst); + nk_end(&ctx); + CHECK(window_texts("0.000s out cubic") == 1); + nk_clear(&ctx); + nka_destroy(a); +} + +static void test_anchors(void) +{ + struct nka_context *a = nka_create(0); + struct nk_vec2 s; + nk_input_begin(&ctx); + nk_input_end(&ctx); + if (nk_begin(&ctx, "anchored", nk_rect(10, 10, 240, 160), 0)) { + nka_anchor_update(a, &ctx); + nk_layout_row_dynamic(&ctx, 20, 1); + CHECK(NEAR(nka_tween_float_rel(a, 1, 2, 0.5f, 0, 1, linear, NKA_POLICY_CUT, NKA_ANCHOR_WINDOW, 0), 120, 1e-4f)); + } + nk_end(&ctx); + nk_clear(&ctx); + s = nka_anchor(a, NKA_ANCHOR_WINDOW); + CHECK(s.x == 240 && s.y == 160); + s = nka_anchor(a, NKA_ANCHOR_WINDOW_CONTENT); + CHECK(s.x > 0 && s.x < 240 && s.y > 0 && s.y < 160); + nka_destroy(a); +} + +int main(void) +{ + font.height = 16; + font.width = text_width; + nk_init_default(&ctx, &font); + test_scroll(0); + test_scroll(1); + test_style(); + test_text(); + test_text_path(); + test_inspector(); + test_timeline(); + test_anchors(); + nk_free(&ctx); + if (failures) printf("%d failed\n", failures); + else printf("nk: ok\n"); + return failures != 0; +} diff --git a/tests/nuklear.c b/tests/nuklear.c new file mode 100644 index 0000000..3a10c4d --- /dev/null +++ b/tests/nuklear.c @@ -0,0 +1,7 @@ +/* Nuklear's implementation on its own, so its warnings stay out of the tests. */ +#ifdef _MSC_VER +#pragma warning(disable: 4116 4127 4701 5287) +#endif +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#define NK_IMPLEMENTATION +#include "nuklear.h" diff --git a/tests/path.c b/tests/path.c new file mode 100644 index 0000000..cd6c824 --- /dev/null +++ b/tests/path.c @@ -0,0 +1,604 @@ +#include + +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#include "nuklear.h" +#define NUKANIM_IMPLEMENTATION +#include "nukanim.h" + +static int failures; + +static void check(int ok, const char *what, int line) +{ + if (ok) return; + printf("path.c:%d: %s\n", line, what); + failures++; +} + +#define CHECK(c) check((c) != 0, #c, __LINE__) +#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps)) + +static const struct nka_ease linear = { NKA_EASE_LINEAR, 0, 0, 0, 0 }; +static const float pi = 3.14159265f; + +static struct nk_vec2 v2(float x, float y) +{ + struct nk_vec2 v; + v.x = x; v.y = y; + return v; +} + +static int near2(struct nk_vec2 p, struct nk_vec2 q, float eps) { return NEAR(p.x, q.x, eps) && NEAR(p.y, q.y, eps); } +static float len2(struct nk_vec2 p) { return sqrtf(p.x * p.x + p.y * p.y); } + +/* The central difference between two points 2h apart. */ +static struct nk_vec2 slope(struct nk_vec2 lo, struct nk_vec2 hi, float h) +{ + return v2((hi.x - lo.x) / (2 * h), (hi.y - lo.y) / (2 * h)); +} + +static void test_curves(void) +{ + struct nk_vec2 p0 = v2(0, 0), p1 = v2(10, 40), p2 = v2(50, -20), p3 = v2(80, 30), q; + const float tension[3] = { 0, 0.5f, 1 }, h = 1e-3f; + int i, k; + + CHECK(near2(nka_bezier_quadratic(p0, p1, p2, 0), p0, 0)); + CHECK(near2(nka_bezier_quadratic(p0, p1, p2, 1), p2, 0)); + q = nka_bezier_quadratic(p0, p1, p2, 0.5f); + CHECK(near2(q, v2((p0.x + 2 * p1.x + p2.x) / 4, (p0.y + 2 * p1.y + p2.y) / 4), 1e-4f)); + CHECK(near2(nka_bezier_cubic(p0, p1, p2, p3, 0), p0, 0)); + CHECK(near2(nka_bezier_cubic(p0, p1, p2, p3, 1), p3, 0)); + q = nka_bezier_cubic(p0, p1, p2, p3, 0.5f); + CHECK(near2(q, v2((p0.x + 3 * p1.x + 3 * p2.x + p3.x) / 8, (p0.y + 3 * p1.y + 3 * p2.y + p3.y) / 8), 1e-4f)); + for (k = 0; k < 3; ++k) { + CHECK(near2(nka_catmull_rom(p0, p1, p2, p3, 0, tension[k]), p1, 1e-4f)); + CHECK(near2(nka_catmull_rom(p0, p1, p2, p3, 1, tension[k]), p2, 1e-4f)); + } + q = nka_catmull_rom(p0, p1, p2, p3, 0.5f, 0); + CHECK(near2(q, v2((-p0.x + 9 * p1.x + 9 * p2.x - p3.x) / 16, (-p0.y + 9 * p1.y + 9 * p2.y - p3.y) / 16), 1e-4f)); + q = nka_catmull_rom_deriv(p0, p1, p2, p3, 0, 0); + CHECK(near2(q, v2((p2.x - p0.x) / 2, (p2.y - p0.y) / 2), 1e-4f)); + CHECK(len2(nka_catmull_rom_deriv(p0, p1, p2, p3, 0, 1)) < 1e-5f); + CHECK(len2(nka_catmull_rom_deriv(p0, p1, p2, p3, 1, 1)) < 1e-5f); + + for (i = 0; i <= 10; ++i) { + float t = (float)i / 10; + q = slope(nka_bezier_quadratic(p0, p1, p2, t - h), nka_bezier_quadratic(p0, p1, p2, t + h), h); + CHECK(near2(nka_bezier_quadratic_deriv(p0, p1, p2, t), q, 0.05f)); + q = slope(nka_bezier_cubic(p0, p1, p2, p3, t - h), nka_bezier_cubic(p0, p1, p2, p3, t + h), h); + CHECK(near2(nka_bezier_cubic_deriv(p0, p1, p2, p3, t), q, 0.05f)); + for (k = 0; k < 3; ++k) { + q = slope(nka_catmull_rom(p0, p1, p2, p3, t - h, tension[k]), nka_catmull_rom(p0, p1, p2, p3, t + h, tension[k]), h); + CHECK(near2(nka_catmull_rom_deriv(p0, p1, p2, p3, t, tension[k]), q, 0.05f)); + } + } +} + +static void circle(struct nka_context *a, nk_hash id, float r) +{ + float k = 0.5522847498f * r; + nka_path_begin(a, id, v2(r, 0)); + nka_path_cubic_to(a, v2(r, k), v2(k, r), v2(0, r)); + nka_path_cubic_to(a, v2(-k, r), v2(-r, k), v2(-r, 0)); + nka_path_cubic_to(a, v2(-r, -k), v2(-k, -r), v2(0, -r)); + nka_path_cubic_to(a, v2(k, -r), v2(r, -k), v2(r, 0)); + nka_path_end(a); +} + +static void test_build(void) +{ + struct nka_context *a = nka_create(0); + nk_hash line = nka_id("line"), ring = nka_id("ring"), tri = nka_id("tri"), cr = nka_id("cr"); + const struct nk_vec2 pts[4] = { { 0, 0 }, { 50, 40 }, { 100, -10 }, { 150, 30 } }; + struct nka__path_data *p; + struct nk_vec2 q; + float acc; + int i; + + CHECK(!nka_path_exists(a, line) && nka_path_length(a, line) == 0); + CHECK(near2(nka_path_evaluate(a, line, 0.5f), v2(0, 0), 0)); + CHECK(near2(nka_path_tangent(a, line, 0.5f), v2(1, 0), 0) && nka_path_angle(a, line, 0.5f) == 0); + nka_path_line_to(a, v2(1, 1)); + nka_path_end(a); + CHECK(a->path_map.len == 0); + + nka_path_begin(a, line, v2(10, 20)); + nka_path_line_to(a, v2(40, 60)); + CHECK(!nka_path_exists(a, line)); + nka_path_end(a); + CHECK(nka_path_exists(a, line)); + CHECK(nka_path_length(a, line) == 50); + CHECK(near2(nka_path_evaluate(a, line, 0), v2(10, 20), 0)); + CHECK(near2(nka_path_evaluate(a, line, 1), v2(40, 60), 0)); + CHECK(near2(nka_path_evaluate(a, line, 0.5f), v2(25, 40), 1e-4f)); + CHECK(near2(nka_path_evaluate(a, line, -3), v2(10, 20), 0) && near2(nka_path_evaluate(a, line, 7), v2(40, 60), 0)); + CHECK(near2(nka_path_tangent(a, line, 0.3f), v2(0.6f, 0.8f), 1e-6f)); + nka_path_build_arc_lut(a, line, 16); + nka_path_begin(a, line, v2(0, 0)); + nka_path_line_to(a, v2(0, 10)); + CHECK(nka_path_length(a, line) == 50 && nka_path_has_arc_lut(a, line)); + nka_path_end(a); + CHECK(nka_path_length(a, line) == 10 && !nka_path_has_arc_lut(a, line)); + CHECK(near2(nka_path_evaluate(a, line, 1), v2(0, 10), 0) && a->path_map.len == 1); + + circle(a, ring, 100); + CHECK(NEAR(nka_path_length(a, ring), 2 * pi * 100, 0.5f)); + for (i = 0; i <= 16; ++i) { + float t = (float)i / 16; + q = nka_path_evaluate(a, ring, t); + CHECK(NEAR(len2(q), 100, 0.05f)); + CHECK(NEAR(nka_path_angle(a, ring, t), atan2f(q.x, -q.y), 2e-3f) || NEAR(fabsf(atan2f(q.x, -q.y)), pi, 2e-3f)); + } + CHECK(near2(nka_path_evaluate(a, ring, 0.125f), v2(70.7107f, 70.7107f), 0.05f)); + + nka_path_begin(a, tri, v2(0, 0)); + nka_path_line_to(a, v2(30, 0)); + nka_path_line_to(a, v2(30, 40)); + nka_path_close(a); + nka_path_close(a); + nka_path_end(a); + p = nka__path_get(a, tri); + CHECK(p && p->n == 3); + CHECK(near2(nka_path_evaluate(a, tri, 1), v2(0, 0), 0)); + CHECK(NEAR(nka_path_length(a, tri), 120, 1e-3f)); + + nka_path_begin(a, cr, pts[0]); + for (i = 1; i < 4; ++i) nka_path_catmull_to(a, pts[i], 0.5f); + nka_path_end(a); + p = nka__path_get(a, cr); + CHECK(p && p->n == 3); + if (p) { + CHECK(near2(p->segs[0].p[0], pts[0], 0) && near2(p->segs[0].p[1], pts[0], 0)); + CHECK(near2(p->segs[0].p[2], pts[1], 0) && near2(p->segs[0].p[3], pts[2], 0)); + CHECK(near2(p->segs[1].p[0], pts[0], 0) && near2(p->segs[1].p[3], pts[3], 0)); + CHECK(near2(p->segs[2].p[0], pts[1], 0) && near2(p->segs[2].p[2], pts[3], 0) && near2(p->segs[2].p[3], pts[3], 0)); + acc = 0; + for (i = 0; i < 3; ++i) { + acc += p->segs[i].length; + CHECK(near2(nka_path_evaluate(a, cr, acc / p->seg_len), pts[i + 1], 1e-3f)); + } + } + + nka_path_begin(a, nka_id("lost"), v2(0, 0)); + nka_path_line_to(a, v2(5, 0)); + nka_path_begin(a, nka_id("kept"), v2(1, 2)); + nka_path_end(a); + CHECK(!nka_path_exists(a, nka_id("lost")) && nka_path_exists(a, nka_id("kept"))); + CHECK(near2(nka_path_evaluate(a, nka_id("kept"), 0.5f), v2(1, 2), 0) && nka_path_length(a, nka_id("kept")) == 0); + + nka_path_begin(a, 0, v2(0, 0)); + nka_path_line_to(a, v2(10, 0)); + nka_path_end(a); + nka_path_begin(a, 1, v2(0, 0)); + nka_path_line_to(a, v2(20, 0)); + nka_path_end(a); + CHECK(nka_path_length(a, 0) == 10 && nka_path_length(a, 1) == 20); + nka_destroy(a); +} + +static void test_tangents(void) +{ + struct nka_context *a = nka_create(0); + nk_hash ring = nka_id("ring"), cusp = nka_id("cusp"), dot = nka_id("dot"), stop = nka_id("stop"); + struct nk_vec2 q, d; + int i; + + circle(a, ring, 100); + for (i = 0; i <= 40; ++i) { + float t = (float)i / 40; + d = nka_path_tangent(a, ring, t); + q = nka_path_evaluate(a, ring, t); + CHECK(NEAR(len2(d), 1, 1e-5f)); + CHECK(nka_path_angle(a, ring, t) == atan2f(d.y, d.x)); + CHECK(NEAR(q.x * d.x + q.y * d.y, 0, 1)); + CHECK(q.x * d.y - q.y * d.x > 0); + } + + nka_path_begin(a, cusp, v2(0, 0)); + nka_path_cubic_to(a, v2(0, 0), v2(0, 50), v2(50, 50)); + nka_path_end(a); + CHECK(near2(nka_path_tangent(a, cusp, 0), v2(0, 1), 1e-3f)); + CHECK(NEAR(nka_path_angle(a, cusp, 0), pi / 2, 1e-3f)); + CHECK(near2(nka_path_tangent(a, cusp, 1), v2(1, 0), 1e-6f)); + + nka_path_begin(a, stop, v2(0, 0)); + nka_path_catmull_to(a, v2(0, 40), 1); + nka_path_catmull_to(a, v2(40, 40), 1); + nka_path_end(a); + CHECK(near2(nka_path_tangent(a, stop, 0), v2(0, 1), 1e-3f)); + CHECK(near2(nka_path_tangent(a, stop, 1), v2(1, 0), 1e-3f)); + + nka_path_begin(a, dot, v2(5, 5)); + nka_path_line_to(a, v2(5, 5)); + nka_path_end(a); + CHECK(near2(nka_path_tangent(a, dot, 0.5f), v2(1, 0), 0) && nka_path_angle(a, dot, 0.5f) == 0); + CHECK(nka_path_length(a, dot) == 0 && near2(nka_path_evaluate(a, dot, 0.5f), v2(5, 5), 0)); + nka_path_build_arc_lut(a, dot, 64); + CHECK(!nka_path_has_arc_lut(a, dot)); + nka_destroy(a); +} + +static void uneven(struct nka_context *a, nk_hash id) +{ + nka_path_begin(a, id, v2(0, 0)); + nka_path_line_to(a, v2(50, 0)); + nka_path_cubic_to(a, v2(140, 0), v2(145, 0), v2(150, 0)); + nka_path_end(a); +} + +static void test_arc_length(void) +{ + struct nka_context *a = nka_create(0); + nk_hash id = nka_id("uneven"), ring = nka_id("ring"), none = nka_id("none"); + struct nk_vec2 before[11], q, d; + int i; + + uneven(a, id); + CHECK(!nka_path_has_arc_lut(a, id)); + CHECK(NEAR(nka_path_length(a, id), 150, 1e-3f)); + CHECK(fabsf(nka_path_evaluate(a, id, 0.4f).x - 60) > 5); + CHECK(NEAR(nka_path_distance_to_t(a, id, 75), 0.5f, 1e-6f)); + CHECK(nka_path_distance_to_t(a, id, -5) == 0 && nka_path_distance_to_t(a, id, 500) == 1); + for (i = 0; i <= 10; ++i) before[i] = nka_path_evaluate(a, id, (float)i / 10); + + nka_path_build_arc_lut(a, id, 256); + CHECK(nka_path_has_arc_lut(a, id) && nka__path_get(a, id)->lut_n == 257); + CHECK(NEAR(nka_path_length(a, id), 150, 1e-2f)); + for (i = 0; i <= 10; ++i) CHECK(near2(nka_path_evaluate(a, id, (float)i / 10), before[i], 0)); + /* The table steps evenly in t, so the step across the corner at 50 interpolates two speeds. */ + for (i = 0; i <= 30; ++i) { + float dist = (float)i * 5; + q = nka_path_evaluate_at_distance(a, id, dist); + CHECK(NEAR(q.x, dist, fabsf(dist - 50) < 2 ? 0.25f : 0.01f) && q.y == 0); + CHECK(near2(nka_path_tangent_at_distance(a, id, dist), v2(1, 0), 1e-6f)); + CHECK(nka_path_angle_at_distance(a, id, dist) == 0); + } + for (i = 0; i <= 300; ++i) CHECK(NEAR(nka_path_evaluate_at_distance(a, id, (float)i / 2).x, (float)i / 2, 0.25f)); + nka_path_begin(a, nka_id("smooth"), v2(0, 0)); + nka_path_cubic_to(a, v2(90, 0), v2(95, 0), v2(100, 0)); + nka_path_end(a); + CHECK(fabsf(nka_path_evaluate(a, nka_id("smooth"), 0.5f).x - 50) > 30); + nka_path_build_arc_lut(a, nka_id("smooth"), 256); + for (i = 0; i <= 200; ++i) + CHECK(NEAR(nka_path_evaluate_at_distance(a, nka_id("smooth"), (float)i / 2).x, (float)i / 2, 0.01f)); + CHECK(near2(nka_path_evaluate_at_distance(a, id, -10), v2(0, 0), 0)); + CHECK(near2(nka_path_evaluate_at_distance(a, id, 1000), v2(150, 0), 1e-4f)); + CHECK(nka_path_distance_to_t(a, id, -1) == 0 && nka_path_distance_to_t(a, id, 1e6f) == 1); + CHECK(NEAR(nka_path_distance_to_t(a, id, 25), 25.0f / 150, 1e-3f)); + + circle(a, ring, 100); + for (i = 0; i <= 10; ++i) before[i] = nka_path_evaluate(a, ring, (float)i / 10); + nka_path_build_arc_lut(a, ring, 0); + CHECK(nka__path_get(a, ring)->lut_n == 65); + nka_path_build_arc_lut(a, ring, 512); + CHECK(NEAR(nka_path_length(a, ring), 628.4f, 0.02f)); + for (i = 0; i <= 10; ++i) CHECK(near2(nka_path_evaluate(a, ring, (float)i / 10), before[i], 0)); + for (i = 0; i < 24; ++i) { + float dist = nka_path_length(a, ring) * (float)i / 24, turn = 2 * pi * (float)i / 24; + q = nka_path_evaluate_at_distance(a, ring, dist); + d = nka_path_tangent_at_distance(a, ring, dist); + CHECK(near2(v2(q.x / len2(q), q.y / len2(q)), v2(cosf(turn), sinf(turn)), 2e-3f)); + CHECK(NEAR(len2(d), 1, 1e-5f)); + CHECK(nka_path_angle_at_distance(a, ring, dist) == atan2f(d.y, d.x)); + } + + uneven(a, id); + CHECK(!nka_path_has_arc_lut(a, id) && NEAR(nka_path_length(a, id), 150, 1e-3f)); + + nka_path_build_arc_lut(a, none, 64); + CHECK(!nka_path_has_arc_lut(a, none) && nka_path_distance_to_t(a, none, 5) == 0); + CHECK(near2(nka_path_evaluate_at_distance(a, none, 5), v2(0, 0), 0)); + CHECK(near2(nka_path_tangent_at_distance(a, none, 5), v2(1, 0), 0) && nka_path_angle_at_distance(a, none, 5) == 0); + nka_destroy(a); +} + +static void test_tweens(void) +{ + struct nka_context *a = nka_create(0); + nk_hash id = nka_id("mover"), ch = nka_id("pos"), path = nka_id("L"), flat = nka_id("flat"); + struct nk_vec2 q; + int i; + + nka_update(a, 0.016f); + q = nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE); + CHECK(near2(q, v2(0, 0), 0) && a->chans.len == 0 && !nka_busy(a)); + CHECK(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE) == 0 && a->chans.len == 0); + + nka_path_begin(a, path, v2(0, 0)); + nka_path_line_to(a, v2(100, 0)); + nka_path_line_to(a, v2(100, 100)); + nka_path_end(a); + + CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(0, 0), 0) && nka_busy(a)); + CHECK(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE) == 0); + nka_update(a, 0.25f); + CHECK(!nka_busy(a)); + CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(50, 0), 1e-3f) && nka_busy(a)); + CHECK(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE) == 0); + nka_update(a, 0.5f); + CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(100, 50), 1e-3f)); + CHECK(NEAR(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), pi / 2, 1e-6f)); + nka_update(a, 0.25f); + CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(100, 100), 0)); + nka_update(a, 0.25f); + CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(100, 100), 0)); + CHECK(NEAR(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), pi / 2, 1e-6f)); + CHECK(!nka_busy(a)); + + /* As upstream: a cut still animates, and a new ease does not start over. */ + CHECK(near2(nka_tween_path(a, id, nka_id("cut"), path, 1, linear, NKA_POLICY_CUT), v2(0, 0), 0) && nka_busy(a)); + nka_tween_path(a, id, nka_id("ease"), path, 1, linear, NKA_POLICY_CROSSFADE); + nka_update(a, 0.5f); + q = nka_tween_path(a, id, nka_id("ease"), path, 1, nka_ease(NKA_EASE_IN_QUAD), NKA_POLICY_CROSSFADE); + CHECK(near2(q, v2(100, 0), 1e-3f)); + nka_update(a, 0.25f); + q = nka_tween_path(a, id, nka_id("ease"), path, 1, nka_ease(NKA_EASE_IN_QUAD), NKA_POLICY_CROSSFADE); + CHECK(near2(q, v2(100, 50), 1e-3f)); + + uneven(a, flat); + nka_tween_path(a, id, nka_id("raw"), flat, 1, linear, NKA_POLICY_CROSSFADE); + nka_update(a, 0.5f); + q = nka_tween_path(a, id, nka_id("raw"), flat, 1, linear, NKA_POLICY_CROSSFADE); + CHECK(q.x > 100); + nka_path_build_arc_lut(a, flat, 256); + q = nka_tween_path(a, id, nka_id("raw"), flat, 1, linear, NKA_POLICY_CROSSFADE); + CHECK(NEAR(q.x, 75, 0.02f)); + + circle(a, nka_id("ring"), 100); + nka_path_build_arc_lut(a, nka_id("ring"), 256); + nka_tween_path_angle(a, id, nka_id("spin"), nka_id("ring"), 1, linear, NKA_POLICY_CROSSFADE); + nka_update(a, 0.25f); + CHECK(NEAR(fabsf(nka_tween_path_angle(a, id, nka_id("spin"), nka_id("ring"), 1, linear, NKA_POLICY_CROSSFADE)), pi, 2e-3f)); + nka_update(a, 0.25f); + CHECK(NEAR(nka_tween_path_angle(a, id, nka_id("spin"), nka_id("ring"), 1, linear, NKA_POLICY_CROSSFADE), -pi / 2, 2e-3f)); + + for (i = 0; i < 10; ++i) nka_update(a, 0.1f); + nka_gc(a, 5); + CHECK(a->chans.len == 0); + CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(0, 0), 0) && nka_busy(a)); + nka_clear(a); + CHECK(nka_path_exists(a, path) && nka_path_has_arc_lut(a, flat)); + nka_destroy(a); +} + +static void test_morph(void) +{ + struct nka_context *a = nka_create(0); + nk_hash pa = nka_id("a"), pb = nka_id("b"), sq = nka_id("square"), ring = nka_id("ring"), bare = nka_id("bare"); + struct nka_morph_opts o = nka_morph_opts_default(); + volatile float zero = 0; + struct nk_vec2 q; + int i; + + CHECK(o.samples == 64 && o.match_endpoints && o.use_arc_length); + nka_path_begin(a, pa, v2(0, 0)); + nka_path_line_to(a, v2(100, 0)); + nka_path_end(a); + nka_path_begin(a, pb, v2(0, 100)); + nka_path_line_to(a, v2(30, 100)); + nka_path_line_to(a, v2(100, 100)); + nka_path_end(a); + nka_path_morph(a, pa, pb, 0.5f, 0, 0); + CHECK(nka_path_has_arc_lut(a, pa) && !nka_path_has_arc_lut(a, pb)); + for (i = 0; i <= 10; ++i) { + float t = (float)i / 10; + CHECK(near2(nka_path_morph(a, pa, pb, t, 0, 0), nka_path_evaluate_at_distance(a, pa, t * nka_path_length(a, pa)), 0)); + CHECK(near2(nka_path_morph(a, pa, pb, t, 1, 0), nka_path_evaluate_at_distance(a, pb, t * nka_path_length(a, pb)), 0)); + CHECK(near2(nka_path_morph(a, pa, pb, t, -2, 0), nka_path_morph(a, pa, pb, t, 0, 0), 0)); + CHECK(near2(nka_path_morph(a, pa, pb, t, 0.5f, 0), v2(100 * t, 50), 1e-3f)); + CHECK(near2(nka_path_morph(a, pa, pb, t, 0.25f, &o), v2(100 * t, 25), 1e-3f)); + CHECK(near2(nka_path_morph_tangent(a, pa, pb, t, 0.5f, 0), v2(1, 0), 1e-4f)); + CHECK(NEAR(nka_path_morph_angle(a, pa, pb, t, 0.5f, 0), 0, 1e-4f)); + CHECK(near2(nka_path_morph_tangent(a, pa, pb, t, 0, 0), nka_path_tangent_at_distance(a, pa, t * nka_path_length(a, pa)), 0)); + } + CHECK(nka_path_has_arc_lut(a, pa) && nka_path_has_arc_lut(a, pb)); + + /* A blend leaving 0 or reaching 1 does not jump, even where t is far from arc length. */ + nka_path_begin(a, nka_id("smooth"), v2(0, 0)); + nka_path_cubic_to(a, v2(90, 0), v2(95, 0), v2(100, 0)); + nka_path_end(a); + for (i = 0; i <= 10; ++i) { + float t = (float)i / 10; + CHECK(near2(nka_path_morph(a, nka_id("smooth"), pb, t, 0, 0), nka_path_morph(a, nka_id("smooth"), pb, t, 1e-4f, 0), 0.05f)); + CHECK(near2(nka_path_morph(a, pb, nka_id("smooth"), t, 1, 0), nka_path_morph(a, pb, nka_id("smooth"), t, 1 - 1e-4f, 0), 0.05f)); + } + o.use_arc_length = 0; + for (i = 0; i <= 10; ++i) { + float t = (float)i / 10; + CHECK(near2(nka_path_morph(a, nka_id("smooth"), pb, t, 0, &o), nka_path_evaluate(a, nka_id("smooth"), t), 0)); + CHECK(near2(nka_path_morph_tangent(a, nka_id("smooth"), pb, t, 0, &o), nka_path_tangent(a, nka_id("smooth"), t), 0)); + } + o = nka_morph_opts_default(); + + nka_path_begin(a, sq, v2(50, -50)); + nka_path_line_to(a, v2(50, 50)); + nka_path_line_to(a, v2(-50, 50)); + nka_path_line_to(a, v2(-50, -50)); + nka_path_close(a); + nka_path_end(a); + circle(a, ring, 70); + o.samples = 32; + nka_path_morph(a, sq, ring, 0.5f, 0.5f, &o); + CHECK(nka__path_get(a, sq)->lut_n == 65 && nka__path_get(a, ring)->lut_n == 65); + for (i = 0; i < 32; i += 3) { + float t = (float)i / 31; + struct nk_vec2 x = nka_path_evaluate_at_distance(a, sq, t * nka_path_length(a, sq)); + struct nk_vec2 y = nka_path_evaluate_at_distance(a, ring, t * nka_path_length(a, ring)); + q = nka_path_morph(a, sq, ring, t, 0.5f, &o); + CHECK(near2(q, v2((x.x + y.x) / 2, (x.y + y.y) / 2), 1e-3f)); + } + CHECK(near2(nka_path_morph(a, sq, ring, 0, 0.5f, &o), v2(60, -25), 1e-3f)); + CHECK(near2(nka_path_morph(a, sq, ring, 1, 0.5f, &o), v2(60, -25), 1e-3f)); + + o.use_arc_length = 0; + nka_path_begin(a, bare, v2(0, 0)); + nka_path_quadratic_to(a, v2(50, 100), v2(100, 0)); + nka_path_end(a); + q = nka_path_morph(a, bare, pa, 0.5f, 0.5f, &o); + CHECK(!nka_path_has_arc_lut(a, bare)); + CHECK(near2(q, v2(50, 25), 0.1f)); + + o = nka_morph_opts_default(); + o.samples = 1; + CHECK(near2(nka_path_morph(a, pa, pb, 0.5f, 0.5f, &o), v2(50, 50), 1e-3f)); + o.samples = -5; + CHECK(near2(nka_path_morph(a, pa, pb, 0.25f, 0.5f, &o), v2(25, 50), 1e-3f)); + CHECK(near2(nka_path_morph(a, pa, nka_id("missing"), 0.5f, 0.5f, 0), v2(25, 0), 1e-3f)); + CHECK(near2(nka_path_morph(a, pa, pb, zero / zero, 0.5f, 0), v2(0, 50), 0)); + CHECK(near2(nka_path_morph(a, pa, pb, 0.5f, zero / zero, 0), v2(50, 0), 0)); + nka_destroy(a); +} + +static void test_morph_tween(void) +{ + struct nka_context *a = nka_create(0); + nk_hash pa = nka_id("a"), pb = nka_id("b"), id = nka_id("mt"), ch = nka_id("c"); + struct nk_vec2 q; + + nka_path_begin(a, pa, v2(0, 0)); + nka_path_line_to(a, v2(100, 0)); + nka_path_end(a); + nka_path_begin(a, pb, v2(0, 100)); + nka_path_cubic_to(a, v2(30, 100), v2(70, 100), v2(100, 100)); + nka_path_end(a); + + nka_update(a, 0.016f); + CHECK(nka_get_morph_blend(a, id, ch) == 0); + q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0); + CHECK(near2(q, v2(0, 0), 0) && nka_busy(a) && nka_get_morph_blend(a, id, ch) == 0); + nka_update(a, 0.5f); + q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0); + CHECK(NEAR(nka_get_morph_blend(a, id, ch), 0.5f, 1e-5f)); + CHECK(near2(q, v2(50, 50), 0.01f)); + nka_update(a, 0.25f); + q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0); + CHECK(NEAR(nka_get_morph_blend(a, id, ch), 0.75f, 1e-5f) && near2(q, v2(75, 75), 0.01f)); + nka_update(a, 0.25f); + q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0); + CHECK(nka_get_morph_blend(a, id, ch) == 1 && near2(q, v2(100, 100), 0)); + nka_update(a, 0.1f); + q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0); + CHECK(near2(q, v2(100, 100), 0) && !nka_busy(a)); + + nka_tween_path_morph(a, id, ch, pa, pb, 0, 1, linear, linear, NKA_POLICY_CROSSFADE, 0); + nka_update(a, 0.5f); + q = nka_tween_path_morph(a, id, ch, pa, pb, 0, 1, linear, linear, NKA_POLICY_CROSSFADE, 0); + CHECK(NEAR(nka_get_morph_blend(a, id, ch), 0.5f, 1e-5f) && near2(q, v2(100, 50), 1e-3f) && nka_busy(a)); + q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CUT, 0); + CHECK(nka_get_morph_blend(a, id, ch) == 1 && near2(q, v2(100, 100), 0)); + nka_update(a, 2); + q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CUT, 0); + CHECK(near2(q, v2(100, 100), 0) && !nka_busy(a)); + + q = nka_tween_path_morph(a, id, nka_id("cut"), pa, pb, 0.25f, 1, linear, linear, NKA_POLICY_CUT, 0); + CHECK(nka_get_morph_blend(a, id, nka_id("cut")) == 0.25f && near2(q, v2(0, 25), 1e-3f) && nka_busy(a)); + CHECK(nka_get_morph_blend(a, id, nka_id("none")) == 0 && nka_get_morph_blend(0, id, ch) == 0); + nka_destroy(a); +} + +static void test_quads(void) +{ + struct nk_vec2 quad[4], glyph[4]; + int i; + + nka_make_glyph_quad(quad, v2(10, 20), 0, 8, 12, 0); + CHECK(near2(quad[0], v2(6, 20), 1e-5f) && near2(quad[1], v2(14, 20), 1e-5f)); + CHECK(near2(quad[2], v2(14, 8), 1e-5f) && near2(quad[3], v2(6, 8), 1e-5f)); + nka_make_glyph_quad(quad, v2(10, 20), 0, 8, 12, 3); + CHECK(near2(quad[0], v2(6, 17), 1e-5f) && near2(quad[2], v2(14, 5), 1e-5f)); + nka_make_glyph_quad(glyph, v2(0, 0), pi / 2, 8, 12, 0); + CHECK(near2(glyph[0], v2(0, -4), 1e-5f) && near2(glyph[1], v2(0, 4), 1e-5f)); + CHECK(near2(glyph[2], v2(12, 4), 1e-5f) && near2(glyph[3], v2(12, -4), 1e-5f)); + nka_make_glyph_quad(quad, v2(0, 0), 0, 8, 12, 0); + nka_transform_quad(quad, v2(0, 0), pi / 2, v2(0, 0)); + for (i = 0; i < 4; ++i) CHECK(near2(quad[i], glyph[i], 1e-5f)); + + quad[0] = v2(0, 0); quad[1] = v2(10, 0); quad[2] = v2(10, 10); quad[3] = v2(0, 10); + nka_transform_quad(quad, v2(5, 5), pi / 2, v2(100, 50)); + CHECK(near2(quad[0], v2(110, 50), 1e-4f) && near2(quad[1], v2(110, 60), 1e-4f)); + CHECK(near2(quad[2], v2(100, 60), 1e-4f) && near2(quad[3], v2(100, 50), 1e-4f)); + nka_transform_quad(quad, v2(0, 0), 0, v2(-100, -50)); + CHECK(near2(quad[0], v2(10, 0), 1e-4f) && near2(quad[2], v2(0, 10), 1e-4f)); + nka_transform_quad(0, v2(0, 0), 1, v2(0, 0)); + nka_make_glyph_quad(0, v2(0, 0), 1, 1, 1, 1); +} + +static int live; + +static void *count_alloc(nk_handle h, void *old, nk_size n) +{ + (void)h; (void)old; + live++; + return malloc(n); +} + +static void count_free(nk_handle h, void *p) +{ + (void)h; + live--; + free(p); +} + +static void wave(struct nka_context *a, nk_hash id, int n) +{ + int i; + nka_path_begin(a, id, v2(0, 0)); + for (i = 0; i < n; ++i) nka_path_quadratic_to(a, v2((float)i * 20 + 10, i & 1 ? 20.0f : -20.0f), v2((float)i * 20 + 20, 0)); + nka_path_end(a); +} + +static void frame(struct nka_context *a) +{ + nka_update(a, 0.016f); + wave(a, 1, 20); + nka_path_build_arc_lut(a, 1, 128); + wave(a, 2, 5); + nka_path_morph(a, 1, 2, 0.5f, 0.5f, 0); + nka_tween_path(a, 7, 7, 1, 1, linear, NKA_POLICY_CROSSFADE); + nka_tween_path_morph(a, 7, 8, 1, 2, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0); +} + +static void test_memory(void) +{ + struct nk_allocator al; + struct nka_context *a; + int i, settled; + al.userdata.ptr = 0; + al.alloc = count_alloc; + al.free = count_free; + a = nka_create(&al); + for (i = 0; i < 3; ++i) frame(a); + settled = live; + for (i = 0; i < 100; ++i) frame(a); + CHECK(live == settled); + CHECK(nka_path_exists(a, 1) && nka_path_has_arc_lut(a, 1) && nka_path_has_arc_lut(a, 2)); + wave(a, 1, 300); + wave(a, 1, 3); + nka_path_build_arc_lut(a, 1, 4000); + CHECK(nka__path_get(a, 1)->n == 3); + nka_path_begin(a, 9, v2(0, 0)); + nka_path_line_to(a, v2(1, 0)); + nka_destroy(a); + CHECK(live == 0); + nka_path_begin(0, 1, v2(0, 0)); + nka_path_line_to(0, v2(1, 0)); + nka_path_end(0); + CHECK(!nka_path_exists(0, 1) && near2(nka_path_morph(0, 1, 2, 0.5f, 0.5f, 0), v2(0, 0), 0)); + CHECK(near2(nka_tween_path(0, 1, 1, 1, 1, linear, NKA_POLICY_CROSSFADE), v2(0, 0), 0)); +} + +int main(void) +{ + test_curves(); + test_build(); + test_tangents(); + test_arc_length(); + test_tweens(); + test_morph(); + test_morph_tween(); + test_quads(); + test_memory(); + if (failures) printf("%d failed\n", failures); + else printf("path: ok\n"); + return failures != 0; +} diff --git a/tools/gif.py b/tools/gif.py new file mode 100644 index 0000000..0731dde --- /dev/null +++ b/tools/gif.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""gif.py CLIPS OUTDIR SCENE... + +Runs the clips example for each scene and turns its raw frames into +OUTDIR/SCENE.gif. Needs Pillow. Run from the repository's root. +""" +import os +import struct +import subprocess +import sys +import tempfile + +from PIL import Image + + +def frames(path): + with open(path, "rb") as f: + magic, w, h, ms = struct.unpack("<4I", f.read(16)) + if magic != 0x464B414E: + sys.exit(f"{path}: not a clips file") + size = w * h * 4 + while True: + data = f.read(size) + if len(data) < size: + return + yield ms, Image.frombytes("RGBA", (w, h), data).convert("RGB") + + +def main(argv): + if len(argv) < 3: + sys.exit(__doc__) + clips, outdir, scenes = argv[0], argv[1], argv[2:] + os.makedirs(outdir, exist_ok=True) + for scene in scenes: + with tempfile.TemporaryDirectory() as tmp: + raw = os.path.join(tmp, scene + ".raw") + subprocess.run([clips, scene, raw], check=True) + images, ms = [], 30 + for ms, image in frames(raw): + images.append(image) + picks = images[:: max(1, len(images) // 16)] + w, h = picks[0].size + mosaic = Image.new("RGB", (w, h * len(picks))) + for k, image in enumerate(picks): + mosaic.paste(image, (0, k * h)) + palette = mosaic.quantize(colors=255, method=Image.Quantize.MEDIANCUT) + quantized = [image.quantize(palette=palette, dither=Image.Dither.NONE) for image in images] + out = os.path.join(outdir, scene + ".gif") + quantized[0].save(out, save_all=True, append_images=quantized[1:], duration=ms, loop=0, optimize=True) + print(f"{out}: {len(images)} frames, {os.path.getsize(out) // 1024} KiB") + + +if __name__ == "__main__": + main(sys.argv[1:])