Skip to content

Commit

Permalink
Update winit to 0.29
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 16, 2023
1 parent 94f2ec2 commit 20881d6
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 765 deletions.
700 changes: 402 additions & 298 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions neothesia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ wgpu-jumpstart = { workspace = true }
neothesia-core = { workspace = true }
piano-math = { workspace = true }

winit = "0.28.2"
rfd = "0.11.2"
winit = { version = "0.29.4", features = ["rwh_05"] }
rfd = "0.12.1"
async-thread = "0.1"

cpal = { version = "0.15.0", optional = true }
Expand Down
509 changes: 136 additions & 373 deletions neothesia/src/iced_utils/iced_conversion.rs

Large diffs are not rendered by default.

99 changes: 57 additions & 42 deletions neothesia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use wgpu_jumpstart::Surface;
use wgpu_jumpstart::{Gpu, TransformUniform};
use winit::{
event::WindowEvent,
event_loop::{ControlFlow, EventLoop, EventLoopBuilder},
event_loop::{EventLoop, EventLoopBuilder},
};

#[derive(Debug)]
Expand All @@ -46,6 +46,7 @@ struct Neothesia {

#[cfg(debug_assertions)]
fps_ticker: fps_ticker::Fps,
last_time: std::time::Instant,
}

impl Neothesia {
Expand All @@ -63,10 +64,15 @@ impl Neothesia {

#[cfg(debug_assertions)]
fps_ticker: fps_ticker::Fps::default(),
last_time: std::time::Instant::now(),
}
}

