Enum mooeye::scene_manager::SceneSwitch
source · pub enum SceneSwitch {
None,
Pop(u32),
Push(Box<dyn Scene>),
Replace(u32, Box<dyn Scene>),
}
Expand description
A SceneSwitch. An element of this type is returned from every scene every frame to check if the scene wants to switch to another scene.
Variants§
None
No scene switch. The current scene stays the top scene and continues running. This should be the default return value of Scene::update
Pop(u32)
Removes the specified number of scenes from the top of the scene stack (especially the current scene, ending it).
Push(Box<dyn Scene>)
Pushes a new Scene on top of the scene stack, thus temporarily halting running of the current scene. Current scene will resume as soon as this scene above is popped of the stack.
Replace(u32, Box<dyn Scene>)
Pops a specified numer of scenes (as with SceneSwitch::Pop) of the stack and pushes a new one in the same action.
Implementations§
source§impl SceneSwitch
impl SceneSwitch
sourcepub fn none() -> Self
pub fn none() -> Self
Creates an instance of SceneSwitch::None.
sourcepub fn push(scene: impl Scene + 'static) -> Self
pub fn push(scene: impl Scene + 'static) -> Self
Creates an instance of SceneSwitch::Push, handling the boxing for you.
Examples found in repository?
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
fn update(&mut self, ctx: &mut Context) -> Result<scene_manager::SceneSwitch, GameError> {
let messages = self.gui.manage_messages(ctx, None);
// Scene switches for different scenes
if messages.contains(&ui::UiMessage::Triggered(1)) {
return Ok(scene_manager::SceneSwitch::push(
crate::b_scene::BScene::new(ctx),
));
}
if messages.contains(&ui::UiMessage::Triggered(2)) {
return Ok(scene_manager::SceneSwitch::push(
crate::c_uielement::CScene::new(ctx),
));
}
if messages.contains(&ui::UiMessage::Triggered(3)) {
return Ok(scene_manager::SceneSwitch::push(
crate::d_containers::DScene::new(ctx)?,
));
}
if messages.contains(&ui::UiMessage::Triggered(4)) {
return Ok(scene_manager::SceneSwitch::push(
crate::e_messages::EScene::new(ctx)?,
));
}
if messages.contains(&ui::UiMessage::Triggered(5)) {
return Ok(scene_manager::SceneSwitch::push(
crate::f_sprites::FScene::new(ctx)?,
));
}
// Exit
if messages.contains(&ui::UiMessage::Triggered(6)) {
return Ok(scene_manager::SceneSwitch::pop(1));
}
Ok(scene_manager::SceneSwitch::None)
}
sourcepub fn pop(pop_amount: u32) -> Self
pub fn pop(pop_amount: u32) -> Self
Creates an instance of SceneSwitch::Pop.
Examples found in repository?
154 155 156 157 158 159 160 161 162 163 164 165
fn update(&mut self, ctx: &mut Context) -> Result<scene_manager::SceneSwitch, GameError> {
// Nothing much to do here, except implement the back button functionality.
let messages = self.gui.manage_messages(ctx, None);
if messages.contains(&ui::UiMessage::Triggered(1)) {
// If it is, we end the current scene (and return to the previous one) by popping it off the stack.
return Ok(scene_manager::SceneSwitch::pop(1));
}
Ok(scene_manager::SceneSwitch::None)
}
More examples
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
fn update(&mut self, ctx: &mut Context) -> Result<scene_manager::SceneSwitch, GameError> {
// Usually, we would first perform our game logic here, but this scene has no logic.
// You can get messages sent by your UI with the manage_messages function.
// Usually, you also pass in extern messages created by your game state to bring the UI up to date. Since we don't have a game state, we can pass None (this is useful for menu scenes and similar).
let messages = self.gui.manage_messages(ctx, None);
// We then check if our button has been clicked by creating a Clicked event with the correct ID and checking if it is contained in the messages set.
if messages.contains(&ui::UiMessage::Triggered(1)){
// If it is, we end the current scene (and return to the previous one) by popping it off the stack.
return Ok(scene_manager::SceneSwitch::pop(1));
}
// Otherwise, no scene switch is neccessary.
Ok(scene_manager::SceneSwitch::None)
}
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
fn update(&mut self, ctx: &mut Context) -> Result<scene_manager::SceneSwitch, GameError> {
// Actually implementing some game state logic.
// Pressing space changes the variant of the sprite.
if ctx
.keyboard
.is_key_just_pressed(winit::event::VirtualKeyCode::Space)
{
self.sprite.set_variant(self.sprite.get_variant() + 1);
}
// Move the sprite.
self.pos += self.v;
// Make the sprite bounce off the screen edges.
let scaling = 5.;
if self.pos.x - scaling * 4. < 0. || self.pos.x + scaling * 4. >= ctx.gfx.drawable_size().0
{
self.v.x *= -1.;
}
if self.pos.y - scaling * 8. < 0. || self.pos.y + scaling * 8. >= ctx.gfx.drawable_size().1
{
self.v.y *= -1.;
}
// And handle messages as usual
let messages = self.gui.manage_messages(ctx, None);
if messages.contains(&ui::UiMessage::Triggered(1)) {
return Ok(scene_manager::SceneSwitch::pop(1));
}
Ok(scene_manager::SceneSwitch::None)
}
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
fn update(&mut self, ctx: &mut Context) -> Result<scene_manager::SceneSwitch, GameError> {
let messages = self.gui.manage_messages(ctx, None);
// Scene switches for different scenes
if messages.contains(&ui::UiMessage::Triggered(1)) {
return Ok(scene_manager::SceneSwitch::push(
crate::b_scene::BScene::new(ctx),
));
}
if messages.contains(&ui::UiMessage::Triggered(2)) {
return Ok(scene_manager::SceneSwitch::push(
crate::c_uielement::CScene::new(ctx),
));
}
if messages.contains(&ui::UiMessage::Triggered(3)) {
return Ok(scene_manager::SceneSwitch::push(
crate::d_containers::DScene::new(ctx)?,
));
}
if messages.contains(&ui::UiMessage::Triggered(4)) {
return Ok(scene_manager::SceneSwitch::push(
crate::e_messages::EScene::new(ctx)?,
));
}
if messages.contains(&ui::UiMessage::Triggered(5)) {
return Ok(scene_manager::SceneSwitch::push(
crate::f_sprites::FScene::new(ctx)?,
));
}
// Exit
if messages.contains(&ui::UiMessage::Triggered(6)) {
return Ok(scene_manager::SceneSwitch::pop(1));
}
Ok(scene_manager::SceneSwitch::None)
}
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
fn update(&mut self, ctx: &mut Context) -> Result<scene_manager::SceneSwitch, GameError> {
// Nothing much to do here, except implement the back button functionality.
let messages = self.gui.manage_messages(ctx, None);
if messages.contains(&ui::UiMessage::Triggered(1)){
// If it is, we end the current scene (and return to the previous one) by popping it off the stack.
return Ok(scene_manager::SceneSwitch::pop(1));
}
if messages.contains(&ui::UiMessage::Triggered(13)){
// If a certain button is pressed, add a small text element to the gui.
self.gui.add_element(100,
// using a duration box as a wrapper will remove the element after a set amount of time
ui::containers::DurationBox::new(
Duration::from_secs_f32(1.5),
graphics::Text::new("Just a small reminder that you pressed button 13.")
.set_font("Bahnschrift")
.set_wrap(true)
.set_bounds(glam::Vec2::new(200., 500.))
.set_scale(28.)
.to_owned()
.to_element_builder(0, ctx)
.with_visuals(ui::Visuals::new(
Color::from_rgb(77, 109, 191),
Color::from_rgb(55, 67, 87),
2.,
4.,
))
.build()
).to_element_builder(0, ctx)
.with_alignment(ui::Alignment::Center, ui::Alignment::Min)
.with_offset(0., 25.)
.build()
);
}
Ok(scene_manager::SceneSwitch::None)
}
sourcepub fn replace(scene: impl Scene + 'static, pop_amount: u32) -> Self
pub fn replace(scene: impl Scene + 'static, pop_amount: u32) -> Self
Creates an instance of SceneSwitch::Replace, handling the boxing for you.