From eb1bd01179c4c5cb5016a65e189cc0e5b1f33276 Mon Sep 17 00:00:00 2001 From: dbr Date: Tue, 12 Oct 2021 13:24:50 +1100 Subject: [PATCH 1/3] Add basic polyline methods to draw list --- imgui-examples/examples/draw_list.rs | 40 +++++++++++++ imgui/src/draw_list.rs | 85 ++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/imgui-examples/examples/draw_list.rs b/imgui-examples/examples/draw_list.rs index f575e77..3294114 100644 --- a/imgui-examples/examples/draw_list.rs +++ b/imgui-examples/examples/draw_list.rs @@ -93,5 +93,45 @@ fn main() { [1.0, 1.0, 1.0], ); }); + + ui.window("Polygons") + .size([300.0, 150.0], Condition::FirstUseEver) + .position([400.0, 110.0], Condition::FirstUseEver) + .scroll_bar(false) + .build(|| { + let draw_list = ui.get_window_draw_list(); + + // Origin + let o = ui.cursor_screen_pos(); + + draw_list + .add_polyline( + vec![ + [o[0] + 0.0, o[1] + 0.0], + [o[0] + 100.0, o[1] + 25.0], + [o[0] + 50.0, o[1] + 50.0], + [o[0] + 100.0, o[1] + 75.0], + [o[0] + 0.0, o[1] + 100.0], + [o[0] + 0.0, o[1] + 0.0], + ], + [1.0, 0.0, 1.0] + ) + .build(); + + draw_list + .add_polyline( + vec![ + [o[0] + 120.0 + 0.0, o[1] + 0.0], + [o[0] + 120.0 + 100.0, o[1] + 25.0], + [o[0] + 120.0 + 50.0, o[1] + 50.0], + [o[0] + 120.0 + 100.0, o[1] + 75.0], + [o[0] + 120.0 + 0.0, o[1] + 100.0], + [o[0] + 120.0 + 0.0, o[1] + 0.0], + ], + [0.0, 1.0, 1.0] + ) + .filled(true) + .build(); + }); }); } diff --git a/imgui/src/draw_list.rs b/imgui/src/draw_list.rs index d58ef6d..d810f57 100644 --- a/imgui/src/draw_list.rs +++ b/imgui/src/draw_list.rs @@ -226,6 +226,22 @@ impl<'ui> DrawListMut<'ui> { Line::new(self, p1, p2, c) } + /// Returns a polygonal line. If filled is rendered as a convex + /// polygon, if not filled is drawn as a line specified by + /// [`PolyLine::thickness`] (default 1.0) + #[doc(alias = "AddPolyline", alias = "AddConvexPolyFilled")] + pub fn add_polyline( + &'ui self, + points: Vec

, + c: C, + ) -> Polyline<'ui> + where + C: Into, + P: Into, + { + Polyline::new(self, points, c) + } + /// Returns a rectangle whose upper-left corner is at point `p1` /// and lower-right corner is at point `p2`, with color `c`. #[doc(alias = "AddRectFilled", alias = "AddRect")] @@ -489,6 +505,75 @@ impl<'ui> Line<'ui> { } } +/// Represents a poly line about to be drawn +#[must_use = "should call .build() to draw the object"] +pub struct Polyline<'ui> { + points: Vec<[f32; 2]>, + thickness: f32, + filled: bool, + color: ImColor32, + draw_list: &'ui DrawListMut<'ui>, +} + +impl<'ui> Polyline<'ui> { + fn new( + draw_list: &'ui DrawListMut<'_>, + points: Vec

, + c: C, + ) -> Self + where + C: Into, + P: Into, + { + Self { + points: points.into_iter().map(|p| p.into().into()).collect(), + color: c.into(), + thickness: 1.0, + filled: false, + draw_list, + } + } + + /// Set line's thickness (default to 1.0 pixel). Has no effect if + /// shape is filled + pub fn thickness(mut self, thickness: f32) -> Self { + self.thickness = thickness; + self + } + + /// Draw shape as filled convex polygon + pub fn filled(mut self, filled: bool) -> Self { + self.filled = filled; + self + } + + /// Draw the line on the window + pub fn build(self) { + if self.filled { + unsafe { + sys::ImDrawList_AddConvexPolyFilled( + self.draw_list.draw_list, + self.points.as_slice().as_ptr() as *const sys::ImVec2, + self.points.len() as i32, + self.color.into(), + ) + } + } else { + unsafe { + sys::ImDrawList_AddPolyline( + self.draw_list.draw_list, + self.points.as_slice().as_ptr() as *const sys::ImVec2, + self.points.len() as i32, + self.color.into(), + sys::ImDrawFlags::default(), + self.thickness, + ) + } + } + } +} + + /// Represents a rectangle about to be drawn #[must_use = "should call .build() to draw the object"] pub struct Rect<'ui> { From 416272a48a40c97516185a9eccc8f83cae5ed7d4 Mon Sep 17 00:00:00 2001 From: dbr Date: Tue, 12 Oct 2021 13:26:33 +1100 Subject: [PATCH 2/3] fmt --- imgui-examples/examples/draw_list.rs | 4 ++-- imgui/src/draw_list.rs | 13 ++----------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/imgui-examples/examples/draw_list.rs b/imgui-examples/examples/draw_list.rs index 3294114..bbf02e0 100644 --- a/imgui-examples/examples/draw_list.rs +++ b/imgui-examples/examples/draw_list.rs @@ -114,7 +114,7 @@ fn main() { [o[0] + 0.0, o[1] + 100.0], [o[0] + 0.0, o[1] + 0.0], ], - [1.0, 0.0, 1.0] + [1.0, 0.0, 1.0], ) .build(); @@ -128,7 +128,7 @@ fn main() { [o[0] + 120.0 + 0.0, o[1] + 100.0], [o[0] + 120.0 + 0.0, o[1] + 0.0], ], - [0.0, 1.0, 1.0] + [0.0, 1.0, 1.0], ) .filled(true) .build(); diff --git a/imgui/src/draw_list.rs b/imgui/src/draw_list.rs index d810f57..19b1b26 100644 --- a/imgui/src/draw_list.rs +++ b/imgui/src/draw_list.rs @@ -230,11 +230,7 @@ impl<'ui> DrawListMut<'ui> { /// polygon, if not filled is drawn as a line specified by /// [`PolyLine::thickness`] (default 1.0) #[doc(alias = "AddPolyline", alias = "AddConvexPolyFilled")] - pub fn add_polyline( - &'ui self, - points: Vec

, - c: C, - ) -> Polyline<'ui> + pub fn add_polyline(&'ui self, points: Vec

, c: C) -> Polyline<'ui> where C: Into, P: Into, @@ -516,11 +512,7 @@ pub struct Polyline<'ui> { } impl<'ui> Polyline<'ui> { - fn new( - draw_list: &'ui DrawListMut<'_>, - points: Vec

, - c: C, - ) -> Self + fn new(draw_list: &'ui DrawListMut<'_>, points: Vec

, c: C) -> Self where C: Into, P: Into, @@ -573,7 +565,6 @@ impl<'ui> Polyline<'ui> { } } - /// Represents a rectangle about to be drawn #[must_use = "should call .build() to draw the object"] pub struct Rect<'ui> { From e25e51d3fc0004c3d7065f6d8e7571a545daca0d Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 13 Oct 2021 15:42:30 +1100 Subject: [PATCH 3/3] Remove redundant as_slice call per PR feedback --- imgui/src/draw_list.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/imgui/src/draw_list.rs b/imgui/src/draw_list.rs index 19b1b26..01d2270 100644 --- a/imgui/src/draw_list.rs +++ b/imgui/src/draw_list.rs @@ -545,7 +545,7 @@ impl<'ui> Polyline<'ui> { unsafe { sys::ImDrawList_AddConvexPolyFilled( self.draw_list.draw_list, - self.points.as_slice().as_ptr() as *const sys::ImVec2, + self.points.as_ptr() as *const sys::ImVec2, self.points.len() as i32, self.color.into(), ) @@ -554,7 +554,7 @@ impl<'ui> Polyline<'ui> { unsafe { sys::ImDrawList_AddPolyline( self.draw_list.draw_list, - self.points.as_slice().as_ptr() as *const sys::ImVec2, + self.points.as_ptr() as *const sys::ImVec2, self.points.len() as i32, self.color.into(), sys::ImDrawFlags::default(),