Skip to content

Commit b21bebb

Browse files
fabox-osclaude
andcommitted
Tracks: enable the Pencil tool to draw MIDI regions (#154)
The Left-click tool menu in the Tracks area had only the Pointer tool working; the Pencil entry was a disabled placeholder, so a MIDI or instrument track had no way to draw an empty region on the timeline. - Pencil Tool is selectable and has its own toolbar icon. - With the Pencil, a click on an empty stretch of a MIDI track adds a one-bar region; a drag sets its length. Both snap to the grid. - Project::add_midi_region adds the empty region, refusing audio and bus tracks and zero-length regions, and tells the engine about it. Other tools in the menu remain disabled for now. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
1 parent 4f89fa7 commit b21bebb

2 files changed

Lines changed: 126 additions & 2 deletions

File tree

‎crates/prowave-app/src/model.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,32 @@ impl Project {
18461846
Some(track.regions.len() - 1)
18471847
}
18481848

1849+
/// Adds an empty MIDI region of `len_bars` bars to track `ti`, starting at the bar
1850+
/// `start_bar` (1-based). This is what the Pencil tool draws on a MIDI track (Logic: Pencil
1851+
/// tool, click or drag on an empty stretch of a software or external MIDI track); the notes
1852+
/// are then drawn in the Piano Roll. Returns its index; `None` for a track that holds no
1853+
/// MIDI regions or for a region with no length.
1854+
pub fn add_midi_region(&mut self, ti: usize, start_bar: f32, len_bars: f32) -> Option<usize> {
1855+
let track = self.tracks.get_mut(ti)?;
1856+
if !track.kind.has_regions() || track.kind == TrackKind::Audio || len_bars <= 0.0 {
1857+
return None;
1858+
}
1859+
track.regions.push(Region {
1860+
name: track.name.clone(),
1861+
start_bar: start_bar.max(1.0),
1862+
len_bars,
1863+
notes: Vec::new(),
1864+
controls: Vec::new(),
1865+
folder: None,
1866+
audio: None,
1867+
params: RegionParams::default(),
1868+
generated: None,
1869+
});
1870+
let ri = track.regions.len() - 1;
1871+
self.song_rev += 1;
1872+
Some(ri)
1873+
}
1874+
18491875
/// Turns a generated region into an ordinary MIDI region with the notes it has now, which
18501876
/// can then be edited note by note.
18511877
pub fn convert_to_midi_region(&mut self, ti: usize, ri: usize) {
@@ -3241,6 +3267,28 @@ mod tests {
32413267
}
32423268
}
32433269

3270+
#[test]
3271+
fn the_pencil_draws_an_empty_midi_region_on_a_midi_track_only() {
3272+
let mut p = Project::empty();
3273+
let synth = p.add_track(TrackKind::Instrument);
3274+
let rev = p.song_rev;
3275+
let r = p
3276+
.add_midi_region(synth, 3.0, 2.0)
3277+
.expect("a MIDI track takes a region");
3278+
let region = &p.tracks[synth].regions[r];
3279+
assert!(region.notes.is_empty() && region.generated.is_none() && region.audio.is_none());
3280+
assert_eq!((region.start_bar, region.len_bars), (3.0, 2.0));
3281+
assert_eq!(region.name, p.tracks[synth].name);
3282+
assert_ne!(p.song_rev, rev, "the engine is told about the new region");
3283+
3284+
// Audio and bus tracks take no MIDI region, and an empty region is no region.
3285+
let audio = p.add_track(TrackKind::Audio);
3286+
assert!(p.add_midi_region(audio, 1.0, 1.0).is_none());
3287+
let aux = p.add_track(TrackKind::Aux);
3288+
assert!(p.add_midi_region(aux, 1.0, 1.0).is_none());
3289+
assert!(p.add_midi_region(synth, 1.0, 0.0).is_none());
3290+
}
3291+
32443292
#[test]
32453293
fn a_tempo_curve_moves_in_a_straight_line_to_the_next_change() {
32463294
let mut p = Project::empty();

‎crates/prowave-app/src/tracks_area.rs‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ impl DragMode {
9696
}
9797
}
9898

99+
/// Where the Pencil tool is in [`TOOLS`].
100+
const PENCIL_TOOL: usize = 1;
99101
/// Where the Fade tool is in [`TOOLS`].
100102
const FADE_TOOL: usize = 9;
103+
/// The shortest region the Pencil draws, in bars (a 16th note of a 4/4 bar).
104+
const PENCIL_MIN_BARS: f32 = 1.0 / 16.0;
101105

102106
pub(crate) const TOOLS: [&str; 14] = [
103107
"Pointer Tool",
@@ -130,6 +134,9 @@ pub struct NormalizeJob {
130134
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
131135
pub enum TrackTool {
132136
Pointer,
137+
/// Click or drag on an empty stretch of a MIDI track to draw an empty MIDI region (Logic:
138+
/// Pencil tool).
139+
Pencil,
133140
/// Drag on an audio region to set its fade in (left half) or fade out (right half).
134141
Fade,
135142
}
@@ -146,6 +153,8 @@ pub struct TracksView {
146153
pub tool: TrackTool,
147154
/// A Fade tool drag in progress: `true` for a fade in, `false` for a fade out.
148155
fade_drag: Option<bool>,
156+
/// A Pencil drag in progress: the (track, region) it is drawing.
157+
pencil_drag: Option<(usize, usize)>,
149158
/// A Flex Time marker being dragged: (track, region, marker, the result's length in
150159
/// seconds when it was grabbed). The length stays fixed for the whole drag: scaling by a
151160
/// length that the drag itself changes would make the marker run away from the pointer.
@@ -355,6 +364,7 @@ impl Default for TracksView {
355364
drag_mode: DragMode::NoOverlap,
356365
tool: TrackTool::Pointer,
357366
fade_drag: None,
367+
pencil_drag: None,
358368
flex_drag: None,
359369
catch: true,
360370
show_tempo: false,
@@ -737,6 +747,13 @@ fn draw_fade_tool(g: &Painter, at: Pos2, c: Color32) {
737747
g.line_segment([at + vec2(-6.0, -5.0), at + vec2(6.0, 5.0)], stroke(c));
738748
}
739749

750+
/// The Pencil tool's icon: a pencil on a diagonal.
751+
fn draw_pencil_tool(g: &Painter, at: Pos2, c: Color32) {
752+
g.line_segment([at + vec2(-5.0, 5.0), at + vec2(4.0, -4.0)], stroke(c));
753+
g.line_segment([at + vec2(-5.0, 5.0), at + vec2(-6.0, 6.0)], stroke(c));
754+
g.line_segment([at + vec2(4.0, -4.0), at + vec2(6.0, -6.0)], stroke(c));
755+
}
756+
740757
fn draw_crosshair(g: &Painter, at: Pos2, c: Color32) {
741758
g.line_segment([at + vec2(-6.0, 0.0), at + vec2(6.0, 0.0)], stroke(c));
742759
g.line_segment([at + vec2(0.0, -6.0), at + vec2(0.0, 6.0)], stroke(c));
@@ -1104,7 +1121,8 @@ fn snap_menu(ui: &Ui, g: &Painter, v: &mut TracksView, rx: f32, cy: f32) -> f32
11041121
label.left() - 12.0
11051122
}
11061123

1107-
/// Left-click and Command-click tool menus ending at `rx` (only the Pointer works so far).
1124+
/// Left-click and Command-click tool menus ending at `rx` (the Pointer, Pencil and Fade tools
1125+
/// work so far).
11081126
fn tool_menus(ui: &Ui, g: &Painter, v: &mut TracksView, rx: f32, cy: f32) {
11091127
for (i, key) in ["tool_l", "tool_r"].iter().enumerate() {
11101128
let r = Rect::from_min_size(
@@ -1118,6 +1136,8 @@ fn tool_menus(ui: &Ui, g: &Painter, v: &mut TracksView, rx: f32, cy: f32) {
11181136
let icon_at = pos2(r.left() + 13.0, cy);
11191137
if i == 0 && v.tool == TrackTool::Fade {
11201138
draw_fade_tool(g, icon_at, TEXT);
1139+
} else if i == 0 && v.tool == TrackTool::Pencil {
1140+
draw_pencil_tool(g, icon_at, TEXT);
11211141
} else if i == 0 {
11221142
draw_pointer(g, icon_at, TEXT);
11231143
} else {
@@ -1137,6 +1157,7 @@ fn tool_menus(ui: &Ui, g: &Painter, v: &mut TracksView, rx: f32, cy: f32) {
11371157
for (n, t) in TOOLS.iter().enumerate() {
11381158
let tool = match n {
11391159
0 => Some(TrackTool::Pointer),
1160+
PENCIL_TOOL => Some(TrackTool::Pencil),
11401161
FADE_TOOL => Some(TrackTool::Fade),
11411162
_ => None,
11421163
};
@@ -2964,7 +2985,9 @@ pub fn show(ui: &mut Ui, p: &mut Project, s: &mut UiState, meters: &crate::mixer
29642985
}
29652986

29662987
// Click on an empty lane: select that track, deselect regions.
2967-
let lane_resp = ui.interact(lanes, id("lanes"), Sense::click());
2988+
// Drags on empty lanes are for the Pencil; regions are drawn over the lanes and take their
2989+
// own drags.
2990+
let lane_resp = ui.interact(lanes, id("lanes"), Sense::click_and_drag());
29682991
let mut picked: Option<CtxAction> = None;
29692992
if lane_resp.clicked() || context_click(&lane_resp) {
29702993
if let Some(pt) = lane_resp.interact_pointer_pos() {
@@ -3038,6 +3061,52 @@ pub fn show(ui: &mut Ui, p: &mut Project, s: &mut UiState, meters: &crate::mixer
30383061
}
30393062
};
30403063
let mut moved = false;
3064+
// Pencil tool (Logic: draw a MIDI region): a click on an empty stretch of a MIDI track
3065+
// makes a region one bar long; a drag sets its length, both snapped as the grid is.
3066+
if v.tool == TrackTool::Pencil {
3067+
let pencil_row = |pt: egui::Pos2| {
3068+
row_at(&rows, pt.y - lanes.top() + scroll_y)
3069+
.filter(|row| p.tracks[*row].kind.has_regions())
3070+
.filter(|row| p.tracks[*row].kind != TrackKind::Audio)
3071+
};
3072+
if lane_resp.drag_started() || lane_resp.clicked() {
3073+
if let Some(pt) = lane_resp.interact_pointer_pos() {
3074+
if let Some(row) = pencil_row(pt) {
3075+
let at = snap_pos(beat_of(pt.x).max(0.0));
3076+
let start_bar = p.beats_to_bar(at) as f32 + 1.0;
3077+
let bar_len = if lane_resp.clicked() {
3078+
1.0
3079+
} else {
3080+
PENCIL_MIN_BARS
3081+
};
3082+
if let Some(ri) = p.add_midi_region(row, start_bar, bar_len) {
3083+
p.selected_track = row;
3084+
v.selected = Some((row, ri));
3085+
if lane_resp.drag_started() {
3086+
v.pencil_drag = Some((row, ri));
3087+
} else {
3088+
moved = true;
3089+
}
3090+
}
3091+
}
3092+
}
3093+
}
3094+
if let (Some((row, ri)), true, Some(pt)) = (
3095+
v.pencil_drag,
3096+
lane_resp.dragged(),
3097+
lane_resp.interact_pointer_pos(),
3098+
) {
3099+
let start = p.region_start_beats(&p.tracks[row].regions[ri]);
3100+
let end = snap_pos(beat_of(pt.x).max(0.0));
3101+
let len = (p.beats_to_bar(end) - p.beats_to_bar(start)) as f32;
3102+
p.tracks[row].regions[ri].len_bars = len.max(PENCIL_MIN_BARS);
3103+
moved = true;
3104+
}
3105+
if lane_resp.drag_stopped() {
3106+
v.pencil_drag = None;
3107+
moved = true;
3108+
}
3109+
}
30413110
for (ti, &(top, row_h)) in rows.iter().enumerate() {
30423111
if row_h <= 0.0 {
30433112
continue;
@@ -3943,6 +4012,13 @@ mod row_tests {
39434012
assert_eq!(s.status, None);
39444013
}
39454014

4015+
#[test]
4016+
fn the_pencil_and_fade_tools_sit_where_their_menu_entries_are() {
4017+
assert_eq!(TOOLS[PENCIL_TOOL], "Pencil Tool");
4018+
assert_eq!(TOOLS[FADE_TOOL], "Fade Tool");
4019+
assert_eq!(TOOLS[0], "Pointer Tool");
4020+
}
4021+
39464022
#[test]
39474023
fn the_fade_tool_measures_from_the_edge_in_sixteenths() {
39484024
// A region from beat 8, 4 beats long.

0 commit comments

Comments
 (0)