Skip to content

Commit

Permalink
Cleanup window event handling (#130)
Browse files Browse the repository at this point in the history
* Cleanup RewindControler window event handling

* Cleanup back, pause & settings event handling
  • Loading branch information
PolyMeilex authored Jan 14, 2024
1 parent e7f5ed8 commit 531dc61
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 93 deletions.
123 changes: 63 additions & 60 deletions neothesia/src/scene/playing_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use midi_file::midly::MidiMessage;
use neothesia_core::render::{QuadInstance, QuadPipeline};
use std::time::Duration;
use wgpu_jumpstart::{Color, TransformUniform, Uniform};
use winit::event::{KeyEvent, WindowEvent};
use winit::{
event::{ElementState, KeyEvent, MouseButton, WindowEvent},
keyboard::{Key, NamedKey},
};

use super::Scene;
use crate::{
Expand Down Expand Up @@ -130,59 +133,16 @@ impl Scene for PlayingScene {
}

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

match &event {
WindowEvent::KeyboardInput { event, .. } => {
self.rewind_controler
.handle_keyboard_input(&mut self.player, event);

if self.rewind_controler.is_rewinding() {
self.keyboard.reset_notes();
}

settings_keyboard_input(target, &mut self.toast_manager, event, &mut self.notes);

if event.state == ElementState::Released {
match event.logical_key {
Key::Named(NamedKey::Escape) => {
target.proxy.send_event(NeothesiaEvent::MainMenu).ok();
}
Key::Named(NamedKey::Space) => {
self.player.pause_resume();
}
_ => {}
}
}
}
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,
state,
button,
);

if self.rewind_controler.is_rewinding() {
self.keyboard.reset_notes();
}
}
WindowEvent::CursorMoved { position, .. } => {
self.rewind_controler.handle_cursor_moved(
&mut self.player,
&target.window_state,
position,
);
}
_ => {}
self.rewind_controler
.handle_window_event(target, event, &mut self.player);

if self.rewind_controler.is_rewinding() {
self.keyboard.reset_notes();
}

handle_back_button(target, event);
handle_pause_button(&mut self.player, event);
handle_settings_input(target, &mut self.toast_manager, &mut self.notes, event);
}

fn midi_event(&mut self, _target: &mut Target, _channel: u8, message: &MidiMessage) {
Expand All @@ -193,22 +153,65 @@ impl Scene for PlayingScene {
}
}

