Skip to content

Commit

Permalink
Support back mouse button
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 17, 2023
1 parent 86494bc commit c116a8f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
4 changes: 2 additions & 2 deletions neothesia/src/iced_utils/iced_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
winit::event::MouseButton::Left => mouse::Button::Left,
winit::event::MouseButton::Right => mouse::Button::Right,
winit::event::MouseButton::Middle => mouse::Button::Middle,
winit::event::MouseButton::Back => mouse::Button::Other(0),
winit::event::MouseButton::Forward => mouse::Button::Other(0),
winit::event::MouseButton::Back => mouse::Button::Other(99),
winit::event::MouseButton::Forward => mouse::Button::Other(98),
// winit::event::MouseButton::Back => mouse::Button::Back,
// winit::event::MouseButton::Forward => mouse::Button::Forward,
winit::event::MouseButton::Other(other) => mouse::Button::Other(other),
Expand Down
8 changes: 8 additions & 0 deletions neothesia/src/iced_utils/iced_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ pub trait Program: Sized {
/// These widgets can produce __messages__ based on user interaction.
fn view(&self, target: &Target) -> Element<'_, Self::Message>;

fn mouse_input(
&self,
_event: &iced_core::mouse::Event,
_target: &Target,
) -> Option<Self::Message> {
None
}

fn keyboard_input(
&self,
_event: &iced_core::keyboard::Event,
Expand Down
24 changes: 18 additions & 6 deletions neothesia/src/scene/menu_scene/iced_menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ impl Program for AppUi {
Command::none()
}

fn mouse_input(&self, event: &iced_core::mouse::Event, _target: &Target) -> Option<Message> {
if let iced_core::mouse::Event::ButtonPressed(iced_core::mouse::Button::Other(99)) = event {
Some(Message::GoToPage(self.current.previous_step()))
} else {
None
}
}

fn keyboard_input(
&self,
event: &iced_runtime::keyboard::Event,
Expand Down Expand Up @@ -186,12 +194,7 @@ impl Program for AppUi {
Step::Main => Some(Message::GoToPage(Step::TrackSelection)),
_ => None,
},
KeyCode::Escape => Some(match self.current {
Step::Exit => Message::GoToPage(Step::Main),
Step::Main => Message::GoToPage(Step::Exit),
Step::Settings => Message::GoToPage(Step::Main),
Step::TrackSelection => Message::GoToPage(Step::Main),
}),
KeyCode::Escape => Some(Message::GoToPage(self.current.previous_step())),
_ => None,
}
} else {
Expand All @@ -213,6 +216,15 @@ pub enum Step {
}

impl<'a> Step {
fn previous_step(&self) -> Self {
match self {
Step::Exit => Step::Main,
Step::Main => Step::Exit,
Step::Settings => Step::Main,
Step::TrackSelection => Step::Main,
}
}

fn view(&'a self, data: &'a Data, target: &Target) -> Element<Message> {
if data.is_loading {
return Self::loading(data);
Expand Down
14 changes: 11 additions & 3 deletions neothesia/src/scene/menu_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,18 @@ impl Scene for MenuScene {
) {
self.iced_state.queue_event(event.clone());

if let iced_core::event::Event::Keyboard(event) = &event {
if let Some(msg) = self.iced_state.program().keyboard_input(event, target) {
self.iced_state.queue_message(msg);
match &event {
iced_core::event::Event::Mouse(event) => {
if let Some(msg) = self.iced_state.program().mouse_input(event, target) {
self.iced_state.queue_message(msg);
}
}
iced_core::event::Event::Keyboard(event) => {
if let Some(msg) = self.iced_state.program().keyboard_input(event, target) {
self.iced_state.queue_message(msg);
}
}
_ => {}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion neothesia/src/scene/playing_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Scene for PlayingScene {

fn window_event(&mut self, target: &mut Target, event: &WindowEvent) {
use winit::{
event::ElementState,
event::{ElementState, MouseButton},
keyboard::{Key, NamedKey},
};

Expand Down Expand Up @@ -154,6 +154,10 @@ impl Scene for PlayingScene {
}
}
WindowEvent::MouseInput { state, button, .. } => {
if let (MouseButton::Back, ElementState::Pressed) = (button, state) {
target.proxy.send_event(NeothesiaEvent::MainMenu).ok();
}

self.rewind_controler.handle_mouse_input(
&mut self.player,
&target.window_state,
Expand Down

0 comments on commit c116a8f

Please sign in to comment.