Added slerp example for angle units and fixed it.

This commit is contained in:
Samuel Guerra 2023-08-17 01:32:25 -03:00
parent 1d94827691
commit fbcc75af43
2 changed files with 30 additions and 9 deletions

View File

@ -1,5 +1,5 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use zero_ui::prelude::*;
use zero_ui::prelude::{units::AngleRadian, *};
use zero_ui_view_prebuilt as zero_ui_view;
@ -35,6 +35,8 @@ fn app_main() {
transformed("Skew-X 15º", skew_x(15.deg())),
transformed("Scale 130%", scale(130.pct())),
transformed("Identity", Transform::identity()),
transformed_sampler("Lerp", animation::Transition::sample),
transformed_sampler("Slerp", units::slerp_sampler),
];
},
Stack! {
@ -172,6 +174,28 @@ fn transformed_at(label: impl Into<Txt>, transform: Transform, origin: impl Into
border = 2, (colors::GRAY, BorderStyle::Dashed);
}
}
fn transformed_sampler(
label: impl Into<Txt>,
sampler: impl Fn(&animation::Transition<AngleRadian>, EasingStep) -> AngleRadian + Send + Sync + 'static,
) -> impl UiNode {
Container! {
child = {
let is_hovered = var(false);
Container! {
rotate = is_hovered
.map(|&hovered| if !hovered { 20.deg() } else { (360 - 20).deg() }.into())
.easing_with(300.ms(), easing::linear, sampler);
child = Text!(label.into());
background_color = color_scheme_map(colors::BROWN.with_alpha(80.pct()), hex!(#EF6950).with_alpha(80.pct()));
padding = 10;
is_hovered;
}
};
border = 2, (colors::GRAY, BorderStyle::Dashed);
}
}
fn transform_stack() -> impl UiNode {
// the panel widget uses its child transform to position the widget for performance reasons,

View File

@ -416,12 +416,9 @@ impl AngleUnits for i32 {
}
fn slerp(from: f32, to: f32, turn: f32, factor: Factor) -> f32 {
let from = from.rem_euclid(turn);
let to = to.rem_euclid(turn);
if (from - to).abs() > turn * 0.5 {
to.lerp(&from, factor)
} else {
from.lerp(&to, factor)
}
let angle_to = {
let d = (to - from) % turn;
2.0 * d % turn - d
};
from + angle_to * factor.0
}