pixel8-console 0.2.0

Pixel8: a PICO-8-like fantasy console for Rust games
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//! The SFX editor, with PICO-8's three views toggled by TAB / the top-left
//! buttons: pitch mode (a graph of pitch bars + a volume strip), tracker mode
//! (a 32-step table edited with piano keys), and the wave designer (drawing a
//! custom waveform into SFX slots 0..8). Space previews the SFX.

use super::history::History;
use crate::{
    shell::{Key, Mods},
    ui::{self, Mouse},
};
use pixel8_runtime::{
    assets::{Assets, CustomWave, Note, Sfx, NOTE_CUSTOM_FLAG, SFX_LEN},
    audio::AudioHandle,
    clipboard::{self, ClipboardPayload, Pasted},
    fb::Framebuffer,
    palette::col,
};

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SfxMode {
    Pitch,
    Tracker,
    Wave,
}

// Tracker layout.
const STEP_Y: i32 = 19;
const COL_X: [i32; 2] = [6, 70];
const FX_X: i32 = 45;
const FX_Y: i32 = 24;
const FX_DY: i32 = 9;
const FX_LABELS: [&str; 5] = ["nz", "bz", "dt", "rv", "dm"];

// Pitch-graph + volume-strip geometry.
const G_TOP: i32 = 28;
const G_BOT: i32 = 93;
const V_TOP: i32 = 103;
const V_BOT: i32 = 117;

/// Editable fields of a tracker step.
#[derive(Clone, Copy, PartialEq)]
enum Field {
    Note,
    Wave,
    Vol,
    Fx,
}
const FIELDS: [Field; 4] = [Field::Note, Field::Wave, Field::Vol, Field::Fx];

/// Piano mapping: key -> semitone offset from C of the current octave.
const PIANO_LOW: &str = "zsxdcvgbhnjm";
const PIANO_HIGH: &str = "q2w3er5t6y7u";

pub struct SfxEditor {
    sfx: usize,
    step: usize,
    field: usize,
    octave: u8,
    mode: SfxMode,
    /// The waveform applied when drawing pitches in pitch mode (0..8).
    wave_sel: u8,
    /// Pitch-mode palette shown as numbers (toggled by the circle button)
    /// rather than waveform glyphs.
    palette_numeric: bool,
    status: ui::StatusMsg,
    /// Undo/redo of the whole SFX bank (last 10 edits).
    history: History<Vec<Sfx>>,
}

impl SfxEditor {
    pub fn new() -> Self {
        Self {
            sfx: 0,
            step: 0,
            field: 0,
            octave: 2,
            mode: SfxMode::Pitch,
            wave_sel: 0,
            palette_numeric: false,
            status: ui::StatusMsg::default(),
            history: History::new(),
        }
    }

    /// Select an SFX slot (used when jumping in from the music editor's pencil).
    pub fn select(&mut self, sfx: usize) {
        self.sfx = sfx % 64;
    }

    /// Whether the pitch-mode button is the lit one (for the shell's top bar).
    pub fn is_pitch(&self) -> bool {
        self.mode == SfxMode::Pitch
    }

    fn preview(&self, assets: &Assets, audio: &AudioHandle) {
        // In the wave designer, play a sustained note *through* the drawn
        // waveform (the slot's own notes don't use its waveform), so the user
        // can actually hear the waveform they are drawing.
        let (sfx, target) = if self.mode == SfxMode::Wave {
            let scratch = if self.sfx == 63 { 62 } else { 63 };
            let mut sfx = assets.sfx.clone();
            let probe = Note {
                pitch: 33,
                wave: NOTE_CUSTOM_FLAG | self.sfx as u8,
                volume: 5,
                effect: 0,
            };
            sfx[scratch] = Sfx {
                notes: [probe; SFX_LEN],
                ..Sfx::default()
            };
            (sfx, scratch)
        } else {
            (assets.sfx.clone(), self.sfx)
        };
        audio.load(sfx, assets.music.clone());
        let playing = audio.with_synth(|s| s.channel_sfx()[0]);
        if playing == Some(target) {
            audio.play_sfx(-1, 0);
        } else {
            audio.play_sfx(target as i32, 0);
        }
    }

