Newer
Older
use bevy::prelude::*;
use kayak_ui::prelude::*;
use kayak_ui::widgets::{NinePatch, NinePatchBundle, TextProps, TextWidgetBundle};
use num_traits::AsPrimitive;
use crate::assets::AssetHandles;
use crate::parent_widget;
use crate::ui::components::{IconContent, InsetIconProps, InsetIconWidget};
use crate::ui::prelude::{edge_px, px, stretch, val_auto, value};
#[derive(Component, Clone, PartialEq, Default)]
pub struct ButtonWidgetProps {
pub text: String,
pub left_icon: IconContent,
pub right_icon: IconContent,
pub font_size: f32,
pub is_disabled: bool,
pub is_fixed: bool,
}
impl ButtonWidgetProps {
pub fn text(text: impl ToString, font_size: impl AsPrimitive<f32>) -> Self {
Self {
text: text.to_string(),
font_size: font_size.as_(),
..Default::default()
}
}
}
pub fn button_props(text: impl ToString, is_disabled: bool, is_fixed: bool) -> ButtonWidgetProps {
ButtonWidgetProps {
text: text.to_string(),
is_fixed,
is_disabled,
font_size: 32.0,
left_icon: IconContent::None,
right_icon: IconContent::None,
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
}
}
impl Widget for ButtonWidgetProps {}
parent_widget!(ButtonWidgetProps => ButtonWidget);
#[derive(Component, PartialEq, Clone, Default)]
pub struct ButtonWidgetState {
pub is_pressed: bool,
pub is_hovered: bool,
}
pub fn render_button_widget(
In((mut widget_context, entity)): In<(KayakWidgetContext, Entity)>,
mut commands: Commands,
state_query: Query<&ButtonWidgetState>,
mut query: Query<(&ButtonWidgetProps, &KChildren, &mut ComputedStyles, &KStyle)>,
assets: Res<AssetHandles>,
) -> bool {
if let Ok((props, children, mut computed, style)) = query.get_mut(entity) {
let state_entity =
widget_context.use_state(&mut commands, entity, ButtonWidgetState::default());
let parent_id = Some(entity);
if let Ok(state) = state_query.get(state_entity) {
let events = OnEvent::new(
move |In((event_dispatcher_context, _, mut event, _)): In<(
EventDispatcherContext,
WidgetState,
Event,
Entity,
)>,
mut params: ParamSet<(
Query<&ButtonWidgetProps>,
Query<&mut ButtonWidgetState>,
)>| {
let widget_props = match params.p0().get(entity) {
Ok(p) => p.clone(),
Err(..) => return (event_dispatcher_context, event),
};
if let Ok(mut state) = params.p1().get_mut(state_entity) {
match &event.event_type {
EventType::Hover(..) | EventType::MouseIn(..) => {
if !widget_props.is_disabled {
state.is_hovered = true;
}
}
EventType::MouseOut(..) => {
state.is_hovered = false;
state.is_pressed = false;
}
EventType::MouseDown(..) => {
if !widget_props.is_disabled {
state.is_pressed = true;
}
}
EventType::MouseUp(..) => {
state.is_pressed = false;
}
EventType::Click(..) => {
if widget_props.is_disabled {
event.prevent_default();
event.stop_propagation();
}
}
_ => {}
}
}
(event_dispatcher_context, event)
},
);
let button_height = props.font_size + 2.0 + 24.0; // + 8.0;
let nine_vals = if props.is_disabled {
NinePatch {
handle: assets.image("button_disabled"),
border: Edge::all(12.0),
}
} else if state.is_pressed {
NinePatch {
handle: assets.image("button_down"),
border: Edge::all(12.0),
}
} else if state.is_hovered {
NinePatch {
handle: assets.image("button_active"),
border: Edge::all(12.0),
}
} else {
NinePatch {
handle: assets.image("button_idle"),
border: Edge::all(12.0),
}
};
let sizing = if state.is_pressed {
KStyle {
top: px(11.0),
right: stretch(1.0),
bottom: px(11.0),
left: stretch(1.0),
..Default::default()
}
KStyle {
top: px(8.0),
right: stretch(1.0),
bottom: px(14.0),
left: stretch(1.0),
..Default::default()
}
};
*computed = KStyle {
render_command: value(RenderCommand::Layout),
min_height: px(32.0),
min_width: px(32.0),
height: px(button_height),
padding: value(Edge::all(Units::Stretch(0.0))),
..Default::default()
}
.with_style(style)
.into();
let ninepatch_styles = KStyle {
layout_type: value(LayoutType::Row),
col_between: px(15.0),
..Default::default()
};
let text_style = KStyle {
color: value(Color::BLACK),
..Default::default()
}
.with_style(if state.is_pressed {
KStyle {
top: px(6.0),
right: stretch(1.0),
bottom: px(16.0),
left: stretch(1.0),
..Default::default()
}
} else {
KStyle {
top: px(3.0),
right: stretch(1.0),
bottom: px(19.0),
left: stretch(1.0),
..Default::default()
}
});
rsx! {
<NinePatchBundle
on_event={events}
nine_patch={nine_vals}
styles={ninepatch_styles}
>
{ if !props.left_icon.is_none() {
constructor!(
<InsetIconWidget
styles={sizing.clone()}
props={InsetIconProps { image: props.left_icon.clone(), size: props.font_size }}
/>
);
}}
<TextWidgetBundle
text={TextProps {
content: props.text.clone(),
word_wrap: false,
subpixel: true,
size: props.font_size,
line_height: Some(props.font_size + 2.0),
..Default::default()
}}
styles={text_style}
/>
{ if !props.right_icon.is_none() {
constructor!(
<InsetIconWidget
styles={sizing.clone()}
props={InsetIconProps { image: props.right_icon.clone(), size: props.font_size }}
/>
);
}}