nightshade-api 0.53.0

Procedural high level API for the nightshade game engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//! Form field components: numeric, text, boolean, slider, color, select, chip,
//! tag, and swatch inputs sharing the `nightshade-field` styling conventions.

use leptos::prelude::*;

fn parse_number(chars: &[char], pos: &mut usize) -> Option<f64> {
    let start = *pos;
    while *pos < chars.len() && (chars[*pos].is_ascii_digit() || chars[*pos] == '.') {
        *pos += 1;
    }
    if *pos == start {
        return None;
    }
    chars[start..*pos].iter().collect::<String>().parse().ok()
}

fn parse_factor(chars: &[char], pos: &mut usize) -> Option<f64> {
    if *pos >= chars.len() {
        return None;
    }
    match chars[*pos] {
        '-' => {
            *pos += 1;
            Some(-parse_factor(chars, pos)?)
        }
        '+' => {
            *pos += 1;
            parse_factor(chars, pos)
        }
        '(' => {
            *pos += 1;
            let value = parse_expr(chars, pos)?;
            if *pos < chars.len() && chars[*pos] == ')' {
                *pos += 1;
                Some(value)
            } else {
                None
            }
        }
        _ => parse_number(chars, pos),
    }
}

fn parse_term(chars: &[char], pos: &mut usize) -> Option<f64> {
    let mut left = parse_factor(chars, pos)?;
    while *pos < chars.len() && matches!(chars[*pos], '*' | '/') {
        let operator = chars[*pos];
        *pos += 1;
        let right = parse_factor(chars, pos)?;
        left = if operator == '*' {
            left * right
        } else {
            left / right
        };
    }
    Some(left)
}

fn parse_expr(chars: &[char], pos: &mut usize) -> Option<f64> {
    let mut left = parse_term(chars, pos)?;
    while *pos < chars.len() && matches!(chars[*pos], '+' | '-') {
        let operator = chars[*pos];
        *pos += 1;
        let right = parse_term(chars, pos)?;
        left = if operator == '+' {
            left + right
        } else {
            left - right
        };
    }
    Some(left)
}

fn eval_expr(input: &str) -> Option<f64> {
    let chars: Vec<char> = input.chars().filter(|c| !c.is_whitespace()).collect();
    let mut pos = 0;
    let value = parse_expr(&chars, &mut pos)?;
    if pos == chars.len() && value.is_finite() {
        Some(value)
    } else {
        None
    }
}

