Skip to content

Commit

Permalink
Refs bevyengine#8900 -- Changing scale factor to f32
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsgntz authored and tygyh committed Dec 6, 2023
1 parent d9aac88 commit cb5a6e0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 46 deletions.
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
}
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
4 changes: 2 additions & 2 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
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
12 changes: 6 additions & 6 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
30 changes: 15 additions & 15 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Window {
///
/// Ratio of physical size to logical size, see [`WindowResolution`].
#[inline]
pub fn scale_factor(&self) -> f64 {
pub fn scale_factor(&self) -> f32 {
self.resolution.scale_factor()
}

Expand All @@ -338,7 +338,7 @@ impl Window {
#[inline]
pub fn cursor_position(&self) -> Option<Vec2> {
self.physical_cursor_position()
.map(|position| (position.as_dvec2() / self.scale_factor()).as_vec2())
.map(|position| (position.as_dvec2() / self.scale_factor() as f64).as_vec2())
}

/// The cursor position in this window in physical pixels.
Expand Down Expand Up @@ -369,7 +369,7 @@ impl Window {
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
pub fn set_cursor_position(&mut self, position: Option<Vec2>) {
self.internal.physical_cursor_position =
position.map(|p| p.as_dvec2() * self.scale_factor());
position.map(|p| p.as_dvec2() * self.scale_factor() as f64);
}

/// Set the cursor position in this window in physical pixels.
Expand Down Expand Up @@ -611,11 +611,11 @@ pub struct WindowResolution {
/// Code-provided ratio of physical size to logical size.
///
/// Should be used instead of `scale_factor` when set.
scale_factor_override: Option<f64>,
scale_factor_override: Option<f32>,
/// OS-provided ratio of physical size to logical size.
///
/// Set automatically depending on the pixel density of the screen.
scale_factor: f64,
scale_factor: f32,
}

impl Default for WindowResolution {
Expand All @@ -640,21 +640,21 @@ impl WindowResolution {
}

/// Builder method for adding a scale factor override to the resolution.
pub fn with_scale_factor_override(mut self, scale_factor_override: f64) -> Self {
pub fn with_scale_factor_override(mut self, scale_factor_override: f32) -> Self {
self.scale_factor_override = Some(scale_factor_override);
self
}

/// The window's client area width in logical pixels.
#[inline]
pub fn width(&self) -> f32 {
(self.physical_width() as f64 / self.scale_factor()) as f32
self.physical_width() as f32 / self.scale_factor()
}

/// The window's client area height in logical pixels.
#[inline]
pub fn height(&self) -> f32 {
(self.physical_height() as f64 / self.scale_factor()) as f32
self.physical_height() as f32 / self.scale_factor()
}

/// The window's client area width in physical pixels.
Expand All @@ -672,7 +672,7 @@ impl WindowResolution {
/// The ratio of physical pixels to logical pixels.
///
/// `physical_pixels = logical_pixels * scale_factor`
pub fn scale_factor(&self) -> f64 {
pub fn scale_factor(&self) -> f32 {
self.scale_factor_override
.unwrap_or_else(|| self.base_scale_factor())
}
Expand All @@ -681,24 +681,24 @@ impl WindowResolution {
///
/// This value is unaffected by [`WindowResolution::scale_factor_override`].
#[inline]
pub fn base_scale_factor(&self) -> f64 {
pub fn base_scale_factor(&self) -> f32 {
self.scale_factor
}

/// The scale factor set with [`WindowResolution::set_scale_factor_override`].
///
/// This value may be different from the scale factor reported by the window backend.
#[inline]
pub fn scale_factor_override(&self) -> Option<f64> {
pub fn scale_factor_override(&self) -> Option<f32> {
self.scale_factor_override
}

/// Set the window's logical resolution.
#[inline]
pub fn set(&mut self, width: f32, height: f32) {
self.set_physical_resolution(
(width as f64 * self.scale_factor()) as u32,
(height as f64 * self.scale_factor()) as u32,
(width * self.scale_factor()) as u32,
(height * self.scale_factor()) as u32,
);
}

Expand All @@ -714,7 +714,7 @@ impl WindowResolution {

/// Set the window's scale factor, this may get overridden by the backend.
#[inline]
pub fn set_scale_factor(&mut self, scale_factor: f64) {
pub fn set_scale_factor(&mut self, scale_factor: f32) {
let (width, height) = (self.width(), self.height());
self.scale_factor = scale_factor;
self.set(width, height);
Expand All @@ -725,7 +725,7 @@ impl WindowResolution {
/// This can change the logical and physical sizes if the resulting physical
/// size is not within the limits.
#[inline]
pub fn set_scale_factor_override(&mut self, scale_factor_override: Option<f64>) {
pub fn set_scale_factor_override(&mut self, scale_factor_override: Option<f32>) {
let (width, height) = (self.width(), self.height());
self.scale_factor_override = scale_factor_override;
self.set(width, height);
Expand Down
14 changes: 7 additions & 7 deletions examples/ui/ui_scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ fn change_scaling(input: Res<Input<KeyCode>>, mut ui_scale: ResMut<TargetScale>)

#[derive(Resource)]
struct TargetScale {
start_scale: f64,
target_scale: f64,
start_scale: f32,
target_scale: f32,
target_time: Timer,
}

impl TargetScale {
fn set_scale(&mut self, scale: f64) {
fn set_scale(&mut self, scale: f32) {
self.start_scale = self.current_scale();
self.target_scale = scale;
self.target_time.reset();
}

fn current_scale(&self) -> f64 {
fn current_scale(&self) -> f32 {
let completion = self.target_time.fraction();
let multiplier = ease_in_expo(completion as f64);
let multiplier = ease_in_expo(completion);
self.start_scale + (self.target_scale - self.start_scale) * multiplier
}

Expand All @@ -139,10 +139,10 @@ fn apply_scaling(
ui_scale.0 = target_scale.current_scale();
}

fn ease_in_expo(x: f64) -> f64 {
fn ease_in_expo(x: f32) -> f32 {
if x == 0. {
0.
} else {
(2.0f64).powf(5. * x - 5.)
2.0f32.powf(5. * x - 5.)
}
}

0 comments on commit cb5a6e0

Please sign in to comment.