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
use rin::graphics::*;
use rin::events::*;
use rin::math::*;
use num_traits::{FromPrimitive,ToPrimitive};
use std::ops::Sub;
use rin::window;
use rin::window::events::*;
use std::fmt::Display;
use std::rc::Rc;
use traits::*;
use rin::color::*;
use rin::color::consts::*;
use std::fmt::Debug;
use rin::events::{RangedParameterMut, ConnectToParameter};
use utils;
use control_shape::ControlShape;

pub struct Slider<T: Debug + Clone + 'static>{
    label: String,
    parameter: RangedParameterMut<'static, T>,
    rendered: Sender<'static, ()>,
    value: StreamRc<'static, T>,
    font: Rc<Ttf>,
    color: Rgb<f32>,
    non_attended: StreamRc<'static, window::Event>,
    geometry: Property<'static, Vec<Vertex2DColor>>,
    text_geometry: Property<'static, Vec<Vertex2DTex>>,
    width: Property<'static, f32>,
    height: Property<'static, f32>,
    position: Property<'static, Pnt2>,
}

pub struct SliderBuilder<T: Clone + Debug + 'static>{
    label: String,
    parameter: RangedParameterMut<'static, T>,
    events: StreamRc<'static, window::Event>,
    shape: ControlShape,
}

fn geometry(rect: Rect<f32>, pct: f32, color: Rgb<f32>, alpha: f32) -> Vec<Vertex2DColor>{
    let x = rect.pos.x;
    let y = rect.pos.y;
    let w = rect.width;
    let h = rect.height;
    let w_value = clamp(pct as f32, 0., 1.) * w;
    let border_color = &rgba!(color, 0.1);
    let slider_color = &rgba!(color, alpha);
    vec![
        // background
        vertex2dcolor(vec2(x,y), border_color),
        vertex2dcolor(vec2(x+w,y), border_color),
        vertex2dcolor(vec2(x+w,y+h), border_color),

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

        // slider
        vertex2dcolor(vec2(x,y), slider_color),
        vertex2dcolor(vec2(x+w_value,y), slider_color),
        vertex2dcolor(vec2(x+w_value,y+h), slider_color),

        vertex2dcolor(vec2(x+w_value,y+h), slider_color),
        vertex2dcolor(vec2(x,y+h), slider_color),
        vertex2dcolor(vec2(x,y), slider_color),
    ]
}

impl<T: FromPrimitive + Sub<Output=T> + Clone + ToPrimitive + Display + PartialEq + Debug + 'static> SliderBuilder<T>{
    pub fn new<S: StreamT<'static, window::Event>>(name: &str, parameter: RangedParameterMut<'static, T>, events: S) -> SliderBuilder<T>{
        let pos = Property::new(origin());
        let width = Property::new(200.);
        let height = Property::new(15.);
        let margin = 0.;
        SliderBuilder{
            label: name.to_string(),
            parameter: parameter,
            events: events.rc(),
            shape: ControlShape::new(pos, width, height, margin, margin, WHITE),
        }
    }

    pub fn create(mut self) -> Slider<T>{
        // if the slider wasn't rendered in the last frame
        // do nothing
        let (rendered_sender, rendered_stream) = utils::rendered_stream(self.events.clone().unique());
        let rendered = rendered_stream.to_property(false);

        let events = self.events.clone();

        // slider box and font
        let width = self.shape.width();
        let height = self.shape.height();
        let position = self.shape.position();
        let rect = position.clone()
            .zip(width.clone())
            .zip(height.clone())
            .flatten()
            .map(|(pos, width, height)| Rect{pos, width, height});
        let font = self.shape.font().unwrap_or_else(|| ::utils::default_font());
        let rect64: Property<'_, Rect<f64>> = rect.clone().map(|r| convert(r));

        // mouse moved over slider
        let over = {
            let rect = rect64.clone();
            let rendered = rendered.clone();
            events.clone().mouse().moved()
                .map(move |pos| *rendered && pos.inside(&rect))
                .to_property(false)
        };

        // then pressed -> active
        let pressed = events.clone().mouse().pressed(MouseButton::Left);
        let pressed_over = pressed
            .filter_by(over.clone())
            .rc();

        // active until it's released
        let released = events.clone().mouse().released(MouseButton::Left).to_bool().not();
        let over_and_pressed = pressed_over.clone()
            .to_bool()
            .merge(released)
            .to_property(false);

        // value from mouse motion when active
        let value = {
            let rect = rect64;
            events.clone().mouse().moved()
                .filter_by(over_and_pressed.clone())
                .merge(pressed_over.map(|(pos,_button)| pos))
                .map(move |pos| clamp((pos.x - rect.pos.x) / rect.width, 0., 1.) )
                .connect_to_ranged_parameter_pct(&mut self.parameter)
        };

        // if over or active filter mouse events
        // so they don't propagate
        let over_copy = over.clone();
        let over_and_pressed_copy = over_and_pressed.clone();
        let non_attended = self.events.clone()
            .filter(move |e| {
                if let window::Event::MousePressed{..} = e {
                    !*over_copy && !*over_and_pressed_copy
                }else{
                    true
                }
            });


        // filter update with rendered stream
        // to use it to filter the geometry generation
        // in case the gui is not being rendered
        let filtered_update = self.events.clone()
            .filter_by(rendered)
            .update()
            .rc();

        // geometry property changes only if value
        // or alpha changes and was rendered on last frame
        let parameter = self.parameter.clone();
        let color = self.shape.color();
        let alpha = over_and_pressed.zip(over).map(|(over_and_pressed, over)|{
            if over_and_pressed {0.9} else if over {0.7} else {0.5}
        });
        let rect1 = rect.clone();
        let geometry = filtered_update.clone()
            .map(move |_| (parameter.pct() as f32, *alpha, *rect1))
            .dedup()
            .map(move |(pct, alpha, rect)| {
                geometry(rect, pct, color, alpha)
            }).to_property(vec![]);


        // text geometry property changes only if
        // value changesVertex2DTexture
        let font_copy = font.clone();
        let label = self.label.to_string();
        let max = self.parameter.max();
        let parameter = self.parameter.clone();
        let text_geometry = if self.shape.label_visible(){
            filtered_update
                .map(move |_| (parameter.to_value(), *rect))
                .dedup()
                .map(move |(value, rect)| {
                    let x = rect.pos.x;
                    let y = rect.pos.y + rect.height + 15.;
                    let total_w = rect.width;
                    let value_w = font_copy.bounding_box(&format!(": {}", max), x, y, CoordinateOrigin::TopLeft).width;
                    let label = utils::shorten(&label, &font_copy, total_w - value_w);
                    let text = format!("{}: {}", label, value);
                    let mesh = font_copy.mesh(&text, x, y, CoordinateOrigin::TopLeft).into();
                    mesh
                }).to_property(vec![])
        }else{
            Property::default()
        };


        let height = if self.shape.label_visible(){
            let line_height = font.line_height();
            height.map(move |h| h + line_height)
        }else{
            height
        };

        Slider{
            label: self.label.to_string(),
            parameter: self.parameter.clone(),
            value: value.rc(),
            rendered: rendered_sender,
            font: font,
            color: self.shape.color(),
            non_attended: non_attended.rc(),
            geometry: geometry,
            text_geometry: text_geometry,
            width,
            height,
            position,
        }
    }
}

