From 35db5fca7273eedf1e1ffd0d03bdc9bc8b4f6645 Mon Sep 17 00:00:00 2001 From: Malik Olivier Boussejra Date: Thu, 29 Mar 2018 11:42:36 +0900 Subject: [PATCH] window_draw_list.rs: Wrap add_bezier_curve --- src/window_draw_list.rs | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/window_draw_list.rs b/src/window_draw_list.rs index 12cb00c..7cd3e25 100644 --- a/src/window_draw_list.rs +++ b/src/window_draw_list.rs @@ -198,6 +198,26 @@ macro_rules! impl_draw_list_methods { { Circle::new(self, center, radius, color) } + + /// Returns a Bezier curve stretching from `pos0` to `pos1`, whose + /// curvature is defined by `cp0` and `cp1`. + pub fn add_bezier_curve( + &self, + pos0: P1, + cp0: P2, + cp1: P3, + pos1: P4, + color: C, + ) -> BezierCurve<'ui, $T> + where + P1: Into, + P2: Into, + P3: Into, + P4: Into, + C: Into, + { + BezierCurve::new(self, pos0, cp0, cp1, pos1, color) + } } }; } @@ -495,3 +515,67 @@ impl<'ui, D: DrawAPI<'ui>> Circle<'ui, D> { } } } + +/// Represents a Bezier curve about to be drawn +pub struct BezierCurve<'ui, D: 'ui> { + pos0: ImVec2, + cp0: ImVec2, + pos1: ImVec2, + cp1: ImVec2, + color: ImColor, + thickness: f32, + /// If num_segments is not set, the bezier curve is auto-tessalated. + num_segments: Option, + draw_list: &'ui D, +} + +impl<'ui, D: DrawAPI<'ui>> BezierCurve<'ui, D> { + fn new(draw_list: &'ui D, pos0: P1, cp0: P2, cp1: P3, pos1: P4, c: C) -> Self + where + P1: Into, + P2: Into, + P3: Into, + P4: Into, + C: Into, + { + Self { + pos0: pos0.into(), + cp0: cp0.into(), + pos1: pos1.into(), + cp1: cp1.into(), + color: c.into(), + thickness: 1.0, + num_segments: None, + draw_list, + } + } + + /// Set curve's thickness (default to 1.0 pixel) + pub fn thickness(mut self, thickness: f32) -> Self { + self.thickness = thickness; + self + } + + /// Set number of segments used to draw the Bezier curve. If not set, the + /// bezier curve is auto-tessalated. + pub fn num_segments(mut self, num_segments: u32) -> Self { + self.num_segments = Some(num_segments); + self + } + + /// Draw the curve on the window. + pub fn build(self) { + unsafe { + sys::ImDrawList_AddBezierCurve( + self.draw_list.draw_list(), + self.pos0, + self.cp0, + self.cp1, + self.pos1, + self.color.into(), + self.thickness, + self.num_segments.unwrap_or(0) as i32, + ) + } + } +}