forked from amethyst/amethyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
179 lines (160 loc) · 5.96 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
//! Displays a shaded sphere to the user.
use amethyst::{
assets::{PrefabLoader, PrefabLoaderSystemDesc, Processor, RonFormat},
audio::{output::init_output, Source},
core::{frame_limiter::FrameRateLimitStrategy, transform::TransformBundle, Time},
derive::SystemDesc,
ecs::prelude::{Entity, System, SystemData, WorldExt, Write},
input::{is_close_requested, is_key_down, InputBundle, StringBindings},
prelude::*,
renderer::{
plugins::RenderToWindow,
rendy::mesh::{Normal, Position, TexCoord},
types::DefaultBackend,
RenderingBundle,
},
shrev::{EventChannel, ReaderId},
ui::{RenderUi, UiBundle, UiCreator, UiEvent, UiFinder, UiText},
utils::{
application_root_dir,
fps_counter::{FpsCounter, FpsCounterBundle},
scene::BasicScenePrefab,
},
winit::VirtualKeyCode,
};
use log::info;
type MyPrefabData = BasicScenePrefab<(Vec<Position>, Vec<Normal>, Vec<TexCoord>)>;
#[derive(Default)]
struct Example {
fps_display: Option<Entity>,
random_text: Option<Entity>,
}
impl SimpleState for Example {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let StateData { mut world, .. } = data;
// Initialise the scene with an object, a light and a camera.
let handle = world.exec(|loader: PrefabLoader<'_, MyPrefabData>| {
loader.load("prefab/sphere.ron", RonFormat, ())
});
world.create_entity().with(handle).build();
init_output(&mut world);
world.exec(|mut creator: UiCreator<'_>| {
creator.create("ui/example.ron", ());
});
}
fn handle_event(
&mut self,
_: StateData<'_, GameData<'_, '_>>,
event: StateEvent,
) -> SimpleTrans {
match &event {
StateEvent::Window(event) => {
if is_close_requested(&event) || is_key_down(&event, VirtualKeyCode::Escape) {
Trans::Quit
} else {
Trans::None
}
}
StateEvent::Ui(ui_event) => {
info!(
"[HANDLE_EVENT] You just interacted with a ui element: {:?}",
ui_event
);
Trans::None
}
StateEvent::Input(input) => {
info!("Input Event detected: {:?}.", input);
Trans::None
}
}
}
fn update(&mut self, state_data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
let StateData { world, .. } = state_data;
if self.fps_display.is_none() {
world.exec(|finder: UiFinder<'_>| {
if let Some(entity) = finder.find("fps") {
self.fps_display = Some(entity);
}
});
}
if self.random_text.is_none() {
world.exec(|finder: UiFinder| {
if let Some(entity) = finder.find("random_text") {
self.random_text = Some(entity);
}
});
}
let mut ui_text = world.write_storage::<UiText>();
{
if let Some(fps_display) = self.fps_display.and_then(|entity| ui_text.get_mut(entity)) {
if world.read_resource::<Time>().frame_number() % 20 == 0 {
let fps = world.read_resource::<FpsCounter>().sampled_fps();
fps_display.text = format!("FPS: {:.*}", 2, fps);
}
}
}
{
if let Some(random_text) = self.random_text.and_then(|entity| ui_text.get_mut(entity)) {
if let Ok(value) = random_text.text.parse::<i32>() {
let mut new_value = value * 10;
if new_value > 100_000 {
new_value = 1;
}
random_text.text = new_value.to_string();
} else {
random_text.text = String::from("1");
}
}
}
Trans::None
}
}
fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default());
let app_root = application_root_dir()?;
let display_config_path = app_root.join("examples/ui/config/display.ron");
let assets_dir = app_root.join("examples/assets");
let game_data = GameDataBuilder::default()
.with_system_desc(PrefabLoaderSystemDesc::<MyPrefabData>::default(), "", &[])
.with_bundle(TransformBundle::new())?
.with_bundle(InputBundle::<StringBindings>::new())?
.with_bundle(UiBundle::<StringBindings>::new())?
.with(Processor::<Source>::new(), "source_processor", &[])
.with_system_desc(UiEventHandlerSystemDesc::default(), "ui_event_handler", &[])
.with_bundle(FpsCounterBundle::default())?
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderUi::default()),
)?;
let mut game = Application::build(assets_dir, Example::default())?
// Unlimited FPS
.with_frame_limit(FrameRateLimitStrategy::Unlimited, 9999)
.build(game_data)?;
game.run();
Ok(())
}
/// This shows how to handle UI events.
#[derive(SystemDesc)]
#[system_desc(name(UiEventHandlerSystemDesc))]
pub struct UiEventHandlerSystem {
#[system_desc(event_channel_reader)]
reader_id: ReaderId<UiEvent>,
}
impl UiEventHandlerSystem {
pub fn new(reader_id: ReaderId<UiEvent>) -> Self {
Self { reader_id }
}
}
impl<'a> System<'a> for UiEventHandlerSystem {
type SystemData = Write<'a, EventChannel<UiEvent>>;
fn run(&mut self, events: Self::SystemData) {
// Reader id was just initialized above if empty
for ev in events.read(&mut self.reader_id) {
info!("[SYSTEM] You just interacted with a ui element: {:?}", ev);
}
}
}