-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomOptions.lua
More file actions
1578 lines (1482 loc) · 53.6 KB
/
Copy pathCustomOptions.lua
File metadata and controls
1578 lines (1482 loc) · 53.6 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
-- WeakestAuras -- the custom-option schema and config validator for imported auras.
-- Mirrors WeakestAurasOptions/AuthorOptions.lua and WeakAuras.lua's validator.
if WeakestAuras.disabled then return end
local WA = WeakestAuras
-- Forward declarations. The field builders near the top of this file close over
-- the read/write helpers defined below them, and in 5.0 a name that is not yet a
-- visible local compiles as a *global* read -- which resolves to nil at call
-- time, with the error swallowed. Every user-mode set went through
-- setOptionValue that way and silently did nothing.
local optionValue
local setOptionValue, setOptionValueAt
WA.author_option_classes = {
toggle = "simple",
input = "simple",
number = "simple",
range = "simple",
color = "simple",
select = "simple",
media = "simple",
multiselect = "simple",
description = "noninteractive",
space = "noninteractive",
header = "noninteractive",
group = "group",
}
WA.author_option_types = {
toggle = "Toggle",
input = "String",
number = "Number",
range = "Slider",
description = "Description",
color = "Color",
select = "Dropdown Menu",
space = "Space",
multiselect = "Toggle List",
media = "Media",
header = "Separator",
group = "Option Group",
}
WA.author_option_fields = {
common = {
type = true,
name = true,
useDesc = true,
desc = true,
key = true,
width = true,
},
number = {
min = 0,
max = 1,
step = .05,
default = 0,
},
range = {
min = 0,
max = 1,
step = .05,
default = 0,
},
input = {
default = "",
useLength = false,
length = 10,
multiline = false,
},
toggle = {
default = false,
},
description = {
text = "",
fontSize = "medium",
},
color = {
default = {1, 1, 1, 1},
},
select = {
values = {"val1"},
default = 1,
},
space = {
variableWidth = true,
useHeight = false,
height = 1,
},
media = {
mediaType = "sound",
media = "Interface\\AddOns\\WeakAuras\\Media\\Sounds\\AirHorn.ogg",
},
multiselect = {
default = {true},
values = {"val1"},
},
header = {
useName = false,
text = "",
},
group = {
groupType = "simple",
useCollapse = true,
collapse = false,
limitType = "none",
size = 10,
nameSource = 0,
hideReorder = true,
entryNames = nil,
subOptions = {},
noMerge = false,
},
}
WA.author_option_media_defaults = {
sound = "Interface\\AddOns\\WeakestAuras\\Media\\Sounds\\AirHorn.ogg",
font = "Friz Quadrata TT",
border = "1 Pixel",
background = "None",
statusbar = "Blizzard",
}
local function clearTable(t)
for key in pairs(t) do t[key] = nil end
end
local function copyDefault(option)
if type(option.default) == "table" then return WA.DeepCopy(option.default) end
return option.default
end
local function valuesEqual(first, second)
if first == second then return true end
if type(first) ~= "table" or type(second) ~= "table" then return false end
for key, value in pairs(first) do
if not valuesEqual(value, second[key]) then return false end
end
for key, value in pairs(second) do
if not valuesEqual(value, first[key]) then return false end
end
return true
end
local significantFieldsForMerge = {
type = true, name = true, key = true, groupType = true,
limitType = true, size = true, mediaType = true,
}
local specialCasesForMerge = { nameSource = true }
local function round(value)
return math.floor(value + 0.5)
end
local function customOptionIsValid(option)
if type(option) ~= "table" or not option.type then
return false
elseif WA.author_option_classes[option.type] == "simple" then
if not option.key or not option.name then return false end
elseif WA.author_option_classes[option.type] == "group" then
if not option.key or not option.name or not option.subOptions then return false end
end
return true
end
local function reportCorruptOption(data, index)
if DEFAULT_CHAT_FRAME and DEFAULT_CHAT_FRAME.AddMessage then
DEFAULT_CHAT_FRAME:AddMessage(
"|cffff0000WeakestAuras|r " .. tostring(data.id) .. " Custom Option #" .. index
.. " in " .. tostring(data.id) .. " has been detected as corrupt, and has been deleted.",
1, 0.3, 0.3)
end
end
local function validateUserConfig(data, options, config)
local authorOptionKeys, corruptOptions = {}, {}
options = options or {}
config = config or {}
for index = 1, table.getn(options) do
local option = options[index]
if not customOptionIsValid(option) or authorOptionKeys[option.key] then
reportCorruptOption(data, index)
corruptOptions[index] = true
else
local optionClass = WA.author_option_classes[option.type]
if option.key then authorOptionKeys[option.key] = index end
if optionClass == "simple" then
if config[option.key] == nil then
config[option.key] = copyDefault(option)
end
elseif optionClass == "group" then
local subOptions = option.subOptions
if type(config[option.key]) ~= "table" then config[option.key] = {} end
local subConfig = config[option.key]
if option.groupType == "array" then
local invalidArray = false
for key, value in pairs(subConfig) do
if type(key) ~= "number" or type(value) ~= "table" then
invalidArray = true
break
end
end
if invalidArray then clearTable(subConfig) end
if option.limitType == "fixed" then
for i = table.getn(subConfig) + 1, option.size do subConfig[i] = {} end
end
if option.limitType ~= "none" then
for i = option.size + 1, table.getn(subConfig) do subConfig[i] = nil end
end
for _, toValidate in pairs(subConfig) do
validateUserConfig(data, subOptions, toValidate)
end
else
local firstKey = next(subConfig)
if type(firstKey) ~= "string" then clearTable(subConfig) end
validateUserConfig(data, subOptions, subConfig)
end
end
end
end
for key, value in pairs(config) do
if not authorOptionKeys[key] then
config[key] = nil
else
local option = options[authorOptionKeys[key]]
local optionClass = WA.author_option_classes[option.type]
if optionClass and optionClass ~= "group" then
if option.type == "media" then
if type(value) ~= "string" and (type(value) ~= "number" or option.mediaType ~= "sound") then
config[key] = copyDefault(option)
end
elseif type(value) ~= type(option.default) then
config[key] = copyDefault(option)
elseif option.type == "input" and option.useLength then
config[key] = string.sub(config[key], 1, option.length)
elseif option.type == "number" or option.type == "range" then
if (option.max and option.max < value) or (option.min and option.min > value) then
config[key] = copyDefault(option)
elseif option.type == "number" and option.step then
local min = option.min or 0
config[key] = option.step * round((value - min) / option.step) + min
end
elseif option.type == "select" then
if value < 1 or value > table.getn(option.values) then config[key] = copyDefault(option) end
elseif option.type == "multiselect" then
local multiselect = config[key]
if type(multiselect) ~= "table" then
config[key] = {}
multiselect = config[key]
end
for i = 1, table.getn(multiselect) do
if option.default[i] ~= nil then
if type(multiselect[i]) ~= "boolean" then multiselect[i] = option.default[i] end
else
multiselect[i] = nil
end
end
if type(option.default) ~= "table" then option.default = {} end
for i = 1, table.getn(option.default) do
if type(multiselect[i]) ~= "boolean" then multiselect[i] = option.default[i] end
end
elseif option.type == "color" then
if type(config[key]) ~= "table" then config[key] = {} end
for i = 1, 4 do
local component = config[key][i]
if type(component) ~= "number" or component < 0 or component > 1 then
config[key] = copyDefault(option)
break
end
end
end
end
end
end
for i = table.getn(options), 1, -1 do
if corruptOptions[i] then table.remove(options, i) end
end
end
function WA.ValidateUserConfig(data)
if not data then return end
data.authorOptions = data.authorOptions or {}
data.config = data.config or {}
validateUserConfig(data, data.authorOptions, data.config)
end
local function customOptionHalf(option)
return (option.width or 1) < 2
end
local function setCustomConfig(data, option, value, config)
config = config or data.config
setOptionValue(data, option, value, config)
end
local function selectFields(fields, data, option, config)
local values, labels = {}, {}
for i = 1, table.getn(option.values or {}) do
values[i] = i
labels[i] = option.values[i]
end
local field = {
type = "select", name = option.name, values = values, labels = labels,
half = customOptionHalf(option),
get = function() return optionValue(option, config) end,
set = function(index)
local value = index
setCustomConfig(data, option, value, config)
end,
}
table.insert(fields, field)
end
local function mediaField(fields, data, option, config)
local mediaType = option.mediaType
if mediaType == "sound" then
local previews = {}
for key in pairs(WA.sound_types or {}) do
if key ~= " custom" and key ~= " KitID" then previews[key] = true end
end
table.insert(fields, {
type = "select", name = option.name, values = WA.SoundValues or {},
labels = WA.sound_types, previews = previews,
onPreview = function(value)
if value ~= " custom" and value ~= " KitID" and WA.PreviewSound then
WA.PreviewSound(value)
end
end,
half = customOptionHalf(option),
get = function() return optionValue(option, config) end,
set = function(value) setCustomConfig(data, option, value, config) end,
})
elseif mediaType == "font" and WA.textCore and WA.textCore.FONTS then
local values, labels = {}, {}
for i = 1, table.getn(WA.textCore.FONTS) do
local font = WA.textCore.FONTS[i]
values[i] = font.path
labels[font.path] = font.name
end
table.insert(fields, {
type = "select", name = option.name, values = values, labels = labels,
half = customOptionHalf(option),
get = function() return optionValue(option, config) end,
set = function(value) setCustomConfig(data, option, value, config) end,
})
elseif mediaType == "statusbar" and WA.Widgets and WA.Widgets.BarTextures then
local values = WA.Widgets.BarTextures()
table.insert(fields, {
type = "select", name = option.name, values = values,
swatches = WA.Widgets.BarTextureSwatches and WA.Widgets.BarTextureSwatches() or nil,
half = customOptionHalf(option),
get = function() return optionValue(option, config) end,
set = function(value) setCustomConfig(data, option, value, config) end,
})
else
table.insert(fields, {
type = "input", name = option.name, half = customOptionHalf(option),
get = function() return optionValue(option, config) end,
set = function(value) setCustomConfig(data, option, value, config) end,
})
end
end
local function simpleOptionField(fields, data, option, config)
config = config or data.config
local half = customOptionHalf(option)
if option.type == "toggle" then
table.insert(fields, {
type = "toggle", name = option.name, half = half,
get = function() return optionValue(option, config) and true or false end,
set = function(value) setCustomConfig(data, option, value and true or false, config) end,
})
elseif option.type == "input" then
table.insert(fields, {
type = option.multiline and "multiline" or "input", name = option.name,
half = half, height = option.multiline and 80 or nil,
get = function() return optionValue(option, config) end,
set = function(value) setCustomConfig(data, option, value, config) end,
})
elseif option.type == "number" then
table.insert(fields, {
type = "input", name = option.name, half = half,
get = function() return optionValue(option, config) end,
set = function(value)
local number = tonumber(value)
if number ~= nil then setCustomConfig(data, option, number, config) end
end,
})
elseif option.type == "range" then
table.insert(fields, {
type = "range", name = option.name, half = half,
min = option.min or 0, max = option.max or 100, step = option.step or 1,
get = function() return optionValue(option, config) end,
set = function(value) setCustomConfig(data, option, tonumber(value), config) end,
})
elseif option.type == "color" then
table.insert(fields, {
type = "color", name = option.name, half = half,
get = function() return optionValue(option, config) end,
set = function(value) setCustomConfig(data, option, value, config) end,
})
elseif option.type == "select" then
selectFields(fields, data, option, config)
elseif option.type == "multiselect" then
table.insert(fields, {
type = "description", name = option.name or "",
})
for i = 1, table.getn(option.values or {}) do
local index = i
table.insert(fields, {
type = "toggle", name = option.values[i], half = half,
get = function()
local values = optionValue(option, config)
return type(values) == "table" and values[index] and true or false
end,
set = function(value)
setOptionValueAt(data, option, index, value and true or false, config)
end,
})
end
elseif option.type == "media" then
mediaField(fields, data, option, config)
end
end
local function placeholderField(fields, option)
local name = option.name or option.key or "Unnamed option"
local message
if option.type == "group" then
message = "Custom option group '" .. tostring(name) .. "' is not editable here."
else
message = "Custom option '" .. tostring(name) .. "' of type '"
.. tostring(option.type) .. "' is not editable here."
end
table.insert(fields, { type = "description", name = message, half = customOptionHalf(option) })
end
local authorOptionTypes = {
"toggle", "input", "number", "range", "color", "select",
"description", "space", "header", "multiselect", "media", "group",
}
local authorWidthValues = {1, 2}
local authorWidthLabels = {[1] = "Half", [2] = "Full"}
local function authorField(field, index, action)
field.authorIndex = index
field.authorAction = action
return field
end
local function authorKeyIsAvailable(data, option, key, options)
if not key or key == "" then return false end
options = options or data.authorOptions or {}
for i = 1, table.getn(options) do
local other = options[i]
if other ~= option and other.key == key then return false end
end
return true
end
local function ensureUniqueAuthorKey(candidate, suffix, options, index)
index = index or 1
local key = candidate
local existingKeys = {}
local goodKey = true
for i = 1, table.getn(options or {}) do
local option = options[i]
if option and option.key then existingKeys[option.key] = true end
end
if existingKeys[key] then goodKey = false end
if not goodKey then
local prefix = candidate .. (suffix or "")
while not goodKey do
key = prefix .. index
goodKey = not existingKeys[key]
index = index + 1
end
end
return key
end
local function authorOptionChanged(data)
WA.MergeDefaults(data)
WA.Add(data)
WA.RefreshAuraEnvConfig(data.id)
if WA.RefreshOptions then WA.RefreshOptions() end
end
local function setAuthorOption(data, option, key, value, options)
if key == "key" and not authorKeyIsAvailable(data, option, value, options) then return end
option[key] = value
authorOptionChanged(data)
end
local function authorInputField(index, name, key, data, option, half, options)
return authorField({
type = "input", name = name, half = half,
get = function() return option[key] end,
set = function(value) setAuthorOption(data, option, key, value, options) end,
}, index, key)
end
local function authorNumberField(index, name, key, data, option, half, options)
return authorField({
type = "input", name = name, half = half,
get = function() return option[key] end,
set = function(value)
local number = tonumber(value)
if number ~= nil then setAuthorOption(data, option, key, number, options) end
end,
}, index, key)
end
local authorMediaTypes = {"sound", "font", "border", "background", "statusbar"}
local authorMediaLabels = {
sound = "Sound", font = "Font", border = "Border",
background = "Background", statusbar = "Status Bar",
}
local function authorMediaValues(option)
local mediaType = option.mediaType
local values, labels = {}, {}
if mediaType == "sound" then
values = WA.SoundValues or {}
labels = WA.sound_types or {}
elseif mediaType == "font" and WA.textCore and WA.textCore.FONTS then
for i = 1, table.getn(WA.textCore.FONTS) do
local font = WA.textCore.FONTS[i]
values[i] = font.path
labels[font.path] = font.name
end
elseif mediaType == "statusbar" and WA.Widgets and WA.Widgets.BarTextures then
values = WA.Widgets.BarTextures()
labels = {}
for i = 1, table.getn(values) do labels[values[i]] = values[i] end
end
return values, labels
end
local function authorMediaFields(fields, data, option, index)
table.insert(fields, authorField({
type = "select", name = "Media Type", values = authorMediaTypes,
labels = authorMediaLabels, half = true,
get = function() return option.mediaType end,
set = function(value)
option.mediaType = value
option.default = WA.author_option_media_defaults[value]
authorOptionChanged(data)
end,
}, index, "mediaType"))
local values, labels = authorMediaValues(option)
if table.getn(values) > 0 then
local field = {
type = "select", name = "Default", values = values, labels = labels,
half = false,
get = function() return option.default end,
set = function(value) setAuthorOption(data, option, "default", value) end,
}
if option.mediaType == "sound" then
field.previews = {}
for i = 1, table.getn(values) do
local value = values[i]
if value ~= " custom" and value ~= " KitID" then field.previews[value] = true end
end
field.onPreview = function(value)
if value ~= " custom" and value ~= " KitID" and WA.PreviewSound then
WA.PreviewSound(value)
end
end
elseif option.mediaType == "statusbar" and WA.Widgets.BarTextureSwatches then
field.swatches = WA.Widgets.BarTextureSwatches()
end
table.insert(fields, authorField(field, index, "default"))
else
table.insert(fields, authorInputField(index, "Default", "default", data, option, false))
end
end
local function normalizeMultiselectDefault(option)
if type(option.default) ~= "table" then option.default = {} end
local count = table.getn(option.values or {})
for i = 1, count do
if type(option.default[i]) ~= "boolean" then option.default[i] = false end
end
for i = table.getn(option.default), count + 1, -1 do option.default[i] = nil end
end
local function reorderMultiselectDefault(option, fromIndex, before)
local value = table.remove(option.default, fromIndex)
local insertAt = (fromIndex < before) and (before - 1) or before
table.insert(option.default, insertAt, value)
normalizeMultiselectDefault(option)
end
local function authorMultiselectFields(fields, data, option, index)
local valuesField = authorField({
type = "namelist", name = "Values", get = function() return option.values end,
onChange = function()
normalizeMultiselectDefault(option)
authorOptionChanged(data)
end,
onReorder = function(fromIndex, before)
reorderMultiselectDefault(option, fromIndex, before)
authorOptionChanged(data)
end,
onRemove = function(indexValue)
table.remove(option.default, indexValue)
authorOptionChanged(data)
end,
onAdd = function()
table.insert(option.default, table.getn(option.values), false)
authorOptionChanged(data)
end,
}, index, "values")
table.insert(fields, valuesField)
normalizeMultiselectDefault(option)
for valueIndex = 1, table.getn(option.values or {}) do
local i = valueIndex
table.insert(fields, authorField({
type = "toggle", name = "Default: " .. tostring(option.values[i]), half = true,
get = function() return option.default[i] and true or false end,
set = function(value)
option.default[i] = value and true or false
authorOptionChanged(data)
end,
}, index, "default" .. i))
end
end
local function authorSimpleTypeFields(fields, data, option, index)
local optionType = option.type
if optionType == "toggle" then
table.insert(fields, authorField({
type = "select", name = "Default", values = {0, 1},
labels = {[0] = "False", [1] = "True"}, half = true,
get = function() return option.default and 1 or 0 end,
set = function(value) setAuthorOption(data, option, "default", value == 1) end,
}, index, "default"))
elseif optionType == "input" then
table.insert(fields, authorInputField(index, "Default", "default", data, option, false))
table.insert(fields, authorField({
type = "toggle", name = "Limit Length", half = true,
get = function() return option.useLength and true or false end,
set = function(value) setAuthorOption(data, option, "useLength", value and true or false) end,
}, index, "useLength"))
if option.useLength then
table.insert(fields, authorNumberField(index, "Length", "length", data, option, true))
end
table.insert(fields, authorField({
type = "toggle", name = "Multiline", half = true,
get = function() return option.multiline and true or false end,
set = function(value) setAuthorOption(data, option, "multiline", value and true or false) end,
}, index, "multiline"))
elseif optionType == "number" then
table.insert(fields, authorNumberField(index, "Default", "default", data, option, true))
table.insert(fields, authorNumberField(index, "Minimum", "min", data, option, true))
table.insert(fields, authorNumberField(index, "Maximum", "max", data, option, true))
table.insert(fields, authorNumberField(index, "Step", "step", data, option, true))
elseif optionType == "range" then
table.insert(fields, authorField({
type = "range", name = "Default", min = option.min or 0,
max = option.max or 1, step = option.step or .05, half = true,
get = function() return option.default end,
set = function(value) setAuthorOption(data, option, "default", tonumber(value)) end,
}, index, "default"))
table.insert(fields, authorNumberField(index, "Minimum", "min", data, option, true))
table.insert(fields, authorNumberField(index, "Maximum", "max", data, option, true))
table.insert(fields, authorNumberField(index, "Step", "step", data, option, true))
elseif optionType == "color" then
table.insert(fields, authorField({
type = "color", name = "Default", half = true,
get = function() return option.default end,
set = function(value) setAuthorOption(data, option, "default", value) end,
}, index, "default"))
elseif optionType == "select" then
local values, labels = {}, {}
for valueIndex = 1, table.getn(option.values or {}) do
values[valueIndex] = valueIndex
labels[valueIndex] = option.values[valueIndex]
end
table.insert(fields, authorField({
type = "select", name = "Default", values = values, labels = labels, half = true,
get = function() return option.default end,
set = function(value) setAuthorOption(data, option, "default", value) end,
}, index, "default"))
table.insert(fields, authorField({
type = "namelist", name = "Values",
get = function() return option.values end,
onChange = function() authorOptionChanged(data) end,
}, index, "values"))
elseif optionType == "media" then
authorMediaFields(fields, data, option, index)
elseif optionType == "multiselect" then
authorMultiselectFields(fields, data, option, index)
elseif optionType == "group" then
placeholderField(fields, option)
end
end
local function authorNoninteractiveFields(fields, data, option, index)
if option.type == "description" then
table.insert(fields, authorInputField(index, "Text", "text", data, option, false))
table.insert(fields, authorField({
type = "select", name = "Font Size", values = {"small", "medium", "large"},
labels = {small = "Small", medium = "Medium", large = "Large"}, half = true,
get = function() return option.fontSize end,
set = function(value) setAuthorOption(data, option, "fontSize", value) end,
}, index, "fontSize"))
elseif option.type == "header" then
table.insert(fields, authorField({
type = "toggle", name = "Use Name", half = true,
get = function() return option.useName and true or false end,
set = function(value) setAuthorOption(data, option, "useName", value and true or false) end,
}, index, "useName"))
table.insert(fields, authorInputField(index, "Text", "text", data, option, false))
elseif option.type == "space" then
table.insert(fields, authorField({
type = "toggle", name = "Variable Width", half = true,
get = function() return option.variableWidth and true or false end,
set = function(value) setAuthorOption(data, option, "variableWidth", value and true or false) end,
}, index, "variableWidth"))
table.insert(fields, authorField({
type = "toggle", name = "Use Height", half = true,
get = function() return option.useHeight and true or false end,
set = function(value) setAuthorOption(data, option, "useHeight", value and true or false) end,
}, index, "useHeight"))
if option.useHeight then
table.insert(fields, authorNumberField(index, "Height", "height", data, option, true))
end
end
end
local function authorGroupFields(fields, data, option, index)
local groupTypes = {"simple", "array"}
local groupTypeLabels = {simple = "Simple", array = "Array"}
table.insert(fields, authorField({
type = "select", name = "Group Type", values = groupTypes,
labels = groupTypeLabels, half = true,
get = function() return option.groupType end,
set = function(value) setAuthorOption(data, option, "groupType", value) end,
}, index, "groupType"))
if option.groupType ~= "array" then return end
local limitTypes = {"none", "max", "fixed"}
local limitLabels = {none = "Unlimited", max = "Limited", fixed = "Fixed Size"}
table.insert(fields, authorField({
type = "select", name = "Limit Type", values = limitTypes,
labels = limitLabels, half = true,
get = function() return option.limitType end,
set = function(value) setAuthorOption(data, option, "limitType", value) end,
}, index, "limitType"))
table.insert(fields, authorNumberField(index, "Size", "size", data, option, true))
local nameSources, nameSourceLabels = {-1, 0}, {
[-1] = "Fixed Names", [0] = "Entry Order",
}
for subIndex = 1, table.getn(option.subOptions or {}) do
local subOption = option.subOptions[subIndex]
if subOption and (subOption.type == "input" or subOption.type == "number"
or subOption.type == "range" or option.nameSource == subIndex) then
table.insert(nameSources, subIndex)
nameSourceLabels[subIndex] = subOption.name or subOption.key or
("Option " .. tostring(subIndex))
end
end
table.insert(fields, authorField({
type = "select", name = "Entry Name Source", values = nameSources,
labels = nameSourceLabels, half = true,
get = function() return option.nameSource or 0 end,
set = function(value) setAuthorOption(data, option, "nameSource", value) end,
}, index, "nameSource"))
table.insert(fields, authorField({
type = "toggle", name = "Hide Reorder", half = true,
get = function() return option.hideReorder and true or false end,
set = function(value) setAuthorOption(data, option, "hideReorder", value and true or false) end,
}, index, "hideReorder"))
end
local function authorOptionFields(fields, data, option, index, options)
table.insert(fields, authorField({
type = "select", name = "Option Type", values = authorOptionTypes,
labels = WA.author_option_types,
get = function() return option.type end,
set = function(value)
local old = option
local defaults = WA.author_option_fields[value] or {}
local newOption = WA.DeepCopy(defaults)
local commonFields = WA.author_option_fields.common
for key in pairs(commonFields) do
if old[key] ~= nil then newOption[key] = WA.DeepCopy(old[key]) end
end
newOption.type = value
local optionClass = WA.author_option_classes[value]
if optionClass == "noninteractive" then
newOption.name = nil
newOption.desc = nil
newOption.key = nil
newOption.useDesc = nil
newOption.default = nil
else
newOption.name = newOption.name or "Option " .. index
newOption.key = newOption.key or ensureUniqueAuthorKey(
"option" .. index, "", options or data.authorOptions, 1)
end
(options or data.authorOptions)[index] = newOption
authorOptionChanged(data)
end,
}, index, "type"))
local optionClass = WA.author_option_classes[option.type]
if optionClass ~= "noninteractive" then
table.insert(fields, authorInputField(index, "Display Name", "name", data, option, true, options))
table.insert(fields, authorInputField(index, "Option Key", "key", data, option, true, options))
if optionClass == "simple" then
table.insert(fields, authorField({
type = "toggle", name = "Description", half = true,
get = function() return option.useDesc and true or false end,
set = function(value) setAuthorOption(data, option, "useDesc", value and true or false) end,
}, index, "useDesc"))
if option.useDesc then
table.insert(fields, authorInputField(index, "Description Text", "desc", data, option, false))
end
end
end
table.insert(fields, authorField({
type = "select", name = "Width", values = authorWidthValues,
labels = authorWidthLabels, half = true,
get = function() return option.width or 1 end,
set = function(value) setAuthorOption(data, option, "width", value) end,
}, index, "width"))
if optionClass == "simple" then
authorSimpleTypeFields(fields, data, option, index)
elseif optionClass == "noninteractive" then
authorNoninteractiveFields(fields, data, option, index)
elseif optionClass == "group" then
authorGroupFields(fields, data, option, index)
end
end
local function authorOptionActions(data, options, index, option)
return WA.Widgets.ListActions(options, index, function()
authorOptionChanged(data)
end, function(list, itemIndex)
local copy = WA.DeepCopy(option)
if copy.key then
copy.key = ensureUniqueAuthorKey(copy.key .. "copy", "", options, 1)
end
if copy.name then copy.name = copy.name .. " - Copy" end
table.insert(list, itemIndex + 1, copy)
end)
end
local function optionPathString(path)
return table.concat(path, ".")
end
local function copyOptionPath(path, value)
local copy = {}
for i = 1, table.getn(path) do copy[i] = path[i] end
table.insert(copy, value)
return copy
end
local arrayPages = {}
local function arrayPageKey(data, path)
return tostring(data.id) .. "::config:" .. optionPathString(path)
end
local function getArrayPage(data, path, count)
local key = arrayPageKey(data, path)
local page = arrayPages[key] or 1
if page < 1 then page = 1 end
if count and count > 0 and page > count then page = count end
arrayPages[key] = page
return page
end
local function setArrayPage(data, path, page)
page = tonumber(page) or 1
if page < 1 then page = 1 end
arrayPages[arrayPageKey(data, path)] = page
end
local function initReferences(mergedOption, data, options, index, config, path, parent)
mergedOption.references = {
[data.id] = {
data = data, options = options, index = index,
config = config, path = path, parent = parent,
},
}
if not mergedOption.subOptions then return end
local subConfig
if config then
if mergedOption.groupType == "simple" then
subConfig = config[mergedOption.key]
else
local configList = config[mergedOption.key]
local count = type(configList) == "table" and table.getn(configList) or 0
local page = getArrayPage(data, path, count)
subConfig = type(configList) == "table" and configList[page] or nil
end
end
local subOptions = options[index].subOptions
for i = 1, table.getn(mergedOption.subOptions) do
local subPath = {}
for j = 1, table.getn(path) do subPath[j] = path[j] end
table.insert(subPath, i)
initReferences(mergedOption.subOptions[i], data, subOptions, i,
subConfig, subPath, mergedOption)
end
end
local function mergeOptions(mergedOptions, data, options, config, prepath, parent)
local nextInsert = 1
prepath = prepath or {}
options = options or {}
for i = 1, table.getn(options) do
local path = {}
for j = 1, table.getn(prepath) do path[j] = prepath[j] end
table.insert(path, i)
local nextOption = options[i]
local shouldMerge = false
if not nextOption.noMerge then
for j = nextInsert, table.getn(mergedOptions) + 1 do
local mergedOption = mergedOptions[j]
if not mergedOption then break end
local validMerge = not mergedOption.noMerge
if validMerge then
for field in pairs(significantFieldsForMerge) do
if nextOption[field] ~= mergedOption[field] then
validMerge = false
break
end
end
end
if validMerge then
shouldMerge = true
nextInsert = j
break
end
end
end
if shouldMerge then
local mergedOption = mergedOptions[nextInsert]
mergedOption.references[data.id] = {
data = data, options = options, index = i,
config = config, path = path, parent = parent,
}
for key, value in pairs(nextOption) do
if key == "subOptions" then
local subConfig
if config then
if mergedOption.groupType == "simple" then
subConfig = config[mergedOption.key]
else
local configList = config[mergedOption.key]
local count = type(configList) == "table" and table.getn(configList) or 0
local page = getArrayPage(data, path, count)
subConfig = type(configList) == "table" and configList[page] or nil
end
end
mergeOptions(mergedOption.subOptions, data, value, subConfig, path, mergedOption)
if mergedOption.groupType == "array" and mergedOption.nameSource ~= nil then
if (nextOption.nameSource or 0) < 1 or mergedOption.nameSource < 1 then
if mergedOption.nameSource ~= nextOption.nameSource then
mergedOption.nameSource = nil
end
else
local source = mergedOption.subOptions[mergedOption.nameSource]
local sourceReference = source and source.references[data.id]
if not sourceReference or sourceReference.index ~= nextOption.nameSource then
mergedOption.nameSource = nil
end
end
end
elseif not specialCasesForMerge[key] and not valuesEqual(mergedOption[key], value) then
mergedOption[key] = nil
end
end
else
nextInsert = table.getn(mergedOptions) + 1
local newOption = WA.DeepCopy(nextOption)
initReferences(newOption, data, options, i, config, path, parent)
table.insert(mergedOptions, nextInsert, newOption)
end
nextInsert = nextInsert + 1
end
end