    pub fn key(&mut self, key: Key, mods: Mods, assets: &mut Assets, audio: &AudioHandle) {
        if mods.ctrl {
            if let Key::Char(c) = key {
                match c.to_ascii_lowercase() {
                    'z' if mods.shift => {
                        self.history.redo(&mut assets.sfx);
                        return;
                    }
                    'z' => {
                        self.history.undo(&mut assets.sfx);
                        return;
                    }
                    'y' => {
                        self.history.redo(&mut assets.sfx);
                        return;
                    }
                    _ => {}
                }
            }
        }
        // Snapshot before, commit after: a key edit is one undo step, and the
        // compare drops keys that change nothing (Tab, navigation, preview).
        self.history.begin(&assets.sfx);
        match key {
            // TAB toggles pitch <-> tracker (wave mode is left via its own button).
            Key::Tab => {
                self.mode = match self.mode {
                    SfxMode::Pitch => SfxMode::Tracker,
                    _ => SfxMode::Pitch,
                }
            }
            Key::PageUp => self.sfx = (self.sfx + 63) % 64,
            Key::PageDown => self.sfx = (self.sfx + 1) % 64,
            Key::Char(' ') => self.preview(assets, audio),
            Key::Char('-') => {
                let s = &mut assets.sfx[self.sfx];
                s.speed = s.speed.saturating_sub(1).max(1);
            }
            Key::Char('=') | Key::Char('+') => {
                let s = &mut assets.sfx[self.sfx];
                s.speed = s.speed.saturating_add(1);
            }
            _ if self.mode == SfxMode::Tracker => self.tracker_key(key, mods, assets),
            _ => {}
        }
        self.history.commit(&assets.sfx);
    }

    fn tracker_key(&mut self, key: Key, mods: Mods, assets: &mut Assets) {
        match key {
            Key::Up => self.step = (self.step + SFX_LEN - 1) % SFX_LEN,
            Key::Down => self.step = (self.step + 1) % SFX_LEN,
            Key::Left => self.field = (self.field + FIELDS.len() - 1) % FIELDS.len(),
            Key::Right => self.field = (self.field + 1) % FIELDS.len(),
            Key::Delete | Key::Backspace => {
                assets.sfx[self.sfx].notes[self.step] = Default::default();
            }
            Key::Char('[') => {
                if mods.shift {
                    let s = &mut assets.sfx[self.sfx];
                    s.loop_end = s.loop_end.saturating_sub(1);
                } else {
                    self.octave = self.octave.saturating_sub(1);
                }
            }
            Key::Char(']') => {
                if mods.shift {
                    let s = &mut assets.sfx[self.sfx];
                    s.loop_end = (s.loop_end + 1).min(SFX_LEN as u8);
                } else {
                    self.octave = (self.octave + 1).min(4);
                }
            }
            Key::Char(c) => self.char_input(c, assets),
            _ => {}
        }
    }

    fn char_input(&mut self, c: char, assets: &mut Assets) {
        let note = &mut assets.sfx[self.sfx].notes[self.step];
        match FIELDS[self.field] {
            Field::Note => {
                let (offset, oct_up) = if let Some(i) = PIANO_LOW.find(c) {
                    (i as u8, 0)
                } else if let Some(i) = PIANO_HIGH.find(c) {
                    (i as u8, 1)
                } else {
                    return;
                };
                note.pitch = ((self.octave + oct_up) * 12 + offset).min(63);
                if note.volume == 0 {
                    note.volume = 5;
                }
                self.step = (self.step + 1) % SFX_LEN;
            }
            Field::Wave => {
                if c == 'i' {
                    note.wave ^= NOTE_CUSTOM_FLAG;
                } else if let Some(d) = c.to_digit(8) {
                    note.wave = (note.wave & NOTE_CUSTOM_FLAG) | d as u8;
                }
            }
            Field::Vol => {
                if let Some(d) = c.to_digit(8) {
                    note.volume = d as u8;
                }
            }
            Field::Fx => {
                if let Some(d) = c.to_digit(8) {
                    note.effect = d as u8;
                }
            }
        }
    }

