vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | |
2 | --[[ KLH Threatmeter KLHTM_SelfGui.lua |
||
3 | |||
4 | Lukon Mod 2: Part of the replacement for Gui.lua and Tables.lua. |
||
5 | Controls the GUI for the personal threat details section of KLH |
||
6 | ThreatMeter. See also KTM_Gui.xml, KTM_Gui.lua. |
||
7 | --]] |
||
8 | |||
9 | local mod = klhtm |
||
10 | local me = { } |
||
11 | mod.guiself = me |
||
12 | |||
13 | -- The number of rows defined in New_Frame.xml |
||
14 | local Max_Rows = 15; |
||
15 | |||
16 | -- Local references to some Gui variables |
||
17 | local options = KLHTM_GuiOptions; |
||
18 | local gui = KLHTM_Gui; |
||
19 | local sizes = KLHTM_GuiSizes; |
||
20 | local state = KLHTM_GuiState; |
||
21 | local heights = KLHTM_GuiHeights; |
||
22 | |||
23 | |||
24 | ------------------------------------------------------------------------------ |
||
25 | -- Sets up the instance variable "gui.self". It contains data on the GUI |
||
26 | -- components in the personal threat details frame. |
||
27 | ------------------------------------------------------------------------------ |
||
28 | function KLHTM_CreateSelfTable() |
||
29 | |||
30 | gui.self = { |
||
31 | ["frame"] = KLHTM_SelfFrame, |
||
32 | ["line"] = KLHTM_SelfFrameLine, -- dividing line between title bar and frame |
||
33 | ["head"] = { -- column headers |
||
34 | ["name"] = { |
||
35 | ["frame"] = KLHTM_SelfFrameHeaderName, |
||
36 | ["text"] = KLHTM_SelfFrameHeaderNameText, |
||
37 | ["width"] = 90, |
||
38 | ["colour"] = {["r"] = 1.0, ["g"] = 1.0, ["b"] = 1.0}, |
||
39 | ["vis"] = function() return (options.self.columnVis.name) end, |
||
40 | }, |
||
41 | ["hits"] = { |
||
42 | ["frame"] = KLHTM_SelfFrameHeaderHits, |
||
43 | ["text"] = KLHTM_SelfFrameHeaderHitsText, |
||
44 | ["width"] = 35, |
||
45 | ["colour"] = {["r"] = 0.5, ["g"] = 0.9, ["b"] = 0.5}, |
||
46 | ["vis"] = function() return (options.self.columnVis.hits) end, |
||
47 | }, |
||
48 | ["rage"] = { |
||
49 | ["frame"] = KLHTM_SelfFrameHeaderRage, |
||
50 | ["text"] = KLHTM_SelfFrameHeaderRageText, |
||
51 | ["width"] = 35, |
||
52 | ["colour"] = {["r"] = 0.9, ["g"] = 0.5, ["b"] = 0.5}, |
||
53 | ["vis"] = function() return (options.self.columnVis.rage) end, |
||
54 | }, |
||
55 | ["dam"] = { |
||
56 | ["frame"] = KLHTM_SelfFrameHeaderDamage, |
||
57 | ["text"] = KLHTM_SelfFrameHeaderDamageText, |
||
58 | ["width"] = 55, |
||
59 | ["colour"] = {["r"] = 0.9, ["g"] = 0.9, ["b"] = 0.3}, |
||
60 | ["vis"] = function() return (options.self.columnVis.dam) end, |
||
61 | }, |
||
62 | ["threat"] = { |
||
63 | ["frame"] = KLHTM_SelfFrameHeaderThreat, |
||
64 | ["text"] = KLHTM_SelfFrameHeaderThreatText, |
||
65 | ["width"] = 50, |
||
66 | ["colour"] = {["r"] = 0.9, ["g"] = 0.5, ["b"] = 0.9}, |
||
67 | ["vis"] = function() return (options.self.columnVis.threat) end, |
||
68 | }, |
||
69 | ["pc"] = { |
||
70 | ["frame"] = KLHTM_SelfFrameHeaderPercentThreat, |
||
71 | ["text"] = KLHTM_SelfFrameHeaderPercentThreatText, |
||
72 | ["width"] = 30, |
||
73 | ["colour"] = {["r"] = 0.9, ["g"] = 0.5, ["b"] = 0.9}, |
||
74 | ["vis"] = function() return (options.self.columnVis.pc) end, |
||
75 | }, |
||
76 | }, |
||
77 | ["rows"] = {}, |
||
78 | ["bottom"] = { -- self totals |
||
79 | ["frame"] = KLHTM_SelfFrameBottom, |
||
80 | ["line"] = KLHTM_SelfFrameBottomLine, -- dividing line between frame and bottom bar |
||
81 | ["reset"] = { |
||
82 | -- a dummy frame used to align the bottom bar strings with the Name header |
||
83 | ["frame"] = KLHTM_SelfFrameBottomName, |
||
84 | -- a dummy element. Referenced here only to be hidden |
||
85 | ["bar"] = KLHTM_SelfFrameBottomBar, |
||
86 | ["but"] = KLHTM_SelfFrameBottomReset, |
||
87 | ["text"] = KLHTM_SelfFrameBottomResetText, |
||
88 | ["width"] = 70, |
||
89 | }, |
||
90 | ["string"] = { |
||
91 | ["hits"] = { |
||
92 | ["frame"] = KLHTM_SelfFrameBottomHits, |
||
93 | ["text"] = KLHTM_SelfFrameBottomHitsText, |
||
94 | }, |
||
95 | ["rage"] = { |
||
96 | ["frame"] = KLHTM_SelfFrameBottomRage, |
||
97 | ["text"] = KLHTM_SelfFrameBottomRageText, |
||
98 | }, |
||
99 | ["dam"] = { |
||
100 | ["frame"] = KLHTM_SelfFrameBottomDamage, |
||
101 | ["text"] = KLHTM_SelfFrameBottomDamageText, |
||
102 | }, |
||
103 | ["threat"] = { |
||
104 | ["frame"] = KLHTM_SelfFrameBottomThreat, |
||
105 | ["text"] = KLHTM_SelfFrameBottomThreatText, |
||
106 | }, |
||
107 | }, |
||
108 | }, |
||
109 | }; |
||
110 | for x = 1, Max_Rows do |
||
111 | gui.self.rows[x] = { |
||
112 | ["frame"] = getglobal("KLHTM_SelfFrameRow" .. x); |
||
113 | ["name"] = { |
||
114 | ["frame"] = getglobal("KLHTM_SelfFrameRow" .. x .. "Name"), |
||
115 | ["text"] = getglobal("KLHTM_SelfFrameRow" .. x .. "NameText"), |
||
116 | }, |
||
117 | ["hits"] = { |
||
118 | ["frame"] = getglobal("KLHTM_SelfFrameRow" .. x .. "Hits"), |
||
119 | ["text"] = getglobal("KLHTM_SelfFrameRow" .. x .. "HitsText"), |
||
120 | }, |
||
121 | ["rage"] = { |
||
122 | ["frame"] = getglobal("KLHTM_SelfFrameRow" .. x .. "Rage"), |
||
123 | ["text"] = getglobal("KLHTM_SelfFrameRow" .. x .. "RageText"), |
||
124 | }, |
||
125 | ["dam"] = { |
||
126 | ["frame"] = getglobal("KLHTM_SelfFrameRow" .. x .. "Damage"), |
||
127 | ["text"] = getglobal("KLHTM_SelfFrameRow" .. x .. "DamageText"), |
||
128 | }, |
||
129 | ["threat"] = { |
||
130 | ["frame"] = getglobal("KLHTM_SelfFrameRow" .. x .. "Threat"), |
||
131 | ["text"] = getglobal("KLHTM_SelfFrameRow" .. x .. "ThreatText"), |
||
132 | }, |
||
133 | ["pc"] = { |
||
134 | ["frame"] = getglobal("KLHTM_SelfFrameRow" .. x .. "PercentThreat"), |
||
135 | ["text"] = getglobal("KLHTM_SelfFrameRow" .. x .. "PercentThreatText"), |
||
136 | }, |
||
137 | ["bar"] = getglobal("KLHTM_SelfFrameRow" .. x .. "Bar"), |
||
138 | }; |
||
139 | end |
||
140 | end |
||
141 | |||
142 | |||
143 | ------------------------------------------------------------------------------ |
||
144 | -- Applies Gui component properties that are not specified in the XML. |
||
145 | ------------------------------------------------------------------------------ |
||
146 | function KLHTM_SetupSelfGui() |
||
147 | |||
148 | -- headers and data |
||
149 | for index, header in gui.self.head do |
||
150 | header.text:SetText(mod.string.get("gui", "self", "head", index)); |
||
151 | header.frame:SetHeight(heights.header); |
||
152 | header.text:SetHeight(heights.header); |
||
153 | KLHTM_AddOutline(header.text); |
||
154 | KLHTM_LocaliseStringWidth(header); |
||
155 | |||
156 | for _, row in gui.self.rows do |
||
157 | row[index].text:SetTextColor(header.colour.r, header.colour.g, header.colour.b); |
||
158 | row[index].frame:SetHeight(heights.data); |
||
159 | KLHTM_AddOutline(row[index].text); |
||
160 | end |
||
161 | end |
||
162 | |||
163 | -- rows |
||
164 | for _, row in gui.self.rows do |
||
165 | row.frame:SetHeight(heights.data); |
||
166 | end |
||
167 | |||
168 | -- bottom |
||
169 | gui.self.bottom.frame:SetHeight(heights.button); |
||
170 | |||
171 | for _, string in gui.self.bottom.string do |
||
172 | string.frame:SetHeight(heights.data); |
||
173 | KLHTM_AddOutline(string.text); |
||
174 | end |
||
175 | |||
176 | gui.self.bottom.reset.bar:Hide(); |
||
177 | gui.self.bottom.reset.but:SetHeight(heights.button); |
||
178 | gui.self.bottom.reset.frame:SetHeight(heights.button); |
||
179 | gui.self.bottom.reset.but:SetText(mod.string.get("gui", "self", "reset")); |
||
180 | |||
181 | local width = gui.self.bottom.reset.text:GetStringWidth(); |
||
182 | if (width + 10 > gui.self.bottom.reset.width) then |
||
183 | gui.self.bottom.reset.width = width + 10; |
||
184 | end |
||
185 | |||
186 | gui.self.bottom.reset.but:SetWidth(gui.self.bottom.reset.width); |
||
187 | end |
||
188 | |||
189 | |||
190 | ------------------------------------------------------------------------------ |
||
191 | -- Sets the default options for displaying the main window and title bar strings |
||
192 | -- when the self view is selected. |
||
193 | ------------------------------------------------------------------------------ |
||
194 | function KLHTM_SetDefaultSelfOptions() |
||
195 | |||
196 | options.self = { |
||
197 | -- column visibility |
||
198 | ["columnVis"] = { |
||
199 | ["name"] = true, |
||
200 | ["hits"] = true, |
||
201 | ["rage"] = false, |
||
202 | ["dam"] = true, |
||
203 | ["threat"] = true, |
||
204 | ["pc"] = true, |
||
205 | }, |
||
206 | -- title bar string visibility |
||
207 | ["stringVis"] = { |
||
208 | ["threat"] = true, |
||
209 | }, |
||
210 | -- if true, rows with 0 threat are not shown |
||
211 | ["hideZeroRows"] = true, |
||
212 | -- if true, values of at least 10,000 are abbreviate with "k" |
||
213 | ["abbreviate"] = false, |
||
214 | -- the column used to sort data rows |
||
215 | ["sortColumn"] = "threat", |
||
216 | -- if true, values increase towards the bottom of the table |
||
217 | ["sortDown"] = true, |
||
218 | }; |
||
219 | |||
220 | -- rage column |
||
221 | local class; |
||
222 | _, class = UnitClass("player"); |
||
223 | if ((class == "WARRIOR") or (class == "DRUID")) then |
||
224 | options.self.columnVis.rage = true; |
||
225 | end |
||
226 | end |
||
227 | |||
228 | |||
229 | ------------------------------------------------------------------------------ |
||
230 | -- Updates the visibility of columns the self table and the bottom bar. The |
||
231 | -- contents are not rendered here. The frame width is stored in sizes.self.x. |
||
232 | -- Should be called whenever the column visibility changes. |
||
233 | ------------------------------------------------------------------------------ |
||
234 | function KLHTM_UpdateSelfFrame() |
||
235 | |||
236 | sizes.self.x = 0; |
||
237 | |||
238 | for column, header in gui.self.head do |
||
239 | -- header |
||
240 | if (header.vis()) then |
||
241 | header.frame:SetWidth(header.width); |
||
242 | header.text:SetWidth(header.width); |
||
243 | sizes.self.x = sizes.self.x + header.width; |
||
244 | header.frame:Show(); |
||
245 | |||
246 | else |
||
247 | header.frame:Hide(); |
||
248 | header.frame:SetWidth(0.1); |
||
249 | end |
||
250 | -- rows |
||
251 | for index, row in gui.self.rows do |
||
252 | if (header.vis()) then |
||
253 | row[column].frame:SetWidth(header.width); |
||
254 | row[column].frame:Show(); |
||
255 | else |
||
256 | row[column].frame:Hide(); |
||
257 | row[column].frame:SetWidth(0.1); |
||
258 | end |
||
259 | end |
||
260 | end |
||
261 | |||
262 | -- bottom bar |
||
263 | for name, string in gui.self.bottom.string do |
||
264 | if (gui.self.head[name].vis()) then |
||
265 | string.frame:SetWidth(gui.self.head[name].width); |
||
266 | string.frame:Show(); |
||
267 | else |
||
268 | string.frame:Hide(); |
||
269 | string.frame:SetWidth(0.1); |
||
270 | end |
||
271 | end |
||
272 | -- a dummy frame used to align the bottom bar strings with |
||
273 | -- the Name header |
||
274 | gui.self.bottom.reset.frame:SetWidth(gui.self.head.name.width); |
||
275 | |||
276 | for _, row in gui.self.rows do |
||
277 | row.frame:SetWidth(sizes.self.x); |
||
278 | end |
||
279 | end |
||
280 | |||
281 | |||
282 | ------------------------------------------------------------------------------ |
||
283 | -- Draws the minimised title bar strings present in the self view. |
||
284 | ------------------------------------------------------------------------------ |
||
285 | function KLHTM_DrawSelfStrings() |
||
286 | |||
287 | local threat = mod.table.mydata[mod.string.get("threatsource", "total")].threat; |
||
288 | threat = math.floor(threat + 0.5); |
||
289 | |||
290 | if (options.self.abbreviate) then |
||
291 | threat = KLHTM_Abbreviate(threat); |
||
292 | end |
||
293 | |||
294 | gui.title.string.threat.text:SetText(threat); |
||
295 | end |
||
296 | |||
297 | |||
298 | -- used by GetSelfData(). Defined at the class level to reduce memory use. |
||
299 | local GetSelfData_data; |
||
300 | local GetSelfData_totals; |
||
301 | ------------------------------------------------------------------------------ |
||
302 | -- Makes a copy of the personal threat data in KLHTM_MyData for use by |
||
303 | -- KLHTM_DrawSelfFrame(). KLHTM_MyData contains some non-integers created |
||
304 | -- by the heroic strike approximations - these are rounded off. |
||
305 | ------------------------------------------------------------------------------ |
||
306 | function KLHTM_GetSelfData() |
||
307 | |||
308 | local data = GetSelfData_data; |
||
309 | local totals = GetSelfData_totals; |
||
310 | |||
311 | if (data == nil) then |
||
312 | data = {}; |
||
313 | totals = {}; |
||
314 | for x = 1, 20 do -- 20: needs to be large enough to fit all the threat sources |
||
315 | -- that can be added to mod.table.mydata |
||
316 | data[x] = {["name"] = "", ["dam"] = 0, ["hits"] = 0, ["rage"] = 0, ["threat"] = 0, ["pc"] = 0,}; |
||
317 | end |
||
318 | end |
||
319 | |||
320 | local totalData = mod.table.mydata[mod.string.get("threatsource", "total")]; |
||
321 | totals.hits = totalData.hits; |
||
322 | totals.rage = math.floor(totalData.rage + 0.5); |
||
323 | -- nb different index names |
||
324 | totals.dam = math.floor(totalData.damage + 0.5); |
||
325 | totals.threat = math.floor(totalData.threat + 0.5); |
||
326 | |||
327 | -- copy the personal threat data |
||
328 | local rowCount = 0; |
||
329 | for ability, info in mod.table.mydata do |
||
330 | |||
331 | -- omit "zero rows" if necessary |
||
332 | if (not (options.self.hideZeroRows and (info.threat == 0))) then |
||
333 | if (ability ~= mod.string.get("threatsource", "total")) then |
||
334 | rowCount = rowCount + 1; |
||
335 | data[rowCount].name = ability; |
||
336 | data[rowCount].hits = info.hits; |
||
337 | data[rowCount].threat = math.floor(info.threat + 0.5); |
||
338 | data[rowCount].dam = math.floor(info.damage + 0.5); |
||
339 | data[rowCount].rage = math.floor(info.rage + 0.5); |
||
340 | if (totals.threat == 0) then |
||
341 | data[rowCount].pc = 0; |
||
342 | else |
||
343 | data[rowCount].pc = math.floor(data[rowCount].threat / totals.threat * 100); |
||
344 | end |
||
345 | end |
||
346 | end |
||
347 | end |
||
348 | table.setn(data, rowCount); |
||
349 | |||
350 | return data, totals |
||
351 | end |
||
352 | |||
353 | |||
354 | ------------------------------------------------------------------------------ |
||
355 | -- Updates the personal threat details. If the frame is minimised, only a |
||
356 | -- brief call to KLHTM_DrawSelfStrings() is made. Otherwise a copy of |
||
357 | -- KLHTM_MyData is made, processed, and used to redraw all the strings in the |
||
358 | -- Self frame. |
||
359 | ------------------------------------------------------------------------------ |
||
360 | function KLHTM_DrawSelfFrame() |
||
361 | |||
362 | if (state.min) then |
||
363 | KLHTM_DrawSelfStrings(); |
||
364 | return; |
||
365 | end |
||
366 | |||
367 | local data, totals = KLHTM_GetSelfData(); |
||
368 | |||
369 | table.sort(data, KLHTM_CompareSelfRows); |
||
370 | |||
371 | -- bar reference value |
||
372 | local barRef; |
||
373 | |||
374 | if (table.getn(data) == 0) then |
||
375 | barRef = 0; |
||
376 | elseif (options.self.sortDown) then |
||
377 | -- set the largest value to 100% |
||
378 | barRef = data[table.getn(data)][options.self.sortColumn]; |
||
379 | else |
||
380 | barRef = data[1][options.self.sortColumn]; |
||
381 | end |
||
382 | |||
383 | if ((barRef == nil) or (tonumber(barRef) == nil)) then |
||
384 | barRef = 0; |
||
385 | else |
||
386 | barRef = sizes.self.x / barRef; |
||
387 | end |
||
388 | |||
389 | local barColours = gui.self.head[options.self.sortColumn].colour; |
||
390 | |||
391 | -- truncate excess rows |
||
392 | if (table.getn(data) > Max_Rows) then |
||
393 | table.setn(data, Max_Rows); |
||
394 | end |
||
395 | |||
396 | -- render the rows |
||
397 | for row = 1, table.getn(data) do |
||
398 | for index, value in data[row] do |
||
399 | if (options.self.abbreviate) then |
||
400 | gui.self.rows[row][index].text:SetText(KLHTM_Abbreviate(value)); |
||
401 | else |
||
402 | gui.self.rows[row][index].text:SetText(value); |
||
403 | end |
||
404 | end |
||
405 | -- bars |
||
406 | if ((barRef == 0) or (data[row][options.self.sortColumn] == 0)) then |
||
407 | gui.self.rows[row].bar:Hide(); |
||
408 | else |
||
409 | gui.self.rows[row].bar:SetWidth(data[row][options.self.sortColumn] * barRef); |
||
410 | gui.self.rows[row].bar:SetVertexColor(barColours.r, barColours.g, barColours.b); |
||
411 | gui.self.rows[row].bar:Show(); |
||
412 | end |
||
413 | gui.self.rows[row].frame:Show(); |
||
414 | end |
||
415 | |||
416 | -- bottom bar |
||
417 | for index, string in gui.self.bottom.string do |
||
418 | if (totals[index] == 0) then |
||
419 | string.text:SetText(""); |
||
420 | else |
||
421 | if (options.self.abbreviate) then |
||
422 | string.text:SetText(KLHTM_Abbreviate(totals[index])); |
||
423 | else |
||
424 | string.text:SetText(totals[index]); |
||
425 | end |
||
426 | end |
||
427 | end |
||
428 | |||
429 | -- hide empty rows |
||
430 | for row = table.getn(data) + 1, Max_Rows do |
||
431 | gui.self.rows[row].frame:Hide(); |
||
432 | end |
||
433 | |||
434 | -- resize the frame |
||
435 | sizes.self.y = heights.header + heights.button + heights.data * table.getn(data); |
||
436 | -- 5 * 2 insets. 1 title gap, 1 row gap, 1 bottom gap |
||
437 | sizes.frame.y = sizes.self.y + sizes.title.y + 13; |
||
438 | gui.frame:SetHeight(sizes.frame.y); |
||
439 | end |
||
440 | |||
441 | |||
442 | ------------------------------------------------------------------------------ |
||
443 | -- Sorts two rows in the self data table according to the sorting options |
||
444 | ------------------------------------------------------------------------------ |
||
445 | function KLHTM_CompareSelfRows(row1, row2) |
||
446 | |||
447 | -- todo: confirm the sign of these comparisoms |
||
448 | if (options.self.sortDown) then |
||
449 | return row1[options.self.sortColumn] < row2[options.self.sortColumn]; |
||
450 | else |
||
451 | return row1[options.self.sortColumn] > row2[options.self.sortColumn]; |
||
452 | end |
||
453 | end |
||
454 | |||
455 | |||
456 | ------------------------------------------------------------------------------ |
||
457 | -- Changes the column used to sort personal threat data |
||
458 | ------------------------------------------------------------------------------ |
||
459 | function KLHTM_SelfHeader_OnClick(name) |
||
460 | |||
461 | if (name == nil) then |
||
462 | if mod.out.checktrace("warning", me, "invalidargument") then |
||
463 | mod.out.printtrace(string.format("The argument '%s' to SelfHeader_OnClick is unrecognised.", tostring(name))); |
||
464 | end |
||
465 | |||
466 | return; |
||
467 | end |
||
468 | |||
469 | if (name == "pc") then |
||
470 | -- due to a quirk in the way percent threat is calculated, sorting by threat |
||
471 | -- should be done via the threat column (see DrawSelfFrame()) |
||
472 | name = "threat"; |
||
473 | end |
||
474 | |||
475 | if (options.self.sortColumn == name) then |
||
476 | options.self.sortDown = not options.self.sortDown; |
||
477 | else |
||
478 | options.self.sortColumn = name; |
||
479 | options.self.sortDown = true; |
||
480 | end |
||
481 | |||
482 | KLHTM_Redraw(true); |
||
483 | end |
||
484 | |||
485 | ------------------------------------------------------------------------------ |
||
486 | -- Called when the used clicks a button in the self view |
||
487 | ------------------------------------------------------------------------------ |
||
488 | function KLHTM_SelfButton_OnClick(name) |
||
489 | |||
490 | if (name == "reset") then |
||
491 | mod.table.resetmytable(); |
||
492 | KLHTM_Redraw(true); |
||
493 | else |
||
494 | if mod.out.checktrace("warning", me, "invalidargument") then |
||
495 | mod.out.printtrace(string.format("The argument '%s' to SelfButton_OnClick is unrecognised.", tostring(name))); |
||
496 | end |
||
497 | end |
||
498 | end |