jackdaw 0.3.1

A 3D level editor built with Bevy
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
//! Per-property "animate" diamond on inspector field rows.
//!
//! Adds a diamond button next to animatable fields (see
//! `ANIMATABLE_FIELDS`). Clicking it finds-or-creates a clip + track
//! and spawns a keyframe at the cursor time. New animatable properties
//! need one entry in `ANIMATABLE_FIELDS` plus matching arms in
//! `spawn_typed_keyframe` and `compile::build_curve_for_track`.

use bevy::prelude::*;
use jackdaw_animation::{
    AnimationTrack, Clip, F32Keyframe, QuatKeyframe, SelectedClip, TimelineCursor, TimelineDirty,
    Vec3Keyframe,
};
use jackdaw_feathers::button::{ButtonClickEvent, ButtonProps, ButtonSize, ButtonVariant, button};
use jackdaw_feathers::icons::Icon;

use super::InspectorFieldRow;

/// Epsilon for "is the cursor on this keyframe?" (~1 frame at 60fps).
const CURSOR_ON_KEYFRAME_EPS: f32 = 0.02;

const TRANSFORM: &str = "bevy_transform::components::transform::Transform";

/// The `(component_type_path, field_path)` pairs that get a keyframe
/// diamond in the inspector. Keep in sync with the compile dispatch
/// in `jackdaw_animation::compile::build_curve_for_track` and with
/// [`spawn_typed_keyframe`] below.
const ANIMATABLE_FIELDS: &[(&str, &str)] = &[
    (TRANSFORM, "translation"),
    (TRANSFORM, "rotation"),
    (TRANSFORM, "scale"),
];

/// Marker on the diamond button. The click observer reads this to
/// know which source entity + property to keyframe.
#[derive(Component, Clone, Debug)]
pub struct AnimDiamondButton {
    pub source_entity: Entity,
    pub component_type_path: String,
    pub field_path: String,
}

/// True if the given `(component_type_path, field_path)` is in the
/// animatable allowlist.
fn is_animatable(component_type_path: &str, field_path: &str) -> bool {
    ANIMATABLE_FIELDS
        .iter()
        .any(|(t, f)| *t == component_type_path && *f == field_path)
}

/// Spawn a diamond button on every newly-added `InspectorFieldRow`
/// whose root property is animatable. Runs in `Update` and fires only
/// when rows are (re-)spawned, so it's cheap.
///
/// The `InspectorFieldRow` marker sits on the row's **outer column
/// container**, which `reflect_fields.rs` spawns with `position_type:
/// Relative` specifically so absolutely-positioned children land in
/// the row's coordinate space. That lets us tuck the diamond into
/// the top-right corner next to the field label without reflowing
/// the column's flex layout, and gives us exactly one diamond per
/// composite field (not one per scalar axis input inside it).
pub fn decorate_animatable_fields(
    new_rows: Query<(Entity, &InspectorFieldRow), Added<InspectorFieldRow>>,
    mut commands: Commands,
) {
    for (row_entity, row) in &new_rows {
        if !is_animatable(&row.type_path, &row.field_path) {
            continue;
        }
        // Two-entity structure: an absolutely-positioned wrapper node
        // that takes care of where the diamond sits in the row, and a
        // child button that carries the `AnimDiamondButton` marker
        // plus the feathers `button()` bundle (which brings its own
        // Node). Splitting it this way avoids the duplicate-Node
        // panic that happens when you stack a custom Node alongside
        // a bundle that already includes one.
        let wrapper = commands
            .spawn((
                Node {
                    position_type: PositionType::Absolute,
                    top: Val::Px(0.0),
                    right: Val::Px(4.0),
                    ..default()
                },
                ChildOf(row_entity),
            ))
            .id();

        commands.spawn((
            AnimDiamondButton {
                source_entity: row.source_entity,
                component_type_path: row.type_path.clone(),
                field_path: row.field_path.clone(),
            },
            button(
                ButtonProps::new("")
                    .with_variant(ButtonVariant::Ghost)
                    .with_size(ButtonSize::IconSM)
                    .with_left_icon(Icon::Diamond),
            ),
            ChildOf(wrapper),
        ));
    }
}