    pub fn tick(&mut self, mouse: &Mouse, assets: &mut Assets, audio: &AudioHandle) {
        self.status.tick();
        let _ = audio;
        let m = *mouse;
        // Bracket each mouse gesture for undo. Edits land on the press/hold
        // frames (spinners, drag-drawing), never on release, so a snapshot taken
        // while the button is down and committed once it comes up captures the
        // whole gesture. The commit compares, so non-editing clicks (mode
        // buttons, slot selection) record nothing.
        if m.left || m.right {
            self.history.begin(&assets.sfx);
        } else {
            self.history.commit(&assets.sfx);
        }
        // Press-edge controls: buttons, spinners, selectors, palette.
        if (m.left_pressed || m.right_pressed) && self.handle_press(&m, assets) {
            return;
        }
        // Click-and-drag drawing on the graph / volume strip / wave canvas.
        if m.left {
            match self.mode {
                SfxMode::Pitch => self.pitch_draw(&m, assets),
                SfxMode::Wave => self.wave_draw(&m, assets),
                SfxMode::Tracker => {}
            }
        }
    }

    /// Handle a press on a button/spinner/selector. Returns `true` when it
    /// consumed the click (so drag-drawing is skipped).
    fn handle_press(&mut self, m: &Mouse, assets: &mut Assets) -> bool {
        let delta: i32 = if m.right_pressed { -1 } else { 1 };
        // Top-left mode buttons (drawn by the shell).
        if m.left_pressed && m.y < 8 {
            if m.over(4, 0, 12, 7) {
                self.mode = SfxMode::Pitch;
                return true;
            } else if m.over(13, 0, 22, 7) {
                self.mode = SfxMode::Tracker;
                return true;
            }
        }
        // SFX selector: left arrow decrements, right arrow increments, and a
        // click on the number itself nudges by the click's direction.
        if m.left_pressed && m.over(4, 9, 8, 16) {
            self.sfx = (self.sfx + 63) % 64;
            return true;
        } else if m.left_pressed && m.over(20, 9, 25, 16) {
            self.sfx = (self.sfx + 1) % 64;
            return true;
        } else if m.over(9, 9, 19, 16) {
            self.sfx = (self.sfx as i32 + delta).rem_euclid(64) as usize;
            return true;
        } else if m.over(49, 9, 59, 16) {
            let s = &mut assets.sfx[self.sfx];
            s.speed = (s.speed as i32 + delta).clamp(1, 255) as u8;
            return true;
        } else if m.over(90, 9, 100, 16) {
            let s = &mut assets.sfx[self.sfx];
            s.loop_start = (s.loop_start as i32 + delta).clamp(0, 31) as u8;
            return true;
        } else if m.over(104, 9, 114, 16) {
            let s = &mut assets.sfx[self.sfx];
            s.loop_end = (s.loop_end as i32 + delta).clamp(0, 32) as u8;
            return true;
        } else if self.sfx < 8 && m.left_pressed && m.over(118, 9, 126, 16) {
            // The wave toggle flips between the wave designer and pitch view.
            self.mode = if self.mode == SfxMode::Wave {
                SfxMode::Pitch
            } else {
                SfxMode::Wave
            };
            return true;
        }
        match self.mode {
            SfxMode::Pitch => {
                // The circle toggles the palette between waveform glyphs and
                // plain numbers.
                if m.left_pressed && m.over(117, 18, 125, 25) {
                    self.palette_numeric = !self.palette_numeric;
                    return true;
                }
                // Palette: click selects the drawing instrument.
                for w in 0..8u8 {
                    let x = 46 + w as i32 * 9;
                    if m.left_pressed && m.over(x, 18, x + 7, 24) {
                        self.wave_sel = w;
                        return true;
                    }
                }
                // Right-click in the graph grabs that note's instrument.
                let step = (m.x - 3) / 4;
                if m.right_pressed
                    && (0..SFX_LEN as i32).contains(&step)
                    && (G_TOP..=G_BOT).contains(&m.y)
                {
                    if let Some(slot) = assets.sfx[self.sfx].notes[step as usize].instrument() {
                        self.wave_sel = slot;
                    }
                    return true;
                }
                false
            }
            SfxMode::Tracker => {
                self.tracker_tick(m, assets, delta);
                true
            }
            SfxMode::Wave => {
                // bass toggle under the canvas.
                if m.left_pressed && m.over(4, WAVE_BOT + 3, 28, WAVE_BOT + 9) {
                    let s = &mut assets.sfx[self.sfx];
                    let mut w = s.custom_wave.unwrap_or(CustomWave {
                        samples: [0; SFX_LEN],
                        bass: false,
                    });
                    w.bass = !w.bass;
                    s.custom_wave = Some(w);
                    return true;
                }
                false
            }
        }
    }

