Files
stimmwerk/src/ui/grip_view.rs
wlkns-dev 7656c8de41 Rhythmusraster, kompaktes Layout für „Singen“, Akkorde auf dem Raster
- Akkordgrenzen rasten auf die Zählzeiten des Rasters ein
- Rhythmusraster im Stil des GuiTacheles-Blatts (Felder anklickbar, speicherbar)
- Singen-Ansicht ohne Scrollen: Kopfzeile, Notenfeld füllt den Rest, Mischpult als Leiste,
  Fenster „Mehr“ und „Ergebnis“
- Regler verbrauchen das Mausrad (Fenster scrollt nicht mehr mit)
- Raster/Muster-Änderungen wirken auch nach Transponieren und während einer Session

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-31 18:58:14 +01:00

329 lines
11 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Gitarrengriff-Diagramme und Strumming-Anzeige.
use crate::chart::NoteChart;
use crate::guitar::{Grip, grip};
use crate::import::chords::{display_chord, prefer_flats};
use eframe::egui::{self, Align2, Color32, FontId, Rect, Stroke, pos2, vec2};
use super::gear::AMBER;
pub const STRIP_H: f32 = 124.0;
const FRETS_SHOWN: u8 = 4;
/// Zeichnet ein Griffbrett-Diagramm in `rect` (Name oben; ×/○ über den Saiten; Punkte; Barré-Balken).
pub fn draw_grip(painter: &egui::Painter, rect: Rect, label: &str, g: &Grip, active: bool) {
let col = if active {
AMBER
} else {
Color32::from_gray(150)
};
painter.text(
pos2(rect.center().x, rect.top() + 9.0),
Align2::CENTER_CENTER,
label,
FontId::proportional(16.0),
col,
);
let board = Rect::from_min_max(
pos2(rect.left() + 18.0, rect.top() + 36.0),
pos2(rect.right() - 18.0, rect.bottom() - 6.0),
);
let sx = |s: usize| board.left() + board.width() * s as f32 / 5.0;
let fy = |f: f32| board.top() + board.height() * f / f32::from(FRETS_SHOWN);
let line = Stroke::new(1.0, Color32::from_gray(120));
let base = g.base_fret();
// Sattel (dick) oder Bundnummer.
if base == 1 {
painter.line_segment(
[
pos2(board.left(), board.top()),
pos2(board.right(), board.top()),
],
Stroke::new(3.0, col),
);
} else {
painter.line_segment(
[
pos2(board.left(), board.top()),
pos2(board.right(), board.top()),
],
line,
);
painter.text(
pos2(board.right() + 4.0, fy(0.5)),
Align2::LEFT_CENTER,
format!("{base}"),
FontId::proportional(11.0),
Color32::from_gray(190),
);
}
for f in 1..=FRETS_SHOWN {
painter.line_segment(
[
pos2(board.left(), fy(f32::from(f))),
pos2(board.right(), fy(f32::from(f))),
],
line,
);
}
for s in 0..6 {
painter.line_segment(
[pos2(sx(s), board.top()), pos2(sx(s), board.bottom())],
line,
);
}
let cell_y = |fret: u8| fy(f32::from(fret - base) + 0.5);
if let Some((fret, a, b)) = g.barre() {
let r = Rect::from_min_max(
pos2(sx(a) - 5.0, cell_y(fret) - 5.0),
pos2(sx(b) + 5.0, cell_y(fret) + 5.0),
);
painter.rect_filled(r, 5.0, col);
}
for (s, f) in g.frets.iter().enumerate() {
match *f {
None => {
painter.text(
pos2(sx(s), board.top() - 9.0),
Align2::CENTER_CENTER,
"×",
FontId::proportional(13.0),
Color32::from_gray(150),
);
}
Some(0) => {
painter.circle_stroke(
pos2(sx(s), board.top() - 9.0),
4.0,
Stroke::new(1.2, Color32::from_gray(190)),
);
}
Some(fret) => {
painter.circle_filled(pos2(sx(s), cell_y(fret)), 5.5, col);
}
}
}
}
/// Streifen „Gitarre“: aktueller und nächster Griff, rechts das Strumming-Muster (falls bekannt).
/// Per Klick geändertes Strumming-Muster, das auf die Übernahme ins Chart wartet.
static PENDING_EDIT: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
/// Holt ein wartendes, geändertes Muster ab.
pub fn take_strum_edit() -> Option<String> {
PENDING_EDIT.lock().ok().and_then(|mut p| p.take())
}
pub fn grip_strip(ui: &mut egui::Ui, chart: &NoteChart, now_ms: f64) {
let (rect, _) =
ui.allocate_exact_size(vec2(ui.available_width(), STRIP_H), egui::Sense::hover());
let painter = ui.painter_at(rect);
painter.rect_filled(rect, 6.0, Color32::from_rgb(24, 24, 26));
painter.rect_stroke(
rect,
6.0,
Stroke::new(1.0, Color32::from_gray(60)),
egui::StrokeKind::Inside,
);
let flats = prefer_flats(&chart.chords);
let cur = chart
.chord_at(now_ms)
.map(|i| chart.chords[i].name.as_str());
let next = chart.next_chord_after(now_ms, cur).map(|c| c.name.as_str());
let mut x = rect.left() + 8.0;
for (name, active) in [(cur, true), (next, false)] {
let cell = Rect::from_min_size(pos2(x, rect.top() + 2.0), vec2(104.0, STRIP_H - 4.0));
if let Some(n) = name {
let label = if active {
display_chord(n, flats)
} else {
format!("dann {}", display_chord(n, flats))
};
if let Some(g) = grip(n) {
draw_grip(&painter, cell, &label, &g, active);
}
}
x += 112.0;
}
if let Some(g) = chart.effective_grid() {
let slots = 2 * g.beats_per_bar as usize;
let pattern = chart
.strum
.as_ref()
.map_or_else(|| "-".repeat(slots), |st| st.pattern.clone());
let area = Rect::from_min_max(
pos2(x + 16.0, rect.top() + 6.0),
rect.right_bottom() - vec2(8.0, 6.0),
);
let estimated = chart.strum.as_ref().is_some_and(|st| st.confidence < 1.0);
if let Some(new) = strum_grid(
ui,
&painter,
area,
&pattern,
slots,
chart.strum_slot(now_ms).map(|s| s.0),
estimated,
chart.strum.is_some(),
) && let Ok(mut p) = PENDING_EDIT.lock()
{
*p = Some(new);
}
} else {
painter.text(
pos2(x + 16.0, rect.center().y),
Align2::LEFT_CENTER,
"Strumming: erst ein Takt-Raster anlegen",
FontId::proportional(13.0),
Color32::from_gray(120),
);
}
}
/// Nächster Zustand eines Feldes beim Anklicken: leer → Abschlag → Aufschlag → leer.
pub fn next_stroke(c: char) -> char {
match c {
'-' => 'D',
'D' => 'U',
_ => '-',
}
}
/// Muster nach einem Klick auf Feld `i`.
pub fn click_slot(pattern: &str, i: usize) -> String {
pattern
.chars()
.enumerate()
.map(|(k, c)| if k == i { next_stroke(c) } else { c })
.collect()
}
/// Abschlag: Kästchen mit dicker Oberkante.
fn paint_downstroke(painter: &egui::Painter, c: egui::Pos2, col: Color32) {
let r = Rect::from_center_size(c, vec2(20.0, 22.0));
painter.rect_stroke(r, 1.0, Stroke::new(1.5, col), egui::StrokeKind::Inside);
painter.rect_filled(Rect::from_min_size(r.min, vec2(r.width(), 6.0)), 0.0, col);
}
/// Aufschlag: V.
fn paint_upstroke(painter: &egui::Painter, c: egui::Pos2, col: Color32) {
let (w, h) = (10.0, 11.0);
let st = Stroke::new(2.2, col);
painter.line_segment([pos2(c.x - w, c.y - h), pos2(c.x, c.y + h)], st);
painter.line_segment([pos2(c.x + w, c.y - h), pos2(c.x, c.y + h)], st);
}
/// Rhythmusraster: ein Takt in Achteln, gezählt „1 und 2 und 3 und 4 und“, darunter je Feld Abschlag (Kästchen mit
/// dicker Oberkante), Aufschlag (V) oder leer. Felder sind anklickbar; Ergebnis ist das geänderte Muster.
#[allow(clippy::too_many_arguments)]
fn strum_grid(
ui: &mut egui::Ui,
painter: &egui::Painter,
rect: Rect,
pattern: &str,
slots: usize,
active: Option<usize>,
estimated: bool,
has_pattern: bool,
) -> Option<String> {
let title = if !has_pattern {
"Rhythmusraster: Felder anklicken (leer → Abschlag → Aufschlag)"
} else if estimated {
"Rhythmusraster (aus dem Mix geschätzt, Felder anklicken zum Ändern)"
} else {
"Rhythmusraster (Felder anklicken zum Ändern)"
};
painter.text(
rect.left_top(),
Align2::LEFT_TOP,
title,
FontId::proportional(12.0),
Color32::from_gray(150),
);
let label_w = 52.0;
let gap = 8.0;
let beats = slots.div_ceil(2);
let cell_w = ((rect.width() - label_w - gap * beats as f32) / slots as f32).clamp(18.0, 44.0);
let (top, h) = (rect.top() + 36.0, 58.0);
painter.text(
pos2(rect.left(), rect.top() + 26.0),
Align2::LEFT_CENTER,
"Zähle:",
FontId::proportional(12.0),
Color32::from_gray(150),
);
let chars: Vec<char> = pattern.chars().collect();
let mut changed = None;
for i in 0..slots {
let x = rect.left() + label_w + i as f32 * cell_w + (i / 2) as f32 * gap;
let cell = Rect::from_min_size(pos2(x, top), vec2(cell_w, h));
let now = active == Some(i);
let resp = ui.interact(cell, ui.scope_id().with(("strum", i)), egui::Sense::click());
let fill = if now {
Color32::from_rgb(70, 52, 18)
} else if resp.hovered() {
Color32::from_rgb(48, 48, 54)
} else {
Color32::from_rgb(34, 34, 38)
};
painter.rect_filled(cell, 2.0, fill);
painter.rect_stroke(
cell,
2.0,
Stroke::new(
if now { 1.6 } else { 1.0 },
if now { AMBER } else { Color32::from_gray(90) },
),
egui::StrokeKind::Inside,
);
let label = if i % 2 == 0 {
format!("{}", i / 2 + 1)
} else {
"und".to_string()
};
painter.text(
pos2(cell.center().x, top - 10.0),
Align2::CENTER_CENTER,
label,
FontId::proportional(if i % 2 == 0 { 15.0 } else { 11.0 }),
if i % 2 == 0 {
Color32::from_gray(220)
} else {
Color32::from_gray(150)
},
);
let c = cell.center();
match chars.get(i).copied().unwrap_or('-') {
'D' => paint_downstroke(painter, c, AMBER),
'U' => paint_upstroke(painter, c, Color32::from_rgb(110, 190, 230)),
_ => {}
}
if resp
.on_hover_text("Klick: leer → Abschlag → Aufschlag → leer")
.clicked()
{
let base: String = (0..slots)
.map(|k| chars.get(k).copied().unwrap_or('-'))
.collect();
changed = Some(click_slot(&base, i));
}
}
changed
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clicking_a_slot_cycles_empty_down_up() {
assert_eq!(next_stroke('-'), 'D');
assert_eq!(next_stroke('D'), 'U');
assert_eq!(next_stroke('U'), '-');
assert_eq!(click_slot("--------", 2), "--D-----");
assert_eq!(click_slot("--D-----", 2), "--U-----");
assert_eq!(click_slot("D-DUD-DU", 7), "D-DUD-D-");
assert_eq!(click_slot("D-DUD-DU", 0).len(), 8);
}
}