fluffl 0.0.5

A cross-platform multimedia layer that exposes opengl,sockets,and audio utilities for desktop and browser
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use super::*;

pub struct SliderState {
    pub slider_button_key: GuiComponentKey,
    pub slider_frame: FrameState,
    pub percentage: f32,
}

impl SliderState {
    fn new() -> Self {
        Self {
            slider_button_key: GuiComponentKey::default(),
            slider_frame: FrameState::new(),
            percentage: 0.0,
        }
    }
}

impl GuiComponent for SliderState {
    fn common(&self) -> &GuiCommonState {
        self.slider_frame.common()
    }
    fn common_mut(&mut self) -> &mut GuiCommonState {
        self.slider_frame.common_mut()
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn is_visible(&self) -> bool {
        self.slider_frame.is_visible()
    }

    fn set_visible(&mut self, is_visible: bool) {
        self.slider_frame.set_visible(is_visible);
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn bounds(&self) -> Vec2<f32> {
        self.slider_frame.bounds()
    }

    fn set_bounds(&mut self, bounds: Vec2<f32>) {
        self.slider_frame.set_bounds(bounds);
    }

    fn render_entry<'a>(
        &mut self,
        gl: &GlowGL,
        state: RenderState<'a>,
        _text_writer: &mut TextWriter,
    ) {
        let win_w = state.win_w;
        let win_h = state.win_h;

        //makes sure whatever gets render is bound within the parent
        layer_lock(gl, state.level, *self.flags());

        state
            .renderer
            .builder(gl, GuiShaderKind::RoundedBox)
            .set_window(win_w, win_h)
            .set_background_color(self.slider_frame.color)
            .set_bounds(self.slider_frame.bounds())
            .set_edge_color(self.slider_frame.edge_color)
            .set_roundness_vec(self.slider_frame.roundness)
            .set_position(
                state.global_position,
                Vec4::convert(self.slider_frame.bounds()),
            )
            .render();

        layer_unlock(gl);
    }

    fn render_exit<'a>(
        &mut self,
        _gl: &GlowGL,
        _state: RenderState<'a>,
        _text_writer: &mut TextWriter,
    ) {
        /* not implemented on purpose  */
    }
}

pub struct SliderBuilder<'a, ProgramState> {
    manager: &'a mut GuiManager<ProgramState>,
    slider_frame_state: Option<SliderState>,
    slider_frame_key: Option<GuiComponentKey>,
    slider_button_state: Option<FrameState>,
    slider_button_key: Option<GuiComponentKey>,
    parent: Option<GuiComponentKey>,
    key: Option<GuiComponentKey>,
}

impl<'a, ProgramState> SliderBuilder<'a, ProgramState> {
    pub fn new(manager: &'a mut GuiManager<ProgramState>) -> Self {
        let slider_frame_key =
            unsafe { Some(manager.add_component_deferred(GuiComponentKey::default(), None)) };
        let slider_button_key =
            unsafe { Some(manager.add_component_deferred(GuiComponentKey::default(), None)) };

        let mut slider_state = SliderState::new();
        slider_state.slider_button_key = slider_button_key.unwrap();

        Self {
            manager,
            parent: None,
            slider_frame_state: Some(slider_state),
            slider_button_state: Some(FrameState::new()),
            slider_frame_key,
            slider_button_key,
            key: slider_frame_key,
        }
    }

    pub fn with_bounds<T: Into<Vec2<f32>>>(mut self, bounds: T) -> Self {
        self.slider_frame_state
            .as_mut()
            .unwrap()
            .slider_frame
            .set_bounds(bounds.into());
        self
    }

    pub fn with_percentage(mut self, percentage: f32) -> Self {
        self.slider_frame_state.as_mut().unwrap().percentage = percentage;
        self
    }

    pub fn with_position<T: Into<Vec2<f32>>>(mut self, rel_pos: T) -> Self {
        self.slider_frame_state
            .as_mut()
            .unwrap()
            .set_rel_position(rel_pos.into());

        self
    }

    pub fn with_color<T: Into<Vec4<f32>>>(mut self, color: T) -> Self {
        self.slider_frame_state.as_mut().unwrap().slider_frame.color = color.into();
        self
    }

    pub fn with_edge_color<T: Into<Vec4<f32>>>(mut self, color: T) -> Self {
        self.slider_frame_state
            .as_mut()
            .unwrap()
            .slider_frame
            .edge_color = color.into();
        self
    }

    pub fn with_roundness<T: Into<Vec4<f32>>>(mut self, roundness: T) -> Self {
        self.slider_frame_state
            .as_mut()
            .unwrap()
            .slider_frame
            .roundness = roundness.into();
        self
    }