    /// Drag on the pitch graph (sets pitch with the selected instrument) or on
    /// the volume strip (sets volume).
    fn pitch_draw(&mut self, m: &Mouse, assets: &mut Assets) {
        let step = (m.x - 3) / 4;
        if !(0..SFX_LEN as i32).contains(&step) {
            return;
        }
        let note = &mut assets.sfx[self.sfx].notes[step as usize];
        if (G_TOP..=G_BOT).contains(&m.y) {
            note.pitch = ((G_BOT - m.y) * 63 / (G_BOT - G_TOP)).clamp(0, 63) as u8;
            if note.volume == 0 {
                note.volume = 5;
            }
            note.wave = (note.wave & NOTE_CUSTOM_FLAG) | self.wave_sel;
        } else if (V_TOP..=V_BOT).contains(&m.y) {
            note.volume = ((V_BOT - m.y) * 7 / (V_BOT - V_TOP)).clamp(0, 7) as u8;
        }
    }

    /// Drag on the wave-designer canvas to draw the waveform samples.
    fn wave_draw(&mut self, m: &Mouse, assets: &mut Assets) {
        let step = (m.x - 3) / 4;
        if self.sfx >= 8
            || !(0..SFX_LEN as i32).contains(&step)
            || !(WAVE_TOP..=WAVE_BOT).contains(&m.y)
        {
            return;
        }
        let mid = (WAVE_TOP + WAVE_BOT) / 2;
        let value = ((mid - m.y) * 16 / ((WAVE_BOT - WAVE_TOP) / 2)).clamp(-16, 15) as i8;
        let w = assets.sfx[self.sfx].custom_wave.get_or_insert(CustomWave {
            samples: [0; SFX_LEN],
            bass: false,
        });
        w.samples[step as usize] = value;
    }

    fn tracker_tick(&mut self, m: &Mouse, assets: &mut Assets, delta: i32) {
        // Filter switches.
        for i in 0..FX_LABELS.len() {
            let y = FX_Y + i as i32 * FX_DY;
            if m.over(FX_X, y - 1, FX_X + 9, y + 5) {
                let s = &mut assets.sfx[self.sfx];
                match i {
                    0 => s.noiz = !s.noiz,
                    1 => s.buzz = !s.buzz,
                    2 => s.detune = (s.detune as i32 + delta).rem_euclid(3) as u8,
                    3 => s.reverb = (s.reverb as i32 + delta).rem_euclid(3) as u8,
                    _ => s.dampen = (s.dampen as i32 + delta).rem_euclid(3) as u8,
                }
                return;
            }
        }
        // Step grid: pick the step + field under the cursor (note cell at x+5).
        for (half, x) in COL_X.iter().enumerate() {
            if m.over(x - 5, STEP_Y, x + 37, STEP_Y + 16 * 6 - 1) {
                self.step = half * 16 + (((m.y - STEP_Y - 1).max(0) / 6) as usize).min(15);
                let cx = m.x - (x + 5);
                self.field = match cx {
                    ..=16 => 0,
                    17..=21 => 1,
                    22..=26 => 2,
                    _ => 3,
                };
            }
        }
    }

    pub fn draw(&self, fb: &mut Framebuffer, assets: &Assets, audio: &AudioHandle) {
        fb.rectfill(0, 8, 127, 119, col::DARK_GREY);
        self.draw_header(fb, assets);
        match self.mode {
            SfxMode::Pitch => self.draw_pitch(fb, assets, audio),
            SfxMode::Tracker => self.draw_tracker(fb, assets, audio),
            SfxMode::Wave => self.draw_wave(fb, assets),
        }
    }

