From d3d251a9ce1991f1e207534593a7860327911673 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 29 Mar 2021 19:50:38 +0200 Subject: [PATCH] Introduce a `Path` struct Co-Authored-By: Max Brunsfeld --- gpui/src/geometry.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++ gpui/src/lib.rs | 2 +- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 gpui/src/geometry.rs diff --git a/gpui/src/geometry.rs b/gpui/src/geometry.rs new file mode 100644 index 0000000000..b4ff991065 --- /dev/null +++ b/gpui/src/geometry.rs @@ -0,0 +1,94 @@ +pub use pathfinder_geometry::*; + +use vector::{vec2f, Vector2F}; + +pub(crate) struct Vertex { + xy_position: Vector2F, + st_position: Vector2F, +} + +pub struct Path { + vertices: Vec, + start: Vector2F, + current: Vector2F, + countours_len: usize, +} + +enum Kind { + Solid, + Quadratic, +} + +impl Path { + fn new() -> Self { + Self { + vertices: Vec::new(), + start: vec2f(0., 0.), + current: vec2f(0., 0.), + countours_len: 0, + } + } + + pub fn reset(&mut self, point: Vector2F) { + self.vertices.clear(); + self.start = point; + self.current = point; + self.countours_len = 0; + } + + pub fn line_to(&mut self, point: Vector2F) { + self.countours_len += 1; + if self.countours_len > 1 { + self.push_triangle(self.start, self.current, point, Kind::Solid); + } + + self.current = point; + } + + pub fn curve_to(&mut self, point: Vector2F, ctrl: Vector2F) { + self.countours_len += 1; + if self.countours_len > 1 { + self.push_triangle(self.start, self.current, point, Kind::Solid); + } + + self.push_triangle(self.current, ctrl, point, Kind::Quadratic); + self.current = point; + } + + pub(crate) fn close(self) -> Vec { + self.vertices + } + + fn push_triangle(&mut self, a: Vector2F, b: Vector2F, c: Vector2F, kind: Kind) { + match kind { + Kind::Solid => { + self.vertices.push(Vertex { + xy_position: a, + st_position: vec2f(0., 1.), + }); + self.vertices.push(Vertex { + xy_position: b, + st_position: vec2f(0., 1.), + }); + self.vertices.push(Vertex { + xy_position: c, + st_position: vec2f(0., 1.), + }); + } + Kind::Quadratic => { + self.vertices.push(Vertex { + xy_position: a, + st_position: vec2f(0., 0.), + }); + self.vertices.push(Vertex { + xy_position: b, + st_position: vec2f(0.5, 0.), + }); + self.vertices.push(Vertex { + xy_position: c, + st_position: vec2f(1., 1.), + }); + } + } + } +} diff --git a/gpui/src/lib.rs b/gpui/src/lib.rs index 0a08438c9d..b2222429bd 100644 --- a/gpui/src/lib.rs +++ b/gpui/src/lib.rs @@ -6,6 +6,7 @@ pub mod elements; pub mod font_cache; pub use font_cache::FontCache; pub mod fonts; +pub mod geometry; mod presenter; mod scene; pub use scene::{Border, Quad, Scene}; @@ -17,7 +18,6 @@ pub mod executor; pub mod keymap; pub mod platform; pub use pathfinder_color as color; -pub use pathfinder_geometry as geometry; pub use platform::Event; pub use presenter::{ AfterLayoutContext, Axis, EventContext, LayoutContext, PaintContext, SizeConstraint,