12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 26 ■ <strong>Creating</strong> Unit Frames with Group Templates 509Additionally, the header is flagged as movable, because at some point in thefutureitmaybeuseful<strong>for</strong>theplayertobeabletomovetheframesaroundthe screen.Setting Name <strong>and</strong> Status BarsIf you were to load the addon at this point, the frames wouldn’t displayanything; however, to update the name <strong>and</strong> health/power bars the frameneeds to know when the unit it’s supposed to be displaying has changed.When the group header reconfigures the frames, it hides each frame <strong>and</strong>then re-shows them. You can use this to update the frame by defining anOnShow script.Open SquareUnitFrames.lua <strong>and</strong> add the following function:function SquareUnitFrames_Frame_OnShow(button)local unit = button:GetAttribute(“unit“)if unit thenlocal guid = UnitGUID(unit)if guid ~= button.guid thenSquareUnitFrames_ResetUnitButton(button.unit, unit)button.guid = guidendendend-- Actually show the header frameSquareUnitFrames_Header:Show()In its current state, the group header has a tendency to show <strong>and</strong> hide theframes more <strong>of</strong>ten than is strictly necessary. Knowing this, you implementa small check to ensure you only reset the frame when the underlying unitactually changes. First you check to make sure that the frame actually has theunit attribute set, <strong>and</strong> then retrieve the GUID <strong>for</strong> that unit. You may recallfrom Chapter 21 that the GUID is a globally unique identifier <strong>for</strong> each unit soyou can use this to track these changes.You call an external function to actually update the button, <strong>and</strong> the reason<strong>for</strong> this will become apparent later in the chapter. Suffice it to say that thereset code is called from multiple places, so abstracting the code into afunction enables you to avoid maintaining it in two different places. Definethe following function after SquareUnitFrames_Frame_OnShow <strong>and</strong> be<strong>for</strong>e thecall that actually shows the header frame:function SquareUnitFrames_ResetUnitButton(button, unit)SquareUnitFrames_ResetHealthBar(button, unit)SquareUnitFrames_ResetPowerBar(button, unit)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!