    fn draw_header(&self, fb: &mut Framebuffer, assets: &Assets) {
        let s = &assets.sfx[self.sfx];
        ui::arrow_l(fb, 4, 11, col::PINK);
        fb.print(&format!("{:02}", self.sfx), 11, 11, col::WHITE);
        ui::arrow_r(fb, 21, 11, col::PINK);
        fb.print("Spd", 35, 11, col::LIGHT_GREY);
        fb.rectfill(49, 9, 59, 16, col::BLACK);
        fb.print(&format!("{:02}", s.speed), 51, 11, col::WHITE);
        // "loop" with both bounds, or "len" when only the start is set.
        let is_len = s.loop_end == 0 && s.loop_start > 0;
        fb.print(if is_len { "Len" } else { "Loop" }, 70, 11, col::LIGHT_GREY);
        fb.rectfill(90, 9, 100, 16, col::BLACK);
        fb.print(&format!("{:02}", s.loop_start), 92, 11, col::WHITE);
        fb.rectfill(104, 9, 114, 16, col::BLACK);
        fb.print(&format!("{:02}", s.loop_end), 106, 11, col::WHITE);
        // Wave-designer toggle, only meaningful for the 8 instrument slots.
        if self.sfx < 8 {
            ui::blit(fb, 118, 11, &ui::WAVEI);
        }
    }

    fn draw_pitch(&self, fb: &mut Framebuffer, assets: &Assets, audio: &AudioHandle) {
        let s = &assets.sfx[self.sfx];
        let head = audio.with_synth(|sy| {
            if sy.channel_sfx()[0] == Some(self.sfx) {
                sy.channel_step()[0]
            } else {
                None
            }
        });
        fb.print(":Pitch", 3, 20, col::LAVENDER);
        self.draw_palette(fb);
        ui::blit(fb, 117, 19, &ui::CIRCLE);

        // Black graph + volume panels.
        fb.rectfill(0, 27, 127, G_BOT, col::BLACK);
        fb.rectfill(0, V_TOP - 2, 127, 118, col::BLACK);

        for (i, note) in s.notes.iter().enumerate() {
            let x = 3 + i as i32 * 4;
            let playing = head == Some(i);
            if note.volume > 0 {
                // PICO-8 draws dark-blue bars and colours the marker on top by
                // the note's waveform/instrument; the playing step is yellow.
                let bar = if playing { col::YELLOW } else { col::DARK_BLUE };
                let h = (note.pitch as i32 * (G_BOT - G_TOP) / 63).max(1);
                fb.rectfill(x, G_BOT - h, x + 2, G_BOT, bar);
                let tip = if playing {
                    col::WHITE
                } else {
                    wave_color(note)
                };
                fb.rectfill(x, G_BOT - h, x + 2, G_BOT - h + 1, tip);
            }
            // Volume marker.
            let vy = V_BOT - note.volume as i32 * (V_BOT - V_TOP) / 7;
            let vc = if playing { col::YELLOW } else { col::PINK };
            fb.rectfill(x, vy, x + 2, vy + 1, vc);
        }

        fb.print(":Volume", 3, 95, col::LAVENDER);
        for i in 0..3 {
            fb.line(118 + i * 2, 96, 118 + i * 2, 99, col::DARK_BLUE);
        }
        self.status.show(fb, "Tab tracker  drag pitch/vol");
    }

    /// The waveform palette: 8 boxes (graphical glyphs or plain numbers), with
    /// the selected waveform's box in red.
    fn draw_palette(&self, fb: &mut Framebuffer) {
        if self.palette_numeric {
            for w in 0..8u8 {
                let x = 46 + w as i32 * 9;
                let sel = w == self.wave_sel;
                fb.rectfill(
                    x,
                    18,
                    x + 7,
                    24,
                    if sel { col::RED } else { col::LIGHT_GREY },
                );
                let c = if sel { col::WHITE } else { col::DARK_GREY };
                fb.print(&format!("{w}"), x + 2, 19, c);
            }
        } else {
            ui::blit(fb, 46, 19, &ui::PALETTE);
            // Move the red selection box to the chosen waveform.
            if self.wave_sel != 0 {
                recolor_box(fb, 46, col::RED, col::LIGHT_GREY);
                recolor_box(fb, 46 + self.wave_sel as i32 * 9, col::LIGHT_GREY, col::RED);
            }
        }
    }

