Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic polyline methods to draw list #550

Merged
merged 3 commits into from Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions imgui-examples/examples/draw_list.rs
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
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_slice().as_ptr() as *const sys::ImVec2,
dbr marked this conversation as resolved.
Show resolved Hide resolved
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> {
Expand Down