bevy/crates
Carter Anderson e4c8bb92db AnimatedField and Rework Evaluators (#16484)
# Objective

Animating component fields requires too much boilerplate at the moment:

```rust
#[derive(Reflect)]
struct FontSizeProperty;

impl AnimatableProperty for FontSizeProperty {
    type Component = TextFont;

    type Property = f32;

    fn get_mut(component: &mut Self::Component) -> Option<&mut Self::Property> {
        Some(&mut component.font_size)
    }
}

animation_clip.add_curve_to_target(
    animation_target_id,
    AnimatableKeyframeCurve::new(
        [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
            .into_iter()
            .zip([24.0, 80.0, 24.0, 80.0, 24.0, 80.0, 24.0]),
    )
    .map(AnimatableCurve::<FontSizeProperty, _>::from_curve)
    .expect("should be able to build translation curve because we pass in valid samples"),
);
```

## Solution

This adds `AnimatedField` and an `animated_field!` macro, enabling the
following:

```rust
animation_clip.add_curve_to_target(
    animation_target_id,
    AnimatableCurve::new(
        animated_field!(TextFont::font_size),
        AnimatableKeyframeCurve::new(
            [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
                .into_iter()
                .zip([24.0, 80.0, 24.0, 80.0, 24.0, 80.0, 24.0]),
        )
        .expect(
            "should be able to build translation curve because we pass in valid samples",
        ),
    ),
);
```

This required reworking the internals a bit, namely stripping out a lot
of the `Reflect` usage, as that implementation was fundamentally
incompatible with the `AnimatedField` pattern. `Reflect` was being used
in this context just to downcast traits. But we can get downcasting
behavior without the `Reflect` requirement by implementing `Downcast`
for `AnimationCurveEvaluator`.

This also reworks "evaluator identity" to support either a (Component /
Field) pair, or a TypeId. This allows properties to reuse evaluators,
even if they have different accessor methods. The "contract" here is
that for a given (Component / Field) pair, the accessor will return the
same value. Fields are identified by their Reflect-ed field index. The
(TypeId, usize) is prehashed and cached to optimize for lookup speed.

This removes the built-in hard-coded TranslationCurve / RotationCurve /
ScaleCurve in favor of AnimatableField.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2024-11-28 00:23:01 +01:00
..
bevy_a11y Remove accesskit re-export from bevy_a11y (#16257) 2024-11-11 22:14:07 +01:00
bevy_animation AnimatedField and Rework Evaluators (#16484) 2024-11-28 00:23:01 +01:00
bevy_app Clarify that bevy_app::App.world() (and mut variant) returns the main SubApp's World (#16527) 2024-11-28 00:23:00 +01:00
bevy_asset Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_audio AudioPlayer::new() (#16287) 2024-11-11 22:08:10 +01:00
bevy_color Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_core Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_core_pipeline Fix CAS shader alpha accessor (#16530) 2024-11-28 00:23:00 +01:00
bevy_derive Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_dev_tools Headless by features (#16401) 2024-11-17 18:39:27 +01:00
bevy_diagnostic Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_dylib Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_ecs Fix Single doc links (#16493) 2024-11-25 23:59:44 +01:00
bevy_encase_derive Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_gilrs Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_gizmos Use normal constructors for EasingCurve, FunctionCurve, ConstantCurve (#16367) 2024-11-16 15:30:10 +01:00
bevy_gltf AnimatedField and Rework Evaluators (#16484) 2024-11-28 00:23:01 +01:00
bevy_hierarchy Fix bevy_hierarchy failing to compile without reflect feature (#16428) 2024-11-19 22:10:25 +01:00
bevy_image use wgpu patch 23.0.1 (#16513) 2024-11-26 00:14:17 +01:00
bevy_input Revert most of #16222 and add gamepad accessors (#16425) 2024-11-19 22:10:24 +01:00
bevy_internal Fix the picking backend features not actually disabling the features (#16470) 2024-11-22 21:24:03 +01:00
bevy_log Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_macro_utils Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_math Use normal constructors for EasingCurve, FunctionCurve, ConstantCurve (#16367) 2024-11-16 15:30:10 +01:00
bevy_mesh use wgpu patch 23.0.1 (#16513) 2024-11-26 00:14:17 +01:00
bevy_mikktspace Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_pbr Do not re-check visibility or re-render shadow maps for point and spot lights for each view (#15156) 2024-11-17 19:06:11 +01:00
bevy_picking Fix the picking backend features not actually disabling the features (#16470) 2024-11-22 21:24:03 +01:00
bevy_ptr Reduce compile time of bevy_ptr::OwnedPtr::make function (#15644) 2024-11-17 19:06:07 +01:00
bevy_reflect bevy_reflect: Add ReflectSerializerProcessor (#15548) 2024-11-17 19:06:13 +01:00
bevy_remote Expose BRP system scheduling and add system set (#16400) 2024-11-17 18:39:34 +01:00
bevy_render use wgpu patch 23.0.1 (#16513) 2024-11-26 00:14:17 +01:00
bevy_scene Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_sprite Add flags to SpritePlugin and UiPlugin to allow disabling their picking backend (without needing to disable features). (#16473) 2024-11-22 21:24:03 +01:00
bevy_state Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_tasks Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_text Skip empty spans when updating text buffers (#16524) 2024-11-28 00:23:00 +01:00
bevy_time Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_transform Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_ui use scale factor for touches in UI focus (#16522) 2024-11-26 23:31:52 +01:00
bevy_utils AnimatedField and Rework Evaluators (#16484) 2024-11-28 00:23:01 +01:00
bevy_window Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_winit Remove accesskit re-export from bevy_a11y (#16257) 2024-11-11 22:14:07 +01:00