fn window_event(&mut self, event: &WindowEvent, control_flow: &mut ControlFlow) {
fn window_event(
&mut self,
event: &WindowEvent,
event_loop: &winit::event_loop::EventLoopWindowTarget<NeothesiaEvent>,
) {
self.target.window_state.window_event(event);

match &event {
Expand All @@ -88,35 +94,51 @@ impl Neothesia {
self.game_scene.resize(&mut self.target);
}
WindowEvent::KeyboardInput {
input:
winit::event::KeyboardInput {
event:
winit::event::KeyEvent {
state: winit::event::ElementState::Pressed,
virtual_keycode: Some(winit::event::VirtualKeyCode::F),
logical_key,
..
},
..
} => {
if self.target.window.fullscreen().is_some() {
self.target.window.set_fullscreen(None);
} else {
let monitor = self.target.window.current_monitor();
if let Some(monitor) = monitor {
let f = winit::window::Fullscreen::Borderless(Some(monitor));
self.target.window.set_fullscreen(Some(f));
} => match logical_key {
winit::keyboard::Key::Character(c) if c.as_str() == "f" => {
if self.target.window.fullscreen().is_some() {
self.target.window.set_fullscreen(None);
} else {
let f = winit::window::Fullscreen::Borderless(None);
self.target.window.set_fullscreen(Some(f));
let monitor = self.target.window.current_monitor();
if let Some(monitor) = monitor {
let f = winit::window::Fullscreen::Borderless(Some(monitor));
self.target.window.set_fullscreen(Some(f));
} else {
let f = winit::window::Fullscreen::Borderless(None);
self.target.window.set_fullscreen(Some(f));
}
}
}
_ => {}
},
WindowEvent::RedrawRequested => {
let delta = self.last_time.elapsed();
self.last_time = std::time::Instant::now();

self.update(delta);
self.render();
}
WindowEvent::CloseRequested => {
event_loop.exit();
}
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
_ => {}
}

self.game_scene.window_event(&mut self.target, event);
}

fn neothesia_event(&mut self, event: NeothesiaEvent, control_flow: &mut ControlFlow) {
fn neothesia_event(
&mut self,
event: NeothesiaEvent,
event_loop: &winit::event_loop::EventLoopWindowTarget<NeothesiaEvent>,
) {
match event {
NeothesiaEvent::Play(song) => {
self.target.iced_manager.renderer.clear();
Expand All @@ -133,7 +155,7 @@ impl Neothesia {
.midi_event(&mut self.target, channel, &message);
}
NeothesiaEvent::Exit => {
*control_flow = ControlFlow::Exit;
event_loop.exit();
}
}
}
Expand Down Expand Up @@ -228,40 +250,33 @@ fn main() {

let mut app = Neothesia::new(target, surface);

let mut last_time = std::time::Instant::now();

// Investigate:
// https://github.com/gfx-rs/wgpu-rs/pull/306

event_loop.run(move |event, _, control_flow| {
use winit::event::Event;
match event {
Event::UserEvent(event) => {
app.neothesia_event(event, control_flow);
}
Event::WindowEvent { event, .. } => {
app.window_event(&event, control_flow);
}
Event::RedrawRequested(_) => {
let delta = last_time.elapsed();
last_time = std::time::Instant::now();

app.update(delta);
app.render();
}
Event::RedrawEventsCleared => {
app.target.window.request_redraw();
event_loop
.run(move |event, event_loop| {
use winit::event::Event;
match event {
Event::UserEvent(event) => {
app.neothesia_event(event, event_loop);
}
Event::WindowEvent { event, .. } => {
app.window_event(&event, event_loop);
}
Event::AboutToWait => {
app.target.window.request_redraw();
}
_ => {}
}
_ => {}
}
});
})
.unwrap();
}

fn init(builder: winit::window::WindowBuilder) -> (EventLoop<NeothesiaEvent>, Target, Surface) {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("neothesia=info"))
.init();

let event_loop = EventLoopBuilder::with_user_event().build();
let event_loop = EventLoopBuilder::with_user_event().build().unwrap();
let proxy = event_loop.create_proxy();

let builder = builder
Expand Down
2 changes: 1 addition & 1 deletion neothesia/src/scene/menu_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Scene for MenuScene {
}

fn window_event(&mut self, target: &mut Target, event: &WindowEvent) {
use winit::event::ModifiersState;
use winit::keyboard::ModifiersState;

let modifiers = ModifiersState::default();

Expand Down
59 changes: 29 additions & 30 deletions neothesia/src/scene/playing_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use midi_file::midly::MidiMessage;
use neothesia_core::render::{QuadInstance, QuadPipeline};
use std::time::Duration;
use wgpu_jumpstart::{Color, TransformUniform, Uniform};
use winit::event::{KeyboardInput, WindowEvent};
use winit::event::{KeyEvent, WindowEvent};

use super::Scene;
use crate::{
Expand Down Expand Up @@ -125,33 +125,35 @@ impl Scene for PlayingScene {
}

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

match &event {
KeyboardInput { input, .. } => {
WindowEvent::KeyboardInput { event, .. } => {
self.rewind_controler
.handle_keyboard_input(&mut self.player, input);
.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, input, &mut self.notes);
settings_keyboard_input(target, &mut self.toast_manager, event, &mut self.notes);

if input.state == ElementState::Released {
match input.virtual_keycode {
Some(VirtualKeyCode::Escape) => {
if event.state == ElementState::Released {
match event.logical_key {
Key::Named(NamedKey::Escape) => {
target.proxy.send_event(NeothesiaEvent::MainMenu).ok();
}
Some(VirtualKeyCode::Space) => {
Key::Named(NamedKey::Space) => {
self.player.pause_resume();
}
_ => {}
}
}
}
MouseInput { state, button, .. } => {
WindowEvent::MouseInput { state, button, .. } => {
self.rewind_controler.handle_mouse_input(
&mut self.player,
&target.window_state,
Expand All @@ -163,7 +165,7 @@ impl Scene for PlayingScene {
self.keyboard.reset_notes();
}
}
CursorMoved { position, .. } => {
WindowEvent::CursorMoved { position, .. } => {
self.rewind_controler.handle_cursor_moved(
&mut self.player,
&target.window_state,
Expand All @@ -185,30 +187,27 @@ impl Scene for PlayingScene {
fn settings_keyboard_input(
target: &mut Target,
toast_manager: &mut ToastManager,
input: &KeyboardInput,
input: &KeyEvent,
waterfall: &mut WaterfallRenderer,
) {
use winit::event::{ElementState, VirtualKeyCode};
use winit::{
event::ElementState,
keyboard::{Key, NamedKey},
};

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

let virtual_keycode = if let Some(virtual_keycode) = input.virtual_keycode {
virtual_keycode
} else {
return;
};

match virtual_keycode {
VirtualKeyCode::Up | VirtualKeyCode::Down => {
let amount = if target.window_state.modifers_state.shift() {
match input.logical_key {
Key::Named(key @ (NamedKey::ArrowUp | NamedKey::ArrowDown)) => {
let amount = if target.window_state.modifers_state.shift_key() {
0.5
} else {
0.1
};

if virtual_keycode == VirtualKeyCode::Up {
if key == NamedKey::ArrowUp {
target.config.speed_multiplier += amount;
} else {
target.config.speed_multiplier -= amount;
Expand All @@ -218,14 +217,14 @@ fn settings_keyboard_input(
toast_manager.speed_toast(target.config.speed_multiplier);
}

VirtualKeyCode::PageUp | VirtualKeyCode::PageDown => {
let amount = if target.window_state.modifers_state.shift() {
Key::Named(key @ (NamedKey::PageUp | NamedKey::PageDown)) => {
let amount = if target.window_state.modifers_state.shift_key() {
500.0
} else {
100.0
};

if virtual_keycode == VirtualKeyCode::PageUp {
if key == NamedKey::PageUp {
target.config.animation_speed += amount;
} else {
target.config.animation_speed -= amount;
Expand All @@ -238,14 +237,14 @@ fn settings_keyboard_input(
toast_manager.animation_speed_toast(target.config.animation_speed);
}

VirtualKeyCode::Minus | VirtualKeyCode::Plus | VirtualKeyCode::Equals => {
let amount = if target.window_state.modifers_state.shift() {
Key::Character(ref ch) if matches!(ch.as_str(), "_" | "-" | "+" | "=") => {
let amount = if target.window_state.modifers_state.shift_key() {
0.1
} else {
0.01
};

if virtual_keycode == VirtualKeyCode::Minus {
if matches!(ch.as_str(), "-" | "_") {
target.config.playback_offset -= amount;
} else {
target.config.playback_offset += amount;
Expand Down
26 changes: 16 additions & 10 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, KeyboardInput, MouseButton, VirtualKeyCode},
event::{ElementState, MouseButton},
};

use super::MidiPlayer;
Expand Down Expand Up @@ -52,30 +52,36 @@ impl RewindController {

pub fn update(&self, player: &mut MidiPlayer, target: &Target) {
if let RewindController::Keyboard { speed, .. } = self {
if target.window_state.modifers_state.shift() {
if target.window_state.modifers_state.shift_key() {
player.rewind(*speed * 2);
} else if target.window_state.modifers_state.ctrl() {
} else if target.window_state.modifers_state.control_key() {
player.rewind(*speed / 2);
} else {
player.rewind(*speed);
}
}
}

pub fn handle_keyboard_input(&mut self, player: &mut MidiPlayer, input: &KeyboardInput) {
if let Some(virtual_keycode) = input.virtual_keycode {
match virtual_keycode {
VirtualKeyCode::Left => {
if let winit::event::ElementState::Pressed = input.state {
pub 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 {
self.stop_rewind(player);
}
}
VirtualKeyCode::Right => {
if let winit::event::ElementState::Pressed = input.state {
NamedKey::ArrowRight => {
if let ElementState::Pressed = input.state {
if !self.is_rewinding() {
self.start_keyboard_rewind(player, 100);
}
Expand Down
Loading

0 comments on commit 20881d6

Please sign in to comment.