concinnity-dev 0.19.119

The Concinnity dev tooling library: world authoring, the in-engine editor, the debug server, docs and packaging
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
//! The Shader source panel's layout half: one of a Shader's `.hlsl` files in a
//! code text area (see `shader_source.rs` and `shader_diagnostics.rs` for the
//! data, `hook/edit/shader_source.rs` for the actions). The title names the
//! Shader and the file's stage, the header carries the path, an unsaved-edits
//! mark, the reference toggle and Save, a status line reports the last compile,
//! and the gutter marks the lines its diagnostics name. The reference column
//! (`shader_reference_panel.rs`) sits at the right, beside the text.

use concinnity_core::components::TextAlign;
use concinnity_core::ecs::World;

use super::registry::PanelKey;
use super::shader_diagnostics::{Status, Tone};
use super::shader_list_panel::tone_color;
use super::shader_reference_panel::{self as reference, COLUMN_W, RefView};
use crate::editor::hud_ids::{hud_ids, panel_base};
use crate::editor::text_area::TextArea;
use crate::editor::text_area::layout::{self, Geometry, Metrics, TextAreaIds, TextAreaView};
use crate::editor::text_area::markers::{GutterMarker, Severity};
use crate::editor::theme;
use crate::editor::widget::{self, place_rounded, point_in};

// The chrome, then the text area and the reference column over it.
hud_ids! {
    base: panel_base(PanelKey::ShaderSource);
    sprites: [
        pub(crate) PANEL_BG,
        CLOSE_BG,
        SAVE_BG,
        REF_BG,
        ..AREA.sprite_ids(),
        ..reference::ids().sprites.iter().copied(),
    ];
    labels: [
        TITLE_LABEL,
        CLOSE_LABEL,
        SAVE_LABEL,
        PATH_LABEL,
        STATUS_LABEL,
        DIRTY_LABEL,
        REF_LABEL,
        ..reference::ids().labels.iter().copied(),
    ];
    code_labels: [
        ..AREA.code_label_ids(),
        ..reference::ids().code_labels.iter().copied(),
    ];
    blocks: [AREA_BASE: TextAreaIds::SPAN, pub(super) REFERENCE_BASE: reference::IDS_SPAN];
}

pub(crate) const AREA: TextAreaIds = TextAreaIds::new(AREA_BASE);

// The default (and minimum) width; the user can widen the panel past this.
const SOURCE_W: f32 = 660.0;
const PAD: f32 = 10.0;
const HEADER_H: f32 = 32.0;
// Two lines: a compiler message quoting a path rarely fits one.
const STATUS_H: f32 = 2.0 * widget::LINE_H + 6.0;
const BTN_W: f32 = 70.0;
const REF_BTN_W: f32 = 90.0;
const DIRTY_W: f32 = 70.0;
const DEFAULT_ROWS: usize = 24;
const CHROME_H: f32 = widget::TITLE_H + HEADER_H + STATUS_H;

const BTN_TINT: [f32; 4] = [0.22, 0.40, 0.56, 1.0];
const BTN_TINT_HOVER: [f32; 4] = [0.28, 0.48, 0.66, 1.0];
const PATH_COLOR: [f32; 3] = [0.62, 0.66, 0.76];

// The per-frame view the hook assembles.
pub(crate) struct SourceView<'a> {
    // "water fragment".
    pub title: &'a str,
    pub path: &'a str,
    pub area: &'a TextArea,
    // Whether the text area holds the keyboard this frame.
    pub focus: bool,
    pub status: Option<&'a Status>,
    pub markers: &'a [GutterMarker],
    pub mouse: [f32; 2],
    // The reference column, while it is shown.
    pub reference: Option<RefView<'a>>,
}

// A resolved source-panel click.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SourceAction {
    // The press landed on the text area; the hook hands it to the area.
    Text,
    // Write the file; the hot-reload watcher recompiles it.
    Save,
    // The status line: jump to the error it reports.
    Status,
    // Show or hide the reference column.
    ToggleReference,
    // A row of the reference column, counted from its first shown row.
    Reference(usize),
    // A click elsewhere on the panel: swallowed so it cannot reach the world.
    Consume,
}