    fn draw_tracker(&self, fb: &mut Framebuffer, assets: &Assets, audio: &AudioHandle) {
        let s = &assets.sfx[self.sfx];
        let head = audio.with_synth(|sy| {
            if sy.channel_sfx()[0] == Some(self.sfx) {
                sy.channel_step()[0]
            } else {
                None
            }
        });
        let len = note_rows(s.loop_start, s.loop_end);
        let loops = s.loop_end > s.loop_start || s.loop_start > 0;
        for (half, &x) in COL_X.iter().enumerate() {
            let nx = x + 5; // note-cell origin, clear of the step gutter
            fb.rectfill(x - 5, STEP_Y - 1, x + 37, 118, col::BLACK);
            for row in 0..16usize {
                let i = half * 16 + row;
                let y = STEP_Y + 1 + row as i32 * 6;
                if i < len && loops {
                    fb.rectfill(x - 5, y, x - 4, y + 4, col::DARK_GREEN);
                }
                if head == Some(i) {
                    fb.rectfill(x - 3, y - 1, x + 34, y + 5, col::DARK_BLUE);
                }
                fb.print(&format!("{i:02}"), x - 3, y, col::DARK_GREY);
                ui::note_cell(fb, nx, y, s.notes[i]);
                // Cursor box on the focused field.
                if i == self.step {
                    let (fx0, fw) = match FIELDS[self.field] {
                        Field::Note => (nx + 2, 7),
                        Field::Wave => (nx + 15, 3),
                        Field::Vol => (nx + 20, 3),
                        Field::Fx => (nx + 24, 3),
                    };
                    fb.rect(fx0 - 1, y - 1, fx0 + fw, y + 5, col::WHITE);
                }
            }
        }
        // Filter switches strip between the columns.
        let levels = [s.noiz as u8, s.buzz as u8, s.detune, s.reverb, s.dampen];
        for (i, (label, &level)) in FX_LABELS.iter().zip(levels.iter()).enumerate() {
            let y = FX_Y + i as i32 * FX_DY;
            let on = level > 0;
            fb.print(label, FX_X, y, if on { col::WHITE } else { col::DARK_GREY });
            let val = if i < 2 {
                if on {
                    "*".into()
                } else {
                    "-".into()
                }
            } else {
                format!("{level}")
            };
            let c = if on { col::ORANGE } else { col::DARK_GREY };
            fb.print(&val, FX_X + 12, y, c);
        }
        self.status
            .show(fb, &format!("Tab pitch  oct {} zsxd..", self.octave));
    }

    fn draw_wave(&self, fb: &mut Framebuffer, assets: &Assets) {
        let s = &assets.sfx[self.sfx];
        fb.print(":Wave", 3, 20, col::LAVENDER);
        fb.rectfill(0, 27, 127, WAVE_BOT + 1, col::BLACK);
        let mid = (WAVE_TOP + WAVE_BOT) / 2;
        fb.line(2, mid, 125, mid, col::DARK_GREY);
        let wave = s.custom_wave.unwrap_or(CustomWave {
            samples: [0; SFX_LEN],
            bass: false,
        });
        for (i, &v) in wave.samples.iter().enumerate() {
            let x = 3 + i as i32 * 4;
            let y = mid - v as i32 * ((WAVE_BOT - WAVE_TOP) / 2) / 16;
            fb.rectfill(x, mid.min(y), x + 2, mid.max(y), col::LAVENDER);
            fb.rectfill(x, y, x + 2, y, col::WHITE);
        }
        // bass toggle.
        let on = wave.bass;
        fb.rectfill(4, WAVE_BOT + 3, 28, WAVE_BOT + 9, col::BLACK);
        fb.print(
            "Bass",
            6,
            WAVE_BOT + 4,
            if on { col::ORANGE } else { col::DARK_GREY },
        );
        self.status.show(fb, "Tab pitch  drag to draw wave");
    }

    /// Set a transient bottom-bar message (used for clipboard errors).
    pub fn set_status(&mut self, msg: String) {
        self.status.set(msg);
    }

    /// Paste a decoded PICO-8 clipboard blob. Only `[sfx]` applies here: its
    /// records overwrite consecutive slots from the selected one.
    pub fn paste(&mut self, pasted: &Pasted, assets: &mut Assets) {
        self.history.begin(&assets.sfx);
        match pasted {
            Pasted::Sfx(clip) => {
                let report = clipboard::paste_sfx(&mut assets.sfx, &clip.records, self.sfx);
                self.status.set(report.summary);
            }
            Pasted::Sprites { .. } => self.status.set("sprites - use sprite editor".into()),
            Pasted::Map { .. } => self.status.set("map - use map editor".into()),
        }
        self.history.commit(&assets.sfx);
    }

