From 878de089e842a9badc7ba283748a3709613bf5f5 Mon Sep 17 00:00:00 2001 From: Malik Olivier Boussejra Date: Fri, 30 Mar 2018 07:42:40 +0900 Subject: [PATCH] window_draw_list.rs: Wrap add_circle ImDrawList_AddCircle was missing an argument in the bindings, resulting in UB. This patches includes it and wrap the AddCircle API. --- imgui-sys/src/lib.rs | 1 + src/window_draw_list.rs | 83 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/imgui-sys/src/lib.rs b/imgui-sys/src/lib.rs index 890fcf5..3d0f6a1 100644 --- a/imgui-sys/src/lib.rs +++ b/imgui-sys/src/lib.rs @@ -1918,6 +1918,7 @@ extern "C" { radius: c_float, col: ImU32, num_segments: c_int, + thickness: c_float, ); pub fn ImDrawList_AddCircleFilled( list: *mut ImDrawList, diff --git a/src/window_draw_list.rs b/src/window_draw_list.rs index 53ebb88..8b31fcb 100644 --- a/src/window_draw_list.rs +++ b/src/window_draw_list.rs @@ -171,6 +171,15 @@ macro_rules! impl_draw_list_methods { ); } } + + /// Returns a circle with the given `center`, `radius` and `color`. + pub fn add_circle(&self, center: P, radius: f32, color: C) -> Circle<'ui, $T> + where + P: Into, + C: Into, + { + Circle::new(self, center, radius, color) + } } }; } @@ -325,3 +334,77 @@ impl<'ui, D: DrawAPI<'ui>> Rect<'ui, D> { } } } + +/// Represents a circle about to be drawn +pub struct Circle<'ui, D: 'ui> { + center: ImVec2, + radius: f32, + color: ImColor, + num_segments: u32, + thickness: f32, + filled: bool, + draw_list: &'ui D, +} + +impl<'ui, D: DrawAPI<'ui>> Circle<'ui, D> { + pub fn new(draw_list: &'ui D, center: P, radius: f32, color: C) -> Self + where + P: Into, + C: Into, + { + Self { + center: center.into(), + radius, + color: color.into(), + num_segments: 12, + thickness: 1.0, + filled: false, + draw_list, + } + } + + /// Set number of segment used to draw the circle, default to 12. + /// Add more segments if you want a smoother circle. + pub fn num_segments(mut self, num_segments: u32) -> Self { + self.num_segments = num_segments; + self + } + + /// Set circle's thickness (default to 1.0 pixel) + pub fn thickness(mut self, thickness: f32) -> Self { + self.thickness = thickness; + self + } + + /// Set to `true` to make a filled circle (default to `false`). + pub fn filled(mut self, filled: bool) -> Self { + self.filled = filled; + self + } + + /// Draw the circle on the window. + pub fn build(self) { + if self.filled { + unsafe { + sys::ImDrawList_AddCircleFilled( + self.draw_list.draw_list(), + self.center, + self.radius, + self.color.into(), + self.num_segments as i32, + ) + } + } else { + unsafe { + sys::ImDrawList_AddCircle( + self.draw_list.draw_list(), + self.center, + self.radius, + self.color.into(), + self.num_segments as i32, + self.thickness, + ) + } + } + } +}