/Tukui/modules/unitframes/plugins/oUF_TotemBar/oUF_TotemBar.lua

http://github.com/Asphyxia/Tukui · Lua · 162 lines · 111 code · 22 blank · 29 comment · 28 complexity · 7b9ed90c1405b2077329ffb8f5589516 MD5 · raw file

  1. local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2. if C.unitframes.enable ~= true or T.myclass ~= "SHAMAN" then return end
  3. --[[
  4. Documentation:
  5. Element handled:
  6. .TotemBar (must be a table with statusbar inside)
  7. .TotemBar only:
  8. .delay : The interval for updates (Default: 0.1)
  9. .colors : The colors for the statusbar, depending on the totem
  10. .Name : The totem name
  11. .Destroy (boolean): Enables/Disable the totem destruction on right click
  12. NOT YET IMPLEMENTED
  13. .Icon (boolean): If true an icon will be added to the left or right of the bar
  14. .IconSize : If the Icon is enabled then changed the IconSize (default: 8)
  15. .IconJustify : any anchor like "TOPLEFT", "BOTTOMRIGHT", "TOP", etc
  16. .TotemBar.bg only:
  17. .multiplier : Sets the multiplier for the text or the background (can be two differents multipliers)
  18. --]]
  19. local _, ns = ...
  20. local oUF = ns.oUF or oUF
  21. if not oUF then return end
  22. local _, pClass = UnitClass("player")
  23. local total = 0
  24. local delay = 0.01
  25. -- In the order, fire, earth, water, air
  26. local colors = {
  27. [1] = {.58,.23,.10},
  28. [2] = {.23,.45,.13},
  29. [3] = {.19,.48,.60},
  30. [4] = {.42,.18,.74},
  31. }
  32. local GetTotemInfo, SetValue, GetTime = GetTotemInfo, SetValue, GetTime
  33. local Abbrev = function(name)
  34. return (string.len(name) > 10) and string.gsub(name, "%s*(.)%S*%s*", "%1. ") or name
  35. end
  36. local function TotemOnClick(self,...)
  37. local id = self.ID
  38. local mouse = ...
  39. --~ print(id, mouse)
  40. if IsShiftKeyDown() then
  41. for j = 1,4 do
  42. DestroyTotem(j)
  43. end
  44. else
  45. DestroyTotem(id)
  46. end
  47. end
  48. local function InitDestroy(self)
  49. local totem = self.TotemBar
  50. for i = 1 , 4 do
  51. local Destroy = CreateFrame("Button",nil, totem[i])
  52. Destroy:SetAllPoints(totem[i])
  53. Destroy:RegisterForClicks("LeftButtonUp", "RightButtonUp")
  54. Destroy.ID = i
  55. Destroy:SetScript("OnClick", TotemOnClick)
  56. end
  57. end
  58. local function UpdateSlot(self, slot)
  59. local totem = self.TotemBar
  60. local haveTotem, name, startTime, duration, totemIcon = GetTotemInfo(slot)
  61. totem[slot]:SetStatusBarColor(unpack(totem.colors[slot]))
  62. totem[slot]:SetValue(0)
  63. -- Multipliers
  64. if (totem[slot].bg.multiplier) then
  65. local mu = totem[slot].bg.multiplier
  66. local r, g, b = totem[slot]:GetStatusBarColor()
  67. r, g, b = r*mu, g*mu, b*mu
  68. totem[slot].bg:SetVertexColor(r, g, b)
  69. end
  70. totem[slot].ID = slot
  71. -- If we have a totem then set his value
  72. if(haveTotem) then
  73. if totem[slot].Name then
  74. totem[slot].Name:SetText(Abbrev(name))
  75. end
  76. if(duration >= 0) then
  77. totem[slot]:SetValue(1 - ((GetTime() - startTime) / duration))
  78. -- Status bar update
  79. totem[slot]:SetScript("OnUpdate",function(self,elapsed)
  80. total = total + elapsed
  81. if total >= delay then
  82. total = 0
  83. haveTotem, name, startTime, duration, totemIcon = GetTotemInfo(self.ID)
  84. if ((GetTime() - startTime) == 0) then
  85. self:SetValue(0)
  86. else
  87. self:SetValue(1 - ((GetTime() - startTime) / duration))
  88. end
  89. end
  90. end)
  91. else
  92. -- There's no need to update because it doesn't have any duration
  93. totem[slot]:SetScript("OnUpdate",nil)
  94. totem[slot]:SetValue(0)
  95. end
  96. else
  97. -- No totem = no time
  98. if totem[slot].Name then
  99. totem[slot].Name:SetText(" ")
  100. end
  101. totem[slot]:SetValue(0)
  102. end
  103. end
  104. local function Update(self, unit)
  105. -- Update every slot on login, still have issues with it
  106. for i = 1, 4 do
  107. UpdateSlot(self, i)
  108. end
  109. end
  110. local function Event(self,event,...)
  111. if event == "PLAYER_TOTEM_UPDATE" then
  112. UpdateSlot(self, ...)
  113. end
  114. end
  115. local function Enable(self, unit)
  116. local totem = self.TotemBar
  117. if(totem) then
  118. self:RegisterEvent("PLAYER_TOTEM_UPDATE" ,Event)
  119. totem.colors = setmetatable(totem.colors or {}, {__index = colors})
  120. delay = totem.delay or delay
  121. if totem.Destroy then
  122. InitDestroy(self)
  123. end
  124. TotemFrame:UnregisterAllEvents()
  125. return true
  126. end
  127. end
  128. local function Disable(self,unit)
  129. local totem = self.TotemBar
  130. if(totem) then
  131. self:UnregisterEvent("PLAYER_TOTEM_UPDATE", Event)
  132. TotemFrame:Show()
  133. end
  134. end
  135. oUF:AddElement("TotemBar",Update,Enable,Disable)