    /// Copy the selected SFX slot (full fidelity, incl. custom wave) as a native blob.
    pub fn copy(&mut self, assets: &Assets) -> String {
        self.status.set(format!("copied SFX {}", self.sfx));
        clipboard::encode(&ClipboardPayload::Sfx {
            slot: self.sfx as u8,
            sfx: assets.sfx[self.sfx].clone(),
        })
    }
}

const WAVE_TOP: i32 = 30;
const WAVE_BOT: i32 = 104;

/// A distinct colour per built-in waveform (0..8), so pitch-graph bars show
/// which instrument each note uses.
const WAVE_COLS: [u8; 8] = [
    col::DARK_BLUE,
    col::BLUE,
    col::LAVENDER,
    col::DARK_GREEN,
    col::GREEN,
    col::ORANGE,
    col::PINK,
    col::LIGHT_GREY,
];

/// The pitch-bar colour for a note: its waveform's colour, or peach when it
/// references a custom instrument.
fn wave_color(note: &Note) -> u8 {
    if note.instrument().is_some() {
        col::PEACH
    } else {
        WAVE_COLS[note.wave_index() as usize]
    }
}

/// Repaint a 8x6 palette box's background colour (cols x..x+7, rows 19..24),
/// leaving the white waveform glyph untouched. Used to move the red selection.
fn recolor_box(fb: &mut Framebuffer, x: i32, from: u8, to: u8) {
    for yy in 19..25 {
        for xx in x..x + 8 {
            if fb.pget(xx, yy) == from {
                fb.pset(xx, yy, to);
            }
        }
    }
}

