Use new type Pos2 for positions (points) instead of Vec2

This commit is contained in:
Emil Ernerfeldt 2020-04-19 01:05:49 +02:00
parent 6eb1053c35
commit 2170081221
11 changed files with 157 additions and 81 deletions

View file

@ -113,7 +113,7 @@ impl ExampleApp {
corner_radius: self.corner_radius, corner_radius: self.corner_radius,
fill_color: Some(gray(136, 255)), fill_color: Some(gray(136, 255)),
rect: Rect::from_min_size( 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, self.size,
), ),
outline: Some(Outline { outline: Some(Outline {

View file

@ -81,7 +81,7 @@ impl Default for Align {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/// Show a pop-over window /// Show a pop-over window
pub fn show_popup<F>(ctx: &Arc<Context>, window_pos: Vec2, add_contents: F) pub fn show_popup<F>(ctx: &Arc<Context>, window_pos: Pos2, add_contents: F)
where where
F: FnOnce(&mut Region), F: FnOnce(&mut Region),
{ {

View file

@ -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<Vec2> 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<Vec2> 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<Pos2> 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<Vec2> 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)] #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
pub struct Rect { pub struct Rect {
min: Vec2, min: Pos2,
max: Vec2, max: Pos2,
} }
impl Rect { 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 } 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 { Rect {
min, min,
max: min + size, max: min + size,
} }
} }
pub fn from_center_size(center: Vec2, size: Vec2) -> Self { pub fn from_center_size(center: Pos2, size: Vec2) -> Self {
Rect { Rect {
min: center - size * 0.5, min: center - size * 0.5,
max: center + size * 0.5, max: center + size * 0.5,
@ -153,23 +234,23 @@ impl Rect {
Rect::from_min_size(self.min() + amnt, self.size()) 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 self.min.x <= p.x
&& p.x <= self.min.x + self.size().x && p.x <= self.min.x + self.size().x
&& self.min.y <= p.y && self.min.y <= p.y
&& p.y <= self.min.y + self.size().y && p.y <= self.min.y + self.size().y
} }
pub fn center(&self) -> Vec2 { pub fn center(&self) -> Pos2 {
Vec2 { Pos2 {
x: self.min.x + self.size().x / 2.0, x: self.min.x + self.size().x / 2.0,
y: self.min.y + self.size().y / 2.0, y: self.min.y + self.size().y / 2.0,
} }
} }
pub fn min(&self) -> Vec2 { pub fn min(&self) -> Pos2 {
self.min self.min
} }
pub fn max(&self) -> Vec2 { pub fn max(&self) -> Pos2 {
self.max self.max
} }
pub fn size(&self) -> Vec2 { pub fn size(&self) -> Vec2 {
@ -184,29 +265,29 @@ impl Rect {
// Convenience functions (assumes origin is towards left top): // Convenience functions (assumes origin is towards left top):
pub fn left_top(&self) -> Vec2 { pub fn left_top(&self) -> Pos2 {
vec2(self.min().x, self.min().y) pos2(self.min().x, self.min().y)
} }
pub fn center_top(&self) -> Vec2 { pub fn center_top(&self) -> Pos2 {
vec2(self.center().x, self.min().y) pos2(self.center().x, self.min().y)
} }
pub fn right_top(&self) -> Vec2 { pub fn right_top(&self) -> Pos2 {
vec2(self.max().x, self.min().y) pos2(self.max().x, self.min().y)
} }
pub fn left_center(&self) -> Vec2 { pub fn left_center(&self) -> Pos2 {
vec2(self.min().x, self.center().y) pos2(self.min().x, self.center().y)
} }
pub fn right_center(&self) -> Vec2 { pub fn right_center(&self) -> Pos2 {
vec2(self.max().x, self.center().y) pos2(self.max().x, self.center().y)
} }
pub fn left_bottom(&self) -> Vec2 { pub fn left_bottom(&self) -> Pos2 {
vec2(self.min().x, self.max().y) pos2(self.min().x, self.max().y)
} }
pub fn center_bottom(&self) -> Vec2 { pub fn center_bottom(&self) -> Pos2 {
vec2(self.center().x, self.max().y) pos2(self.center().x, self.max().y)
} }
pub fn right_bottom(&self) -> Vec2 { pub fn right_bottom(&self) -> Pos2 {
vec2(self.max().x, self.max().y) pos2(self.max().x, self.max().y)
} }
} }

View file

@ -33,7 +33,7 @@ impl Memory {
self.windows.insert(id, state); 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() { for window_id in self.window_order.iter().rev() {
if let Some(state) = self.windows.get(window_id) { if let Some(state) = self.windows.get(window_id) {
if state.rect.contains(pos) { if state.rect.contains(pos) {

View file

@ -1,19 +1,14 @@
#![allow(clippy::identity_op)] #![allow(clippy::identity_op)]
/// Outputs render info in a format suitable for e.g. OpenGL. /// Outputs render info in a format suitable for e.g. OpenGL.
use crate::{ use crate::{color::Color, fonts::Fonts, math::*, types::PaintCmd};
color::Color,
fonts::Fonts,
math::{remap, vec2, Rect, Vec2, TAU},
types::PaintCmd,
};
const WHITE_UV: (u16, u16) = (1, 1); const WHITE_UV: (u16, u16) = (1, 1);
#[derive(Clone, Copy, Debug, Default, Serialize)] #[derive(Clone, Copy, Debug, Default, Serialize)]
pub struct Vertex { pub struct Vertex {
/// Pixel coordinates /// Pixel coordinates
pub pos: Vec2, pub pos: Pos2,
/// Texel indices into the texture /// Texel indices into the texture
pub uv: (u16, u16), pub uv: (u16, u16),
/// sRGBA /// sRGBA
@ -53,12 +48,12 @@ impl Mesh {
self.triangle(idx + 2, idx + 1, idx + 3); self.triangle(idx + 2, idx + 1, idx + 3);
let top_right = Vertex { 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), uv: (bottom_right.uv.0, top_left.uv.1),
color: top_left.color, color: top_left.color,
}; };
let botom_left = Vertex { 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), uv: (top_left.uv.0, bottom_right.uv.1),
color: top_left.color, color: top_left.color,
}; };
@ -124,7 +119,7 @@ impl Mesh {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
pub struct PathPoint { pub struct PathPoint {
pos: Vec2, pos: Pos2,
/// For filled paths the normal is used for antialiasing. /// For filled paths the normal is used for antialiasing.
/// For outlines the normal is used for figuring out how to make the line wide /// For outlines the normal is used for figuring out how to make the line wide
@ -142,11 +137,11 @@ impl Path {
self.0.clear(); 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 }); 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 let n = 32; // TODO: parameter
for i in 0..n { for i in 0..n {
let angle = remap(i as f32, 0.0, n as f32, 0.0, TAU); 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(); let n = points.len();
assert!(n >= 2); assert!(n >= 2);
@ -176,10 +171,10 @@ impl Path {
pub fn add_rectangle(&mut self, rect: &Rect) { pub fn add_rectangle(&mut self, rect: &Rect) {
let min = rect.min(); let min = rect.min();
let max = rect.max(); let max = rect.max();
self.add_point(vec2(min.x, min.y), vec2(-1.0, -1.0)); self.add_point(pos2(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(pos2(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(pos2(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, max.y), vec2(-1.0, 1.0));
} }
pub fn add_rounded_rectangle(&mut self, rect: &Rect, corner_radius: f32) { pub fn add_rounded_rectangle(&mut self, rect: &Rect, corner_radius: f32) {
@ -193,14 +188,14 @@ impl Path {
if cr <= 0.0 { if cr <= 0.0 {
self.add_rectangle(rect); self.add_rectangle(rect);
} else { } else {
self.add_circle_quadrant(vec2(max.x - cr, max.y - cr), cr, 0.0); self.add_circle_quadrant(pos2(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(pos2(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(pos2(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, 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; let n = 8;
const RIGHT_ANGLE: f32 = TAU / 4.0; const RIGHT_ANGLE: f32 = TAU / 4.0;
for i in 0..=n { for i in 0..=n {

View file

@ -23,7 +23,7 @@ pub struct Region {
/// Where the next widget will be put. /// Where the next widget will be put.
/// Progresses along self.dir /// Progresses along self.dir
pub(crate) cursor: Vec2, pub(crate) cursor: Pos2,
/// Bounding box of children. /// Bounding box of children.
/// We keep track of our max-size along the orthogonal to self.dir /// We keep track of our max-size along the orthogonal to self.dir
@ -79,7 +79,7 @@ impl Region {
self.dir self.dir
} }
pub fn cursor(&self) -> Vec2 { pub fn cursor(&self) -> Pos2 {
self.cursor self.cursor
} }
@ -142,8 +142,8 @@ impl Region {
// Draw a minus: // Draw a minus:
self.add_paint_cmd(PaintCmd::Line { self.add_paint_cmd(PaintCmd::Line {
points: vec![ points: vec![
vec2(small_icon_rect.min().x, small_icon_rect.center().y), pos2(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.max().x, small_icon_rect.center().y),
], ],
color: stroke_color, color: stroke_color,
width: self.style.line_width, width: self.style.line_width,
@ -152,8 +152,8 @@ impl Region {
// Draw it as a plus: // Draw it as a plus:
self.add_paint_cmd(PaintCmd::Line { self.add_paint_cmd(PaintCmd::Line {
points: vec![ points: vec![
vec2(small_icon_rect.center().x, small_icon_rect.min().y), pos2(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.max().y),
], ],
color: stroke_color, color: stroke_color,
width: self.style.line_width, width: self.style.line_width,
@ -207,7 +207,7 @@ impl Region {
style: self.style, style: self.style,
id: self.id, id: self.id,
dir: self.dir, dir: self.dir,
cursor: self.cursor + rect.min(), cursor: self.cursor + rect.min().to_vec2(),
align: self.align, align: self.align,
bounding_size: vec2(0.0, 0.0), bounding_size: vec2(0.0, 0.0),
available_space: rect.size(), available_space: rect.size(),
@ -222,7 +222,7 @@ impl Region {
Align::Max => self.available_space.x - width, Align::Max => self.available_space.x - width,
}; };
self.relative_region(Rect::from_min_size( self.relative_region(Rect::from_min_size(
vec2(x, 0.0), pos2(x, 0.0),
vec2(width, self.available_space.y), vec2(width, self.available_space.y),
)) ))
} }
@ -332,7 +332,7 @@ impl Region {
/// Reserve this much space and move the cursor. /// Reserve this much space and move the cursor.
/// Returns where to put the widget. /// 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; let mut pos = self.cursor;
if self.dir == Direction::Horizontal { if self.dir == Direction::Horizontal {
pos.y += match self.align { pos.y += match self.align {
@ -369,7 +369,7 @@ impl Region {
// Helper function // Helper function
pub fn floating_text( pub fn floating_text(
&mut self, &mut self,
pos: Vec2, pos: Pos2,
text: &str, text: &str,
text_style: TextStyle, text_style: TextStyle,
align: (Align, Align), align: (Align, Align),
@ -388,13 +388,13 @@ impl Region {
Align::Center => pos.y - 0.5 * text_size.y, Align::Center => pos.y - 0.5 * text_size.y,
Align::Max => pos.y - 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 text_size
} }
pub fn add_text( pub fn add_text(
&mut self, &mut self,
pos: Vec2, pos: Pos2,
text_style: TextStyle, text_style: TextStyle,
text: Vec<TextFragment>, text: Vec<TextFragment>,
color: Option<Color>, color: Option<Color>,

View file

@ -77,7 +77,7 @@ impl Style {
pub fn icon_rectangles(&self, rect: &Rect) -> (Rect, Rect) { pub fn icon_rectangles(&self, rect: &Rect) -> (Rect, Rect) {
let box_side = 16.0; let box_side = 16.0;
let big_icon_rect = Rect::from_center_size( 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), vec2(box_side, box_side),
); );

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
color::Color, color::Color,
fonts::TextStyle, fonts::TextStyle,
math::{Rect, Vec2}, math::{Pos2, Rect, Vec2},
mesher::Mesh, mesher::Mesh,
}; };
@ -14,7 +14,7 @@ pub struct RawInput {
pub mouse_down: bool, pub mouse_down: bool,
/// Current position of the mouse in points. /// Current position of the mouse in points.
pub mouse_pos: Option<Vec2>, pub mouse_pos: Option<Pos2>,
/// Size of the screen in points. /// Size of the screen in points.
pub screen_size: Vec2, pub screen_size: Vec2,
@ -36,7 +36,7 @@ pub struct GuiInput {
pub mouse_released: bool, pub mouse_released: bool,
/// Current position of the mouse in points. /// Current position of the mouse in points.
pub mouse_pos: Option<Vec2>, pub mouse_pos: Option<Pos2>,
/// How much the mouse moved compared to last frame, in points. /// How much the mouse moved compared to last frame, in points.
pub mouse_move: Vec2, pub mouse_move: Vec2,
@ -95,13 +95,13 @@ pub struct Outline {
#[serde(rename_all = "snake_case", tag = "kind")] #[serde(rename_all = "snake_case", tag = "kind")]
pub enum PaintCmd { pub enum PaintCmd {
Circle { Circle {
center: Vec2, center: Pos2,
fill_color: Option<Color>, fill_color: Option<Color>,
outline: Option<Outline>, outline: Option<Outline>,
radius: f32, radius: f32,
}, },
Line { Line {
points: Vec<Vec2>, points: Vec<Pos2>,
color: Color, color: Color,
width: f32, width: f32,
}, },
@ -115,7 +115,7 @@ pub enum PaintCmd {
Text { Text {
color: Color, color: Color,
/// Top left corner of the first character. /// Top left corner of the first character.
pos: Vec2, pos: Pos2,
text: String, text: String,
text_style: TextStyle, text_style: TextStyle,
/// Start each character in the text, as offset from pos. /// Start each character in the text, as offset from pos.

View file

@ -156,9 +156,9 @@ impl<'a> Widget for Checkbox<'a> {
if *self.checked { if *self.checked {
region.add_paint_cmd(PaintCmd::Line { region.add_paint_cmd(PaintCmd::Line {
points: vec![ points: vec![
vec2(small_icon_rect.min().x, small_icon_rect.center().y), pos2(small_icon_rect.min().x, small_icon_rect.center().y),
vec2(small_icon_rect.center().x, small_icon_rect.max().y), pos2(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.max().x, small_icon_rect.min().y),
], ],
color: stroke_color, color: stroke_color,
width: region.style().line_width, width: region.style().line_width,
@ -424,7 +424,7 @@ impl<'a> Widget for Slider<'a> {
}); });
region.add_paint_cmd(PaintCmd::Circle { 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)), fill_color: Some(region.style().interact_fill_color(&interact)),
outline: Some(Outline { outline: Some(Outline {
color: region.style().interact_stroke_color(&interact), 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); let interact = region.reserve_space(vec2(self.width, available_space.y), None);
( (
vec![ vec![
vec2(interact.rect.center().x, interact.rect.min().y), pos2(interact.rect.center().x, interact.rect.min().y),
vec2(interact.rect.center().x, interact.rect.max().y), pos2(interact.rect.center().x, interact.rect.max().y),
], ],
interact, interact,
) )
@ -483,8 +483,8 @@ impl Widget for Separator {
let interact = region.reserve_space(vec2(available_space.x, self.width), None); let interact = region.reserve_space(vec2(available_space.x, self.width), None);
( (
vec![ vec![
vec2(interact.rect.min().x, interact.rect.center().y), pos2(interact.rect.min().x, interact.rect.center().y),
vec2(interact.rect.max().x, interact.rect.center().y), pos2(interact.rect.max().x, interact.rect.center().y),
], ],
interact, interact,
) )

View file

@ -29,7 +29,7 @@ impl Window {
let mut state = ctx.memory.lock().get_or_create_window( let mut state = ctx.memory.lock().get_or_create_window(
id, id,
Rect::from_min_size( Rect::from_min_size(
vec2(400.0, 200.0), // TODO pos2(400.0, 200.0), // TODO
vec2(200.0, 200.0), // TODO vec2(200.0, 200.0), // TODO
), ),
); );

View file

@ -6,7 +6,7 @@ use {
emigui::{ emigui::{
example_app::ExampleApp, example_app::ExampleApp,
label, label,
math::vec2, math::*,
widgets::{Button, Label}, widgets::{Button, Label},
Align, Emigui, Window, Align, Emigui, Window,
}, },
@ -63,7 +63,7 @@ fn main() {
raw_input.mouse_down = state == glutin::ElementState::Pressed; raw_input.mouse_down = state == glutin::ElementState::Pressed;
} }
glutin::WindowEvent::CursorMoved { position, .. } => { 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, .. } => { glutin::WindowEvent::KeyboardInput { input, .. } => {
if input.virtual_keycode == Some(glutin::VirtualKeyCode::Q) if input.virtual_keycode == Some(glutin::VirtualKeyCode::Q)