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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
use std::cell::Cell;
use std::cell::RefCell;
use std::cell::RefMut;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::SystemTime;
use Draw;
use Matrix;
use WidgetId;
use animations::Interpolation;
pub fn draw() -> SharedDrawContext {
SharedDrawContext {
shared1: Arc::new(Shared1 {
next_widget_id: AtomicUsize::new(1),
cursor_hovered_widget: AtomicBool::new(false),
})
}
}
pub struct SharedDrawContext {
shared1: Arc<Shared1>,
}
impl SharedDrawContext {
pub fn draw<'b, D: ?Sized + Draw + 'b>(&self, width: f32, height: f32, draw: &'b mut D,
cursor: Option<[f32; 2]>, cursor_was_pressed: bool,
cursor_was_released: bool) -> DrawContext<'b, D>
{
DrawContext {
matrix: Matrix::identity(),
width: width,
height: height,
animation: None,
cursor: cursor,
cursor_was_pressed: cursor_was_pressed,
cursor_was_released: cursor_was_released,
shared1: self.shared1.clone(),
shared2: Rc::new(Shared2 {
draw: RefCell::new(draw),
cursor_hovered_widget: Cell::new(false),
}),
}
}
#[inline]
pub fn cursor_hovered_widget(&self) -> bool {
self.shared1.cursor_hovered_widget.load(Ordering::Relaxed)
}
}
struct Shared1 {
next_widget_id: AtomicUsize,
cursor_hovered_widget: AtomicBool,
}
pub struct DrawContext<'b, D: ?Sized + Draw + 'b> {
shared1: Arc<Shared1>,
shared2: Rc<Shared2<'b, D>>,
matrix: Matrix,
width: f32,
height: f32,
animation: Option<(Matrix, f32)>,
cursor: Option<[f32; 2]>,
cursor_was_pressed: bool,
cursor_was_released: bool,
}
struct Shared2<'a, D: ?Sized + Draw + 'a> {
draw: RefCell<&'a mut D>,
cursor_hovered_widget: Cell<bool>,
}
impl<'b, D: ?Sized + Draw + 'b> DrawContext<'b, D> {
#[inline]
#[doc(hidden)]
pub fn draw(&self) -> RefMut<&'b mut D> {
self.shared2.draw.borrow_mut()
}
#[inline]
pub fn matrix(&self) -> Matrix {
if let Some((matrix, percent)) = self.animation {
#[inline]
fn lerp(a: f32, b: f32, f: f32) -> f32 { a + (b - a) * f }
let matrix = matrix.0;
let my_m = self.matrix.0;
Matrix([
[lerp(matrix[0][0], my_m[0][0], percent), lerp(matrix[0][1], my_m[0][1], percent)],
[lerp(matrix[1][0], my_m[1][0], percent), lerp(matrix[1][1], my_m[1][1], percent)],
[lerp(matrix[2][0], my_m[2][0], percent), lerp(matrix[2][1], my_m[2][1], percent)]
])
} else {
self.matrix
}
}
#[inline]
pub fn cursor_was_pressed(&self) -> bool {
self.cursor_was_pressed
}
#[inline]
pub fn cursor_was_released(&self) -> bool {
self.cursor_was_released
}
#[inline]
pub fn cursor_hovered_widget(&self) -> bool {
self.shared2.cursor_hovered_widget.get()
}
#[inline]
pub fn set_cursor_hovered_widget(&self) {
self.shared1.cursor_hovered_widget.store(true, Ordering::Relaxed);
self.shared2.cursor_hovered_widget.set(true);
}
#[inline]
pub fn reserve_widget_id(&self) -> WidgetId {
self.shared1.next_widget_id.fetch_add(1, Ordering::Relaxed).into()
}
#[inline]
pub fn is_cursor_hovering(&self) -> bool {
fn test(matrix: &Matrix, point: &[f32; 2]) -> bool {
let top_left = *matrix * [-1.0, 1.0, 1.0];
let top_left = [top_left[0] / top_left[2], top_left[1] / top_left[2]];
let top_right = *matrix * [1.0, 1.0, 1.0];
let top_right = [top_right[0] / top_right[2], top_right[1] / top_right[2]];
let bot_left = *matrix * [-1.0, -1.0, 1.0];
let bot_left = [bot_left[0] / bot_left[2], bot_left[1] / bot_left[2]];
let bot_right = *matrix * [1.0, -1.0, 1.0];
let bot_right = [bot_right[0] / bot_right[2], bot_right[1] / bot_right[2]];
if (point[0] - top_left[0]) * (top_right[0] - top_left[0]) +
(point[1] - top_left[1]) * (top_right[1] - top_left[1]) < 0.0
{
return false;
}
if (point[0] - top_right[0]) * (bot_right[0] - top_right[0]) +
(point[1] - top_right[1]) * (bot_right[1] - top_right[1]) < 0.0
{
return false;
}
if (point[0] - bot_right[0]) * (bot_left[0] - bot_right[0]) +
(point[1] - bot_right[1]) * (bot_left[1] - bot_right[1]) < 0.0
{
return false;
}
if (point[0] - bot_left[0]) * (top_left[0] - bot_left[0]) +
(point[1] - bot_left[1]) * (top_left[1] - bot_left[1]) < 0.0
{
return false;
}
true
}
if let Some(cursor) = self.cursor {
test(&self.matrix(), &cursor)
} else {
false
}
}
pub fn cursor_hover_coordinates(&self) -> Option<[f32; 2]> {
let m = match self.matrix().invert() {
Some(m) => m,
None => return None,
};
let in_pos = match self.cursor {
Some(p) => p,
None => return None,
};
let output_mouse = [
in_pos[0]*m[0][0] + in_pos[1]*m[1][0] + m[2][0],
in_pos[0]*m[0][1] + in_pos[1]*m[1][1] + m[2][1],
in_pos[0]*m[0][2] + in_pos[1]*m[1][2] + m[2][2],
];
let output_mouse = [output_mouse[0] / output_mouse[2], output_mouse[1] / output_mouse[2]];
if output_mouse[0] < -1.0 || output_mouse[0] > 1.0 || output_mouse[0] != output_mouse[0] ||
output_mouse[1] < -1.0 || output_mouse[1] > 1.0 || output_mouse[1] != output_mouse[1]
{
return None;
}
Some(output_mouse)
}
#[inline]
pub fn width_per_height(&self) -> f32 {
self.width / self.height
}
#[inline]
pub fn margin(&self, top: f32, right: f32, bottom: f32, left: f32) -> DrawContext<'b, D> {
DrawContext {
matrix: self.matrix * Matrix::translate(left - right, bottom - top)
* Matrix::scale_wh(1.0 - right - left, 1.0 - top - bottom),
width: self.width * (1.0 - left - right),
height: self.height * (1.0 - top - bottom),
shared1: self.shared1.clone(),
shared2: self.shared2.clone(),
animation: self.animation,
cursor: self.cursor,
cursor_was_pressed: self.cursor_was_pressed,
cursor_was_released: self.cursor_was_released,
}
}
#[inline]
pub fn uniform_margin(&self, top: f32, right: f32, bottom: f32, left: f32)
-> DrawContext<'b, D>
{
let wph = self.width_per_height();
let wph = if wph < 1.0 { 1.0 } else { wph };
let hpw = 1.0 / self.width_per_height();
let hpw = if hpw < 1.0 { 1.0 } else { hpw };
self.margin(top / hpw, right / wph, bottom / hpw, left / wph)
}
pub fn enforce_aspect_ratio_downscale(&self, width_per_height: f32, alignment: &Alignment)
-> DrawContext<'b, D>
{
let current_width_per_height = self.width_per_height();
if width_per_height > current_width_per_height {
let alignment = alignment.vertical;
self.vertical_rescale(current_width_per_height / width_per_height, &alignment)
} else {
let alignment = alignment.horizontal;
self.horizontal_rescale(width_per_height / current_width_per_height, &alignment)
}
}
pub fn enforce_aspect_ratio_upscale(&self, width_per_height: f32, alignment: &Alignment)
-> DrawContext<'b, D>
{
let current_width_per_height = self.width_per_height();
if width_per_height > current_width_per_height {
let alignment = alignment.horizontal;
self.horizontal_rescale(width_per_height / current_width_per_height, &alignment)
} else {
let alignment = alignment.vertical;
self.vertical_rescale(current_width_per_height / width_per_height, &alignment)
}
}
#[inline]
pub fn vertical_rescale(&self, scale: f32, alignment: &VerticalAlignment)
-> DrawContext<'b, D>
{
let y = match alignment {
&VerticalAlignment::Center => 0.0,
&VerticalAlignment::Bottom => scale - 1.0,
&VerticalAlignment::Top => 1.0 - scale,
};
DrawContext {
matrix: self.matrix * Matrix::translate(0.0, y) * Matrix::scale_wh(1.0, scale),
width: self.width,
height: self.height * scale,
animation: self.animation,
shared1: self.shared1.clone(),
shared2: self.shared2.clone(),
cursor: self.cursor,
cursor_was_pressed: self.cursor_was_pressed,
cursor_was_released: self.cursor_was_released,
}
}
#[inline]
pub fn horizontal_rescale(&self, scale: f32, alignment: &HorizontalAlignment)
-> DrawContext<'b, D>
{
let x = match alignment {
&HorizontalAlignment::Center => 0.0,
&HorizontalAlignment::Left => scale - 1.0,
&HorizontalAlignment::Right => 1.0 - scale,
};
DrawContext {
matrix: self.matrix * Matrix::translate(x, 0.0) * Matrix::scale_wh(scale, 1.0),
width: self.width * scale,
height: self.height,
animation: self.animation,
shared1: self.shared1.clone(),
shared2: self.shared2.clone(),
cursor: self.cursor,
cursor_was_pressed: self.cursor_was_pressed,
cursor_was_released: self.cursor_was_released,
}
}
#[inline]
pub fn vertical_split<'a>(&'a self, splits: usize) -> SplitsIter<'a, 'b, OneGen, D> {
let iter = OneGen { n: splits };
self.vertical_split_weights(iter)
}
#[inline]
pub fn vertical_split_weights<'a, I>(&'a self, weights: I) -> SplitsIter<'a, 'b, I::IntoIter, D>
where I: IntoIterator<Item = f32>, I::IntoIter: ExactSizeIterator + Clone
{
self.split_weights(weights.into_iter(), true)
}
#[inline]
pub fn horizontal_split<'a>(&'a self, splits: usize) -> SplitsIter<'a, 'b, OneGen, D> {
let iter = OneGen { n: splits };
self.horizontal_split_weights(iter)
}
#[inline]
pub fn horizontal_split_weights<'a, I>(&'a self, weights: I) -> SplitsIter<'a, 'b, I::IntoIter, D>
where I: IntoIterator<Item = f32>, I::IntoIter: ExactSizeIterator + Clone
{
self.split_weights(weights.into_iter(), false)
}
#[inline]
fn split_weights<'a, I>(&'a self, weights: I, vertical: bool) -> SplitsIter<'a, 'b, I, D>
where I: ExactSizeIterator<Item = f32> + Clone
{
assert!(weights.len() != 0);
let total_weight = weights.clone().fold(0.0, |a, b| a + b);
let total_weight_inverse = 1.0 / total_weight;
SplitsIter {
parent: self,
weights: weights,
total_weight_inverse: total_weight_inverse,
current_offset: 0.0,
vertical: vertical,
}
}
pub fn rescale(&self, width_percent: f32, height_percent: f32, alignment: &Alignment)
-> DrawContext<'b, D>
{
let x = match alignment.horizontal {
HorizontalAlignment::Center => 0.0,
HorizontalAlignment::Left => width_percent - 1.0,
HorizontalAlignment::Right => 1.0 - width_percent,
};
let y = match alignment.vertical {
VerticalAlignment::Center => 0.0,
VerticalAlignment::Bottom => height_percent - 1.0,
VerticalAlignment::Top => 1.0 - height_percent,
};
DrawContext {
matrix: self.matrix * Matrix::translate(x, y)
* Matrix::scale_wh(width_percent, height_percent),
width: self.width * width_percent,
height: self.height * height_percent,
animation: self.animation,
shared1: self.shared1.clone(),
shared2: self.shared2.clone(),
cursor: self.cursor,
cursor_was_pressed: self.cursor_was_pressed,
cursor_was_released: self.cursor_was_released,
}
}
#[inline]
pub fn animation_start<I>(&self, interpolation: I, start_time: SystemTime, duration: Duration)
-> DrawContext<'b, D>
where I: Interpolation
{
let now = SystemTime::now();
let interpolation = interpolation.calculate(now, start_time, duration) as f32;
let current_matrix = self.matrix();
DrawContext {
matrix: self.matrix,
width: self.width,
height: self.height,
animation: Some((current_matrix, interpolation)),
shared1: self.shared1.clone(),
shared2: self.shared2.clone(),
cursor: self.cursor,
cursor_was_pressed: self.cursor_was_pressed,
cursor_was_released: self.cursor_was_released,
}
}
#[inline]
pub fn animation_stop(&self) -> DrawContext<'b, D> {
DrawContext {
matrix: self.matrix(),
width: self.width,
height: self.height,
animation: None,
shared1: self.shared1.clone(),
shared2: self.shared2.clone(),
cursor: self.cursor,
cursor_was_pressed: self.cursor_was_pressed,
cursor_was_released: self.cursor_was_released,
}
}
}
impl<'a, 'b, D: ?Sized + Draw + 'b> Clone for DrawContext<'b, D> {
fn clone(&self) -> DrawContext<'b, D> {
DrawContext {
matrix: self.matrix.clone(),
width: self.width.clone(),
height: self.height.clone(),
animation: self.animation.clone(),
shared1: self.shared1.clone(),
shared2: self.shared2.clone(),
cursor: self.cursor.clone(),
cursor_was_pressed: self.cursor_was_pressed,
cursor_was_released: self.cursor_was_released,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Alignment {
pub horizontal: HorizontalAlignment,
pub vertical: VerticalAlignment,
}
impl Alignment {
#[inline]
pub fn center() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Center,
vertical: VerticalAlignment::Center,
}
}
#[inline]
pub fn bottom() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Center,
vertical: VerticalAlignment::Bottom,
}
}
#[inline]
pub fn top() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Center,
vertical: VerticalAlignment::Top,
}
}
#[inline]
pub fn right() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Right,
vertical: VerticalAlignment::Center,
}
}
#[inline]
pub fn left() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Left,
vertical: VerticalAlignment::Center,
}
}
#[inline]
pub fn top_left() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Left,
vertical: VerticalAlignment::Top,
}
}
#[inline]
pub fn top_right() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Right,
vertical: VerticalAlignment::Top,
}
}
#[inline]
pub fn bottom_right() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Right,
vertical: VerticalAlignment::Bottom,
}
}
#[inline]
pub fn bottom_left() -> Alignment {
Alignment {
horizontal: HorizontalAlignment::Left,
vertical: VerticalAlignment::Bottom,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum HorizontalAlignment {
Center,
Left,
Right,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum VerticalAlignment {
Center,
Top,
Bottom,
}
pub struct SplitsIter<'a, 'b: 'a, I, D: ?Sized + Draw + 'b> {
parent: &'a DrawContext<'b, D>,
weights: I,
total_weight_inverse: f32,
current_offset: f32,
vertical: bool,
}
impl<'a, 'b: 'a, I, D: ?Sized + Draw + 'b> Iterator for SplitsIter<'a, 'b, I, D>
where I: Iterator<Item = f32>
{
type Item = DrawContext<'b, D>;
fn next(&mut self) -> Option<Self::Item> {
let weight = match self.weights.next() {
Some(w) => w,
None => return None
};
let new_width = if !self.vertical { self.parent.width * weight * self.total_weight_inverse }
else { self.parent.width };
let new_height = if self.vertical { self.parent.height * weight * self.total_weight_inverse }
else { self.parent.height };
let scale_matrix = if self.vertical {
Matrix::scale_wh(1.0, weight * self.total_weight_inverse)
} else {
Matrix::scale_wh(weight * self.total_weight_inverse, 1.0)
};
let pos_matrix = if self.vertical {
let y = 1.0 - 2.0 * (self.current_offset + weight * 0.5) * self.total_weight_inverse;
Matrix::translate(0.0, y)
} else {
let x = 2.0 * (self.current_offset + weight * 0.5) * self.total_weight_inverse - 1.0;
Matrix::translate(x, 0.0)
};
self.current_offset += weight;
Some(DrawContext {
matrix: self.parent.matrix * pos_matrix * scale_matrix,
width: new_width,
height: new_height,
animation: self.parent.animation,
shared1: self.parent.shared1.clone(),
shared2: self.parent.shared2.clone(),
cursor: self.parent.cursor,
cursor_was_pressed: self.parent.cursor_was_pressed,
cursor_was_released: self.parent.cursor_was_released,
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.weights.size_hint()
}
}
impl<'a, 'b: 'a, I, D: ?Sized + Draw + 'b> ExactSizeIterator for SplitsIter<'a, 'b, I, D>
where I: ExactSizeIterator<Item = f32>
{
}
#[derive(Debug, Clone)]
pub struct OneGen {
n: usize
}
impl Iterator for OneGen {
type Item = f32;
#[inline]
fn next(&mut self) -> Option<f32> {
if self.n == 0 { return None; }
self.n -= 1;
Some(1.0)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.n, Some(self.n))
}
}
impl ExactSizeIterator for OneGen {
}