/// The playable step count of an SFX (loop end, LEN marker, or the full 32).
fn note_rows(loop_start: u8, loop_end: u8) -> usize {
    if loop_end > loop_start {
        loop_end as usize
    } else if loop_start > 0 {
        loop_start as usize
    } else {
        SFX_LEN
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn dummy() -> AudioHandle {
        AudioHandle::dummy()
    }

    #[test]
    fn tab_toggles_pitch_and_tracker() {
        let mut ed = SfxEditor::new();
        let mut a = Assets::default();
        assert_eq!(ed.mode, SfxMode::Pitch);
        ed.key(Key::Tab, Mods::default(), &mut a, &dummy());
        assert_eq!(ed.mode, SfxMode::Tracker);
        ed.key(Key::Tab, Mods::default(), &mut a, &dummy());
        assert_eq!(ed.mode, SfxMode::Pitch);
    }

    #[test]
    fn wave_preview_plays_through_the_drawn_waveform() {
        use pixel8_runtime::assets::SFX_LEN;
        let mut a = Assets::default();
        a.sfx[0].custom_wave = Some(CustomWave {
            samples: [15; SFX_LEN],
            bass: false,
        });
        let mut ed = SfxEditor::new();
        ed.mode = SfxMode::Wave;
        ed.sfx = 0;
        let audio = dummy();
        ed.preview(&a, &audio);
        let mut peak = 0.0f32;
        audio.with_synth(|s| {
            for _ in 0..2000 {
                peak = peak.max(s.next_sample().abs());
            }
        });
        assert!(peak > 0.01, "drawing into the waveform should be audible");
    }

    fn ctrl(shift: bool) -> Mods {
        Mods {
            ctrl: true,
            shift,
            ..Default::default()
        }
    }

    #[test]
    fn undo_and_redo_a_speed_change() {
        let mut ed = SfxEditor::new();
        let mut a = Assets::default();
        let start = a.sfx[0].speed;
        ed.key(Key::Char('='), Mods::default(), &mut a, &dummy());
        assert_eq!(a.sfx[0].speed, start + 1);
        ed.key(Key::Char('z'), ctrl(false), &mut a, &dummy());
        assert_eq!(a.sfx[0].speed, start, "undo restores the speed");
        ed.key(Key::Char('z'), ctrl(true), &mut a, &dummy()); // Ctrl+Shift+Z
        assert_eq!(a.sfx[0].speed, start + 1, "redo re-applies it");
    }

    #[test]
    fn undo_reverts_a_wave_drag() {
        let mut ed = SfxEditor::new();
        ed.mode = SfxMode::Wave;
        let mut a = Assets::default();
        let m = Mouse {
            x: 4,
            y: WAVE_TOP + 2,
            left: true,
            left_pressed: true,
            ..Default::default()
        };
        ed.tick(&m, &mut a, &dummy());
        ed.tick(&Mouse::default(), &mut a, &dummy()); // release closes the stroke.
        assert!(a.sfx[0].custom_wave.is_some());
        ed.key(Key::Char('z'), ctrl(false), &mut a, &dummy());
        assert!(
            a.sfx[0].custom_wave.is_none(),
            "undo removes the drawn waveform"
        );
    }

    #[test]
    fn wave_drag_draws_into_custom_wave() {
        let mut ed = SfxEditor::new();
        ed.mode = SfxMode::Wave;
        let mut a = Assets::default();
        // Drag near the top of the canvas at step 0 -> a high positive sample.
        let m = Mouse {
            x: 4,
            y: WAVE_TOP + 2,
            left: true,
            left_pressed: true,
            ..Default::default()
        };
        ed.tick(&m, &mut a, &dummy());
        let w = a.sfx[0].custom_wave.expect("wave created");
        assert!(
            w.samples[0] > 8,
            "drawn sample should be high, got {}",
            w.samples[0]
        );
    }
}

#[cfg(test)]
mod paste_tests {
    use super::*;
    use pixel8_runtime::clipboard::{parse, Pasted, SfxClip, Slotted};

    fn one_sfx(pitch: u8) -> Sfx {
        let mut s = Sfx::default();
        s.notes[0].pitch = pitch;
        s.notes[0].volume = 4;
        s
    }

    #[test]
    fn pastes_sfx_from_selected_slot() {
        let mut ed = SfxEditor::new();
        ed.select(7);
        let mut assets = Assets::default();
        let clip = SfxClip {
            records: vec![Slotted {
                src: 0,
                value: one_sfx(33),
            }],
            patterns: vec![],
        };
        ed.paste(&Pasted::Sfx(clip), &mut assets);
        assert_eq!(assets.sfx[7].notes[0].pitch, 33);
        assert!(ed.status.current().unwrap().contains("SFX 7"));
    }

    #[test]
    fn copies_selected_sfx() {
        let mut ed = SfxEditor::new();
        ed.select(7);
        let mut assets = Assets::default();
        assets.sfx[7].notes[0] = Note {
            pitch: 40,
            wave: 2,
            volume: 5,
            effect: 0,
        };
        let blob = ed.copy(&assets);
        assert!(ed.status.current().unwrap().contains("copied SFX 7"));
        let Pasted::Sfx(clip) = parse(&blob).unwrap() else {
            panic!("not sfx")
        };
        assert_eq!(clip.records[0].src, 7);
        assert_eq!(clip.records[0].value.notes[0].pitch, 40);
    }

    #[test]
    fn undo_and_redo_a_paste() {
        let mut ed = SfxEditor::new();
        ed.select(7);
        let mut assets = Assets::default();
        let clip = SfxClip {
            records: vec![Slotted {
                src: 0,
                value: one_sfx(33),
            }],
            patterns: vec![],
        };
        ed.paste(&Pasted::Sfx(clip), &mut assets);
        assert_eq!(assets.sfx[7].notes[0].pitch, 33);

        let ctrl = Mods {
            ctrl: true,
            shift: false,
            ..Default::default()
        };
        let ctrl_shift = Mods {
            ctrl: true,
            shift: true,
            ..Default::default()
        };
        ed.key(Key::Char('z'), ctrl, &mut assets, &AudioHandle::dummy());
        assert_eq!(assets.sfx[7].notes[0].pitch, 0, "undo restores the slot");
        ed.key(
            Key::Char('z'),
            ctrl_shift,
            &mut assets,
            &AudioHandle::dummy(),
        );
        assert_eq!(
            assets.sfx[7].notes[0].pitch, 33,
            "redo re-applies the paste"
        );
    }
}