/// A numeric field that evaluates arithmetic expressions, supports drag-to-scrub on its
/// label, optional reset-to-default, clamping to `min`/`max`, integer rounding, and live
/// validation. Emits `(value, committed)` through `on_change`, where `committed` marks
/// the final value on blur, Enter, or scrub end.
#[component]
pub fn NumberField(
    #[prop(into)] label: String,
    value: Signal<f64>,
    #[prop(optional)] step: Option<f64>,
    #[prop(optional)] min: Option<f64>,
    #[prop(optional)] max: Option<f64>,
    #[prop(optional)] integer: bool,
    #[prop(into, optional)] help: String,
    #[prop(into, optional)] error: String,
    #[prop(into, optional)] disabled: Signal<bool>,
    #[prop(optional)] default: Option<f64>,
    #[prop(optional)] validate: Option<Callback<f64, Option<String>>>,
    on_change: Callback<(f64, bool)>,
) -> impl IntoView {
    let step = step.unwrap_or(if integer { 1.0 } else { 0.1 });
    let validation = RwSignal::new(String::new());
    let error_signal = Signal::derive(move || {
        if error.is_empty() {
            validation.get()
        } else {
            error.clone()
        }
    });
    let clamp = move |parsed: f64| {
        let mut result = if integer { parsed.round() } else { parsed };
        if let Some(min) = min {
            result = result.max(min);
        }
        if let Some(max) = max {
            result = result.min(max);
        }
        result
    };
    let format_value = move |raw: f64| {
        if integer {
            format!("{:.0}", raw.round())
        } else {
            format!("{raw:.3}")
        }
    };
    let commit = move |raw: String, committed: bool| {
        let parsed = raw.parse::<f64>().ok().or_else(|| eval_expr(&raw));
        match parsed {
            Some(parsed) => {
                let clamped = clamp(parsed);
                if let Some(validate) = validate {
                    validation.set(validate.run(clamped).unwrap_or_default());
                } else {
                    validation.set(String::new());
                }
                on_change.run((clamped, committed));
            }
            None if committed && !raw.trim().is_empty() => {
                validation.set("Enter a number or expression".to_string());
            }
            None => {}
        }
    };
    let live = move |raw: String| {
        if let Ok(parsed) = raw.parse::<f64>() {
            on_change.run((clamp(parsed), false));
        }
    };
    let label_ref = NodeRef::<leptos::html::Span>::new();
    let drag = StoredValue::new(None::<(f64, f64)>);
    let on_scrub_down = move |event: web_sys::PointerEvent| {
        event.prevent_default();
        drag.set_value(Some((event.client_x() as f64, value.get_untracked())));
        if let Some(node) = label_ref.get() {
            let _ = node.set_pointer_capture(event.pointer_id());
        }
    };
    let on_scrub_move = move |event: web_sys::PointerEvent| {
        if let Some((start_x, start_value)) = drag.get_value() {
            let delta = event.client_x() as f64 - start_x;
            on_change.run((clamp(start_value + delta * step), false));
        }
    };
    let on_scrub_up = move |event: web_sys::PointerEvent| {
        if drag.get_value().is_some() {
            drag.set_value(None);
            on_change.run((value.get_untracked(), true));
            if let Some(node) = label_ref.get() {
                let _ = node.release_pointer_capture(event.pointer_id());
            }
        }
    };
    view! {
        <div class="nightshade-field-group">
            <label class="nightshade-field">
                <span
                    node_ref=label_ref
                    class="nightshade-field-label nightshade-scrub"
                    on:pointerdown=on_scrub_down
                    on:pointermove=on_scrub_move
                    on:pointerup=on_scrub_up
                >
                    {label}
                </span>
                <input
                    type="text"
                    inputmode="decimal"
                    step=step
                    disabled=move || disabled.get()
                    prop:value=move || format_value(value.get())
                    on:input=move |event| live(event_target_value(&event))
                    on:change=move |event| commit(event_target_value(&event), true)
                />
                {default
                    .map(|fallback| {
                        view! {
                            <span
                                class="nightshade-field-reset"
                                role="button"
                                tabindex="0"
                                title="Reset to default"
                                class:hidden=move || value.get() == fallback
                                on:click=move |_| on_change.run((fallback, true))
                            >
                                "\u{21ba}"
                            </span>
                        }
                    })}
            </label>
            <FieldNote help=help error=error_signal />
        </div>
    }
}

/// Three numeric inputs labelled X, Y, and Z for editing a `[f64; 3]` vector. Each axis
/// clamps to `min`/`max` and evaluates arithmetic expressions on commit. Emits the full
/// updated array with a `committed` flag through `on_change`.
#[component]
pub fn Vec3Field(
    #[prop(into)] label: String,
    value: Signal<[f64; 3]>,
    #[prop(optional)] step: Option<f64>,
    #[prop(optional)] min: Option<f64>,
    #[prop(optional)] max: Option<f64>,
    #[prop(into, optional)] disabled: Signal<bool>,
    on_change: Callback<([f64; 3], bool)>,
) -> impl IntoView {
    let clamp = move |parsed: f64| {
        let mut result = parsed;
        if let Some(min) = min {
            result = result.max(min);
        }
        if let Some(max) = max {
            result = result.min(max);
        }
        result
    };
    let axis = move |index: usize, tag: &'static str| {
        let apply = move |raw: String, committed: bool| {
            let parsed = if committed {
                raw.parse::<f64>().ok().or_else(|| eval_expr(&raw))
            } else {
                raw.parse::<f64>().ok()
            };
            if let Some(parsed) = parsed {
                let mut next = value.get_untracked();
                next[index] = clamp(parsed);
                on_change.run((next, committed));
            }
        };
        view! {
            <div class="nightshade-vec-axis">
                <span class="nightshade-vec-tag">{tag}</span>
                <input
                    type="text"
                    inputmode="decimal"
                    step=step.unwrap_or(0.1)
                    disabled=move || disabled.get()
                    prop:value=move || format!("{:.3}", value.get()[index])
                    on:input=move |event| apply(event_target_value(&event), false)
                    on:change=move |event| apply(event_target_value(&event), true)
                />
            </div>
        }
    };
    view! {
        <div class="nightshade-field-group">
            <span class="nightshade-field-label">{label}</span>
            <div class="nightshade-vec-field">
                {axis(0, "X")} {axis(1, "Y")} {axis(2, "Z")}
            </div>
        </div>
    }
}

