bevyengine / bevyengine/bevy

Implement math operations for Val2 and Val

Open
#23,926 2 comments 0 reactions 0 assignees View on GitHub
A-UI C-Feature C-Usability S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?
Working with movable UI can be tedious because you need to de-structure all the UiTransform up-to the single f32 inside the Val variant to apply a delta.

When working with entities, Vec3 implements all the basic operations with itself and f32, but for UI, Val2 implements nothing.

Of course Val2 is different because Val is a enum with different variants but maybe we can find a middle ground.

## What solution would you like?

Implements math operations for Val2. My proposal is to implements against Vec2, where each member would operate in the internal f32 of the Val variant, not changing the variant.

``` rust
let mut ui_transform_translate = Val2 { x: Val::Px(10.), y: Val::Px(5.)};
let drag_delta = Vec2::new(15., 7.);
ui_transform_translate += drag_delta;

assert_eq!(ui_transform_translate.x , Val::Px(25.));
assert_eq!(ui_transform_translate.y , Val::Px(12.));

```
Implementing for f32 could be useful too
## What alternative(s) have you considered?

De-structing the UiTransform to apply a delta to each member:

``` rust
pub fn drag_node(
drag: On>,
mut transform: Query<&mut UiTransform>,
parent: Query<&ChildOf>,
) {
let Ok(parent) = parent.get(drag.entity) else {
return;
};
let Ok(mut transform) = transform.get_mut(parent.0) else {
return;
};

if let Val::Px(ref mut x) = transform.translation.x {
*x += drag.delta.x;
}

if let Val::Px(ref mut y) = transform.translation.y {
*y += drag.delta.y;
}
}
```

## Additional context

---

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.