Refactor: define rect by min/max

This commit is contained in:
Emil Ernerfeldt 2020-04-15 11:47:03 +02:00
parent 7fc2500973
commit 0265b3d43a
3 changed files with 31 additions and 24 deletions

View file

@ -122,19 +122,26 @@ pub fn vec2(x: f32, y: f32) -> Vec2 {
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
pub struct Rect {
pub pos: Vec2,
pub size: Vec2,
min: Vec2,
max: Vec2,
}
impl Rect {
pub fn from_min_max(min: Vec2, max: Vec2) -> Self {
Rect { min, max: max }
}
pub fn from_min_size(min: Vec2, size: Vec2) -> Self {
Rect { pos: min, size }
Rect {
min,
max: min + size,
}
}
pub fn from_center_size(center: Vec2, size: Vec2) -> Self {
Rect {
pos: center - size * 0.5,
size,
min: center - size * 0.5,
max: center + size * 0.5,
}
}
@ -144,38 +151,36 @@ impl Rect {
}
pub fn contains(&self, p: Vec2) -> bool {
self.pos.x <= p.x
&& p.x <= self.pos.x + self.size.x
&& self.pos.y <= p.y
&& p.y <= self.pos.y + self.size.y
self.min.x <= p.x
&& p.x <= self.min.x + self.size().x
&& self.min.y <= p.y
&& p.y <= self.min.y + self.size().y
}
pub fn center(&self) -> Vec2 {
Vec2 {
x: self.pos.x + self.size.x / 2.0,
y: self.pos.y + self.size.y / 2.0,
x: self.min.x + self.size().x / 2.0,
y: self.min.y + self.size().y / 2.0,
}
}
pub fn min(&self) -> Vec2 {
self.pos
self.min
}
pub fn max(&self) -> Vec2 {
self.pos + self.size
self.max
}
pub fn size(&self) -> Vec2 {
self.size
self.max - self.min
}
pub fn width(&self) -> f32 {
self.size.x
self.max.x - self.min.x
}
pub fn height(&self) -> f32 {
self.size.y
self.max.y - self.min.y
}
// Convenience functions:
// Convenience functions (assumes origin is towards left top):
pub fn left_top(&self) -> Vec2 {
vec2(self.min().x, self.min().y)
}

View file

@ -347,7 +347,9 @@ impl Mesher {
let min = rect.min();
let max = rect.max();
let cr = corner_radius.min(rect.size.x * 0.5).min(rect.size.y * 0.5);
let cr = corner_radius
.min(rect.width() * 0.5)
.min(rect.height() * 0.5);
if cr <= 0.0 {
path_points.push(vec2(min.x, min.y));

View file

@ -227,7 +227,7 @@ impl Widget for RadioButton {
center: big_icon_rect.center(),
fill_color: Some(fill_color),
outline: None,
radius: big_icon_rect.size.x / 2.0,
radius: big_icon_rect.size().x / 2.0,
});
if self.checked {
@ -235,7 +235,7 @@ impl Widget for RadioButton {
center: small_icon_rect.center(),
fill_color: Some(stroke_color),
outline: None,
radius: small_icon_rect.size.x / 2.0,
radius: small_icon_rect.size().x / 2.0,
});
}
@ -409,7 +409,7 @@ impl<'a> Widget for Slider<'a> {
let rect = interact.rect;
let thickness = rect.size().y;
let thin_size = vec2(rect.size.x, thickness / 5.0);
let thin_size = vec2(rect.size().x, thickness / 5.0);
let thin_rect = Rect::from_center_size(rect.center(), thin_size);
let marker_center_x = remap_clamp(value, min, max, rect.min().x, rect.max().x);