/// Observer: when a diamond button is clicked, ensure a clip + track
/// exist for the bound property and spawn a keyframe at the current
/// cursor time.
pub fn on_diamond_click(
    event: On<ButtonClickEvent>,
    buttons: Query<&AnimDiamondButton>,
    mut commands: Commands,
) {
    let Ok(button_ref) = buttons.get(event.entity) else {
        return;
    };
    let source_entity = button_ref.source_entity;
    let component_type_path = button_ref.component_type_path.clone();
    let field_path = button_ref.field_path.clone();

    commands.queue(move |world: &mut World| {
        let cursor_time = world
            .get_resource::<TimelineCursor>()
            .map(|c| c.seek_time)
            .unwrap_or(0.0);

        // Step 1: find or create a Clip as a child of the source
        // entity. The clip's name reuses the source's Name if set.
        let clip_entity = find_or_create_clip(world, source_entity);
        let Some(clip_entity) = clip_entity else {
            warn!(
                "Diamond click: source entity {source_entity} has no Name - \
                 give it one in the inspector first so the clip's target can \
                 resolve"
            );
            return;
        };

        // Step 2: find or create a track for this property under the
        // clip.
        let track_entity =
            find_or_create_track(world, clip_entity, &component_type_path, &field_path);

        // Step 3: snapshot the current reflected field value and
        // spawn the right typed keyframe component as a child of the
        // track.
        spawn_typed_keyframe(
            world,
            source_entity,
            track_entity,
            &component_type_path,
            &field_path,
            cursor_time,
        );

        // Step 4: grow the clip's authored duration if needed so the
        // new keyframe is visible in the timeline view.
        if let Some(mut clip) = world.get_mut::<Clip>(clip_entity) {
            if cursor_time > clip.duration {
                clip.duration = cursor_time;
            }
        }

        // Step 5: make this the active clip and force a timeline
        // rebuild so the new diamond/keyframe row appears.
        if let Some(mut selected) = world.get_resource_mut::<SelectedClip>() {
            selected.0 = Some(clip_entity);
        }
        if let Some(mut dirty) = world.get_resource_mut::<TimelineDirty>() {
            dirty.0 = true;
        }
    });
}

/// Return an existing `Clip` child of `source_entity`, or spawn one
/// and return its entity. Returns `None` if the source entity has no
/// `Name`, because name is required for the compile step to derive
/// the `AnimationTargetId`.
fn find_or_create_clip(world: &mut World, source_entity: Entity) -> Option<Entity> {
    let target_name = world
        .get::<Name>(source_entity)
        .map(|n| n.as_str().to_string())?;

    // Check existing Clip children.
    if let Some(children) = world.get::<Children>(source_entity) {
        let children_vec: Vec<Entity> = children.iter().collect();
        for child in children_vec {
            if world.get::<Clip>(child).is_some() {
                return Some(child);
            }
        }
    }

    // None exist - spawn one as a child of the source.
    let clip = world
        .spawn((
            Clip::default(),
            Name::new(format!("{target_name} Clip")),
            ChildOf(source_entity),
        ))
        .id();
    Some(clip)
}

/// Return an existing `AnimationTrack` child of `clip_entity` matching
/// `(component_type_path, field_path)`, or spawn a new one.
fn find_or_create_track(
    world: &mut World,
    clip_entity: Entity,
    component_type_path: &str,
    field_path: &str,
) -> Entity {
    if let Some(children) = world.get::<Children>(clip_entity) {
        let children_vec: Vec<Entity> = children.iter().collect();
        for child in children_vec {
            if let Some(track) = world.get::<AnimationTrack>(child) {
                if track.component_type_path == component_type_path
                    && track.field_path == field_path
                {
                    return child;
                }
            }
        }
    }

    let label = format!("/ {field_path}");
    world
        .spawn((
            AnimationTrack::new(component_type_path.to_string(), field_path.to_string()),
            Name::new(label),
            ChildOf(clip_entity),
        ))
        .id()
}

/// Snapshot the current value of the animated field on the source
/// entity and spawn the appropriate typed keyframe component.
///
/// This is the dispatch mirror of
/// `jackdaw_animation::compile::build_curve_for_track` and
/// `jackdaw_animation::timeline::handle_add_keyframe_click`. Adding a
/// new animatable property means a new arm here plus a new arm
/// there. Keep them in sync.
fn spawn_typed_keyframe(
    world: &mut World,
    source_entity: Entity,
    track_entity: Entity,
    component_type_path: &str,
    field_path: &str,
    time: f32,
) {
    match (component_type_path, field_path) {
        (TRANSFORM, "translation") => {
            let Some(transform) = world.get::<Transform>(source_entity).copied() else {
                warn!("Diamond click: source has no Transform");
                return;
            };
            world.spawn((
                Vec3Keyframe {
                    time,
                    value: transform.translation,
                },
                ChildOf(track_entity),
            ));
        }
        (TRANSFORM, "rotation") => {
            let Some(transform) = world.get::<Transform>(source_entity).copied() else {
                warn!("Diamond click: source has no Transform");
                return;
            };
            world.spawn((
                QuatKeyframe {
                    time,
                    value: transform.rotation,
                },
                ChildOf(track_entity),
            ));
        }
        (TRANSFORM, "scale") => {
            let Some(transform) = world.get::<Transform>(source_entity).copied() else {
                warn!("Diamond click: source has no Transform");
                return;
            };
            world.spawn((
                Vec3Keyframe {
                    time,
                    value: transform.scale,
                },
                ChildOf(track_entity),
            ));
        }
        _ => {
            let _ = F32Keyframe::default();
            warn!("Diamond click: no snapshot dispatch for {component_type_path}.{field_path}",);
        }
    }
}

