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
use crate::color::TransparentableColor;
use crate::color;
use std::hash::{Hash, Hasher};
use crate::figures::shape::AttributeField::StrokeWidth;

pub struct Shape {
    style: ShapeStyle,
    subshape: SubShape
}

impl Hash for Shape {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.style.hash(state);
        self.subshape.hash(state);
    }
}

impl Shape {
    pub fn new(style: ShapeStyle, subshape: SubShape) -> Shape {
        Shape {
            style,
            subshape
        }
    }

    pub fn to_styled_element(&self) -> web_sys::Element {
        let element = self.subshape.to_element();

        self.style.apply_style(&element);

        element
    }
}

#[derive(Hash, PartialEq)]
pub enum AttributeField {
    StrokeWidth,
    StrokeColor,
    FillColor
}

impl AttributeField {
    pub fn set_attribute(&self, element: &web_sys::Element, value: &str) {
        element
            .set_attribute(self.to_attribute_string(), value)
            .expect("Unable to set attribute of Shape");
    }

    fn to_attribute_string(&self) -> &str {
        match self {
            AttributeField::StrokeWidth => "stroke-width",
            AttributeField::StrokeColor => "stroke",
            AttributeField::FillColor => "fill"
        }
    }
}

pub struct ShapeStyle {
    attributes: Vec<(AttributeField, String)>
}

impl Hash for ShapeStyle {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.attributes.iter().for_each(|x| x.hash(state));
    }
}

const DEFAULT_STROKE_WIDTH: u32 = 1;
const DEFAULT_STROKE_COLOR: TransparentableColor = TransparentableColor::RGB(color::default::BLACK);
const DEFAULT_FILL_COLOR: TransparentableColor = TransparentableColor::Transparent;

use AttributeField::*;
use crate::figures::shape::SubShape::Path;

impl ShapeStyle {
    pub fn new_from_default() -> ShapeStyle {
        ShapeStyle {
            attributes: vec![
                (StrokeWidth, DEFAULT_STROKE_WIDTH.to_string()),
                (StrokeColor, DEFAULT_STROKE_COLOR.to_string()),
                (FillColor, DEFAULT_FILL_COLOR.to_string())
            ]
        }
    }

    pub fn new() -> ShapeStyle {
        ShapeStyle {
            attributes: vec![]
        }
    }

    pub fn add_style(&mut self, attribute: AttributeField, value: String) {
        let duplicate = self.attributes
            .iter_mut()
            .find(
                |x|
                    x.0 == attribute
            ); // Find item with same AttributeField

        match duplicate {
            None => self.attributes.push((attribute, value)),
            Some(dupl) => *dupl = (attribute, value)
        }
    }

    pub fn apply_style(&self, element: &web_sys::Element) {
        self.attributes
            .iter()
            .for_each(
                |x|
                    x.0.set_attribute(element, &x.1[..])
            );
    }
}

use SubShape::*;
use crate::figures::path::PathProps;
use crate::figures::circle::CircleProps;

pub enum SubShape {
    Path(PathProps),
    Circle(CircleProps)
}

impl Hash for SubShape {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Path(props) => props.hash(state),
            Circle(props) => props.hash(state),
        }
    }
}

impl SubShape {
    fn to_element(&self) -> web_sys::Element {
        match self {
            Path(props) => props.to_element(),
            Circle(props) => props.to_element(),
        }
    }
}