/// A labelled checkbox bound to a `bool` signal. Emits the new checked state through
/// `on_change`.
#[component]
pub fn CheckField(
    #[prop(into)] label: String,
    value: Signal<bool>,
    on_change: Callback<bool>,
    #[prop(into, optional)] disabled: Signal<bool>,
) -> impl IntoView {
    view! {
        <label class="nightshade-field check">
            <input
                type="checkbox"
                disabled=move || disabled.get()
                prop:checked=move || value.get()
                on:change=move |event| on_change.run(event_target_checked(&event))
            />
            <span class="nightshade-field-label">{label}</span>
        </label>
    }
}

/// A labelled toggle switch (an ARIA `switch` button) bound to a `bool` signal. Emits the
/// toggled state through `on_change`.
#[component]
pub fn Switch(
    #[prop(into)] label: String,
    value: Signal<bool>,
    on_change: Callback<bool>,
    #[prop(into, optional)] disabled: Signal<bool>,
) -> impl IntoView {
    view! {
        <label class="nightshade-field nightshade-switch-field">
            <span class="nightshade-field-label">{label}</span>
            <button
                type="button"
                role="switch"
                class="nightshade-switch"
                class:on=move || value.get()
                aria-checked=move || value.get().to_string()
                disabled=move || disabled.get()
                on:click=move |_| on_change.run(!value.get_untracked())
            >
                <span class="nightshade-switch-thumb"></span>
            </button>
        </label>
    }
}

/// A single-line text field with optional help and error notes. Commits on change, or
/// debounces input by `debounce` milliseconds when set, emitting the text through
/// `on_commit`.
#[component]
pub fn TextField(
    #[prop(into)] label: String,
    value: Signal<String>,
    on_commit: Callback<String>,
    #[prop(into, optional)] placeholder: String,
    #[prop(into, optional)] help: String,
    #[prop(into, optional)] error: String,
    #[prop(into, optional)] disabled: Signal<bool>,
    #[prop(optional)] debounce: Option<u32>,
) -> impl IntoView {
    let error_signal = Signal::derive(move || error.clone());
    let generation = StoredValue::new(0u32);
    let on_input = move |event: web_sys::Event| {
        let Some(delay) = debounce else {
            return;
        };
        let text = event_target_value(&event);
        let current = generation.get_value().wrapping_add(1);
        generation.set_value(current);
        set_timeout(
            move || {
                if generation.get_value() == current {
                    on_commit.run(text.clone());
                }
            },
            std::time::Duration::from_millis(delay as u64),
        );
    };
    view! {
        <div class="nightshade-field-group">
            <label class="nightshade-field">
                <span class="nightshade-field-label">{label}</span>
                <input
                    type="text"
                    placeholder=placeholder
                    disabled=move || disabled.get()
                    prop:value=move || value.get()
                    on:input=on_input
                    on:change=move |event| {
                        if debounce.is_none() {
                            on_commit.run(event_target_value(&event));
                        }
                    }
                />
            </label>
            <FieldNote help=help error=error_signal />
        </div>
    }
}

/// A range slider bound to a `f64` signal with reactive `min`, `max`, and a fixed `step`,
/// showing the current value. Emits `(value, committed)` through `on_change`, with
/// `committed` false during drag and true on release.
#[component]
pub fn SliderField(
    #[prop(into)] label: String,
    value: Signal<f64>,
    #[prop(into)] min: Signal<f64>,
    #[prop(into)] max: Signal<f64>,
    #[prop(default = 0.01)] step: f64,
    #[prop(into, optional)] disabled: Signal<bool>,
    on_change: Callback<(f64, bool)>,
) -> impl IntoView {
    view! {
        <label class="nightshade-field">
            <span class="nightshade-field-label">{label}</span>
            <input
                type="range"
                min=move || min.get()
                max=move || max.get()
                step=step
                disabled=move || disabled.get()
                prop:value=move || value.get()
                on:input=move |event| {
                    if let Ok(parsed) = event_target_value(&event).parse::<f64>() {
                        on_change.run((parsed, false));
                    }
                }
                on:change=move |event| {
                    if let Ok(parsed) = event_target_value(&event).parse::<f64>() {
                        on_change.run((parsed, true));
                    }
                }
            />
            <span class="nightshade-field-value">{move || format!("{:.2}", value.get())}</span>
        </label>
    }
}

