settings for text+bg colors, anim speed, rounding

This commit is contained in:
galister
2025-12-22 15:03:17 +09:00
parent b9513c3c36
commit a0bc4001c0
16 changed files with 134 additions and 76 deletions

View File

@@ -1,29 +1,30 @@
use crate::{
animation::{Animation, AnimationEasing},
assets::AssetPath,
components::{self, Component, ComponentBase, ComponentTrait, RefreshData, tooltip::ComponentTooltip},
components::{self, tooltip::ComponentTooltip, Component, ComponentBase, ComponentTrait, RefreshData},
drawing::{self, Boundary, Color},
event::{CallbackDataCommon, EventListenerCollection, EventListenerID, EventListenerKind},
i18n::Translation,
layout::{LayoutTask, WidgetID, WidgetPair},
renderer_vk::{
text::{
FontWeight, TextStyle,
custom_glyph::{CustomGlyphContent, CustomGlyphData},
FontWeight, TextStyle,
},
util::centered_matrix,
},
widget::{
self, ConstructEssentials, EventResult, WidgetData,
self,
label::{WidgetLabel, WidgetLabelParams},
rectangle::{WidgetRectangle, WidgetRectangleParams},
sprite::{WidgetSprite, WidgetSpriteParams},
util::WLength,
ConstructEssentials, EventResult, WidgetData,
},
};
use glam::{Mat4, Vec3};
use std::{cell::RefCell, rc::Rc};
use taffy::{AlignItems, JustifyContent, prelude::length};
use taffy::{prelude::length, AlignItems, JustifyContent};
pub struct Params<'a> {
pub text: Option<Translation>, // if unset, label will not be populated
@@ -170,10 +171,13 @@ impl ComponentButton {
return;
}
let anim_mult = common.state.globals.defaults().animation_mult;
let anim_ticks = if sticky_down { 5. } else { 10. };
let state = self.state.clone();
let anim = Animation::new(
self.data.id_rect,
if sticky_down { 5 } else { 10 },
(anim_ticks * anim_mult) as _,
AnimationEasing::OutCubic,
Box::new(move |common, anim_data| {
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -227,10 +231,10 @@ fn anim_hover(
rect.params.border_color = init_border_color.lerp(&colors.hover_border_color, mult);
}
fn anim_hover_create(state: Rc<RefCell<State>>, widget_id: WidgetID, fade_in: bool) -> Animation {
fn anim_hover_create(state: Rc<RefCell<State>>, widget_id: WidgetID, fade_in: bool, anim_mult: f32) -> Animation {
Animation::new(
widget_id,
if fade_in { 5 } else { 10 },
((if fade_in { 5. } else { 10. }) * anim_mult) as _,
AnimationEasing::OutCubic,
Box::new(move |common, anim_data| {
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -254,6 +258,7 @@ fn register_event_mouse_enter(
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
info: Option<components::tooltip::TooltipInfo>,
anim_mult: f32,
) -> EventListenerID {
listeners.register(
EventListenerKind::MouseEnter,
@@ -262,7 +267,7 @@ fn register_event_mouse_enter(
common.alterables.mark_redraw();
common
.alterables
.animate(anim_hover_create(state.clone(), event_data.widget_id, true));
.animate(anim_hover_create(state.clone(), event_data.widget_id, true, anim_mult));
if let Some(info) = info.clone() {
common.alterables.tasks.push(LayoutTask::ModifyLayoutState({
@@ -282,14 +287,18 @@ fn register_event_mouse_enter(
)
}
fn register_event_mouse_leave(state: Rc<RefCell<State>>, listeners: &mut EventListenerCollection) -> EventListenerID {
fn register_event_mouse_leave(
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> EventListenerID {
listeners.register(
EventListenerKind::MouseLeave,
Box::new(move |common, event_data, (), ()| {
common.alterables.trigger_haptics();
common
.alterables
.animate(anim_hover_create(state.clone(), event_data.widget_id, false));
.animate(anim_hover_create(state.clone(), event_data.widget_id, false, anim_mult));
let mut state = state.borrow_mut();
state.active_tooltip = None;
state.hovered = false;
@@ -510,9 +519,16 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
id: root.id,
lhandles: {
let mut widget = ess.layout.state.widgets.get(id_rect).unwrap().state();
let anim_mult = ess.layout.state.globals.defaults().animation_mult;
vec![
register_event_mouse_enter(data.clone(), state.clone(), &mut widget.event_listeners, params.tooltip),
register_event_mouse_leave(state.clone(), &mut widget.event_listeners),
register_event_mouse_enter(
data.clone(),
state.clone(),
&mut widget.event_listeners,
params.tooltip,
anim_mult,
),
register_event_mouse_leave(state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_press(state.clone(), &mut widget.event_listeners),
register_event_mouse_release(data.clone(), state.clone(), &mut widget.event_listeners),
]

View File

@@ -1,7 +1,7 @@
use std::{cell::RefCell, rc::Rc};
use taffy::{
AlignItems,
prelude::{length, percent},
AlignItems,
};
use crate::{
@@ -13,10 +13,10 @@ use crate::{
layout::{self, WidgetID, WidgetPair},
renderer_vk::text::{FontWeight, TextStyle},
widget::{
ConstructEssentials, EventResult,
label::{WidgetLabel, WidgetLabelParams},
rectangle::{WidgetRectangle, WidgetRectangleParams},
util::WLength,
ConstructEssentials, EventResult,
},
};
@@ -120,10 +120,10 @@ fn anim_hover(rect: &mut WidgetRectangle, pos: f32, pressed: bool) {
}
}
fn anim_hover_in(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
fn anim_hover_in(state: Rc<RefCell<State>>, widget_id: WidgetID, anim_mult: f32) -> Animation {
Animation::new(
widget_id,
5,
(5. * anim_mult) as _,
AnimationEasing::OutQuad,
Box::new(move |common, anim_data| {
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -133,10 +133,10 @@ fn anim_hover_in(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
)
}
fn anim_hover_out(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
fn anim_hover_out(state: Rc<RefCell<State>>, widget_id: WidgetID, anim_mult: f32) -> Animation {
Animation::new(
widget_id,
8,
(8. * anim_mult) as _,
AnimationEasing::OutQuad,
Box::new(move |common, anim_data| {
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -146,28 +146,36 @@ fn anim_hover_out(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
)
}
fn register_event_mouse_enter(state: Rc<RefCell<State>>, listeners: &mut EventListenerCollection) -> EventListenerID {
fn register_event_mouse_enter(
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> EventListenerID {
listeners.register(
EventListenerKind::MouseEnter,
Box::new(move |common, event_data, (), ()| {
common.alterables.trigger_haptics();
common
.alterables
.animate(anim_hover_in(state.clone(), event_data.widget_id));
.animate(anim_hover_in(state.clone(), event_data.widget_id, anim_mult));
state.borrow_mut().hovered = true;
Ok(EventResult::Pass)
}),
)
}
fn register_event_mouse_leave(state: Rc<RefCell<State>>, listeners: &mut EventListenerCollection) -> EventListenerID {
fn register_event_mouse_leave(
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> EventListenerID {
listeners.register(
EventListenerKind::MouseLeave,
Box::new(move |common, event_data, (), ()| {
common.alterables.trigger_haptics();
common
.alterables
.animate(anim_hover_out(state.clone(), event_data.widget_id));
.animate(anim_hover_out(state.clone(), event_data.widget_id, anim_mult));
state.borrow_mut().hovered = false;
Ok(EventResult::Pass)
}),
@@ -341,9 +349,10 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
id: root.id,
lhandles: {
let mut widget = ess.layout.state.widgets.get(id_container).unwrap().state();
let anim_mult = ess.layout.state.globals.defaults().animation_mult;
vec![
register_event_mouse_enter(state.clone(), &mut widget.event_listeners),
register_event_mouse_leave(state.clone(), &mut widget.event_listeners),
register_event_mouse_enter(state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_leave(state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_press(state.clone(), &mut widget.event_listeners),
register_event_mouse_release(data.clone(), state.clone(), &mut widget.event_listeners),
]

View File

@@ -18,11 +18,11 @@ use crate::{
util,
},
widget::{
ConstructEssentials, EventResult,
div::WidgetDiv,
label::{WidgetLabel, WidgetLabelParams},
rectangle::{WidgetRectangle, WidgetRectangleParams},
util::WLength,
ConstructEssentials, EventResult,
},
};
@@ -263,10 +263,10 @@ fn anim_rect(rect: &mut WidgetRectangle, pos: f32) {
rect.params.border_color = drawing::Color::lerp(&HANDLE_BORDER_COLOR, &HANDLE_BORDER_COLOR_HOVERED, pos);
}
fn on_enter_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID) {
fn on_enter_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID, anim_mult: f32) {
common.alterables.animate(Animation::new(
handle_id,
20,
(20. * anim_mult) as _,
AnimationEasing::OutBack,
Box::new(move |common, data| {
let rect = data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -277,10 +277,10 @@ fn on_enter_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID) {
));
}
fn on_leave_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID) {
fn on_leave_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID, anim_mult: f32) {
common.alterables.animate(Animation::new(
handle_id,
10,
(10. * anim_mult) as _,
AnimationEasing::OutQuad,
Box::new(move |common, data| {
let rect = data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -295,13 +295,14 @@ fn register_event_mouse_enter(
data: Rc<Data>,
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> event::EventListenerID {
listeners.register(
EventListenerKind::MouseEnter,
Box::new(move |common, _data, (), ()| {
common.alterables.trigger_haptics();
state.borrow_mut().hovered = true;
on_enter_anim(common, data.slider_handle_rect_id);
on_enter_anim(common, data.slider_handle_rect_id, anim_mult);
Ok(EventResult::Pass)
}),
)
@@ -311,13 +312,14 @@ fn register_event_mouse_leave(
data: Rc<Data>,
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> event::EventListenerID {
listeners.register(
EventListenerKind::MouseLeave,
Box::new(move |common, _data, (), ()| {
common.alterables.trigger_haptics();
state.borrow_mut().hovered = false;
on_leave_anim(common, data.slider_handle_rect_id);
on_leave_anim(common, data.slider_handle_rect_id, anim_mult);
Ok(EventResult::Pass)
}),
)
@@ -500,12 +502,12 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
id: root.id,
lhandles: {
let mut widget = ess.layout.state.widgets.get(body_id).unwrap().state();
let anim_mult = ess.layout.state.globals.defaults().animation_mult;
vec![
register_event_mouse_enter(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_leave(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_enter(data.clone(), state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_leave(data.clone(), state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_motion(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_press(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_leave(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_release(state.clone(), &mut widget.event_listeners),
]
},

View File

@@ -22,7 +22,10 @@ pub struct Defaults {
pub accent_color: drawing::Color,
pub danger_color: drawing::Color,
pub faded_color: drawing::Color,
pub bg_color: drawing::Color,
pub translucent_alpha: f32,
pub animation_mult: f32,
pub rounding_mult: f32,
}
impl Default for Defaults {
@@ -34,7 +37,10 @@ impl Default for Defaults {
accent_color: drawing::Color::new(0.0, 0.54, 1.0, 1.0),
danger_color: drawing::Color::new(0.8, 0.0, 0.0, 1.0),
faded_color: drawing::Color::new(0.4, 0.5, 0.6, 1.0),
bg_color: drawing::Color::new(0.0039, 0.0078, 0.0235, 0.8352),
translucent_alpha: 0.25,
animation_mult: 1.0,
rounding_mult: 1.0,
}
}
}

View File

@@ -1,13 +1,13 @@
use crate::{
assets::AssetPath,
components::{Component, button, tooltip},
components::{button, tooltip, Component},
drawing::Color,
i18n::Translation,
layout::WidgetID,
parser::{
AttribPair, ParserContext, ParserFile, parse_check_f32, parse_check_i32, parse_children, print_invalid_attrib,
process_component,
parse_check_f32, parse_check_i32, parse_children, print_invalid_attrib, process_component,
style::{parse_color_opt, parse_round, parse_style, parse_text_style},
AttribPair, ParserContext, ParserFile,
},
widget::util::WLength,
};
@@ -45,7 +45,7 @@ pub fn parse_component_button<'a>(
translation = Some(Translation::from_translation_key(value));
}
"round" => {
parse_round(value, &mut round);
parse_round(value, &mut round, ctx.doc_params.globals.get().defaults.rounding_mult);
}
"color" => {
parse_color_opt(value, &mut color);

View File

@@ -8,7 +8,7 @@ mod widget_rectangle;
mod widget_sprite;
use crate::{
assets::{AssetPath, AssetPathOwned, normalize_path},
assets::{normalize_path, AssetPath, AssetPathOwned},
components::{Component, ComponentWeak},
drawing::{self},
globals::WguiGlobals,
@@ -400,6 +400,7 @@ impl ParserContext<'_> {
insert_color_vars!(self, "accent", def.accent_color, def.translucent_alpha);
insert_color_vars!(self, "danger", def.danger_color, def.translucent_alpha);
insert_color_vars!(self, "faded", def.faded_color, def.translucent_alpha);
insert_color_vars!(self, "bg", def.bg_color, def.translucent_alpha);
}
}

View File

@@ -6,22 +6,22 @@ use taffy::{
use crate::{
drawing,
parser::{
AttribPair, is_percent, parse_color_hex, parse_f32, parse_percent, parse_size_unit, parse_val,
print_invalid_attrib, print_invalid_value,
is_percent, parse_color_hex, parse_f32, parse_percent, parse_size_unit, parse_val, print_invalid_attrib,
print_invalid_value, AttribPair,
},
renderer_vk::text::{FontWeight, HorizontalAlign, TextStyle},
widget::util::WLength,
};
pub fn parse_round(value: &str, round: &mut WLength) {
pub fn parse_round(value: &str, round: &mut WLength, multiplier: f32) {
if is_percent(value) {
if let Some(val) = parse_percent(value) {
*round = WLength::Percent(val);
*round = WLength::Percent((val * multiplier).clamp(0., 1.));
} else {
print_invalid_value(value);
}
} else if let Some(val) = parse_f32(value) {
*round = WLength::Units(val);
*round = WLength::Units((val * multiplier).max(0.));
} else {
print_invalid_value(value);
}

View File

@@ -2,8 +2,9 @@ use crate::{
drawing::GradientMode,
layout::WidgetID,
parser::{
AttribPair, ParserContext, ParserFile, parse_children, parse_widget_universal, print_invalid_attrib,
parse_children, parse_widget_universal, print_invalid_attrib,
style::{parse_color, parse_round, parse_style},
AttribPair, ParserContext, ParserFile,
},
widget::rectangle::{WidgetRectangle, WidgetRectangleParams},
};
@@ -40,7 +41,11 @@ pub fn parse_widget_rectangle<'a>(
}
}
"round" => {
parse_round(value, &mut params.round);
parse_round(
value,
&mut params.round,
ctx.doc_params.globals.get().defaults.rounding_mult,
);
}
"border" => {
params.border = value.parse().unwrap_or_else(|_| {