diff --git a/emigui/src/example_app.rs b/emigui/src/example_app.rs index 16ee228c..0f6ee661 100644 --- a/emigui/src/example_app.rs +++ b/emigui/src/example_app.rs @@ -113,7 +113,7 @@ impl ExampleApp { corner_radius: self.corner_radius, fill_color: Some(gray(136, 255)), rect: Rect::from_min_size( - vec2(pos.x + (i as f32) * (self.size.x * 1.1), pos.y), + pos2(pos.x + (i as f32) * (self.size.x * 1.1), pos.y), self.size, ), outline: Some(Outline { diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index ed62b00b..052019bc 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -81,7 +81,7 @@ impl Default for Align { // ---------------------------------------------------------------------------- /// Show a pop-over window -pub fn show_popup(ctx: &Arc, window_pos: Vec2, add_contents: F) +pub fn show_popup(ctx: &Arc, window_pos: Pos2, add_contents: F) where F: FnOnce(&mut Region), { diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 29704441..b154bd30 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -120,25 +120,106 @@ pub fn vec2(x: f32, y: f32) -> Vec2 { // ---------------------------------------------------------------------------- +/// Sometimes called a Point. I prefer the shorter Pos2 so it is equal length to Vec2 +#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)] +pub struct Pos2 { + pub x: f32, + pub y: f32, + // implicit w = 1 +} + +impl Pos2 { + pub fn dist(a: Pos2, b: Pos2) -> f32 { + (a - b).length() + } + + pub fn dist_sq(a: Pos2, b: Pos2) -> f32 { + (a - b).length_sq() + } + + // TODO: remove? + pub fn to_vec2(self) -> Vec2 { + Vec2 { + x: self.x, + y: self.y, + } + } +} + +impl std::ops::AddAssign for Pos2 { + fn add_assign(&mut self, rhs: Vec2) { + *self = Pos2 { + x: self.x + rhs.x, + y: self.y + rhs.y, + }; + } +} + +impl std::ops::Add for Pos2 { + type Output = Pos2; + fn add(self, rhs: Vec2) -> Pos2 { + Pos2 { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} + +// impl std::ops::Add for Vec2 { +// type Output = Pos2; +// fn add(self, rhs: Pos2) -> Pos2 { +// Pos2 { +// x: self.x + rhs.x, +// y: self.y + rhs.y, +// } +// } +// } + +impl std::ops::Sub for Pos2 { + type Output = Vec2; + fn sub(self, rhs: Pos2) -> Vec2 { + Vec2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} + +impl std::ops::Sub for Pos2 { + type Output = Pos2; + fn sub(self, rhs: Vec2) -> Pos2 { + Pos2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} + +pub fn pos2(x: f32, y: f32) -> Pos2 { + Pos2 { x, y } +} + +// ---------------------------------------------------------------------------- + #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)] pub struct Rect { - min: Vec2, - max: Vec2, + min: Pos2, + max: Pos2, } impl Rect { - pub fn from_min_max(min: Vec2, max: Vec2) -> Self { + pub fn from_min_max(min: Pos2, max: Pos2) -> Self { Rect { min, max: max } } - pub fn from_min_size(min: Vec2, size: Vec2) -> Self { + pub fn from_min_size(min: Pos2, size: Vec2) -> Self { Rect { min, max: min + size, } } - pub fn from_center_size(center: Vec2, size: Vec2) -> Self { + pub fn from_center_size(center: Pos2, size: Vec2) -> Self { Rect { min: center - size * 0.5, max: center + size * 0.5, @@ -153,23 +234,23 @@ impl Rect { Rect::from_min_size(self.min() + amnt, self.size()) } - pub fn contains(&self, p: Vec2) -> bool { + pub fn contains(&self, p: Pos2) -> bool { 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 { + pub fn center(&self) -> Pos2 { + Pos2 { x: self.min.x + self.size().x / 2.0, y: self.min.y + self.size().y / 2.0, } } - pub fn min(&self) -> Vec2 { + pub fn min(&self) -> Pos2 { self.min } - pub fn max(&self) -> Vec2 { + pub fn max(&self) -> Pos2 { self.max } pub fn size(&self) -> Vec2 { @@ -184,29 +265,29 @@ impl Rect { // Convenience functions (assumes origin is towards left top): - pub fn left_top(&self) -> Vec2 { - vec2(self.min().x, self.min().y) + pub fn left_top(&self) -> Pos2 { + pos2(self.min().x, self.min().y) } - pub fn center_top(&self) -> Vec2 { - vec2(self.center().x, self.min().y) + pub fn center_top(&self) -> Pos2 { + pos2(self.center().x, self.min().y) } - pub fn right_top(&self) -> Vec2 { - vec2(self.max().x, self.min().y) + pub fn right_top(&self) -> Pos2 { + pos2(self.max().x, self.min().y) } - pub fn left_center(&self) -> Vec2 { - vec2(self.min().x, self.center().y) + pub fn left_center(&self) -> Pos2 { + pos2(self.min().x, self.center().y) } - pub fn right_center(&self) -> Vec2 { - vec2(self.max().x, self.center().y) + pub fn right_center(&self) -> Pos2 { + pos2(self.max().x, self.center().y) } - pub fn left_bottom(&self) -> Vec2 { - vec2(self.min().x, self.max().y) + pub fn left_bottom(&self) -> Pos2 { + pos2(self.min().x, self.max().y) } - pub fn center_bottom(&self) -> Vec2 { - vec2(self.center().x, self.max().y) + pub fn center_bottom(&self) -> Pos2 { + pos2(self.center().x, self.max().y) } - pub fn right_bottom(&self) -> Vec2 { - vec2(self.max().x, self.max().y) + pub fn right_bottom(&self) -> Pos2 { + pos2(self.max().x, self.max().y) } } diff --git a/emigui/src/memory.rs b/emigui/src/memory.rs index 27cf4f33..bdeb9e4c 100644 --- a/emigui/src/memory.rs +++ b/emigui/src/memory.rs @@ -33,7 +33,7 @@ impl Memory { self.windows.insert(id, state); } - pub fn layer_at(&self, pos: Vec2) -> Layer { + pub fn layer_at(&self, pos: Pos2) -> Layer { for window_id in self.window_order.iter().rev() { if let Some(state) = self.windows.get(window_id) { if state.rect.contains(pos) { diff --git a/emigui/src/mesher.rs b/emigui/src/mesher.rs index bf043a12..d53cf36b 100644 --- a/emigui/src/mesher.rs +++ b/emigui/src/mesher.rs @@ -1,19 +1,14 @@ #![allow(clippy::identity_op)] /// Outputs render info in a format suitable for e.g. OpenGL. -use crate::{ - color::Color, - fonts::Fonts, - math::{remap, vec2, Rect, Vec2, TAU}, - types::PaintCmd, -}; +use crate::{color::Color, fonts::Fonts, math::*, types::PaintCmd}; const WHITE_UV: (u16, u16) = (1, 1); #[derive(Clone, Copy, Debug, Default, Serialize)] pub struct Vertex { /// Pixel coordinates - pub pos: Vec2, + pub pos: Pos2, /// Texel indices into the texture pub uv: (u16, u16), /// sRGBA @@ -53,12 +48,12 @@ impl Mesh { self.triangle(idx + 2, idx + 1, idx + 3); let top_right = Vertex { - pos: vec2(bottom_right.pos.x, top_left.pos.y), + pos: pos2(bottom_right.pos.x, top_left.pos.y), uv: (bottom_right.uv.0, top_left.uv.1), color: top_left.color, }; let botom_left = Vertex { - pos: vec2(top_left.pos.x, bottom_right.pos.y), + pos: pos2(top_left.pos.x, bottom_right.pos.y), uv: (top_left.uv.0, bottom_right.uv.1), color: top_left.color, }; @@ -124,7 +119,7 @@ impl Mesh { // ---------------------------------------------------------------------------- pub struct PathPoint { - pos: Vec2, + pos: Pos2, /// For filled paths the normal is used for antialiasing. /// For outlines the normal is used for figuring out how to make the line wide @@ -142,11 +137,11 @@ impl Path { self.0.clear(); } - pub fn add_point(&mut self, pos: Vec2, normal: Vec2) { + pub fn add_point(&mut self, pos: Pos2, normal: Vec2) { self.0.push(PathPoint { pos, normal }); } - pub fn add_circle(&mut self, center: Vec2, radius: f32) { + pub fn add_circle(&mut self, center: Pos2, radius: f32) { let n = 32; // TODO: parameter for i in 0..n { let angle = remap(i as f32, 0.0, n as f32, 0.0, TAU); @@ -155,7 +150,7 @@ impl Path { } } - pub fn add_line(&mut self, points: &[Vec2]) { + pub fn add_line(&mut self, points: &[Pos2]) { let n = points.len(); assert!(n >= 2); @@ -176,10 +171,10 @@ impl Path { pub fn add_rectangle(&mut self, rect: &Rect) { let min = rect.min(); let max = rect.max(); - self.add_point(vec2(min.x, min.y), vec2(-1.0, -1.0)); - self.add_point(vec2(max.x, min.y), vec2(1.0, -1.0)); - self.add_point(vec2(max.x, max.y), vec2(1.0, 1.0)); - self.add_point(vec2(min.x, max.y), vec2(-1.0, 1.0)); + self.add_point(pos2(min.x, min.y), vec2(-1.0, -1.0)); + self.add_point(pos2(max.x, min.y), vec2(1.0, -1.0)); + self.add_point(pos2(max.x, max.y), vec2(1.0, 1.0)); + self.add_point(pos2(min.x, max.y), vec2(-1.0, 1.0)); } pub fn add_rounded_rectangle(&mut self, rect: &Rect, corner_radius: f32) { @@ -193,14 +188,14 @@ impl Path { if cr <= 0.0 { self.add_rectangle(rect); } else { - self.add_circle_quadrant(vec2(max.x - cr, max.y - cr), cr, 0.0); - self.add_circle_quadrant(vec2(min.x + cr, max.y - cr), cr, 1.0); - self.add_circle_quadrant(vec2(min.x + cr, min.y + cr), cr, 2.0); - self.add_circle_quadrant(vec2(max.x - cr, min.y + cr), cr, 3.0); + self.add_circle_quadrant(pos2(max.x - cr, max.y - cr), cr, 0.0); + self.add_circle_quadrant(pos2(min.x + cr, max.y - cr), cr, 1.0); + self.add_circle_quadrant(pos2(min.x + cr, min.y + cr), cr, 2.0); + self.add_circle_quadrant(pos2(max.x - cr, min.y + cr), cr, 3.0); } } - pub fn add_circle_quadrant(&mut self, center: Vec2, radius: f32, quadrant: f32) { + pub fn add_circle_quadrant(&mut self, center: Pos2, radius: f32, quadrant: f32) { let n = 8; const RIGHT_ANGLE: f32 = TAU / 4.0; for i in 0..=n { diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 6fdf2869..53503cfb 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -23,7 +23,7 @@ pub struct Region { /// Where the next widget will be put. /// Progresses along self.dir - pub(crate) cursor: Vec2, + pub(crate) cursor: Pos2, /// Bounding box of children. /// We keep track of our max-size along the orthogonal to self.dir @@ -79,7 +79,7 @@ impl Region { self.dir } - pub fn cursor(&self) -> Vec2 { + pub fn cursor(&self) -> Pos2 { self.cursor } @@ -142,8 +142,8 @@ impl Region { // Draw a minus: self.add_paint_cmd(PaintCmd::Line { points: vec![ - vec2(small_icon_rect.min().x, small_icon_rect.center().y), - vec2(small_icon_rect.max().x, small_icon_rect.center().y), + pos2(small_icon_rect.min().x, small_icon_rect.center().y), + pos2(small_icon_rect.max().x, small_icon_rect.center().y), ], color: stroke_color, width: self.style.line_width, @@ -152,8 +152,8 @@ impl Region { // Draw it as a plus: self.add_paint_cmd(PaintCmd::Line { points: vec![ - vec2(small_icon_rect.center().x, small_icon_rect.min().y), - vec2(small_icon_rect.center().x, small_icon_rect.max().y), + pos2(small_icon_rect.center().x, small_icon_rect.min().y), + pos2(small_icon_rect.center().x, small_icon_rect.max().y), ], color: stroke_color, width: self.style.line_width, @@ -207,7 +207,7 @@ impl Region { style: self.style, id: self.id, dir: self.dir, - cursor: self.cursor + rect.min(), + cursor: self.cursor + rect.min().to_vec2(), align: self.align, bounding_size: vec2(0.0, 0.0), available_space: rect.size(), @@ -222,7 +222,7 @@ impl Region { Align::Max => self.available_space.x - width, }; self.relative_region(Rect::from_min_size( - vec2(x, 0.0), + pos2(x, 0.0), vec2(width, self.available_space.y), )) } @@ -332,7 +332,7 @@ impl Region { /// Reserve this much space and move the cursor. /// Returns where to put the widget. - pub fn reserve_space_without_padding(&mut self, size: Vec2) -> Vec2 { + pub fn reserve_space_without_padding(&mut self, size: Vec2) -> Pos2 { let mut pos = self.cursor; if self.dir == Direction::Horizontal { pos.y += match self.align { @@ -369,7 +369,7 @@ impl Region { // Helper function pub fn floating_text( &mut self, - pos: Vec2, + pos: Pos2, text: &str, text_style: TextStyle, align: (Align, Align), @@ -388,13 +388,13 @@ impl Region { Align::Center => pos.y - 0.5 * text_size.y, Align::Max => pos.y - text_size.y, }; - self.add_text(vec2(x, y), text_style, text, text_color); + self.add_text(pos2(x, y), text_style, text, text_color); text_size } pub fn add_text( &mut self, - pos: Vec2, + pos: Pos2, text_style: TextStyle, text: Vec, color: Option, diff --git a/emigui/src/style.rs b/emigui/src/style.rs index db8d94a9..d99949a2 100644 --- a/emigui/src/style.rs +++ b/emigui/src/style.rs @@ -77,7 +77,7 @@ impl Style { pub fn icon_rectangles(&self, rect: &Rect) -> (Rect, Rect) { let box_side = 16.0; let big_icon_rect = Rect::from_center_size( - vec2(rect.min().x + 4.0 + box_side * 0.5, rect.center().y), + pos2(rect.min().x + 4.0 + box_side * 0.5, rect.center().y), vec2(box_side, box_side), ); diff --git a/emigui/src/types.rs b/emigui/src/types.rs index 1b524f36..9d458212 100644 --- a/emigui/src/types.rs +++ b/emigui/src/types.rs @@ -1,7 +1,7 @@ use crate::{ color::Color, fonts::TextStyle, - math::{Rect, Vec2}, + math::{Pos2, Rect, Vec2}, mesher::Mesh, }; @@ -14,7 +14,7 @@ pub struct RawInput { pub mouse_down: bool, /// Current position of the mouse in points. - pub mouse_pos: Option, + pub mouse_pos: Option, /// Size of the screen in points. pub screen_size: Vec2, @@ -36,7 +36,7 @@ pub struct GuiInput { pub mouse_released: bool, /// Current position of the mouse in points. - pub mouse_pos: Option, + pub mouse_pos: Option, /// How much the mouse moved compared to last frame, in points. pub mouse_move: Vec2, @@ -95,13 +95,13 @@ pub struct Outline { #[serde(rename_all = "snake_case", tag = "kind")] pub enum PaintCmd { Circle { - center: Vec2, + center: Pos2, fill_color: Option, outline: Option, radius: f32, }, Line { - points: Vec, + points: Vec, color: Color, width: f32, }, @@ -115,7 +115,7 @@ pub enum PaintCmd { Text { color: Color, /// Top left corner of the first character. - pos: Vec2, + pos: Pos2, text: String, text_style: TextStyle, /// Start each character in the text, as offset from pos. diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index 7b0e3140..8e7927c9 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -156,9 +156,9 @@ impl<'a> Widget for Checkbox<'a> { if *self.checked { region.add_paint_cmd(PaintCmd::Line { points: vec![ - vec2(small_icon_rect.min().x, small_icon_rect.center().y), - vec2(small_icon_rect.center().x, small_icon_rect.max().y), - vec2(small_icon_rect.max().x, small_icon_rect.min().y), + pos2(small_icon_rect.min().x, small_icon_rect.center().y), + pos2(small_icon_rect.center().x, small_icon_rect.max().y), + pos2(small_icon_rect.max().x, small_icon_rect.min().y), ], color: stroke_color, width: region.style().line_width, @@ -424,7 +424,7 @@ impl<'a> Widget for Slider<'a> { }); region.add_paint_cmd(PaintCmd::Circle { - center: vec2(marker_center_x, thin_rect.center().y), + center: pos2(marker_center_x, thin_rect.center().y), fill_color: Some(region.style().interact_fill_color(&interact)), outline: Some(Outline { color: region.style().interact_stroke_color(&interact), @@ -473,8 +473,8 @@ impl Widget for Separator { let interact = region.reserve_space(vec2(self.width, available_space.y), None); ( vec![ - vec2(interact.rect.center().x, interact.rect.min().y), - vec2(interact.rect.center().x, interact.rect.max().y), + pos2(interact.rect.center().x, interact.rect.min().y), + pos2(interact.rect.center().x, interact.rect.max().y), ], interact, ) @@ -483,8 +483,8 @@ impl Widget for Separator { let interact = region.reserve_space(vec2(available_space.x, self.width), None); ( vec![ - vec2(interact.rect.min().x, interact.rect.center().y), - vec2(interact.rect.max().x, interact.rect.center().y), + pos2(interact.rect.min().x, interact.rect.center().y), + pos2(interact.rect.max().x, interact.rect.center().y), ], interact, ) diff --git a/emigui/src/window.rs b/emigui/src/window.rs index e22caae0..04d8182b 100644 --- a/emigui/src/window.rs +++ b/emigui/src/window.rs @@ -29,7 +29,7 @@ impl Window { let mut state = ctx.memory.lock().get_or_create_window( id, Rect::from_min_size( - vec2(400.0, 200.0), // TODO + pos2(400.0, 200.0), // TODO vec2(200.0, 200.0), // TODO ), ); diff --git a/example_glium/src/main.rs b/example_glium/src/main.rs index 47127611..2bf3c0a1 100644 --- a/example_glium/src/main.rs +++ b/example_glium/src/main.rs @@ -6,7 +6,7 @@ use { emigui::{ example_app::ExampleApp, label, - math::vec2, + math::*, widgets::{Button, Label}, Align, Emigui, Window, }, @@ -63,7 +63,7 @@ fn main() { raw_input.mouse_down = state == glutin::ElementState::Pressed; } glutin::WindowEvent::CursorMoved { position, .. } => { - raw_input.mouse_pos = Some(vec2(position.x as f32, position.y as f32)); + raw_input.mouse_pos = Some(pos2(position.x as f32, position.y as f32)); } glutin::WindowEvent::KeyboardInput { input, .. } => { if input.virtual_keycode == Some(glutin::VirtualKeyCode::Q)