yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
tracker_view.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <vector>
5
6#include "absl/strings/str_format.h"
8#include "imgui/imgui.h"
9
10namespace yaze {
11namespace editor {
12namespace music {
13
14using namespace yaze::zelda3::music;
15
16namespace {
17// Theme-aware color helpers
18ImU32 GetColorNote() {
19 const auto& theme = gui::ThemeManager::Get().GetCurrentTheme();
20 return ImGui::GetColorU32(gui::ConvertColorToImVec4(theme.success));
21}
22
24 const auto& theme = gui::ThemeManager::Get().GetCurrentTheme();
25 return ImGui::GetColorU32(gui::ConvertColorToImVec4(theme.info));
26}
27
29 const auto& theme = gui::ThemeManager::Get().GetCurrentTheme();
30 return ImGui::GetColorU32(gui::ConvertColorToImVec4(theme.warning));
31}
32
34 const auto& theme = gui::ThemeManager::Get().GetCurrentTheme();
35 auto color = theme.active_selection;
36 color.alpha = 0.15f; // Low opacity for subtle highlight
37 return ImGui::GetColorU32(gui::ConvertColorToImVec4(color));
38}
39
40ImU32 GetColorSelection(bool is_range) {
41 const auto& theme = gui::ThemeManager::Get().GetCurrentTheme();
42 auto color = theme.editor_selection;
43 color.alpha = is_range ? 0.4f : 0.7f;
44 return ImGui::GetColorU32(gui::ConvertColorToImVec4(color));
45}
46std::string DescribeCommand(uint8_t opcode) {
47 switch (opcode) {
48 case 0xE0:
49 return "Set Instrument";
50 case 0xE1:
51 return "Set Pan";
52 case 0xE5:
53 return "Master Volume";
54 case 0xE6:
55 return "Master Volume Fade";
56 case 0xE7:
57 return "Set Tempo";
58 case 0xE8:
59 return "Tempo Fade";
60 case 0xE9:
61 return "Global Transpose";
62 case 0xEA:
63 return "Channel Transpose";
64 case 0xEB:
65 return "Tremolo On";
66 case 0xEC:
67 return "Tremolo Off";
68 case 0xED:
69 return "Channel Volume";
70 case 0xEE:
71 return "Channel Volume Fade";
72 case 0xEF:
73 return "Call Subroutine";
74 case 0xF0:
75 return "Vibrato Fade";
76 case 0xF1:
77 return "Pitch Env To";
78 case 0xF2:
79 return "Pitch Env From";
80 case 0xF3:
81 return "Pitch Env Off";
82 case 0xF4:
83 return "Tuning";
84 case 0xF5:
85 return "Echo Bits";
86 case 0xF6:
87 return "Echo Off";
88 case 0xF7:
89 return "Echo Params";
90 case 0xF8:
91 return "Echo Vol Fade";
92 case 0xF9:
93 return "Pitch Slide";
94 case 0xFA:
95 return "Percussion Patch";
96 default:
97 return "Command";
98 }
99}
100
101// Command options for combo box
103 const char* name;
104 uint8_t opcode;
105};
106
108 {"Set Instrument", 0xE0}, {"Set Pan", 0xE1},
109 {"Vibrato On", 0xE3}, {"Vibrato Off", 0xE4},
110 {"Master Volume", 0xE5}, {"Master Volume Fade", 0xE6},
111 {"Set Tempo", 0xE7}, {"Tempo Fade", 0xE8},
112 {"Global Transpose", 0xE9}, {"Channel Transpose", 0xEA},
113 {"Tremolo On", 0xEB}, {"Tremolo Off", 0xEC},
114 {"Channel Volume", 0xED}, {"Channel Volume Fade", 0xEE},
115 {"Call Subroutine", 0xEF}, {"Vibrato Fade", 0xF0},
116 {"Pitch Env To", 0xF1}, {"Pitch Env From", 0xF2},
117 {"Pitch Env Off", 0xF3}, {"Tuning", 0xF4},
118 {"Echo Bits", 0xF5}, {"Echo Off", 0xF6},
119 {"Echo Params", 0xF7}, {"Echo Vol Fade", 0xF8},
120 {"Pitch Slide", 0xF9}, {"Percussion Patch", 0xFA},
121};
122} // namespace
123
124void TrackerView::Draw(MusicSong* song, const MusicBank* bank) {
125 if (!song) {
126 ImGui::TextDisabled("No song loaded");
127 return;
128 }
129
130 // Handle input before drawing to avoid 1-frame lag
131 if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)) {
135 }
136
137 DrawToolbar(song);
138 ImGui::Separator();
139
140 ImGui::BeginChild("TrackerGrid", ImVec2(0, 0), true);
141 DrawGrid(song, bank);
142 ImGui::EndChild();
143}
144
146 ImGui::Text("%s", song->name.empty() ? "Untitled" : song->name.c_str());
147 ImGui::SameLine();
148 ImGui::TextDisabled("(%d ticks)", song->GetTotalDuration());
149
150 ImGui::SameLine();
151 ImGui::Text("| Bank: %s", song->bank == 0
152 ? "Overworld"
153 : (song->bank == 1 ? "Dungeon" : "Credits"));
154
155 ImGui::SameLine();
156 ImGui::PushItemWidth(100);
157 if (ImGui::DragInt("Ticks/Row", &ticks_per_row_, 1, 1, 96)) {
158 if (ticks_per_row_ < 1)
159 ticks_per_row_ = 1;
160 }
161 ImGui::PopItemWidth();
162}
163
164void TrackerView::DrawGrid(MusicSong* song, const MusicBank* bank) {
165 if (song->segments.empty())
166 return;
167
168 // Use the first segment for now (TODO: Handle multiple segments)
169 // Non-const reference to allow editing
170 auto& segment = song->segments[0];
171 uint32_t duration = segment.GetDuration();
172
173 // Table setup: Row number + 8 Channels
174 if (ImGui::BeginTable("TrackerTable", 9,
175 ImGuiTableFlags_Borders | ImGuiTableFlags_ScrollY |
176 ImGuiTableFlags_RowBg |
177 ImGuiTableFlags_Resizable)) {
178
179 // Header
180 ImGui::TableSetupColumn("Tick", ImGuiTableColumnFlags_WidthFixed, 50.0f);
181 for (int i = 0; i < 8; ++i) {
182 ImGui::TableSetupColumn(absl::StrFormat("Ch %d", i + 1).c_str());
183 }
184 ImGui::TableHeadersRow();
185
186 // Rows
187 int total_rows = (duration + ticks_per_row_ - 1) / ticks_per_row_;
188 ImGuiListClipper clipper;
189 clipper.Begin(total_rows, row_height_);
190
191 while (clipper.Step()) {
192 for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row) {
193 int tick_start = row * ticks_per_row_;
194 int tick_end = tick_start + ticks_per_row_;
195
196 ImGui::TableNextRow();
197
198 // Highlight every 4th row (beat)
199 if (row % 4 == 0) {
200 ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0,
201 GetColorBeatHighlight());
202 }
203
204 // Tick Number Column
205 ImGui::TableSetColumnIndex(0);
206 ImGui::TextDisabled("%04X", tick_start);
207
208 // Channel Columns
209 for (int ch = 0; ch < 8; ++ch) {
210 ImGui::TableSetColumnIndex(ch + 1);
211
212 // Selection drawing
213 bool is_selected =
214 (row == selected_row_ && (ch + 1) == selected_col_);
215
216 // Calculate range selection
217 if (selection_anchor_row_ != -1 && selection_anchor_col_ != -1) {
218 int row_start = std::min(selected_row_, selection_anchor_row_);
219 int row_end = std::max(selected_row_, selection_anchor_row_);
220 int col_start = std::min(selected_col_, selection_anchor_col_);
221 int col_end = std::max(selected_col_, selection_anchor_col_);
222
223 if (row >= row_start && row <= row_end && (ch + 1) >= col_start &&
224 (ch + 1) <= col_end) {
225 ImGui::TableSetBgColor(
226 ImGuiTableBgTarget_CellBg,
227 GetColorSelection(true)); // Range selection
228 }
229 } else if (is_selected) {
230 ImGui::TableSetBgColor(
231 ImGuiTableBgTarget_CellBg,
232 GetColorSelection(false)); // Single selection
233
234 // Auto-scroll to selection
235 if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)) {
236 // Simple scroll-into-view logic could go here if needed,
237 // but ImGui handles focused items usually well enough if using Selectable
238 }
239 }
240
241 // Find event in this time range
242 int event_index = -1;
243 auto& track = segment.tracks[ch];
244 for (size_t idx = 0; idx < track.events.size(); ++idx) {
245 const auto& evt = track.events[idx];
246 if (evt.tick >= tick_start && evt.tick < tick_end) {
247 event_index = static_cast<int>(idx);
248 break;
249 }
250 if (evt.tick >= tick_end)
251 break;
252 }
253
254 DrawEventCell(track, event_index, ch, tick_start, bank);
255
256 // Handle cell click for selection
257 // Invisible button to capture clicks
258 ImGui::PushID(row * 100 + ch);
259 if (ImGui::Selectable("##cell", false,
260 ImGuiSelectableFlags_SpanAllColumns |
261 ImGuiSelectableFlags_AllowOverlap,
262 ImVec2(0, row_height_))) {
263 if (ImGui::GetIO().KeyShift) {
264 if (selection_anchor_row_ == -1) {
267 }
268 } else {
271 }
272 selected_row_ = row;
273 selected_col_ = ch + 1;
274 }
275 ImGui::PopID();
276 }
277 }
278 }
279 ImGui::EndTable();
280 }
281}
282
283void TrackerView::DrawEventCell(MusicTrack& track, int event_index,
284 int channel_idx, uint16_t tick,
285 const MusicBank* bank) {
286 TrackEvent* event_ptr =
287 (event_index >= 0 && event_index < static_cast<int>(track.events.size()))
288 ? &track.events[event_index]
289 : nullptr;
290
291 bool has_event = event_ptr != nullptr;
292
293 if (!has_event) {
294 ImGui::TextDisabled("...");
295 } else {
296 auto& event = *event_ptr;
297 switch (event.type) {
298 case TrackEvent::Type::Note:
299 ImGui::TextColored(ImColor(GetColorNote()), "%s",
300 event.note.GetNoteName().c_str());
301 if (ImGui::IsItemHovered()) {
302 ImGui::SetTooltip("Note: %s\nDuration: %d\nVelocity: %d",
303 event.note.GetNoteName().c_str(),
304 event.note.duration, event.note.velocity);
305 }
306 break;
307
308 case TrackEvent::Type::Command: {
309 ImU32 color = GetColorCommand();
310 std::string label = absl::StrFormat("CMD %02X", event.command.opcode);
311 std::string tooltip = DescribeCommand(event.command.opcode);
312
313 // Improved display for common commands
314 if (event.command.opcode == 0xE0) { // Set Instrument
315 if (bank) {
316 const auto* inst = bank->GetInstrument(event.command.params[0]);
317 if (inst) {
318 label = absl::StrFormat("Instr: %s", inst->name.c_str());
319 tooltip =
320 absl::StrFormat("Set Instrument: %s (ID %02X)",
321 inst->name.c_str(), event.command.params[0]);
322 } else {
323 label = absl::StrFormat("Instr: %02X", event.command.params[0]);
324 }
325 } else {
326 label = absl::StrFormat("Instr: %02X", event.command.params[0]);
327 }
328 } else if (event.command.opcode == 0xE1) { // Set Pan
329 int pan = event.command.params[0];
330 if (pan == 0x0A)
331 label = "Pan: Center";
332 else if (pan < 0x0A)
333 label = absl::StrFormat("Pan: L%d", 0x0A - pan);
334 else
335 label = absl::StrFormat("Pan: R%d", pan - 0x0A);
336 } else if (event.command.opcode == 0xE7) { // Set Tempo
337 label = absl::StrFormat("Tempo: %d", event.command.params[0]);
338 } else if (event.command.opcode == 0xED) { // Channel Volume
339 label = absl::StrFormat("Vol: %d", event.command.params[0]);
340 } else if (event.command.opcode == 0xE5) { // Master Volume
341 label = absl::StrFormat("M.Vol: %d", event.command.params[0]);
342 }
343
344 ImGui::TextColored(ImColor(color), "%s", label.c_str());
345 if (ImGui::IsItemHovered()) {
346 ImGui::SetTooltip("%s\nOpcode: %02X\nParams: %02X %02X %02X",
347 tooltip.c_str(), event.command.opcode,
348 event.command.params[0], event.command.params[1],
349 event.command.params[2]);
350 }
351 break;
352 }
353
354 case TrackEvent::Type::SubroutineCall:
355 ImGui::TextColored(ImColor(GetColorSubroutine()), "CALL");
356 break;
357
358 case TrackEvent::Type::End:
359 ImGui::TextDisabled("END");
360 break;
361 }
362 }
363
364 bool hovered = ImGui::IsItemHovered();
365 const bool double_clicked =
366 hovered && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left);
367 const bool right_clicked =
368 hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Right);
369
370 // If empty and double-clicked, insert a default note
371 if (!has_event && double_clicked) {
373 tick, static_cast<uint8_t>(kNoteMinPitch + 36), kDurationSixteenth);
374 track.InsertEvent(evt);
375 if (on_edit_)
376 on_edit_();
377 return;
378 }
379
380 const std::string popup_id =
381 absl::StrFormat("EditEvent##%d_%d_%d", channel_idx, tick, event_index);
382 if ((double_clicked || right_clicked) && has_event) {
383 ImGui::OpenPopup(popup_id.c_str());
384 }
385
386 if (ImGui::BeginPopup(popup_id.c_str())) {
387 if (!has_event) {
388 ImGui::TextDisabled("Empty");
389 ImGui::EndPopup();
390 return;
391 }
392
393 auto& event = *event_ptr;
394 if (event.type == TrackEvent::Type::Note) {
395 static int edit_pitch = kNoteMinPitch + 36;
396 static int edit_duration = kDurationSixteenth;
397 edit_pitch = event.note.pitch;
398 edit_duration = event.note.duration;
399
400 static std::vector<std::string> note_labels;
401 if (note_labels.empty()) {
402 for (int p = kNoteMinPitch; p <= kNoteMaxPitch; ++p) {
403 Note n;
404 n.pitch = static_cast<uint8_t>(p);
405 note_labels.push_back(n.GetNoteName());
406 }
407 }
408 int idx = edit_pitch - kNoteMinPitch;
409 idx = std::clamp(idx, 0, static_cast<int>(note_labels.size()) - 1);
410
411 ImGui::Text("Edit Note");
412 if (ImGui::BeginCombo("Pitch", note_labels[idx].c_str())) {
413 for (int i = 0; i < static_cast<int>(note_labels.size()); ++i) {
414 bool sel = (i == idx);
415 if (ImGui::Selectable(note_labels[i].c_str(), sel)) {
416 idx = i;
417 edit_pitch = kNoteMinPitch + i;
418 }
419 if (sel)
420 ImGui::SetItemDefaultFocus();
421 }
422 ImGui::EndCombo();
423 }
424
425 if (ImGui::SliderInt("Duration", &edit_duration, 1, 0xFF)) {
426 edit_duration = std::clamp(edit_duration, 1, 0xFF);
427 }
428
429 if (ImGui::Button("Apply")) {
430 event.note.pitch = static_cast<uint8_t>(edit_pitch);
431 event.note.duration = static_cast<uint8_t>(edit_duration);
432 if (on_edit_)
433 on_edit_();
434 ImGui::CloseCurrentPopup();
435 }
436 } else if (event.type == TrackEvent::Type::Command) {
437 int current_cmd_idx = 0;
438 for (size_t i = 0; i < IM_ARRAYSIZE(kCommandOptions); ++i) {
439 if (kCommandOptions[i].opcode == event.command.opcode) {
440 current_cmd_idx = static_cast<int>(i);
441 break;
442 }
443 }
444 ImGui::Text("Edit Command");
445 if (ImGui::BeginCombo("Opcode", kCommandOptions[current_cmd_idx].name)) {
446 for (size_t i = 0; i < IM_ARRAYSIZE(kCommandOptions); ++i) {
447 bool sel = (static_cast<int>(i) == current_cmd_idx);
448 if (ImGui::Selectable(kCommandOptions[i].name, sel)) {
449 current_cmd_idx = static_cast<int>(i);
450 }
451 if (sel)
452 ImGui::SetItemDefaultFocus();
453 }
454 ImGui::EndCombo();
455 }
456
457 int p0 = event.command.params[0];
458 int p1 = event.command.params[1];
459 int p2 = event.command.params[2];
460
461 uint8_t opcode = kCommandOptions[current_cmd_idx].opcode;
462
463 if (opcode == 0xE0 && bank) { // Set Instrument
464 // Instrument selector
465 const auto* inst = bank->GetInstrument(p0);
466 std::string preview =
467 inst ? absl::StrFormat("%02X: %s", p0, inst->name.c_str())
468 : absl::StrFormat("%02X", p0);
469 if (ImGui::BeginCombo("Instrument", preview.c_str())) {
470 for (size_t i = 0; i < bank->GetInstrumentCount(); ++i) {
471 const auto* item = bank->GetInstrument(i);
472 bool is_selected = (static_cast<int>(i) == p0);
473 if (ImGui::Selectable(
474 absl::StrFormat("%02X: %s", i, item->name.c_str()).c_str(),
475 is_selected)) {
476 p0 = static_cast<int>(i);
477 }
478 if (is_selected)
479 ImGui::SetItemDefaultFocus();
480 }
481 ImGui::EndCombo();
482 }
483 } else {
484 ImGui::InputInt("Param 0 (hex)", &p0, 1, 4,
485 ImGuiInputTextFlags_CharsHexadecimal);
486 }
487
488 ImGui::InputInt("Param 1 (hex)", &p1, 1, 4,
489 ImGuiInputTextFlags_CharsHexadecimal);
490 ImGui::InputInt("Param 2 (hex)", &p2, 1, 4,
491 ImGuiInputTextFlags_CharsHexadecimal);
492
493 if (ImGui::Button("Apply")) {
494 event.command.opcode = opcode;
495 event.command.params[0] = static_cast<uint8_t>(p0 & 0xFF);
496 event.command.params[1] = static_cast<uint8_t>(p1 & 0xFF);
497 event.command.params[2] = static_cast<uint8_t>(p2 & 0xFF);
498 if (on_edit_)
499 on_edit_();
500 ImGui::CloseCurrentPopup();
501 }
502 ImGui::SameLine();
503 ImGui::TextDisabled("%s", DescribeCommand(event.command.opcode).c_str());
504 } else {
505 ImGui::TextDisabled("Unsupported edit type");
506 }
507
508 ImGui::EndPopup();
509 }
510}
511
513 if (ImGui::IsKeyPressed(ImGuiKey_UpArrow)) {
514 if (ImGui::GetIO().KeyShift && selection_anchor_row_ == -1) {
517 }
518 if (!ImGui::GetIO().KeyShift && selection_anchor_row_ != -1) {
521 }
522 selected_row_ = std::max(0, selected_row_ - 1);
523 }
524 if (ImGui::IsKeyPressed(ImGuiKey_DownArrow)) {
525 if (ImGui::GetIO().KeyShift && selection_anchor_row_ == -1) {
528 }
529 if (!ImGui::GetIO().KeyShift && selection_anchor_row_ != -1) {
532 }
533 selected_row_++; // Limit checked against song length later
534 }
535 if (ImGui::IsKeyPressed(ImGuiKey_LeftArrow)) {
536 if (ImGui::GetIO().KeyShift && selection_anchor_row_ == -1) {
539 }
540 if (!ImGui::GetIO().KeyShift && selection_anchor_row_ != -1) {
543 }
544 selected_col_ = std::max(1, selected_col_ - 1);
545 }
546 if (ImGui::IsKeyPressed(ImGuiKey_RightArrow)) {
547 if (ImGui::GetIO().KeyShift && selection_anchor_row_ == -1) {
550 }
551 if (!ImGui::GetIO().KeyShift && selection_anchor_row_ != -1) {
554 }
555 selected_col_ = std::min(8, selected_col_ + 1);
556 }
557
558 if (ImGui::IsKeyPressed(ImGuiKey_PageUp)) {
559 selected_row_ = std::max(0, selected_row_ - 16);
560 }
561 if (ImGui::IsKeyPressed(ImGuiKey_PageDown)) {
562 selected_row_ += 16;
563 }
564 if (ImGui::IsKeyPressed(ImGuiKey_Home)) {
565 selected_row_ = 0;
566 }
567}
568
570 if (!song || song->segments.empty())
571 return;
572
573 if (selected_row_ < 0 || selected_col_ < 1 || selected_col_ > 8)
574 return;
575 int ch = selected_col_ - 1;
576
577 auto& track = song->segments[0].tracks[ch];
578 int tick = selected_row_ * ticks_per_row_;
579
580 // Helper to trigger undo
581 auto TriggerEdit = [this]() {
582 if (on_edit_)
583 on_edit_();
584 };
585
586 // Handle Note Entry
587 // Mapping: Z=C, S=C#, X=D, D=D#, C=E, V=F, G=F#, B=G, H=G#, N=A, J=A#, M=B
588 // Octave +1: Q, 2, W, 3, E, R, 5, T, 6, Y, 7, U
589 struct KeyNote {
590 ImGuiKey key;
591 int semitone;
592 int octave_offset;
593 };
594 static const KeyNote key_map[] = {
595 {ImGuiKey_Z, 0, 0}, {ImGuiKey_S, 1, 0}, {ImGuiKey_X, 2, 0},
596 {ImGuiKey_D, 3, 0}, {ImGuiKey_C, 4, 0}, {ImGuiKey_V, 5, 0},
597 {ImGuiKey_G, 6, 0}, {ImGuiKey_B, 7, 0}, {ImGuiKey_H, 8, 0},
598 {ImGuiKey_N, 9, 0}, {ImGuiKey_J, 10, 0}, {ImGuiKey_M, 11, 0},
599 {ImGuiKey_Q, 0, 1}, {ImGuiKey_2, 1, 1}, {ImGuiKey_W, 2, 1},
600 {ImGuiKey_3, 3, 1}, {ImGuiKey_E, 4, 1}, {ImGuiKey_R, 5, 1},
601 {ImGuiKey_5, 6, 1}, {ImGuiKey_T, 7, 1}, {ImGuiKey_6, 8, 1},
602 {ImGuiKey_Y, 9, 1}, {ImGuiKey_7, 10, 1}, {ImGuiKey_U, 11, 1}};
603
604 static int base_octave = 4; // Default octave
605
606 // Octave Control
607 if (ImGui::IsKeyPressed(ImGuiKey_F1))
608 base_octave = std::max(1, base_octave - 1);
609 if (ImGui::IsKeyPressed(ImGuiKey_F2))
610 base_octave = std::min(6, base_octave + 1);
611
612 // Check note keys
613 for (const auto& kn : key_map) {
614 if (ImGui::IsKeyPressed(kn.key)) {
615 TriggerEdit();
616
617 int octave = base_octave + kn.octave_offset;
618 if (octave > 6)
619 octave = 6;
620
621 uint8_t pitch = kNoteMinPitch + (octave - 1) * 12 + kn.semitone;
622
623 // Check for existing event at this tick
624 bool found = false;
625 for (auto& evt : track.events) {
626 if (evt.tick == tick) {
627 if (evt.type == TrackEvent::Type::Note) {
628 evt.note.pitch = pitch; // Update pitch, keep duration
629 } else {
630 // Replace command/other with note
631 evt = TrackEvent::MakeNote(tick, pitch, kDurationSixteenth);
632 }
633 found = true;
634 break;
635 }
636 }
637
638 if (!found) {
639 track.InsertEvent(
641 }
642
643 // Auto-advance
645 return;
646 }
647 }
648
649 // Deletion
650 if (ImGui::IsKeyPressed(ImGuiKey_Delete) ||
651 ImGui::IsKeyPressed(ImGuiKey_Backspace)) {
652 bool changed = false;
653
654 // Handle range deletion if selected
655 if (selection_anchor_row_ != -1) {
656 // TODO: Implement range deletion logic
657 } else {
658 // Single cell deletion
659 for (size_t i = 0; i < track.events.size(); ++i) {
660 if (track.events[i].tick == tick) {
661 TriggerEdit();
662 track.RemoveEvent(i);
663 changed = true;
664 break;
665 }
666 }
667 }
668
669 if (changed) {
671 }
672 }
673
674 // Special keys
675 if (ImGui::IsKeyPressed(ImGuiKey_Space)) {
676 // Insert Key Off / Rest
677 TriggerEdit();
678 // TODO: Check existing and set to Rest (0xC9) or insert Rest
679 }
680}
681
683 if (!song || song->segments.empty())
684 return;
685 if (selected_row_ < 0 || selected_col_ < 1 || selected_col_ > 8)
686 return;
687
688 int ch = selected_col_ - 1;
689 auto& track = song->segments[0].tracks[ch];
690 int tick = selected_row_ * ticks_per_row_;
691
692 auto TriggerEdit = [this]() {
693 if (on_edit_)
694 on_edit_();
695 };
696
697 // Insert simple SetInstrument command (Cmd+I / Ctrl+I)
698 if (ImGui::IsKeyPressed(ImGuiKey_I) &&
699 (ImGui::GetIO().KeyCtrl || ImGui::GetIO().KeySuper)) {
700 TriggerEdit();
701 // default instrument 0
702 TrackEvent cmd = TrackEvent::MakeCommand(tick, 0xE0, 0x00);
703 track.InsertEvent(cmd);
704 }
705
706 // Insert SetPan (Cmd+P)
707 if (ImGui::IsKeyPressed(ImGuiKey_P) &&
708 (ImGui::GetIO().KeyCtrl || ImGui::GetIO().KeySuper)) {
709 TriggerEdit();
710 TrackEvent cmd = TrackEvent::MakeCommand(tick, 0xE1, 0x10); // center pan
711 track.InsertEvent(cmd);
712 }
713
714 // Insert Channel Volume (Cmd+V)
715 if (ImGui::IsKeyPressed(ImGuiKey_V) &&
716 (ImGui::GetIO().KeyCtrl || ImGui::GetIO().KeySuper)) {
717 TriggerEdit();
718 TrackEvent cmd = TrackEvent::MakeCommand(tick, 0xED, 0x7F);
719 track.InsertEvent(cmd);
720 }
721
722 // Quick duration tweak for the note at this tick (Alt+[ / Alt+])
723 if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket) && ImGui::GetIO().KeyAlt) {
724 for (auto& evt : track.events) {
725 if (evt.tick == tick && evt.type == TrackEvent::Type::Note) {
726 TriggerEdit();
727 evt.note.duration = std::max<uint16_t>(1, evt.note.duration - 6);
728 break;
729 }
730 }
731 }
732 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket) && ImGui::GetIO().KeyAlt) {
733 for (auto& evt : track.events) {
734 if (evt.tick == tick && evt.type == TrackEvent::Type::Note) {
735 TriggerEdit();
736 evt.note.duration = evt.note.duration + 6;
737 break;
738 }
739 }
740 }
741}
742
743} // namespace music
744} // namespace editor
745} // namespace yaze
void HandleKeyboardInput(MusicSong *song)
std::function< void()> on_edit_
void HandleEditShortcuts(MusicSong *song)
void DrawEventCell(MusicTrack &track, int event_index, int channel_idx, uint16_t tick, const MusicBank *bank)
void DrawGrid(MusicSong *song, const MusicBank *bank)
void DrawToolbar(MusicSong *song)
void Draw(MusicSong *song, const MusicBank *bank=nullptr)
Draw the tracker view for the given song.
const Theme & GetCurrentTheme() const
static ThemeManager & Get()
Manages the collection of songs, instruments, and samples from a ROM.
Definition music_bank.h:27
MusicInstrument * GetInstrument(int index)
Get an instrument by index.
size_t GetInstrumentCount() const
Get the number of instruments.
Definition music_bank.h:176
ImVec4 ConvertColorToImVec4(const Color &color)
Definition color.h:23
Contains classes and functions for handling music data in Zelda 3.
constexpr uint8_t kDurationSixteenth
Definition song_data.h:68
constexpr uint8_t kNoteMinPitch
Definition song_data.h:55
constexpr uint8_t kNoteMaxPitch
Definition song_data.h:56
float alpha
Definition color.h:18
A complete song composed of segments.
Definition song_data.h:334
std::vector< MusicSegment > segments
Definition song_data.h:336
uint32_t GetTotalDuration() const
Definition song_data.h:343
One of 8 channels in a music segment.
Definition song_data.h:291
void InsertEvent(TrackEvent event)
Definition song_data.h:467
std::vector< TrackEvent > events
Definition song_data.h:292
Represents a single musical note.
Definition song_data.h:192
std::string GetNoteName() const
Definition song_data.h:433
A single event in a music track (note, command, or control).
Definition song_data.h:247
static TrackEvent MakeNote(uint16_t tick, uint8_t pitch, uint8_t duration, uint8_t velocity=0)
Definition song_data.h:259
static TrackEvent MakeCommand(uint16_t tick, uint8_t opcode, uint8_t p1=0, uint8_t p2=0, uint8_t p3=0)
Definition song_data.h:270