2020-11-13 04:39:47 +00:00
|
|
|
use crate::window::Window;
|
2020-10-05 08:35:24 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-04-26 04:29:35 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Type of scaling to use.
|
2019-07-26 02:28:53 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
2019-04-26 04:29:35 +00:00
|
|
|
pub enum ScaleMode {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Scale against physical size.
|
2019-04-26 04:29:35 +00:00
|
|
|
Absolute(f64),
|
2019-05-17 09:22:32 +00:00
|
|
|
// Scale based on the window's physical size, but maintain aspect ratio of widgets.
|
2020-02-01 20:39:39 +00:00
|
|
|
// Contains width and height of the "default" window size (ie where there should be no
|
|
|
|
// scaling).
|
2019-04-26 04:29:35 +00:00
|
|
|
RelativeToWindow(Vec2<f64>),
|
2020-11-11 17:37:26 +00:00
|
|
|
// Use the dpi factor provided by the windowing system (i.e. use logical size).
|
|
|
|
#[serde(other)]
|
|
|
|
// Would be `RelativeToWindow([1920.0, 1080.0].into())`, but only supported on unit variants
|
|
|
|
DpiFactor,
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 02:28:53 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2019-04-26 04:29:35 +00:00
|
|
|
pub struct Scale {
|
|
|
|
mode: ScaleMode,
|
|
|
|
// Current dpi factor
|
2020-02-20 23:41:51 +00:00
|
|
|
scale_factor: f64,
|
2019-04-26 04:29:35 +00:00
|
|
|
// Current logical window size
|
|
|
|
window_dims: Vec2<f64>,
|
2020-10-31 20:04:20 +00:00
|
|
|
// TEMP
|
|
|
|
extra_factor: f64,
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Scale {
|
2020-10-31 20:04:20 +00:00
|
|
|
pub fn new(window: &Window, mode: ScaleMode, extra_factor: f64) -> Self {
|
2019-04-26 04:29:35 +00:00
|
|
|
let window_dims = window.logical_size();
|
2020-11-13 04:39:47 +00:00
|
|
|
let scale_factor = window.scale_factor();
|
2019-04-26 04:29:35 +00:00
|
|
|
Scale {
|
|
|
|
mode,
|
2020-02-20 23:41:51 +00:00
|
|
|
scale_factor,
|
2019-04-26 04:29:35 +00:00
|
|
|
window_dims,
|
2020-10-31 20:04:20 +00:00
|
|
|
extra_factor,
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Change the scaling mode.
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn set_scaling_mode(&mut self, mode: ScaleMode) { self.mode = mode; }
|
|
|
|
|
2019-07-26 02:28:53 +00:00
|
|
|
// Get scaling mode transformed into absolute scaling
|
|
|
|
pub fn scaling_mode_as_absolute(&self) -> ScaleMode {
|
|
|
|
ScaleMode::Absolute(self.scale_factor_physical())
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
|
|
|
// Get scaling mode transformed to be relative to the window with the same
|
|
|
|
// aspect ratio as the current window
|
2019-07-26 02:28:53 +00:00
|
|
|
pub fn scaling_mode_as_relative(&self) -> ScaleMode {
|
|
|
|
let scale = self.scale_factor_logical();
|
|
|
|
ScaleMode::RelativeToWindow(self.window_dims.map(|e| e / scale))
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2020-10-31 20:04:20 +00:00
|
|
|
/// Calculate factor to transform between logical coordinates and our scaled
|
|
|
|
/// coordinates.
|
|
|
|
/// Multiply by scaled coordinates to get the logical coordinates
|
2019-04-26 04:29:35 +00:00
|
|
|
pub fn scale_factor_logical(&self) -> f64 {
|
2020-10-31 20:04:20 +00:00
|
|
|
self.extra_factor
|
|
|
|
* match self.mode {
|
|
|
|
ScaleMode::Absolute(scale) => scale / self.scale_factor,
|
|
|
|
ScaleMode::DpiFactor => 1.0,
|
|
|
|
ScaleMode::RelativeToWindow(dims) => {
|
|
|
|
(self.window_dims.x / dims.x).min(self.window_dims.y / dims.y)
|
|
|
|
},
|
|
|
|
}
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2020-10-31 20:04:20 +00:00
|
|
|
/// Calculate factor to transform between physical coordinates and our
|
|
|
|
/// scaled coordinates.
|
|
|
|
/// Multiply by scaled coordinates to get the physical coordinates
|
2020-02-20 23:41:51 +00:00
|
|
|
pub fn scale_factor_physical(&self) -> f64 { self.scale_factor_logical() * self.scale_factor }
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2020-11-13 04:39:47 +00:00
|
|
|
/// Updates window size
|
|
|
|
/// Returns true if the value was changed
|
2020-11-12 01:00:31 +00:00
|
|
|
#[allow(clippy::float_cmp)]
|
2020-11-13 04:39:47 +00:00
|
|
|
pub fn window_resized(&mut self, new_dims: Vec2<f64>) -> bool {
|
2020-11-12 01:00:31 +00:00
|
|
|
let old_window_dims = self.window_dims;
|
2019-04-26 04:29:35 +00:00
|
|
|
self.window_dims = new_dims;
|
2020-11-13 04:39:47 +00:00
|
|
|
old_window_dims != self.window_dims
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates scale factor
|
|
|
|
/// Returns true if the value was changed
|
|
|
|
#[allow(clippy::float_cmp)]
|
|
|
|
pub fn scale_factor_changed(&mut self, scale_factor: f64) -> bool {
|
|
|
|
let old_scale_factor = self.scale_factor;
|
|
|
|
self.scale_factor = scale_factor;
|
|
|
|
old_scale_factor != self.scale_factor
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2020-10-31 20:04:20 +00:00
|
|
|
/// Get scaled window size.
|
2020-11-13 04:39:47 +00:00
|
|
|
pub fn scaled_resolution(&self) -> Vec2<f64> { self.window_dims / self.scale_factor_logical() }
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2020-10-31 20:04:20 +00:00
|
|
|
/// Get logical window size
|
2020-11-13 04:39:47 +00:00
|
|
|
pub fn logical_resolution(&self) -> Vec2<f64> { self.window_dims }
|
|
|
|
|
|
|
|
/// Get physical window size
|
|
|
|
pub fn physical_resolution(&self) -> Vec2<u16> {
|
|
|
|
(self.window_dims * self.scale_factor).map(|e| e.round() as u16)
|
|
|
|
}
|
2020-10-31 20:04:20 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Transform point from logical to scaled coordinates.
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn scale_point(&self, point: Vec2<f64>) -> Vec2<f64> { point / self.scale_factor_logical() }
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|