-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
229 lines (209 loc) · 7.34 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// A simple game menu, with buttons that use a nine-patch system for design (i.e., composed of
// images for the corners and middle segments) and an image to the right of the buttons. For normal
// screen sizes, the menu is centered in the middle of the screen For 400px width and lower, the
// buttons fill the screen width and the image is above the buttons.
use std::sync::OnceLock;
use bevy::{prelude::*, window::WindowResized};
use bevy_nine_slice_ui::{prelude::*, NineSliceUiMaterialBundle};
use futures_signals::signal::Mutable;
use haalka::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
position: WindowPosition::Centered(MonitorSelection::Primary),
..default()
}),
..default()
})
.set(ImagePlugin::default_nearest()),
HaalkaPlugin,
NineSliceUiPlugin::default(),
))
.add_systems(Startup, (setup, ui_root).chain())
.add_systems(Update, on_resize)
.insert_resource(Width(Mutable::new(0.)))
.run();
}
const BASE_SIZE: f32 = 600.;
const GAP: f32 = 10.;
static NINE_SLICE_TEXTURE_ATLAS: OnceLock<Handle<Image>> = OnceLock::new();
pub fn nine_slice_texture_atlas() -> &'static Handle<Image> {
NINE_SLICE_TEXTURE_ATLAS
.get()
.expect("expected NINE_SLICE_TEXTURE_ATLAS to be initialized")
}
static IMAGE: OnceLock<Handle<Image>> = OnceLock::new();
pub fn image() -> &'static Handle<Image> {
IMAGE.get().expect("expected IMAGE to be initialized")
}
struct NineSliceEl<Bundle = NineSliceUiMaterialBundle>(El<Bundle>);
impl_haalka_methods! {
NineSliceEl => {
NineSliceUiMaterialBundle => [
style: Style,
nine_slice_texture: NineSliceUiTexture,
],
},
}
impl NineSliceEl {
pub fn new(frame_signal: impl Signal<Item = usize> + Send + 'static) -> Self {
Self(
El::from(NineSliceUiMaterialBundle {
nine_slice_texture: NineSliceUiTexture::from_slice(
nine_slice_texture_atlas().clone(),
Rect::new(0., 0., 32., 32.),
),
..default()
})
)
.on_signal_with_nine_slice_texture(frame_signal, |nine_slice, frame| {
if let Some(bounds) = &mut nine_slice.bounds {
bounds.min.x = frame as f32 * 32.;
bounds.max.x = 32. + frame as f32 * 32.;
}
})
}
}
impl ElementWrapper for NineSliceEl {
type EL = El<NineSliceUiMaterialBundle>;
fn element_mut(&mut self) -> &mut Self::EL {
&mut self.0
}
}
fn nine_slice_button() -> impl Element {
let hovered = Mutable::new(false);
let pressed = Mutable::new(false);
NineSliceEl::new(map_ref! {
let hovered = hovered.signal(),
let pressed = pressed.signal() => {
if *pressed {
2
} else if *hovered {
1
} else {
0
}
}
})
.with_style(|style| {
style.width = Val::Px(100.);
style.height = Val::Px(50.);
})
.hovered_sync(hovered)
.pressed_sync(pressed)
}
#[derive(Resource)]
struct Width(Mutable<f32>);
fn horizontal() -> RawHaalkaEl<NodeBundle> {
Row::<NodeBundle>::new()
.with_style(|style| {
style.width = Val::Percent(100.);
style.height = Val::Percent(100.);
style.column_gap = Val::Px(GAP);
})
.item(
Column::<NodeBundle>::new()
.with_style(|style| {
style.width = Val::Percent(50.);
style.height = Val::Percent(100.);
style.row_gap = Val::Px(GAP);
})
.align_content(Align::center())
.items((0..8).into_iter().map(|_| nine_slice_button())),
)
.item(El::<ImageBundle>::new().image(UiImage::new(image().clone())))
.into_raw()
}
fn vertical() -> RawHaalkaEl<NodeBundle> {
Column::<NodeBundle>::new()
.with_style(|style| {
style.width = Val::Percent(100.);
style.height = Val::Percent(100.);
style.row_gap = Val::Px(GAP);
})
.item(El::<ImageBundle>::new().image(UiImage::new(image().clone())))
.item(
Row::<NodeBundle>::new()
.multiline()
.align_content(Align::center())
.with_style(|style| {
style.width = Val::Percent(100.);
style.height = Val::Percent(50.);
style.column_gap = Val::Px(GAP);
})
.items((0..8).into_iter().map(|_| nine_slice_button())),
)
.into_raw()
}
fn menu(width: Mutable<f32>) -> impl Element {
NineSliceEl::new(always(3))
.with_style(|style| {
style.height = Val::Px(BASE_SIZE);
style.padding = UiRect::all(Val::Px(GAP));
})
.on_signal_with_style(
width.signal().map(|width| BASE_SIZE.min(width)).dedupe(),
|style, width| style.width = Val::Px(width),
)
.update_raw_el(|raw_el| {
raw_el.child_signal(
width
.signal()
.map(|width| width > 400.)
.dedupe()
.map_bool(horizontal, vertical),
)
})
}
fn ui_root(world: &mut World) {
let width = world.resource::<Width>().0.clone();
El::<NodeBundle>::new()
.with_style(|style| {
style.width = Val::Percent(100.);
style.height = Val::Percent(100.);
})
.align_content(Align::center())
.child(
Column::<NodeBundle>::new()
.with_style(|style| style.row_gap = Val::Px(GAP))
.item(
Row::<NodeBundle>::new()
.with_style(|style| style.padding.left = Val::Px(GAP))
.item(El::<TextBundle>::new().text(Text::from_section(
"width: ",
TextStyle {
font_size: 40.,
..default()
},
)))
.item(El::<TextBundle>::new().text_signal(width.signal().map(|width| {
Text::from_section(
width.to_string(),
TextStyle {
font_size: 40.,
..default()
},
)
}))),
)
.item(menu(width)),
)
.spawn(world);
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
NINE_SLICE_TEXTURE_ATLAS
.set(asset_server.load("panels.png"))
.expect("failed to initialize NINE_SLICE_TEXTURE_ATLAS");
IMAGE
.set(asset_server.load("icon.png"))
.expect("failed to initialize IMAGE");
commands.spawn(Camera2dBundle::default());
}
fn on_resize(width: Res<Width>, mut resize_events: EventReader<WindowResized>) {
for event in resize_events.read() {
width.0.set(event.width)
}
}