-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.rs
184 lines (168 loc) · 5 KB
/
ui.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
use bevy::{
app::AppExit,
prelude::{
AssetServer, Commands, EventWriter, Plugin, Res,
ResMut, World,
},
};
use iyes_loopless::state::{CurrentState, NextState};
use kayak_ui::{
bevy::{
BevyContext, BevyKayakUIPlugin, FontMapping,
ImageManager, UICameraBundle,
},
core::{
bind, render, rsx,
styles::{
Corner, Edge, LayoutType, Style, StyleProp,
Units,
},
widget, Binding, Bound, Color, EventType, Index,
MutableBound, OnEvent,
},
widgets::{App, If, NinePatch, Text},
};
use crate::{
assets::ImageAssets, GameState, STARTING_GAME_STATE,
};
mod button;
pub struct UiPlugin;
impl Plugin for UiPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_plugin(BevyKayakUIPlugin)
.insert_resource(bind(STARTING_GAME_STATE))
.add_startup_system(game_ui)
.add_system(bind_gamestate);
}
}
pub fn bind_gamestate(
state: Res<CurrentState<GameState>>,
binding: Res<Binding<GameState>>,
) {
if state.is_changed() {
binding.set(state.0);
}
}
// THIS ONLY RUNS ONCE. VERY IMPORTANT FACT.
pub fn game_ui(
mut commands: Commands,
mut font_mapping: ResMut<FontMapping>,
asset_server: Res<AssetServer>,
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.set_default(
asset_server.load("roboto.kayak_font"),
);
let context = BevyContext::new(|context| {
render! {
<App>
<GameMenu/>
</App>
}
});
commands.insert_resource(context);
}
#[widget]
fn GameMenu() {
let container_styles = Style {
border_radius: StyleProp::Value(Corner::all(15.0)),
background_color: StyleProp::Value(Color::WHITE),
bottom: StyleProp::Value(Units::Stretch(1.0)),
height: StyleProp::Value(Units::Pixels(500.0)),
layout_type: StyleProp::Value(LayoutType::Column),
left: StyleProp::Value(Units::Stretch(1.0)),
padding: StyleProp::Value(Edge::all(
Units::Stretch(1.0),
)),
right: StyleProp::Value(Units::Stretch(1.0)),
row_between: StyleProp::Value(Units::Pixels(20.0)),
top: StyleProp::Value(Units::Stretch(1.0)),
width: StyleProp::Value(Units::Pixels(360.0)),
..Default::default()
};
let show_menus = {
let gamestate = context
.query_world::<Res<Binding<GameState>>, _, _>(
|state| state.clone(),
);
context.bind(&gamestate);
gamestate.get() == GameState::Menu
};
let green_panel = context
.query_world::<Res<ImageAssets>, _, _>(|assets| {
assets.green_panel.clone()
});
let container = context
.get_global_mut::<World>()
.map(|mut world| {
world
.get_resource_mut::<ImageManager>()
.unwrap()
.get(&green_panel)
})
.unwrap();
let on_click_new_game =
OnEvent::new(|ctx, event| match event.event_type {
EventType::Click(..) => {
let mut world =
ctx.get_global_mut::<World>().unwrap();
world.insert_resource(NextState(
GameState::Playing,
));
}
_ => {}
});
let on_click_settings =
OnEvent::new(|_, event| match event.event_type {
EventType::Click(..) => {
dbg!("clicked settings");
}
_ => {}
});
let on_click_exit =
OnEvent::new(|ctx, event| match event.event_type {
EventType::Click(..) => {
ctx
.query_world::<EventWriter<AppExit>, _, _>(
|mut exit| {
exit.send(AppExit);
},
);
}
_ => {}
});
rsx! {
<If condition={show_menus}>
<NinePatch
styles={Some(container_styles)}
border={Edge::all(10.0)}
handle={container}
>
<button::SnakeButton
on_event={Some(on_click_new_game)}
>
<Text
size={20.0}
content={"New Game".to_string()}
/>
</button::SnakeButton>
<button::SnakeButton
on_event={Some(on_click_settings)}
>
<Text
size={20.0}
content={"Settings".to_string()}
/>
</button::SnakeButton>
<button::SnakeButton
on_event={Some(on_click_exit)}
>
<Text
size={20.0}
content={"Exit".to_string()}
/>
</button::SnakeButton>
</NinePatch>
</If>
}
}