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