/// State of an animatable field, used to style its diamond icon.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DiamondState {
    /// No track exists yet for this `(source, component, field)`.
    /// The diamond reads "click me to start animating this."
    NoTrack,
    /// A track exists and has keyframes, but the cursor isn't
    /// sitting on any of them - clicking adds a new keyframe.
    HasTrack,
    /// The cursor is exactly on an existing keyframe - clicking
    /// would replace it (currently spawns a duplicate, but the
    /// compile step dedupes on time).
    OnKeyframe,
}

/// Per-frame highlight updater: walks each diamond button's source
/// entity → `Clip` child → matching `AnimationTrack` → keyframes, and
/// paints the diamond icon with the right state color.
///
/// Runs every frame. Cheap because there are only a handful of
/// diamonds on screen (one per animatable Transform field on the
/// inspected entity) and the tree walk is shallow.
pub fn update_anim_diamond_highlights(
    buttons: Query<(Entity, &AnimDiamondButton)>,
    children_query: Query<&Children>,
    clips: Query<(), With<Clip>>,
    tracks: Query<&AnimationTrack>,
    vec3_keyframes: Query<&Vec3Keyframe>,
    quat_keyframes: Query<&QuatKeyframe>,
    f32_keyframes: Query<&F32Keyframe>,
    cursor: Res<TimelineCursor>,
    mut text_colors: Query<&mut TextColor>,
) {
    for (btn_entity, btn) in &buttons {
        let state = compute_diamond_state(
            btn,
            &children_query,
            &clips,
            &tracks,
            &vec3_keyframes,
            &quat_keyframes,
            &f32_keyframes,
            cursor.seek_time,
        );
        let color = match state {
            // Dim and slightly transparent - the field isn't
            // animated yet. Still clickable, just unobtrusive.
            DiamondState::NoTrack => Color::srgba(0.55, 0.55, 0.55, 0.65),
            // Accent blue - matches the track strip diamonds in
            // the timeline. "There's a track here; click to add a
            // keyframe at the current cursor time."
            DiamondState::HasTrack => Color::srgb(0.38, 0.72, 1.0),
            // Amber - "you're standing on an existing keyframe."
            // Same color the timeline widget uses for selected
            // keyframes, so the visual language is consistent.
            DiamondState::OnKeyframe => Color::srgb(1.0, 0.78, 0.12),
        };

        // The feathers `button()` bundle spawns an icon child
        // (`Text` + `TextFont`) via `setup_button`. Walk the
        // button's children and recolor any text nodes we find -
        // there should be exactly one (the Diamond glyph).
        recolor_button_icon(btn_entity, color, &children_query, &mut text_colors);
    }
}

fn recolor_button_icon(
    root: Entity,
    color: Color,
    children_query: &Query<&Children>,
    text_colors: &mut Query<&mut TextColor>,
) {
    let Ok(children) = children_query.get(root) else {
        return;
    };
    for child in children.iter() {
        if let Ok(mut tc) = text_colors.get_mut(child) {
            tc.0 = color;
        }
        // Feathers sometimes wraps the icon in an extra container;
        // recurse one level to be safe.
        recolor_button_icon(child, color, children_query, text_colors);
    }
}

#[allow(clippy::too_many_arguments)]
fn compute_diamond_state(
    btn: &AnimDiamondButton,
    children_query: &Query<&Children>,
    clips: &Query<(), With<Clip>>,
    tracks: &Query<&AnimationTrack>,
    vec3_keyframes: &Query<&Vec3Keyframe>,
    quat_keyframes: &Query<&QuatKeyframe>,
    f32_keyframes: &Query<&F32Keyframe>,
    cursor_time: f32,
) -> DiamondState {
    // Step 1: find the `Clip` child of the source entity.
    let Ok(source_children) = children_query.get(btn.source_entity) else {
        return DiamondState::NoTrack;
    };
    let clip_entity = source_children.iter().find(|c| clips.contains(*c));
    let Some(clip_entity) = clip_entity else {
        return DiamondState::NoTrack;
    };

    // Step 2: find an `AnimationTrack` under that clip matching this
    // button's (component_type_path, field_path).
    let Ok(clip_children) = children_query.get(clip_entity) else {
        return DiamondState::NoTrack;
    };
    let track_entity = clip_children.iter().find(|c| {
        tracks
            .get(*c)
            .map(|t| {
                t.component_type_path == btn.component_type_path && t.field_path == btn.field_path
            })
            .unwrap_or(false)
    });
    let Some(track_entity) = track_entity else {
        return DiamondState::NoTrack;
    };

    // Step 3: walk the track's keyframes and check if any lands
    // on the cursor time within the epsilon.
    let Ok(track_children) = children_query.get(track_entity) else {
        return DiamondState::HasTrack;
    };
    for kf in track_children.iter() {
        let t = vec3_keyframes
            .get(kf)
            .map(|k| k.time)
            .or_else(|_| quat_keyframes.get(kf).map(|k| k.time))
            .or_else(|_| f32_keyframes.get(kf).map(|k| k.time))
            .ok();
        if let Some(t) = t {
            if (t - cursor_time).abs() < CURSOR_ON_KEYFRAME_EPS {
                return DiamondState::OnKeyframe;
            }
        }
    }

    DiamondState::HasTrack
}