1mod components;
2mod consts;
3mod game;
4mod gameover;
5mod gameplay;
6mod layers;
7mod load;
8mod pause;
9mod resources;
10mod title;
11
12use bevy::prelude::*;
13use bevy::window::WindowMode;
14use consts::RESOLUTION;
15use resources::Modifiers;
16use crate::gameover::GameoverPlugin;
17use crate::gameplay::{BoardUpdate, GameplayPlugin};
18use crate::load::LoadPlugin;
19use crate::pause::PausePlugin;
20use crate::title::TitlePlugin;
21
22use crate::resources::AppState;
23
24pub mod prelude {
25 pub mod consts {
26 pub use crate::consts::*;
27 }
28 pub use super::NonogramApp;
29}
30
31#[derive(Clone, Copy, Debug)]
32pub struct NonogramApp;
33
34impl Plugin for NonogramApp {
35
36 fn build(&self, app: &mut App) {
37 app
38 .add_event::<BoardUpdate>()
39 .init_state::<AppState>()
40 .add_plugins(LoadPlugin)
41 .add_plugins(TitlePlugin)
42 .add_plugins(GameplayPlugin)
43 .add_plugins(PausePlugin)
44 .add_plugins(GameoverPlugin)
45 .add_systems(PreUpdate, resize_system)
46 .add_systems(Update, keyboard_system)
47 ;
48 }
49}
50
51fn keyboard_system(
52 input: Res<ButtonInput<KeyCode>>,
53 mut window: Query<&mut Window>,
54 mut modifiers: ResMut<Modifiers>,
55 mut exit: EventWriter<AppExit>,
56) {
57 #[cfg(target_os="macos")]
58 let ctrl = input.pressed(KeyCode::SuperLeft);
59 #[cfg(not(target_os="macos"))]
60 let ctrl = input.pressed(KeyCode::ControlLeft) || input.pressed(KeyCode::ControlRight);
61 if ctrl && input.just_pressed(KeyCode::KeyQ) {
62 exit.write(AppExit::Success);
63 }
64 if input.just_pressed(KeyCode::F11) {
65 if let Ok(mut window) = window.single_mut() {
66 window.mode = match window.mode {
67 WindowMode::Windowed => WindowMode::BorderlessFullscreen(MonitorSelection::Primary),
68 _ => WindowMode::Windowed,
69 };
70 };
71 }
72 modifiers.ctrl = ctrl;
73 modifiers.shift = input.pressed(KeyCode::ShiftLeft) || input.pressed(KeyCode::ShiftRight);
74}
75
76fn resize_system(mut window_query: Query<&mut Window, Changed<Window>>) {
77 let Ok(mut window) = window_query.single_mut() else { return };
78
79 let resolution = &mut window.resolution;
80 let xf = resolution.physical_width() as f32 / RESOLUTION.x;
81 let yf = resolution.physical_height() as f32 / RESOLUTION.y;
82 let factor = xf.min(yf);
83 resolution.set_scale_factor_override(Some(factor));
84}