    pub fn with_button_bounds<T: Into<Vec2<f32>>>(mut self, bounds: T) -> Self {
        self.slider_button_state
            .as_mut()
            .unwrap()
            .set_bounds(bounds.into());
        self
    }

    pub fn with_button_color<T: Into<Vec4<f32>>>(mut self, color: T) -> Self {
        self.slider_button_state.as_mut().unwrap().color = color.into();
        self
    }

    pub fn with_button_edge_color<T: Into<Vec4<f32>>>(mut self, color: T) -> Self {
        self.slider_button_state.as_mut().unwrap().edge_color = color.into();
        self
    }

    pub fn with_button_roundness<T: Into<Vec4<f32>>>(mut self, roundness: T) -> Self {
        self.slider_button_state.as_mut().unwrap().roundness = roundness.into();
        self
    }

    pub fn with_button_listener<CB>(self, kind: GuiEventKind, mut cb: CB) -> Self
    where
        CB: FnMut(&mut FrameState, EventKind, &mut MutationRequestQueue<ProgramState>) + 'static,
    {
        let slider_button_key = self.slider_button_key.expect("slider key not found");
        self.manager.push_listener(
            slider_button_key,
            ComponentEventListener::new(
                kind,
                Box::new(move |info| {
                    let slider_button_key = info.key;
                    let mutation_queue = info.mutation_queue;
                    let event = info.event;

                    let slider_button_state = info
                        .gui_comp_tree
                        .get_mut(slider_button_key)?
                        .as_any_mut()
                        .downcast_mut::<FrameState>()?;

                    cb(slider_button_state, event, mutation_queue);

                    None
                }),
            ),
        );
        self
    }
    pub fn with_button_listener_advanced<CB>(self, kind: GuiEventKind, mut cb: CB) -> Self
    where
        CB: FnMut(EventListenerInfo<'_, ProgramState>) + 'static,
    {
        let slider_button_key = self.slider_button_key.expect("slider key not found");
        self.manager.push_listener(
            slider_button_key,
            ComponentEventListener::new(
                kind,
                Box::new(move |info| {
                    cb(info);
                    None
                }),
            ),
        );
        self
    }
}

impl<'a, ProgramState> HasComponentBuilder<ProgramState> for SliderBuilder<'a, ProgramState> {
    type ComponentKind = SliderState;

    fn key(&mut self) -> &mut Option<GuiComponentKey> {
        &mut self.key
    }

    fn manager(&mut self) -> &mut GuiManager<ProgramState> {
        self.manager
    }

    fn parent(&mut self) -> &mut Option<GuiComponentKey> {
        &mut self.parent
    }

    fn state(&mut self) -> &mut Option<Self::ComponentKind> {
        &mut self.slider_frame_state
    }

