try adding resize constraints to base game plugin

main
Elijah Voigt 2 months ago
parent a6adc5dfc8
commit d4cfeb42ff

@ -2,9 +2,20 @@ use games::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(BaseGamePlugin { target_resolution: (360.0, 640.0).into(), ..default() }) .add_plugins(BaseGamePlugin {
target_resolution: (360.0, 640.0).into(),
resize_constraints: WindowResizeConstraints {
max_height: 1080.0,
min_height: 640.0,
..default()
},
..default()
})
.add_systems(Startup, init_window_info) .add_systems(Startup, init_window_info)
.add_systems(Update, update_window_info.run_if(any_component_changed::<Window>)) .add_systems(
Update,
update_window_info.run_if(any_component_changed::<Window>),
)
.run(); .run();
} }

@ -6,6 +6,7 @@ pub struct BaseGamePlugin {
pub name: String, pub name: String,
pub game_type: GameType, pub game_type: GameType,
pub target_resolution: WindowResolution, pub target_resolution: WindowResolution,
pub resize_constraints: WindowResizeConstraints,
} }
pub enum GameType { pub enum GameType {
@ -20,6 +21,7 @@ impl Default for BaseGamePlugin {
name: "mygame".into(), name: "mygame".into(),
game_type: GameType::Three, game_type: GameType::Three,
target_resolution: WindowResolution::default(), target_resolution: WindowResolution::default(),
resize_constraints: WindowResizeConstraints::default(),
} }
} }
} }
@ -32,6 +34,7 @@ impl Plugin for BaseGamePlugin {
primary_window: Some(Window { primary_window: Some(Window {
fit_canvas_to_parent: true, fit_canvas_to_parent: true,
resolution: self.target_resolution.clone(), resolution: self.target_resolution.clone(),
resize_constraints: self.resize_constraints.clone(),
..default() ..default()
}), }),
..default() ..default()

@ -13,6 +13,7 @@ impl Plugin for DebuggingPlugin {
.init_resource::<ToolTip>() .init_resource::<ToolTip>()
.init_resource::<Fps>() .init_resource::<Fps>()
.init_resource::<EntityCount>() .init_resource::<EntityCount>()
.init_resource::<WindowInfo>()
.init_resource::<Notice>() .init_resource::<Notice>()
.add_systems(Startup, init_debug_ui) .add_systems(Startup, init_debug_ui)
.add_systems( .add_systems(
@ -33,7 +34,9 @@ impl Plugin for DebuggingPlugin {
track_fps, track_fps,
sync_resource_to_ui::<Fps>.run_if(resource_changed::<Fps>), sync_resource_to_ui::<Fps>.run_if(resource_changed::<Fps>),
track_entity_count, track_entity_count,
track_window_info.run_if(any_component_changed::<Window>),
sync_resource_to_ui::<EntityCount>.run_if(resource_changed::<EntityCount>), sync_resource_to_ui::<EntityCount>.run_if(resource_changed::<EntityCount>),
sync_resource_to_ui::<WindowInfo>.run_if(resource_changed::<WindowInfo>),
sync_resource_to_ui::<Notice>.run_if(resource_changed::<Notice>), sync_resource_to_ui::<Notice>.run_if(resource_changed::<Notice>),
) )
.run_if(in_state(DebuggingState::On)), .run_if(in_state(DebuggingState::On)),
@ -170,6 +173,15 @@ fn init_debug_ui(mut commands: Commands) {
TextColor(BLACK.into()), TextColor(BLACK.into()),
SyncResource::<EntityCount>::default(), SyncResource::<EntityCount>::default(),
)); ));
parent.spawn((
// Window info
DebuggingState::On,
Name::new("Window Info"),
GlobalZIndex(i32::MAX - 1),
Text::new("Window Info..."),
TextColor(BLACK.into()),
SyncResource::<WindowInfo>::default(),
));
}); });
// Tooltip // Tooltip
@ -492,6 +504,33 @@ fn track_entity_count(query: Query<Entity>, mut count: ResMut<EntityCount>) {
count.0 = query.iter().len(); count.0 = query.iter().len();
} }
#[derive(Resource, Default, Debug)]
struct WindowInfo {
logical_size: (f32, f32),
physical_size: (f32, f32),
scale_factor: f32,
mode: String,
}
impl Display for WindowInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
writeln!(
f,
"Res (Logi): {:?}\nRes (Phys): {:?}\nWindow Scale: {}\nWindow Mode: {}",
self.logical_size, self.physical_size, self.scale_factor, self.mode
)
}
}
fn track_window_info(window: Single<&Window>, mut info: ResMut<WindowInfo>) {
(info.logical_size.0, info.logical_size.1) =
(window.resolution.size().x, window.resolution.size().y);
(info.physical_size.0, info.physical_size.1) =
(window.resolution.size().x, window.resolution.size().y);
info.scale_factor = window.resolution.scale_factor();
info.mode = format!("{:?}", window.mode);
}
/// Toggle the debug state when a button is clicked /// Toggle the debug state when a button is clicked
fn toggle_debug( fn toggle_debug(
_trigger: Trigger<Pointer<Click>>, _trigger: Trigger<Pointer<Click>>,

@ -18,7 +18,7 @@ pub use std::fmt::Display;
// Community libraries // Community libraries
pub use bevy::{ pub use bevy::{
asset::{AssetLoader, LoadContext, LoadState, LoadedFolder, io::Reader, AssetMetaCheck}, asset::{AssetLoader, AssetMetaCheck, LoadContext, LoadState, LoadedFolder, io::Reader},
color::palettes::css::*, color::palettes::css::*,
gizmos::{aabb::AabbGizmoPlugin, light::LightGizmoPlugin}, gizmos::{aabb::AabbGizmoPlugin, light::LightGizmoPlugin},
input::{ input::{

Loading…
Cancel
Save