Skip to content

Commit

Permalink
Merge pull request imgui-rs#550 from dbr/polyline
Browse files Browse the repository at this point in the history
Add basic polyline methods to draw list
  • Loading branch information
dbr authored Oct 13, 2021
2 parents dd86066 + e25e51d commit 3b8ec75
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
40 changes: 40 additions & 0 deletions imgui-examples/examples/draw_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}
76 changes: 76 additions & 0 deletions imgui/src/draw_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ 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")]
Expand Down Expand Up @@ -489,6 +501,70 @@ 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_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_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> {
Expand Down

0 comments on commit 3b8ec75

Please sign in to comment.