// Where the panel sits until the user drags it: left of center, below the
// top bar, so it opens beside the Shaders list rather than over it.
pub(crate) fn default_origin(vw: f32) -> [f32; 2] {
    [
        (vw * 0.5 - SOURCE_W).max(8.0),
        crate::editor::hud::body_top() + 8.0,
    ]
}

// The room the reference column takes from the text, when shown.
fn reference_w(reference: bool) -> f32 {
    if reference { COLUMN_W + PAD } else { 0.0 }
}

pub(crate) fn size(reference: bool) -> [f32; 2] {
    [
        SOURCE_W + reference_w(reference),
        CHROME_H + layout::height_for_rows(DEFAULT_ROWS, Metrics::code().line_h) + PAD,
    ]
}

// The tallest the panel resizes to: the text area's whole row pool shown.
pub(crate) fn max_size() -> [f32; 2] {
    let rows = layout::height_for_rows(layout::ROW_POOL, Metrics::code().line_h);
    [f32::INFINITY, CHROME_H + rows + PAD]
}

fn save_rect(o: [f32; 2], w: f32) -> [f32; 4] {
    [
        o[0] + w - PAD - BTN_W,
        o[1] + widget::TITLE_H + 4.0,
        BTN_W,
        HEADER_H - 8.0,
    ]
}

fn reference_button_rect(o: [f32; 2], w: f32) -> [f32; 4] {
    let save = save_rect(o, w);
    [save[0] - PAD - REF_BTN_W, save[1], REF_BTN_W, save[3]]
}

fn status_rect(o: [f32; 2], w: f32) -> [f32; 4] {
    [
        o[0] + PAD,
        o[1] + widget::TITLE_H + HEADER_H + 1.0,
        (w - 2.0 * PAD).max(0.0),
        STATUS_H - 4.0,
    ]
}

pub(crate) fn area_rect(o: [f32; 2], s: [f32; 2], reference: bool) -> [f32; 4] {
    let top = o[1] + CHROME_H;
    [
        o[0] + PAD,
        top,
        (s[0] - 2.0 * PAD - reference_w(reference)).max(0.0),
        (o[1] + s[1] - PAD - top).max(0.0),
    ]
}

// The reference column: the text's height, at the panel's right.
pub(crate) fn reference_rect(o: [f32; 2], s: [f32; 2]) -> [f32; 4] {
    let text = area_rect(o, s, true);
    [o[0] + s[0] - PAD - COLUMN_W, text[1], COLUMN_W, text[3]]
}

pub(crate) fn area_geometry(
    o: [f32; 2],
    s: [f32; 2],
    reference: bool,
    area: &TextArea,
    m: Metrics,
) -> Geometry {
    layout::geometry(area_rect(o, s, reference), m, area.line_count())
}

pub(crate) fn cursor_over_area(
    mx: f32,
    my: f32,
    o: [f32; 2],
    s: [f32; 2],
    reference: bool,
) -> bool {
    point_in(mx, my, area_rect(o, s, reference))
}

pub(crate) fn cursor_over_reference(mx: f32, my: f32, o: [f32; 2], s: [f32; 2]) -> bool {
    point_in(mx, my, reference_rect(o, s))
}

