mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 15:14:50 +00:00
Rename Val
evaluate
to resolve
and implement viewport variant support (#9568)
# Objective Rename `Val`'s `evaluate` method to `resolve`. Implement `resolve` support for `Val`'s viewport variants. fixes #9535 --- ## Changelog `bevy_ui::ui_node::Val`: * Renamed the following methods and added a `viewport_size` parameter: - `evaluate` to `resolve` - `try_add_with_size` to `try_add_with_context` - `try_add_assign_with_size` to `try_add_assign_with_context` - `try_sub_with_size` to `try_sub_with_context` - `try_sub_assign_with_size` to `try_sub_assign_with_context` * Implemented `resolve` support for `Val`'s viewport coordinate types ## Migration Guide * Renamed the following `Val` methods and added a `viewport_size` parameter: - `evaluate` to `resolve` - `try_add_with_size` to `try_add_with_context` - `try_add_assign_with_size` to `try_add_assign_with_context` - `try_sub_with_size` to `try_sub_with_context` - `try_sub_assign_with_size` to `try_sub_assign_with_context`
This commit is contained in:
parent
9073d446dc
commit
64dcaf002b
1 changed files with 52 additions and 16 deletions
|
@ -237,16 +237,19 @@ pub enum ValArithmeticError {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Val {
|
impl Val {
|
||||||
/// A convenience function for simple evaluation of [`Val::Percent`] variant into a concrete [`Val::Px`] value.
|
/// Resolves a [`Val`] to its value in logical pixels and returns this as an [`f32`].
|
||||||
/// Returns a [`ValArithmeticError::NonEvaluateable`] if the [`Val`] is impossible to evaluate into [`Val::Px`].
|
/// Returns a [`ValArithmeticError::NonEvaluateable`] if the [`Val`] is impossible to resolve into a concrete value.
|
||||||
/// Otherwise it returns an [`f32`] containing the evaluated value in pixels.
|
|
||||||
///
|
///
|
||||||
/// **Note:** If a [`Val::Px`] is evaluated, it's inner value returned unchanged.
|
/// **Note:** If a [`Val::Px`] is resolved, it's inner value is returned unchanged.
|
||||||
pub fn evaluate(&self, size: f32) -> Result<f32, ValArithmeticError> {
|
pub fn resolve(self, parent_size: f32, viewport_size: Vec2) -> Result<f32, ValArithmeticError> {
|
||||||
match self {
|
match self {
|
||||||
Val::Percent(value) => Ok(size * value / 100.0),
|
Val::Percent(value) => Ok(parent_size * value / 100.0),
|
||||||
Val::Px(value) => Ok(*value),
|
Val::Px(value) => Ok(value),
|
||||||
_ => Err(ValArithmeticError::NonEvaluateable),
|
Val::Vw(value) => Ok(viewport_size.x * value / 100.0),
|
||||||
|
Val::Vh(value) => Ok(viewport_size.y * value / 100.0),
|
||||||
|
Val::VMin(value) => Ok(viewport_size.min_element() * value / 100.0),
|
||||||
|
Val::VMax(value) => Ok(viewport_size.max_element() * value / 100.0),
|
||||||
|
Val::Auto => Err(ValArithmeticError::NonEvaluateable),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1714,33 +1717,66 @@ impl Default for ZIndex {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::Val;
|
||||||
use crate::GridPlacement;
|
use crate::GridPlacement;
|
||||||
use crate::ValArithmeticError;
|
use crate::ValArithmeticError;
|
||||||
|
use bevy_math::vec2;
|
||||||
use super::Val;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn val_evaluate() {
|
fn val_evaluate() {
|
||||||
let size = 250.;
|
let size = 250.;
|
||||||
let result = Val::Percent(80.).evaluate(size).unwrap();
|
let viewport_size = vec2(1000., 500.);
|
||||||
|
let result = Val::Percent(80.).resolve(size, viewport_size).unwrap();
|
||||||
|
|
||||||
assert_eq!(result, size * 0.8);
|
assert_eq!(result, size * 0.8);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn val_evaluate_px() {
|
fn val_resolve_px() {
|
||||||
let size = 250.;
|
let size = 250.;
|
||||||
let result = Val::Px(10.).evaluate(size).unwrap();
|
let viewport_size = vec2(1000., 500.);
|
||||||
|
let result = Val::Px(10.).resolve(size, viewport_size).unwrap();
|
||||||
|
|
||||||
assert_eq!(result, 10.);
|
assert_eq!(result, 10.);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn val_invalid_evaluation() {
|
fn val_resolve_viewport_coords() {
|
||||||
let size = 250.;
|
let size = 250.;
|
||||||
let evaluate_auto = Val::Auto.evaluate(size);
|
let viewport_size = vec2(500., 500.);
|
||||||
|
|
||||||
assert_eq!(evaluate_auto, Err(ValArithmeticError::NonEvaluateable));
|
for value in (-10..10).map(|value| value as f32) {
|
||||||
|
// for a square viewport there should be no difference between `Vw` and `Vh` and between `Vmin` and `Vmax`.
|
||||||
|
assert_eq!(
|
||||||
|
Val::Vw(value).resolve(size, viewport_size),
|
||||||
|
Val::Vh(value).resolve(size, viewport_size)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Val::VMin(value).resolve(size, viewport_size),
|
||||||
|
Val::VMax(value).resolve(size, viewport_size)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Val::VMin(value).resolve(size, viewport_size),
|
||||||
|
Val::Vw(value).resolve(size, viewport_size)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let viewport_size = vec2(1000., 500.);
|
||||||
|
assert_eq!(Val::Vw(100.).resolve(size, viewport_size).unwrap(), 1000.);
|
||||||
|
assert_eq!(Val::Vh(100.).resolve(size, viewport_size).unwrap(), 500.);
|
||||||
|
assert_eq!(Val::Vw(60.).resolve(size, viewport_size).unwrap(), 600.);
|
||||||
|
assert_eq!(Val::Vh(40.).resolve(size, viewport_size).unwrap(), 200.);
|
||||||
|
assert_eq!(Val::VMin(50.).resolve(size, viewport_size).unwrap(), 250.);
|
||||||
|
assert_eq!(Val::VMax(75.).resolve(size, viewport_size).unwrap(), 750.);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn val_auto_is_non_resolveable() {
|
||||||
|
let size = 250.;
|
||||||
|
let viewport_size = vec2(1000., 500.);
|
||||||
|
let resolve_auto = Val::Auto.resolve(size, viewport_size);
|
||||||
|
|
||||||
|
assert_eq!(resolve_auto, Err(ValArithmeticError::NonEvaluateable));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue