-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcp_tracker_ui_table.lua
More file actions
2957 lines (2615 loc) · 123 KB
/
Copy pathfcp_tracker_ui_table.lua
File metadata and controls
2957 lines (2615 loc) · 123 KB
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- fcp_tracker_ui_table.lua
-- Main region table rendering with scrolling and paint logic
-- Requires: fcp_tracker_ui_helpers.lua, fcp_tracker_ui_tabs.lua (for WANT_CENTER_ON_TAB, CENTER_DELAY_FRAMES)
local reaper = reaper
local ImGui = reaper
-- UI-local paint state
local PAINT = { down = false, seen = {}, did_any = false, pending_redirect = nil }
local TIME_PAINT = { down = false, min_row = nil, max_row = nil }
local LAST_ACTIVE_ROW = nil
-- Minimap bounds for scroll speed detection (from previous frame)
local MINIMAP_BOUNDS = { y1 = 0, y2 = 0 }
-- Previous-frame hover preview for the measure-offset InputInt
local HOVER_PREVIEW_OFFSET = nil -- nil = not hovering, number = preview offset
local HOVER_CURRENT_REGION = false -- true when hovering the active (current) region row
local HOVER_MODIFIER_DISTANCE = nil -- nil = not modifier-hovering, number = measure distance from cursor
local HEADER_LABEL_HOVERED = false -- true when hovering the leftmost header cell
local HEADER_LABEL_WAS_HOVERED = false -- previous frame's hover state for edge detection
local HEADER_JUMP_CLICKED = false -- true after jump click, suppresses preview-scroll for rest of hover
-- Pending OV deletions: { {trackname, measure_num, delete_time}, ... }
local PENDING_OV_DELETIONS = {}
-- Check and process pending OV deletions
local function process_pending_ov_deletions()
local now = reaper.time_precise()
local i = 1
while i <= #PENDING_OV_DELETIONS do
local entry = PENDING_OV_DELETIONS[i]
if now >= entry.delete_time then
-- Check if OV still exists and is still invalid
local trackname = entry.trackname
local measure_num = entry.measure_num
local row = entry.row
-- Find the OV note covering this measure and check if ANY measure in its span has notes
local still_invalid = false
local tr = find_track_by_name(trackname)
if tr then
local tk = first_midi_take_on_track(tr)
if tk then
-- Get measure bounds to find OV note
local _, qn_start, qn_end = reaper.TimeMap_GetMeasureInfo(0, measure_num - 1)
local time_start = reaper.TimeMap_QNToTime(qn_start)
local time_end = reaper.TimeMap_QNToTime(qn_end)
local ppq_meas_start = reaper.MIDI_GetPPQPosFromProjTime(tk, time_start)
local ppq_meas_end = reaper.MIDI_GetPPQPosFromProjTime(tk, time_end)
-- Find the OV note in this measure
local ov_ppq_s, ov_ppq_e = nil, nil
local _, note_cnt = reaper.MIDI_CountEvts(tk)
for ni = 0, note_cnt - 1 do
local ok, _, _, ppq_s, ppq_e, _, pitch = reaper.MIDI_GetNote(tk, ni)
if ok and pitch == OVERDRIVE_PITCH then
if ppq_s < ppq_meas_end and ppq_e > ppq_meas_start then
ov_ppq_s, ov_ppq_e = ppq_s, ppq_e
break
end
end
end
if ov_ppq_s and ov_ppq_e then
-- Check all measures this OV note covers for playable notes
local ov_proj_start = reaper.MIDI_GetProjTimeFromPPQPos(tk, ov_ppq_s)
local ov_proj_end = reaper.MIDI_GetProjTimeFromPPQPos(tk, ov_ppq_e)
local span_has_notes = false
local first_m = OVERDRIVE_MEASURES.first or 1
local last_m = OVERDRIVE_MEASURES.last or 1
for m = first_m, last_m do
local _, qn_m_start, qn_m_end = reaper.TimeMap_GetMeasureInfo(0, m - 1)
local meas_time_start = reaper.TimeMap_QNToTime(qn_m_start)
local meas_time_end = reaper.TimeMap_QNToTime(qn_m_end)
-- Check if OV covers this measure
local epsilon = 0.0001
if ov_proj_start < (meas_time_end - epsilon) and ov_proj_end > (meas_time_start + epsilon) then
-- OV covers this measure - check for notes (note: 0 is falsy-like, must check > 0)
local note_count = OVERDRIVE_NOTES[row] and OVERDRIVE_NOTES[row][m] or 0
if note_count > 0 then
span_has_notes = true
break
end
end
end
-- Only invalid if the entire span has no notes
still_invalid = not span_has_notes
end
end
end
if still_invalid then
-- Delete the OV note
if tr then
local tk = first_midi_take_on_track(tr)
if tk then
local _, qn_start, qn_end = reaper.TimeMap_GetMeasureInfo(0, measure_num - 1)
local time_start = reaper.TimeMap_QNToTime(qn_start)
local time_end = reaper.TimeMap_QNToTime(qn_end)
local ppq_start = reaper.MIDI_GetPPQPosFromProjTime(tk, time_start)
local ppq_end = reaper.MIDI_GetPPQPosFromProjTime(tk, time_end)
local _, note_cnt = reaper.MIDI_CountEvts(tk)
for ni = note_cnt - 1, 0, -1 do
local ok, _, _, ppq_s, ppq_e, _, pitch = reaper.MIDI_GetNote(tk, ni)
if ok and pitch == OVERDRIVE_PITCH then
if ppq_s < ppq_end and ppq_e > ppq_start then
reaper.MIDI_DeleteNote(tk, ni)
break
end
end
end
reaper.MIDI_Sort(tk)
collect_overdrive_data()
end
end
end
-- Remove from pending list
table.remove(PENDING_OV_DELETIONS, i)
else
i = i + 1
end
end
end
function draw_table(ctx, redirect_focus_after_click)
-- Dispatch to specialized table for Overdrive tab
if current_tab == "Overdrive" then
draw_overdrive_table(ctx, redirect_focus_after_click)
return
end
local row_h = ImGui.ImGui_GetTextLineHeightWithSpacing(ctx) * 0.976
local row_of_cursor = active_region_index()
--------------------------------------------------------------
-- HEADER (fixed, no extra child)
--------------------------------------------------------------
if ImGui.ImGui_BeginTable(
ctx, "hdr_tbl", 3,
ImGui.ImGui_TableFlags_SizingFixedFit() +
ImGui.ImGui_TableFlags_Borders()
) then
local display_diff
if current_tab == "Vocals" then
display_diff = VOCALS_MODE
elseif current_tab == "Venue" then
display_diff = VENUE_MODE
elseif current_tab == "Keys" and PRO_KEYS_ACTIVE then
local diff_map = { Expert="X", Hard="H", Medium="M", Easy="E" }
display_diff = "Pro " .. (diff_map[ACTIVE_DIFF] or "X")
else
display_diff = ACTIVE_DIFF
end
ImGui.ImGui_TableSetupColumn(
ctx, "Region",
ImGui.ImGui_TableColumnFlags_WidthFixed(), FIRST_COL_W
)
ImGui.ImGui_TableSetupColumn(
ctx, display_diff,
ImGui.ImGui_TableColumnFlags_WidthFixed(), REGION_COL_W
)
ImGui.ImGui_TableSetupColumn(
ctx, "Timer",
ImGui.ImGui_TableColumnFlags_WidthFixed(), TIME_COL_W
)
ImGui.ImGui_TableNextRow(ctx, ImGui.ImGui_TableRowFlags_Headers())
ImGui.ImGui_TableNextColumn(ctx)
do
local rx0 = ImGui.ImGui_GetCursorPosX(ctx)
local ry0 = ImGui.ImGui_GetCursorPosY(ctx)
local rw = select(1, ImGui.ImGui_GetContentRegionAvail(ctx))
local row_height = ImGui.ImGui_GetTextLineHeightWithSpacing(ctx)
-- Right-aligned measure-offset InputInt (from Jump Regions)
if FCP_JUMP_REGIONS then
local em = ImGui.ImGui_GetFontSize(ctx)
local input_w = math.floor(em * 3)
local gap = 4 -- gap between label area and InputInt
-- Draw label as an invisible button that triggers jump on click
local label_w = rw - input_w - gap
if label_w < 1 then label_w = 1 end
ImGui.ImGui_SetCursorPosX(ctx, rx0)
ImGui.ImGui_SetCursorPosY(ctx, ry0)
ImGui.ImGui_InvisibleButton(ctx, "##region_jump", label_w, row_height)
local cell_clicked = ImGui.ImGui_IsItemClicked(ctx, 0)
local label_hovered = ImGui.ImGui_IsItemHovered(ctx)
HEADER_LABEL_HOVERED = label_hovered
-- Determine header state based on what's being hovered
local any_hover = label_hovered or HOVER_CURRENT_REGION or (HOVER_PREVIEW_OFFSET ~= nil) or (HOVER_MODIFIER_DISTANCE ~= nil)
-- The value that would be shown in the textbox right now
local effective_val
if HOVER_MODIFIER_DISTANCE ~= nil then
effective_val = HOVER_MODIFIER_DISTANCE
elseif HOVER_PREVIEW_OFFSET ~= nil then
effective_val = HOVER_PREVIEW_OFFSET
else
effective_val = FCP_JUMP_REGIONS.MEAS_OFFSET
end
local show_current = any_hover and (effective_val == 0)
local show_orange = any_hover and (effective_val ~= 0)
-- Draw label text over the invisible button
local text_h = ImGui.ImGui_GetTextLineHeight(ctx)
local label_y_offset = math.floor((row_height - text_h) / 2)
ImGui.ImGui_SetCursorPosX(ctx, rx0)
ImGui.ImGui_SetCursorPosY(ctx, ry0 + label_y_offset)
local header_label = "Target"
if show_current then
header_label = "Current"
elseif show_orange then
header_label = "Jump"
ImGui.ImGui_PushStyleColor(ctx, ImGui.ImGui_Col_Text(), COL_PREVIEW_LINE)
end
ImGui.ImGui_Text(ctx, header_label)
if show_orange then
ImGui.ImGui_PopStyleColor(ctx)
end
-- InputInt right-aligned, orange text when previewing
ImGui.ImGui_SetCursorPosX(ctx, rx0 + rw - input_w)
ImGui.ImGui_SetCursorPosY(ctx, ry0)
ImGui.ImGui_SetNextItemWidth(ctx, input_w)
local display_val = FCP_JUMP_REGIONS.MEAS_OFFSET
local is_previewing = (HOVER_PREVIEW_OFFSET ~= nil) or (HOVER_MODIFIER_DISTANCE ~= nil)
if HOVER_MODIFIER_DISTANCE ~= nil then
display_val = HOVER_MODIFIER_DISTANCE
elseif HOVER_PREVIEW_OFFSET ~= nil then
display_val = HOVER_PREVIEW_OFFSET
end
if show_orange then
ImGui.ImGui_PushStyleColor(ctx, ImGui.ImGui_Col_Text(), COL_PREVIEW_LINE)
end
local changed, v = ImGui.ImGui_InputInt(ctx, "##meas_off", display_val, 0, 0)
if show_orange then
ImGui.ImGui_PopStyleColor(ctx)
end
local input_active = ImGui.ImGui_IsItemActive(ctx)
FCP_JUMP_REGIONS.input_active = input_active
if changed and not is_previewing then FCP_JUMP_REGIONS.MEAS_OFFSET = v end
if ImGui.ImGui_IsItemDeactivatedAfterEdit(ctx) then
if redirect_focus_after_click then reaper.defer(redirect_focus_after_click) end
end
-- Trigger jump when the label area is clicked
if cell_clicked and not input_active then
FCP_JUMP_REGIONS.do_jump(true)
HEADER_JUMP_CLICKED = true
end
else
ImGui.ImGui_Text(ctx, "Region")
end
end
ImGui.ImGui_TableNextColumn(ctx)
local x0 = ImGui.ImGui_GetCursorPosX(ctx)
local w = select(1, ImGui.ImGui_GetContentRegionAvail(ctx))
local diff_label_y = ImGui.ImGui_GetCursorPosY(ctx)
ImGui.ImGui_Text(ctx, display_diff)
local pct = diff_pct(current_tab, display_diff)
local t = tostring(pct) .. "%"
local tw = select(1, ImGui.ImGui_CalcTextSize(ctx, t))
local th = select(2, ImGui.ImGui_CalcTextSize(ctx, t))
local pct_row_h = ImGui.ImGui_GetTextLineHeightWithSpacing(ctx)
local pct_y_off = math.floor((pct_row_h - th) / 2)
ImGui.ImGui_SetCursorPosX(ctx, x0 + w - tw)
ImGui.ImGui_SetCursorPosY(ctx, diff_label_y + pct_y_off)
ImGui.ImGui_PushStyleColor(ctx, ImGui.ImGui_Col_Text(), pct_to_u32(pct))
ImGui.ImGui_Text(ctx, t)
ImGui.ImGui_PopStyleColor(ctx)
-- Time header column
ImGui.ImGui_TableNextColumn(ctx)
ImGui.ImGui_Text(ctx, "Timer")
ImGui.ImGui_EndTable(ctx)
end
--------------------------------------------------------------
-- BODY metrics
--------------------------------------------------------------
local avail_h = select(2, ImGui.ImGui_GetContentRegionAvail(ctx))
local rows_fit = math.max(1, math.min(#REGIONS, math.floor(avail_h / row_h)))
local body_h = rows_fit * row_h + 1
local max_n = math.max(0, #REGIONS - rows_fit)
local key = current_tab
local need_center_now = false
-- Handle delayed centering after screenset load
if CENTER_DELAY_FRAMES > 0 then
CENTER_DELAY_FRAMES = CENTER_DELAY_FRAMES - 1
if CENTER_DELAY_FRAMES == 0 then
WANT_CENTER_ON_TAB = true
end
end
if row_of_cursor then
if WANT_CENTER_ON_TAB or row_of_cursor ~= LAST_ACTIVE_ROW then
local desired = row_of_cursor - math.floor(rows_fit / 2)
if desired < 0 then
desired = 0
elseif desired > max_n then
desired = max_n
end
TAB_SCROLL_ROW[key] = desired
WANT_CENTER_ON_TAB = false
LAST_ACTIVE_ROW = row_of_cursor
need_center_now = true
end
end
-- Header hover scroll: center on preview line row or snap back to current region
if HEADER_LABEL_HOVERED and not HEADER_JUMP_CLICKED and FCP_JUMP_REGIONS and FCP_JUMP_REGIONS.MEAS_OFFSET ~= 0 and row_of_cursor then
local st = reaper.GetPlayState()
local cursor_t = (st & 1) == 1 and reaper.GetPlayPosition() or reaper.GetCursorPosition()
local target_t = jump_time_by_measures(cursor_t, FCP_JUMP_REGIONS.MEAS_OFFSET)
local target_row = nil
for i = 1, #REGIONS do
local rs = REGIONS[i].pos or 0
local re = REGIONS[i].r_end or 0
if target_t >= rs and target_t < re then target_row = i; break end
end
if target_row then
local desired = target_row - math.floor(rows_fit / 2)
if desired < 0 then desired = 0 elseif desired > max_n then desired = max_n end
TAB_SCROLL_ROW[key] = desired
need_center_now = true
end
elseif HEADER_LABEL_HOVERED and HEADER_JUMP_CLICKED and row_of_cursor then
-- After jump click, stay centered on the (now-updated) active region
local desired = row_of_cursor - math.floor(rows_fit / 2)
if desired < 0 then desired = 0 elseif desired > max_n then desired = max_n end
TAB_SCROLL_ROW[key] = desired
need_center_now = true
elseif not HEADER_LABEL_HOVERED and HEADER_LABEL_WAS_HOVERED and row_of_cursor then
local desired = row_of_cursor - math.floor(rows_fit / 2)
if desired < 0 then desired = 0 elseif desired > max_n then desired = max_n end
TAB_SCROLL_ROW[key] = desired
need_center_now = true
HEADER_JUMP_CLICKED = false
end
HEADER_LABEL_WAS_HOVERED = HEADER_LABEL_HOVERED
-- Mouse press/release edge: reset paint set
do
local now = ImGui.ImGui_IsMouseDown(ctx, 0)
if now ~= PAINT.down then
-- On mouse release, call pending redirect if any
if not now and PAINT.pending_redirect then
reaper.defer(PAINT.pending_redirect)
end
PAINT.seEN, PAINT.did_any, PAINT.down, PAINT.pending_redirect = {}, false, now, nil
end
end
-- Right-click press/release edge: handle time selection paint
do
local now = ImGui.ImGui_IsMouseDown(ctx, 1)
if now ~= TIME_PAINT.down then
if not now and TIME_PAINT.min_row and TIME_PAINT.max_row then
local start_time = REGIONS[TIME_PAINT.min_row].pos or 0
local end_time = REGIONS[TIME_PAINT.max_row].r_end or 0
reaper.GetSet_LoopTimeRange(true, false, start_time, end_time, false)
end
TIME_PAINT.down = now
TIME_PAINT.min_row = nil
TIME_PAINT.max_row = nil
end
end
--------------------------------------------------------------
-- BODY: native scrollbar, 1-row wheel steps, snap to rows
--------------------------------------------------------------
local child_flags = ImGui.ImGui_WindowFlags_NoScrollWithMouse()
if ImGui.ImGui_BeginChild(ctx, "rows_scroller", 0, body_h, 0, child_flags) then
-- Visible bounds of the scroll child (screen coords) for hit-test clamping
local child_sx, child_sy = ImGui.ImGui_GetWindowPos(ctx)
local child_visible_y1 = child_sy
local child_visible_y2 = child_sy + body_h
local sy = ImGui.ImGui_GetScrollY(ctx)
if not need_center_now then
local n_from_sy = math.max(0, math.min(
max_n, math.floor((sy / row_h) + 0.5)
))
if TAB_SCROLL_ROW[key] ~= n_from_sy then
TAB_SCROLL_ROW[key] = n_from_sy
end
end
if TAB_SCROLL_ROW[key] ~= nil then
local target_sy = (TAB_SCROLL_ROW[key] or 0) * row_h
if math.abs(sy - target_sy) > 0.5 then
ImGui.ImGui_SetScrollY(ctx, target_sy)
sy = target_sy
end
end
if ImGui.ImGui_IsWindowHovered(ctx, 0) then
local wheel = ImGui.ImGui_GetMouseWheel(ctx) or 0
if wheel ~= 0 then
local step = (wheel > 0) and -1 or 1
local n = (TAB_SCROLL_ROW[key] or 0) + step
if n < 0 then n = 0
elseif n > max_n then n = max_n end
TAB_SCROLL_ROW[key] = n
ImGui.ImGui_SetScrollY(ctx, n * row_h)
end
end
if ImGui.ImGui_BeginTable(
ctx, "body_tbl", 3,
ImGui.ImGui_TableFlags_SizingFixedFit() +
ImGui.ImGui_TableFlags_Borders()
) then
local display_diff
if current_tab == "Vocals" then
display_diff = VOCALS_MODE
elseif current_tab == "Venue" then
display_diff = VENUE_MODE
elseif current_tab == "Keys" and PRO_KEYS_ACTIVE then
local diff_map = { Expert="X", Hard="H", Medium="M", Easy="E" }
display_diff = "Pro " .. (diff_map[ACTIVE_DIFF] or "X")
else
display_diff = ACTIVE_DIFF
end
ImGui.ImGui_TableSetupColumn(
ctx, "Region",
ImGui.ImGui_TableColumnFlags_WidthFixed(), FIRST_COL_W
)
ImGui.ImGui_TableSetupColumn(
ctx, display_diff,
ImGui.ImGui_TableColumnFlags_WidthFixed(), REGION_COL_W
)
ImGui.ImGui_TableSetupColumn(
ctx, "Timer",
ImGui.ImGui_TableColumnFlags_WidthFixed(), TIME_COL_W
)
local hovered_region_row = nil
local region_cell_positions = {}
HOVER_CURRENT_REGION = false -- reset each frame before row loop
for r = 1, #REGIONS do
if r == #REGIONS then
ImGui.ImGui_TableNextRow(ctx, 0, row_h + 1)
else
ImGui.ImGui_TableNextRow(ctx)
end
-- Highlight entire row (including area right of columns) for the current region
if row_of_cursor == r then
ImGui.ImGui_TableSetBgColor(
ctx, ImGui.ImGui_TableBgTarget_RowBg0(), REG_COL_U32[r].header
)
end
-- Region cell
ImGui.ImGui_TableNextColumn(ctx)
if row_of_cursor ~= r then
ImGui.ImGui_TableSetBgColor(
ctx, ImGui.ImGui_TableBgTarget_CellBg(), REG_COL_U32[r].header
)
end
local cell_x, cell_y = ImGui.ImGui_GetCursorScreenPos(ctx)
region_cell_positions[r] = { x = cell_x, y = cell_y }
ImGui.ImGui_PushID(ctx, "region_click|" .. r)
local clicked_region = ImGui.ImGui_Selectable(ctx, REGIONS[r].name, false)
local region_hovered = ImGui.ImGui_IsItemHovered(ctx)
-- Right-click drag paint for time selection
if TIME_PAINT.down and region_hovered then
if TIME_PAINT.min_row == nil then
TIME_PAINT.min_row = r
TIME_PAINT.max_row = r
else
if r < TIME_PAINT.min_row then TIME_PAINT.min_row = r end
if r > TIME_PAINT.max_row then TIME_PAINT.max_row = r end
end
end
if clicked_region then
local modifier_held = any_modifier_held()
if modifier_held then
-- Compute floored measure distance from cursor to clicked region start
if FCP_JUMP_REGIONS and row_of_cursor then
local st = reaper.GetPlayState()
local cursor_t = (st & 1) == 1 and reaper.GetPlayPosition() or reaper.GetCursorPosition()
local cur_m = measure_index_at_time(cursor_t)
local cur_frac = frac_in_measure_at_time(cursor_t)
local cur_eff = (cur_frac > 0.001) and (cur_m + 1) or cur_m
local hov_m = measure_index_at_time(REGIONS[r].pos or 0)
local hov_frac = frac_in_measure_at_time(REGIONS[r].pos or 0)
local hov_eff = (hov_frac > 0.001) and (hov_m + 1) or hov_m
FCP_JUMP_REGIONS.MEAS_OFFSET = math.floor(hov_eff - cur_eff)
end
reaper.SetProjExtState(
PROJ, JUMP_EXT_SECTION, JUMP_EXT_KEY, "ABS:" .. tostring(REGIONS[r].id)
)
else
reaper.SetProjExtState(
PROJ, JUMP_EXT_SECTION, JUMP_EXT_KEY, tostring(REGIONS[r].id)
)
end
if redirect_focus_after_click then
reaper.defer(redirect_focus_after_click)
end
end
ImGui.ImGui_PopID(ctx)
if region_hovered and row_of_cursor and row_of_cursor ~= r then
hovered_region_row = r
end
-- Track hovering over the current (active) region row
if region_hovered and row_of_cursor and row_of_cursor == r then
HOVER_CURRENT_REGION = true
end
-- Draw cursor position line if this is the active region
if row_of_cursor == r then
local reg_start = REGIONS[r].pos or 0
local reg_end = REGIONS[r].r_end or 0
local reg_len = reg_end - reg_start
if reg_len > 0 then
local st = reaper.GetPlayState()
local cursor_t = (st & 1) == 1 and reaper.GetPlayPosition() or reaper.GetCursorPosition()
local pct_through = (cursor_t - reg_start) / reg_len
if pct_through < 0 then pct_through = 0 end
if pct_through > 1 then pct_through = 1 end
local cell_w = FIRST_COL_W
local cell_h = row_h
local line_x = cell_x + (cell_w * pct_through)
local dl = ImGui.ImGui_GetWindowDrawList(ctx)
ImGui.ImGui_DrawList_AddLine(dl, line_x, cell_y - 2, line_x, cell_y + cell_h - 3, COL_CURSOR_LINE, 2.0)
end
end
-- Progress cell with drag-paint
ImGui.ImGui_TableNextColumn(ctx)
local st
if current_tab == "Keys" and PRO_KEYS_ACTIVE then
local diff_map = { Expert="X", Hard="H", Medium="M", Easy="E" }
local diff_key = diff_map[ACTIVE_DIFF] or "X"
st = (STATE_PRO_KEYS[diff_key] and STATE_PRO_KEYS[diff_key][r]) or 0
else
st = (STATE[current_tab]
and STATE[current_tab][display_diff]
and STATE[current_tab][display_diff][r]) or 0
end
local text = STATE_TEXT[st]
local col = STATE_COLOR[st]
ImGui.ImGui_PushID(ctx, current_tab .. "|" .. display_diff .. "|" .. r)
-- Get cell screen position before drawing
local prog_cell_x, prog_cell_y = ImGui.ImGui_GetCursorScreenPos(ctx)
ImGui.ImGui_PushStyleColor(ctx, ImGui.ImGui_Col_Text(), col)
local clicked = ImGui.ImGui_Selectable(ctx, text, false)
ImGui.ImGui_PopStyleColor(ctx)
-- Manual hit test for drag-paint: check if mouse is within this cell's bounds
local mouse_x, mouse_y = ImGui.ImGui_GetMousePos(ctx)
local cell_w = REGION_COL_W
local cell_h = row_h
local mouse_in_cell = mouse_x >= prog_cell_x and mouse_x < prog_cell_x + cell_w
and mouse_y >= prog_cell_y and mouse_y < prog_cell_y + cell_h
and mouse_y >= child_visible_y1 and mouse_y < child_visible_y2
if PAINT.down and mouse_in_cell and not PAINT.seEN[r] and not LISTEN_DRAG_ACTIVE then
apply_toggle(current_tab, display_diff, r)
PAINT.seEN[r], PAINT.did_any = true, true
-- Store redirect to call on mouse release, don't call immediately
if redirect_focus_after_click then
PAINT.pending_redirect = redirect_focus_after_click
end
end
-- Handle single click (only if not already processed by drag-paint)
if clicked and not PAINT.seEN[r] and not LISTEN_DRAG_ACTIVE then
apply_toggle(current_tab, display_diff, r)
PAINT.seEN[r], PAINT.did_any = true, true
-- Store redirect to call on mouse release, don't call immediately
if redirect_focus_after_click then
PAINT.pending_redirect = redirect_focus_after_click
end
end
ImGui.ImGui_PopID(ctx)
-- Time cell
ImGui.ImGui_TableNextColumn(ctx)
local time_diff = current_timer_diff()
local secs = get_region_time(current_tab, time_diff, r)
local time_str
if secs >= 3600 then
time_str = string.format("%d:%02d:%02d", math.floor(secs/3600), math.floor(secs/60)%60, secs%60)
elseif secs >= 60 then
time_str = string.format("%d:%02d", math.floor(secs/60), secs%60)
else
time_str = string.format(":%02d", secs)
end
ImGui.ImGui_PushID(ctx, "time|" .. current_tab .. "|" .. time_diff .. "|" .. r)
local time_clicked = ImGui.ImGui_Selectable(ctx, time_str, false)
if time_clicked then
if any_modifier_held() then
-- Modifier+click: reset to 0
REGION_TIME[current_tab] = REGION_TIME[current_tab] or {}
REGION_TIME[current_tab][time_diff] = REGION_TIME[current_tab][time_diff] or {}
REGION_TIME[current_tab][time_diff][r] = 0
save_region_time(current_tab, time_diff, r, 0)
else
-- Left click: subtract 10 seconds (min 0)
local new_secs = math.max(0, secs - 10)
REGION_TIME[current_tab] = REGION_TIME[current_tab] or {}
REGION_TIME[current_tab][time_diff] = REGION_TIME[current_tab][time_diff] or {}
REGION_TIME[current_tab][time_diff][r] = new_secs
save_region_time(current_tab, time_diff, r, new_secs)
end
REGION_TIME_LAST_TICK = nil -- reset tick anchor so counting restarts fresh
end
-- Right-click: reset to 0
if ImGui.ImGui_IsItemClicked(ctx, 1) then
REGION_TIME[current_tab] = REGION_TIME[current_tab] or {}
REGION_TIME[current_tab][time_diff] = REGION_TIME[current_tab][time_diff] or {}
REGION_TIME[current_tab][time_diff][r] = 0
save_region_time(current_tab, time_diff, r, 0)
REGION_TIME_LAST_TICK = nil -- reset tick anchor so counting restarts fresh
end
ImGui.ImGui_PopID(ctx)
end
-- Draw preview line showing where cursor would land after jump
if hovered_region_row and row_of_cursor then
draw_preview_line(ctx, row_of_cursor, hovered_region_row, region_cell_positions, row_h)
end
-- Update hover preview offset for next frame's header InputInt
if hovered_region_row and row_of_cursor then
local cur_reg = REGIONS[row_of_cursor]
local hov_reg = REGIONS[hovered_region_row]
if cur_reg and hov_reg and not any_modifier_held() then
local cur_m = measure_index_at_time(cur_reg.pos or 0)
local cur_frac = frac_in_measure_at_time(cur_reg.pos or 0)
local cur_eff = (cur_frac > 0.001) and (cur_m + 1) or cur_m
local hov_m = measure_index_at_time(hov_reg.pos or 0)
local hov_frac = frac_in_measure_at_time(hov_reg.pos or 0)
local hov_eff = (hov_frac > 0.001) and (hov_m + 1) or hov_m
HOVER_PREVIEW_OFFSET = hov_eff - cur_eff
HOVER_MODIFIER_DISTANCE = nil
elseif cur_reg and hov_reg and any_modifier_held() then
HOVER_PREVIEW_OFFSET = nil
-- Compute measure distance from cursor to hovered region start
local st = reaper.GetPlayState()
local cursor_t = (st & 1) == 1 and reaper.GetPlayPosition() or reaper.GetCursorPosition()
local cur_m = measure_index_at_time(cursor_t)
local cur_frac = frac_in_measure_at_time(cursor_t)
local cur_eff = (cur_frac > 0.001) and (cur_m + 1) or cur_m
local hov_m = measure_index_at_time(hov_reg.pos or 0)
local hov_frac = frac_in_measure_at_time(hov_reg.pos or 0)
local hov_eff = (hov_frac > 0.001) and (hov_m + 1) or hov_m
local dist = hov_eff - cur_eff
HOVER_MODIFIER_DISTANCE = dist
else
HOVER_PREVIEW_OFFSET = nil
HOVER_MODIFIER_DISTANCE = nil
end
else
-- Hovering current region (hovered_region_row is nil)
if HOVER_CURRENT_REGION and any_modifier_held() and row_of_cursor then
HOVER_PREVIEW_OFFSET = nil
local hov_reg = REGIONS[row_of_cursor]
if hov_reg then
local st = reaper.GetPlayState()
local cursor_t = (st & 1) == 1 and reaper.GetPlayPosition() or reaper.GetCursorPosition()
local cur_m = measure_index_at_time(cursor_t)
local cur_frac = frac_in_measure_at_time(cursor_t)
local cur_eff = (cur_frac > 0.001) and (cur_m + 1) or cur_m
local hov_m = measure_index_at_time(hov_reg.pos or 0)
local hov_frac = frac_in_measure_at_time(hov_reg.pos or 0)
local hov_eff = (hov_frac > 0.001) and (hov_m + 1) or hov_m
HOVER_MODIFIER_DISTANCE = hov_eff - cur_eff
-- Draw preview line at the region start when distance is non-zero
if HOVER_MODIFIER_DISTANCE ~= 0 and region_cell_positions[row_of_cursor] then
local tc = region_cell_positions[row_of_cursor]
local dl = ImGui.ImGui_GetWindowDrawList(ctx)
ImGui.ImGui_DrawList_AddLine(dl, tc.x, tc.y - 2, tc.x, tc.y + row_h - 3, COL_PREVIEW_LINE, 2.0)
end
else
HOVER_MODIFIER_DISTANCE = nil
end
elseif HOVER_CURRENT_REGION then
-- Hovering current region without modifier: preview offset is 0
HOVER_PREVIEW_OFFSET = 0
HOVER_MODIFIER_DISTANCE = nil
else
HOVER_PREVIEW_OFFSET = nil
HOVER_MODIFIER_DISTANCE = nil
end
end
-- Draw jump-target line based on measure offset when not hovering a region
if not hovered_region_row and not HOVER_CURRENT_REGION and FCP_JUMP_REGIONS and FCP_JUMP_REGIONS.MEAS_OFFSET ~= 0 then
local st = reaper.GetPlayState()
local cursor_t = (st & 1) == 1 and reaper.GetPlayPosition() or reaper.GetCursorPosition()
local target_t = jump_time_by_measures(cursor_t, FCP_JUMP_REGIONS.MEAS_OFFSET)
local target_row = nil
for i = 1, #REGIONS do
local rs = REGIONS[i].pos or 0
local re = REGIONS[i].r_end or 0
if target_t >= rs and target_t < re then target_row = i; break end
end
if target_row and region_cell_positions[target_row] then
local treg = REGIONS[target_row]
local tlen = (treg.r_end or 0) - (treg.pos or 0)
if tlen > 0 then
local pct = (target_t - treg.pos) / tlen
-- Snap to next region if landing at boundary
if pct >= 0.9999 and target_row < #REGIONS then
target_row = target_row + 1
pct = 0
end
if pct < 0 then pct = 0 elseif pct > 1 then pct = 1 end
local tc = region_cell_positions[target_row]
if tc then
local line_x = tc.x + (FIRST_COL_W * pct)
local dl = ImGui.ImGui_GetWindowDrawList(ctx)
ImGui.ImGui_DrawList_AddLine(dl, line_x, tc.y - 2, line_x, tc.y + row_h - 3, COL_PREVIEW_LINE, 2.0)
end
end
end
end
ImGui.ImGui_EndTable(ctx)
end
end
ImGui.ImGui_EndChild(ctx)
end
-- Overdrive table drawing -----------------------------------------------
-- Track last cursor measure for centering logic (edit cursor only)
local OV_LAST_CURSOR_M = nil
-- Helper to get region color as U32
local function get_region_color_u32(region, alpha)
local native_color = region.color or 0
if native_color == 0 then
native_color = reaper.GetThemeColor("col_region", 0) or 0
end
if (native_color & 0x1000000) ~= 0 then
native_color = native_color & 0xFFFFFF
end
local r, g, b = reaper.ColorFromNative(native_color)
return ImGui.ImGui_ColorConvertDouble4ToU32((r or 0)/255, (g or 0)/255, (b or 0)/255, alpha or 1)
end
-- Helper to find region containing a given time
local function find_region_at_time(t)
for i = 1, #REGIONS do
local rs = REGIONS[i].pos or 0
local re = REGIONS[i].r_end or 0
if t >= rs and t < re then
return REGIONS[i], i
end
end
return nil, nil
end
-- Draw region color bar above overdrive table
local function draw_region_bar(ctx, first_m, start_col, end_col, cell_w, label_w, cell_padding, border_padding)
local bar_h = 6 -- Height of the region bar
local bar_y_offset = 0 -- Gap between bar and table
-- Get current cursor position (where bar will be drawn)
local bar_start_x, bar_start_y = ImGui.ImGui_GetCursorScreenPos(ctx)
-- Offset for the label column
local measures_start_x = bar_start_x + label_w + cell_padding + (border_padding / 2) - 4
local dl = ImGui.ImGui_GetWindowDrawList(ctx)
-- Track which region each measure belongs to
local measure_regions = {}
for c = start_col, end_col do
local measure_num = (OVERDRIVE_MEASURES.first or 1) + c
-- Get the time at the first beat of this measure
local _, qn_start = reaper.TimeMap_GetMeasureInfo(0, measure_num - 1)
local time_start = reaper.TimeMap_QNToTime(qn_start)
local region = find_region_at_time(time_start)
measure_regions[c] = region
end
-- Draw continuous segments for each region and store segment bounds for tooltip
local col_total_w = cell_w + cell_padding
local segment_start_col = start_col
local current_region = measure_regions[start_col]
local segments = {} -- Store segment bounds and region for tooltip
for c = start_col, end_col + 1 do
local this_region = measure_regions[c]
-- Check if region changed or we're at the end
if this_region ~= current_region or c > end_col then
-- Draw segment for the previous region
if current_region then
local seg_x1 = measures_start_x + (segment_start_col - start_col) * col_total_w
local seg_x2 = measures_start_x + (c - start_col) * col_total_w - (cell_padding / 2)
local col = get_region_color_u32(current_region, 1.0)
ImGui.ImGui_DrawList_AddRectFilled(dl, seg_x1, bar_start_y, seg_x2, bar_start_y + bar_h, col)
table.insert(segments, { x1 = seg_x1, x2 = seg_x2, region = current_region })
end
-- Start new segment
segment_start_col = c
current_region = this_region
end
end
-- Check for tooltip on hover
local mouse_x, mouse_y = ImGui.ImGui_GetMousePos(ctx)
if mouse_y >= bar_start_y and mouse_y <= bar_start_y + bar_h then
for _, seg in ipairs(segments) do
if mouse_x >= seg.x1 and mouse_x <= seg.x2 then
ImGui.ImGui_SetTooltip(ctx, seg.region.name or "Unknown Region")
break
end
end
end
-- Reserve space for the bar
ImGui.ImGui_Dummy(ctx, 0, bar_h + bar_y_offset)
end
-- Helper to get track color as U32
local function get_track_color_u32(trackname, alpha)
local n = reaper.CountTracks(0)
for i = 0, n - 1 do
local tr = reaper.GetTrack(0, i)
local ok, name = reaper.GetTrackName(tr)
if ok and name == trackname then
local native_color = reaper.GetTrackColor(tr)
if native_color == 0 then
-- Track has no custom color, return a default gray
return ImGui.ImGui_ColorConvertDouble4ToU32(0.3, 0.3, 0.3, alpha or 1)
end
-- Convert native color to RGB
local r, g, b = reaper.ColorFromNative(native_color)
return ImGui.ImGui_ColorConvertDouble4ToU32((r or 0)/255, (g or 0)/255, (b or 0)/255, alpha or 1)
end
end
-- Track not found, return default
return ImGui.ImGui_ColorConvertDouble4ToU32(0.3, 0.3, 0.3, alpha or 1)
end
-- Helper to find track by name
local function find_track_by_name(trackname)
local n = reaper.CountTracks(0)
for i = 0, n - 1 do
local tr = reaper.GetTrack(0, i)
local ok, name = reaper.GetTrackName(tr)
if ok and name == trackname then
return tr
end
end
return nil
end
-- Helper to get measure number from PPQ position (1-based)
local function ppq_to_measure(tk, ppq)
local proj_time = reaper.MIDI_GetProjTimeFromPPQPos(tk, ppq)
local _, measure_raw = reaper.TimeMap2_timeToBeats(0, proj_time)
return math.floor(measure_raw) + 1
end
-- Helper to find an existing overdrive note's exact PPQ bounds in any track for a given measure
local function find_existing_ov_ppq_bounds(measure_num)
-- Get measure bounds in project time
local _, qn_start, qn_end = reaper.TimeMap_GetMeasureInfo(0, measure_num - 1)
local time_start = reaper.TimeMap_QNToTime(qn_start)
local time_end = reaper.TimeMap_QNToTime(qn_end)
-- Search all overdrive tracks for an existing OV note in this measure
for _, tn in ipairs(OVERDRIVE_TRACKS) do
local tr = find_track_by_name(tn)
if tr then
local tk = first_midi_take_on_track(tr)
if tk then
local ppq_meas_start = reaper.MIDI_GetPPQPosFromProjTime(tk, time_start)
local ppq_meas_end = reaper.MIDI_GetPPQPosFromProjTime(tk, time_end)
local _, note_cnt = reaper.MIDI_CountEvts(tk)
for ni = 0, note_cnt - 1 do
local ok, _, _, ppq_s, ppq_e, _, pitch = reaper.MIDI_GetNote(tk, ni)
if ok and pitch == OVERDRIVE_PITCH then
if ppq_s < ppq_meas_end and ppq_e > ppq_meas_start then
-- Found an OV note in this measure, return its project time bounds
local proj_time_start = reaper.MIDI_GetProjTimeFromPPQPos(tk, ppq_s)
local proj_time_end = reaper.MIDI_GetProjTimeFromPPQPos(tk, ppq_e)
return proj_time_start, proj_time_end
end
end
end
end
end
end
return nil, nil -- No existing OV found
end
-- Helper to insert overdrive note on a track at exact project time bounds
local function insert_ov_at_times(trackname, proj_time_start, proj_time_end, measure_num)
local tr = find_track_by_name(trackname)
if not tr then return false end
local tk = first_midi_take_on_track(tr)
if not tk then return false end
-- Convert project times to PPQ for this take
local ppq_start = reaper.MIDI_GetPPQPosFromProjTime(tk, proj_time_start)
local ppq_end = reaper.MIDI_GetPPQPosFromProjTime(tk, proj_time_end)
-- Get measure bounds for FILL note removal
local _, qn_start, qn_end = reaper.TimeMap_GetMeasureInfo(0, measure_num - 1)
local meas_time_start = reaper.TimeMap_QNToTime(qn_start)
local meas_time_end = reaper.TimeMap_QNToTime(qn_end)
local meas_ppq_start = reaper.MIDI_GetPPQPosFromProjTime(tk, meas_time_start)
local meas_ppq_end = reaper.MIDI_GetPPQPosFromProjTime(tk, meas_time_end)
-- Remove any FILL notes (120-124) in this measure
local _, note_cnt = reaper.MIDI_CountEvts(tk)
for ni = note_cnt - 1, 0, -1 do
local ok, _, _, ppq_s, ppq_e, _, pitch = reaper.MIDI_GetNote(tk, ni)
if ok and pitch >= 120 and pitch <= 124 then
if ppq_s < meas_ppq_end and ppq_e > meas_ppq_start then
reaper.MIDI_DeleteNote(tk, ni)
end
end
end
-- Check if OV already exists in this measure (don't duplicate)
_, note_cnt = reaper.MIDI_CountEvts(tk)
for ni = 0, note_cnt - 1 do
local ok, _, _, ppq_s, ppq_e, _, pitch = reaper.MIDI_GetNote(tk, ni)
if ok and pitch == OVERDRIVE_PITCH then
if ppq_s < meas_ppq_end and ppq_e > meas_ppq_start then
-- Already has OV, skip
reaper.MIDI_Sort(tk)
return false
end
end
end
-- Insert the new overdrive note
reaper.MIDI_InsertNote(tk, false, false, ppq_start, ppq_end, 0, OVERDRIVE_PITCH, 100, false)
reaper.MIDI_Sort(tk)
return true
end
-- Helper to delete overdrive note from a specific track in a measure
local function delete_ov_from_track(trackname, measure_num)
local tr = find_track_by_name(trackname)
if not tr then return false end
local tk = first_midi_take_on_track(tr)
if not tk then return false end