1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! A simple, non-interactive widget for drawing a single **Oval**.

use {Color, Colorable, Dimensions, Point, Rect, Scalar, Sizeable, Widget};
use super::Style as Style;
use widget;


/// A simple, non-interactive widget for drawing a single **Oval**.
#[derive(Copy, Clone, Debug, WidgetCommon_)]
pub struct Oval {
    /// Data necessary and common for all widget builder types.
    #[conrod(common_builder)]
    pub common: widget::CommonBuilder,
    /// Unique styling.
    pub style: Style,
    /// The number of lines used to draw the edge.
    pub resolution: usize,
}

/// Unique state for the **Oval**.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct State {
    /// The number of lines used to draw the edge.
    pub resolution: usize,
}

/// The default circle resolution if none is specified.
pub const DEFAULT_RESOLUTION: usize = 50;


impl Oval {

    /// Build an **Oval** with the given dimensions and style.
    pub fn styled(dim: Dimensions, style: Style) -> Self {
        Oval {
            common: widget::CommonBuilder::default(),
            style: style,
            resolution: DEFAULT_RESOLUTION,
        }.wh(dim)
    }

    /// Build a new **Fill**ed **Oval**.
    pub fn fill(dim: Dimensions) -> Self {
        Oval::styled(dim, Style::fill())
    }

    /// Build a new **Oval** **Fill**ed with the given color.
    pub fn fill_with(dim: Dimensions, color: Color) -> Self {
        Oval::styled(dim, Style::fill_with(color))
    }

    /// Build a new **Outline**d **Oval** widget.
    pub fn outline(dim: Dimensions) -> Self {
        Oval::styled(dim, Style::outline())
    }

    /// Build a new **Oval** **Outline**d with the given style.
    pub fn outline_styled(dim: Dimensions, line_style: widget::line::Style) -> Self {
        Oval::styled(dim, Style::outline_styled(line_style))
    }

    /// The number of lines used to draw the edge.
    ///
    /// By default, `DEFAULT_RESOLUTION` is used.
    pub fn resolution(mut self, resolution: usize) -> Self {
        self.resolution = resolution;
        self
    }
}


impl Widget for Oval {
    type State = State;
    type Style = Style;
    type Event = ();

    fn init_state(&self, _: widget::id::Generator) -> Self::State {
        State {
            resolution: DEFAULT_RESOLUTION,
        }
    }

    fn style(&self) -> Self::Style {
        self.style.clone()
    }

    fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
        let widget::UpdateArgs { state, .. } = args;
        if state.resolution != self.resolution {
            state.update(|state| state.resolution = self.resolution);
        }
    }

}


impl Colorable for Oval {
    fn color(mut self, color: Color) -> Self {
        self.style.set_color(color);
        self
    }
}


/// An iterator yielding the `Oval`'s edges as a circumference represented as a series of edges.
pub fn circumference(rect: Rect, resolution: usize) -> Circumference {
    use std::f64::consts::PI;
    let (x, y, w, h) = rect.x_y_w_h();
    Circumference {
        index: 0,
        num_points: resolution + 1,
        point: [x, y],
        half_w: w / 2.0,
        half_h: h / 2.0,
        rad_step: 2.0 * PI / resolution as Scalar,
    }
}

/// An iterator yielding the triangles that describe the given oval.
///
/// Returns `None` if the `resolution` is less than `3`.
pub fn triangles(rect: Rect, resolution: usize) -> Option<Triangles> {
    widget::polygon::triangles(circumference(rect, resolution))
}

/// An iterator yielding the `Oval`'s edges as a circumference represented as a series of edges.
#[derive(Clone)]
#[allow(missing_copy_implementations)]
pub struct Circumference {
    index: usize,
    num_points: usize,
    point: Point,
    rad_step: Scalar,
    half_w: Scalar,
    half_h: Scalar,
}

/// An iterator yielding the triangles that describe an oval.
pub type Triangles = widget::polygon::Triangles<Circumference>;

impl Iterator for Circumference {
    type Item = Point;
    fn next(&mut self) -> Option<Self::Item> {
        let Circumference { ref mut index, num_points, point, rad_step, half_w, half_h } = *self;
        if *index >= num_points { return None; } else { *index += 1; }
        let x = point[0] + half_w * (rad_step * *index as Scalar).cos();
        let y = point[1] + half_h * (rad_step * *index as Scalar).sin();
        Some([x, y])
    }
}