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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use rin::events::*;
use rin::math::*;
use rin::color::*;
use rin::graphics::Ttf;
use std::rc::Rc;

/// A shape helper class for all controls
pub struct ControlShape{
    pos: Property<'static, Pnt2>,
    width: Property<'static, f32>,
    height: Property<'static, f32>,
    h_margin: f32,
    v_margin: f32,
    color: Rgb<f32>,
    font: Option<Rc<Ttf>>,
    show_label: bool,
}

impl ControlShape{
    pub fn new<C: ToRgb>(pos: Property<'static, Pnt2>, width: Property<'static, f32>, height: Property<'static, f32>, h_margin: f32, v_margin: f32, color: C) -> ControlShape {
        ControlShape{
            pos,
            width,
            height,
            h_margin,
            v_margin,
            color: color.to_rgb(),
            font: None,
            show_label: true,
        }
    }

    pub fn contained(&self, margin_top: Property<'static, f32>, margin_right: Property<'static, f32>, margin_bottom: Property<'static, f32>, margin_left: Property<'static, f32>) -> ControlShape{
        let pos = self.position()
            .zip(margin_top.clone())
            .zip(margin_left.clone())
            .flatten()
            .map(|(pos, top, left)| pnt2(pos.x + left, pos.y + top));

        let width = self.width()
            .zip(margin_left)
            .zip(margin_right)
            .flatten()
            .map(|(width, left, right)| width - left - right);

        let height = self.height()
            .zip(margin_top)
            .zip(margin_bottom)
            .flatten()
            .map(|(height, top, bottom)| height - top - bottom);

        ControlShape{
            pos,
            width,
            height,
            h_margin: self.h_margin,
            v_margin: self.v_margin,
            color: self.color,
            font: self.font.clone(),
            show_label: true,
        }
    }

    pub fn set_rect(&mut self, rect: Rect<f32>){
        self.pos.set(rect.pos);
        self.width.set(rect.width);
        self.height.set(rect.height);
    }

    // pub fn rect<T: BaseNum>(&self) -> Property<'static, Rect<T>>
    //     where f32: SubsetOf<T>
    // {
    //     self.pos.clone()
    //         .zip(self.width.clone())
    //         .zip(self.height.clone())
    //         .flatten()
    //         .map(|(pos, width, height)| convert(Rect{pos, width, height}))
    // }

    pub fn set_position(&mut self, pos: Pnt2) {
        self.pos.set(pos);
    }

    pub fn set_size(&mut self, size: Vec2){
        self.width.set(size.x);
        self.height.set(size.y);
    }

    pub fn set_position_property(&mut self, pos: Property<'static, Pnt2>) {
        self.pos = pos;
    }

    pub fn set_width(&mut self, width: f32) {
        self.width.set(width);
    }

    pub fn set_width_property(&mut self, width: Property<'static, f32>) {
        self.width = width;
    }

    pub fn set_height(&mut self, height: f32) {
        self.height.set(height);
    }

    pub fn set_height_property(&mut self, height: Property<'static, f32>) {
        self.height = height;
    }

    pub fn position(&self) -> Property<'static, Pnt2> {
        self.pos.clone()
    }

    pub fn size(&self) -> Property<'static, Vec2> {
        self.width.clone()
            .zip(self.height.clone())
            .map(|(w, h)| vec2(w,h))
    }

    pub fn width(&self) -> Property<'static, f32>{
        self.width.clone()
    }

    pub fn height(&self) -> Property<'static, f32>{
        self.height.clone()
    }

    pub fn set_inner_margin(&mut self, margin: f32) {
        self.h_margin = margin;
        self.v_margin = margin;
    }

    pub fn set_h_margin(&mut self, margin: f32) {
        self.h_margin = margin;
    }

    pub fn set_v_margin(&mut self, margin: f32) {
        self.v_margin = margin;
    }

    pub fn set_color<C: ToRgb>(&mut self, color: C) {
        self.color = color.to_rgb();
    }

    pub fn h_margin(&self) -> f32 {
        self.h_margin
    }

    pub fn v_margin(&self) -> f32 {
        self.v_margin
    }

    pub fn color(&self) -> Rgb<f32> {
        self.color
    }

    pub fn set_font(&mut self, font: Rc<Ttf>) {
        self.font = Some(font)
    }

    pub fn font(&self) -> Option<Rc<Ttf>> {
        self.font.clone()
    }

    pub fn hide_label(&mut self){
        self.show_label = false;
    }

    pub fn show_label(&mut self){
        self.show_label = true;
    }

    pub fn label_visible(&self) -> bool{
        self.show_label
    }
}