-
Notifications
You must be signed in to change notification settings - Fork 24
/
OutfitterScriptDialog.lua
445 lines (322 loc) · 11.5 KB
/
OutfitterScriptDialog.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
------------------------------------
Outfitter._EditScriptDialog = {}
------------------------------------
Outfitter._EditScriptDialog.Widgets =
{
"Title",
"Source",
"Settings",
"SettingsDescription",
"SourceScriptEditBox",
"SourceStatusMessage",
"PresetScript",
}
function Outfitter._EditScriptDialog:Construct()
Outfitter._PortraitWindowFrame.Construct(self)
self.Widgets.SourceScriptEditBox.Dialog = self
self.Widgets.SourceScriptEditBox.TextChanged = Outfitter.EditorScript_TextChanged
self.CloseButton:SetScript("OnClick", function (self) self:GetParent():Done() end)
-- Tabs
PanelTemplates_SetNumTabs(self, 2)
self.selectedTab = 1
PanelTemplates_UpdateTabs(self)
-- Setting frames
self.FrameCache = {}
end
function Outfitter._EditScriptDialog:Open(pOutfit, pShowSource)
self.Outfit = pOutfit
self.Widgets.Title:SetText(string.format(Outfitter.cEditScriptTitle, pOutfit:GetName()))
local vScript = Outfitter:GetScript(pOutfit)
if vScript then
self.Widgets.SourceScriptEditBox:SetText(vScript)
else
self.Widgets.SourceScriptEditBox:SetText("")
end
-- Copy the script values
self.ScriptSettings = {}
if self.Outfit.ScriptSettings then
for vKey, vValue in pairs(self.Outfit.ScriptSettings) do
self.ScriptSettings[vKey] = vValue
end
end
self:Show()
local vScriptID = pOutfit.ScriptID or Outfitter:FindMatchingPresetScriptID(vScript)
self:SetPresetScriptID(vScriptID)
--
if pShowSource then
self:SetPanelIndex(2) -- Show the source panel
else
self:SetPanelIndex(1) -- Show the settings panel
end
Outfitter.UIElementsLib:BeginDialog(self)
end
function Outfitter._EditScriptDialog:Close()
Outfitter.SchedulerLib:UnscheduleTask(self.CheckScriptErrors, self)
self.Outfit = nil
self:Hide()
Outfitter.UIElementsLib:EndDialog(self)
end
function Outfitter._EditScriptDialog:Cancel()
self:Close()
end
function Outfitter._EditScriptDialog:Done()
-- Save the script
local vScript = self.Widgets.SourceScriptEditBox:GetText()
local vScriptID = Outfitter:FindMatchingPresetScriptID(vScript)
if vScriptID then
Outfitter:SetScriptID(self.Outfit, vScriptID)
else
Outfitter:SetScript(self.Outfit, vScript)
end
-- Save the settings
self.Outfit.ScriptSettings = self:GetScriptSettings()
--
self:Close()
end
function Outfitter._EditScriptDialog:LoadScript(pScript)
local vFunction, vMessage = loadstring(Outfitter.cScriptPrefix..pScript..Outfitter.cScriptSuffix, "Script for "..(self.Outfit.Name or "untitled outfit"))
if vMessage then
local _, _, vLine, vMessage2 = string.find(vMessage, Outfitter.cExtractErrorFormat)
if vLine then
vMessage = string.format(Outfitter.cScriptErrorFormat, vLine - Outfitter.cScriptPrefixNumLines, vMessage2)
end
end
return vFunction, vMessage
end
function Outfitter._EditScriptDialog:CheckScriptErrors()
local vScript = self.Widgets.SourceScriptEditBox:GetText()
local vScriptFields, vMessage = Outfitter:ParseScriptFields(vScript)
if not vMessage then
_, vMessage = self:LoadScript(vScript)
end
if vMessage then
self.Widgets.SourceStatusMessage:SetText(vMessage)
else
self.Widgets.SourceStatusMessage:SetText("OK")
end
self:SetPresetScriptID(Outfitter:FindMatchingPresetScriptID(vScript))
end
function Outfitter._EditScriptDialog:SetPresetScriptID(pID)
local vPresetScript = Outfitter:GetPresetScriptByID(pID)
if not vPresetScript then
self.SelectedScriptID = "CUSTOM"
self.Widgets.PresetScript:SetCurrentValueText(Outfitter.cCustom)
return
end
self.SelectedScriptID = pID
self.Widgets.PresetScript:SetCurrentValueText(vPresetScript.Name)
self.Widgets.SourceScriptEditBox:SetText(vPresetScript.Script)
local vScriptFields, vMessage = Outfitter:ParseScriptFields(vPresetScript.Script)
if vScriptFields then
self:ConstructSettingsFields(vScriptFields)
end
end
function Outfitter._EditScriptDialog:SetPanelIndex(pIndex)
if pIndex == 1 then
local vScript = self.Widgets.SourceScriptEditBox:GetText()
local vScriptFields = Outfitter:ParseScriptFields(vScript)
if vScriptFields then
self:ConstructSettingsFields(vScriptFields)
end
self.Widgets.Settings:Show()
self.Widgets.Source:Hide()
else
self.ScriptSettings = self:GetScriptSettings()
self.Widgets.Settings:Hide()
self.Widgets.Source:Show()
end
PanelTemplates_SetTab(self, pIndex)
end
function Outfitter._EditScriptDialog:ConstructSettingsFields(pSettings)
-- Outfitter:DebugTable(pSettings, "ConstructSettingsFields")
local vNumFramesUsed = {}
self.SettingsFrames = {}
-- Hide and de-anchor all frames
for vFrameType, vFrames in pairs(self.FrameCache) do
for _, vFrame in ipairs(vFrames) do
vFrame:ClearAllPoints()
vFrame:Hide()
end
vNumFramesUsed[vFrameType] = 0
end
--
local vPreviousFrame = nil
local vPreviousOffsetX = 0
if pSettings.Inputs then
self.Widgets.SettingsDescription:SetText(pSettings.Description or " ")
for _, vDescriptor in ipairs(pSettings.Inputs) do
local vType = string.lower(vDescriptor.Type)
local vSettingTypeInfo = Outfitter.SettingTypeInfo[vType]
if not vSettingTypeInfo then
Outfitter:ErrorMessage("Unknown $SETTING type %s in the script, I can't create a control for it", vType or "nil")
break
end
local vFrameType = vSettingTypeInfo.FrameType
if not vNumFramesUsed[vFrameType] then
vNumFramesUsed[vFrameType] = 0
end
local vFrameIndex = vNumFramesUsed[vFrameType] + 1
local vFrame
-- Create a new frame if needed
local vFrameCache = self.FrameCache[vFrameType]
if not vFrameCache then
vFrameCache = {}
self.FrameCache[vFrameType] = vFrameCache
end
vFrame = vFrameCache[vFrameIndex]
if not vFrame then
local vFrameName = self.Widgets.Settings:GetName()..vFrameType..vFrameIndex
if vFrameType == "ScrollableEditBox" then
vFrame = CreateFrame("ScrollFrame", vFrameName, self.Widgets.Settings, "OutfitterScrollableEditBox")
vFrame:SetWidth(300)
vFrame:SetHeight(80)
elseif vFrameType == "EditBox" then
vFrame = CreateFrame("EditBox", vFrameName, self.Widgets.Settings, "OutfitterInputBoxTemplate")
vFrame:SetAutoFocus(false)
vFrame:SetWidth(300)
vFrame:SetHeight(18)
elseif vFrameType == "ZoneListEditBox" then
vFrame = CreateFrame("ScrollFrame", vFrameName, self.Widgets.Settings, "OutfitterZoneListEditBox")
vFrame:SetWidth(180)
vFrame:SetHeight(80)
elseif vFrameType == "Checkbox" then
vFrame = CreateFrame("CheckButton", vFrameName, self.Widgets.Settings, "OutfitterCheckboxTemplate")
vFrame:SetWidth(24)
vFrame:SetHeight(24)
end
if vFrame then
vFrameCache[vFrameIndex] = vFrame
end
end
if vFrame then
table.insert(self.SettingsFrames, vFrame)
vFrame.Descriptor = vDescriptor
-- Position the frame
local vOffsetX, vOffsetY = 0, -10
if vDescriptor.Type == "Number" then
vFrame:SetWidth(100)
end
if vFrameType == "EditBox" then
vOffsetX = 6
end
if not vPreviousFrame then
vFrame:SetPoint("TOPLEFT", self.Widgets.SettingsDescription, "BOTTOMLEFT", vOffsetX - vPreviousOffsetX, vOffsetY)
else
vFrame:SetPoint("TOPLEFT", vPreviousFrame, "BOTTOMLEFT", vOffsetX - vPreviousOffsetX, vOffsetY)
end
vPreviousFrame = vFrame
vPreviousOffsetX = vOffsetX
vNumFramesUsed[vFrameType] = vFrameIndex
-- Set the label
local vLabelText = getglobal(vFrame:GetName().."Label")
if not vLabelText then
vLabelText = getglobal(vFrame:GetName().."Text")
end
vLabelText:SetText(vDescriptor.Label)
-- Set the suffix
local vSuffixText = getglobal(vFrame:GetName().."Suffix")
if vSuffixText then
vSuffixText:SetText(vDescriptor.Suffix or "")
end
-- Set the zone type if it's a zone list
if vFrameType == "ZoneListEditBox" then
local vType = vDescriptor.ZoneType
if not vType then
vType = "Zone"
end
local vZoneButton = getglobal(vFrame:GetName().."ZoneButton")
vZoneButton.GetTextFunc = getglobal("Get"..vType.."Text")
vZoneButton:SetText(string.format(Outfitter.cInsertFormat, vZoneButton.GetTextFunc()))
end
-- Show it
vFrame:Show()
end
end
else -- if pSettings.Inputs
self.Widgets.SettingsDescription:SetText(pSettings.Description or Outfitter.cNoScriptSettings)
end
self:SetScriptSettings()
end
function Outfitter._EditScriptDialog:GetScriptSettings()
local vSettings = {}
if self.SettingsFrames then
for _, vFrame in ipairs(self.SettingsFrames) do
local vType = string.lower(vFrame.Descriptor.Type)
local vValue
if vType == "string" then
vValue = vFrame:GetText()
elseif vType == "number" then
vValue = tonumber(vFrame:GetText())
elseif vType == "boolean" then
vValue = vFrame:GetChecked()
elseif vType == "stringtable"
or vType == "zonelist" then
local vEditBox = getglobal(vFrame:GetName().."EditBox")
vValue = {}
for vLine in string.gmatch(vEditBox:GetText(), "([^\r\n]*)") do
if string.len(vLine) > 0 then
table.insert(vValue, vLine)
end
end
else
Outfitter:DebugMessage("EditScriptDialog:GetScriptSettings: Unknown type %s", vType or "nil")
end
vSettings[vFrame.Descriptor.Field] = vValue
end
end
-- Outfitter:DebugTable(vSettings, "GetScriptSettings")
return vSettings
end
function Outfitter._EditScriptDialog:SetScriptSettings()
if not self.SettingsFrames then
return
end
for _, vFrame in ipairs(self.SettingsFrames) do
local vType = string.lower(vFrame.Descriptor.Type)
local vValue = self.ScriptSettings[vFrame.Descriptor.Field]
if not vValue then
local vSettingTypeInfo = Outfitter.SettingTypeInfo[vType]
if not vSettingTypeInfo then
return
end
vValue = vSettingTypeInfo.Default
end
if vType == "string"
or vType == "number" then
vFrame:SetText(vValue)
elseif vType == "boolean" then
vFrame:SetChecked(vValue)
elseif vType == "stringtable"
or vType == "zonelist" then
local vEditBox = getglobal(vFrame:GetName().."EditBox")
if type(vValue) == "table" then
vEditBox:SetText(table.concat(vValue, "\n"))
else
vEditBox:SetText("")
end
end
end
end
function Outfitter._EditScriptDialog:ScriptMenuFunc(menu, outfit)
assert(outfit, "ScriptMenuFunc: outfit is nil")
local category
for _, presetScript in ipairs(Outfitter.PresetScripts) do
if not presetScript.Class or presetScript.Class == Outfitter.PlayerClass then
local newCategory = presetScript.Category or presetScript.Class or "GENERAL"
if category ~= newCategory then
category = newCategory
Outfitter:AddScriptCategorySubmenu(menu, category,
function ()
return self.SelectedScriptID
end, function (menu, value)
self:SetPresetScriptID(value)
end)
end
end
end
end
function Outfitter.EditorScript_TextChanged(pEditBox)
local vDialog = pEditBox.Dialog
vDialog.Widgets.SourceStatusMessage:SetText(Outfitter.cTyping)
Outfitter.SchedulerLib:RescheduleTask(1.5, vDialog.CheckScriptErrors, vDialog)
end