    fn build(mut self) -> GuiComponentKey {
        let slider_frame_parent = self.parent.unwrap_or_default();
        let slider_frame_key = self.slider_frame_key.expect("slider_frame_key not set");
        let slider_button_key = self.slider_button_key.expect("slider_button_key");

        /*scope block so that all variables inside go out of scope when finished*/
        {
            let slider_frame_state = self
                .slider_frame_state
                .take()
                .expect("slider_frame_state not found");

            let mut slider_button_state = self
                .slider_button_state
                .take()
                .expect("slider button state not found");

            //used to clamp and verticially center slider_button
            let slider_frame_bounds = slider_frame_state.bounds();
            let mut slider_button_bounds = slider_button_state.bounds();

            //this clamps max height of button to be the parents height
            slider_button_bounds[1] = slider_button_bounds[1].clamp(0.0, slider_frame_bounds[1]);

            //assign newly clamped bounds to state
            slider_button_state.set_bounds(slider_button_bounds);

            //center slider
            slider_button_state.set_rel_position(Vec2::from([
                0.0,
                (slider_frame_bounds[1] - slider_button_bounds[1]) * 0.5,
            ]));

            //finally write components to the deferred tree nodes
            *self
                .manager
                .gui_component_tree
                .get_mut_uninit(slider_frame_key) = MaybeUninit::new(Box::new(slider_frame_state));

            *self
                .manager
                .gui_component_tree
                .get_mut_uninit(slider_button_key) =
                MaybeUninit::new(Box::new(slider_button_state));

            //set frame parent
            self.manager
                .gui_component_tree
                .set_parent(slider_frame_key, slider_frame_parent);

            //frame should be parent of button
            self.manager
                .gui_component_tree
                .set_parent(slider_button_key, slider_frame_key);
        }

        self.manager.push_listener(
            slider_button_key,
            ComponentEventListener::new(
                GuiEventKind::OnDrag,
                Box::new(|info| {
                    let disp = info.event.disp();
                    let tree = info.gui_comp_tree;
                    let slider_button_key = info.key;
                    let slider_frame_key = tree
                        .get_parent_id(slider_button_key)
                        .expect("slider_button SHOULD have a parent");

                    let frame_bounds = tree
                        .get(slider_frame_key)
                        .expect("slider_frame component")
                        .bounds();

                    let button_bounds = tree
                        .get(slider_button_key)
                        .expect("slider_button component")
                        .bounds();

                    //translate the slider like normal
                    tree.get_mut(slider_button_key)
                        .expect("slider_button")
                        .translate(disp);

                    //vertically center the slider on drag
                    let max_horizontal_rel_pos = frame_bounds[0] - button_bounds[0];
                    let button_slider_pos = *tree
                        .get(slider_button_key)
                        .expect("slider_button_key invalid")
                        .rel_position();

                    let vertically_centered_and_horizontally_clamped_relative_position =
                        Vec2::from([
                            button_slider_pos[0].clamp(0.0, max_horizontal_rel_pos),
                            frame_bounds[1] * 0.5 - button_bounds[1] * 0.5,
                        ]);

                    //set newly computed position
                    tree.get_mut(slider_button_key)
                        .expect("slider_button_key invalid")
                        .set_rel_position(
                            vertically_centered_and_horizontally_clamped_relative_position,
                        );

                    //update percentage
                    let new_percentage =
                        (button_slider_pos.x() / max_horizontal_rel_pos).clamp(0.0, 1.0);
                    tree.get_mut(slider_frame_key)
                        .expect("slider_frame_key invalid")
                        .as_any_mut()
                        .downcast_mut::<Self::ComponentKind>()
                        .expect("slider_frame_key should alias SliderState")
                        .percentage = new_percentage;

                    None
                }),
            ),
        );

        self.manager.push_listener(
            slider_frame_key,
            ComponentEventListener::new(
                GuiEventKind::OnWheelWhileHovered,
                Box::new(|info| {
                    let wheel = info.event.wheel();

                    let tree = info.gui_comp_tree;

                    let slider_frame_key = info.key;

                    let slider_button_key = tree
                        .get_mut(slider_frame_key)
                        .unwrap()
                        .as_any_mut()
                        .downcast_mut::<SliderState>()
                        .unwrap()
                        .slider_button_key;

                    let frame_bounds = tree
                        .get(slider_frame_key)
                        .expect("slider_frame component")
                        .bounds();

                    let button_bounds = tree
                        .get(slider_button_key)
                        .expect("slider_button component")
                        .bounds();

                    //move slider button,horizontally by increments of 5% of the parent bounds width
                    let wheel_dx = (frame_bounds[0] * 0.05) * wheel;

                    //translate the slider like normal
                    tree.get_mut(slider_button_key)
                        .expect("slider_button")
                        .translate(Vec2::from([wheel_dx, 0.0]));

                    //vertically center the slider on drag
                    let max_horizontal_rel_pos = frame_bounds[0] - button_bounds[0];
                    let button_slider_pos = *tree
                        .get(slider_button_key)
                        .expect("slider_button_key invalid")
                        .rel_position();

                    let vertically_centered_and_horizontally_clamped_relative_position =
                        Vec2::from([
                            button_slider_pos.x().clamp(0.0, max_horizontal_rel_pos),
                            frame_bounds.y() * 0.5 - button_bounds.y() * 0.5,
                        ]);

                    //set newly computed position
                    tree.get_mut(slider_button_key)
                        .expect("slider_button_key invalid")
                        .set_rel_position(
                            vertically_centered_and_horizontally_clamped_relative_position,
                        );

                    //update percentage
                    let new_percentage =
                        (button_slider_pos.x() / max_horizontal_rel_pos).clamp(0.0, 1.0);
                    tree.get_mut(slider_frame_key)
                        .expect("slider_frame_key invalid")
                        .as_any_mut()
                        .downcast_mut::<Self::ComponentKind>()
                        .expect("slider_frame_key should alias SliderState")
                        .percentage = new_percentage;

                    None
                }),
            ),
        );

        //pretty much all build implementations will have this
        self.manager.gui_component_tree.reconstruct_preorder();
        slider_frame_key
    }
}

impl<ProgramState> GuiManager<ProgramState> {
    pub fn builder_slider(&mut self) -> SliderBuilder<ProgramState> {
        SliderBuilder::new(self)
    }
}