mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-17 16:38:28 +00:00
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.
This commit is contained in:
parent
d1879b2a04
commit
878de089e8
@ -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,
|
||||
|
||||
@ -171,6 +171,15 @@ macro_rules! impl_draw_list_methods {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a circle with the given `center`, `radius` and `color`.
|
||||
pub fn add_circle<P, C>(&self, center: P, radius: f32, color: C) -> Circle<'ui, $T>
|
||||
where
|
||||
P: Into<ImVec2>,
|
||||
C: Into<ImColor>,
|
||||
{
|
||||
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<P, C>(draw_list: &'ui D, center: P, radius: f32, color: C) -> Self
|
||||
where
|
||||
P: Into<ImVec2>,
|
||||
C: Into<ImColor>,
|
||||
{
|
||||
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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user