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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use traits::*;
use group_v::*;
use utils;
use rin::graphics::*;
use rin::events::*;
use rin::math::*;
use rin::color::*;
use rin::color::consts::*;
use rin::window::{self, Events};
use std::rc::Rc;
use std::ops::{Deref,DerefMut};
use ::ParameterGroup;
use control_shape::ControlShape;

pub struct Panel{
    font: Rc<Ttf>,
    text_geometry: Property<'static, Vec<Vertex2DTex>>,
    geometry: Property<'static, Vec<Vertex2DColor>>,
    group: GroupV,
    width: Property<'static, f32>,
    height: Property<'static, f32>,
    position: Property<'static, Pnt2>,
    rendered: Sender<'static, ()>,
}

fn geometry(pos: Pnt2, w: f32, height: f32, line_height: f32, margin: f32, color: Rgb<f32>) -> Vec<Vertex2DColor>{
    let x = pos.x;
    let y = pos.y;
    let h = height + margin;
    let alpha = 0.7;
    let bg_color = &rgba!(rgb!(1.0, 1.0, 1.0) - color, alpha);
    let bg_bg_color = &rgba!(color, 0.5);
    let line_y = y + line_height * 1.2 + margin;
    let line_w = 2.;
    let line_alpha = 0.5;
    let line_color = &rgba!(color, line_alpha);
    vec![
        // first one layer so it works on any color
        vertex2dcolor(vec2(x,y), bg_bg_color),
        vertex2dcolor(vec2(x+w,y), bg_bg_color),
        vertex2dcolor(vec2(x+w,y+h), bg_bg_color),

        vertex2dcolor(vec2(x+w,y+h), bg_bg_color),
        vertex2dcolor(vec2(x,y+h), bg_bg_color),
        vertex2dcolor(vec2(x,y), bg_bg_color),

        // background
        vertex2dcolor(vec2(x,y), bg_color),
        vertex2dcolor(vec2(x+w,y), bg_color),
        vertex2dcolor(vec2(x+w,y+h), bg_color),

        vertex2dcolor(vec2(x+w,y+h), bg_color),
        vertex2dcolor(vec2(x,y+h), bg_color),
        vertex2dcolor(vec2(x,y), bg_color),

        // separator line
        vertex2dcolor(vec2(x,line_y), line_color),
        vertex2dcolor(vec2(x+w,line_y), line_color),
        vertex2dcolor(vec2(x+w,line_y+line_w), line_color),

        vertex2dcolor(vec2(x+w,line_y+line_w), line_color),
        vertex2dcolor(vec2(x,line_y+line_w), line_color),
        vertex2dcolor(vec2(x,line_y), line_color),
    ]
}

pub struct PanelBuilder{
    label: String,
    shape: ControlShape,
    events: StreamRc<'static, window::Event>,
}

impl PanelBuilder{
    pub fn new<S: StreamT<'static, window::Event>>(name: &str, pos: Property<'static, Pnt2>, events: S) -> PanelBuilder{
        let margin = 10.;
        let line_height = 10.;
        let header_height = Property::new(line_height * 1.2 + margin);
        let shape = ControlShape::new(pos, Property::new(200.), header_height, margin, margin, WHITE);
        PanelBuilder{
            label: name.to_owned(),
            shape,
            events: events.rc(),
        }
    }

    pub fn create(self) -> Panel{
        let (rendered_sender, rendered_stream) = utils::rendered_stream(self.events.clone().unique());
        let rendered = rendered_stream.to_property(false);
        let font = self.shape.font().unwrap_or_else(|| ::utils::default_font());
        let text = self.label.clone();
        let v_margin = self.shape.v_margin();
        let h_margin = self.shape.v_margin();
        let line_height = font.line_height();

        let height = if self.shape.label_visible(){
            line_height * 1.2  + v_margin * 2.
        }else{
            0.
        };

        let width = self.shape.width();
        let line_height = font.line_height();
        let color = self.shape.color();
        let filtered_update = self.events.clone().filter_by(rendered).update().rc();
        let position = self.shape.position();
        let geometry = if self.shape.label_visible() {
            filtered_update.clone().map(move |_| (*position, *width))
                .dedup()
                .map(move |(pos, width)| geometry(pos, width, height, line_height, v_margin, color))
                .to_property(vec![])
        }else{
            Property::default()
        };

        let text_geometry = if self.shape.label_visible(){
            let font = font.clone();
            let position = self.shape.position();
            filtered_update.map(move |_| *position).dedup().map(move |pos|{
                let text_y = pos.y + line_height * 1.2;
                font.mesh(&text, pos.x + h_margin, text_y, CoordinateOrigin::TopLeft).into()
            })
            .to_property(vec![])
        }else{
            Property::default()
        };

        let group_shape = self.shape.contained(Property::new(height), Property::new(0.), Property::new(0.), Property::new(0.));
        let group_builder = GroupBuilder::from_control_shape(&self.label, group_shape, self.events);
        let group = group_builder.create();

        let width = self.shape.width();
        let height = group.height().clone().map(move |gh| height + gh + h_margin * 2.);
        let position = self.shape.position();

        Panel{
            font: font,
            text_geometry: text_geometry,
            geometry: geometry,
            group: group,
            width,
            height,
            position,
            rendered: rendered_sender,
        }
    }
}

impl ControlBuilder for PanelBuilder {
    type Control = Panel;

    fn shape(&mut self) -> &mut ControlShape {
        &mut self.shape
    }

    fn create(self) -> Panel{
        self.create()
    }
}