/// A native color picker bound to an `[f32; 3]` RGB signal (components in `0.0..=1.0`).
/// Emits `(rgb, committed)` through `on_change`, with `committed` false during input and
/// true on change.
#[component]
pub fn ColorField(
    #[prop(into)] label: String,
    value: Signal<[f32; 3]>,
    on_change: Callback<([f32; 3], bool)>,
    #[prop(into, optional)] disabled: Signal<bool>,
) -> impl IntoView {
    view! {
        <label class="nightshade-field">
            <span class="nightshade-field-label">{label}</span>
            <input
                type="color"
                disabled=move || disabled.get()
                prop:value=move || rgb_to_hex(value.get())
                on:input=move |event| {
                    if let Some(rgb) = hex_to_rgb(&event_target_value(&event)) {
                        on_change.run((rgb, false));
                    }
                }
                on:change=move |event| {
                    if let Some(rgb) = hex_to_rgb(&event_target_value(&event)) {
                        on_change.run((rgb, true));
                    }
                }
            />
        </label>
    }
}

/// A labelled dropdown built from `(value, text)` option pairs, bound to a `String`
/// signal. Emits the selected option value through `on_change`.
#[component]
pub fn Select(
    #[prop(into)] label: String,
    value: Signal<String>,
    options: Vec<(String, String)>,
    on_change: Callback<String>,
    #[prop(into, optional)] disabled: Signal<bool>,
) -> impl IntoView {
    view! {
        <label class="nightshade-field">
            <span class="nightshade-field-label">{label}</span>
            <select
                class="nightshade-select"
                disabled=move || disabled.get()
                prop:value=move || value.get()
                on:change=move |event| on_change.run(event_target_value(&event))
            >
                {options
                    .into_iter()
                    .map(|(option_value, text)| view! { <option value=option_value>{text}</option> })
                    .collect_view()}
            </select>
        </label>
    }
}

/// A flex container that lays out chip children, with an optional extra `class`.
#[component]
pub fn ChipGroup(#[prop(into, optional)] class: String, children: Children) -> impl IntoView {
    view! { <div class=format!("nightshade-chip-group {class}")>{children()}</div> }
}

/// A pill-shaped toggle button reflecting an `active` signal via `aria-pressed`. Emits a
/// unit event through `on_toggle` when clicked.
#[component]
pub fn ToggleChip(
    #[prop(into)] label: String,
    #[prop(into)] active: Signal<bool>,
    on_toggle: Callback<()>,
    #[prop(into, optional)] disabled: Signal<bool>,
) -> impl IntoView {
    view! {
        <button
            type="button"
            class="nightshade-chip"
            class:active=move || active.get()
            aria-pressed=move || active.get().to_string()
            disabled=move || disabled.get()
            on:click=move |_| on_toggle.run(())
        >
            {label}
        </button>
    }
}

/// An editable list of tags with a text entry that adds a trimmed tag on Enter. Emits new
/// tags through `on_add` and removed tags through `on_remove`.
#[component]
pub fn TagInput(
    #[prop(into)] tags: Signal<Vec<String>>,
    on_add: Callback<String>,
    on_remove: Callback<String>,
    #[prop(into, optional)] placeholder: String,
) -> impl IntoView {
    let draft = RwSignal::new(String::new());
    let placeholder = if placeholder.is_empty() {
        "Add tag…".to_string()
    } else {
        placeholder
    };
    let submit = move || {
        let value = draft.get_untracked().trim().to_string();
        if !value.is_empty() {
            on_add.run(value);
            draft.set(String::new());
        }
    };
    view! {
        <div class="nightshade-tag-input">
            <For each=move || tags.get() key=|tag| tag.clone() let:tag>
                {
                    let removed = tag.clone();
                    view! {
                        <span class="nightshade-tag">
                            {tag.clone()}
                            <button
                                class="nightshade-tag-remove"
                                aria-label="Remove tag"
                                on:click=move |_| on_remove.run(removed.clone())
                            >
                                "\u{00d7}"
                            </button>
                        </span>
                    }
                }
            </For>
            <input
                type="text"
                class="nightshade-tag-field"
                placeholder=placeholder
                prop:value=move || draft.get()
                on:input=move |event| draft.set(event_target_value(&event))
                on:keydown=move |event| {
                    if event.key() == "Enter" {
                        event.prevent_default();
                        submit();
                    }
                }
            />
        </div>
    }
}

