Question on UnitFrames addon

90 Night Elf Hunter
6545
Does anyone know a really cleanly coded Unit Frames addon? I'm looking for one to try and understand the inner working of how the textures are being moved. I understand the UNIT_HEALTH event is firing and the addon is calculating the percentage drop in health by getting the max and current health and then sending an update to the frame, but I'm not sure how to set the texture or repaint it.

Again. Nice, cleanly coded Unit Frames addon for me to get my hands dirty with.
Reply Quote
100 Dwarf Mage
11375
Unit Frames use StatusBar objects (created with CreateFrame("StatusBar", ...)) to display health/mana bars. All you need to do is use the :SetMinMaxValues method when the unit's max health changes to set the bounds of the bar then use the :SetValue method when the unit's current health changes to change the size of the texture.

You can see the relevant StatusBar methods here:
http://www.wowpedia.org/Widget_API#StatusBar
http://wowprogramming.com/docs/widgets#statusbar

As for a unit frame AddOn to look at, you could try the default UI (specifically TextStatusBar.lua/xml, PlayFrame.lua/xml and TargetFrame.lua/xml), oUF (which is a framework rather than a stand-alone AddOn) or PitBull4.

To extract Blizzard's interface code, you can follow these instructions:
http://www.wowpedia.org/Viewing_Blizzard%27s_interface_code

You can also view it online here:
http://wow.go-hero.net/framexml/live
http://wowprogramming.com/utils/xmlbrowser

WoW Programming tends to be a bit slow to update its interface code. Go-Hero is quicker to update and has a few features that WoW Programming doesn't, like function links and the ability to navigate directly to a file or function from the search feature.

http://wow.go-hero.net/framexml/about
Reply Quote
90 Night Elf Hunter
6545
Is it possible to use a texture to represent health? As in, a solid bar, with a sliding arrow that denotes at what position health is at?
Reply Quote
100 Dwarf Mage
11375
12/04/2012 07:34 AMPosted by Arazon
Is it possible to use a texture to represent health? As in, a solid bar, with a sliding arrow that denotes at what position health is at?

Sure, you'd just have do a bit more of the updating manually. Create a texture for the bar and another for the arrow then every time the unit's health changes, anchor the arrow texture (current_hp/max_hp) * bar_width units to the right of the bar's left edge.

Something like this (the forums aren't wide enough to display the comments properly, you should copy/paste it into your text/code editor of choice):local function Bar_OnUnitHealth(self, currentHealth) -- self should be the bar frame, currentHealth should be the unit's current health
local percent = currentHealth / self.maxHealth
local offset = percent * self.width

self.arrow:SetPoint("CENTER", self, "LEFT", offset, 0) -- Anchor the arrow's centre 'offset' units from the bar's left edge

-- Do any other stuff like updating text here
end

local function Bar_OnUnitMaxHealth(self, maxHealth) -- self should be the bar frame, maxHealth should be the unit's max health
self.maxHealth = maxHealth -- It's more efficient to cache the max health than to call UnitMaxHealth every time the unit's health changes

-- Do any other stuff like updating text here
end

local function CreateBar(parent, barName)
local bar = CreateFrame("Frame", "$parent" .. barName, parent) -- Create the bar's main frame as a child of the parent
bar:SetHeight(20) -- Set the bar's size
bar:SetWidth(100)
bar.width = 100 -- Table lookups are more efficient than method calls. Make sure this value is updated whenever the width changes.
bar:SetPoint("LEFT", parent, "LEFT", 0, 0) -- Anchor the bar to the parent frame

bar.bgtex = bar:CreateTexture("$parentBackground", "BACKGROUND") -- Create the background texture
bar.bgtex:SetTexture("Interface\\AddOns\\MyAddOn\\media\\statusBG.tga") -- Set the texture file
bar.bgtex:SetAllPoints() -- Anchor it to the main frame

bar.arrow = bar:CreateTexture("$parentArrow", "OVERLAY") -- Create the arrow texture
bar.arrow:SetTexture("Interface\\AddOns\\MyAddOn\\media\\statusArrow.tga") -- Set the texture file
bar.arrow:SetHeight(20) -- Set the arrow's size
bar.arrow:SetHeight(20)
-- The arrow should be anchored by the first call to bar:OnUnitHealth

bar.OnUnitHealth = Bar_OnUnitHealth -- Should be called as `bar:OnUnitHealth(currentHealth)` when the unit's current health changes.
bar.OnUnitMaxHealth = Bar_OnUnitMaxHealth -- Should be called as `bar:OnUnitMaxHealth(maxHealth)` when the unit's max health changes

parent[barName] = bar -- Store the bar in the parent frame
end
Edited by Choonster on 12/4/2012 8:14 AM PST
Reply Quote
90 Night Elf Hunter
6545
Genius. Thank you sir! You are an inspiration to all of us.
Reply Quote

Please report any Code of Conduct violations, including:

Threats of violence. We take these seriously and will alert the proper authorities.

Posts containing personal information about other players. This includes physical addresses, e-mail addresses, phone numbers, and inappropriate photos and/or videos.

Harassing or discriminatory language. This will not be tolerated.

Forums Code of Conduct

Report Post # written by

Reason
Explain (256 characters max)

Reported!

[Close]