From eb1bd01179c4c5cb5016a65e189cc0e5b1f33276 Mon Sep 17 00:00:00 2001 From: dbr Date: Tue, 12 Oct 2021 13:24:50 +1100 Subject: [PATCH] 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> {