fn settings_keyboard_input(
fn handle_pause_button(player: &mut MidiPlayer, event: &WindowEvent) {
match event {
WindowEvent::KeyboardInput {
event:
KeyEvent {
state: ElementState::Released,
logical_key: Key::Named(NamedKey::Space),
..
},
..
} => {
player.pause_resume();
}
_ => {}
}
}

fn handle_back_button(target: &Target, event: &WindowEvent) {
let mut is_back_event = matches!(
event,
WindowEvent::KeyboardInput {
event: KeyEvent {
state: ElementState::Released,
logical_key: Key::Named(NamedKey::Escape),
..
},
..
}
);

is_back_event |= matches!(
event,
WindowEvent::MouseInput {
state: ElementState::Pressed,
button: MouseButton::Back,
..
}
);

if is_back_event {
target.proxy.send_event(NeothesiaEvent::MainMenu).ok();
}
}

fn handle_settings_input(
target: &mut Target,
toast_manager: &mut ToastManager,
input: &KeyEvent,
waterfall: &mut WaterfallRenderer,
event: &WindowEvent,
) {
use winit::{
event::ElementState,
keyboard::{Key, NamedKey},
let WindowEvent::KeyboardInput { event, .. } = event else {
return;
};

if input.state != ElementState::Released {
if event.state != ElementState::Released {
return;
}

match input.logical_key {
match event.logical_key {
Key::Named(key @ (NamedKey::ArrowUp | NamedKey::ArrowDown)) => {
let amount = if target.window_state.modifers_state.shift_key() {
0.5
Expand Down
94 changes: 61 additions & 33 deletions neothesia/src/scene/playing_scene/rewind_controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use winit::{
dpi::PhysicalPosition,
event::{ElementState, MouseButton},
event::{ElementState, MouseButton, WindowEvent},
};

use super::MidiPlayer;
Expand Down Expand Up @@ -62,66 +62,94 @@ impl RewindController {
}
}

pub fn handle_keyboard_input(
pub fn handle_window_event(
&mut self,
target: &mut Target,
event: &WindowEvent,
player: &mut MidiPlayer,
input: &winit::event::KeyEvent,
) {
match &event {
WindowEvent::KeyboardInput { event, .. } => {
self.handle_keyboard_input(player, event);
}
WindowEvent::MouseInput { state, button, .. } => {
self.handle_mouse_input(player, &target.window_state, state, button);
}
WindowEvent::CursorMoved { position, .. } => {
self.handle_cursor_moved(player, &target.window_state, position);
}
_ => {}
}
}

fn handle_keyboard_input(&mut self, player: &mut MidiPlayer, input: &winit::event::KeyEvent) {
use winit::keyboard::{Key, NamedKey};

if let Key::Named(name) = input.logical_key {
match name {
NamedKey::ArrowLeft => {
if let ElementState::Pressed = input.state {
if !self.is_rewinding() {
self.start_keyboard_rewind(player, -100);
}
} else if let RewindController::Keyboard { .. } = self {
let Key::Named(name) = input.logical_key else {
return;
};

match name {
NamedKey::ArrowLeft => match input.state {
ElementState::Pressed => {
if !self.is_rewinding() {
self.start_keyboard_rewind(player, -100);
}
}
ElementState::Released => {
if let RewindController::Keyboard { .. } = self {
self.stop_rewind(player);
}
}
NamedKey::ArrowRight => {
if let ElementState::Pressed = input.state {
if !self.is_rewinding() {
self.start_keyboard_rewind(player, 100);
}
} else if let RewindController::Keyboard { .. } = self {
},
NamedKey::ArrowRight => match input.state {
ElementState::Pressed => {
if !self.is_rewinding() {
self.start_keyboard_rewind(player, 100);
}
}
ElementState::Released => {
if let RewindController::Keyboard { .. } = self {
self.stop_rewind(player);
}
}
_ => {}
}
},
_ => {}
}
}

pub fn handle_mouse_input(
fn handle_mouse_input(
&mut self,
player: &mut MidiPlayer,
window_state: &WindowState,
state: &ElementState,
button: &MouseButton,
) {
if let (ElementState::Pressed, MouseButton::Left) = (state, button) {
let pos = &window_state.cursor_logical_position;
match (state, button) {
(ElementState::Pressed, MouseButton::Left) => {
let pos = &window_state.cursor_logical_position;

if pos.y < 20.0 && !self.is_rewinding() {
self.start_mouse_rewind(player);
if pos.y < 20.0 && !self.is_rewinding() {
self.start_mouse_rewind(player);

let x = window_state.cursor_logical_position.x;
let w = window_state.logical_size.width;
let x = window_state.cursor_logical_position.x;
let w = window_state.logical_size.width;

let p = x / w;
log::debug!("Progressbar: x:{},p:{}", x, p);
player.set_percentage_time(p);
let p = x / w;
log::debug!("Progressbar: x:{},p:{}", x, p);
player.set_percentage_time(p);
}
}
} else if let (ElementState::Released, MouseButton::Left) = (state, button) {
if let RewindController::Mouse { .. } = self {
self.stop_rewind(player);
(ElementState::Released, MouseButton::Left) => {
if let RewindController::Mouse { .. } = self {
self.stop_rewind(player);
}
}
_ => {}
}
}

pub fn handle_cursor_moved(
fn handle_cursor_moved(
&mut self,
player: &mut MidiPlayer,
window_state: &WindowState,
Expand Down

0 comments on commit 531dc61

Please sign in to comment.