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
//! Beat timing as the user interface shows it: the engine owns Link and the
//! tempo; this mirrors its state and turns the timing buttons into actions.
use egui::{Button, Color32, CornerRadius, Slider, Stroke, TextFormat, Ui, Vec2, text::LayoutJob};
use gled_protocol::state::ClientInput;
use gled_protocol::action::UiAction;
use crate::app::timing::BeatState;
/// The color of Ableton Link in the interface.
pub(crate) const LINK_ACTIVE_COLOR: Color32 = Color32::from_rgb(41, 116, 145);
#[derive(Debug, Default)]
pub(crate) struct ClientTiming {
beat: BeatState,
tap_count: usize,
/// The BPM slider's value while it is being dragged.
slider: Option<f32>,
}
impl ClientTiming {
/// Takes the beat timing the engine published.
pub(crate) fn update(&mut self, beat: BeatState, tap_count: usize) {
self.beat = beat;
self.tap_count = tap_count;
}
pub(crate) fn beat_progression(&self) -> f32 {
self.beat.beat_progression
}
pub(crate) fn beats_per_minute(&self) -> f32 {
self.beat.beats_per_minute
}
/// Frame rate of the render thread, `None` until its first report.
pub(crate) fn framerate(&self) -> Option<f32> {
(self.beat.framerate > 0.0).then_some(self.beat.framerate)
}
pub(crate) fn beat_state(&self) -> BeatState {
self.beat
}
/// The tempo slider. Changes go to the engine while it is dragged.
pub(crate) fn bpm_slider(&mut self, ui: &mut Ui, enabled: bool) {
let mut value = self.slider.unwrap_or(self.beat.beats_per_minute);
let response = ui.add_enabled(
enabled,
Slider::new(&mut value, 20.0..=999.0).custom_formatter(|n, _| format!("{n:.1} bpm")),
);
if response.changed() {
ClientInput::SetBeatsPerMinute(value).enqueue();
}
self.slider = (response.dragged() || response.has_focus()).then_some(value);
}
pub(crate) fn half_button(&self, ui: &mut Ui, ableton_link_read_only: bool) {
if ui
.add_enabled(!ableton_link_read_only, Button::new("x½"))
.clicked()
{
UiAction::SpeedMultiply(0.5).enqueue();
}
}
pub(crate) fn double_button(&self, ui: &mut Ui, ableton_link_read_only: bool) {
if ui
.add_enabled(!ableton_link_read_only, Button::new("x2"))
.clicked()
{
UiAction::SpeedMultiply(2.0).enqueue();
}
}
pub(crate) fn tap_button(
&self,
ui: &mut Ui,
menu_button_size: Vec2,
ableton_link_read_only: bool,
) {
ui.scope(|ui| {
ui.style_mut().visuals.widgets.inactive.bg_stroke =
ui.style().visuals.widgets.noninteractive.bg_stroke;
let underlined = TextFormat {
underline: Stroke::new(1.0, Color32::GRAY),
..Default::default()
};
let mut tap_text = LayoutJob::default();
tap_text.append("T", 0.0, underlined);
tap_text.append(
&format!(
"ap{}",
match (self.tap_count, self.tap_count % 4) {
(0, _) => "",
(_, 0) => "/",
(_, 1) => "–",
(_, 2) => "\\",
_ => "|",
}
),
0.0,
TextFormat::default(),
);
let response = ui.add_enabled(
!ableton_link_read_only,
Button::new(tap_text).min_size(menu_button_size),
);
let mut alpha = None;
let bar_progression = self.beat.beat_progression % 1.0;
if bar_progression < 0.10 {
alpha = Some(30.0);
} else if bar_progression < 0.20 {
alpha = Some(20.0 - ((bar_progression - 0.1) * 200.0));
} else if bar_progression > 0.9 {
alpha = Some((bar_progression - 0.9) * 200.0);
}
if let Some(alpha) = alpha {
ui.painter().rect_filled(
match self.beat_flank() {
0 =>
// top left
{
response
.rect
.split_left_right_at_fraction(0.5)
.0
.split_top_bottom_at_fraction(0.5)
.0
}
1 =>
// top right
{
response
.rect
.split_left_right_at_fraction(0.5)
.1
.split_top_bottom_at_fraction(0.5)
.0
}
2 =>
// bottom left
{
response
.rect
.split_left_right_at_fraction(0.5)
.0
.split_top_bottom_at_fraction(0.5)
.1
}
3 =>
// bottom right
{
response
.rect
.split_left_right_at_fraction(0.5)
.1
.split_top_bottom_at_fraction(0.5)
.1
}
_ => unreachable!(),
}
.shrink(1.0),
CornerRadius::default(),
Color32::from_white_alpha(alpha as u8),
);
}
if response.clicked() {
UiAction::Tap.enqueue();
}
});
}
pub(crate) fn beat_flank(&self) -> u8 {
match self.beat.beat_progression % 4.0 {
0.5..1.5 => 1,
1.5..2.5 => 2,
2.5..3.5 => 3,
_ => 0,
}
}
}