// Resolve a click at `(mx, my)` against the panel at origin `o`, size `s`,
// with the reference column shown or not. `None` means the click missed the
// panel.
pub(crate) fn hit_test(
    mx: f32,
    my: f32,
    o: [f32; 2],
    s: [f32; 2],
    reference: bool,
) -> Option<SourceAction> {
    if point_in(mx, my, save_rect(o, s[0])) {
        return Some(SourceAction::Save);
    }
    if point_in(mx, my, reference_button_rect(o, s[0])) {
        return Some(SourceAction::ToggleReference);
    }
    if point_in(mx, my, status_rect(o, s[0])) {
        return Some(SourceAction::Status);
    }
    if point_in(mx, my, area_rect(o, s, reference)) {
        return Some(SourceAction::Text);
    }
    if reference && let Some(slot) = reference::slot_at(reference_rect(o, s), mx, my) {
        return Some(SourceAction::Reference(slot));
    }
    point_in(mx, my, widget::outer_rect(o, s)).then_some(SourceAction::Consume)
}

// The status line's text and tone: the gutter marker or reference name under
// the cursor while one is hovered, else the panel's own status.
fn status_line(view: &SourceView, g: &Geometry, reference: [f32; 4]) -> Option<Status> {
    let area_view = TextAreaView {
        area: view.area,
        focused: view.focus,
        markers: view.markers,
        mouse: view.mouse,
    };
    match layout::hovered_marker(&area_view, g) {
        Some(m) => {
            let tone = match m.severity {
                Severity::Error => Tone::Error,
                Severity::Warning => Tone::Warning,
            };
            Some(Status::new(
                format!("line {}: {}", m.line + 1, m.message),
                tone,
            ))
        }
        None => view
            .reference
            .as_ref()
            .and_then(|r| reference::hovered(r, reference))
            .and_then(|row| row.describe())
            .map(|text| Status::new(text, Tone::Info))
            .or_else(|| view.status.cloned()),
    }
}

// Position + show the panel (`Some(view)`) at effective size `s`, or blank every
// element (`None`).
pub(crate) fn place(
    world: &mut World,
    view: Option<&SourceView>,
    o: [f32; 2],
    s: [f32; 2],
    m: Metrics,
) {
    let Some(view) = view else {
        ids().hide(world);
        return;
    };
    let w = s[0];
    let dirty = view.area.is_dirty();
    widget::place_panel(world, PANEL_BG, widget::outer_rect(o, s));
    let title = widget::title_rect(o, w);
    let heading = match dirty {
        true => format!("{} *", view.title),
        false => view.title.to_string(),
    };
    widget::place_heading(world, TITLE_LABEL, title, &heading);
    let close_hover = point_in(view.mouse[0], view.mouse[1], widget::close_rect(title));
    widget::place_close(world, CLOSE_BG, CLOSE_LABEL, title, close_hover);
    place_header(world, view, o, w, dirty);

    let shows_reference = view.reference.is_some();
    let g = area_geometry(o, s, shows_reference, view.area, m);
    let reference_at = reference_rect(o, s);
    let status = status_line(view, &g, reference_at);
    let (text, tone) = status
        .as_ref()
        .map_or(("", Tone::Info), |s| (s.text.as_str(), s.tone));
    widget::place_message(
        world,
        STATUS_LABEL,
        status_rect(o, w),
        text,
        tone_color(tone),
        status.is_some(),
    );
    let area_view = TextAreaView {
        area: view.area,
        focused: view.focus,
        markers: view.markers,
        mouse: view.mouse,
    };
    layout::place(world, AREA, Some(&area_view), &g);
    reference::place(world, view.reference.as_ref(), reference_at);
}

