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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use rin::graphics::*;
use rin::color::*;
use rin::window;
use rin::events::{StreamRc, StreamT, Property};
use rin::math::{Rect, Pnt2};
use mopa;
use control_shape::ControlShape;

use std::rc::Rc;

pub trait ControlGeometry{
    fn flexible() -> bool where Self: Sized;
    fn geometry(&self, container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DColor>>;
    fn text_geometry(&self, container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DTex>>;
    fn texture_geometry(&self, container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DTex>>;
    fn font(&self) -> Option<&Ttf>;
    fn color(&self) -> &Rgb<f32>;
    fn position(&self) -> &Property<'static, Pnt2>;
    fn width(&self) -> &Property<'static, f32>;
    fn height(&self) -> &Property<'static, f32>;
    fn rect(&self) -> Rect<f32>{
        Rect{
            pos: self.position().to_value(),
            width: self.width().to_value(),
            height: self.height().to_value(),
        }
    }
}

pub trait ControlEvents{
    fn non_attended(&self) -> StreamRc<'static, window::Event>;
}

pub trait Control: ControlGeometry + ControlEvents + mopa::Any{
    fn name(&self) -> &str;
}

pub trait ControlBuilt<P: IntoControlBuilder<Self::Builder>> : Control where Self: Sized{
    type Builder: ControlBuilder<Control = Self>;
}

pub trait ControlBuilder{
    type Control: Control;
    fn shape(&mut self) -> &mut ControlShape;
    fn create(self) -> Self::Control;

    fn position(mut self, pos: Pnt2) -> Self where Self: Sized {
        self.shape().set_position(pos);
        self
    }

    fn position_property(mut self, pos: Property<'static, Pnt2>) -> Self where Self: Sized {
        self.shape().set_position_property(pos);
        self
    }

    fn position_property_opt(mut self, pos: Option<Property<'static, Pnt2>>) -> Self where Self: Sized {
        if let Some(pos) = pos {
            self.shape().set_position_property(pos);
        }
        self
    }

    fn width(mut self, width: f32) -> Self where Self: Sized {
        self.shape().set_width(width);
        self
    }

    fn width_opt(mut self, width: Option<f32>) -> Self where Self: Sized {
        if let Some(width) = width {
            self.shape().set_width(width);
        }
        self
    }

    fn width_property(mut self, width: Property<'static, f32>) -> Self where Self: Sized {
        self.shape().set_width_property(width);
        self
    }

    fn width_property_opt(mut self, width: Option<Property<'static, f32>>) -> Self where Self: Sized {
        if let Some(width) = width {
            self.shape().set_width_property(width);
        }
        self
    }

    fn height(mut self, height: f32) -> Self where Self: Sized {
        self.shape().set_height(height);
        self
    }

    fn height_opt(mut self, height: Option<f32>) -> Self where Self: Sized {
        if let Some(height) = height {
            self.shape().set_width(height);
        }
        self
    }

    fn height_property(mut self, height: Property<'static, f32>) -> Self where Self: Sized {
        self.shape().set_height_property(height);
        self
    }

    fn height_property_opt(mut self, height: Option<Property<'static, f32>>) -> Self where Self: Sized {
        if let Some(height) = height {
            self.shape().set_height_property(height);
        }
        self
    }

    fn inner_margin(mut self, margin: f32) -> Self where Self: Sized {
        self.shape().set_inner_margin(margin);
        self
    }

    fn v_margin(mut self, margin: f32) -> Self where Self: Sized {
        self.shape().set_v_margin(margin);
        self
    }

    fn v_margin_opt(mut self, margin: Option<f32>) -> Self where Self: Sized {
        if let Some(margin) = margin{
            self.shape().set_v_margin(margin);
        }
        self
    }

    fn h_margin(mut self, margin: f32) -> Self where Self: Sized {
        self.shape().set_h_margin(margin);
        self
    }

    fn h_margin_opt(mut self, margin: Option<f32>) -> Self where Self: Sized {
        if let Some(margin) = margin{
            self.shape().set_h_margin(margin);
        }
        self
    }

    fn font(mut self, font: Rc<Ttf>) -> Self where Self: Sized {
        self.shape().set_font(font);
        self
    }

    fn color<C: ToRgb>(mut self, color: C) -> Self where Self: Sized {
        self.shape().set_color(color);
        self
    }

    fn color_opt(mut self, color: Option<Rgb<f32>>) -> Self where Self: Sized {
        if let Some(color) = color {
            self.shape().set_color(color);
        }
        self
    }

    fn hide_label(mut self) -> Self where Self: Sized {
        self.shape().hide_label();
        self
    }

    fn hide_label_opt(mut self, hide: Option<bool>) -> Self where Self: Sized {
        if let Some(hide) = hide {
            if hide {
                self.shape().hide_label();
            }else{
                self.shape().show_label();
            }
        }
        self
    }

    fn from_json_style(self, style: &str) -> Self where Self: Sized {
        #[derive(Deserialize, Default)]
        struct Des{
            width: Option<f32>,
            height: Option<f32>,
            v_margin: Option<f32>,
            h_margin: Option<f32>,
            color: Option<Rgb<f32>>,
            hide_label: Option<bool>,
        }

        let des: Des = serde_json::from_str(style).unwrap();

        self.width_opt(des.width)
            .height_opt(des.height)
            .v_margin_opt(des.v_margin)
            .h_margin_opt(des.h_margin)
            .color_opt(des.color)
            .hide_label_opt(des.hide_label)
    }
}

pub trait BuilderFromProperty<P>: ControlBuilder{
    fn from_parameter<S: StreamT<'static, window::Event>>(parameter: P, name: &str, events: S) -> Self;
}

pub trait IntoControlBuilder<B: ControlBuilder>{
    fn into_builder<S: StreamT<'static, window::Event>>(self, name: &str, events: S) -> B;
}

impl<B:BuilderFromProperty<P>,P> IntoControlBuilder<B> for P{
    fn into_builder<S: StreamT<'static, window::Event>>(self, name: &str, events: S) -> B{
        B::from_parameter(self, name, events)
    }
}

pub trait ControlDefaultProperty where Self: Sized{
    type Builder: ControlBuilder + BuilderFromProperty<Self>;
}

mopafy!(Control);

#[cfg(any(feature = "gl", feature = "gles", feature="webgl"))]
mod gl {
    use rin::gl::{self, Renderer2d};
    use rin::graphics::*;
    use utils;
    use rin::gl::RenderSurface;
    use rin::math::{convert, Rect};

    impl gl::Render3d for super::ControlGeometry{
        fn render<R: RenderSurface>(&self, gl: &gl::Renderer<R>){
            let viewport = *gl.context().state().viewport();
            let viewport: Rect<i32> = viewport.into();
            let viewport: Rect<f32> = convert(viewport);
            let viewport = Some(&viewport);
            if let Some(geometry) = self.geometry(viewport).map(|g| mesh(g, PrimitiveType::Triangles)){
                let gl = gl.with_properties(&[
                    gl::Property::Blend(true),
                    gl::Property::BlendEquation(gl::FUNC_ADD),
                    gl::Property::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA),
                ]);
                gl.draw_mesh(&geometry);
            }

            if let (Some(text_geometry), Some(font)) = (self.text_geometry(viewport), self.font()){
                let font_material = utils::gl::font_material(gl, font, self.color());
                let text_geometry = mesh(text_geometry, PrimitiveType::Triangles);
                gl.draw_mesh_with_material(&text_geometry, font_material);
            }
        }
    }
}

use std::marker;

pub struct As<C>(marker::PhantomData<C>);

pub struct AsControl<C, P>{
    property: P,
    marker: marker::PhantomData<C>
}

impl<C> As<C>{
    pub fn new<P>(property: P) -> AsControl<C, P>{
        AsControl{
            property,
            marker: marker::PhantomData,
        }
    }
}

impl<C: ControlBuilt<P>, P> ControlDefaultProperty for AsControl<C, P>
where <C as ControlBuilt<P>>::Builder: BuilderFromProperty<P>
{
    type Builder = C::Builder;
}

impl<B: ControlBuilder, P> BuilderFromProperty<AsControl<B::Control, P>> for B
where B: BuilderFromProperty<P>{
    fn from_parameter<S: StreamT<'static, window::Event>>(parameter: AsControl<B::Control,P>, name: &str, events: S) -> Self{
        B::from_parameter(parameter.property, name, events)
    }
}

#[macro_export]
macro_rules! as_control {
    ($control: ty, $property: expr) => {
        $crate::As::<$control>::new($property)
    };
}

pub struct Styled<P>{
    pub(crate) parameter: P,
    pub(crate) style: String
}

pub fn styled<P>(parameter: P, style: &str) -> Styled<P>{
    Styled{
        parameter,
        style: style.to_owned()
    }
}

pub trait IntoStyled{
    type Property;
    fn into_styled(self) -> Styled<Self::Property>;
}

impl<P> IntoStyled for Styled<P>{
    type Property = P;
    fn into_styled(self) -> Styled<P>{
        self
    }
}

impl<P: ControlDefaultProperty> IntoStyled for P{
    type Property = P;
    fn into_styled(self) -> Styled<P>{
        Styled{
            parameter: self,
            style: "{}".to_owned(),
        }
    }
}