/// A single color swatch button showing `color` as its background, marked active via the
/// `active` signal. Runs the optional `on_select` callback when clicked.
#[component]
pub fn Swatch(
    #[prop(into)] color: String,
    #[prop(into, optional)] active: Signal<bool>,
    #[prop(optional)] on_select: Option<Callback<()>>,
) -> impl IntoView {
    view! {
        <button
            type="button"
            class="nightshade-swatch"
            class:active=move || active.get()
            title=color.clone()
            style=format!("background:{color}")
            on:click=move |_| {
                if let Some(callback) = on_select {
                    callback.run(());
                }
            }
        ></button>
    }
}

/// A row of [`Swatch`] buttons built from `colors`, highlighting the one matching the
/// `selected` signal. Emits the chosen color string through `on_select`.
#[component]
pub fn SwatchPalette(
    colors: Vec<String>,
    #[prop(into)] selected: Signal<String>,
    on_select: Callback<String>,
) -> impl IntoView {
    view! {
        <div class="nightshade-swatch-palette">
            {colors
                .into_iter()
                .map(|color| {
                    let value = color.clone();
                    let compare = color.clone();
                    view! {
                        <Swatch
                            color=color
                            active=Signal::derive(move || selected.get() == compare)
                            on_select=Callback::new(move |_| on_select.run(value.clone()))
                        />
                    }
                })
                .collect_view()}
        </div>
    }
}

#[component]
fn FieldNote(
    #[prop(into, optional)] help: String,
    #[prop(into)] error: Signal<String>,
) -> impl IntoView {
    let show_help = !help.is_empty();
    view! {
        <Show when=move || !error.get().is_empty() fallback=|| ()>
            <div class="nightshade-field-footer">
                <span class="nightshade-field-error">{move || error.get()}</span>
            </div>
        </Show>
        {show_help
            .then(|| {
                let help = help.clone();
                view! {
                    <Show when=move || error.get().is_empty() fallback=|| ()>
                        <div class="nightshade-field-footer">
                            <span class="nightshade-field-help">{help.clone()}</span>
                        </div>
                    </Show>
                }
            })}
    }
}

fn rgb_to_hex(rgb: [f32; 3]) -> String {
    format!(
        "#{:02x}{:02x}{:02x}",
        (rgb[0].clamp(0.0, 1.0) * 255.0) as u8,
        (rgb[1].clamp(0.0, 1.0) * 255.0) as u8,
        (rgb[2].clamp(0.0, 1.0) * 255.0) as u8,
    )
}

fn hex_to_rgb(hex: &str) -> Option<[f32; 3]> {
    let hex = hex.strip_prefix('#')?;
    if hex.len() != 6 {
        return None;
    }
    let red = u8::from_str_radix(&hex[0..2], 16).ok()?;
    let green = u8::from_str_radix(&hex[2..4], 16).ok()?;
    let blue = u8::from_str_radix(&hex[4..6], 16).ok()?;
    Some([
        red as f32 / 255.0,
        green as f32 / 255.0,
        blue as f32 / 255.0,
    ])
}

#[cfg(test)]
mod tests {
    use super::{eval_expr, hex_to_rgb, rgb_to_hex};

    #[test]
    fn hex_round_trips_through_rgb() {
        assert_eq!(rgb_to_hex([1.0, 0.0, 0.0]), "#ff0000");
        assert_eq!(rgb_to_hex([0.0, 1.0, 0.0]), "#00ff00");
        let parsed = hex_to_rgb("#3366cc").expect("valid hex");
        assert_eq!(rgb_to_hex(parsed), "#3366cc");
    }

    #[test]
    fn malformed_hex_is_rejected() {
        assert!(hex_to_rgb("3366cc").is_none());
        assert!(hex_to_rgb("#fff").is_none());
        assert!(hex_to_rgb("#gggggg").is_none());
    }

    #[test]
    fn evaluates_arithmetic_with_precedence_and_parens() {
        assert_eq!(eval_expr("2+3*4"), Some(14.0));
        assert_eq!(eval_expr("(2+3)*4"), Some(20.0));
        assert_eq!(eval_expr("-1.5 + 2"), Some(0.5));
        assert_eq!(eval_expr("10/4"), Some(2.5));
    }

    #[test]
    fn rejects_malformed_expressions() {
        assert!(eval_expr("2+").is_none());
        assert!(eval_expr("abc").is_none());
        assert!(eval_expr("1/0").is_none());
        assert!(eval_expr("(1+2").is_none());
    }
}