171 uint32_t duration = segment.GetDuration();
174 if (ImGui::BeginTable(
"TrackerTable", 9,
175 ImGuiTableFlags_Borders | ImGuiTableFlags_ScrollY |
176 ImGuiTableFlags_RowBg |
177 ImGuiTableFlags_Resizable)) {
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());
184 ImGui::TableHeadersRow();
188 ImGuiListClipper clipper;
191 while (clipper.Step()) {
192 for (
int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row) {
196 ImGui::TableNextRow();
200 ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0,
201 GetColorBeatHighlight());
205 ImGui::TableSetColumnIndex(0);
206 ImGui::TextDisabled(
"%04X", tick_start);
209 for (
int ch = 0; ch < 8; ++ch) {
210 ImGui::TableSetColumnIndex(ch + 1);
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));
229 }
else if (is_selected) {
230 ImGui::TableSetBgColor(
231 ImGuiTableBgTarget_CellBg,
232 GetColorSelection(
false));
235 if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)) {
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);
250 if (evt.tick >= tick_end)
258 ImGui::PushID(row * 100 + ch);
259 if (ImGui::Selectable(
"##cell",
false,
260 ImGuiSelectableFlags_SpanAllColumns |
261 ImGuiSelectableFlags_AllowOverlap,
263 if (ImGui::GetIO().KeyShift) {
284 int channel_idx, uint16_t tick,
287 (event_index >= 0 && event_index < static_cast<int>(track.
events.size()))
288 ? &track.
events[event_index]
291 bool has_event = event_ptr !=
nullptr;
294 ImGui::TextDisabled(
"...");
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);
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);
314 if (event.command.opcode == 0xE0) {
316 const auto* inst = bank->
GetInstrument(event.command.params[0]);
318 label = absl::StrFormat(
"Instr: %s", inst->name.c_str());
320 absl::StrFormat(
"Set Instrument: %s (ID %02X)",
321 inst->name.c_str(), event.command.params[0]);
323 label = absl::StrFormat(
"Instr: %02X", event.command.params[0]);
326 label = absl::StrFormat(
"Instr: %02X", event.command.params[0]);
328 }
else if (event.command.opcode == 0xE1) {
329 int pan =
event.command.params[0];
331 label =
"Pan: Center";
333 label = absl::StrFormat(
"Pan: L%d", 0x0A - pan);
335 label = absl::StrFormat(
"Pan: R%d", pan - 0x0A);
336 }
else if (event.command.opcode == 0xE7) {
337 label = absl::StrFormat(
"Tempo: %d", event.command.params[0]);
338 }
else if (event.command.opcode == 0xED) {
339 label = absl::StrFormat(
"Vol: %d", event.command.params[0]);
340 }
else if (event.command.opcode == 0xE5) {
341 label = absl::StrFormat(
"M.Vol: %d", event.command.params[0]);
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]);
354 case TrackEvent::Type::SubroutineCall:
355 ImGui::TextColored(ImColor(GetColorSubroutine()),
"CALL");
358 case TrackEvent::Type::End:
359 ImGui::TextDisabled(
"END");
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);
371 if (!has_event && double_clicked) {
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());
386 if (ImGui::BeginPopup(popup_id.c_str())) {
388 ImGui::TextDisabled(
"Empty");
393 auto&
event = *event_ptr;
394 if (event.type == TrackEvent::Type::Note) {
398 edit_duration =
event.note.duration;
400 static std::vector<std::string> note_labels;
401 if (note_labels.empty()) {
404 n.
pitch =
static_cast<uint8_t
>(p);
409 idx = std::clamp(idx, 0,
static_cast<int>(note_labels.size()) - 1);
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)) {
420 ImGui::SetItemDefaultFocus();
425 if (ImGui::SliderInt(
"Duration", &edit_duration, 1, 0xFF)) {
426 edit_duration = std::clamp(edit_duration, 1, 0xFF);
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);
434 ImGui::CloseCurrentPopup();
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);
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);
452 ImGui::SetItemDefaultFocus();
457 int p0 =
event.command.params[0];
458 int p1 =
event.command.params[1];
459 int p2 =
event.command.params[2];
461 uint8_t opcode = kCommandOptions[current_cmd_idx].
opcode;
463 if (opcode == 0xE0 && bank) {
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())) {
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(),
476 p0 =
static_cast<int>(i);
479 ImGui::SetItemDefaultFocus();
484 ImGui::InputInt(
"Param 0 (hex)", &p0, 1, 4,
485 ImGuiInputTextFlags_CharsHexadecimal);
488 ImGui::InputInt(
"Param 1 (hex)", &p1, 1, 4,
489 ImGuiInputTextFlags_CharsHexadecimal);
490 ImGui::InputInt(
"Param 2 (hex)", &p2, 1, 4,
491 ImGuiInputTextFlags_CharsHexadecimal);
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);
500 ImGui::CloseCurrentPopup();
503 ImGui::TextDisabled(
"%s", DescribeCommand(event.command.opcode).c_str());
505 ImGui::TextDisabled(
"Unsupported edit type");
570 if (!song || song->
segments.empty())
573 if (
selected_row_ < 0 || selected_col_ < 1 || selected_col_ > 8)
577 auto& track = song->
segments[0].tracks[ch];
581 auto TriggerEdit = [
this]() {
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}};
604 static int base_octave = 4;
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);
613 for (
const auto& kn : key_map) {
614 if (ImGui::IsKeyPressed(kn.key)) {
617 int octave = base_octave + kn.octave_offset;
621 uint8_t pitch =
kNoteMinPitch + (octave - 1) * 12 + kn.semitone;
625 for (
auto& evt : track.events) {
626 if (evt.tick == tick) {
627 if (evt.type == TrackEvent::Type::Note) {
628 evt.note.pitch = pitch;
650 if (ImGui::IsKeyPressed(ImGuiKey_Delete) ||
651 ImGui::IsKeyPressed(ImGuiKey_Backspace)) {
652 bool changed =
false;
659 for (
size_t i = 0; i < track.events.size(); ++i) {
660 if (track.events[i].tick == tick) {
662 track.RemoveEvent(i);
675 if (ImGui::IsKeyPressed(ImGuiKey_Space)) {