wgui: make hover effects more responsive (remove fade-in duration, use cubic interpolation)
This commit is contained in:
@@ -8,8 +8,12 @@ use crate::{
|
|||||||
|
|
||||||
pub enum AnimationEasing {
|
pub enum AnimationEasing {
|
||||||
Linear,
|
Linear,
|
||||||
InQuad,
|
InQuad, // ^2
|
||||||
OutQuad,
|
InCubic, // ^3
|
||||||
|
InQuint, // ^5
|
||||||
|
OutQuad, // ^2
|
||||||
|
OutCubic, // ^3
|
||||||
|
OutQuint, // ^5
|
||||||
OutBack,
|
OutBack,
|
||||||
InBack,
|
InBack,
|
||||||
}
|
}
|
||||||
@@ -18,8 +22,12 @@ impl AnimationEasing {
|
|||||||
fn interpolate(&self, x: f32) -> f32 {
|
fn interpolate(&self, x: f32) -> f32 {
|
||||||
match self {
|
match self {
|
||||||
Self::Linear => x,
|
Self::Linear => x,
|
||||||
Self::InQuad => x * x,
|
Self::InQuad => x.powi(2),
|
||||||
Self::OutQuad => 1.0 - (1.0 - x) * (1.0 - x),
|
Self::InCubic => x.powi(3),
|
||||||
|
Self::InQuint => x.powi(5),
|
||||||
|
Self::OutQuad => 1.0 - (1.0 - x).powi(2),
|
||||||
|
Self::OutCubic => 1.0 - (1.0 - x).powi(3),
|
||||||
|
Self::OutQuint => 1.0 - (1.0 - x).powi(5),
|
||||||
Self::OutBack => {
|
Self::OutBack => {
|
||||||
let a = 1.7;
|
let a = 1.7;
|
||||||
let b = a + 1.0;
|
let b = a + 1.0;
|
||||||
@@ -60,12 +68,7 @@ pub struct Animation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Animation {
|
impl Animation {
|
||||||
pub fn new(
|
pub fn new(target_widget: WidgetID, ticks: u32, easing: AnimationEasing, callback: AnimationCallback) -> Self {
|
||||||
target_widget: WidgetID,
|
|
||||||
ticks: u32,
|
|
||||||
easing: AnimationEasing,
|
|
||||||
callback: AnimationCallback,
|
|
||||||
) -> Self {
|
|
||||||
Self::new_ex(target_widget, 0, ticks, easing, callback)
|
Self::new_ex(target_widget, 0, ticks, easing, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,9 +144,7 @@ impl Animations {
|
|||||||
anim.ticks_remaining -= 1;
|
anim.ticks_remaining -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self.running_animations.retain(|anim| anim.ticks_remaining > 0);
|
||||||
.running_animations
|
|
||||||
.retain(|anim| anim.ticks_remaining > 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process(&mut self, state: &LayoutState, alterables: &mut EventAlterables, alpha: f32) {
|
pub fn process(&mut self, state: &LayoutState, alterables: &mut EventAlterables, alpha: f32) {
|
||||||
|
|||||||
@@ -102,24 +102,11 @@ fn anim_hover(rect: &mut WidgetRectangle, data: &Data, pos: f32, pressed: bool)
|
|||||||
rect.params.border = 2.0;
|
rect.params.border = 2.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn anim_hover_in(data: Rc<Data>, state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
|
|
||||||
Animation::new(
|
|
||||||
widget_id,
|
|
||||||
2,
|
|
||||||
AnimationEasing::OutQuad,
|
|
||||||
Box::new(move |common, anim_data| {
|
|
||||||
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
|
|
||||||
anim_hover(rect, &data, anim_data.pos, state.borrow().down);
|
|
||||||
common.alterables.mark_redraw();
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn anim_hover_out(data: Rc<Data>, state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
|
fn anim_hover_out(data: Rc<Data>, state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
|
||||||
Animation::new(
|
Animation::new(
|
||||||
widget_id,
|
widget_id,
|
||||||
8,
|
15,
|
||||||
AnimationEasing::OutQuad,
|
AnimationEasing::OutCubic,
|
||||||
Box::new(move |common, anim_data| {
|
Box::new(move |common, anim_data| {
|
||||||
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
|
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
|
||||||
anim_hover(rect, &data, 1.0 - anim_data.pos, state.borrow().down);
|
anim_hover(rect, &data, 1.0 - anim_data.pos, state.borrow().down);
|
||||||
@@ -140,10 +127,11 @@ fn register_event_mouse_enter<U1, U2>(
|
|||||||
EventListenerKind::MouseEnter,
|
EventListenerKind::MouseEnter,
|
||||||
Box::new(move |common, event_data, _, _| {
|
Box::new(move |common, event_data, _, _| {
|
||||||
common.alterables.trigger_haptics();
|
common.alterables.trigger_haptics();
|
||||||
common
|
common.alterables.mark_redraw();
|
||||||
.alterables
|
let rect = event_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
|
||||||
.animate(anim_hover_in(data.clone(), state.clone(), event_data.widget_id));
|
let mut state = state.borrow_mut();
|
||||||
state.borrow_mut().hovered = true;
|
anim_hover(rect, &data, 1.0, state.down);
|
||||||
|
state.hovered = true;
|
||||||
Ok(())
|
Ok(())
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user