Mercurial > wow > buffalo2
comparison Config/Config.lua @ 47:1a322b92dbfa
file cleanup
author | Nenue |
---|---|
date | Thu, 28 Apr 2016 05:54:21 -0400 |
parents | Config.lua@66b927b46776 |
children | 16465f3fd919 |
comparison
equal
deleted
inserted
replaced
46:aa693607b813 | 47:1a322b92dbfa |
---|---|
1 --- All the control GUI stuff, including chat command functions | |
2 -- @file-author@ | |
3 -- @project-revision@ @project-hash@ | |
4 -- @file-revision@ @file-hash@ | |
5 -- Created: 3/12/2016 12:49 AM | |
6 local B, _G = select(2,...).frame, _G | |
7 local M = B:RegisterModule("Options") | |
8 local tostring, tonumber, floor, format = tostring, tonumber, floor, string.format | |
9 local unpack, select, pairs, ipairs, type, wipe = unpack, select, pairs, ipairs, type, table.wipe | |
10 local CreateFrame, IsControlKeyDown = _G.CreateFrame, _G.IsControlKeyDown | |
11 local max = math.max | |
12 local OpacitySliderFrame, ColorPickerFrame = _G.OpacitySliderFrame, _G.ColorPickerFrame | |
13 local print = B.print('Cfgl') | |
14 local function round(number, decimals) | |
15 if floor(number) == number then | |
16 return ('%d'):format(number) | |
17 end | |
18 | |
19 return (("%%.%df"):format(decimals)):format(number) | |
20 end | |
21 | |
22 M.defaults = { | |
23 enable = true | |
24 } | |
25 | |
26 --- STATE VARIABLES | |
27 local configInit | |
28 --- Dummies for addon table upvalues | |
29 local configFrames = {} -- actual frame objects | |
30 local displays = B.displays -- anchor objects dummy | |
31 | |
32 | |
33 --- Returns a value retreival function and the current value stored in config | |
34 -- @paramsig value, previousValue = configInteger(key) | |
35 -- @param key Name of the config field being represented. | |
36 local defaultGroup = 'BuffButton' | |
37 local configInteger = function(group, key) | |
38 return function(self ,display) | |
39 return floor(tonumber(self:GetValue()) + 0.5) | |
40 end, (B.Conf[group ..key] or B.Conf[defaultGroup..key]) | |
41 end | |
42 local configPercent = function(group, key) | |
43 return function(self, display) | |
44 local value = self:GetValue() | |
45 if display then | |
46 return tostring(floor(value*100+0.5))..' %' | |
47 else | |
48 return floor((value*100+0.5))/100 | |
49 end | |
50 end, (B.Conf[group ..key] or B.Conf[defaultGroup..key]) | |
51 end | |
52 local configColor = function(group, key) | |
53 -- table for config, color value list for text | |
54 return function(self, display) | |
55 if display then | |
56 return "|cFFFF4444" .. round(self.rgba[1], 1) .. "|r, |cFF44FF44" .. round(self.rgba[2], 1) .. "|r, |cFF4488FF" .. | |
57 round(self.rgba[3], 1) .. "|r, " .. round(self.rgba[4], 1) | |
58 else | |
59 return self.rgba | |
60 end | |
61 end, (B.Conf[group ..key] or B.Conf[defaultGroup..key]) | |
62 end | |
63 local configCheck = function(group, key) | |
64 return function(self) return self:GetChecked() end, B.Conf[group ..key] or B.Conf[defaultGroup..key] | |
65 end | |
66 -- initializes the corresponding type of config field | |
67 local frameTypeConv = { | |
68 Color = 'Button', | |
69 Font = 'Frame', | |
70 } | |
71 local configTypeParams = { | |
72 Slider = function(frame, optionInfo) | |
73 frame:SetMinMaxValues(optionInfo[5], optionInfo[6]) | |
74 frame:SetValueStep(optionInfo[7]) | |
75 frame:SetStepsPerPage(optionInfo[8]) | |
76 print(frame.OptName, '\n {', optionInfo[5], optionInfo[6], optionInfo[7], optionInfo[8], '}') | |
77 end, | |
78 CheckButton = function(frame, optionInfo) | |
79 frame.SetValue = function(self, ...) | |
80 self:SetChecked(...) | |
81 B.Conf[self.OptName] = self:GetChecked() | |
82 print(self.OptTab) | |
83 B.UpdateAll() | |
84 end | |
85 frame:SetScript("OnClick",function(self) | |
86 B.Conf[self.OptName] = self:GetChecked() | |
87 print(B.Conf[self.OptName], self:GetChecked()) | |
88 B.UpdateAll() | |
89 end) | |
90 end, | |
91 Color = function(frame, optionInfo) | |
92 frame.rgba = { frame.current:GetVertexColor() } | |
93 local colorPickerCallback = function(restore) | |
94 local newR, newG, newB, newA | |
95 if restore then | |
96 newR, newG, newB, newA = unpack(restore) | |
97 else | |
98 newA, newR, newG, newB = OpacitySliderFrame:GetValue(), ColorPickerFrame:GetColorRGB() | |
99 print('not cancel', newA, newR, newB, newG) | |
100 end | |
101 frame:SetValue({newR, newG, newB, newA}) | |
102 B.UpdateBuffs(frame.OptTab) | |
103 end | |
104 frame:SetScript("OnClick", function(self) | |
105 print('got a click') | |
106 local r, g, b, a = frame.current:GetVertexColor() | |
107 ColorPickerFrame:SetColorRGB(r, g, b) | |
108 ColorPickerFrame.hasOpacity = (a ~= nil) | |
109 ColorPickerFrame.opacity = a | |
110 ColorPickerFrame.previousValues = {r,g,b,a} | |
111 ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc = | |
112 colorPickerCallback, colorPickerCallback,colorPickerCallback | |
113 ColorPickerFrame:Hide() | |
114 ColorPickerFrame:Show() | |
115 end) | |
116 frame.SetValue = function(self, rgba) | |
117 print(rgba) | |
118 frame.rgba = rgba | |
119 B.Conf[self.OptName] = rgba | |
120 frame.current:SetVertexColor(unpack(rgba)) | |
121 frame.fieldvalue:SetText(frame.OptValue(frame, true)) | |
122 end | |
123 end | |
124 } | |
125 --- configDialog | |
126 -- @usage tinsert(configDialog, {prefix, row, [...] }) | |
127 -- Each top level member defines a group of config value handlers, structured as an iterative table where the | |
128 -- first member is a key prefix, the second member is an integer row value, and all following members are treated | |
129 -- as a widget resource, defined initially as a complete sub-table, which can be re-used further down by passing | |
130 -- the string literal widget suffix. | |
131 -- widget table: ... {'suffix', 'description', valueCallback, 'template', [widget parameters]} | |
132 -- widget copy: ... 'suffix', ... | |
133 local configDialog = { | |
134 {'BuffButton', 1, | |
135 | |
136 {'Max', 'Max', configInteger, 'Slider', | |
137 1, _G.BUFF_MAX_DISPLAY, 1, 1}, -- valueMin, valueMax, valueStep, stepsPerPage | |
138 {'PerRow', 'Per Row', configInteger, 'Slider', | |
139 1, _G.BUFF_MAX_DISPLAY, 1, 1}, -- valueMin, valueMax, valueStep, stepsPerPage, | |
140 {'Size', 'Icon Size', configInteger, 'Slider', | |
141 1, 256, 1, 1}, | |
142 {'Spacing', 'Icon Spacing', configInteger, 'Slider', | |
143 1, 50, 1, 1}, | |
144 {'DurationSize', 'Duration Text Height', configInteger, 'Slider', | |
145 1, 72, 1, 1}, | |
146 {'Zoom', 'Icon Zoom', configInteger, 'Slider', | |
147 0, 100, 1, 1}, | |
148 {'Border', 'Border', configInteger, 'Slider', | |
149 1, 16, 1, 1}, | |
150 {'Color', 'Default Border', configColor, 'Color'}, | |
151 {'RaidColor', 'RaidBuff Border', configColor, 'Color'}, | |
152 {'PlayerColor', 'Player Buffs', configColor, 'Color'}, | |
153 {'BossColor', 'Encounter Buffs', configColor, 'Color'}, | |
154 {'ShowSelfCast', 'Show name for self-casts', configCheck, 'CheckButton'} | |
155 }, | |
156 { 'DebuffButton', 1, | |
157 {'Max', 'Max', configInteger, 'Slider', | |
158 1, _G.DEBUFF_MAX_DISPLAY, 1, 1 } | |
159 , | |
160 {'PerRow', 'Per Row', configInteger, 'Slider', | |
161 1, _G.DEBUFF_MAX_DISPLAY, 1, 1 }, | |
162 'Size', 'Spacing', 'DurationSize', 'Zoom', 'Border', | |
163 'Color', 'RaidColor', 'PlayerColor', 'BossColor', | |
164 }, | |
165 { 'TempEnchant', 1, | |
166 {'Max', 'Max', configInteger, 'Slider', | |
167 1, _G.NUM_TEMP_ENCHANT_FRAMES, 1, 1 }, | |
168 {'PerRow', 'Per Row', configInteger, 'Slider', | |
169 1, _G.NUM_TEMP_ENCHANT_FRAMES, 1, 1}, | |
170 'Size', 'Spacing', 'DurationSize', 'Zoom', 'Border', | |
171 'Color', 'RaidColor', 'PlayerColor', 'BossColor', | |
172 }, | |
173 { 'ConsolidatedBuff', 2, | |
174 {'Position', 'Slot Position', configInteger, 'Slider', | |
175 1, _G.BUFF_MAX_DISPLAY, 1, 1 } | |
176 | |
177 }, | |
178 { 'ConsolidatedBuff', 2, | |
179 'Size' | |
180 }, | |
181 { 'Raid', 3, | |
182 {'ShowMissing', 'Verbose missing raid buffs', configCheck, 'CheckButton'} | |
183 } | |
184 } | |
185 | |
186 | |
187 | |
188 | |
189 local configFrame | |
190 local optionTemplates = {} | |
191 local configPadding, configSpacing = 3, 3 | |
192 | |
193 --- Walks the structure table to generate a pretty config panel | |
194 local InitConfig = function() | |
195 configInit = true | |
196 local configWidth = B:GetWidth() | |
197 local optionWidth = (configWidth - configPadding) / 3 - configSpacing | |
198 local configHeight = 0 | |
199 local bottom_extent = 0 | |
200 local clusterHeight = 0 | |
201 local clusterOffset = 0 | |
202 local lastCluster | |
203 local cluster = 1 | |
204 local col = 0 | |
205 for t, taboptions in ipairs(configDialog) do | |
206 local group = taboptions[1] | |
207 cluster = taboptions[2] | |
208 col = col + 1 | |
209 | |
210 | |
211 if not configFrames[t] then | |
212 configFrames[t] = {} | |
213 end | |
214 | |
215 | |
216 if cluster ~= lastCluster then | |
217 configHeight = configHeight + clusterHeight | |
218 print('|cFFFF8800## new cluster|r, advancing offset from', clusterOffset, 'to', clusterOffset + clusterHeight) | |
219 clusterOffset = clusterOffset + clusterHeight | |
220 col = 1 | |
221 clusterHeight = 0 | |
222 lastCluster = cluster | |
223 end | |
224 | |
225 print('processing tab', group) | |
226 local row = 0 | |
227 for i = 3, #taboptions do | |
228 row = row + 1 | |
229 local optionInfo = taboptions[i] | |
230 if type(optionInfo) == 'string' then | |
231 optionInfo = optionTemplates[optionInfo] | |
232 end | |
233 local key, fieldname, valueFuncGenerator, configType = unpack(optionInfo) | |
234 | |
235 if not optionTemplates[key] then | |
236 optionTemplates[key] = optionInfo | |
237 end | |
238 | |
239 local fullkey = group .. key | |
240 print(fullkey, fieldname) | |
241 | |
242 if not configFrames[t][row] then | |
243 print('building frame', t, group, row) | |
244 local frameTemplate = 'VeneerConfig'..configType | |
245 local frameType = frameTypeConv[configType] or configType | |
246 configFrames[t][row] = CreateFrame(frameType, fullkey, B, frameTemplate) | |
247 local f = configFrames[t][row] | |
248 f.OptKey = key | |
249 f.OptTab = group | |
250 f.OptName = fullkey | |
251 local valueFunc, initialValue = valueFuncGenerator(group, key) | |
252 print(' value getter', fullkey,'->', valueFunc,initialValue) | |
253 configTypeParams[configType](f, optionInfo) | |
254 f.OptValue = valueFunc | |
255 | |
256 --- Enclosing these to | |
257 -- a) make the panel easy to bring up externally | |
258 -- b) limit gameplay risk from config frame errors | |
259 -- c) milk the iterator scope for all its worth | |
260 f.OnChange = function(self) | |
261 | |
262 -- holding control; mirror this setting in other categories | |
263 if IsControlKeyDown() and not (configInit) then | |
264 configInit = true | |
265 for optTab, opts in pairs(configFrames) do | |
266 for _, opt in ipairs(opts) do | |
267 if opt.OptKey == key then | |
268 if optTab ~= group then | |
269 print('mapping to', optTab, opt.OptKey) | |
270 opt:SetValue(self:GetValue()) | |
271 end | |
272 | |
273 end | |
274 end | |
275 end | |
276 configInit = nil | |
277 end | |
278 local newValue = valueFunc(self) | |
279 if newValue ~= B.Conf[fullkey] then | |
280 print(newValue, fullkey) | |
281 f.fieldvalue:SetText(valueFunc(self, true)) | |
282 B.Conf[fullkey] = valueFunc(self) | |
283 -- prepare to update | |
284 wipe(B.drawn[f.OptTab]) | |
285 B.UpdateBuffs(self.OptTab) | |
286 B.UpdateConfigLayers() | |
287 end | |
288 | |
289 end | |
290 | |
291 f:SetValue(initialValue) | |
292 local yBuffer = configPadding | |
293 if f.fieldname then | |
294 f.fieldname:SetText(fieldname) | |
295 yBuffer = yBuffer + f.fieldname:GetHeight() | |
296 end | |
297 if f.fieldvalue then | |
298 f.fieldvalue:SetText(f:OptValue(true)) | |
299 end | |
300 | |
301 local point, relative, x, y = 'TOPLEFT', 'BOTTOMLEFT', 0, -3 | |
302 | |
303 local base | |
304 if (row == 1) then | |
305 bottom_extent = 0 | |
306 base = B.header | |
307 x = (col-1) * (optionWidth+configSpacing) | |
308 y = -configPadding | |
309 else | |
310 base = configFrames[t][row-1] | |
311 end | |
312 | |
313 print('|cFFFF0088'..cluster..'|r |cFF00FF00'.. row..'|r', col, base:GetName(), x, y - clusterOffset) | |
314 | |
315 if frameType ~= 'CheckButton' then | |
316 f:SetWidth(optionWidth) | |
317 end | |
318 | |
319 f:SetPoint(point, base, relative, x, y-yBuffer-clusterOffset) | |
320 --print('creating', frameType, fieldname) | |
321 f:Show() | |
322 | |
323 bottom_extent = bottom_extent + f:GetHeight() + yBuffer + configSpacing | |
324 | |
325 | |
326 | |
327 clusterHeight = max(clusterHeight, bottom_extent) | |
328 --print('y', floor(yBuffer+0.5), 'f:H', floor(f:GetHeight()+0.5), 'hTally', floor(bottom_extent+0.5), 'hMax', floor(configHeight+0.5)) | |
329 end | |
330 end | |
331 end | |
332 | |
333 -- grab the last cluster | |
334 if lastCluster == cluster then | |
335 print('|cFF00FF00##scooping up last cluster info') | |
336 configHeight = configHeight + clusterHeight | |
337 end | |
338 | |
339 if not B.configFramesCreated then | |
340 B.configFramesCreated = true | |
341 B:SetHeight(B.header:GetStringHeight() + configSpacing*3 + configHeight) | |
342 end | |
343 if configInit then configInit = nil end | |
344 end | |
345 | |
346 M.Command = function(enable, editbox) | |
347 displays = B.displays | |
348 if type(enable) == 'boolean' then | |
349 B.Conf.ConfigMode = enable | |
350 else | |
351 B.Conf.ConfigMode = (B.Conf.ConfigMode == false) and true or false | |
352 end | |
353 | |
354 print('/BUFF', B.Conf.ConfigMode, type(B.Conf.ConfigMode)) | |
355 if B.Conf.ConfigMode then | |
356 if not B.configFramesCreated then | |
357 InitConfig() | |
358 end | |
359 print('Veneer config') | |
360 B:Show() | |
361 else | |
362 B:Hide() | |
363 end | |
364 B.UpdateAll() | |
365 B.UpdateConfigLayers() | |
366 end | |
367 | |
368 B.Close = function () | |
369 M.Command() | |
370 end | |
371 | |
372 B.ToggleGuides = function(_, self) | |
373 B.Conf.GuidesMode = (not B.Conf.GuidesMode) | |
374 if B.Conf.GuidesMode then | |
375 self:GetNormalTexture():SetTexture(0.94, 0.21, 0.21, 1) | |
376 else | |
377 self:GetNormalTexture():SetTexture(0, 0, 0, 1) | |
378 end | |
379 | |
380 B.UpdateConfigLayers() | |
381 end | |
382 | |
383 M.OnEnable = function() | |
384 print('|cFFFF0088config module', B.Conf.ConfigMode) | |
385 M.Command(B.Conf.ConfigMode) | |
386 end | |
387 | |
388 M.OnInitialize = function() | |
389 DEFAULT_CHAT_FRAME:AddMessage("|cFF22D822Veneer|r") | |
390 SLASH_BUFFALO1, SLASH_BUFFALO2 = "/buffalo", "/buff" | |
391 SlashCmdList.BUFFALO = M.Command | |
392 | |
393 end |