|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// SPDX-FileCopyrightText: 2026 hyperpolymath |
| 3 | +// |
| 4 | +// PixiUI.affine — bindings for the `@pixi/ui` v2.x npm library |
| 5 | +// (bindings #3 in docs/bindings-roadmap.adoc). |
| 6 | +// |
| 7 | +// `@pixi/ui` provides interactive UI components built on top of |
| 8 | +// PixiJS — Button, FancyButton, Switch, Slider, Input, ScrollBox. |
| 9 | +// Reference consumer: idaptik's `src/bindings/PixiUI.res`, the |
| 10 | +// HUD + menu layer over the core PixiJS scene graph. |
| 11 | +// |
| 12 | +// MVP coverage (this version): Button, FancyButton, Slider, Switch |
| 13 | +// constructors + their typical event callback + the upcast to |
| 14 | +// `Container` (so a UI component can be added to the PixiJS scene |
| 15 | +// graph via `Pixi::pixiContainerAddChild`). |
| 16 | +// |
| 17 | +// Deferred (follow-ups): |
| 18 | +// - Input — text-entry, focus, blur, value accessor |
| 19 | +// - ScrollBox — scroll position, content add/remove, viewport |
| 20 | +// - Full event surface (onHover / onOut / onDown / onUp) |
| 21 | +// - FancyButton style accessors (textures per state) |
| 22 | +// - Slider min/max/step accessors |
| 23 | +// |
| 24 | +// Targets the Deno-ESM backend. Consumer (or its host wrapper) sets |
| 25 | +// `globalThis.__as_pixi_ui` to the `@pixi/ui` namespace at module- |
| 26 | +// init time: |
| 27 | +// |
| 28 | +// import * as PixiUI from "@pixi/ui"; |
| 29 | +// globalThis.__as_pixi_ui = PixiUI; |
| 30 | +// |
| 31 | +// Tests mock the same shape — see `tests/codegen-deno/pixiui_smoke.*`. |
| 32 | +// |
| 33 | +// @pixi/ui type hierarchy: Button, FancyButton, Slider, Switch are |
| 34 | +// all `Container` subclasses (via PixiJS). AffineScript has no |
| 35 | +// subtype polymorphism, so the binding exposes explicit upcast |
| 36 | +// functions (`pixiUiButtonAsContainer`, etc.). These are zero-cost |
| 37 | +// identity lowerings; the underlying JS value is the same object. |
| 38 | +// The `Container` type is re-declared as an opaque extern here for |
| 39 | +// parity with `stdlib/Pixi.affine` — the JS-side handle for the two |
| 40 | +// is identical (a PIXI.Container instance). |
| 41 | + |
| 42 | +module PixiUI; |
| 43 | + |
| 44 | +use Deno::{Json}; |
| 45 | + |
| 46 | +// ── Opaque host types ────────────────────────────────────────────── |
| 47 | +// |
| 48 | +// Each maps to its same-named `@pixi/ui` class. They're treated |
| 49 | +// opaquely at the AS boundary; the only consumer-visible operations |
| 50 | +// are construction, callback registration, and upcast to Container. |
| 51 | + |
| 52 | +pub extern type Button; |
| 53 | +pub extern type FancyButton; |
| 54 | +pub extern type Slider; |
| 55 | +pub extern type Switch; |
| 56 | + |
| 57 | +/// `PIXI.Container` — re-declared as opaque extern so this module can |
| 58 | +/// be consumed without a hard dependency on `stdlib/Pixi.affine`. |
| 59 | +/// When both modules are imported, the underlying JS values coincide |
| 60 | +/// (both resolve to the same `PIXI.Container` host class), so a |
| 61 | +/// container produced here can be added as a child of an |
| 62 | +/// `Application.stage` from `Pixi.affine` with no further marshalling. |
| 63 | +pub extern type Container; |
| 64 | + |
| 65 | +// ── Button ───────────────────────────────────────────────────────── |
| 66 | + |
| 67 | +/// `new PixiUI.Button(options)`. |
| 68 | +/// `options` is the @pixi/ui Button-options JSON (typically `{ view }` |
| 69 | +/// where `view` is a PIXI display object). |
| 70 | +pub extern fn pixiUiButtonNew(options: Json) -> Button; |
| 71 | + |
| 72 | +/// `button.onPress.connect(callback)` — register a press-event |
| 73 | +/// handler. `callback` crosses the boundary as opaque `Json` (the |
| 74 | +/// host-side JS function value). Returns 0. |
| 75 | +pub extern fn pixiUiButtonOnPress(b: Button, callback: Json) -> Int; |
| 76 | + |
| 77 | +/// Upcast a Button to its Container superclass. Zero-cost identity |
| 78 | +/// lowering — the underlying JS value is the same object. |
| 79 | +pub extern fn pixiUiButtonAsContainer(b: Button) -> Container; |
| 80 | + |
| 81 | +// ── FancyButton ──────────────────────────────────────────────────── |
| 82 | + |
| 83 | +/// `new PixiUI.FancyButton(options)`. `options` is the @pixi/ui |
| 84 | +/// FancyButton-options JSON (text + textures-per-state + padding). |
| 85 | +pub extern fn pixiUiFancyButtonNew(options: Json) -> FancyButton; |
| 86 | + |
| 87 | +/// Upcast a FancyButton to its Container superclass. Zero-cost |
| 88 | +/// identity lowering. |
| 89 | +pub extern fn pixiUiFancyButtonAsContainer(b: FancyButton) -> Container; |
| 90 | + |
| 91 | +// ── Slider ───────────────────────────────────────────────────────── |
| 92 | + |
| 93 | +/// `new PixiUI.Slider(options)`. `options` is the @pixi/ui |
| 94 | +/// Slider-options JSON (min/max/value/bg/fill/slider textures). |
| 95 | +pub extern fn pixiUiSliderNew(options: Json) -> Slider; |
| 96 | + |
| 97 | +/// `slider.onUpdate.connect(callback)` — register a value-change |
| 98 | +/// handler. Callback receives the new value (number). Returns 0. |
| 99 | +pub extern fn pixiUiSliderOnUpdate(s: Slider, callback: Json) -> Int; |
| 100 | + |
| 101 | +/// Upcast a Slider to its Container superclass. Zero-cost identity |
| 102 | +/// lowering. |
| 103 | +pub extern fn pixiUiSliderAsContainer(s: Slider) -> Container; |
| 104 | + |
| 105 | +// ── Switch ───────────────────────────────────────────────────────── |
| 106 | + |
| 107 | +/// `new PixiUI.Switch(options)`. `options` is the @pixi/ui |
| 108 | +/// Switch-options JSON (textures + value). |
| 109 | +pub extern fn pixiUiSwitchNew(options: Json) -> Switch; |
| 110 | + |
| 111 | +/// `switch.onChange.connect(callback)` — register a state-change |
| 112 | +/// handler. Returns 0. |
| 113 | +pub extern fn pixiUiSwitchOnChange(sw: Switch, callback: Json) -> Int; |
| 114 | + |
| 115 | +/// Upcast a Switch to its Container superclass. Zero-cost identity |
| 116 | +/// lowering. |
| 117 | +pub extern fn pixiUiSwitchAsContainer(sw: Switch) -> Container; |
0 commit comments