-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
1216 lines (1145 loc) · 45.7 KB
/
init.lua
File metadata and controls
1216 lines (1145 loc) · 45.7 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
--[[
lua event manager -- aquietone
]]
local mq = require 'mq'
require 'ImGui'
local zep = require('Zep')
local events = require('events')
-- for scripts with a check on lem.events being required, since they won't find lem.events if the require used the name events
require('lem.events')
local templates = require('templates.index')
require('write')
local persistence = require('persistence')
local icons = require('mq.icons')
local version = '0.10.2'
local safemode = false
---@type Zep.Editor
local editor = nil
---@type Zep.Buffer
local buffer = nil
-- application state
local state = {
terminate = false,
ui = {
main = {
title = 'Lua Event Manager (v%s)%s###lem',
open_ui = true,
draw_ui = true,
menu_idx = 1,
event_idx = nil,
category_idx = 0,
menu_width = 120,
filter = '',
dirty = false,
},
editor = {
open_ui = false,
draw_ui = false,
action = nil,
event_idx = nil,
event_type = nil,
template = '',
},
},
inputs = {
import = '',
add_event = {name='', category='', enabled=false, pattern='', code='',load={always=false,zone='',class='',characters='',},},
add_category = {name='',parent='',parent_idx=0},
},
}
local function fileExists(path)
local f = io.open(path, "r")
if f ~= nil then io.close(f) return true else return false end
end
if fileExists(mq.luaDir..'/lem.lua') then
os.remove(mq.luaDir..'/lem.lua')
end
local table_flags = bit32.bor(ImGuiTableFlags.Hideable, ImGuiTableFlags.RowBg, ImGuiTableFlags.ScrollY, ImGuiTableFlags.BordersOuter, ImGuiTableFlags.Resizable, ImGuiTableFlags.Sortable)
local actions = {add=1,edit=2,view=3,add_category=4,import=5}
local base_dir = mq.luaDir .. '/lem'
local menu_default_width = 120
local settings, text_events, condition_events, categories, char_settings, filtered_events
local show_code = false
local sortable_events = {}
local first_load = true
local function save_settings()
persistence.store(('%s/settings.lua'):format(base_dir), settings)
--mq.pickle(('%s/settings.lua'):format(base_dir), settings)
end
local function save_character_settings()
persistence.store(('%s/characters/%s.lua'):format(base_dir, mq.TLO.Me.CleanName():lower():gsub('\'s corpse', '')), char_settings)
--mq.pickle(('%s/characters/%s.lua'):format(base_dir, mq.TLO.Me.CleanName():lower():gsub('\'s corpse', '')), char_settings)
end
local function init_settings()
local ok, module = pcall(require, 'settings')
if not ok then
if persistence.file_exists(base_dir..'/settings.lua') then
print('\arLEM: Unable to load settings.lua, exiting!\ax')
return
end
settings = {
text_events = {},
condition_events = {},
categories = {},
settings = {
frequency = 250,
},
}
save_settings()
else
settings = module
end
text_events = settings.text_events or {}
condition_events = settings.condition_events or {}
categories = settings.categories or {}
for i,category in ipairs(categories) do
if type(category) == 'string' then
categories[i] = {name=category, children={}}
end
end
if not settings.settings or not settings.settings.frequency then
settings['settings'] = {frequency = 250, broadcast = 'DanNet'}
end
if not settings.settings.broadcast then settings.settings.broadcast = 'None' end
events.setSettings(settings)
end
local function init_char_settings()
local my_name = mq.TLO.Me.CleanName():lower():gsub('\'s corpse', '')
local ok, module = pcall(require, 'characters.'..my_name)
if not ok then
char_settings = {events={}, conditions={}}
save_character_settings()
else
char_settings = module
end
end
local function reset_add_event_inputs(event_type)
state.inputs.add_event = {name='', category='', enabled=false, pattern='', singlecommand=false, command='', load={always=false,zone='',class='',characters='',},}
if event_type == events.types.text then
buffer:SetText(templates.text_base)
elseif event_type == events.types.cond then
buffer:SetText(templates.condition_base)
end
show_code = false
end
local function set_add_event_inputs(event)
state.inputs.add_event = {
name=event.name,
category=event.category,
enabled=char_settings[state.ui.editor.event_type][event.name],
pattern=event.pattern,
singlecommand=event.singlecommand,
command=event.command,
code=event.code,
load=event.load,
}
if event.load then
state.inputs.add_event.load = {
always=event.load.always,
characters=event.load.characters,
class=event.load.class,
zone=event.load.zone,
}
else
state.inputs.add_event.load = {
always=false,
characters='',
class='',
zone=''
}
end
show_code = false
end
local function set_editor_state(open, action, event_type, event_idx)
state.ui.editor.open_ui = open
state.ui.editor.action = action
state.ui.editor.event_idx = event_idx
state.ui.editor.event_type = event_type
show_code = false
end
local function get_event_list(event_type)
if event_type == events.types.text then
return text_events
else
return condition_events
end
end
local function toggle_event(event, event_type)
char_settings[event_type][event.name] = not char_settings[event_type][event.name]
save_character_settings()
end
local function save_event()
local event_type = state.ui.editor.event_type
local add_event = state.inputs.add_event
if event_type == events.types.text and add_event.pattern:len() == 0 then return end
local event_list = get_event_list(event_type)
local original_event = event_list[add_event.name]
-- per character enabled flag currently in use instead of dynamic load options
if original_event and not events.changed(original_event, add_event) and not buffer.dirty then
-- code and pattern did not change
if add_event.enabled ~= char_settings[event_type][add_event.name] then
-- just enabling or disabling the event
toggle_event(original_event, event_type)
end
else
--if original_event and events.changed(original_event, add_event) then
local new_event = {name=add_event.name,category=add_event.category,}
new_event.load = {always=add_event.load.always, characters=add_event.load.characters, class=add_event.load.class, zone=add_event.load.zone,}
if event_type == events.types.text then
new_event.pattern = add_event.pattern
new_event.singlecommand = add_event.singlecommand
new_event.command = add_event.command
end
if state.ui.editor.action == actions.edit or (state.ui.editor.action == actions.import and event_list[add_event.name] ~= nil) then
-- replacing event, disable then unload it first before it is saved
char_settings[event_type][add_event.name] = nil
if event_type == events.types.text then mq.unevent(add_event.name) end
events.unload_package(add_event.name, event_type)
end
if not buffer.filePath:find(add_event.name) then
local filename = events.filename(add_event.name, event_type)
local tmpTxt = buffer:GetText()
buffer:Load(filename)
buffer:SetText(tmpTxt)
end
buffer:Save()
event_list[add_event.name] = new_event
save_settings()
char_settings[event_type][add_event.name] = add_event.enabled
save_character_settings()
first_load = true -- so event list re-sorts with new event included
end
state.ui.editor.open_ui = false
end
local function drawEditor()
local footerHeight = 0
local contentSizeX, contentSizeY = ImGui.GetContentRegionAvail()
contentSizeY = contentSizeY - footerHeight
editor:Render(ImVec2(contentSizeX, contentSizeY))
end
local function draw_event_editor_general(add_event)
add_event.name,_ = ImGui.InputText('Event Name', add_event.name)
if ImGui.BeginCombo('Category', add_event.category or '') then
for _,j in pairs(categories) do
if ImGui.Selectable(j.name, j.name == add_event.category) then
add_event.category = j.name
end
for _,k in pairs(j.children) do
if ImGui.Selectable('- '..k.name, k.name == add_event.category) then
add_event.category = k.name
end
end
end
ImGui.EndCombo()
end
-- per character enabled flag currently in use instead of dynamic load options
add_event.enabled,_ = ImGui.Checkbox('Event Enabled', add_event.enabled)
if state.ui.editor.event_type == events.types.text then
add_event.pattern,_ = ImGui.InputText('Event Pattern', add_event.pattern)
add_event.singlecommand = ImGui.Checkbox('Single Command Action', add_event.singlecommand)
if add_event.singlecommand then
local changed = false
add_event.command,changed = ImGui.InputText('Command', add_event.command)
if changed then
buffer:SetText(templates.command_base:format(add_event.command))
end
end
end
if ImGui.BeginCombo('Code Templates', state.ui.editor.template or '') then
for _,template in ipairs(templates.files) do
if ImGui.Selectable(template, state.ui.editor.template == template) then
state.ui.editor.template = template
end
end
ImGui.EndCombo()
end
local buttons_active = true
if state.ui.editor.template == '' then
ImGui.PushStyleColor(ImGuiCol.Button, .3, 0, 0,1)
ImGui.PushStyleColor(ImGuiCol.ButtonActive, .3, 0, 0,1)
ImGui.PushStyleColor(ImGuiCol.ButtonHovered, .3, 0, 0,1)
buttons_active = false
end
if ImGui.Button('Load Template') and state.ui.editor.template ~= '' then
buffer:SetText(events.read_event_file(templates.filename(state.ui.editor.template)))
end
if not buttons_active then
ImGui.PopStyleColor(3)
end
ImGui.SameLine()
ImGui.TextColored(1, 0, 0, 1, 'This will OVERWRITE the existing event code')
if not add_event.singlecommand then
ImGui.NewLine()
if show_code then
ImGui.PushStyleColor(ImGuiCol.Text, ImVec4(0,1,0,1))
ImGui.Text(icons.FA_TOGGLE_ON)
else
ImGui.PushStyleColor(ImGuiCol.Text, ImVec4(1,0,0,1))
ImGui.Text(icons.FA_TOGGLE_OFF)
end
if ImGui.IsItemHovered() and ImGui.IsMouseReleased(ImGuiMouseButton.Left) then
show_code = not show_code
end
ImGui.PopStyleColor()
ImGui.SameLine()
ImGui.Text('Show Code')
if show_code then
local x, y = ImGui.GetContentRegionAvail()
drawEditor()
end
end
end
local function draw_event_editor_load(add_event)
ImGui.TextColored(1, 1, 0, 1, '>>> UNDER CONSTRUCTION - NOT IN USE <<<')
add_event.load.always = ImGui.Checkbox('Always', add_event.load.always)
add_event.load.zone,_ = ImGui.InputText('Zone Shortname', add_event.load.zone)
add_event.load.class,_ = ImGui.InputText('Classes', add_event.load.class)
add_event.load.character,_ = ImGui.InputText('Characters', add_event.load.characters)
end
local function draw_event_editor()
if not state.ui.editor.open_ui then return end
local title = 'Event Editor###lemeditor'
if state.ui.editor.action == actions.add then
title = 'Add Event###lemeditor'
elseif state.ui.editor.action == actions.import then
title = 'Import Event###lemeditor'
end
state.ui.editor.open_ui, state.ui.editor.draw_ui = ImGui.Begin(title, state.ui.editor.open_ui)
if state.ui.editor.draw_ui then
if ImGui.Button('Save') then
save_event()
end
local add_event = state.inputs.add_event
local event_type = state.ui.editor.event_type
local event_list = get_event_list(event_type)
if state.ui.editor.action == actions.import and event_list[add_event.name] ~= nil then
ImGui.SameLine()
ImGui.TextColored(1, 0, 0, 1, '(Overwrite existing)')
end
if ImGui.BeginTabBar('EventTabs') then
if ImGui.BeginTabItem('General') then
draw_event_editor_general(add_event)
ImGui.EndTabItem()
end
--[[if ImGui.BeginTabItem('Load') then
draw_event_editor_load(add_event)
ImGui.EndTabItem()
end]]
ImGui.EndTabBar()
end
end
ImGui.End()
end
local function draw_import_window()
if ImGui.Button('Import Event') then
local imported_event = events.import(state.inputs.import, categories)
if imported_event then
set_editor_state(true, actions.import, imported_event.type, nil)
set_add_event_inputs(imported_event)
buffer:Load(events.filename(imported_event.name, imported_event.type))
buffer:SetText(imported_event.code)
state.inputs.import = ''
end
end
ImGui.SameLine()
if ImGui.Button('Paste from clipboard') then
state.inputs.import = ImGui.GetClipboardText()
end
state.inputs.import = ImGui.InputText('##importeventtext', state.inputs.import)
local width = ImGui.GetContentRegionAvail()
ImGui.PushTextWrapPos(width-15)
ImGui.Text('Paste base64 encoded string data (it will look like a very long, random string of letters and numbers)')
ImGui.PopTextWrapPos()
end
local function draw_event_viewer_general(event)
local width = ImGui.GetContentRegionAvail()
ImGui.PushTextWrapPos(width-15)
if ImGui.Button('Edit Event') then
buffer:Load(events.filename(event.name, state.ui.editor.event_type))
state.ui.editor.action = actions.edit
buffer.readonly = false
set_add_event_inputs(event)
end
ImGui.SameLine()
if ImGui.Button('Edit In VS Code') then
os.execute('start "" "'..events.filename(event.name, state.ui.editor.event_type)..'"')
end
ImGui.SameLine()
if ImGui.Button('Export Event') then
ImGui.SetClipboardText(events.export(event, state.ui.editor.event_type))
end
ImGui.SameLine()
if ImGui.Button('Reload Source') then
buffer:Load(events.filename(event.name, state.ui.editor.event_type))
events.reload(event, state.ui.editor.event_type)
end
if event.failed then
ImGui.TextColored(1, 0, 0, 1, 'ERROR: Event failed to load!')
end
ImGui.TextColored(1, 1, 0, 1, 'Name: ')
ImGui.SameLine()
ImGui.SetCursorPosX(100)
-- per character enabled flag currently in use instead of dynamic load options
if char_settings[state.ui.editor.event_type][event.name] then
--if event.loaded then
ImGui.TextColored(0, 1, 0, 1, event.name)
else
ImGui.TextColored(1, 0, 0, 1, event.name .. ' (Disabled)')
end
ImGui.TextColored(1, 1, 0, 1, 'Category: ')
ImGui.SameLine()
ImGui.SetCursorPosX(100)
ImGui.Text(event.category or '')
if state.ui.editor.event_type == events.types.text then
ImGui.TextColored(1, 1, 0, 1, 'Pattern: ')
ImGui.SameLine()
ImGui.SetCursorPosX(100)
ImGui.TextColored(1, 0, 1, 1, '%s', event.pattern)
if event.singlecommand then
ImGui.TextColored(1, 1, 0, 1, 'Command: ')
ImGui.SameLine()
ImGui.SetCursorPosX(100)
ImGui.TextColored(1, 0, 1, 1, '%s', event.command or '')
end
end
if show_code then
ImGui.PushStyleColor(ImGuiCol.Text, ImVec4(0,1,0,1))
ImGui.Text(icons.FA_TOGGLE_ON)
else
ImGui.PushStyleColor(ImGuiCol.Text, ImVec4(1,0,0,1))
ImGui.Text(icons.FA_TOGGLE_OFF)
end
if ImGui.IsItemHovered() and ImGui.IsMouseReleased(ImGuiMouseButton.Left) then
show_code = not show_code
end
ImGui.PopStyleColor()
ImGui.SameLine()
ImGui.Text('Show Code')
if show_code then
drawEditor()
end
end
local function draw_event_viewer_load(event)
ImGui.TextColored(1, 1, 0, 1, '>>> UNDER CONSTRUCTION - NOT IN USE <<<')
ImGui.TextColored(1, 1, 0, 1, 'Always: ')
ImGui.SameLine()
ImGui.SetCursorPosX(125)
ImGui.Text(('%s'):format(event.load.always))
ImGui.TextColored(1, 1, 0, 1, 'Zone Shortname: ')
ImGui.SameLine()
ImGui.SetCursorPosX(125)
ImGui.Text(event.load.zone)
ImGui.TextColored(1, 1, 0, 1, 'Classes: ')
ImGui.SameLine()
ImGui.SetCursorPosX(125)
ImGui.Text(event.load.class)
ImGui.TextColored(1, 1, 0, 1, 'Characters: ')
ImGui.SameLine()
ImGui.SetCursorPosX(125)
ImGui.Text(event.load.characters)
end
local function draw_event_viewer()
if not state.ui.editor.open_ui then return end
state.ui.editor.open_ui, state.ui.editor.draw_ui = ImGui.Begin('Event Viewer###lemeditor', state.ui.editor.open_ui)
local event_list = get_event_list(state.ui.editor.event_type)
local event = event_list[state.ui.editor.event_idx]
if state.ui.editor.draw_ui and event then
if ImGui.BeginTabBar('EventViewer') then
if ImGui.BeginTabItem('General') then
draw_event_viewer_general(event)
ImGui.EndTabItem()
end
if ImGui.BeginTabItem('Load') then
draw_event_viewer_load(event)
ImGui.EndTabItem()
end
ImGui.EndTabBar()
end
end
ImGui.End()
end
local function draw_event_control_buttons(event_type)
local event_list = get_event_list(event_type)
if ImGui.Button('Add Event...') then
set_editor_state(true, actions.add, event_type, nil)
reset_add_event_inputs(event_type)
buffer.readonly = false
buffer.syntax = 'lua'
end
local buttons_active = true
if not state.ui.main.event_idx then
ImGui.PushStyleColor(ImGuiCol.Button, .3, 0, 0,1)
ImGui.PushStyleColor(ImGuiCol.ButtonActive, .3, 0, 0,1)
ImGui.PushStyleColor(ImGuiCol.ButtonHovered, .3, 0, 0,1)
buttons_active = false
end
local event = event_list[state.ui.main.event_idx]
ImGui.SameLine()
if ImGui.Button('View Event') and state.ui.main.event_idx and event then
set_editor_state(true, actions.view, event_type, state.ui.main.event_idx)
buffer.readonly = true
buffer:Load(events.filename(event.name, state.ui.editor.event_type))
end
ImGui.SameLine()
if ImGui.Button('Edit Event') and state.ui.main.event_idx and event then
set_editor_state(true, actions.edit, event_type, state.ui.main.event_idx)
buffer.readonly = false
buffer:Load(events.filename(event.name, event_type))
set_add_event_inputs(event)
end
ImGui.SameLine()
if ImGui.Button('Remove Event') and state.ui.main.event_idx and event then
event_list[event.name] = nil
if event_type == events.types.text and char_settings[event_type][event.name] then
mq.unevent(event.name)
end
char_settings[event_type][event.name] = nil
events.unload_package(event.name, event_type)
state.ui.main.event_idx = nil
events.delete_event_file(events.filename(event.name, event_type))
save_settings()
save_character_settings()
set_editor_state(false, nil, nil, nil)
state.ui.main.dirty = true
end
if not buttons_active then
ImGui.PopStyleColor(3)
end
end
local function draw_event_table_context_menu(event, event_type)
if ImGui.BeginPopupContextItem() then
if ImGui.MenuItem('Export') then
ImGui.SetClipboardText(events.export(event, event_type))
end
if ImGui.MenuItem('Edit in VS Code') then
os.execute('start "" "'..events.filename(event.name, event_type)..'"')
end
if ImGui.MenuItem('Reload Source') then
buffer:Load(events.filename(event.name, event_type))
events.reload(event, event_type)
end
local event_enabled = char_settings[event_type][event.name] or false
local enable_prefix = event_enabled and 'Disable' or 'Enable'
local action = event_enabled and '0' or '1'
local type_singular = event_type == 'events' and 'event' or 'cond'
if ImGui.MenuItem(enable_prefix..' For All') then
mq.cmdf('/dga /lem %s "%s" %s', type_singular, event.name, action)
end
if ImGui.MenuItem(enable_prefix..' For Raid') then
mq.cmdf('/dgra /lem %s "%s" %s', type_singular, event.name, action)
end
if ImGui.MenuItem(enable_prefix..' For Group') then
mq.cmdf('/dgga /lem %s "%s" %s', type_singular, event.name, action)
end
if ImGui.MenuItem('DEBUG: Run event script') then
mq.cmdf('/lua run "lem/%s/%s"', event_type, event.name)
end
ImGui.EndPopup()
end
end
local function draw_event_table_row(event, event_type)
-- per character enabled flag currently in use instead of dynamic load options
local enabled = ImGui.Checkbox('##'..event.name, char_settings[event_type][event.name] or false)
if enabled ~= (char_settings[event_type][event.name] or false) then
toggle_event(event, event_type)
end
ImGui.TableNextColumn()
local row_label = event.name
-- per character enabled flag currently in use instead of dynamic load options
if char_settings[event_type][event.name] and not event.failed then
--if event.loaded then
ImGui.PushStyleColor(ImGuiCol.Text, 0, 1, 0, 1)
else
ImGui.PushStyleColor(ImGuiCol.Text, 1, 0, 0, 1)
if event.failed then
row_label = row_label .. ' (Failed to load)'
end
end
if ImGui.Selectable(row_label, state.ui.main.event_idx == event.name, ImGuiSelectableFlags.SpanAllColumns) then
if state.ui.main.event_idx ~= event.name then
state.ui.main.event_idx = event.name
end
end
if ImGui.IsItemHovered() and ImGui.IsMouseDoubleClicked(0) then
set_editor_state(true, actions.view, event_type, event.name)
buffer.readonly = true
buffer:Load(events.filename(event.name, state.ui.editor.event_type))
end
ImGui.PopStyleColor()
draw_event_table_context_menu(event, event_type)
end
local ColumnID_OnOff = 1
local ColumnID_Name = 2
local current_sort_specs = nil
local sort_event_type = nil
local function CompareWithSortSpecs(a, b)
for n = 1, current_sort_specs.SpecsCount, 1 do
-- Here we identify columns using the ColumnUserID value that we ourselves passed to TableSetupColumn()
-- We could also choose to identify columns based on their index (sort_spec.ColumnIndex), which is simpler!
local sort_spec = current_sort_specs:Specs(n)
local delta = 0
local sortA = a
local sortB = b
if sort_spec.ColumnUserID == ColumnID_OnOff then
sortA = char_settings[sort_event_type][a.name] or false
sortB = char_settings[sort_event_type][b.name] or false
if sort_spec.SortDirection == ImGuiSortDirection.Ascending then
--return sortA == true and sortB == false or a.name < b.name
return sortA and a.name < b.name
else
--return sortB == true and sortA == false or b.name < a.name
return sortB and b.name < a.name
end
elseif sort_spec.ColumnUserID == ColumnID_Name then
sortA = a.name
sortB = b.name
end
if sortA < sortB then
delta = -1
elseif sortB < sortA then
delta = 1
else
delta = 0
end
if delta ~= 0 then
if sort_spec.SortDirection == ImGuiSortDirection.Ascending then
return delta < 0
end
return delta > 0
end
end
-- Always return a way to differentiate items.
-- Your own compare function may want to avoid fallback on implicit sort specs e.g. a Name compare if it wasn't already part of the sort specs.
return a.name < b.name
end
local function draw_events_table(event_type)
local event_list = get_event_list(event_type)
local new_filter,_ = ImGui.InputTextWithHint('##tablefilter', 'Filter...', state.ui.main.filter, 0)
if new_filter ~= state.ui.main.filter or state.ui.main.dirty then
state.ui.main.filter = new_filter:lower()
filtered_events = {}
sortable_events = {}
first_load = true
for event_name,event in pairs(event_list) do
if event_name:lower():find(state.ui.main.filter) then
filtered_events[event_name] = event
end
end
end
if ImGui.BeginTable('EventTable', 2, table_flags, 0, 0, 0.0) then
local column_label = 'Event Name'
ImGui.TableSetupColumn('On/Off', ImGuiTableColumnFlags.DefaultSort, 1, ColumnID_OnOff)
ImGui.TableSetupColumn(column_label, ImGuiTableColumnFlags.DefaultSort, 3, ColumnID_Name)
ImGui.TableSetupScrollFreeze(0, 1) -- Make row always visible
ImGui.TableHeadersRow()
local sort_specs = ImGui.TableGetSortSpecs()
if sort_specs then
if sort_specs.SpecsDirty or first_load then
first_load = false
sortable_events = {}
if state.ui.main.filter ~= '' then
for _,event in pairs(filtered_events) do
table.insert(sortable_events, event)
end
else
for _,event in pairs(event_list) do
table.insert(sortable_events, event)
end
end
current_sort_specs = sort_specs
sort_event_type = event_type
table.sort(sortable_events, CompareWithSortSpecs)
sort_event_type = nil
current_sort_specs = nil
sort_specs.SpecsDirty = false
end
end
if state.ui.main.filter ~= '' then
for _,event in pairs(sortable_events) do
ImGui.TableNextRow()
ImGui.TableNextColumn()
draw_event_table_row(event, event_type)
end
else
for _,category in ipairs(categories) do
ImGui.TableNextRow()
ImGui.TableNextColumn()
local open = ImGui.TreeNodeEx(category.name, ImGuiTreeNodeFlags.SpanFullWidth)
ImGui.TableNextColumn()
if open then
for _,subcategory in ipairs(category.children) do
ImGui.TableNextRow()
ImGui.TableNextColumn()
local subopen = ImGui.TreeNodeEx(subcategory.name, ImGuiTreeNodeFlags.SpanFullWidth)
ImGui.TableNextColumn()
if subopen then
for _,event in pairs(sortable_events) do
if event.category == subcategory.name then
ImGui.TableNextRow()
ImGui.TableNextColumn()
draw_event_table_row(event, event_type)
end
end
ImGui.TreePop()
end
end
for _,event in pairs(sortable_events) do
if event.category == category.name then
ImGui.TableNextRow()
ImGui.TableNextColumn()
draw_event_table_row(event, event_type)
end
end
ImGui.TreePop()
end
end
for _,event in pairs(sortable_events) do
if not event.category or event.category == '' then
ImGui.TableNextRow()
ImGui.TableNextColumn()
draw_event_table_row(event, event_type)
end
end
end
ImGui.EndTable()
end
end
local function draw_events_section(event_type)
draw_event_control_buttons(event_type)
draw_events_table(event_type)
end
local function draw_settings_section()
ImGui.PushStyleColor(ImGuiCol.Text, 1, 1, 0, 1)
ImGui.SetNextItemWidth(100)
settings.settings.frequency = ImGui.InputInt('Frequency', settings.settings.frequency)
ImGui.SetNextItemWidth(100)
if ImGui.BeginCombo('Broadcast Event Enable/Disable', settings.settings.broadcast or 'None') then
for _,channel in ipairs({'None', 'DanNet', 'EQBC'}) do
if ImGui.Selectable(channel, settings.settings.broadcast == channel) then
settings.settings.broadcast = channel
end
end
ImGui.EndCombo()
end
ImGui.PopStyleColor()
if ImGui.Button('Save') then
save_settings()
end
ImGui.SetNextItemWidth(100)
if ImGui.BeginCombo('Log Level (Not Saved)', Write.loglevel) then
for _,loglevel in ipairs({'help', 'fatal', 'error', 'warn', 'info', 'debug', 'trace'}) do
if ImGui.Selectable(loglevel, Write.loglevel == loglevel) then
Write.loglevel = loglevel
end
end
ImGui.EndCombo()
end
end
local function draw_reload_section()
if ImGui.Button('Reload Settings') then
mq.cmd('/timed 10 /lua run lem')
state.terminate = true
end
ImGui.Text('Reload currently just restarts the script.')
end
local function save_category()
if state.inputs.add_category.name:len() > 0 then
if state.inputs.add_category.parent:len() > 0 then
table.insert(settings.categories[state.inputs.add_category.parent_idx].children, {name=state.inputs.add_category.name, children={}})
else
table.insert(settings.categories, {name=state.inputs.add_category.name, children={}})
end
save_settings()
state.ui.editor.open_ui = false
state.inputs.add_category.name = ''
state.inputs.add_category.parent = ''
state.inputs.add_category.parent_idx = 0
end
end
local function draw_category_editor()
state.ui.editor.open_ui, state.ui.editor.draw_ui = ImGui.Begin('Add Category###lemeditor', state.ui.editor.open_ui)
if state.ui.editor.draw_ui then
if ImGui.Button('Save') then
save_category()
end
state.inputs.add_category.name,_ = ImGui.InputText('Category Name', state.inputs.add_category.name)
if ImGui.BeginCombo('Parent Category', state.inputs.add_category.parent or '') then
if ImGui.Selectable('None', state.inputs.add_category.parent == '') then
state.inputs.add_category.parent = ''
state.inputs.add_category.parent_idx = 0
end
for parentIdx,category in ipairs(categories) do
if ImGui.Selectable(category.name, state.inputs.add_category.parent == category.name) then
state.inputs.add_category.parent = category.name
state.inputs.add_category.parent_idx = parentIdx
end
end
ImGui.EndCombo()
end
end
ImGui.End()
end
local function draw_categories_control_buttons()
if ImGui.Button('Add Category...') then
state.ui.editor.open_ui = true
state.ui.editor.action = actions.add_catogory
end
if state.ui.main.category_name or state.ui.main.subcategory_name then
ImGui.SameLine()
if ImGui.Button('Remove Category') then
local categoryName = state.ui.main.subcategory_name or state.ui.main.category_name
for _,event in pairs(text_events) do
if event.category == categoryName then
printf('\arCannot delete category \ay%s\ax, text event \ay%s\ax belongs to it.\ax', categoryName, event.name)
return
end
end
for _,event in pairs(condition_events) do
if event.category == categoryName then
printf('\arCannot delete category \ay%s\ax, condition event \ay%s\ax belongs to it.\ax', categoryName, event.name)
return
end
end
if not state.ui.main.subcategory_name and #categories[state.ui.main.category_idx].children > 0 then
printf('\arCannot delete category \ay%s\ax as it has sub-categories.\ax', categoryName)
return
end
if state.ui.main.subcategory_name then
table.remove(categories[state.ui.main.category_idx].children, state.ui.main.category_subidx)
else
table.remove(categories, state.ui.main.category_idx)
end
state.ui.main.category_idx = 0
state.ui.main.category_subidx = 0
state.ui.main.category_name = nil
state.ui.main.subcategory_name = nil
save_settings()
end
end
end
local function draw_categories_table()
if ImGui.BeginTable('CategoryTable', 1, table_flags, 0, 0, 0.0) then
ImGui.TableSetupColumn('Category', ImGuiTableColumnFlags.NoSort, -1, 1)
ImGui.TableSetupScrollFreeze(0, 1) -- Make row always visible
ImGui.TableHeadersRow()
local clipper = ImGuiListClipper.new()
clipper:Begin(#categories)
while clipper:Step() do
for row_n = clipper.DisplayStart, clipper.DisplayEnd - 1, 1 do
local category = categories[row_n + 1]
ImGui.TableNextRow()
ImGui.TableNextColumn()
if #category.children > 0 then
local open = ImGui.TreeNode(category.name)
if open then
for subindex,subcategory in ipairs(category.children) do
ImGui.TableNextRow()
ImGui.TableNextColumn()
if ImGui.Selectable(subcategory.name, state.ui.main.category_subidx == subindex and state.ui.main.category_idx == row_n + 1) then
if state.ui.main.category_subidx ~= subindex or state.ui.main.category_idx ~= row_n + 1 then
state.ui.main.category_idx = row_n + 1
state.ui.main.category_subidx = subindex
state.ui.main.category_name = category.name
state.ui.main.subcategory_name = subcategory.name
end
end
end
ImGui.TreePop()
end
else
if ImGui.Selectable(category.name, state.ui.main.category_idx == row_n + 1) then
if state.ui.main.category_idx ~= row_n + 1 then
state.ui.main.category_idx = row_n + 1
state.ui.main.category_subidx = 0
state.ui.main.category_name = category.name
state.ui.main.subcategory_name = nil
end
end
end
end
end
ImGui.EndTable()
end
end
local function draw_categories_section()
draw_categories_control_buttons()
draw_categories_table()
end
local sections = {
{
name='Text Events',
handler=draw_events_section,
arg=events.types.text,
},
{
name='Condition Events',
handler=draw_events_section,
arg=events.types.cond,
},
{
name='Categories',
handler=draw_categories_section,
},
{
name='Settings',
handler=draw_settings_section,
},
{
name='Reload',
handler=draw_reload_section,
},
{
name='Import',
handler=draw_import_window,
}
}
local function draw_selected_section()
local x,y = ImGui.GetContentRegionAvail()
if ImGui.BeginChild("right", x, y-1, ImGuiChildFlags.Border) then
if state.ui.main.menu_idx > 0 then
sections[state.ui.main.menu_idx].handler(sections[state.ui.main.menu_idx].arg)
end
end
ImGui.EndChild()
end
local function draw_menu()
local _,y = ImGui.GetContentRegionAvail()
if ImGui.BeginChild("left", state.ui.main.menu_width, y-1, ImGuiChildFlags.Border) then
if ImGui.BeginTable('MenuTable', 1, table_flags, 0, 0, 0.0) then
for idx,section in ipairs(sections) do
ImGui.TableNextRow()
ImGui.TableNextColumn()
if ImGui.Selectable(section.name, state.ui.main.menu_idx == idx) then
if state.ui.main.menu_idx ~= idx then
state.ui.main.menu_idx = idx
state.ui.main.event_idx = nil
state.ui.main.filter = ''
first_load = true
end
end
end
ImGui.EndTable()
end
end
ImGui.EndChild()
end
local function draw_splitter(thickness, size0, min_size0)
local x,y = ImGui.GetCursorPos()
local delta = 0
ImGui.SetCursorPosX(x + size0)
ImGui.PushStyleColor(ImGuiCol.Button, 0, 0, 0, 0)
ImGui.PushStyleColor(ImGuiCol.ButtonActive, 0, 0, 0, 0)
ImGui.PushStyleColor(ImGuiCol.ButtonHovered, 0.6, 0.6, 0.6, 0.1)
ImGui.Button('##splitter', thickness, -1)
ImGui.PopStyleColor(3)
ImGui.SetNextItemAllowOverlap()
if ImGui.IsItemActive() then
delta,_ = ImGui.GetMouseDragDelta()
if delta < min_size0 - size0 then
delta = min_size0 - size0
end
if delta > 200 - size0 then
delta = 200 - size0
end
size0 = size0 + delta
state.ui.main.menu_width = size0
else
menu_default_width = state.ui.main.menu_width