Skip to content
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

Change Window scale factor to f32 (adopted) #10897

Merged
merged 7 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct RenderTargetInfo {
/// The physical size of this render target (ignores scale factor).
pub physical_size: UVec2,
/// The scale factor of this render target.
pub scale_factor: f64,
pub scale_factor: f32,
}

/// Holds internally computed [`Camera`] values.
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Camera {
#[inline]
pub fn to_logical(&self, physical_size: UVec2) -> Option<Vec2> {
let scale = self.computed.target_info.as_ref()?.scale_factor;
Some((physical_size.as_dvec2() / scale).as_vec2())
Some(physical_size.as_vec2() / scale)
}

/// The rendered physical bounds [`URect`] of the camera. If the `viewport` field is
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl TextPipeline {
&mut self,
fonts: &Assets<Font>,
sections: &[TextSection],
scale_factor: f64,
scale_factor: f32,
text_alignment: JustifyText,
linebreak_behavior: BreakLineOn,
bounds: Vec2,
Expand Down Expand Up @@ -129,7 +129,7 @@ impl TextMeasureInfo {
pub fn from_text(
text: &Text,
fonts: &Assets<Font>,
scale_factor: f64,
scale_factor: f32,
) -> Result<TextMeasureInfo, TextError> {
let sections = &text.sections;
for section in sections {
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn extract_text2d_sprite(
// TODO: Support window-independent scaling: https://github.com/bevyengine/bevy/issues/5621
let scale_factor = windows
.get_single()
.map(|window| window.resolution.scale_factor() as f32)
.map(|window| window.resolution.scale_factor())
.unwrap_or(1.0);
let scaling = GlobalTransform::from_scale(Vec2::splat(scale_factor.recip()).extend(1.));

Expand Down Expand Up @@ -225,6 +225,6 @@ pub fn update_text2d_layout(
}

/// Scales `value` by `factor`.
pub fn scale_value(value: f32, factor: f64) -> f32 {
(value as f64 * factor) as f32
pub fn scale_value(value: f32, factor: f32) -> f32 {
value * factor
}
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,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.0 as f32);
.map(|cursor_position| cursor_position / ui_scale.0);

// 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`
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ui/src/layout/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ impl Val {
match self {
Val::Auto => taffy::style::LengthPercentageAuto::Auto,
Val::Percent(value) => taffy::style::LengthPercentageAuto::Percent(value / 100.),
Val::Px(value) => taffy::style::LengthPercentageAuto::Points(
(context.scale_factor * value as f64) as f32,
),
Val::Px(value) => {
taffy::style::LengthPercentageAuto::Points(context.scale_factor * value)
}
Val::VMin(value) => {
taffy::style::LengthPercentageAuto::Points(context.min_size * value / 100.)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ use taffy::Taffy;
use thiserror::Error;

pub struct LayoutContext {
pub scale_factor: f64,
pub scale_factor: f32,
pub physical_size: Vec2,
pub min_size: f32,
pub max_size: f32,
}

impl LayoutContext {
/// create new a [`LayoutContext`] from the window's physical size and scale factor
fn new(scale_factor: f64, physical_size: Vec2) -> Self {
fn new(scale_factor: f32, physical_size: Vec2) -> Self {
Self {
scale_factor,
physical_size,
Expand Down Expand Up @@ -382,7 +382,7 @@ pub fn ui_layout_system(
&ui_surface,
&mut node_transform_query,
&just_children_query,
inverse_target_scale_factor as f32,
inverse_target_scale_factor,
Vec2::ZERO,
Vec2::ZERO,
);
Expand All @@ -399,7 +399,7 @@ pub fn resolve_outlines_system(
.get_single()
.map(|window| Vec2::new(window.resolution.width(), window.resolution.height()))
.unwrap_or(Vec2::ZERO)
/ ui_scale.0 as f32;
/ ui_scale.0;

for (outline, mut node) in outlines_query.iter_mut() {
let node = node.bypass_change_detection();
Expand Down Expand Up @@ -806,7 +806,7 @@ mod tests {
.copied()
.collect::<Vec<Entity>>();

for r in [2, 3, 5, 7, 11, 13, 17, 19, 21, 23, 29, 31].map(|n| (n as f64).recip()) {
for r in [2, 3, 5, 7, 11, 13, 17, 19, 21, 23, 29, 31].map(|n| (n as f32).recip()) {
let mut s = r;
while s <= 5. {
world.resource_mut::<UiScale>().0 = s;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub enum UiSystem {
/// A multiplier to fixed-sized ui values.
/// **Note:** This will only affect fixed ui values like [`Val::Px`]
#[derive(Debug, Reflect, Resource, Deref, DerefMut)]
pub struct UiScale(pub f64);
pub struct UiScale(pub f32);

impl Default for UiScale {
fn default() -> Self {
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,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.0 as f32;
/ ui_scale.0;

for (node, global_transform, style, border_color, parent, view_visibility, clip) in
uinode_query.iter()
Expand Down Expand Up @@ -545,7 +545,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.0 as f32).recip();
let scale = (ui_scale.0).recip();
for (entity, camera, camera_ui) in &query {
// ignore cameras with disabled ui
if matches!(camera_ui, Some(&UiCameraConfig { show_ui: false, .. })) {
Expand Down Expand Up @@ -624,11 +624,11 @@ pub fn extract_text_uinodes(
) {
// TODO: Support window-independent UI scale: https://github.com/bevyengine/bevy/issues/5621

let scale_factor = (windows
let scale_factor = windows
.get_single()
.map(|window| window.scale_factor())
.unwrap_or(1.)
* ui_scale.0) as f32;
* ui_scale.0;

let inverse_scale_factor = scale_factor.recip();

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/render/ui_material_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ pub fn extract_ui_material_nodes<M: UiMaterial>(
.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.0 as f32;
/ ui_scale.0;
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
if let Ok((entity, uinode, style, transform, handle, view_visibility, clip)) =
uinode_query.get(*entity)
Expand Down
18 changes: 9 additions & 9 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ impl Node {

/// Returns the size of the node in physical pixels based on the given scale factor and `UiScale`.
#[inline]
pub fn physical_size(&self, scale_factor: f64, ui_scale: f64) -> Vec2 {
pub fn physical_size(&self, scale_factor: f32, ui_scale: f32) -> Vec2 {
Vec2::new(
(self.calculated_size.x as f64 * scale_factor * ui_scale) as f32,
(self.calculated_size.y as f64 * scale_factor * ui_scale) as f32,
self.calculated_size.x * scale_factor * ui_scale,
self.calculated_size.y * scale_factor * ui_scale,
)
}

Expand All @@ -75,18 +75,18 @@ impl Node {
pub fn physical_rect(
&self,
transform: &GlobalTransform,
scale_factor: f64,
ui_scale: f64,
scale_factor: f32,
ui_scale: f32,
) -> Rect {
let rect = self.logical_rect(transform);
Rect {
min: Vec2::new(
(rect.min.x as f64 * scale_factor * ui_scale) as f32,
(rect.min.y as f64 * scale_factor * ui_scale) as f32,
rect.min.x * scale_factor * ui_scale,
rect.min.y * scale_factor * ui_scale,
),
max: Vec2::new(
(rect.max.x as f64 * scale_factor * ui_scale) as f32,
(rect.max.y as f64 * scale_factor * ui_scale) as f32,
rect.max.x * scale_factor * ui_scale,
rect.max.y * scale_factor * ui_scale,
),
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ui/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type UpdateImageFilter = With<Node>;

/// Updates content size of the node based on the image provided
pub fn update_image_content_size_system(
mut previous_combined_scale_factor: Local<f64>,
mut previous_combined_scale_factor: Local<f32>,
windows: Query<&Window, With<PrimaryWindow>>,
ui_scale: Res<UiScale>,
textures: Res<Assets<Image>>,
Expand All @@ -101,7 +101,7 @@ pub fn update_image_content_size_system(
image_size.size = size;
content_size.set(ImageMeasure {
// multiply the image size by the scale factor to get the physical size
size: size * combined_scale_factor as f32,
size: size * combined_scale_factor,
});
}
}
Expand All @@ -112,7 +112,7 @@ pub fn update_image_content_size_system(

/// Updates content size of the node based on the texture atlas sprite
pub fn update_atlas_content_size_system(
mut previous_combined_scale_factor: Local<f64>,
mut previous_combined_scale_factor: Local<f32>,
windows: Query<&Window, With<PrimaryWindow>>,
ui_scale: Res<UiScale>,
atlases: Res<Assets<TextureAtlas>>,
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn update_atlas_content_size_system(
image_size.size = size;
content_size.set(ImageMeasure {
// multiply the image size by the scale factor to get the physical size
size: size * combined_scale_factor as f32,
size: size * combined_scale_factor,
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Measure for TextMeasure {
#[inline]
fn create_text_measure(
fonts: &Assets<Font>,
scale_factor: f64,
scale_factor: f32,
text: Ref<Text>,
mut content_size: Mut<ContentSize>,
mut text_flags: Mut<TextFlags>,
Expand Down Expand Up @@ -117,7 +117,7 @@ fn create_text_measure(
/// color changes. This can be expensive, particularly for large blocks of text, and the [`bypass_change_detection`](bevy_ecs::change_detection::DetectChangesMut::bypass_change_detection)
/// method should be called when only changing the `Text`'s colors.
pub fn measure_text_system(
mut last_scale_factor: Local<f64>,
mut last_scale_factor: Local<f32>,
fonts: Res<Assets<Font>>,
windows: Query<&Window, With<PrimaryWindow>>,
ui_scale: Res<UiScale>,
Expand Down Expand Up @@ -158,8 +158,8 @@ fn queue_text(
texture_atlases: &mut Assets<TextureAtlas>,
textures: &mut Assets<Image>,
text_settings: &TextSettings,
scale_factor: f64,
inverse_scale_factor: f64,
scale_factor: f32,
inverse_scale_factor: f32,
text: &Text,
node: Ref<Node>,
mut text_flags: Mut<TextFlags>,
Expand All @@ -173,8 +173,8 @@ fn queue_text(
} else {
// `scale_factor` is already multiplied by `UiScale`
Vec2::new(
(node.unrounded_size.x as f64 * scale_factor) as f32,
(node.unrounded_size.y as f64 * scale_factor) as f32,
node.unrounded_size.x * scale_factor,
node.unrounded_size.y * scale_factor,
)
};

Expand Down Expand Up @@ -220,7 +220,7 @@ fn queue_text(
#[allow(clippy::too_many_arguments)]
pub fn text_system(
mut textures: ResMut<Assets<Image>>,
mut last_scale_factor: Local<f64>,
mut last_scale_factor: Local<f32>,
fonts: Res<Assets<Font>>,
windows: Query<&Window, With<PrimaryWindow>>,
text_settings: Res<TextSettings>,
Expand Down
Loading