comparison Libs/DF/normal_bar.lua @ 11:2f09fe4be15c

Added an Options Panel.
author Tercio
date Mon, 20 Apr 2015 16:34:18 -0300
parents
children dc1c77254f80
comparison
equal deleted inserted replaced
10:f1e32be6773e 11:2f09fe4be15c
1
2
3
4 local DF = _G ["DetailsFramework"]
5 local _
6
7 local _rawset = rawset --> lua locals
8 local _rawget = rawget --> lua locals
9 local _setmetatable = setmetatable --> lua locals
10 local _unpack = unpack --> lua locals
11 local _type = type --> lua locals
12 local _math_floor = math.floor --> lua locals
13
14 local SharedMedia = LibStub:GetLibrary ("LibSharedMedia-3.0")
15
16 local cleanfunction = function() end
17 local BarMetaFunctions = {}
18 local APIBarFunctions
19
20 ------------------------------------------------------------------------------------------------------------
21 --> metatables
22
23 BarMetaFunctions.__call = function (_table, value)
24 if (not value) then
25 return _table.statusbar:GetValue()
26 else
27 return _table.statusbar:SetValue (value)
28 end
29 end
30
31 BarMetaFunctions.__add = function (v1, v2)
32 if (_type (v1) == "table") then
33 local v = v1.statusbar:GetValue()
34 v = v + v2
35 v1.statusbar:SetValue (v)
36 else
37 local v = v2.statusbar:GetValue()
38 v = v + v1
39 v2.statusbar:SetValue (v)
40 end
41 end
42
43 BarMetaFunctions.__sub = function (v1, v2)
44 if (_type (v1) == "table") then
45 local v = v1.statusbar:GetValue()
46 v = v - v2
47 v1.statusbar:SetValue (v)
48 else
49 local v = v2.statusbar:GetValue()
50 v = v - v1
51 v2.statusbar:SetValue (v)
52 end
53 end
54
55 ------------------------------------------------------------------------------------------------------------
56 --> members
57
58 --> tooltip
59 local function gmember_tooltip (_object)
60 return _object:GetTooltip()
61 end
62 --> shown
63 local gmember_shown = function (_object)
64 return _object.statusbar:IsShown()
65 end
66 --> frame width
67 local gmember_width = function (_object)
68 return _object.statusbar:GetWidth()
69 end
70 --> frame height
71 local gmember_height = function (_object)
72 return _object.statusbar:GetHeight()
73 end
74 --> value
75 local gmember_value = function (_object)
76 return _object.statusbar:GetValue()
77 end
78 --> right text
79 local gmember_rtext = function (_object)
80 return _object.textright:GetText()
81 end
82 --> left text
83 local gmember_ltext = function (_object)
84 return _object.textleft:GetText()
85 end
86 --> left color
87 local gmember_color = function (_object)
88 return _object._texture.original_colors
89 end
90 --> icon
91 local gmember_icon = function (_object)
92 return _object._icon:GetTexture()
93 end
94 --> texture
95 local gmember_texture = function (_object)
96 return _object._texture:GetTexture()
97 end
98 --> font size
99 local gmember_textsize = function (_object)
100 local _, fontsize = _object.textleft:GetFont()
101 return fontsize
102 end
103 --> font face
104 local gmember_textfont = function (_object)
105 local fontface = _object.textleft:GetFont()
106 return fontface
107 end
108 --> font color
109 local gmember_textcolor = function (_object)
110 return _object.textleft:GetTextColor()
111 end
112
113 local get_members_function_index = {
114 ["tooltip"] = gmember_tooltip,
115 ["shown"] = gmember_shown,
116 ["width"] = gmember_width,
117 ["height"] = gmember_height,
118 ["value"] = gmember_value,
119 ["lefttext"] = gmember_ltext,
120 ["righttext"] = gmember_rtext,
121 ["color"] = gmember_color,
122 ["icon"] = gmember_icon,
123 ["texture"] = gmember_texture,
124 ["fontsize"] = gmember_textsize,
125 ["fontface"] = gmember_textfont,
126 ["fontcolor"] = gmember_textcolor,
127 ["textsize"] = gmember_textsize, --alias
128 ["textfont"] = gmember_textfont, --alias
129 ["textcolor"] = gmember_textcolor --alias
130 }
131
132 BarMetaFunctions.__index = function (_table, _member_requested)
133
134 local func = get_members_function_index [_member_requested]
135 if (func) then
136 return func (_table, _member_requested)
137 end
138
139 local fromMe = _rawget (_table, _member_requested)
140 if (fromMe) then
141 return fromMe
142 end
143
144 return BarMetaFunctions [_member_requested]
145 end
146
147
148 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
149
150
151 --> tooltip
152 local smember_tooltip = function (_object, _value)
153 return _object:SetTooltip (_value)
154 end
155 --> show
156 local smember_shown = function (_object, _value)
157 if (_value) then
158 return _object:Show()
159 else
160 return _object:Hide()
161 end
162 end
163 --> hide
164 local smember_hide = function (_object, _value)
165 if (_value) then
166 return _object:Hide()
167 else
168 return _object:Show()
169 end
170 end
171 --> width
172 local smember_width = function (_object, _value)
173 return _object.statusbar:SetWidth (_value)
174 end
175 --> height
176 local smember_height = function (_object, _value)
177 return _object.statusbar:SetHeight (_value)
178 end
179 --> statusbar value
180 local smember_value = function (_object, _value)
181 _object.statusbar:SetValue (_value)
182 return _object.div:SetPoint ("left", _object.statusbar, "left", _value * (_object.statusbar:GetWidth()/100) - 16, 0)
183 end
184 --> right text
185 local smember_rtext = function (_object, _value)
186 return _object.textright:SetText (_value)
187 end
188 --> left text
189 local smember_ltext = function (_object, _value)
190 return _object.textleft:SetText (_value)
191 end
192 --> color
193 local smember_color = function (_object, _value)
194 local _value1, _value2, _value3, _value4 = DF:ParseColors (_value)
195
196 _object.statusbar:SetStatusBarColor (_value1, _value2, _value3, _value4)
197 _object._texture.original_colors = {_value1, _value2, _value3, _value4}
198 return _object._texture:SetVertexColor (_value1, _value2, _value3, _value4)
199 end
200 --> icon
201 local smember_icon = function (_object, _value)
202 if (type (_value) == "table") then
203 local _value1, _value2 = _unpack (_value)
204 _object._icon:SetTexture (_value1)
205 if (_value2) then
206 _object._icon:SetTexCoord (_unpack (_value2))
207 end
208 else
209 _object._icon:SetTexture (_value)
210 end
211 return
212 end
213 --> texture
214 local smember_texture = function (_object, _value)
215 if (type (_value) == "table") then
216 local _value1, _value2 = _unpack (_value)
217 _object._texture:SetTexture (_value1)
218 if (_value2) then
219 _object._texture:SetTexCoord (_unpack (_value2))
220 end
221 else
222 if (_value:find ("\\")) then
223 _object._texture:SetTexture (_value)
224 else
225 local file = SharedMedia:Fetch ("statusbar", _value)
226 if (file) then
227 _object._texture:SetTexture (file)
228 else
229 _object._texture:SetTexture (_value)
230 end
231 end
232 end
233 return
234 end
235 --> font face
236 local smember_textfont = function (_object, _value)
237 DF:SetFontFace (_object.textleft, _value)
238 return DF:SetFontFace (_object.textright, _value)
239 end
240 --> font size
241 local smember_textsize = function (_object, _value)
242 DF:SetFontSize (_object.textleft, _value)
243 return DF:SetFontSize (_object.textright, _value)
244 end
245 --> font color
246 local smember_textcolor = function (_object, _value)
247 local _value1, _value2, _value3, _value4 = DF:ParseColors (_value)
248 _object.textleft:SetTextColor (_value1, _value2, _value3, _value4)
249 return _object.textright:SetTextColor (_value1, _value2, _value3, _value4)
250 end
251 --> outline (shadow)
252 local smember_outline = function (_object, _value)
253 DF:SetFontOutline (_object.textleft, _value)
254 return DF:SetFontOutline (_object.textright, _value)
255 end
256
257 local set_members_function_index = {
258 ["tooltip"] = smember_tooltip,
259 ["shown"] = smember_shown,
260 ["width"] = smember_width,
261 ["height"] = smember_height,
262 ["value"] = smember_value,
263 ["righttext"] = smember_rtext,
264 ["lefttext"] = smember_ltext,
265 ["color"] = smember_color,
266 ["icon"] = smember_icon,
267 ["texture"] = smember_texture,
268 ["fontsize"] = smember_textsize,
269 ["fontface"] = smember_textfont,
270 ["fontcolor"] = smember_textcolor,
271 ["textsize"] = smember_textsize, --alias
272 ["textfont"] = smember_textfont, --alias
273 ["textcolor"] = smember_textcolor, --alias
274 ["shadow"] = smember_outline,
275 ["outline"] = smember_outline, --alias
276 }
277
278 BarMetaFunctions.__newindex = function (_table, _key, _value)
279
280 local func = set_members_function_index [_key]
281 if (func) then
282 return func (_table, _value)
283 else
284 return _rawset (_table, _key, _value)
285 end
286 end
287
288 ------------------------------------------------------------------------------------------------------------
289 --> methods
290
291 --> show & hide
292 function BarMetaFunctions:Show()
293 self.statusbar:Show()
294 end
295 function BarMetaFunctions:Hide()
296 self.statusbar:Hide()
297 end
298
299 --> set value (status bar)
300 function BarMetaFunctions:SetValue (value)
301 if (not value) then
302 value = 0
303 end
304 self.statusbar:SetValue (value)
305 self.div:SetPoint ("left", self.statusbar, "left", value * (self.statusbar:GetWidth()/100) - 16, 0)
306 end
307
308 --> set point
309 function BarMetaFunctions:SetPoint (v1, v2, v3, v4, v5)
310 v1, v2, v3, v4, v5 = DF:CheckPoints (v1, v2, v3, v4, v5, self)
311 if (not v1) then
312 print ("Invalid parameter for SetPoint")
313 return
314 end
315 return self.widget:SetPoint (v1, v2, v3, v4, v5)
316 end
317
318 --> set sizes
319 function BarMetaFunctions:SetSize (w, h)
320 if (w) then
321 self.statusbar:SetWidth (w)
322 end
323 if (h) then
324 self.statusbar:SetHeight (h)
325 end
326 end
327
328 --> set texture
329 function BarMetaFunctions:SetTexture (texture)
330 self._texture:SetTexture (texture)
331 end
332
333 --> set texts
334 function BarMetaFunctions:SetLeftText (text)
335 self.textleft:SetText (text)
336 end
337 function BarMetaFunctions:SetRightText (text)
338 self.textright:SetText (text)
339 end
340
341 --> set color
342 function BarMetaFunctions:SetColor (r, g, b, a)
343 r, g, b, a = DF:ParseColors (r, g, b, a)
344
345 self._texture:SetVertexColor (r, g, b, a)
346 self.statusbar:SetStatusBarColor (r, g, b, a)
347 self._texture.original_colors = {r, g, b, a}
348 end
349
350 --> set icons
351 function BarMetaFunctions:SetIcon (texture, ...)
352 self._icon:SetTexture (texture)
353 if (...) then
354 local L, R, U, D = _unpack (...)
355 self._icon:SetTexCoord (L, R, U, D)
356 end
357 end
358
359 --> show div
360 function BarMetaFunctions:ShowDiv (bool)
361 if (bool) then
362 self.div:Show()
363 else
364 self.div:Hide()
365 end
366 end
367
368 -- tooltip
369 function BarMetaFunctions:SetTooltip (tooltip)
370 if (tooltip) then
371 return _rawset (self, "have_tooltip", tooltip)
372 else
373 return _rawset (self, "have_tooltip", nil)
374 end
375 end
376 function BarMetaFunctions:GetTooltip()
377 return _rawget (self, "have_tooltip")
378 end
379
380 -- frame levels
381 function BarMetaFunctions:GetFrameLevel()
382 return self.statusbar:GetFrameLevel()
383 end
384 function BarMetaFunctions:SetFrameLevel (level, frame)
385 if (not frame) then
386 return self.statusbar:SetFrameLevel (level)
387 else
388 local framelevel = frame:GetFrameLevel (frame) + level
389 return self.statusbar:SetFrameLevel (framelevel)
390 end
391 end
392
393 -- frame stratas
394 function BarMetaFunctions:SetFrameStrata()
395 return self.statusbar:GetFrameStrata()
396 end
397 function BarMetaFunctions:SetFrameStrata (strata)
398 if (_type (strata) == "table") then
399 self.statusbar:SetFrameStrata (strata:GetFrameStrata())
400 else
401 self.statusbar:SetFrameStrata (strata)
402 end
403 end
404
405 --> container
406 function BarMetaFunctions:SetContainer (container)
407 self.container = container
408 end
409
410 --> hooks
411 function BarMetaFunctions:SetHook (hookType, func)
412 if (func) then
413 _rawset (self, hookType.."Hook", func)
414 else
415 _rawset (self, hookType.."Hook", nil)
416 end
417 end
418
419 ------------------------------------------------------------------------------------------------------------
420 --> scripts
421
422 local OnEnter = function (frame)
423 if (frame.MyObject.OnEnterHook) then
424 local interrupt = frame.MyObject.OnEnterHook (frame, frame.MyObject)
425 if (interrupt) then
426 return
427 end
428 end
429
430 frame.MyObject.background:Show()
431
432 if (frame.MyObject.have_tooltip) then
433 GameCooltip2:Reset()
434 GameCooltip2:AddLine (frame.MyObject.have_tooltip)
435 GameCooltip2:ShowCooltip (frame, "tooltip")
436 end
437
438 end
439
440 local OnLeave = function (frame)
441 if (frame.MyObject.OnLeaveHook) then
442 local interrupt = frame.MyObject.OnLeaveHook (frame)
443 if (interrupt) then
444 return
445 end
446 end
447
448 if (frame.MyObject.have_tooltip) then
449 GameCooltip2:ShowMe (false)
450 end
451 end
452
453 local OnHide = function (frame)
454 if (frame.MyObject.OnHideHook) then
455 local interrupt = frame.MyObject.OnHideHook (frame)
456 if (interrupt) then
457 return
458 end
459 end
460 end
461
462 local OnShow = function (frame)
463 if (frame.MyObject.OnShowHook) then
464 local interrupt = frame.MyObject.OnShowHook (frame)
465 if (interrupt) then
466 return
467 end
468 end
469 end
470
471 local OnMouseDown = function (frame, button)
472 if (frame.MyObject.OnMouseDownHook) then
473 local interrupt = frame.MyObject.OnMouseDownHook (frame, button)
474 if (interrupt) then
475 return
476 end
477 end
478
479 if (not frame.MyObject.container.isLocked and frame.MyObject.container:IsMovable()) then
480 if (not frame.isLocked and frame:IsMovable()) then
481 frame.MyObject.container.isMoving = true
482 frame.MyObject.container:StartMoving()
483 end
484 end
485 end
486
487 local OnMouseUp = function (frame, button)
488 if (frame.MyObject.OnMouseUpHook) then
489 local interrupt = frame.MyObject.OnMouseUpHook (frame, button)
490 if (interrupt) then
491 return
492 end
493 end
494
495 if (frame.MyObject.container.isMoving) then
496 frame.MyObject.container:StopMovingOrSizing()
497 frame.MyObject.container.isMoving = false
498 end
499 end
500
501 ------------------------------------------------------------------------------------------------------------
502 --> timer
503
504 function BarMetaFunctions:OnTimerEnd()
505 if (self.OnTimerEndHook) then
506 local interrupt = self.OnTimerEndHook()
507 if (interrupt) then
508 return
509 end
510 end
511 self.timer_texture:Hide()
512 self.div_timer:Hide()
513 self:Hide()
514 self.timer = false
515 end
516
517 local OnUpdate = function (self, elapsed)
518 local timepct = (elapsed / self.tempo) * 100
519 self.c = self.c - (timepct*self.width/100)
520 self.remaining = self.remaining - elapsed
521 self.righttext:SetText (_math_floor (self.remaining))
522 self.timertexture:SetWidth (self.c)
523 if (self.c < 1) then
524 self:SetScript ("OnUpdate", nil)
525 self.MyObject:OnTimerEnd()
526 end
527 end
528
529 function BarMetaFunctions:SetTimer (tempo)
530
531 self.statusbar.width = self.statusbar:GetWidth()
532 self.statusbar.tempo = tempo
533 self.statusbar.remaining = tempo
534 self.statusbar.c = self.statusbar.width
535
536 self.timer_texture:Show()
537 self.timer_texture:SetWidth (self.statusbar.width)
538 self.statusbar.t = self.timer_texture
539 self (1)
540
541 self.div_timer:Show()
542 self.background:Show()
543
544 self.timer = true
545
546 self.statusbar:SetScript ("OnUpdate", OnUpdate)
547 end
548
549 ------------------------------------------------------------------------------------------------------------
550 --> object constructor
551
552 function DetailsFrameworkNormalBar_OnCreate (self)
553 self.texture.original_colors = {1, 1, 1, 1}
554 self.background.original_colors = {.3, .3, .3, .3}
555 self.timertexture.original_colors = {.3, .3, .3, .3}
556 return true
557 end
558
559 function DF:CreateBar (parent, texture, w, h, value, member, name)
560 return DF:NewBar (parent, parent, name, member, w, h, value, texture)
561 end
562
563 function DF:NewBar (parent, container, name, member, w, h, value, texture_name)
564
565 if (not name) then
566 name = "DetailsFrameworkBarNumber" .. DF.BarNameCounter
567 DF.BarNameCounter = DF.BarNameCounter + 1
568
569 elseif (not parent) then
570 return nil
571 elseif (not container) then
572 container = parent
573 end
574
575 if (name:find ("$parent")) then
576 name = name:gsub ("$parent", parent:GetName())
577 end
578
579 local BarObject = {type = "bar", dframework = true}
580
581 if (member) then
582 parent [member] = BarObject
583 end
584
585 if (parent.dframework) then
586 parent = parent.widget
587 end
588 if (container.dframework) then
589 container = container.widget
590 end
591
592 value = value or 0
593 w = w or 150
594 h = h or 14
595
596 --> default members:
597 --> hooks
598 BarObject.OnEnterHook = nil
599 BarObject.OnLeaveHook = nil
600 BarObject.OnHideHook = nil
601 BarObject.OnShowHook = nil
602 BarObject.OnMouseDownHook = nil
603 BarObject.OnMouseUpHook = nil
604 BarObject.OnTimerEndHook = nil
605 --> misc
606 BarObject.tooltip = nil
607 BarObject.locked = false
608 BarObject.have_tooltip = nil
609
610 BarObject.container = container
611
612 --> create widgets
613 BarObject.statusbar = CreateFrame ("statusbar", name, parent, "DetailsFrameworkNormalBarTemplate")
614 BarObject.widget = BarObject.statusbar
615
616 if (not APIBarFunctions) then
617 APIBarFunctions = true
618 local idx = getmetatable (BarObject.statusbar).__index
619 for funcName, funcAddress in pairs (idx) do
620 if (not BarMetaFunctions [funcName]) then
621 BarMetaFunctions [funcName] = function (object, ...)
622 local x = loadstring ( "return _G."..object.statusbar:GetName()..":"..funcName.."(...)")
623 return x (...)
624 end
625 end
626 end
627 end
628
629 BarObject.statusbar:SetHeight (h)
630 BarObject.statusbar:SetWidth (w)
631 BarObject.statusbar:SetFrameLevel (parent:GetFrameLevel()+1)
632 BarObject.statusbar:SetMinMaxValues (0, 100)
633 BarObject.statusbar:SetValue (value or 50)
634 BarObject.statusbar.MyObject = BarObject
635
636 BarObject.timer_texture = _G [name .. "_timerTexture"]
637 BarObject.timer_texture:SetWidth (w)
638 BarObject.timer_texture:SetHeight (h)
639
640 BarObject._texture = _G [name .. "_statusbarTexture"]
641 BarObject.background = _G [name .. "_background"]
642 BarObject._icon = _G [name .. "_icon"]
643 BarObject.textleft = _G [name .. "_TextLeft"]
644 BarObject.textright = _G [name .. "_TextRight"]
645 BarObject.div = _G [name .. "_sparkMouseover"]
646 BarObject.div_timer = _G [name .. "_sparkTimer"]
647
648 --> hooks
649 BarObject.statusbar:SetScript ("OnEnter", OnEnter)
650 BarObject.statusbar:SetScript ("OnLeave", OnLeave)
651 BarObject.statusbar:SetScript ("OnHide", OnHide)
652 BarObject.statusbar:SetScript ("OnShow", OnShow)
653 BarObject.statusbar:SetScript ("OnMouseDown", OnMouseDown)
654 BarObject.statusbar:SetScript ("OnMouseUp", OnMouseUp)
655
656 --> set class
657 _setmetatable (BarObject, BarMetaFunctions)
658
659 --> set texture
660 if (texture_name) then
661 smember_texture (BarObject, texture_name)
662 end
663
664 return BarObject
665 end