Passagen, Übungsmodus, Stimmfinder-Überarbeitung, Gerätedesign, Metronom und Akkorde
- Passagenwahl, Original-Kurve, Spulen/Üben und Pause - Startseite, Klartext-Oberfläche, Stimmfinder mit Stimmtyp-Vorschlag - Gerätedesign (Regler, Schalter, LED) und Metronom (frei/songsynchron, Tap) - Akkorderkennung aus der Begleitung, Akkordleiste und -karte - zweiter Song als Chart Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
465
src/ui/gear.rs
Normal file
465
src/ui/gear.rs
Normal file
@ -0,0 +1,465 @@
|
||||
//! Bedienelemente im Stil von Musikgeräten: Frontplatte mit Schrauben, Drehregler, Kippschalter mit LED,
|
||||
//! LED-Pegelkette. Die Mathematik (Winkel, Ziehen, LED-Anzahl) ist rein und getestet.
|
||||
|
||||
use eframe::egui::{self, Align2, Color32, FontId, Rect, Response, Sense, Stroke, Ui, pos2, vec2};
|
||||
|
||||
/// Warmes Bernstein als Akzent (Beleuchtung, aktive Elemente).
|
||||
pub const AMBER: Color32 = Color32::from_rgb(255, 176, 48);
|
||||
pub const AMBER_DIM: Color32 = Color32::from_rgb(120, 78, 16);
|
||||
pub const LED_GREEN: Color32 = Color32::from_rgb(90, 235, 120);
|
||||
pub const LED_YELLOW: Color32 = Color32::from_rgb(245, 205, 60);
|
||||
pub const LED_RED: Color32 = Color32::from_rgb(245, 80, 65);
|
||||
/// Gehäuse- und Plattenfarben.
|
||||
pub const CASE_BG: Color32 = Color32::from_rgb(24, 22, 20);
|
||||
pub const PLATE_BG: Color32 = Color32::from_rgb(38, 35, 32);
|
||||
pub const PLATE_EDGE: Color32 = Color32::from_rgb(78, 70, 62);
|
||||
pub const ENGRAVING: Color32 = Color32::from_rgb(190, 175, 150);
|
||||
const LED_OFF: Color32 = Color32::from_rgb(46, 44, 42);
|
||||
|
||||
/// Warmes, dunkles Geräte-Design für die ganze Oberfläche: Metall-Grau, Bernstein als Akzent.
|
||||
pub fn apply_theme(ctx: &egui::Context) {
|
||||
let mut v = egui::Visuals::dark();
|
||||
v.panel_fill = CASE_BG;
|
||||
v.window_fill = PLATE_BG;
|
||||
v.extreme_bg_color = Color32::from_rgb(14, 13, 12);
|
||||
v.faint_bg_color = Color32::from_rgb(32, 30, 28);
|
||||
v.selection.bg_fill = AMBER_DIM;
|
||||
v.selection.stroke = Stroke::new(1.0, AMBER);
|
||||
v.hyperlink_color = AMBER;
|
||||
let w = &mut v.widgets;
|
||||
w.noninteractive.fg_stroke = Stroke::new(1.0, Color32::from_rgb(205, 195, 178));
|
||||
let metal = Color32::from_rgb(58, 53, 48);
|
||||
for (st, fill, fg) in [
|
||||
(&mut w.inactive, metal, Color32::from_rgb(228, 218, 200)),
|
||||
(
|
||||
&mut w.hovered,
|
||||
Color32::from_rgb(86, 76, 64),
|
||||
Color32::from_rgb(255, 238, 205),
|
||||
),
|
||||
(&mut w.active, AMBER_DIM, Color32::WHITE),
|
||||
] {
|
||||
st.bg_fill = fill;
|
||||
st.weak_bg_fill = fill;
|
||||
st.fg_stroke = Stroke::new(1.0, fg);
|
||||
st.bg_stroke = Stroke::new(1.0, PLATE_EDGE);
|
||||
st.corner_radius = egui::CornerRadius::same(5);
|
||||
}
|
||||
ctx.set_visuals(v);
|
||||
}
|
||||
|
||||
/// Hauptknopf im Geräte-Stil: leuchtendes Bernstein mit dunkler Schrift.
|
||||
pub fn accent_button(ui: &mut Ui, text: &str, size: f32, enabled: bool) -> Response {
|
||||
ui.add_enabled(
|
||||
enabled,
|
||||
egui::Button::new(
|
||||
egui::RichText::new(text)
|
||||
.size(size)
|
||||
.strong()
|
||||
.color(Color32::from_rgb(34, 22, 4)),
|
||||
)
|
||||
.fill(AMBER)
|
||||
.stroke(Stroke::new(1.5, Color32::from_rgb(255, 214, 130)))
|
||||
.min_size(vec2(190.0, 38.0)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Winkel (Radiant, 0 = oben, im Uhrzeigersinn) für einen Reglerwert 0..=1: 270° Drehbereich von −135° bis +135°.
|
||||
pub fn knob_angle(frac: f32) -> f32 {
|
||||
(-135.0 + 270.0 * frac.clamp(0.0, 1.0)).to_radians()
|
||||
}
|
||||
|
||||
/// Neuer Wert nach Ziehen: `dy_px` Pixel nach oben (positiv) bewegen den Regler; 200 px = voller Bereich,
|
||||
/// mit `fine` zehnmal feiner. Ergebnis bleibt im Bereich.
|
||||
pub fn knob_drag(value: f32, min: f32, max: f32, dy_px: f32, fine: bool) -> f32 {
|
||||
let per_px = (max - min) / 200.0 * if fine { 0.1 } else { 1.0 };
|
||||
(value + dy_px * per_px).clamp(min, max)
|
||||
}
|
||||
|
||||
/// Anzahl leuchtender LEDs einer Kette mit `n` Segmenten bei Pegel `frac` (0..=1).
|
||||
pub fn lit_segments(frac: f32, n: usize) -> usize {
|
||||
(frac.clamp(0.0, 1.0) * n as f32).round() as usize
|
||||
}
|
||||
|
||||
/// LED-Farbe für Segment `i` von `n`: unten grün, oben gelb, ganz oben rot.
|
||||
pub fn segment_color(i: usize, n: usize) -> Color32 {
|
||||
let f = i as f32 / n.max(1) as f32;
|
||||
if f < 0.62 {
|
||||
LED_GREEN
|
||||
} else if f < 0.85 {
|
||||
LED_YELLOW
|
||||
} else {
|
||||
LED_RED
|
||||
}
|
||||
}
|
||||
|
||||
fn dim(c: Color32, k: f32) -> Color32 {
|
||||
c.gamma_multiply(k)
|
||||
}
|
||||
|
||||
/// Frontplatte: dunkles Metall mit Rand, gravierter Überschrift und vier Schrauben.
|
||||
pub fn panel<R>(ui: &mut Ui, title: &str, add: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
let frame = egui::Frame::new()
|
||||
.fill(PLATE_BG)
|
||||
.stroke(Stroke::new(1.5, PLATE_EDGE))
|
||||
.corner_radius(8.0)
|
||||
.inner_margin(egui::Margin::symmetric(14, 10));
|
||||
let out = frame.show(ui, |ui| {
|
||||
ui.label(
|
||||
egui::RichText::new(title.to_uppercase())
|
||||
.small()
|
||||
.strong()
|
||||
.color(ENGRAVING),
|
||||
);
|
||||
ui.add_space(2.0);
|
||||
add(ui)
|
||||
});
|
||||
let r = out.response.rect;
|
||||
for (x, y) in [(6.0, 6.0), (-6.0, 6.0), (6.0, -6.0), (-6.0, -6.0)] {
|
||||
let c = pos2(
|
||||
if x > 0.0 { r.left() + x } else { r.right() + x },
|
||||
if y > 0.0 { r.top() + y } else { r.bottom() + y },
|
||||
);
|
||||
screw(ui.painter(), c);
|
||||
}
|
||||
out.inner
|
||||
}
|
||||
|
||||
fn screw(p: &egui::Painter, c: egui::Pos2) {
|
||||
p.circle_filled(c, 3.2, Color32::from_rgb(92, 86, 78));
|
||||
p.circle_stroke(c, 3.2, Stroke::new(0.8, Color32::from_rgb(30, 28, 26)));
|
||||
p.line_segment(
|
||||
[c + vec2(-2.0, 0.5), c + vec2(2.0, -0.5)],
|
||||
Stroke::new(1.0, Color32::from_rgb(40, 37, 33)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Eine LED (leuchtend mit Schein oder dunkel).
|
||||
pub fn led(p: &egui::Painter, c: egui::Pos2, on: bool, color: Color32, r: f32) {
|
||||
if on {
|
||||
p.circle_filled(c, r * 2.0, dim(color, 0.18));
|
||||
p.circle_filled(c, r, color);
|
||||
p.circle_filled(
|
||||
c - vec2(r * 0.3, r * 0.3),
|
||||
r * 0.35,
|
||||
Color32::from_white_alpha(150),
|
||||
);
|
||||
} else {
|
||||
p.circle_filled(c, r, LED_OFF);
|
||||
p.circle_stroke(c, r, Stroke::new(0.8, Color32::from_rgb(20, 19, 18)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Drehregler mit Beschriftung. Ziehen (hoch/runter) oder Mausrad ändert den Wert (Umschalt = fein),
|
||||
/// Doppelklick setzt auf `default` zurück. `show` formatiert den Wert für die Anzeige beim Bedienen.
|
||||
pub fn knob(
|
||||
ui: &mut Ui,
|
||||
value: &mut f32,
|
||||
(min, max): (f32, f32),
|
||||
default: f32,
|
||||
label: &str,
|
||||
show: impl Fn(f32) -> String,
|
||||
enabled: bool,
|
||||
) -> Response {
|
||||
const SIZE: f32 = 64.0;
|
||||
let width = SIZE.max(
|
||||
ui.fonts_mut(|f| {
|
||||
f.layout_no_wrap(
|
||||
label.to_string(),
|
||||
FontId::proportional(13.0),
|
||||
Color32::WHITE,
|
||||
)
|
||||
.size()
|
||||
.x
|
||||
}) + 6.0,
|
||||
);
|
||||
let (rect, mut resp) = ui.allocate_exact_size(
|
||||
vec2(width, SIZE + 34.0),
|
||||
if enabled {
|
||||
Sense::click_and_drag()
|
||||
} else {
|
||||
Sense::hover()
|
||||
},
|
||||
);
|
||||
if enabled {
|
||||
let fine = ui.input(|i| i.modifiers.shift);
|
||||
if resp.dragged() {
|
||||
*value = knob_drag(*value, min, max, -resp.drag_delta().y, fine);
|
||||
resp.mark_changed();
|
||||
}
|
||||
if resp.hovered() {
|
||||
let scroll = ui.input(|i| i.smooth_scroll_delta.y);
|
||||
if scroll != 0.0 {
|
||||
*value = knob_drag(*value, min, max, scroll * 0.5, fine);
|
||||
resp.mark_changed();
|
||||
}
|
||||
}
|
||||
if resp.double_clicked() {
|
||||
*value = default.clamp(min, max);
|
||||
resp.mark_changed();
|
||||
}
|
||||
}
|
||||
let frac = if max > min {
|
||||
(*value - min) / (max - min)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let p = ui.painter_at(rect);
|
||||
let c = pos2(rect.center().x, rect.top() + SIZE / 2.0 + 2.0);
|
||||
let alpha = if enabled { 1.0 } else { 0.4 };
|
||||
// Skala: 11 Striche, bis zum Wert beleuchtet.
|
||||
for i in 0..=10 {
|
||||
let f = i as f32 / 10.0;
|
||||
let a = knob_angle(f);
|
||||
let (s, co) = a.sin_cos();
|
||||
let (r0, r1) = (
|
||||
SIZE / 2.0 + 1.0,
|
||||
SIZE / 2.0 + if i % 5 == 0 { 7.0 } else { 5.0 },
|
||||
);
|
||||
let lit = f <= frac + 0.001 && frac > 0.0;
|
||||
p.line_segment(
|
||||
[c + vec2(s * r0, -co * r0), c + vec2(s * r1, -co * r1)],
|
||||
Stroke::new(
|
||||
2.0,
|
||||
dim(
|
||||
if lit {
|
||||
AMBER
|
||||
} else {
|
||||
Color32::from_rgb(70, 64, 56)
|
||||
},
|
||||
alpha,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
// Reglerkörper: Rand, Fläche, Glanz.
|
||||
let hot = resp.hovered() || resp.dragged();
|
||||
p.circle_filled(c, SIZE / 2.0 - 2.0, Color32::from_rgb(20, 19, 18));
|
||||
p.circle_filled(
|
||||
c,
|
||||
SIZE / 2.0 - 5.0,
|
||||
dim(Color32::from_rgb(70, 66, 62), alpha),
|
||||
);
|
||||
p.circle_filled(
|
||||
c,
|
||||
SIZE / 2.0 - 9.0,
|
||||
dim(Color32::from_rgb(54, 50, 47), alpha),
|
||||
);
|
||||
p.circle_filled(
|
||||
c - vec2(6.0, 8.0),
|
||||
SIZE / 2.0 - 22.0,
|
||||
Color32::from_white_alpha(if hot { 14 } else { 8 }),
|
||||
);
|
||||
let a = knob_angle(frac);
|
||||
let (s, co) = a.sin_cos();
|
||||
p.line_segment(
|
||||
[
|
||||
c + vec2(s * 8.0, -co * 8.0),
|
||||
c + vec2(s * (SIZE / 2.0 - 8.0), -co * (SIZE / 2.0 - 8.0)),
|
||||
],
|
||||
Stroke::new(3.5, dim(AMBER, alpha)),
|
||||
);
|
||||
// Beschriftung, darunter der Wert (nur beim Bedienen/Überfahren, sonst Platz freihalten).
|
||||
p.text(
|
||||
pos2(rect.center().x, rect.bottom() - 22.0),
|
||||
Align2::CENTER_CENTER,
|
||||
label,
|
||||
FontId::proportional(13.0),
|
||||
dim(ENGRAVING, alpha),
|
||||
);
|
||||
p.text(
|
||||
pos2(rect.center().x, rect.bottom() - 6.0),
|
||||
Align2::CENTER_CENTER,
|
||||
if hot { show(*value) } else { String::new() },
|
||||
FontId::monospace(12.0),
|
||||
AMBER,
|
||||
);
|
||||
resp.on_hover_text(format!(
|
||||
"{label}: ziehen oder Mausrad (Umschalt = fein), Doppelklick = zurücksetzen"
|
||||
))
|
||||
}
|
||||
|
||||
/// Kippschalter mit LED und Beschriftung; Klick schaltet um.
|
||||
pub fn toggle(ui: &mut Ui, on: &mut bool, label: &str, enabled: bool) -> Response {
|
||||
let text_w = ui.fonts_mut(|f| {
|
||||
f.layout_no_wrap(
|
||||
label.to_string(),
|
||||
FontId::proportional(14.0),
|
||||
Color32::WHITE,
|
||||
)
|
||||
.size()
|
||||
.x
|
||||
});
|
||||
let (rect, mut resp) = ui.allocate_exact_size(
|
||||
vec2(text_w + 78.0, 34.0),
|
||||
if enabled {
|
||||
Sense::click()
|
||||
} else {
|
||||
Sense::hover()
|
||||
},
|
||||
);
|
||||
if enabled && resp.clicked() {
|
||||
*on = !*on;
|
||||
resp.mark_changed();
|
||||
}
|
||||
let p = ui.painter_at(rect);
|
||||
let alpha = if enabled { 1.0 } else { 0.4 };
|
||||
let y = rect.center().y;
|
||||
led(
|
||||
&p,
|
||||
pos2(rect.left() + 8.0, y),
|
||||
*on && enabled,
|
||||
LED_GREEN,
|
||||
4.0,
|
||||
);
|
||||
// Wippe: Gehäuse und Hebel, der sich nach rechts/links neigt.
|
||||
let body = Rect::from_center_size(pos2(rect.left() + 42.0, y), vec2(34.0, 18.0));
|
||||
p.rect_filled(body, 9.0, Color32::from_rgb(16, 15, 14));
|
||||
p.rect_stroke(
|
||||
body,
|
||||
9.0,
|
||||
Stroke::new(1.0, PLATE_EDGE),
|
||||
egui::StrokeKind::Inside,
|
||||
);
|
||||
let knob_x = if *on {
|
||||
body.right() - 9.0
|
||||
} else {
|
||||
body.left() + 9.0
|
||||
};
|
||||
p.circle_filled(
|
||||
pos2(knob_x, y),
|
||||
7.0,
|
||||
dim(
|
||||
if *on {
|
||||
AMBER
|
||||
} else {
|
||||
Color32::from_rgb(120, 114, 106)
|
||||
},
|
||||
alpha,
|
||||
),
|
||||
);
|
||||
p.circle_filled(
|
||||
pos2(knob_x - 2.0, y - 2.0),
|
||||
2.2,
|
||||
Color32::from_white_alpha(120),
|
||||
);
|
||||
p.text(
|
||||
pos2(rect.left() + 66.0, y),
|
||||
Align2::LEFT_CENTER,
|
||||
label,
|
||||
FontId::proportional(14.0),
|
||||
dim(Color32::from_gray(225), alpha),
|
||||
);
|
||||
resp
|
||||
}
|
||||
|
||||
/// Kleines Display: dunkles Glas mit leuchtender Schrift und kleiner Beschriftung.
|
||||
pub fn lcd(ui: &mut Ui, text: &str, caption: &str, color: Color32, width: f32) -> Response {
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(width, 58.0), Sense::hover());
|
||||
let p = ui.painter_at(rect);
|
||||
p.rect_filled(rect, 5.0, Color32::from_rgb(10, 13, 11));
|
||||
p.rect_stroke(
|
||||
rect,
|
||||
5.0,
|
||||
Stroke::new(1.5, PLATE_EDGE),
|
||||
egui::StrokeKind::Inside,
|
||||
);
|
||||
p.text(
|
||||
rect.left_top() + vec2(8.0, 9.0),
|
||||
Align2::LEFT_CENTER,
|
||||
caption.to_uppercase(),
|
||||
FontId::proportional(11.0),
|
||||
color.gamma_multiply(0.75),
|
||||
);
|
||||
let pos = rect.left_center() + vec2(10.0, 8.0);
|
||||
p.text(
|
||||
pos,
|
||||
Align2::LEFT_CENTER,
|
||||
text,
|
||||
FontId::monospace(30.0),
|
||||
color.gamma_multiply(0.3),
|
||||
);
|
||||
p.text(
|
||||
pos,
|
||||
Align2::LEFT_CENTER,
|
||||
text,
|
||||
FontId::monospace(30.0),
|
||||
color,
|
||||
);
|
||||
resp
|
||||
}
|
||||
|
||||
/// Waagerechte LED-Pegelkette (`frac` 0..=1) aus `n` Segmenten.
|
||||
pub fn led_ladder(ui: &mut Ui, frac: f32, n: usize, width: f32) -> Response {
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(width, 16.0), Sense::hover());
|
||||
let p = ui.painter_at(rect);
|
||||
let lit = lit_segments(frac, n);
|
||||
let step = rect.width() / n as f32;
|
||||
for i in 0..n {
|
||||
let cx = rect.left() + (i as f32 + 0.5) * step;
|
||||
led(
|
||||
&p,
|
||||
pos2(cx, rect.center().y),
|
||||
i < lit,
|
||||
segment_color(i, n),
|
||||
(step * 0.32).clamp(2.0, 5.0),
|
||||
);
|
||||
}
|
||||
resp
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn knob_angle_spans_270_degrees_centered_on_top() {
|
||||
assert!((knob_angle(0.0).to_degrees() + 135.0).abs() < 1e-3);
|
||||
assert!(knob_angle(0.5).abs() < 1e-6, "Mitte zeigt nach oben");
|
||||
assert!((knob_angle(1.0).to_degrees() - 135.0).abs() < 1e-3);
|
||||
assert_eq!(knob_angle(-3.0), knob_angle(0.0));
|
||||
assert_eq!(knob_angle(9.0), knob_angle(1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn knob_drag_scales_clamps_and_has_a_fine_mode() {
|
||||
// 200 Pixel nach oben = voller Bereich.
|
||||
assert!((knob_drag(0.0, 0.0, 1.0, 200.0, false) - 1.0).abs() < 1e-6);
|
||||
assert!((knob_drag(0.5, 0.0, 1.0, 50.0, false) - 0.75).abs() < 1e-6);
|
||||
assert!(
|
||||
(knob_drag(0.5, 0.0, 1.0, -50.0, false) - 0.25).abs() < 1e-6,
|
||||
"nach unten = leiser"
|
||||
);
|
||||
assert!(
|
||||
(knob_drag(0.5, 0.0, 1.0, 50.0, true) - 0.525).abs() < 1e-6,
|
||||
"fein: ein Zehntel"
|
||||
);
|
||||
assert_eq!(knob_drag(0.9, 0.0, 1.0, 500.0, false), 1.0, "oben begrenzt");
|
||||
assert_eq!(
|
||||
knob_drag(0.1, 0.0, 1.0, -500.0, false),
|
||||
0.0,
|
||||
"unten begrenzt"
|
||||
);
|
||||
assert!(
|
||||
(knob_drag(10.0, 0.0, 100.0, 100.0, false) - 60.0).abs() < 1e-4,
|
||||
"andere Bereiche"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn led_ladder_counts_and_colors() {
|
||||
assert_eq!(lit_segments(0.0, 20), 0);
|
||||
assert_eq!(lit_segments(1.0, 20), 20);
|
||||
assert_eq!(lit_segments(0.5, 20), 10);
|
||||
assert_eq!(lit_segments(2.0, 20), 20, "begrenzt");
|
||||
assert_eq!(lit_segments(-1.0, 20), 0);
|
||||
assert_eq!(segment_color(0, 20), LED_GREEN);
|
||||
assert_eq!(segment_color(14, 20), LED_YELLOW);
|
||||
assert_eq!(segment_color(19, 20), LED_RED);
|
||||
// Nur das obere Ende ist rot, der größte Teil grün.
|
||||
let reds = (0..20).filter(|&i| segment_color(i, 20) == LED_RED).count();
|
||||
let greens = (0..20)
|
||||
.filter(|&i| segment_color(i, 20) == LED_GREEN)
|
||||
.count();
|
||||
assert!(reds <= 4 && greens >= 11, "rot {reds}, grün {greens}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user