Skip to content

Commit 5128ab0

Browse files
committed
Splitting up more
1 parent 8245d1e commit 5128ab0

File tree

6 files changed

+116
-33
lines changed

6 files changed

+116
-33
lines changed

src/app.rs

+10-32
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
11
use projectm_rs::core::{projectm, projectm_handle};
2-
use projectm_rs::playlist;
32
use sdl2::video::GLProfile;
43

4+
pub mod config;
55
pub mod main_loop;
6-
7-
pub struct Config {
8-
pub preset_path: Option<String>,
9-
}
10-
11-
impl Default for Config {
12-
fn default() -> Self {
13-
// default preset path
14-
Self {
15-
// load from home dir or w/e
16-
preset_path: Some(String::from("/usr/local/share/projectM/presets")),
17-
}
18-
}
19-
}
6+
pub mod playlist;
7+
pub mod video;
208

219
pub struct App {
2210
pm: projectm_handle,
23-
playlist: playlist::Playlist,
11+
playlist: projectm_rs::playlist::Playlist,
2412
sdl_context: sdl2::Sdl,
2513
gl_context: sdl2::video::GLContext,
2614
window: sdl2::video::Window,
2715
}
2816

17+
pub fn default_config() -> config::Config {
18+
config::Config::default()
19+
}
20+
2921
impl App {
30-
pub fn new(config: Option<Config>) -> Self {
22+
pub fn new(config: Option<crate::app::config::Config>) -> Self {
3123
// setup sdl
3224
let sdl_context = sdl2::init().unwrap();
3325
let video_subsystem = sdl_context.video().unwrap();
@@ -62,7 +54,7 @@ impl App {
6254
// initialize projectM
6355
let pm = projectm::create();
6456
// and a preset playlist
65-
let mut playlist = projectm_rs::playlist::Playlist::create(pm);
57+
let playlist = projectm_rs::playlist::Playlist::create(pm);
6658

6759
// get/set window size
6860
let (width, height) = window.drawable_size(); // highDPI aware
@@ -83,18 +75,4 @@ impl App {
8375

8476
this
8577
}
86-
87-
pub fn load_config(&mut self, config: Config) {
88-
// load presets if provided
89-
if let Some(preset_path) = config.preset_path {
90-
self.add_preset_path(&preset_path);
91-
}
92-
}
93-
94-
/// Add presets to the playlist recursively skipping duplicates.
95-
pub fn add_preset_path(&mut self, preset_path: &str) {
96-
self.playlist.add_path(preset_path, true);
97-
println!("added preset path: {}", preset_path);
98-
println!("playlist size: {}", self.playlist.len());
99-
}
10078
}

src/app/config.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::app::App;
2+
3+
pub struct Config {
4+
pub preset_path: Option<String>,
5+
}
6+
7+
impl Default for Config {
8+
fn default() -> Self {
9+
// default preset path
10+
Self {
11+
// load from home dir or w/e
12+
preset_path: Some(String::from("/usr/local/share/projectM/presets")),
13+
}
14+
}
15+
}
16+
17+
impl App {
18+
pub fn load_config(&mut self, config: Config) {
19+
// load presets if provided
20+
if let Some(preset_path) = config.preset_path {
21+
self.add_preset_path(&preset_path);
22+
}
23+
}
24+
}

src/app/main_loop.rs

+48
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,61 @@ impl App {
1515
// check for event
1616
for event in event_pump.poll_iter() {
1717
match event {
18+
// quit (Esc)
1819
Event::Quit { .. }
1920
| Event::KeyDown {
2021
keycode: Some(Keycode::Escape),
2122
..
2223
} => {
2324
break 'running;
2425
}
26+
27+
// Next preset (N, right-arrow)
28+
Event::KeyUp {
29+
keycode: Some(Keycode::N),
30+
..
31+
} => {
32+
self.playlist_play_next();
33+
}
34+
// XXX: how to collapse these into one case?
35+
Event::KeyUp {
36+
keycode: Some(Keycode::Right),
37+
..
38+
} => {
39+
self.playlist_play_next();
40+
}
41+
42+
// Previous preset (P, left-arrow)
43+
Event::KeyUp {
44+
keycode: Some(Keycode::P),
45+
..
46+
} => {
47+
self.playlist_play_prev();
48+
}
49+
Event::KeyUp {
50+
keycode: Some(Keycode::Left),
51+
..
52+
} => {
53+
self.playlist_play_prev();
54+
}
55+
56+
// Random preset (R)
57+
Event::KeyUp {
58+
keycode: Some(Keycode::R),
59+
..
60+
} => {
61+
self.playlist.play_random();
62+
}
63+
64+
// Toggle fullscreen (F)
65+
Event::KeyUp {
66+
keycode: Some(Keycode::F),
67+
..
68+
} => {
69+
self.toggle_fullscreen();
70+
}
71+
72+
// default
2573
_ => {}
2674
}
2775
}

src/app/playlist.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::app::App;
2+
3+
impl App {
4+
/// Add presets to the playlist recursively skipping duplicates.
5+
pub fn add_preset_path(&mut self, preset_path: &str) {
6+
self.playlist.add_path(preset_path, true);
7+
println!("added preset path: {}", preset_path);
8+
println!("playlist size: {}", self.playlist.len());
9+
}
10+
11+
pub fn playlist_play_next(&mut self) {
12+
self.playlist.play_next();
13+
}
14+
pub fn playlist_play_prev(&mut self) {
15+
self.playlist.play_prev();
16+
}
17+
pub fn playlist_play_random(&mut self) {
18+
self.playlist.play_random();
19+
}
20+
}

src/app/video.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::app::App;
2+
3+
impl App {
4+
pub fn toggle_fullscreen(&mut self) {
5+
let is_fullscreen = self.window.fullscreen_state();
6+
self.window
7+
.set_fullscreen(match is_fullscreen {
8+
sdl2::video::FullscreenType::True => sdl2::video::FullscreenType::Off,
9+
_ => sdl2::video::FullscreenType::True,
10+
})
11+
.unwrap();
12+
}
13+
}

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod app;
22
mod dummy_audio;
33

44
fn main() -> Result<(), String> {
5-
let config = app::Config::default();
5+
let config = app::default_config();
66
// TODO: parse args here for config
77
// config.preset_path = Some("/usr/local/share/projectM/presets".to_string());
88

0 commit comments

Comments
 (0)