bevy/crates
Carter Anderson af10aa38aa
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-27 22:19:55 +00:00
..
bevy_a11y Remove accesskit re-export from bevy_a11y (#16257) 2024-11-08 21:01:16 +00:00
bevy_animation AnimatedField and Rework Evaluators (#16484) 2024-11-27 22:19:55 +00:00
bevy_app Clarify that bevy_app::App.world() (and mut variant) returns the main SubApp's World (#16527) 2024-11-27 16:09:09 +00:00
bevy_asset Support creating asset directories (#16220) 2024-11-04 22:06:00 +00:00
bevy_audio AudioPlayer::new() (#16287) 2024-11-08 01:51:50 +00:00
bevy_color Fix MSRVs for standalone crates (#16333) 2024-11-17 09:38:13 +00:00
bevy_core Support on_thread_spawn and on_thread_destroy for TaskPoolPlugin (#13045) 2024-11-11 20:00:01 +00:00
bevy_core_pipeline Fix CAS shader alpha accessor (#16530) 2024-11-27 21:51:29 +00:00
bevy_derive move ANDROID_APP to bevy_window (#15585) 2024-10-02 03:01:06 +00:00
bevy_dev_tools Headless by features (#16401) 2024-11-16 21:33:37 +00:00
bevy_diagnostic Revert "Update sysinfo version to 0.32.1 (#16517)" (#16523) 2024-11-26 22:44:23 +00:00
bevy_dylib Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_ecs Fix Single doc links (#16493) 2024-11-24 18:44:00 +00:00
bevy_encase_derive Update `glam to 0.29, encase` to 0.10. (#15249) 2024-09-23 19:44:02 +00:00
bevy_gilrs Use Name component for gamepad (#16233) 2024-11-05 00:30:48 +00:00
bevy_gizmos Use normal constructors for EasingCurve, FunctionCurve, ConstantCurve (#16367) 2024-11-13 15:30:05 +00:00
bevy_gltf AnimatedField and Rework Evaluators (#16484) 2024-11-27 22:19:55 +00:00
bevy_hierarchy Fix bevy_hierarchy failing to compile without reflect feature (#16428) 2024-11-19 01:28:42 +00:00
bevy_image use wgpu patch 23.0.1 (#16513) 2024-11-25 22:47:31 +00:00
bevy_input Revert most of #16222 and add gamepad accessors (#16425) 2024-11-19 00:00:16 +00:00
bevy_internal Fix the picking backend features not actually disabling the features (#16470) 2024-11-22 18:14:16 +00:00
bevy_log Use en-us locale for typos (#16037) 2024-10-20 18:55:17 +00:00
bevy_macro_utils Modify derive_label to support no_std environments (#15465) 2024-09-27 20:23:26 +00:00
bevy_math feat: mark some functions in bevy_math as const (#16439) 2024-11-22 18:26:47 +00:00
bevy_mesh use wgpu patch 23.0.1 (#16513) 2024-11-25 22:47:31 +00:00
bevy_mikktspace Use en-us locale for typos (#16037) 2024-10-20 18:55:17 +00:00
bevy_pbr Fix meshlet private item regression (#16404) 2024-11-16 22:06:26 +00:00
bevy_picking Fix the picking backend features not actually disabling the features (#16470) 2024-11-22 18:14:16 +00:00
bevy_ptr Fix MSRVs for standalone crates (#16333) 2024-11-17 09:38:13 +00:00
bevy_reflect bevy_reflect: Add ReflectSerializerProcessor (#15548) 2024-11-17 14:05:39 +00:00
bevy_remote Expose BRP system scheduling and add system set (#16400) 2024-11-16 23:34:06 +00:00
bevy_render use wgpu patch 23.0.1 (#16513) 2024-11-25 22:47:31 +00:00
bevy_scene fix: add reflect to SceneInstanceReady and other observers/events (#16018) 2024-10-20 13:51:41 +00:00
bevy_sprite Add flags to SpritePlugin and UiPlugin to allow disabling their picking backend (without needing to disable features). (#16473) 2024-11-22 18:16:34 +00:00
bevy_state Fix typo in bevy_ecs (#16195) 2024-10-31 19:20:01 +00:00
bevy_tasks Support on_thread_spawn and on_thread_destroy for TaskPoolPlugin (#13045) 2024-11-11 20:00:01 +00:00
bevy_text Skip empty spans when updating text buffers (#16524) 2024-11-27 03:28:43 +00:00
bevy_time Use en-us locale for typos (#16037) 2024-10-20 18:55:17 +00:00
bevy_transform Improved the global transform api to access rotation and scale (#16211) 2024-11-04 15:35:16 +00:00
bevy_ui use scale factor for touches in UI focus (#16522) 2024-11-26 21:23:12 +00:00
bevy_utils AnimatedField and Rework Evaluators (#16484) 2024-11-27 22:19:55 +00:00
bevy_window Support prefers_home_indicator_hidden (#16005) 2024-10-31 16:09:30 +00:00
bevy_winit Handle failed cursor grab mode changes so that the cursor grab mode change can be attempted again (#16293) 2024-11-09 23:30:57 +00:00