// The path on the left, then the unsaved-edits mark and Save on the right.
fn place_header(world: &mut World, view: &SourceView, o: [f32; 2], w: f32, dirty: bool) {
    let y = o[1] + widget::TITLE_H + HEADER_H * 0.5 - theme::TEXT_HALF;
    let save = save_rect(o, w);
    let toggle = reference_button_rect(o, w);
    let path_w = (toggle[0] - DIRTY_W - PAD - (o[0] + PAD)).max(0.0);
    widget::place_message(
        world,
        PATH_LABEL,
        [o[0] + PAD, y, path_w, widget::LINE_H],
        view.path,
        PATH_COLOR,
        true,
    );
    if let Some(l) = widget::label_mut(world, DIRTY_LABEL) {
        l.x = toggle[0] - PAD;
        l.y = y;
        l.align = TextAlign::Right;
        l.color = theme::LOG_WARN;
        l.visible = dirty;
        l.content = "unsaved".to_string();
    }
    let open = view.reference.is_some();
    let toggle_tint = match (open, point_in(view.mouse[0], view.mouse[1], toggle)) {
        (true, _) => theme::ACCENT_TINT,
        (false, true) => theme::HOVER_TINT,
        (false, false) => theme::BUTTON_TINT,
    };
    place_rounded(
        world,
        REF_BG,
        toggle,
        toggle_tint,
        theme::CONTROL_RADIUS,
        true,
    );
    widget::place_center_label(
        world,
        REF_LABEL,
        [
            toggle[0] + toggle[2] * 0.5,
            toggle[1] + toggle[3] * 0.5 - theme::TEXT_HALF,
        ],
        "Reference",
        [1.0, 1.0, 1.0],
        true,
    );
    let hover = point_in(view.mouse[0], view.mouse[1], save);
    let tint = if hover { BTN_TINT_HOVER } else { BTN_TINT };
    place_rounded(world, SAVE_BG, save, tint, theme::CONTROL_RADIUS, true);
    widget::place_center_label(
        world,
        SAVE_LABEL,
        [
            save[0] + save[2] * 0.5,
            save[1] + save[3] * 0.5 - theme::TEXT_HALF,
        ],
        "Save",
        [1.0, 1.0, 1.0],
        true,
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::editor::panels::shader_reference::{RefRow, Reference};
    use concinnity_core::components::TextLabel;
    use concinnity_core::ecs::asset_id::AssetId;
    use concinnity_core::render::shader_programs::vocabulary::ENTRIES;

    const M: Metrics = Metrics {
        line_h: 20.0,
        advance: 8.0,
        text_px: 14.0,
        label_scale: 0.7,
    };

    fn injected_world() -> World {
        ids().test_world()
    }

    fn view<'a>(
        area: &'a TextArea,
        status: Option<&'a Status>,
        markers: &'a [GutterMarker],
    ) -> SourceView<'a> {
        SourceView {
            title: "water fragment",
            path: "shaders/water.hlsl",
            area,
            focus: true,
            status,
            markers,
            mouse: [0.0, 0.0],
            reference: None,
        }
    }

    fn label(world: &World, id: AssetId) -> TextLabel {
        world.get_by_id::<TextLabel>(id).cloned().unwrap()
    }

    #[test]
    fn hit_test_resolves_save_status_and_text() {
        let o = [40.0, 40.0];
        let s = size(false);
        let at = |r: [f32; 4]| (r[0] + 5.0, r[1] + 5.0);
        let (x, y) = at(save_rect(o, s[0]));
        assert_eq!(hit_test(x, y, o, s, false), Some(SourceAction::Save));
        let (x, y) = at(status_rect(o, s[0]));
        assert_eq!(hit_test(x, y, o, s, false), Some(SourceAction::Status));
        let (x, y) = at(area_rect(o, s, false));
        assert_eq!(hit_test(x, y, o, s, false), Some(SourceAction::Text));
        assert_eq!(
            hit_test(o[0] + 2.0, o[1] + widget::TITLE_H + 4.0, o, s, false),
            Some(SourceAction::Consume)
        );
        assert_eq!(hit_test(5000.0, 5000.0, o, s, false), None);
    }

    #[test]
    fn unsaved_edits_mark_the_title_and_header() {
        let mut world = injected_world();
        let mut area = TextArea::from_text("float4 shade() {}");
        place(
            &mut world,
            Some(&view(&area, None, &[])),
            [20.0, 20.0],
            size(false),
            M,
        );
        assert_eq!(label(&world, TITLE_LABEL).content, "water fragment");
        assert!(!label(&world, DIRTY_LABEL).visible);
        area.type_char('x');
        place(
            &mut world,
            Some(&view(&area, None, &[])),
            [20.0, 20.0],
            size(false),
            M,
        );
        assert_eq!(label(&world, TITLE_LABEL).content, "water fragment *");
        assert!(label(&world, DIRTY_LABEL).visible);
    }

    #[test]
    fn the_status_line_takes_its_tone() {
        let mut world = injected_world();
        let area = TextArea::from_text("a");
        let status = Status::new("line 1: bad", Tone::Error);
        place(
            &mut world,
            Some(&view(&area, Some(&status), &[])),
            [20.0, 20.0],
            size(false),
            M,
        );
        let l = label(&world, STATUS_LABEL);
        assert!(l.visible && l.content == "line 1: bad");
        assert_eq!(l.color, theme::LOG_ERROR);
    }

    // Hovering a marker in the gutter reads it out on the status line.
    #[test]
    fn a_hovered_marker_reads_out_on_the_status_line() {
        let mut world = injected_world();
        let area = TextArea::from_text("a\nb\nc");
        let markers = [GutterMarker {
            line: 1,
            column: 0,
            severity: Severity::Warning,
            message: "unused".to_string(),
        }];
        let o = [20.0, 20.0];
        let r = area_rect(o, size(false), false);
        let mut v = view(&area, None, &markers);
        v.mouse = [r[0] + 2.0, r[1] + M.line_h * 1.5];
        place(&mut world, Some(&v), o, size(false), M);
        let l = label(&world, STATUS_LABEL);
        assert_eq!(l.content, "line 2: unused");
        assert_eq!(l.color, theme::LOG_WARN);
    }

    fn reference_rows() -> Vec<RefRow<'static>> {
        Reference::default().rows(ENTRIES)
    }

    #[test]
    fn the_reference_column_widens_the_panel_beside_the_text() {
        let o = [20.0, 20.0];
        let s = size(true);
        assert_eq!(s[0], size(false)[0] + COLUMN_W + PAD);
        let text = area_rect(o, s, true);
        let column = reference_rect(o, s);
        assert_eq!(
            text[2],
            area_rect(o, size(false), false)[2],
            "the text keeps its width"
        );
        assert!(
            text[0] + text[2] <= column[0],
            "the column sits right of the text"
        );
        assert_eq!((column[1], column[3]), (text[1], text[3]));
    }

    #[test]
    fn hit_test_resolves_the_toggle_and_reference_rows() {
        let o = [40.0, 40.0];
        let s = size(true);
        let t = reference_button_rect(o, s[0]);
        assert_eq!(
            hit_test(t[0] + 2.0, t[1] + 2.0, o, s, true),
            Some(SourceAction::ToggleReference)
        );
        let c = reference_rect(o, s);
        assert_eq!(
            hit_test(c[0] + 5.0, c[1] + 30.0, o, s, true),
            Some(SourceAction::Reference(1))
        );
        assert_eq!(
            hit_test(c[0] + 5.0, c[1] + 30.0, o, s, false),
            Some(SourceAction::Text),
            "a hidden column's room is the text's"
        );
    }

    #[test]
    fn a_hovered_reference_name_reads_out_on_the_status_line() {
        let mut world = injected_world();
        let area = TextArea::from_text("a");
        let rows = reference_rows();
        let o = [20.0, 20.0];
        let s = size(true);
        let c = reference_rect(o, s);
        let status = Status::new("compiled", Tone::Info);
        let mut v = view(&area, Some(&status), &[]);
        let mouse = [c[0] + 5.0, c[1] + 30.0];
        v.mouse = mouse;
        v.reference = Some(RefView {
            rows: &rows,
            scroll: 0,
            mouse,
        });
        place(&mut world, Some(&v), o, s, M);
        let l = label(&world, STATUS_LABEL);
        assert!(
            l.content
                .starts_with("float4 shade_surface(VertexOut v, GpuObjectData od): "),
            "{}",
            l.content
        );
        assert!(label(&world, REF_LABEL).visible);
    }
}