conrod_core/widget/primitive/shape/
rectangle.rs

1//! A simple, non-interactive rectangle shape widget.
2//!
3//! Due to the frequency of its use in GUIs, the `Rectangle` gets its own widget to allow backends
4//! to specialise their rendering implementations.
5
6use super::Style;
7use widget;
8use widget::triangles::Triangle;
9use {Color, Colorable, Dimensions, Point, Rect, Sizeable, Widget};
10
11/// A basic, non-interactive rectangle shape widget.
12#[derive(Copy, Clone, Debug, WidgetCommon_)]
13pub struct Rectangle {
14    /// Data necessary and common for all widget builder types.
15    #[conrod(common_builder)]
16    pub common: widget::CommonBuilder,
17    /// Unique styling for the **Rectangle**.
18    pub style: Style,
19}
20
21/// Unique state for the Rectangle.
22#[derive(Copy, Clone, Debug, PartialEq)]
23pub struct State {
24    kind: Kind,
25}
26
27/// Whether the rectangle is drawn as an outline or a filled color.
28#[derive(Copy, Clone, Debug, PartialEq)]
29pub enum Kind {
30    /// Only the outline of the rectangle is drawn.
31    Outline,
32    /// The rectangle area is filled with some color.
33    Fill,
34}
35
36impl Rectangle {
37    /// Build a rectangle with the dimensions and style.
38    pub fn styled(dim: Dimensions, style: Style) -> Self {
39        Rectangle {
40            common: widget::CommonBuilder::default(),
41            style: style,
42        }
43        .wh(dim)
44    }
45
46    /// Build a new filled rectangle.
47    pub fn fill(dim: Dimensions) -> Self {
48        Rectangle::styled(dim, Style::fill())
49    }
50
51    /// Build a new filled rectangle widget filled with the given color.
52    pub fn fill_with(dim: Dimensions, color: Color) -> Self {
53        Rectangle::styled(dim, Style::fill_with(color))
54    }
55
56    /// Build a new outlined rectangle widget.
57    pub fn outline(dim: Dimensions) -> Self {
58        Rectangle::styled(dim, Style::outline())
59    }
60
61    /// Build an outlined rectangle rather than a filled one.
62    pub fn outline_styled(dim: Dimensions, line_style: widget::line::Style) -> Self {
63        Rectangle::styled(dim, Style::outline_styled(line_style))
64    }
65}
66
67impl Widget for Rectangle {
68    type State = State;
69    type Style = Style;
70    type Event = ();
71
72    fn init_state(&self, _: widget::id::Generator) -> Self::State {
73        State { kind: Kind::Fill }
74    }
75
76    fn style(&self) -> Self::Style {
77        self.style.clone()
78    }
79
80    /// Update the state of the Rectangle.
81    fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
82        let widget::UpdateArgs { state, style, .. } = args;
83
84        let kind = match *style {
85            Style::Fill(_) => Kind::Fill,
86            Style::Outline(_) => Kind::Outline,
87        };
88
89        if state.kind != kind {
90            state.update(|state| state.kind = kind);
91        }
92    }
93}
94
95impl Colorable for Rectangle {
96    fn color(mut self, color: Color) -> Self {
97        self.style.set_color(color);
98        self
99    }
100}
101
102/// The two triangles that describe the given `Rect`.
103pub fn triangles(rect: Rect) -> (Triangle<Point>, Triangle<Point>) {
104    let (l, r, b, t) = rect.l_r_b_t();
105    let quad = [[l, t], [r, t], [r, b], [l, b]];
106    widget::triangles::from_quad(quad)
107}