impl<T: Clone + Debug + 'static + FromPrimitive + ToPrimitive + Sub<Output=T>> Slider<T>{
    pub fn stream(&self) -> Stream<'static, T>{
        let parameter = self.parameter.clone();
        self.value.clone().map(move |_| parameter.to_value())
    }

    pub fn set_parameter(&mut self, parameter: RangedParameterMut<'static, T>){
        //self.value.connect_to_ranged_parameter_pct(&mut parameter);
        self.parameter = parameter;
    }
}

impl<T: Debug + Clone + 'static> ControlGeometry for Slider<T>{
    fn flexible() -> bool{
        true
    }

    fn geometry(&self, _container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DColor>>{
        self.rendered.send(());
        Some(self.geometry.to_value())
    }

    fn text_geometry(&self, _container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DTex>>{
        Some(self.text_geometry.to_value())
    }

    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.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<T: Debug + Clone + 'static> ControlEvents for Slider<T>{
    fn non_attended(&self) -> StreamRc<'static, window::Event>{
        self.non_attended.clone()
    }
}

use std::any::Any;
impl<T: Any + Debug + 'static + Clone> Control for Slider<T>{
    fn name(&self) -> &str{
        &self.label
    }
}


impl<T: Debug> ControlBuilder for SliderBuilder<T>
    where T: FromPrimitive + Sub<Output=T> + Clone + ToPrimitive + Display + PartialEq + Debug + 'static{
    type Control = Slider<T>;

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

    fn create(self) -> Slider<T>{
        self.create()
    }
}

impl<T: Debug> ControlBuilt<RangedParameterMut<'static, T>> for Slider<T>
    where T: FromPrimitive + Sub<Output=T> + Clone + ToPrimitive + Display + PartialEq + Debug + 'static{
    type Builder = SliderBuilder<T>;
}

impl<T: Debug> ControlDefaultProperty for RangedParameterMut<'static, T>
    where T: FromPrimitive + Sub<Output=T> + Clone + ToPrimitive + Display + PartialEq + Debug + 'static{
    type Builder = SliderBuilder<T>;
}

impl<T: Debug> BuilderFromProperty<RangedParameterMut<'static, T>> for SliderBuilder<T>
    where T: FromPrimitive + Sub<Output=T> + Clone + ToPrimitive + Display + PartialEq + Debug + 'static{
    fn from_parameter<S: StreamT<'static, window::Event>>(parameter: RangedParameterMut<'static, T>, name: &str, events: S) -> SliderBuilder<T>{
        SliderBuilder::new(name, parameter, events)
    }
}

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

    impl<T: Debug + Clone> gl::Render3d for super::Slider<T>{
        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);
            if self.rect().inside(&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);
                }
            }
        }
    }
}