vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[ KLH Threatmeter KLHTM_Gui.lua
2  
3 local version -> to be integrated into kenco files
4  
5 Lukon Mod 2: The replacement for Gui.lua and Tables.lua. Controls the GUI
6 for KLH ThreatMeter, along with KTM_Frame.xml, KTM_RaidGui.lua,
7 KTM_SelfGui.lua, KTM_TitleGui.lua.
8  
9 There is a single main frame, which shows either raid threat data
10 (KTM_RaidGui.lua) or personal threat details (KTM_SelfGui.lua). Each of
11 these two frames contains column headers, data rows and a bottom bar.
12 Additionally there is a title bar frame (KTM_TitleGui). The GUI elements are
13 contained in a table "KLHTM_Gui", populated by the CreateGuiTable(). This
14 also contains some properties not set in the XML file, such as some
15 dimensions and colours.
16  
17 The appearance of the GUI can be customised via the "KLHTM_GuiOptions" table.
18 This determines the visibility of command buttons and data columns, the way
19 data is displayed in the main tables, and other settings. User access to
20 these settings is via the options gui (KTM_OptionsFrame.xml,
21 KTM_OptionsGui.lua).
22  
23 The visibility and some properties of the frame as a whole are kept in the
24 "state" table.
25  
26 NB the indexes in the tables "gui", "options", and elsewhere shouldn't be
27 changed. They are assumed to match by various methods.
28 --]]
29  
30 -- ken
31 local mod = klhtm
32 local me = { }
33 mod.gui = me
34  
35 -- The minimum period, in seconds, between data table redrawing
36 local Min_Redraw_Period = 0.2;
37  
38 -- When the table was last redrawn
39 local lastRedraw = 0;
40 KLHTM_LastRedraw = lastRedraw;
41  
42 -- Set to true when a redraw has been requested but not yet performed
43 local needsRedraw = false;
44 KLHTM_NeedsRedraw = needsRedraw;
45  
46 -- Contains settings describing the way raid and self data should displayed.
47 -- Loaded from saved variables or initialised by _CreateDefaultOptions()
48 local options = {};
49 KLHTM_GuiOptions = options;
50  
51 -- Contains window state information. Loaded from saved variables or
52 -- initialised by _CreateDefaultState().
53 local state = {};
54 KLHTM_GuiState = state;
55  
56 -- Contains references to the GUI components and some additional properties
57 -- not specified in the XML file. Initialised by _CreateGuiTable()
58 local gui = {};
59 KLHTM_Gui = gui;
60  
61 -- true if initialisation (SetupGui) has finished, and rendering is possible
62 local isInitialised = false;
63 KLHTM_IsLoaded = false;
64  
65 -- the maximum change in string width allowed by KLHTM_LocaliseStringWidth()
66 local Max_Localisation_Factor = 2;
67  
68 -- frame scale constants
69 KLHTM_Scale = {["min"] = 0.6, ["max"] = 1.3, ["tick"] = 0.02, ["default"] = 1.0};
70  
71 -- the current dimensions of the frame and subframes. These are filled by the various
72 -- UpdateXFrame and Redraw methods.
73 local sizes = { ["raid"] = {}, ["self"] = {}, ["but"] = {},
74 ["string"] = {}, ["title"] = {}, ["frame"] = {}, };
75 KLHTM_GuiSizes = sizes;
76  
77 -- The heights of some sizes
78 KLHTM_GuiHeights = {["button"] = 15, ["string"] = 12, ["header"] = 14, ["data"] = 12};
79  
80 -- The texture gradient colours used by the main frame and options frame's title bar
81 KLHTM_TitleBarColours = {
82 -- purple
83 ["raid"] = {["minR"] = 0.2, ["minG"] = 0.0, ["minB"] = 0.2, ["minA"] = 0.5,
84 ["maxR"] = 1.0, ["maxG"] = 0.0, ["maxB"] = 1.0, ["maxA"] = 0.5, },
85 -- dark green
86 ["self"] = {["minR"] = 0.0, ["minG"] = 0.2, ["minB"] = 0.0, ["minA"] = 0.5,
87 ["maxR"] = 0.0, ["maxG"] = 0.7, ["maxB"] = 0.0, ["maxA"] = 0.5, },
88 -- blue
89 ["gen"] = {["minR"] = 0.0, ["minG"] = 0.2, ["minB"] = 0.2, ["minA"] = 0.5,
90 ["maxR"] = 0.0, ["maxG"] = 1.0, ["maxB"] = 1.0, ["maxA"] = 0.5, }, };
91  
92 ------------------------------------------------------------------------------
93 -- Prepares the GUI for use. Called after the Variables Loaded event is
94 -- received (see KLHTM_Frame <OnLoad>, <OnEvent>).
95 --
96 -- some patches to official KLHTM: (a) removed setupaftervariablesloaded
97 -- method, and its reference in ktmMain. (b) events now come from the update
98 -- frame. The main frame only sends the variables loaded event.
99 ------------------------------------------------------------------------------
100  
101 me.myevents = { "ADDON_LOADED" }
102  
103 me.onevent = function()
104  
105 KLHTM_SetupGui()
106  
107 end
108  
109 me.onupdate = function()
110  
111 KLHTM_Redraw()
112  
113 end
114  
115 function KLHTM_SetupGui()
116  
117 if (isInitialised) then
118 -- will this ever happen? who knows...
119 return;
120 end
121  
122 KLHTM_LoadVariables();
123 KLHTM_CreateGuiTable();
124 KLHTM_SetupGuiComponents();
125  
126 -- apply state and options
127 KLHTM_UpdateSelfFrame();
128 KLHTM_UpdateRaidFrame();
129 KLHTM_UpdateTitleButtons();
130 KLHTM_UpdateTitleStrings();
131  
132 isInitialised = true;
133  
134 -- probably going to cause some dumb error...
135 KLHTM_Redraw(true);
136  
137 KLHTM_UpdateFrame();
138 KLHTM_SetGuiScale(options.scale);
139  
140 if (state.closed ~= true) then
141 gui.frame:Show();
142 end
143 end
144  
145  
146 ------------------------------------------------------------------------------
147 -- Loads data from saved variables. If the required data is not found, default
148 -- settings are used instead.
149 ------------------------------------------------------------------------------
150 function KLHTM_LoadVariables()
151  
152 if (KLHTM_SavedVariables == nil) then
153 KLHTM_SavedVariables = {};
154 end
155  
156 if (KLHTM_SavedVariables.gui) then
157  
158 if mod.out.checktrace("info", me, "savedvariables") then
159 mod.out.printtrace("Loading KTM saved variables");
160 end
161  
162 -- nb byval copies to preserve external references to KLHTM_GuiState and
163 -- KLHTM_GuiOptions. Todo: more robust format
164 for index, value in KLHTM_SavedVariables.gui.state do
165 state[index] = value;
166 end
167 for index, value in KLHTM_SavedVariables.gui.options do
168 options[index] = value;
169 end
170  
171 -- todo: better saved variables upgrading
172 if (options.buttonVis.min.targ == nil) then
173 options.buttonVis.min.targ = false;
174 options.buttonVis.max.targ = true;
175 end
176 if (options.buttonVis.min.clear == nil) then
177 options.buttonVis.min.clear = false;
178 options.buttonVis.max.clear = false;
179 end
180 else
181 if mod.out.checktrace("info", me, "savedvariables") then
182 mod.out.printtrace("Performing fresh install of KTM")
183 end
184  
185 KLHTM_SetDefaultOptions();
186 KLHTM_SetDefaultState();
187 end
188  
189 KLHTM_SavedVariables.gui = {};
190 KLHTM_SavedVariables.gui.version = mod.build;
191 KLHTM_SavedVariables.gui.options = options;
192 KLHTM_SavedVariables.gui.state = state;
193 end
194  
195  
196 ------------------------------------------------------------------------------
197 -- Sets up the variable "KLHTM_Gui". It contains (a) references to the gui
198 -- components, (b) visibility data, (c) string widths, (d) some colours.
199 ------------------------------------------------------------------------------
200 function KLHTM_CreateGuiTable()
201  
202 gui.frame = KLHTM_Frame;
203 gui.topdiv = KLHTM_FrameTopDivider;
204 gui.bottomdiv = KLHTM_FrameBottomDivider;
205  
206 KLHTM_CreateTitleTable();
207 KLHTM_CreateRaidTable();
208 KLHTM_CreateSelfTable();
209 KLHTM_CreateOptionsTable();
210 end
211  
212  
213 ------------------------------------------------------------------------------
214 -- Applies Gui component properties that are not specified in the XML.
215 ------------------------------------------------------------------------------
216 function KLHTM_SetupGuiComponents()
217  
218 KLHTM_SetupTitleGui();
219 KLHTM_SetupRaidGui();
220 KLHTM_SetupSelfGui();
221 KLHTM_SetupOptionsGui();
222  
223 -- frame
224 gui.frame:RegisterForDrag("LeftButton");
225 gui.frame:SetMovable(true);
226 gui.frame:SetUserPlaced(true);
227 gui.frame:SetBackdropColor(0.1, 0.1, 0.1);
228 gui.frame:SetBackdropBorderColor(1, 1, 1);
229 end
230  
231  
232 ------------------------------------------------------------------------------
233 -- Initialises the local variable "options". It contains settings describing
234 -- how the self and data windows and the title bar should be displayed.
235 ------------------------------------------------------------------------------
236 function KLHTM_SetDefaultOptions()
237  
238 options.scale = KLHTM_Scale.default;
239  
240 KLHTM_SetDefaultRaidOptions();
241 KLHTM_SetDefaultSelfOptions();
242 KLHTM_SetDefaultTitleOptions();
243 end
244  
245  
246 ------------------------------------------------------------------------------
247 -- Initialises the local variable "state", containing GUI display properties
248 ------------------------------------------------------------------------------
249 function KLHTM_SetDefaultState()
250  
251 state.min = false;
252 state.max = true;
253 state.minmax = "max";
254  
255 state.raid = true;
256 state.self = false;
257 state.view = "raid";
258  
259 state.pinned = false;
260 state.closed = false;
261 end
262  
263  
264 ------------------------------------------------------------------------------
265 -- Changes the raid \ self view.
266 --
267 -- [newView] - either "self" or "raid"
268 ------------------------------------------------------------------------------
269 function KLHTM_SetView(newView)
270  
271 if ((newView ~= "self") and (newView ~= "raid")) then
272 if mod.out.checktrace("warning", me, "invalidargument") then
273 mod.out.printtrace(string.format("The argument '%s' to SetView is not recognised.", tostring(newView)))
274 end
275 return;
276 end
277  
278 state.view = newView;
279 state.raid = not state.raid;
280 state.self = not state.self;
281  
282 KLHTM_Redraw(true);
283  
284 local col = KLHTM_TitleBarColours[state.view];
285 gui.title.back:SetGradientAlpha("VERTICAL", col.minR, col.minG, col.minB, col.minA, col.maxR, col.maxG, col.maxB, col.maxA);
286  
287 KLHTM_UpdateTitleButtons();
288 KLHTM_UpdateTitleStrings();
289 KLHTM_UpdateFrame();
290 end
291  
292  
293 ------------------------------------------------------------------------------
294 -- Changes the minimised \ maximised state.
295 --
296 -- [newMinMax] - either "min" or "max"
297 ------------------------------------------------------------------------------
298 function KLHTM_SetMinMax(newMinMax)
299  
300 if ((newMinMax ~= "min") and (newMinMax ~= "max")) then
301 if mod.out.checktrace("warning", me, "invalidargument") then
302 mod.out.printtrace(string.format("The argument '%s' to SetMinMax is not recognised.", tostring(newMinMax)))
303 end
304 return;
305 end
306  
307 state.minmax = newMinMax;
308 state.min = not state.min;
309 state.max = not state.max;
310  
311 KLHTM_Redraw(true);
312  
313 KLHTM_UpdateTitleButtons();
314 KLHTM_UpdateTitleStrings();
315 KLHTM_UpdateFrame();
316 end
317  
318  
319 ------------------------------------------------------------------------------
320 -- Sets the frame visibility. Should this method even exist?
321 --
322 -- [newVisible] - set to true to show the frame
323 ------------------------------------------------------------------------------
324 function KLHTM_SetVisible(newVisible)
325  
326 if ((newVisible ~= false) and (newVisible ~= true)) then
327 if mod.out.checktrace("warning", me, "invalidargument") then
328 mod.out.printtrace(string.format("The argument to '%s' to SetVisible is not recognised.", tostring(newVisible)))
329 end
330 end
331  
332 state.closed = not newVisible;
333  
334 if (newVisible) then
335 KLHTM_Redraw(true);
336 gui.frame:Show();
337 else
338 gui.frame:Hide();
339 end
340 end
341  
342  
343 ------------------------------------------------------------------------------
344 -- Changes the global scale of the main frame.
345 --
346 -- [newScale] - a value between 0.5 and 1.2
347 ------------------------------------------------------------------------------
348 function KLHTM_SetGuiScale(newScale)
349  
350 local argument = newScale
351 newScale = tonumber(newScale);
352  
353 if (newScale == nil) then
354 if mod.out.checktrace("warning", me, "invalidargument") then
355 mod.out.printtrace(string.format("The argument '%s' to SetGuiScale is not a number.", tostring(argument)))
356 end
357 return;
358 end
359  
360 if ((newScale < KLHTM_Scale.min) or (newScale > KLHTM_Scale.max)) then
361 if mod.out.checktrace("warning", me, "invalidargument") then
362 mod.out.printtrace(string.format("The argument '%s' to SetGuiScale is outside the valid bounds.", newScale))
363 end
364 return;
365 end
366  
367 -- maintain the top-right corner when resizing
368 local right = gui.frame:GetRight();
369 local top = gui.frame:GetTop();
370  
371 gui.frame:SetScale(newScale);
372  
373 if ((top ~= nil) and (right ~= nil)) then
374 top = top * options.scale / newScale;
375 right = right * options.scale / newScale;
376 gui.frame:ClearAllPoints();
377 gui.frame:SetPoint("TOPRIGHT", UIParent, "BOTTOMLEFT", right, top);
378 else
379 -- should occur the first time the mod is loaded
380 gui.frame:ClearAllPoints();
381 gui.frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
382 end
383  
384 options.scale = newScale;
385 end
386  
387  
388 ------------------------------------------------------------------------------
389 -- Add an black outline to the specified FontString. This increases its
390 -- legibility when viewed on a light background, eg the self view bars. It is
391 -- normally able to be specified via the XML, but this is not possible due to
392 -- compatibility reasons with some localisations. The default minimim ouline is
393 -- very dark and overpowering, so the shadow's alpha is reduced (to 0, but there
394 -- seems to be a minimum value).
395 --
396 -- [fontstring] - a GUI FontString object
397 ------------------------------------------------------------------------------
398 function KLHTM_AddOutline(fontstring)
399  
400 local path, height;
401 path, height = fontstring:GetFont();
402  
403 fontstring:SetFont(path, height, "OUTLINE");
404 fontstring:SetShadowColor(0,0,0,0.3);
405 end
406  
407  
408 ------------------------------------------------------------------------------
409 -- Increases the space allocated to the specified string to match the localised
410 -- string width.
411 --
412 -- [stringData] - a table containing a [width] element and a [text] element.
413 -- the latter should be a FontString whose text has been set.
414 ------------------------------------------------------------------------------
415 function KLHTM_LocaliseStringWidth(stringData)
416  
417 local width = math.ceil(stringData.text:GetStringWidth());
418  
419 if (width > stringData.width) then
420  
421 local newValue = math.min(width, stringData.width * Max_Localisation_Factor);
422  
423 if mod.out.checktrace("info", me, "resizing") then
424 mod.out.printtrace(string.format("Extending the width of %s from %s to %s.", stringData.frame:GetName(), stringData.width, newValue))
425 end
426  
427 stringData.width = newValue;
428 end
429 end
430  
431 ------------------------------------------------------------------------------
432 -- Repositions the frame in the center of the screen and shows it.
433 ------------------------------------------------------------------------------
434 function KLHTM_ResetFrame()
435  
436 local scale = options.scale;
437  
438 gui.frame:SetScale(1);
439 gui.frame:ClearAllPoints();
440 gui.frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
441  
442 KLHTM_SetGuiScale(scale);
443 KLHTM_SetVisible(true);
444 end
445  
446 ------------------------------------------------------------------------------
447 -- Updates the visibility of the data tables, and recalculates the frame bounds.
448 -- Should be called whenever the sizes of the subframes changes. ie:
449 --
450 -- a) view changes
451 -- b) minmax changes
452 -- c) column visibility changes
453 -- d) command button visibility changes
454 --
455 -- This method uses the widths calculated by the other methods such as
456 -- Update...Table(), UpdateTitleButtons() and UpdateTitleStrings() which
457 -- should be called prior to this method when the affected gui components are
458 -- changed.
459 --
460 -- When the frame is maximised, its width is determined by the visible data frame.
461 -- If it is smaller than the title bar's preferred width, then overlap can
462 -- occur between the title bar strings and buttons. In this case the default
463 -- string is replaced by the short version.
464 ------------------------------------------------------------------------------
465 function KLHTM_UpdateFrame()
466  
467 -- raid frame
468 if (state.max and state.raid) then
469 sizes.frame.x = sizes.raid.x;
470 gui.raid.frame:Show();
471 -- height is set by the redraw method
472 else
473 gui.raid.frame:Hide();
474 end
475  
476 -- self frame
477 if (state.max and state.self) then
478 sizes.frame.x = sizes.self.x;
479 gui.self.frame:Show();
480 -- height is set by the redraw method
481 else
482 gui.self.frame:Hide();
483 end
484  
485 -- title frame
486 sizes.title.x = sizes.string.x + sizes.but.x;
487 sizes.title.y = math.max(sizes.string.y, sizes.but.y);
488 gui.title.frame:SetHeight(sizes.title.y);
489  
490 if (state.min) then
491 sizes.frame.y = sizes.title.y;
492 sizes.frame.x = sizes.title.x;
493 gui.frame:SetHeight(sizes.frame.y + 10); -- 10?
494 end
495  
496 -- maintain the top-right corner when resizing
497 local right = gui.frame:GetRight();
498 local top = gui.frame:GetTop();
499  
500 gui.frame:SetWidth(sizes.frame.x + 12); -- 12: 5 inset + 1 gap * 2 for each side.
501  
502 if ((top ~= nil) and (right ~= nil)) then
503 gui.frame:ClearAllPoints();
504 gui.frame:SetPoint("TOPRIGHT", UIParent, "BOTTOMLEFT", right, top);
505 else
506 -- harmless, occurs once at startup
507 end
508  
509 -- check for lack of horizontal space in the title bar
510 if (state.max) then
511 if (sizes.title.x > sizes.frame.x) then
512 gui.title.string.short.frame:SetWidth(gui.title.string.short.width);
513 gui.title.string.short.frame:Show();
514 gui.title.string.long.frame:SetWidth(0.1);
515 gui.title.string.long.frame:Hide();
516 else
517 gui.title.string.short.frame:SetWidth(0.1);
518 gui.title.string.short.frame:Hide();
519 gui.title.string.long.frame:SetWidth(gui.title.string.long.width);
520 gui.title.string.long.frame:Show();
521 end
522 -- sometimes the anchors aren't reapplied. Not sure if this is needed now
523 -- that the raid and self titles have been replaced
524 gui.title.string.short.frame:GetLeft();
525 gui.title.string.long.frame:GetLeft();
526 end
527  
528 end
529  
530  
531 ------------------------------------------------------------------------------
532 -- Notifies the GUI that the frame should be updated. The frame will be redrawn
533 -- within Min_Redraw_Period seconds. Should be called by data-receiving modules,
534 -- eg networking and combat.
535 --
536 -- [view] - set to "raid" or "self" to prevent redraw if it does not match
537 -- [state.view]. Ignored if nil.
538 ------------------------------------------------------------------------------
539 function KLHTM_RequestRedraw(view)
540  
541 if ((view ~= nil) and (view ~= state.view)) then
542 return;
543 end
544 if (state.closed) then
545 needsRedraw = false;
546 return;
547 end
548  
549 needsRedraw = true;
550 end
551  
552  
553 KLHTM_RedrawDebug = {0,0,0,0,0,0,0,0,0,0};
554 _redrawindex = 1;
555 ------------------------------------------------------------------------------
556 -- Redraws the data table contents, if needed. The redraw will only occur if:
557 --
558 -- a) [needsRedraw] is true. This is set by data-receiving modules via
559 -- KLHTM_RequestRedraw()
560 -- b) The last update was at least Min_Redraw_Period seconds ago
561 -- c) the frame is visible and initialised
562 --
563 -- This method is automatically called via KLHTM_GuiOnUpdate in order to track
564 -- processor usage. It should not be called externally; use KLHTM_RequestRedraw()
565 -- instead.
566 --
567 -- [forceRedraw] - if true, bypasses conditions (a) and (b) above. Use this
568 -- before updating GUI element visibility to prevent flickering.
569 ------------------------------------------------------------------------------
570 function KLHTM_Redraw(forceRedraw)
571  
572 if (not isInitialised) then
573 return;
574 end
575  
576 if (forceRedraw ~= true) then
577 if ((GetTime() - lastRedraw) < Min_Redraw_Period) then
578 return;
579 end
580 if (needsRedraw == false) then
581 return;
582 end
583 end
584  
585 -- some debugging to check frequency of redraws
586 KLHTM_RedrawDebug[_redrawindex] = GetTime() - lastRedraw;
587 _redrawindex = _redrawindex + 1;
588 if (_redrawindex == 11) then
589 _redrawindex = 1;
590 end
591  
592 lastRedraw = GetTime();
593 needsRedraw = false;
594  
595 -- draw stuff... ? NO!
596 if (state.raid) then
597 KLHTM_DrawRaidFrame();
598 else
599 KLHTM_DrawSelfFrame();
600 end
601 end
602  
603 ------------------------------------------------------------------------------
604 -- Abbreviates large number with the "k" suffix. Works on positive and negative
605 -- numbers. Non-numbers are returned as is.
606 ------------------------------------------------------------------------------
607 function KLHTM_Abbreviate(input)
608  
609 if (type(input) ~= "number") then
610 return input;
611 end
612  
613 local isNegative = false;
614 if (input < 0) then
615 isNegative = true;
616 input = -1 * input;
617 end
618  
619 local answer;
620 if (input < 10000) then
621 answer = input;
622 elseif (input < 100000) then
623 answer = math.floor(input / 100 + 0.5) / 10;
624 if (math.mod(answer, 1) == 0) then
625 answer = answer .. ".0";
626 end
627 answer = answer .. "k";
628 else
629 answer = math.floor(input / 1000 + 0.5) .. "k";
630 end
631  
632 if (isNegative) then
633 answer = "-" .. answer;
634 end
635 return answer;
636 end
637  
638  
639 ------------------------------------------------------------------------------
640 -- Frame dragging
641 ------------------------------------------------------------------------------
642 function KLHTM_Frame_OnDragStart()
643 if (gui.frame:IsMovable() and (state.pinned == false)) then
644 gui.frame:StartMoving();
645 end
646 end
647 function KLHTM_Frame_OnDragStop()
648 gui.frame:StopMovingOrSizing();
649 end