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) {
381 const std::string popup_id =
382 absl::StrFormat(
"EditEvent##%d_%d_%d", channel_idx, tick, event_index);
383 if ((double_clicked || right_clicked) && has_event) {
384 ImGui::OpenPopup(popup_id.c_str());
387 if (ImGui::BeginPopup(popup_id.c_str())) {
389 ImGui::TextDisabled(
"Empty");
394 auto&
event = *event_ptr;
395 if (event.type == TrackEvent::Type::Note) {
399 edit_duration =
event.note.duration;
401 static std::vector<std::string> note_labels;
402 if (note_labels.empty()) {
405 n.
pitch =
static_cast<uint8_t
>(p);
410 idx = std::clamp(idx, 0,
static_cast<int>(note_labels.size()) - 1);
412 ImGui::Text(
"Edit Note");
413 if (ImGui::BeginCombo(
"Pitch", note_labels[idx].c_str())) {
414 for (
int i = 0; i < static_cast<int>(note_labels.size()); ++i) {
415 bool sel = (i == idx);
416 if (ImGui::Selectable(note_labels[i].c_str(), sel)) {
421 ImGui::SetItemDefaultFocus();
426 if (ImGui::SliderInt(
"Duration", &edit_duration, 1, 0xFF)) {
427 edit_duration = std::clamp(edit_duration, 1, 0xFF);
430 if (ImGui::Button(
"Apply")) {
434 event.note.pitch =
static_cast<uint8_t
>(edit_pitch);
435 event.note.duration =
static_cast<uint8_t
>(edit_duration);
436 ImGui::CloseCurrentPopup();
438 }
else if (event.type == TrackEvent::Type::Command) {
439 int current_cmd_idx = 0;
440 for (
size_t i = 0; i < IM_ARRAYSIZE(kCommandOptions); ++i) {
441 if (kCommandOptions[i].opcode == event.command.
opcode) {
442 current_cmd_idx =
static_cast<int>(i);
446 ImGui::Text(
"Edit Command");
447 if (ImGui::BeginCombo(
"Opcode", kCommandOptions[current_cmd_idx].name)) {
448 for (
size_t i = 0; i < IM_ARRAYSIZE(kCommandOptions); ++i) {
449 bool sel = (
static_cast<int>(i) == current_cmd_idx);
450 if (ImGui::Selectable(kCommandOptions[i].name, sel)) {
451 current_cmd_idx =
static_cast<int>(i);
454 ImGui::SetItemDefaultFocus();
459 int p0 =
event.command.params[0];
460 int p1 =
event.command.params[1];
461 int p2 =
event.command.params[2];
463 uint8_t opcode = kCommandOptions[current_cmd_idx].
opcode;
465 if (opcode == 0xE0 && bank) {
468 std::string preview =
469 inst ? absl::StrFormat(
"%02X: %s", p0, inst->name.c_str())
470 : absl::StrFormat(
"%02X", p0);
471 if (ImGui::BeginCombo(
"Instrument", preview.c_str())) {
474 bool is_selected = (
static_cast<int>(i) == p0);
475 if (ImGui::Selectable(
476 absl::StrFormat(
"%02X: %s", i, item->name.c_str()).c_str(),
478 p0 =
static_cast<int>(i);
481 ImGui::SetItemDefaultFocus();
486 ImGui::InputInt(
"Param 0 (hex)", &p0, 1, 4,
487 ImGuiInputTextFlags_CharsHexadecimal);
490 ImGui::InputInt(
"Param 1 (hex)", &p1, 1, 4,
491 ImGuiInputTextFlags_CharsHexadecimal);
492 ImGui::InputInt(
"Param 2 (hex)", &p2, 1, 4,
493 ImGuiInputTextFlags_CharsHexadecimal);
495 if (ImGui::Button(
"Apply")) {
499 event.command.opcode = opcode;
500 event.command.params[0] =
static_cast<uint8_t
>(p0 & 0xFF);
501 event.command.params[1] =
static_cast<uint8_t
>(p1 & 0xFF);
502 event.command.params[2] =
static_cast<uint8_t
>(p2 & 0xFF);
503 ImGui::CloseCurrentPopup();
506 ImGui::TextDisabled(
"%s", DescribeCommand(event.command.opcode).c_str());
508 ImGui::TextDisabled(
"Unsupported edit type");
573 if (!song || song->
segments.empty())
576 if (
selected_row_ < 0 || selected_col_ < 1 || selected_col_ > 8)
580 auto& track = song->
segments[0].tracks[ch];
584 auto TriggerEdit = [
this]() {
597 static const KeyNote key_map[] = {
598 {ImGuiKey_Z, 0, 0}, {ImGuiKey_S, 1, 0}, {ImGuiKey_X, 2, 0},
599 {ImGuiKey_D, 3, 0}, {ImGuiKey_C, 4, 0}, {ImGuiKey_V, 5, 0},
600 {ImGuiKey_G, 6, 0}, {ImGuiKey_B, 7, 0}, {ImGuiKey_H, 8, 0},
601 {ImGuiKey_N, 9, 0}, {ImGuiKey_J, 10, 0}, {ImGuiKey_M, 11, 0},
602 {ImGuiKey_Q, 0, 1}, {ImGuiKey_2, 1, 1}, {ImGuiKey_W, 2, 1},
603 {ImGuiKey_3, 3, 1}, {ImGuiKey_E, 4, 1}, {ImGuiKey_R, 5, 1},
604 {ImGuiKey_5, 6, 1}, {ImGuiKey_T, 7, 1}, {ImGuiKey_6, 8, 1},
605 {ImGuiKey_Y, 9, 1}, {ImGuiKey_7, 10, 1}, {ImGuiKey_U, 11, 1}};
607 static int base_octave = 4;
610 if (ImGui::IsKeyPressed(ImGuiKey_F1))
611 base_octave = std::max(1, base_octave - 1);
612 if (ImGui::IsKeyPressed(ImGuiKey_F2))
613 base_octave = std::min(6, base_octave + 1);
616 for (
const auto& kn : key_map) {
617 if (ImGui::IsKeyPressed(kn.key)) {
620 int octave = base_octave + kn.octave_offset;
624 uint8_t pitch =
kNoteMinPitch + (octave - 1) * 12 + kn.semitone;
628 for (
auto& evt : track.events) {
629 if (evt.tick == tick) {
630 if (evt.type == TrackEvent::Type::Note) {
631 evt.note.pitch = pitch;
653 if (ImGui::IsKeyPressed(ImGuiKey_Delete) ||
654 ImGui::IsKeyPressed(ImGuiKey_Backspace)) {
655 bool changed =
false;
662 for (
size_t i = 0; i < track.events.size(); ++i) {
663 if (track.events[i].tick == tick) {
665 track.RemoveEvent(i);
678 if (ImGui::IsKeyPressed(ImGuiKey_Space)) {