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
// Copyright (c) 2022 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Apache-2.0 License that can be found
// in the LICENSE file.

use crate::base::RectF;
use crate::kernel::{PainterTrait, PathTrait};
use crate::platforms::Path;
use crate::shapes::ShapeTrait;

const MIN_VERTEX: usize = 3;
const MAX_VERTEX: usize = 99;

#[derive(Debug)]
pub struct StarShape {
    corners: usize,
    corner_radius: f64,
    path: Path,
    path_is_dirty: bool,
}

impl StarShape {
    /// Create a new star shape object with specified `corners`.
    ///
    /// Corner radius is set to 0.0.
    pub fn new(corners: usize) -> Self {
        assert!(corners >= MIN_VERTEX && corners <= MAX_VERTEX);
        let path = Path::new();
        Self {
            corners,
            corner_radius: 0.0,
            path,
            path_is_dirty: true,
        }
    }

    /// Create a five-corner star shape.
    pub fn new_star() -> Self {
        Self::new(5)
    }

    /// Get current number of corners.
    pub fn corners(&self) -> usize {
        self.corners
    }

    /// Set number of corners.
    ///
    /// Note that minimum corners shall be 3.
    pub fn set_corners(&mut self, corners: usize) {
        assert!(corners >= MIN_VERTEX && corners <= MAX_VERTEX);
        if self.corners != corners {
            self.path_is_dirty = true;
            self.corners = corners;
        }
    }

    /// Get current corner radius.
    pub fn corner_radius(&self) -> f64 {
        self.corner_radius
    }

    /// Set corner radius.
    ///
    /// Note that radius shall be a non-negative number.
    pub fn set_corner_radius(&mut self, radius: f64) {
        assert!(radius >= 0.0);
        if self.corner_radius != radius {
            self.path_is_dirty = true;
            self.corner_radius = radius;
        }
    }

    fn update_path(&mut self) {
        if !self.path_is_dirty {
            return;
        }
        self.path.clear();
        // TODO(Shaohua): draw star shape.
        self.path_is_dirty = false;
    }
}

impl ShapeTrait for StarShape {
    fn bounding_rect(&self) -> RectF {
        todo!()
    }

    fn repaint(&mut self, painter: &mut dyn PainterTrait) {
        self.update_path();
        painter.stroke(&self.path);
    }
}