Advertisement
Guest User

Untitled

a guest
May 20th, 2016
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. local _G = _G
  2. local RAID_CLASS_COLORS = RAID_CLASS_COLORS
  3. local select = select
  4. local unpack = unpack
  5. local UnitPower = UnitPower
  6. local UnitPowerMax = UnitPowerMax
  7. local UnitHealth = UnitHealth
  8. local UnitHealthMax = UnitHealthMax
  9.  
  10. local GREEN = { 0, 1, 0 }
  11. local RED = { 1, 0, 0 }
  12.  
  13. local powerColors = {
  14. ["MANA"] = {0.1, 0.1, 0.98},
  15. --["MANA_BG"] = {0.0, 0.0, 0.2,},
  16. ["RAGE"] = {0.81, 0.01, 0.10},
  17. --["RAGE_BG"] = {0.2, 0, 0, 0.9},
  18. ["ENERGY"] = {0.90, 0.90, 0.0},
  19. --["ENERGY_BG"] = {0.2, 0.2, 0.0, 0.9},
  20. --["RUNIC_POWER"] = {0, 0.5 , 0.5 },
  21. --["RUNICPOWER_BG"] = {0.0, 0.16, 0.2, },
  22. }
  23.  
  24.  
  25.  
  26. local unit_changed = {
  27. target = "PLAYER_TARGET_CHANGED",
  28. focus = "PLAYER_FOCUS_CHANGED",
  29. mouseover = "UPDATE_MOUSEOVER_UNIT",
  30. player = "",
  31. }
  32.  
  33. local function frame_OnDragStart(self)
  34. if IsShiftKeyDown() and self:GetAttribute("drag") then
  35. self:StartMoving()
  36. end
  37. end
  38.  
  39. local function frame_OnDragStop(self)
  40. self:StopMovingOrSizing()
  41. -- SavePosition(self) addons could save position here
  42. end
  43.  
  44.  
  45. local function UpdateStatus(self, event,...)
  46. local unit = ...
  47. if unit == self.unit then
  48.  
  49. self:SetValue(self.status_current(self.unit) / self.status_max(self.unit)*100)
  50. local selfType = self.type
  51. if selfType == "Health" then
  52. self.text:SetText(UnitName(self.unit))
  53. elseif selfType == "Power" then
  54. self.text:SetText(self:GetValue())
  55. end
  56.  
  57. end
  58. end
  59.  
  60. local cache = {}
  61. local function UpdatePowerType(self)
  62. local integer, str = UnitPowerType(self.unit)
  63. self:UnregisterAllEvents()
  64. if str then
  65. local event = cache[str]
  66. if not event then
  67. event = "UNIT_"..str
  68. cache[str] = event
  69. end
  70. self:RegisterEvent(event)
  71. self:SetStatusBarColor(unpack(powerColors[str]))
  72. end
  73. end
  74.  
  75. local function OnShow(self)
  76. local selfType = self.type
  77. if selfType == "Health" then
  78. self.text:SetText(UnitName(self.unit))
  79. elseif selfType == "Power" then
  80. self.text:SetText(self:GetValue())
  81. end
  82.  
  83. if selfType == "Power" then
  84. UpdatePowerType(self)
  85. elseif selfType == "Health" then
  86. if self.classcolor then
  87. local _, CLASS = UnitClass(self.unit)
  88. if CLASS then
  89. local c = RAID_CLASS_COLORS[CLASS]
  90. self:SetStatusBarColor(c.r, c.g, c.b)
  91. else
  92. local color = UnitIsFriend(self.unit, "player") and GREEN or RED
  93. self:SetStatusBarColor(unpack(color))
  94. end
  95. else
  96. local color = UnitIsFriend(self.unit, "player") and GREEN or RED
  97. self:SetStatusBarColor(unpack(color))
  98. end
  99. end
  100. end
  101.  
  102. local function OnUnitChanged(self, event, ...)
  103. OnShow(self.statusbar)
  104. UpdateStatus(self.statusbar,"", self.unit)
  105.  
  106. end
  107.  
  108. local function SetBorderColor(self, ...)
  109. self.statusbar.border:SetBackdropBorderColor(...)
  110. end
  111.  
  112. local created_frames = {}
  113. function GetStatusbar(name, unit, statusbar_type)
  114. local event = statusbar_type=="Power" and "UNIT_POWER" or "UNIT_HEALTH"
  115. local f = CreateFrame("Button",name and "SimpleBars_"..name or nil,UIParent, "SecureUnitButtonTemplate,SecureHandlerDoubleClickTemplate") -- enable cool features
  116. -- cache frame for later use
  117. created_frames[f] = true
  118.  
  119.  
  120. f:SetSize(150,20)
  121. f.unit = unit
  122. f:SetPoint("CENTER",UIParent)
  123. f.statusbar = CreateFrame("Statusbar",nil,f)
  124. f.statusbar.type = statusbar_type
  125. f.statusbar.status_current = _G["Unit"..statusbar_type]
  126. f.statusbar.status_max = _G["Unit"..statusbar_type.."Max"]
  127.  
  128. f.statusbar.unit = unit
  129. f.statusbar:SetMinMaxValues(0,100)
  130. f.statusbar:SetPoint("CENTER",f)
  131. f.statusbar:SetSize(150,20)
  132. f.statusbar:SetValue(100)
  133. f.statusbar:SetStatusBarColor(0.4, 0.4,0.4) -- pick some random color
  134. --f.statusbar:Lower()
  135. -- Statusbar texture
  136. f.statusbar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
  137.  
  138. -- f.statusbarrame Border
  139. f.statusbar.border = CreateFrame("Frame", nil, f.statusbar)
  140. f.statusbar.border:SetBackdrop({
  141. bgFile=nil,
  142. edgeFile="Interface\\ChatFrame\\ChatFrameBackground",
  143. tile=true,
  144. tileSize=5,
  145. edgeSize= 2, -- border size
  146. })
  147.  
  148. --f.statusbar.border:SetBackdropColor(0,1,0)
  149. f.statusbar.border:SetBackdropBorderColor(0,0,0)
  150. f.statusbar.border:SetAllPoints(f.statusbar)
  151.  
  152. f.statusbar.text = f.statusbar:CreateFontString(nil,"ARTWORK")
  153. f.statusbar.text:SetFont(STANDARD_TEXT_FONT,12,"OUTLINE")
  154. f.statusbar.text:SetPoint("CENTER", f.statusbar)
  155. f.statusbar.text:SetText("")
  156. f.statusbar.text:SetTextColor(1, 1, 1)
  157.  
  158.  
  159.  
  160.  
  161.  
  162. -- click support
  163. f:SetAttribute("unit", unit)
  164. f:SetAttribute("type1", "target")
  165. f:SetAttribute("type2", "focus")
  166.  
  167. f:RegisterForClicks("AnyDown", "AnyUp")
  168.  
  169. -- Enable Drag on double click
  170. f:SetAttribute("drag", false)
  171. f:SetAttribute("_ondoubleclick", [=[
  172.  
  173. GREEN = GREEN or newtable(0,1,0)
  174. NORMAL = NORMAL or newtable(0,0,0)
  175. local enabled = not self:GetAttribute("drag")
  176. self:SetAttribute("drag",enabled)
  177. local newcolor = enabled and GREEN or NORMAL
  178. control:CallMethod("SetBorderColor", unpack(newcolor))
  179. ]=]);
  180.  
  181. -- Handle updating statusbar with whatever
  182. f.statusbar:SetScript("OnEvent", UpdateStatus)
  183. f.statusbar:RegisterEvent(event)
  184.  
  185. --f:SetScript("OnAttributeChanged", function(self, attribute, arg)
  186. --print("Attribute:", attribute, arg)
  187. --end)
  188.  
  189. -- Handle dragging
  190. f:SetMovable(true)
  191. f:EnableMouse(true)
  192. f:RegisterForDrag("LeftButton")
  193. f:SetScript("OnDragStart", frame_OnDragStart)
  194. f:SetScript("OnDragStop", frame_OnDragStop)
  195.  
  196. -- show / hide bar if unit exists
  197. RegisterUnitWatch(f) -- show / hide bar if unit exists
  198.  
  199. -- Update other stuff on show, such as name if unit changes
  200. f.statusbar:SetScript("OnShow", OnShow)
  201.  
  202. -- Handle unit changing
  203. f:RegisterEvent(unit_changed[unit] or "")
  204. f:SetScript("OnEvent", OnUnitChanged)
  205.  
  206. f:Show()
  207. f.statusbar:Show()
  208.  
  209. if UnitExists(f.unit) then
  210. UpdateStatus(f.statusbar, "", f.unit)
  211. end
  212.  
  213. -- install some methods, they can be called from secure click handler
  214. f.SetBorderColor = SetBorderColor
  215.  
  216. return f
  217. end
  218. local targethp = GetStatusbar("targethp","target", "Health") -- or GetStatusbar("player", "Health")
  219. targethp.statusbar.classcolor = true;
  220.  
  221. local tpower = GetStatusbar("targetpower", "target", "Power") -- or GetStatusbar("player", "Health")
  222.  
  223. -- update changes
  224. targethp:Hide()
  225. targethp:Show()
  226.  
  227. local hp = GetStatusbar("playerhp", "player", "Health")
  228. hp.classcolor = true
  229. hp:Hide();hp:Show()
  230. local power = GetStatusbar("playerpower", "player", "Power")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement