Skip to content

Fix mouse behavior in scale_factor_override example #7382

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

Closed
wants to merge 1 commit into from
Closed
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
40 changes: 19 additions & 21 deletions examples/window/scale_factor_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@

use bevy::{prelude::*, window::WindowResolution};

#[derive(Resource, Default)]
struct Override(Option<f64>);

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: WindowResolution::new(500., 300.).with_scale_factor_override(1.0),
resolution: WindowResolution::new(500., 300.),
..default()
}),
..default()
}))
.init_resource::<Override>()
.add_startup_system(setup)
.add_system(display_override)
.add_system(update_window)
.add_system(toggle_override)
.add_system(change_scale_factor)
.run();
Expand Down Expand Up @@ -47,7 +51,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.with_children(|parent| {
parent.spawn(
TextBundle::from_section(
"Example text",
"[Enter] Toggle override\n[↑↓] Change scale",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
Expand All @@ -63,39 +67,33 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
});
}

/// Set the title of the window to the current override
fn display_override(mut windows: Query<&mut Window>) {
fn update_window(mut windows: Query<&mut Window>, over: Res<Override>) {
if !over.is_changed() {
return;
}

let mut window = windows.single_mut();

window.resolution.set_scale_factor_override(over.0);

window.title = format!(
"Scale override: {:?}",
window.resolution.scale_factor_override()
);
}

/// This system toggles scale factor overrides when enter is pressed
fn toggle_override(input: Res<Input<KeyCode>>, mut windows: Query<&mut Window>) {
let mut window = windows.single_mut();

fn toggle_override(input: Res<Input<KeyCode>>, mut over: ResMut<Override>) {
if input.just_pressed(KeyCode::Return) {
let scale_factor_override = window.resolution.scale_factor_override();
window
.resolution
.set_scale_factor_override(scale_factor_override.xor(Some(1.0)));
over.0 = over.0.xor(Some(1.0));
}
}

/// This system changes the scale factor override when up or down is pressed
fn change_scale_factor(input: Res<Input<KeyCode>>, mut windows: Query<&mut Window>) {
let mut window = windows.single_mut();
let scale_factor_override = window.resolution.scale_factor_override();
fn change_scale_factor(input: Res<Input<KeyCode>>, mut over: ResMut<Override>) {
if input.just_pressed(KeyCode::Up) {
window
.resolution
.set_scale_factor_override(scale_factor_override.map(|n| n + 1.0));
over.0 = over.0.map(|n| n + 1.0);
} else if input.just_pressed(KeyCode::Down) {
window
.resolution
.set_scale_factor_override(scale_factor_override.map(|n| (n - 1.0).max(1.0)));
over.0 = over.0.map(|n| (n - 1.0).max(1.0));
}
}