Change UiScale to a tuple struct (#9444)

# Objective

Inconvenient initialization of `UiScale`

## Solution

Change `UiScale` to a tuple struct

## Migration Guide

Replace initialization of `UiScale` like ```UiScale { scale: 1.0 }```
with ```UiScale(1.0)```
This commit is contained in:
st0rmbtw 2023-08-16 21:18:50 +03:00 committed by GitHub
parent 3aad5c6b99
commit b6a9d8eba7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 17 deletions

View File

@ -191,7 +191,7 @@ pub fn ui_focus_system(
.or_else(|| touches_input.first_pressed_position())
// The cursor position returned by `Window` only takes into account the window scale factor and not `UiScale`.
// To convert the cursor position to logical UI viewport coordinates we have to divide it by `UiScale`.
.map(|cursor_position| cursor_position / ui_scale.scale as f32);
.map(|cursor_position| cursor_position / ui_scale.0 as f32);
// prepare an iterator that contains all the nodes that have the cursor in their rect,
// from the top node to the bottom one. this will also reset the interaction to `None`

View File

@ -256,7 +256,7 @@ pub fn ui_layout_system(
ui_surface.update_window(entity, &window.resolution);
}
let scale_factor = logical_to_physical_factor * ui_scale.scale;
let scale_factor = logical_to_physical_factor * ui_scale.0;
let layout_context = LayoutContext::new(scale_factor, physical_size);

View File

@ -19,6 +19,7 @@ pub mod node_bundles;
pub mod update;
pub mod widget;
use bevy_derive::{Deref, DerefMut};
#[cfg(feature = "bevy_text")]
use bevy_render::camera::CameraUpdateSystem;
use bevy_render::{extract_component::ExtractComponentPlugin, RenderApp};
@ -67,15 +68,12 @@ pub enum UiSystem {
///
/// A multiplier to fixed-sized ui values.
/// **Note:** This will only affect fixed ui values like [`Val::Px`]
#[derive(Debug, Resource)]
pub struct UiScale {
/// The scale to be applied.
pub scale: f64,
}
#[derive(Debug, Resource, Deref, DerefMut)]
pub struct UiScale(pub f64);
impl Default for UiScale {
fn default() -> Self {
Self { scale: 1.0 }
Self(1.0)
}
}

View File

@ -286,7 +286,7 @@ pub fn extract_uinode_borders(
.unwrap_or(Vec2::ZERO)
// The logical window resolution returned by `Window` only takes into account the window scale factor and not `UiScale`,
// so we have to divide by `UiScale` to get the size of the UI viewport.
/ ui_scale.scale as f32;
/ ui_scale.0 as f32;
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
if let Ok((node, global_transform, style, border_color, parent, visibility, clip)) =
@ -450,7 +450,7 @@ pub fn extract_default_ui_camera_view<T: Component>(
ui_scale: Extract<Res<UiScale>>,
query: Extract<Query<(Entity, &Camera, Option<&UiCameraConfig>), With<T>>>,
) {
let scale = (ui_scale.scale as f32).recip();
let scale = (ui_scale.0 as f32).recip();
for (entity, camera, camera_ui) in &query {
// ignore cameras with disabled ui
if matches!(camera_ui, Some(&UiCameraConfig { show_ui: false, .. })) {
@ -527,7 +527,7 @@ pub fn extract_text_uinodes(
.get_single()
.map(|window| window.resolution.scale_factor())
.unwrap_or(1.0)
* ui_scale.scale;
* ui_scale.0;
let inverse_scale_factor = (scale_factor as f32).recip();

View File

@ -87,7 +87,7 @@ pub fn update_image_content_size_system(
.get_single()
.map(|window| window.resolution.scale_factor())
.unwrap_or(1.)
* ui_scale.scale;
* ui_scale.0;
for (mut content_size, image, mut image_size) in &mut query {
if let Some(texture) = textures.get(&image.texture) {
@ -129,7 +129,7 @@ pub fn update_atlas_content_size_system(
.get_single()
.map(|window| window.resolution.scale_factor())
.unwrap_or(1.)
* ui_scale.scale;
* ui_scale.0;
for (mut content_size, atlas, atlas_image, mut image_size) in &mut atlas_query {
if let Some(atlas) = atlases.get(atlas) {

View File

@ -135,7 +135,7 @@ pub fn measure_text_system(
.map(|window| window.resolution.scale_factor())
.unwrap_or(1.);
let scale_factor = ui_scale.scale * window_scale_factor;
let scale_factor = ui_scale.0 * window_scale_factor;
#[allow(clippy::float_cmp)]
if *last_scale_factor == scale_factor {
@ -252,7 +252,7 @@ pub fn text_system(
.map(|window| window.resolution.scale_factor())
.unwrap_or(1.);
let scale_factor = ui_scale.scale * window_scale_factor;
let scale_factor = ui_scale.0 * window_scale_factor;
if *last_scale_factor == scale_factor {
// Scale factor unchanged, only recompute text for modified text nodes

View File

@ -136,7 +136,7 @@ fn apply_scaling(
return;
}
ui_scale.scale = target_scale.current_scale();
ui_scale.0 = target_scale.current_scale();
}
fn ease_in_expo(x: f64) -> f64 {

View File

@ -28,7 +28,7 @@ enum Coords {
fn main() {
App::new()
.insert_resource(UiScale { scale: 2.0 })
.insert_resource(UiScale(2.0))
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: [1600., 1200.].into(),