Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Song out of Context struct #201

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions neothesia/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;
use crate::config::Config;
use crate::input_manager::InputManager;
use crate::render::TextRenderer;
use crate::song::Song;
use crate::utils::window::WindowState;
use crate::{output_manager::OutputManager, NeothesiaEvent, TransformUniform};
use wgpu_jumpstart::{Gpu, Uniform};
Expand All @@ -25,7 +24,6 @@ pub struct Context {

pub output_manager: OutputManager,
pub input_manager: InputManager,
pub song: Option<Song>,
pub config: Config,

pub proxy: EventLoopProxy<NeothesiaEvent>,
Expand Down Expand Up @@ -59,23 +57,6 @@ impl Context {
);

let config = Config::new();
let args: Vec<String> = std::env::args().collect();

let midi_file = if args.len() > 1 {
if let Ok(midi) = midi_file::MidiFile::new(&args[1]) {
Some(midi)
} else {
None
}
} else if let Some(last) = config.last_opened_song.as_ref() {
if let Ok(midi) = midi_file::MidiFile::new(last) {
Some(midi)
} else {
None
}
} else {
None
};

Self {
window,
Expand All @@ -89,7 +70,6 @@ impl Context {

output_manager: Default::default(),
input_manager: InputManager::new(proxy.clone()),
song: midi_file.map(Song::new),
config,
proxy,
}
Expand Down
10 changes: 6 additions & 4 deletions neothesia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::time::Duration;
use context::Context;
use iced_core::Renderer;
use scene::{menu_scene, playing_scene, Scene};
use song::Song;
use utils::window::WindowState;

use midi_file::midly::MidiMessage;
Expand All @@ -29,7 +30,7 @@ pub enum NeothesiaEvent {
/// Go to playing scene
Play(song::Song),
/// Go to main menu scene
MainMenu,
MainMenu(Option<song::Song>),
MidiInput {
/// The MIDI channel that this message is associated with.
channel: u8,
Expand All @@ -51,7 +52,8 @@ struct Neothesia {

impl Neothesia {
fn new(mut context: Context, surface: Surface) -> Self {
let game_scene = menu_scene::MenuScene::new(&mut context);
let song = Song::from_env(&context);
let game_scene = menu_scene::MenuScene::new(&mut context, song);

context.resize();
context.gpu.submit();
Expand Down Expand Up @@ -144,8 +146,8 @@ impl Neothesia {
let to = playing_scene::PlayingScene::new(&self.context, song);
self.game_scene = Box::new(to);
}
NeothesiaEvent::MainMenu => {
let to = menu_scene::MenuScene::new(&mut self.context);
NeothesiaEvent::MainMenu(song) => {
let to = menu_scene::MenuScene::new(&mut self.context, song);
self.game_scene = Box::new(to);
}
NeothesiaEvent::MidiInput { channel, message } => {
Expand Down
9 changes: 6 additions & 3 deletions neothesia/src/scene/menu_scene/iced_menu/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ impl Page for MainPage {
PageMessage::None
}

fn view<'a>(data: &'a Data, ctx: &Context) -> neothesia_iced_widgets::Element<'a, Self::Event> {
fn view<'a>(
data: &'a Data,
_ctx: &Context,
) -> neothesia_iced_widgets::Element<'a, Self::Event> {
let buttons = column![
NeoBtn::new_with_label("Select File")
.on_press(Event::MidiFilePicker(MidiFilePickerMessage::open()))
Expand All @@ -68,7 +71,7 @@ impl Page for MainPage {

let mut layout = Layout::new().body(top_padded(column));

if let Some(song) = ctx.song.as_ref() {
if let Some(song) = data.song.as_ref() {
let tracks = NeoBtn::new(icons::note_list_icon().size(30.0).center())
.height(Length::Fixed(60.0))
.min_width(80.0)
Expand Down Expand Up @@ -166,7 +169,7 @@ fn midi_file_picker_update(
MidiFilePickerMessage::MidiFileLoaded(midi) => {
if let Some((midi, path)) = midi {
ctx.config.last_opened_song = Some(path);
ctx.song = Some(Song::new(midi));
data.song = Some(Song::new(midi));
}
data.is_loading = false;
}
Expand Down
8 changes: 6 additions & 2 deletions neothesia/src/scene/menu_scene/iced_menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
iced_utils::iced_state::{Element, Program},
output_manager::OutputDescriptor,
scene::menu_scene::iced_menu::main::MainPage,
song::Song,
NeothesiaEvent,
};

Expand Down Expand Up @@ -50,6 +51,8 @@ pub struct Data {
is_loading: bool,

logo_handle: ImageHandle,

song: Option<Song>,
}

pub struct AppUi {
Expand All @@ -58,7 +61,7 @@ pub struct AppUi {
}

impl AppUi {
pub fn new(_ctx: &Context) -> Self {
pub fn new(_ctx: &Context, song: Option<Song>) -> Self {
let mut page_stack = VecDeque::new();
page_stack.push_front(Step::Main);

Expand All @@ -74,6 +77,7 @@ impl AppUi {
is_loading: false,

logo_handle: ImageHandle::from_bytes(include_bytes!("../img/banner.png").to_vec()),
song,
},
}
}
Expand Down Expand Up @@ -214,7 +218,7 @@ pub enum Step {
}

fn play(data: &Data, ctx: &mut Context) {
let Some(song) = ctx.song.as_ref() else {
let Some(song) = data.song.as_ref() else {
return;
};

Expand Down
12 changes: 6 additions & 6 deletions neothesia/src/scene/menu_scene/iced_menu/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ impl Page for TracksPage {
fn update(data: &mut Data, event: Event, ctx: &mut Context) -> PageMessage {
match event {
Event::AllTracksPlayer(config) => {
if let Some(song) = ctx.song.as_mut() {
if let Some(song) = data.song.as_mut() {
for track in song.config.tracks.iter_mut() {
track.player = config.clone();
}
}
}
Event::TrackPlayer(track, config) => {
if let Some(song) = ctx.song.as_mut() {
if let Some(song) = data.song.as_mut() {
song.config.tracks[track].player = config;
}
}
Event::TrackVisibility(track, visible) => {
if let Some(song) = ctx.song.as_mut() {
if let Some(song) = data.song.as_mut() {
song.config.tracks[track].visible = visible;
}
}
Expand All @@ -57,9 +57,9 @@ impl Page for TracksPage {
PageMessage::none()
}

fn view<'a>(_data: &'a Data, ctx: &Context) -> Element<'a, Event> {
fn view<'a>(data: &'a Data, ctx: &Context) -> Element<'a, Event> {
let mut tracks = Vec::new();
if let Some(song) = ctx.song.as_ref() {
if let Some(song) = data.song.as_ref() {
for track in song.file.tracks.iter().filter(|t| !t.notes.is_empty()) {
let config = &song.config.tracks[track.track_id];

Expand Down Expand Up @@ -140,7 +140,7 @@ impl Page for TracksPage {
.min_width(80.0)
.on_press(Event::Play);

if ctx.song.is_some() {
if data.song.is_some() {
row![play]
} else {
row![]
Expand Down
5 changes: 3 additions & 2 deletions neothesia/src/scene/menu_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
iced_state::{self, Program},
},
scene::Scene,
song::Song,
};

type Renderer = iced_wgpu::Renderer;
Expand All @@ -31,8 +32,8 @@ pub struct MenuScene {
}

impl MenuScene {
pub fn new(ctx: &mut Context) -> Self {
let menu = AppUi::new(ctx);
pub fn new(ctx: &mut Context, song: Option<Song>) -> Self {
let menu = AppUi::new(ctx, song);
let iced_state =
iced_state::State::new(menu, ctx.iced_manager.viewport.logical_size(), ctx);

Expand Down
12 changes: 8 additions & 4 deletions neothesia/src/scene/playing_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ impl Scene for PlayingScene {
self.quad_pipeline.prepare(&ctx.gpu.device, &ctx.gpu.queue);

if self.player.is_finished() && !self.player.is_paused() {
ctx.proxy.send_event(NeothesiaEvent::MainMenu).ok();
ctx.proxy
.send_event(NeothesiaEvent::MainMenu(Some(self.player.song().clone())))
.ok();
}
}

Expand All @@ -186,7 +188,7 @@ impl Scene for PlayingScene {
self.keyboard.reset_notes();
}

handle_back_button(ctx, event);
handle_back_button(ctx, self.player.song(), event);
handle_pause_button(&mut self.player, event);
handle_settings_input(ctx, &mut self.toast_manager, &mut self.waterfall, event);

Expand Down Expand Up @@ -220,7 +222,7 @@ fn handle_pause_button(player: &mut MidiPlayer, event: &WindowEvent) {
}
}

fn handle_back_button(ctx: &Context, event: &WindowEvent) {
fn handle_back_button(ctx: &Context, song: &Song, event: &WindowEvent) {
let mut is_back_event = matches!(
event,
WindowEvent::KeyboardInput {
Expand All @@ -243,7 +245,9 @@ fn handle_back_button(ctx: &Context, event: &WindowEvent) {
);

if is_back_event {
ctx.proxy.send_event(NeothesiaEvent::MainMenu).ok();
ctx.proxy
.send_event(NeothesiaEvent::MainMenu(Some(song.clone())))
.ok();
}
}

Expand Down
4 changes: 3 additions & 1 deletion neothesia/src/scene/playing_scene/top_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ impl TopBar {
fn on_msg(scene: &mut PlayingScene, ctx: &mut Context, msg: Msg) {
match msg {
Msg::GoBack => {
ctx.proxy.send_event(NeothesiaEvent::MainMenu).ok();
ctx.proxy
.send_event(NeothesiaEvent::MainMenu(Some(scene.player.song().clone())))
.ok();
}
Msg::PlayResume => {
scene.player.pause_resume();
Expand Down
23 changes: 23 additions & 0 deletions neothesia/src/song.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use midi_file::MidiTrack;

use crate::context::Context;

#[derive(Debug, Clone)]
pub enum PlayerConfig {
Mute,
Expand Down Expand Up @@ -49,4 +51,25 @@ impl Song {
let config = SongConfig::new(&file.tracks);
Self { file, config }
}

pub fn from_env(ctx: &Context) -> Option<Self> {
let args: Vec<String> = std::env::args().collect();
let midi_file = if args.len() > 1 {
if let Ok(midi) = midi_file::MidiFile::new(&args[1]) {
Some(midi)
} else {
None
}
} else if let Some(last) = ctx.config.last_opened_song.as_ref() {
if let Ok(midi) = midi_file::MidiFile::new(last) {
Some(midi)
} else {
None
}
} else {
None
};

Some(Self::new(midi_file?))
}
}
Loading