Skip to content

Commit 635ee89

Browse files
committed
update main screen scaling and hide quit button from wasm
closes #56 closes #54
1 parent 8e8480b commit 635ee89

File tree

4 files changed

+86
-94
lines changed

4 files changed

+86
-94
lines changed

art/fonts.aseprite

-185 Bytes
Binary file not shown.

assets/font.ttf

1.29 KB
Binary file not shown.

src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl Plugin for AppPlugin {
5959
mode: WindowMode::BorderlessFullscreen(MonitorSelection::Current),
6060
#[cfg(debug_assertions)]
6161
mode: WindowMode::Windowed,
62+
// resolution: WindowResolution::new(853, 480),
6263
resolution: WindowResolution::new(1920, 1080),
6364
resizable: false,
6465
position: WindowPosition::Centered(MonitorSelection::Current),
@@ -69,6 +70,8 @@ impl Plugin for AppPlugin {
6970
.set(ImagePlugin::default_nearest()),
7071
);
7172

73+
app.insert_resource(ScaleFactor(1.0));
74+
7275
app.add_plugins(InputPlugin);
7376

7477
app.add_plugins(audio::plugin);
@@ -132,9 +135,13 @@ fn spawn_camera(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
132135
commands.spawn((Camera2d, Msaa::Off, OuterCamera, HIGH_RES_LAYERS));
133136
}
134137

138+
#[derive(Resource)]
139+
pub struct ScaleFactor(f32);
140+
135141
fn fit_canvas(
136142
mut resize_messages: MessageReader<WindowResized>,
137143
mut projection: Single<&mut Projection, With<OuterCamera>>,
144+
mut scale_factor: ResMut<ScaleFactor>,
138145
) {
139146
let Projection::Orthographic(proj) = &mut **projection else {
140147
return;
@@ -143,6 +150,8 @@ fn fit_canvas(
143150
for msg in resize_messages.read() {
144151
let h_scale = msg.width / RES_WIDTH as f32;
145152
let v_scale = msg.height / RES_HEIGHT as f32;
146-
proj.scale = 1. / h_scale.min(v_scale);
153+
let scale = h_scale.min(v_scale);
154+
proj.scale = 1. / scale;
155+
scale_factor.0 = scale;
147156
}
148157
}

src/screens/title.rs

Lines changed: 76 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy::prelude::*;
22
use leafwing_input_manager::prelude::ActionState;
33

44
use crate::{
5-
HIGH_RES_LAYERS, PIXEL_PERFECT_LAYERS,
5+
PIXEL_PERFECT_LAYERS, ScaleFactor,
66
input::Action,
77
screens::{Screen, splash::Title},
88
sundry::{MEDIUM_GRAY, TRANSPARENT_MEDIUM_GRAY, WHITE},
@@ -37,112 +37,56 @@ fn spawn_title_screen(
3737
mut commands: Commands,
3838
asset_server: Res<AssetServer>,
3939
mut title_transform: Query<&mut Transform, With<Title>>,
40+
scale_factor: Res<ScaleFactor>,
4041
) {
42+
let scale = scale_factor.0;
4143
let mut transform = title_transform.single_mut().unwrap();
4244
transform.translation = Vec3::new(0.0, 32.0, 0.0);
43-
let font_handle = asset_server.load("font.ttf");
45+
let font_handle: Handle<Font> = asset_server.load("font.ttf");
46+
47+
let new_game = title_menu_button(
48+
"NEW GAME",
49+
0,
50+
MenuButton::NewGame,
51+
font_handle.clone(),
52+
scale,
53+
);
54+
let settings = title_menu_button(
55+
"SETTINGS",
56+
1,
57+
MenuButton::Settings,
58+
font_handle.clone(),
59+
scale,
60+
);
61+
let quit = title_menu_button("QUIT", 2, MenuButton::Quit, font_handle.clone(), scale);
62+
63+
#[cfg(target_arch = "wasm32")]
64+
let menu_buttons = children![new_game, settings];
65+
#[cfg(not(target_arch = "wasm32"))]
66+
let menu_buttons = children![new_game, settings, quit];
67+
4468
commands.spawn((
4569
Node {
46-
width: Val::Px(512.0),
47-
height: Val::Px(400.0),
48-
bottom: Val::Px(0.0),
49-
position_type: PositionType::Absolute,
50-
flex_direction: FlexDirection::Column,
51-
align_items: AlignItems::Center,
52-
justify_content: JustifyContent::Center,
53-
padding: UiRect::all(Val::Px(32.0)),
54-
margin: UiRect::all(Val::Auto),
70+
width: Val::Percent(100.0),
71+
height: Val::Percent(100.0),
72+
padding: UiRect::all(Val::Px(8.0 * scale)),
5573
..default()
5674
},
5775
DespawnOnExit(Screen::Title),
76+
PIXEL_PERFECT_LAYERS,
5877
children![(
5978
Name::new("Title Screen Menu"),
6079
Node {
61-
width: Val::Percent(70.0),
62-
max_width: Val::Px(900.0),
63-
margin: UiRect::all(Val::Auto),
80+
width: Val::Percent(100.0),
6481
flex_direction: FlexDirection::Column,
6582
align_items: AlignItems::Center,
6683
justify_content: JustifyContent::Center,
84+
position_type: PositionType::Absolute,
85+
bottom: Val::Px(8.0 * scale),
6786
..default()
6887
},
69-
children![
70-
(
71-
Name::new("New Game Button"),
72-
Button,
73-
Node {
74-
width: Val::Percent(100.0),
75-
height: Val::Px(100.0),
76-
align_items: AlignItems::Center,
77-
justify_content: JustifyContent::Center,
78-
..default()
79-
},
80-
MenuButton::NewGame,
81-
PIXEL_PERFECT_LAYERS,
82-
children![(
83-
Name::new("New Game Text"),
84-
Text::new("NEW GAME"),
85-
TextColor(TRANSPARENT_MEDIUM_GRAY),
86-
ButtonText(0),
87-
TextFont {
88-
font_size: 32.0,
89-
font: font_handle.clone(),
90-
..default()
91-
},
92-
FadeIn,
93-
)]
94-
),
95-
(
96-
Name::new("Settings Button"),
97-
Button,
98-
MenuButton::Settings,
99-
Node {
100-
width: Val::Percent(100.0),
101-
height: Val::Px(100.0),
102-
align_items: AlignItems::Center,
103-
justify_content: JustifyContent::Center,
104-
..default()
105-
},
106-
HIGH_RES_LAYERS,
107-
children![(
108-
Name::new("Settings"),
109-
Text::new("SETTINGS"),
110-
TextColor(TRANSPARENT_MEDIUM_GRAY),
111-
ButtonText(1),
112-
TextFont {
113-
font_size: 32.0,
114-
font: font_handle.clone(),
115-
..default()
116-
},
117-
FadeIn,
118-
)]
119-
),
120-
(
121-
Name::new("Quit Button"),
122-
Button,
123-
MenuButton::Quit,
124-
Node {
125-
width: Val::Percent(100.0),
126-
height: Val::Px(100.0),
127-
align_items: AlignItems::Center,
128-
justify_content: JustifyContent::Center,
129-
..default()
130-
},
131-
HIGH_RES_LAYERS,
132-
children![(
133-
Name::new("Quit Text"),
134-
Text::new("QUIT"),
135-
TextColor(TRANSPARENT_MEDIUM_GRAY),
136-
ButtonText(2),
137-
TextFont {
138-
font_size: 32.0,
139-
font: font_handle.clone(),
140-
..default()
141-
},
142-
FadeIn,
143-
)]
144-
)
145-
]
88+
PIXEL_PERFECT_LAYERS,
89+
menu_buttons,
14690
)],
14791
));
14892
}
@@ -156,16 +100,21 @@ fn input_system(
156100
) {
157101
let action_state = input_query.single().unwrap();
158102

103+
#[cfg(not(target_arch = "wasm32"))]
104+
let max_index = 2;
105+
#[cfg(target_arch = "wasm32")]
106+
let max_index = 1;
107+
159108
if action_state.just_pressed(&Action::Up) {
160109
if active_index.0 == 0 {
161-
active_index.0 = 2;
110+
active_index.0 = max_index;
162111
} else {
163112
active_index.0 -= 1;
164113
}
165114
}
166115

167116
if action_state.just_pressed(&Action::Down) {
168-
if active_index.0 == 2 {
117+
if active_index.0 == max_index {
169118
active_index.0 = 0
170119
} else {
171120
active_index.0 += 1
@@ -255,3 +204,37 @@ fn fade_in(
255204
}
256205
}
257206
}
207+
208+
fn title_menu_button(
209+
button: &str,
210+
button_index: usize,
211+
menu_button: MenuButton,
212+
font_handle: Handle<Font>,
213+
scale: f32,
214+
) -> impl Bundle {
215+
(
216+
Name::new(format!("{button} Button")),
217+
Button,
218+
Node {
219+
width: Val::Percent(100.0),
220+
height: Val::Px(32.0 * scale),
221+
align_items: AlignItems::Center,
222+
justify_content: JustifyContent::Center,
223+
..default()
224+
},
225+
menu_button,
226+
PIXEL_PERFECT_LAYERS,
227+
children![(
228+
Name::new(format!("{button}Text")),
229+
Text::new(button.to_string()),
230+
TextColor(TRANSPARENT_MEDIUM_GRAY),
231+
ButtonText(button_index),
232+
TextFont {
233+
font_size: 16.0 * scale,
234+
font: font_handle,
235+
..default()
236+
},
237+
FadeIn,
238+
)],
239+
)
240+
}

0 commit comments

Comments
 (0)