Add basic polyline methods to draw list

This commit is contained in:
dbr 2021-10-12 13:24:50 +11:00
parent de69a0c103
commit eb1bd01179
2 changed files with 125 additions and 0 deletions

View File

@ -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();
});
});
}

View File

@ -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<C, P>(
&'ui self,
points: Vec<P>,
c: C,
) -> Polyline<'ui>
where
C: Into<ImColor32>,
P: Into<MintVec2>,
{
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<C, P>(
draw_list: &'ui DrawListMut<'_>,
points: Vec<P>,
c: C,
) -> Self
where
C: Into<ImColor32>,
P: Into<MintVec2>,
{
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> {