This commit is contained in:
Leon Liu 2025-08-14 12:53:16 +09:00
parent ad4c3ada92
commit b992a1dd5f

View File

@ -1,14 +1,11 @@
use bevy::{ use bevy::{
core_pipeline::tonemapping::Tonemapping, core_pipeline::{bloom::Bloom, tonemapping::Tonemapping},
input::mouse::{MouseScrollUnit, MouseWheel}, input::mouse::{MouseScrollUnit, MouseWheel},
math::DVec3, math::DVec3,
prelude::*, prelude::*,
window::WindowResolution, window::{WindowMode, WindowResolution},
};
use bevy_inspector_egui::{
bevy_egui::{EguiContextSettings, EguiPlugin},
quick::WorldInspectorPlugin,
}; };
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
use iyes_perf_ui::{PerfUiPlugin, prelude::*}; use iyes_perf_ui::{PerfUiPlugin, prelude::*};
// Scaling factor to convert AU to game units // Scaling factor to convert AU to game units
@ -79,14 +76,6 @@ struct CameraFollow {
distance: f32, // Current zoom distance from target distance: f32, // Current zoom distance from target
} }
// Resource to manage UI scaling across different resolutions
#[derive(Resource)]
struct UiScale {
scale_factor: f32,
base_font_size: f32,
current_font_size: f32,
}
pub struct SolarRenderingPlugin; pub struct SolarRenderingPlugin;
impl Plugin for SolarRenderingPlugin { impl Plugin for SolarRenderingPlugin {
@ -95,11 +84,6 @@ impl Plugin for SolarRenderingPlugin {
target: None, target: None,
distance: 10.0, distance: 10.0,
}) })
.insert_resource(UiScale {
scale_factor: 1.0,
base_font_size: 16.0,
current_font_size: 16.0,
})
.add_systems(Startup, (setup_rendering, setup_ui)) .add_systems(Startup, (setup_rendering, setup_ui))
.add_systems( .add_systems(
FixedPostUpdate, FixedPostUpdate,
@ -117,8 +101,6 @@ impl Plugin for SolarRenderingPlugin {
handle_label_clicks, handle_label_clicks,
handle_scroll_zoom, handle_scroll_zoom,
handle_speed_controls, handle_speed_controls,
update_ui_scale,
update_label_font_sizes.after(update_ui_scale),
), ),
); );
} }
@ -199,14 +181,7 @@ fn setup_rendering(mut commands: Commands) {
}), }),
Tonemapping::TonyMcMapface, Tonemapping::TonyMcMapface,
Transform::from_translation(Vec3::new(0., 0., 10.0)), Transform::from_translation(Vec3::new(0., 0., 10.0)),
// Bloom::NATURAL, Bloom::NATURAL,
// PanOrbitCamera {
// pan_sensitivity: 0.0, // Disable panning by setting sensitivity to 0
// focus: Vec3::ZERO,
// zoom_lower_limit: 1e-8,
// allow_upside_down: true,
// ..default()
// },
)); ));
commands.spawn(PointLight { commands.spawn(PointLight {
@ -245,7 +220,7 @@ fn sync_name_labels(
Text::new(name.0.0.clone()), Text::new(name.0.0.clone()),
TextColor(Color::WHITE), TextColor(Color::WHITE),
TextFont { TextFont {
font_size: ui_scale.current_font_size, font_size: 16.0,
..default() ..default()
}, },
Node { Node {
@ -279,7 +254,7 @@ fn update_label_positions(
let world_pos = global_transform.translation(); let world_pos = global_transform.translation();
if let Ok(screen_pos) = camera.world_to_viewport(camera_transform, world_pos) { if let Ok(screen_pos) = camera.world_to_viewport(camera_transform, world_pos) {
// Apply scale factor to label offset to maintain relative positioning // Apply scale factor to label offset to maintain relative positioning
let scaled_offset = 10.0 * ui_scale.scale_factor; let scaled_offset = 10.0;
// Position the label on screen, offset slightly to avoid overlapping the object // Position the label on screen, offset slightly to avoid overlapping the object
node.left = Val::Px(screen_pos.x + scaled_offset); node.left = Val::Px(screen_pos.x + scaled_offset);
@ -422,42 +397,6 @@ fn handle_speed_controls(
} }
} }
fn update_ui_scale(
windows: Query<&Window>,
mut ui_scale: ResMut<UiScale>,
egui_contexts: Query<&mut EguiContextSettings>,
) {
if let Ok(window) = windows.single() {
// Calculate scale factor based on physical resolution
// Use 1080p as base resolution (1920x1080)
let base_height = 1080.0;
let current_height = window.physical_height() as f32;
let new_scale_factor = current_height / base_height;
// Update scale factor if it changed significantly
if (new_scale_factor - ui_scale.scale_factor).abs() > 0.01 {
ui_scale.scale_factor = new_scale_factor;
ui_scale.current_font_size = ui_scale.base_font_size * new_scale_factor;
// Apply scaling to egui (affects WorldInspectorPlugin)
for mut egui_settings in egui_contexts {
egui_settings.scale_factor = new_scale_factor
}
}
}
}
fn update_label_font_sizes(
mut label_query: Query<&mut TextFont, With<ObjectLabel>>,
ui_scale: Res<UiScale>,
) {
if ui_scale.is_changed() {
for mut text_font in label_query.iter_mut() {
text_font.font_size = ui_scale.current_font_size;
}
}
}
fn initialize_camera_follow( fn initialize_camera_follow(
earth_query: Query<Entity, (With<Earth>, With<Trackable>)>, earth_query: Query<Entity, (With<Earth>, With<Trackable>)>,
mut camera_follow: ResMut<CameraFollow>, mut camera_follow: ResMut<CameraFollow>,
@ -602,8 +541,8 @@ fn main() {
.add_plugins(DefaultPlugins.set(WindowPlugin { .add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
title: "Solar Sim".to_string(), title: "Solar Sim".to_string(),
// mode: WindowMode::BorderlessFullscreen(MonitorSelection::Primary), mode: WindowMode::BorderlessFullscreen(MonitorSelection::Primary),
resolution: WindowResolution::default().with_scale_factor_override(1.0), resolution: WindowResolution::default().with_scale_factor_override(2.0),
..default() ..default()
}), }),
..default() ..default()