Thread Tools Display Modes
09-28-13, 07:34 AM   #1
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
update healthbar/power

So two things, one my health I wanna know how I can update my bars more smoothly than the default clucky chuncks at a time, and two I seemingly can't remember how to check for the type of power the unit has.

Lua Code:
  1. --BobHUD, an extremely simple HUD addon
  2. local _, cfg = ... --export config
  3. local addon, ns = ... --get addon namespace
  4.  
  5. local f = CreateFrame("Button","BobPlayerHUD",UIParent,"SecureUnitButtonTemplate")
  6. f.unit = "player" --change to unit you want to track
  7. --Make the Health Bar
  8. --background texture
  9. f.back = f:CreateTexture(nil,"BACKGROUND")
  10. f.back:SetSize(unpack(cfg.barsize))
  11. f.back:SetPoint("CENTER",UIParent,cfg.healthx,cfg.healthy)
  12. f.back:SetTexture("Interface\\Addons\\BobHUD\\media\\healthbar")
  13. f.back:SetVertexColor(0,0,0,0.5)
  14. --health bar
  15. f.hp = CreateFrame("StatusBar",nil,f,"TextStatusBar")
  16. f.hp:SetSize(unpack(cfg.barsize))
  17. f.hp:SetPoint("CENTER",UIParent,cfg.healthx,cfg.healthy)
  18. f.hp:SetStatusBarTexture("Interface\\Addons\\BobHUD\\media\\healthbar")
  19. f.hp:SetStatusBarColor(0,.8,0)
  20. f.hp:SetMinMaxValues(0,1)
  21. f.hp:SetOrientation("VERTICAL")
  22. f.hp:EnableMouse(0)
  23. f.hp:SetAlpha(cfg.alpha)
  24. --health fontstring
  25. f.HealthText = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  26. f.HealthText:SetPoint("BOTTOM",f.hp,2,0)
  27. --percent
  28. f.HealthText2 = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  29. f.HealthText2:SetPoint("BOTTOM",f.hp,28,12)
  30.  
  31. --Make the Mana Bar
  32. f:SetAttribute("unit",f.unit)
  33. f:SetAttribute("type","target")
  34. --background texture
  35. f.back2 = f:CreateTexture(nil,"BACKGROUND")
  36. f.back2:SetSize(unpack(cfg.barsize))
  37. f.back2:SetPoint("CENTER",UIParent,cfg.powerx,cfg.powery)
  38. f.back2:SetTexture("Interface\\Addons\\BobHUD\\media\\powerbar")
  39. f.back2:SetVertexColor(0,0,0,0.5)
  40. --mana bar
  41. f.mp = CreateFrame("StatusBar",nil,f,"TextStatusBar")
  42. f.mp:SetSize(unpack(cfg.barsize))
  43. f.mp:SetPoint("CENTER",UIParent,cfg.powerx,cfg.powery)
  44. f.mp:SetStatusBarTexture("Interface\\Addons\\BobHUD\\media\\powerbar")
  45. f.mp:SetStatusBarColor(0,0,1)
  46. f.mp:SetMinMaxValues(0,1)
  47. f.mp:SetOrientation("VERTICAL")
  48. f.mp:EnableMouse(0)
  49. f.mp:SetAlpha(cfg.alpha)
  50. --power fontstring
  51. f.PowerText = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  52. f.PowerText:SetPoint("BOTTOM",f.mp,-2,0)
  53. --percent
  54. f.PowerText2 = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  55. f.PowerText2:SetPoint("BOTTOM",f.mp,-28,12)
  56.  
  57. --Updates the status bar to new health
  58. f:SetScript("OnEvent", function(self, event, unit)
  59.     if event == "UNIT_HEALTH" or event == "PLAYER_ENTERING_WORLD" then
  60.       --update health
  61.         self.hp:SetValue(UnitHealth(f.unit)/UnitHealthMax(f.unit))
  62.         local currentHealth, maxHealth = UnitHealth(f.unit), UnitHealthMax(f.unit)
  63.         local percentHealth = currentHealth / maxHealth * 100
  64.         self.HealthText:SetFormattedText("%d/%d", currentHealth, maxHealth, percentHealth)
  65.         self.HealthText2:SetFormattedText("%d%%", percentHealth)
  66.     end
  67.     if event == "UNIT_POWER" or event == "PLAYER_ENTERING_WORLD" then
  68.         --update powa
  69.         self.mp:SetValue(UnitPower(f.unit)/UnitPowerMax(f.unit))
  70.         local currentPower, maxPower = UnitPower(f.unit), UnitPowerMax(f.unit)
  71.         local percentPower = currentPower / maxPower * 100
  72.         self.PowerText:SetFormattedText("%d/%d", currentPower, maxPower, percentPower)
  73.         self.PowerText2:SetFormattedText("%d%%", percentPower)
  74.     end
  75. end)
  76. f:RegisterEvent("PLAYER_ENTERING_WORLD")
  77. f:RegisterUnitEvent("UNIT_HEALTH", f.unit)
  78. f:RegisterUnitEvent("UNIT_POWER", f.unit)
__________________
Tweets YouTube Website
  Reply With Quote
09-28-13, 08:07 AM   #2
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
actually just figured out the coloring >.> been too long since I played with .lua or even the game I suppose.
__________________
Tweets YouTube Website

Last edited by 10leej : 09-28-13 at 08:16 AM.
  Reply With Quote
09-28-13, 09:02 AM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
http://wowprogramming.com/docs/api/UnitPowerType
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
09-28-13, 09:21 AM   #4
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Yeah I actually found it in a wowhead post.

Lua Code:
  1. local UnitPowerType = UnitPowerType
  2. local powercolor = PowerBarColor[select(2, UnitPowerType(f.unit))] or PowerBarColor['MANA']
  3. f.mp:SetStatusBarColor(powercolor.r, powercolor.g, powercolor.b)
__________________
Tweets YouTube Website
  Reply With Quote
09-28-13, 10:11 AM   #5
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
UNIT_HEALTH = super clunky
UNIT_HEALTH_FREQUENT = faster but still clunky

To get it smooth you need an OnUpdate script. Use a function to ease out the value difference. An example for this can be found in oUF_Smooth.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 09-28-13 at 10:17 AM.
  Reply With Quote
09-28-13, 10:26 AM   #6
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
UNIT_HEALTH_FREQUENT definitely helped out, run that until I can decipher oUF_Smooth too much time with html5 and java lately.
__________________
Tweets YouTube Website
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » update healthbar/power


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off