-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathpiano_range.rs
126 lines (113 loc) · 4.18 KB
/
piano_range.rs
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
use iced_core::{
border::{Border, Radius},
renderer::Quad,
Background, Color, Length, Rectangle, Size, Theme, Vector, Widget,
};
use super::Element;
pub struct PianoRange(pub std::ops::RangeInclusive<u8>);
impl<M, R: iced_core::Renderer> Widget<M, Theme, R> for PianoRange {
fn size(&self) -> Size<Length> {
Size {
width: iced_core::Length::Fill,
height: iced_core::Length::Fixed(100.0),
}
}
fn layout(
&self,
_tree: &mut iced_core::widget::Tree,
_renderer: &R,
limits: &iced_core::layout::Limits,
) -> iced_core::layout::Node {
let size = Widget::<M, Theme, R>::size(self);
iced_core::layout::atomic(limits, size.width, size.height)
}
fn draw(
&self,
_tree: &iced_core::widget::Tree,
renderer: &mut R,
_theme: &Theme,
_style: &iced_core::renderer::Style,
layout: iced_core::Layout<'_>,
_cursor: iced_core::mouse::Cursor,
_viewport: &iced_core::Rectangle,
) {
let bounds = layout.bounds();
renderer.with_translation(Vector::new(bounds.x, bounds.y), |renderer| {
let range = piano_math::KeyboardRange::new(self.0.clone());
let white_count = range.white_count();
let neutral_width = bounds.width / white_count as f32;
let neutral_height = bounds.height;
let layout =
piano_math::KeyboardLayout::from_range(neutral_width, neutral_height, range);
let mut neutral = layout
.keys
.iter()
.filter(|key| key.kind().is_neutral())
.enumerate()
.peekable();
while let Some((n, key)) = neutral.next() {
let bounds = Rectangle {
x: key.x(),
y: 0.0,
width: key.width(),
height: key.height(),
};
renderer.fill_quad(
Quad {
bounds,
border: Border {
radius: if n == 0 {
Radius::new(0.0)
.top_left(12.0)
.top_right(0.0)
.bottom_right(5.0)
.bottom_left(12.0)
} else if neutral.peek().is_none() {
Radius::new(0.0)
.top_left(0.0)
.top_right(12.0)
.bottom_right(12.0)
.bottom_left(5.0)
} else {
Radius::new(0.0)
.top_left(0.0)
.top_right(0.0)
.bottom_right(5.0)
.bottom_left(5.0)
},
width: 0.0,
color: Color::TRANSPARENT,
},
shadow: Default::default(),
},
Background::Color(Color::WHITE),
);
}
for key in layout.keys.iter().filter(|key| key.kind().is_sharp()) {
let bounds = Rectangle {
x: key.x(),
y: 0.0,
width: key.width(),
height: key.height(),
};
renderer.fill_quad(
Quad {
bounds,
border: Border {
radius: Radius::default(),
width: 0.0,
color: Color::TRANSPARENT,
},
shadow: Default::default(),
},
Background::Color(Color::BLACK),
);
}
});
}
}
impl<'a, M: 'a> From<PianoRange> for Element<'a, M> {
fn from(value: PianoRange) -> Self {
Self::new(value)
}
}