/
Key Sequences.lua
2574 lines (2479 loc) · 101 KB
/
Key Sequences.lua
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
--@author Souk21
--@description Key Sequences
--@about Create key sequence shortcuts
--@changelog
-- Upgrade to ReaImGui 0.9
-- Use ReaImGui shims to stay compatible through future ReaImGui releases
--@version 2.3
--@provides
-- [main] . > souk21_Key Sequences.lua
local font_name = "Verdana"
if reaper.CF_GetCommandText == nil or reaper.ImGui_GetBuiltinPath == nil or reaper.JS_Window_Find == nil then
reaper.ShowMessageBox("This script requires SWS, ReaImGui (min 0.9) and js_ReaScriptAPI.", "Missing dependency", 0)
return
end
package.path = reaper.ImGui_GetBuiltinPath() .. '/?.lua'
local ImGui = require 'imgui' '0.9'
local script_path = reaper.GetResourcePath() .. "/Scripts/Souk21_sequences/"
local prefix = "souk21_sequences_"
local imgui_window_name = "Key Sequences"
local default_style_filename = "defaultStyle.data"
local ctx = ImGui.CreateContext(imgui_window_name)
local draw_list
local font
if font_name ~= nil then
font = ImGui.CreateFont(font_name, 13)
ImGui.Attach(ctx, font)
end
local FLT_MIN = ImGui.NumericLimits_Float()
local sections = {
{ name = "Main", id = 0, short_name = "Main" },
{ name = "Main (alt recording)", id = 100, short_name = "Main (alt)" },
{ name = "MIDI Editor", id = 32060, short_name = "Midi Editor" },
{ name = "MIDI Event List Editor", id = 32061, short_name = "Midi Event List" },
{ name = "Media Explorer", id = 32063, short_name = "Media Ex." }
--It's impossible to send commands to midi inline editor
--{name= "MIDI Inline Editor", id=32062, short_name="Midi Inline"},
}
local files = {}
local removed = {}
local dirty = {}
local cur_file_idx = 0
local selected_section = 1
local new_seq_name = ""
local new_display_name = ""
local new_text = ""
local new_exit = false
local new_hide = false
local new_wants_focus = false
local edit_wants_focus = false
local table_scroll = 0
local first_load = true
local last_dirty_name = ""
local adding = nil
local key_popup_requested = false
local key_popup_opened = false
local did_change_show_after = false
local key_hwnd
local center
local dragging
local move_request_old
local move_request_new
local preview_opened = false
local default_style = {
background_color = 0x000000,
foreground_color = 0xffffff,
flash_color = 0xff5555,
hover_color = 0xffaaaa,
font = "Verdana",
font_size = 13,
padding = 20,
desc_offset = 20,
line_offset = 5,
pos_mode = 0, --0 follow mouse, 1 fixed position
size_mode = 0, --0 auto, 1 fixed size
mouse_h_align = 0, --0 left, 1 middle, 2 right
mouse_v_align = 0, --0 bottom, 1 middle, 2 top
first_frame = true, --set to false by UI() after first frame
shown = false,
}
local copied_style
local current_style = nil
local edit_flags = ImGui.WindowFlags_AlwaysAutoResize
local edit_frame_count = -1
local style_help_text = {
["None"] = "Press P to open/close preview...",
["PosMode"] = "Mouse: spawn at mouse. Fixed: spawn at a fixed position",
["MousePos"] = "Anchor for hints window",
["FixedPos"] = "Position where hints will spawn",
["SizeMode"] = "Auto: size is computed from layout. Fixed: size is fixed",
["FixedSize"] = "Size of the hints window",
["BG"] = "Background color",
["FG"] = "Text color",
["Flash"] = "Color on key down / click",
["Hover"] = "Color on mouse hover",
["Padding"] =
"Space between window borders and text (In fixed size mode: space between top and left border and text)",
["DescOffset"] = "Horizontal space between shortcut key(s) and name",
["LineOffset"] = "Vertical space between lines",
["Font"] = "Font family, unknown font will result in default font being used",
["FontSize"] = "Font size",
}
local action_popup_requested = false
local action_popup_opened = false
-- -1: not waiting, 0: waiting during new action/key creation, n: waiting during action[n] key/action update/change
local waiting_for_key = -1
local waiting_for_action = -1
local rect_flags = ImGui.DrawFlags_RoundCornersAll
local popup_flags = ImGui.WindowFlags_NoMove | ImGui.WindowFlags_AlwaysAutoResize
local main_hwnd = nil
-- global colors/styles
local colors = {
{ ImGui.Col_WindowBg, 0x202123FF },
{ ImGui.Col_PopupBg, 0x202123FF },
{ ImGui.Col_TitleBgActive, 0x343434ff },
{ ImGui.Col_TitleBg, 0x242424ff },
{ ImGui.Col_Button, 0x565656ff },
{ ImGui.Col_ButtonHovered, 0x606060ff },
{ ImGui.Col_ButtonActive, 0x707070ff },
{ ImGui.Col_FrameBg, 0x00000000 },
{ ImGui.Col_FrameBgHovered, 0xffffff33 },
{ ImGui.Col_Header, 0xFFFFFF00 },
{ ImGui.Col_HeaderHovered, 0xFCFCFC00 },
{ ImGui.Col_HeaderActive, 0xFFFFFF00 },
{ ImGui.Col_ChildBg, 0x2D2D2D00 },
{ ImGui.Col_ScrollbarBg, 0xfff00000 },
{ ImGui.Col_TableRowBgAlt, 0x00000000 },
{ ImGui.Col_Text, 0xffffffcd },
{ ImGui.Col_ResizeGrip, 0xffffff33 },
{ ImGui.Col_ResizeGripHovered, 0xffffff44 },
{ ImGui.Col_Border, 0x606060ff },
}
local styles = {
--window rounding causes a transparent line to show between title bar and inner window :/
{ ImGui.StyleVar_WindowRounding, 7 },
{ ImGui.StyleVar_WindowPadding, 10, 10 },
{ ImGui.StyleVar_WindowBorderSize, 1 },
{ ImGui.StyleVar_WindowTitleAlign, 0.5, 0.5 },
{ ImGui.StyleVar_FrameBorderSize, 0 },
{ ImGui.StyleVar_FrameRounding, 3 },
{ ImGui.StyleVar_ScrollbarSize, 12 },
{ ImGui.StyleVar_ScrollbarRounding, 12 },
{ ImGui.StyleVar_CellPadding, 3, 5 },
{ ImGui.StyleVar_ChildRounding, 3 },
}
function Move(t, old, new)
table.insert(t, new, table.remove(t, old))
end
function StripComma(str)
str = tostring(str)
str = string.gsub(str, ",", "")
return str
end
function SequencesToCSV()
local result = "Sequence,Key,Text,Command,Hide,Keep Open\n"
for _, sequence in ipairs(files) do
for _, action in ipairs(sequence.actions) do
if action.text ~= nil then
result = result .. string.format("%s,,%s,,,\n",
StripComma(sequence.name),
StripComma(action.text)
)
else
result = result .. string.format("%s,%s,%s,%s,%s,%s\n",
StripComma(sequence.name),
StripComma(action.key_text),
StripComma(action.display_name),
StripComma(action.action_text),
StripComma(action.hidden),
StripComma(not action.exit)
)
end
end
end
return result
end
function Button(txt, wIn, hIn, alpha)
if hIn == nil then
hIn = 20
end
local ret = false
ImGui.PushStyleColor(ctx, ImGui.Col_Border, 0xffffff00)
if alpha ~= nil then
ImGui.PushStyleColor(ctx, ImGui.Col_Text, 0xffffff00 + math.floor(alpha * 255))
end
ImGui.PushStyleVar(ctx, ImGui.StyleVar_FrameBorderSize, 1)
ImGui.PushStyleVar(ctx, ImGui.StyleVar_FramePadding, 0, 0)
if ImGui.Button(ctx, txt, wIn, hIn) then
ret = true
end
local w = ImGui.GetItemRectSize(ctx)
local x, y = ImGui.GetItemRectMin(ctx)
local margin = 2
w = w - margin * 2
x = x + margin
ImGui.DrawList_AddLine(draw_list, x, y, x + w, y, 0xffffff15, 1)
ImGui.PopStyleVar(ctx, 2)
ImGui.PopStyleColor(ctx)
if alpha ~= nil then
ImGui.PopStyleColor(ctx)
end
return ret
end
function ArrowButton(wIn, hIn, up, disabled)
local x, y = ImGui.GetCursorScreenPos(ctx)
local btn_name = "##up"
if not up then btn_name = "##down" end
local ret = Button(btn_name, wIn, hIn)
local color = 0xffffffcc
if disabled then
color = 0xffffff33
end
x = x + 15
if up then
y = y + 14
ImGui.DrawList_AddTriangleFilled(draw_list, x, y, x - 10, y, x - 5, y - 8, color)
else
y = y + 6
ImGui.DrawList_AddTriangleFilled(draw_list, x, y, x - 5, y + 8, x - 10, y, color)
end
return ret
end
function DeleteButton(name, wIn, hIn)
if wIn == nil then
wIn = 20
end
if hIn == nil then
hIn = 20
end
local ret = Button(name, wIn, hIn)
local w = ImGui.GetItemRectSize(ctx)
local x, y = ImGui.GetItemRectMin(ctx)
x = x + w / 2
y = y + w / 2
local size = 4
ImGui.DrawList_AddLine(draw_list, x - size, y - size, x + size, y + size, 0xffffffcc, 2)
ImGui.DrawList_AddLine(draw_list, x - size, y + size, x + size, y - size, 0xffffffcc, 2)
return ret
end
function DragDouble(label, value, speed, min, max, format)
local ret, new_value = ImGui.DragDouble(ctx, label, value, speed, min, max, format)
if ImGui.IsItemHovered(ctx) then
ImGui.SetMouseCursor(ctx, ImGui.MouseCursor_ResizeEW)
end
return ret, new_value
end
function ValidateName(name)
local black_list = "[^%w _%-.]"
name = string.gsub(name, black_list, "")
--remove leading/trailing spaces and '.'
local matched = false
local count
repeat
matched = false
name, count = string.gsub(name, "^%s+", "")
if count > 0 then
matched = true
end
name, count = string.gsub(name, "%s+$", "")
if count > 0 then
matched = true
end
name, count = string.gsub(name, "^%.+", "")
if count > 0 then
matched = true
end
name, count = string.gsub(name, "%.+$", "")
if count > 0 then
matched = true
end
until not matched
if #name == 0 then
name = "Invalid"
end
while true do
local found_duplicate = false
for _, file in ipairs(files) do
if string.lower(file.name) == string.lower(name) then
found_duplicate = true
name = name .. "2"
end
end
if not found_duplicate then
break
end
end
return name
end
function MatchStyle(str)
function NumberOrNilIfEmpty(str)
if str == "" then return nil end
return tonumber(str)
end
-- Using * instead of + for X, Y, W and H so they are optional
local style_pat =
"STYLE BG (%S+) FG (%S+) FC (%S+) HOVER (%S+) FNTSZ (%S+) PAD (%S+) DESC (%S+) LINE (%S+) POS (%S+) SIZE (%S+) X (%S*) Y (%S*) W (%S*) H (%S*) MH (%S+) MV (%S+) FONT ([^\n]*)"
local bg, fg, flash, hover, font_size, padding, desc_offset, line_offset, pos_mode, size_mode, x, y, w, h, mouse_h, mouse_v, font =
string
.match(str
, style_pat)
if bg == nil then
return nil
end
x = NumberOrNilIfEmpty(x)
y = NumberOrNilIfEmpty(y)
w = NumberOrNilIfEmpty(w)
h = NumberOrNilIfEmpty(h)
return {
background_color = tonumber(bg, 16),
foreground_color = tonumber(fg, 16),
flash_color = tonumber(flash, 16),
hover_color = tonumber(hover, 16),
font = font,
font_size = tonumber(font_size),
padding = tonumber(padding),
desc_offset = tonumber(desc_offset),
line_offset = tonumber(line_offset),
pos_mode = tonumber(pos_mode),
size_mode = tonumber(size_mode),
pos_x = x,
pos_y = y,
width = w,
height = h,
mouse_h_align = tonumber(mouse_h),
mouse_v_align = tonumber(mouse_v),
first_frame = true,
shown = false,
}
end
function Sanitize(str)
str = string.gsub(str, "\"", "'")
str = string.gsub(str, "\\", "\\\\")
return string.gsub(str, "]]", "")
end
function UI(actions, layout, shown, window_name)
function ToRGB(int)
local r = ((int >> 16) & 255) / 255.0
local g = ((int >> 8) & 255) / 255.0
local b = (int & 255) / 255.0
return r, g, b
end
function SetColor(int)
local r, g, b = ToRGB(int)
gfx.set(r, g, b)
end
if layout.first_frame then
--Before doing anything we init window, so gfx_retina is computed, etc
--We still have first frame work to do later so we don't set first_frame = false
gfx.init(window_name, 0, 0, 0, 0, 0)
gfx.ext_retina = 1
layout.hwnd = reaper.JS_Window_Find(window_name, true)
reaper.JS_Window_SetStyle(layout.hwnd, "POPUP")
reaper.JS_Window_SetOpacity(layout.hwnd, "ALPHA", 0)
reaper.JS_Window_SetFocus(layout.hwnd)
end
local longest_key = ""
local longest_name = ""
for _, action in ipairs(actions) do
if not action.hidden then
if action.text == nil then
if #action.key_text > #longest_key then
longest_key = action.key_text
end
if #action.display_name > #longest_name then
longest_name = action.display_name
end
else
if #action.text > #longest_name then
longest_name = action.text
end
end
end
end
local os = reaper.GetOS()
local is_mac = os == "OSX32" or os == "OSX64" or os == "macOS-arm64"
local retina_scale = 1
if is_mac then
retina_scale = gfx.ext_retina
end
local font_size = layout.font_size * gfx.ext_retina
gfx.setfont(1, layout.font, font_size)
longest_key = gfx.measurestr(longest_key)
longest_name = gfx.measurestr(longest_name)
local nonHiddenActionCount = 0
for _, action in ipairs(actions) do
if not action.hidden then
nonHiddenActionCount = nonHiddenActionCount + 1
end
end
local padding = layout.padding * retina_scale
local line_offset = layout.line_offset * retina_scale
local desc_offset = layout.desc_offset * retina_scale
local desc_position = padding + longest_key + desc_offset
local height, width
if layout.height ~= nil then
height = layout.height * retina_scale
else
height = padding * 2 + nonHiddenActionCount * gfx.texth + (nonHiddenActionCount - 1) * line_offset
end
if layout.width ~= nil then
width = layout.width * retina_scale
else
width = desc_position + longest_name + padding
end
-- On Windows and Linux, screen coordinates are relative to *upper* left corner of the primary display, and the positive Y-axis points downward.
-- On macOS, screen coordinates are relative to the *bottom* left corner of the primary display, and the positive Y-axis points upward.
-- On Windows, windows will appear by default at the bottom right of mouse cursor
-- On macOS, windows will appear by default at the top right of mouse cursor
if layout.first_frame then
layout.first_frame = false
layout.flash_time = nil
layout.flash_action = nil
local x, y = reaper.GetMousePosition()
if is_mac then
if layout.pos_mode == 0 then
--Mouse position
local left = x
local bot = y
local right = x + (width / retina_scale)
local top = y + (height / retina_scale)
if layout.mouse_h_align == 1 then
left = left - (width / retina_scale) / 2
right = right - (width / retina_scale) / 2
elseif layout.mouse_h_align == 2 then
left = left - (width / retina_scale)
right = right - (width / retina_scale)
end
if layout.mouse_v_align == 1 then
top = top - (height / retina_scale) / 2
bot = bot - (height / retina_scale) / 2
elseif layout.mouse_v_align == 2 then
top = top - (height / retina_scale)
bot = bot - (height / retina_scale)
end
local screen_left, screen_top, screen_right, screen_bot = reaper.JS_Window_GetViewportFromRect(10, 10, 10
, 10
, true)
if left <= 0 then
right = right - left + 1
left = 1
elseif right >= screen_right then
left = left - (right - (screen_right - 1))
right = screen_right - 1
end
if bot <= 0 then
top = top - bot
bot = 1
elseif top >= screen_top then
bot = bot - (top - (screen_top - 1))
top = screen_top - 1
end
layout.pos_x = left
layout.pos_y = bot
end
else
if layout.pos_mode == 0 then
local left = x
local top = y
local right = x + width
local bot = y + height
if layout.mouse_v_align == 0 then
bot = bot - height
top = top - height
elseif layout.mouse_v_align == 1 then
bot = bot - height / 2
top = top - height / 2
end
if layout.mouse_h_align == 1 then
left = left - width / 2
right = right - width / 2
elseif layout.mouse_h_align == 2 then
left = left - width
right = right - width
end
-- credit @nofish
-- acount for multi-monitor setups
-- https://forum.cockos.com/showpost.php?p=1883879&postcount=4
-- use current mouse position for the second (multimonitor) rectangle
local screen_left, screen_top, screen_right, screen_bot = reaper.my_getViewport(10, 10, 10, 10, x, y,
x + 10, y + 10, true)
if left <= 0 then
right = right - left + 1
left = 1
elseif right >= screen_right then
left = left - (right - (screen_right - 1))
right = screen_right - 1
end
if top <= 0 then
bot = bot - top + 1
top = 1
elseif bot >= screen_bot then
top = top - (bot - (screen_bot - 1))
bot = screen_bot - 1
end
layout.pos_x = left
layout.pos_y = top
end
end
end
local pos_x = layout.pos_x
local pos_y = layout.pos_y
local char = gfx.getchar()
local unmatched = char > 0
local cap = gfx.mouse_cap
local mouse_down = cap & 1 == 1
local clicked = false
if not layout.mouse_down and mouse_down then
clicked = true
end
layout.mouse_down = mouse_down
local cmd = cap & 4 == 4
local shift = cap & 8 == 8
local alt = cap & 16 == 16
local ctrl = cap & 32 == 32
local exit = false
local command = nil
if shown then
if not layout.shown then
-- Only on first shown frame
reaper.JS_Window_SetZOrder(layout.hwnd, "TOPMOST")
reaper.JS_Window_SetOpacity(layout.hwnd, "ALPHA", 1)
layout.shown = true
end
-- an empty string resizes and reposition current gfx window
gfx.init("", width / retina_scale, height / retina_scale, 0, pos_x, pos_y)
gfx.update()
SetColor(layout.background_color)
gfx.rect(0, 0, width, height, true)
gfx.y = padding
end
local actionDrawn = 1
for i, action in ipairs(actions) do
local wasDrawn = false
if action.text ~= nil then
gfx.x = desc_position
SetColor(layout.foreground_color)
gfx.drawstr(action.text)
wasDrawn = true
else
local key_pressed = char == action.key and action.cmd == cmd and action.shift == shift and
action.alt == alt
and
action.ctrl == ctrl
if action.hidden then
if key_pressed then
if action.native then
command = action.action
else
command = reaper.NamedCommandLookup(action.action)
end
if action.exit then
exit = true
end
end
else
wasDrawn = true
gfx.x = padding
local hover_mid = gfx.y + gfx.texth / 2
local hover_top = math.max(0, hover_mid - line_offset / 2 - gfx.texth / 2)
local hover_bot = math.min(gfx.h, hover_mid + line_offset / 2 + gfx.texth / 2)
local is_hover = gfx.mouse_x >= 0 and gfx.mouse_x <= gfx.w and gfx.mouse_y >= hover_top and
gfx.mouse_y < hover_bot
local is_flashing = layout.flash_action == i and reaper.time_precise() - layout.flash_time < 0.1
if is_flashing then
SetColor(layout.flash_color)
elseif key_pressed or (is_hover and clicked) then
SetColor(layout.flash_color)
layout.flash_action = i
layout.flash_time = reaper.time_precise()
if action.native then
command = action.action
else
command = reaper.NamedCommandLookup(action.action)
end
if action.exit then
exit = true
end
elseif is_hover then
SetColor(layout.hover_color)
else
SetColor(layout.foreground_color)
end
if shown then
gfx.drawstr(action.key_text)
gfx.x = desc_position
gfx.drawstr(action.display_name)
end
end
end
if wasDrawn then
if actionDrawn ~= nonHiddenActionCount then
gfx.y = gfx.y + gfx.texth + line_offset
end
actionDrawn = actionDrawn + 1
end
end
if command ~= nil then
unmatched = false
end
return char, command, exit, layout.hwnd, unmatched
end
function ActionInfo(int_id, section_id)
local native, stable_id, command_text
command_text = Sanitize(reaper.CF_GetCommandText(section_id, int_id))
local lookup = reaper.ReverseNamedCommandLookup(int_id)
if lookup == nil then
native = true
stable_id = int_id
else
native = false
stable_id = "_" .. lookup
end
return stable_id, native, command_text
end
--returns nil if not opened / canceled, false if waiting for action, true if got action
function ActionPopup(id, section_id)
local popup_name = "Action##" .. id
local action, action_text, native
if action_popup_requested then
action_popup_requested = false
action_popup_opened = true
reaper.PromptForAction(1, 0, section_id)
ImGui.OpenPopup(ctx, popup_name)
end
if not action_popup_opened then return nil end
local got_action = false ---@type boolean | nil
ImGui.SetNextWindowPos(ctx, center[1], center[2], ImGui.Cond_Appearing, 0.5, 0.5)
if ImGui.BeginPopupModal(ctx, popup_name, nil, popup_flags) then
local ret = reaper.PromptForAction(0, 0, section_id)
if ret > 0 then
action, native, action_text = ActionInfo(ret, section_id)
got_action = true
action_popup_opened = false
ImGui.CloseCurrentPopup(ctx)
reaper.PromptForAction(-1, 0, section_id)
end
ImGui.Text(ctx, "Pick an action in the action list")
MoveCursor(0, 7)
if Button("Cancel", -FLT_MIN) or ret == -1 then
action_popup_opened = false
ImGui.CloseCurrentPopup(ctx)
reaper.PromptForAction(-1, 0, section_id)
got_action = nil
end
ImGui.EndPopup(ctx)
return got_action, action, native, action_text
end
end
function TextPopup(id)
local ret = nil
ImGui.SetNextWindowPos(ctx, center[1], center[2], ImGui.Cond_Appearing, 0.5, 0.5)
ImGui.PushStyleVar(ctx, ImGui.StyleVar_WindowPadding, 10, 10)
ImGui.PushStyleVar(ctx, ImGui.StyleVar_FramePadding, 10, 8)
if ImGui.BeginPopupModal(ctx, id, nil, popup_flags) then
ImGui.PushStyleColor(ctx, ImGui.Col_FrameBg, 0x444444ff)
ImGui.PushStyleVar(ctx, ImGui.StyleVar_Alpha, 0.7)
MoveCursor(3, 16)
ImGui.Text(ctx, "Text")
ImGui.PopStyleVar(ctx)
ImGui.SameLine(ctx)
MoveCursor(6, -6)
if edit_wants_focus then
ImGui.SetKeyboardFocusHere(ctx)
edit_wants_focus = false
end
ImGui.PushItemWidth(ctx, 300)
_, new_text = ImGui.InputText(ctx, "##actionname", new_text)
ImGui.PopItemWidth(ctx)
ImGui.PopStyleColor(ctx)
MoveCursor(0, 6)
local avail_x = ImGui.GetContentRegionAvail(ctx)
if Button("Cancel##edit" .. id, avail_x / 2.1) then
ImGui.CloseCurrentPopup(ctx)
end
ImGui.SameLine(ctx)
local enter_pressed = ImGui.IsKeyPressed(ctx, ImGui.Key_KeypadEnter)
enter_pressed = enter_pressed or ImGui.IsKeyPressed(ctx, ImGui.Key_Enter)
if Button("Ok##edit" .. id, -FLT_MIN) or enter_pressed then
ret = new_text
ImGui.CloseCurrentPopup(ctx)
end
ImGui.EndPopup(ctx)
end
ImGui.PopStyleVar(ctx, 2)
return ret
end
--returns nil if not opened, false if waiting for input, true if got input
function KeyPopup(id, own_index)
local gfx_name = "WaitingForKey"
local popup_name = "Keyboard" .. id
local key, key_text, cmd, shift, alt, ctrl
if key_popup_requested then
key_popup_requested = false
key_popup_opened = true
gfx.init(gfx_name, 0, 0, 0, 0, 0)
key_hwnd = reaper.JS_Window_Find(gfx_name, true)
reaper.JS_Window_SetOpacity(key_hwnd, "ALPHA", 0)
ImGui.OpenPopup(ctx, popup_name)
end
if not key_popup_opened then return nil end
local got_input = false
ImGui.SetNextWindowPos(ctx, center[1], center[2], ImGui.Cond_Appearing, 0.5, 0.5)
if ImGui.BeginPopupModal(ctx, popup_name, nil, popup_flags) then
if reaper.JS_Window_GetFocus() ~= key_hwnd then
gfx.quit()
ImGui.CloseCurrentPopup(ctx)
key_popup_opened = false
end
local getchar = gfx.getchar()
if getchar > 0 then
key = getchar
local cap = gfx.mouse_cap
key_text = ToKeyText(getchar, cap)
cmd = cap & 4 == 4
shift = cap & 8 == 8
alt = cap & 16 == 16
ctrl = cap & 32 == 32
gfx.quit()
ImGui.CloseCurrentPopup(ctx)
key_popup_opened = false
got_input = true
elseif getchar == -1 then
gfx.quit()
ImGui.CloseCurrentPopup(ctx)
key_popup_opened = false
end
local text = "Press key(s)"
local text_size = ImGui.CalcTextSize(ctx, text)
local avail_x = ImGui.GetContentRegionAvail(ctx)
MoveCursor(avail_x / 2 - text_size / 2, 0)
ImGui.Text(ctx, text)
MoveCursor(0, 7)
if Button("Cancel", 120, 20) then
gfx.quit()
ImGui.CloseCurrentPopup(ctx)
key_popup_opened = false
end
ImGui.EndPopup(ctx)
if got_input then
local duplicate = false
local duplicate_name = ""
for j, action in ipairs(files[cur_file_idx].actions) do
-- skip text "actions"
if action.text == nil then
-- don't check existing action when updating its key
if own_index == nil or (own_index ~= nil and own_index ~= j) then
if math.floor(action.key) == math.floor(key) and action.shift == shift and action.ctrl == ctrl
and
action.alt == alt and action.cmd == cmd then
duplicate = true
duplicate_name = action.action_text
end
end
end
end
if duplicate then
reaper.ShowMessageBox("Key is used by " .. duplicate_name .. "\nPlease pick another one",
"Key is already in use"
, 0)
key_popup_requested = true
return false
end
return true, key, key_text, cmd, shift, alt, ctrl
else
return false
end
end
end
function SetDirty(file)
table.insert(dirty, file)
last_dirty_name = file.name
end
function TryUTFChar(int)
return pcall(function() return utf8.char(int) end)
end
function ToUTFChar(int)
local utfOffset = 1962934272; -- 'u' << 24
local OK, char = TryUTFChar(int)
if OK then
return char
end
OK, char = TryUTFChar(int - utfOffset)
if OK then
return char
end
return "?"
end
function ToKeyText(int, cap)
local os = reaper.GetOS()
local is_osx = os == "OSX32" or os == "OSX64" or os == "macOS-arm64"
local cmd_txt = "Cmd "
local ctrl_txt = "Ctrl "
local cmd, ctrl, alt, shift
if not is_osx then
cmd_txt = "Ctrl "
ctrl_txt = "Win "
end
local function mods(ignoreshift)
local ret = ""
if cmd then ret = cmd_txt end
if ctrl then ret = ret .. ctrl_txt end
if alt then ret = ret .. "Alt " end
if not ignoreshift and shift then ret = ret .. "Shift " end
return ret
end
local keys = {
[27] = "Esc",
[32] = "Space",
[8] = "BckSpace",
[9] = "Tab",
[1752132965] = "Home",
[6647396] = "End",
[1885824110] = "PgDwn",
[1885828464] = "PgUp",
[6909555] = "Ins",
[6579564] = "Del",
[13] = "Ret",
[30064] = "Up",
[1685026670] = "Down",
[1818584692] = "Left",
[1919379572] = "Right",
[26161] = "F1",
[26162] = "F2",
[26163] = "F3",
[26164] = "F4",
[26165] = "F5",
[26166] = "F6",
[26167] = "F7",
[26168] = "F8",
[26169] = "F9",
[6697264] = "F10",
[6697265] = "F11",
[6697266] = "F12"
}
if int == 0 then return nil end
cmd = cap & 4 == 4
shift = cap & 8 == 8
alt = cap & 16 == 16
ctrl = cap & 32 == 32
if (cmd or ctrl) and int >= 1 and int <= 26 then
int = int + 96 - 32
return mods(false) .. ToUTFChar(int)
elseif cmd and alt and int >= 257 and int <= 282 then
int = int - 160 - 32
return mods(false) .. ToUTFChar(int)
elseif alt and int >= 321 and int <= 346 then
int = int - 256
return mods(false) .. ToUTFChar(int)
elseif keys[int] ~= nil then
return mods(false) .. keys[int]
elseif int >= 0x41 and int <= 0x5a then
-- Different treatment for characters in the [A-Z] range so "Shift + " doesn't show up
return mods(true) .. ToUTFChar(int)
elseif int >= 33 and int <= 255 then
return mods(false) .. ToUTFChar(int)
else
return mods(false) .. ToUTFChar(int)
end
end
function MoveCursor(x, y)
local ox, oy = ImGui.GetCursorPos(ctx)
ImGui.SetCursorPos(ctx, ox + x, oy + y)
end
function ToBool(str)
return str == "true"
end
function ToCap(shift, cmd, ctrl, alt)
local ret = 0
if cmd then ret = ret + 4 end
if shift then ret = ret + 8 end
if alt then ret = ret + 16 end
if ctrl then ret = ret + 32 end
return ret
end
-- Get function source, from first line to last line
function FunctionSource(fn)
local info = debug.getinfo(fn, 'S')
local line_start = info.linedefined
local line_end = info.lastlinedefined
local path = info.source:sub(2)
local ret = ""
local i = 1
for line in io.lines(path) do
if i >= line_start and i <= line_end then
ret = ret .. line .. "\n"
elseif i > line_end then
break
end
i = i + 1
end
return ret
end
function Parse_V0(file, txt)
local function sectionId(section_str)
if section_str == "MAIN" then
return 1
elseif section_str == "MALT" then
return 2
elseif section_str == "MIDI" then
return 3
elseif section_str == "EVNT" then
return 4
elseif section_str == "MEXP" then
return 5
end
end
local function commandPat(section_str)
if section_str == "MAIN" then
return "reaper%.Main_OnCommand%((%d+),0%)"
elseif section_str == "MALT" then
return "reaper%.Main_OnCommand%((%d+),0%)"
elseif section_str == "MIDI" then
return "reaper%.MIDIEditor_OnCommand%(midi_editor,(%d+)%)"
elseif section_str == "EVNT" then
return "reaper%.MIDIEditor_OnCommand%(midi_editor,(%d+)%)"
elseif section_str == "MEXP" then
return "reaper%.JS_Window_OnCommand%(explorerHWND,(%d+)%)"
end
end
local section_pat = "--SEC:(%w%w%w%w)"
local section_str = string.match(txt, section_pat)
file.section = sectionId(section_str)
local pat = "if char == (%d+) and shift == (%S+) and cmd == (%S+) and ctrl == (%S+) and alt == (%S+) then%s*"
pat = pat .. commandPat(section_str)
pat = pat .. "%s*exit = (%S+)"
local names_pat = 'gfx.drawstr%("%[.-%] (.-)"%)'
local names = {}
for name in string.gmatch(txt, names_pat) do
table.insert(names, name)
end
file.actions = {}
for key_id, shift, cmd, ctrl, alt, action_id, exit in string.gmatch(txt, pat) do
shift = ToBool(shift)
cmd = ToBool(cmd)
ctrl = ToBool(ctrl)
alt = ToBool(alt)
exit = ToBool(exit)
action_id = tonumber(action_id)
local id, native, command_text = ActionInfo(action_id, sections[file.section].id)
local new = {
key = key_id,
action = id,
shift = shift,
cmd = cmd,
ctrl = ctrl,
alt = alt,
exit = exit,
key_text = ToKeyText(tonumber(key_id), ToCap(shift, cmd, ctrl, alt)),