pub struct PanelBuilderFromParameterGroup<P: ParameterGroup>{
    builder: PanelBuilder,
    group: P,
}

impl<P: ParameterGroup> PanelBuilderFromParameterGroup<P>{
    pub fn new<S: StreamT<'static, window::Event>>(name: &str, pos: Property<'static, Pnt2>, events: S, group: P) -> PanelBuilderFromParameterGroup<P>{
        let builder = PanelBuilder::new(name, pos, events);
        PanelBuilderFromParameterGroup{
            builder,
            group,
        }
    }
}

impl<P: ParameterGroup> ControlBuilder for PanelBuilderFromParameterGroup<P> {
    type Control = Panel;

    fn shape(&mut self) -> &mut ControlShape {
        self.builder.shape()
    }

    fn create(self) -> Panel{
        let mut panel = self.builder.create();
        self.group.add_parameters(&mut panel);
        panel
    }
}

impl Panel{
    pub fn new(name: &str, pos: Pnt2, width: f32, events: Stream<'static,window::Event>) -> Panel{
        PanelBuilder::new(name, Property::new(pos), events)
            .width(width)
            .create()
    }
}

impl ControlGeometry for Panel{
    fn flexible() -> bool{
        true
    }

    fn geometry(&self, container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DColor>>{
        self.rendered.send(());
        let mut geometry = self.geometry.to_value();
        if let Some(group_geometry) = self.group.geometry(container){
            geometry.extend(group_geometry);
        }
        Some(geometry)
    }

    fn text_geometry(&self, container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DTex>>{
        let mut text_geometry = (*self.text_geometry).clone();
        if let Some(group_geometry) = self.group.text_geometry(container){
            text_geometry.extend_from_slice(&group_geometry);
        }
        Some(text_geometry)
    }

    fn texture_geometry(&self, _container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DTex>>{
        None
    }

    fn font(&self) -> Option<&Ttf>{
        Some(&self.font)
    }

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

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

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

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

impl ControlEvents for Panel{
    fn non_attended(&self) -> StreamRc<'static, window::Event>{
        self.group.non_attended()
    }
}

impl Control for Panel{
    fn name(&self) -> &str{
        self.group.name()
    }
}

impl Deref for Panel{
    type Target = GroupV;
    fn deref(&self) -> &GroupV{
        &self.group
    }
}

impl DerefMut for Panel{
    fn deref_mut(&mut self) -> &mut GroupV{
        &mut self.group
    }
}

impl<P: ParameterGroup> ControlDefaultProperty for P {
    type Builder = PanelBuilderFromParameterGroup<P>;
}

impl<P: ParameterGroup> BuilderFromProperty<P> for PanelBuilderFromParameterGroup<P> {
    fn from_parameter<S: StreamT<'static, window::Event>>(parameter: P, name: &str, events: S) -> PanelBuilderFromParameterGroup<P> {
        PanelBuilderFromParameterGroup::new(name, Property::new(pnt2!(0.)), events, parameter)
    }
}


// Panel from Rgb property, almost works but the values are not propagated to the original property
// pub struct Color<T: Clone + Debug + 'static>{
//     r: RangedParameterMut<'static, T>,
//     g: RangedParameterMut<'static, T>,
//     b: RangedParameterMut<'static, T>,
//     _p: StreamRc<'static, Rgb<T>>,
// }

// impl<T: Clone + Debug + 'static> ParameterGroup for Color<T>
//     where T: FromPrimitive + Sub<Output=T> + Clone + ToPrimitive + Display + PartialEq + Debug + 'static
// {
//     fn add_parameters(&self, group: &mut ::Group){
//         group.add("r", self.r.clone());
//         group.add("g", self.g.clone());
//         group.add("b", self.b.clone());
//     }

//     fn set(&mut self, other: &Self){
//         self.r.set(other.r.to_value());
//         self.g.set(other.g.to_value());
//         self.b.set(other.b.to_value());
//     }
// }

// impl<T> ControlDefaultProperty for Property<'static, Rgb<T>>
//     where T: Channel + Zero + FromPrimitive + Sub<Output=T> + Clone + ToPrimitive + Display + PartialEq + Debug + 'static
// {
//     type Builder = PanelBuilderFromParameterGroup<Color<T>>;
// }

// impl<T> BuilderFromProperty<Property<'static, Rgb<T>>> for PanelBuilderFromParameterGroup<Color<T>>
//     where T: Channel + Zero + FromPrimitive + Sub<Output=T> + Clone + ToPrimitive + Display + PartialEq + Debug + 'static
// {
//     fn from_parameter<S: StreamT<'static, window::Event>>(mut color: Property<'static, Rgb<T>>, name: &str, events: S) -> PanelBuilderFromParameterGroup<Color<T>> {
//         let r = color.clone().map(|c| c.r);
//         let g = color.clone().map(|c| c.g);
//         let b = color.clone().map(|c| c.b);
//         let _p = r.clone()
//             .zip(g.clone())
//             .zip(b.clone())
//             .flatten()
//             .map(|(r, g, b)| rgb!(r,g,b))
//             .stream()
//             .connect_to_property(&mut color);

//         let r = RangedParameterMut::from_property(r, zero() .. <T as Channel>::max());
//         let g = RangedParameterMut::from_property(g, zero() .. <T as Channel>::max());
//         let b = RangedParameterMut::from_property(b, zero() .. <T as Channel>::max());
//         PanelBuilderFromParameterGroup::new(name, Property::new(pnt2!(0.)), events, Color{r,g,b,_p})
//     }
// }

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

    impl gl::Render3d for super::Panel{
        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);
            }
        }
    }
}