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.

274 Part II ■ <strong>Programming</strong> in <strong>World</strong> <strong>of</strong> <strong>Warcraft</strong>As mentioned in Chapter 8, each frame type has a number <strong>of</strong> widget scriptsthat can be set with a h<strong>and</strong>ler function. Table 14-1 describes the scripts you’reusing here (you can find more details about these scripts <strong>and</strong> the argumentsthey accept in Chapter 29, ‘‘Widget <strong>Reference</strong>’’).Table 14-1: Script H<strong>and</strong>lers Used in CombatTrackerSCRIPTUSEThe basic initialization function <strong>for</strong> CombatTracker willbe called CombatTracker_OnLoad(). It’s called fromthe h<strong>and</strong>ler on the frame when the framehas finished initializing. It works only <strong>for</strong> frames that aredefined in XML.The addon will respond to a number <strong>of</strong> events, so youmust define an OnEvent script to h<strong>and</strong>le them. TheCombatTracker_OnEvent() function will beresponsible <strong>for</strong> each <strong>of</strong> the events. The h<strong>and</strong>ling functionwill need to know which event it’s being passed, soensure you are passing the frame self, the event nameevent, <strong>and</strong> the variable set <strong>of</strong> arguments ... to thefunction.When the player right-clicks CombatTrackerFrame,theOnClick script h<strong>and</strong>ler will call theCombatTracker_ReportDPS() function to print thecurrent combat status. The status message will be sentto party chat if the player is in a party, otherwise it will beprintedtothechatframe.The code <strong>for</strong> allowing a frame to be dragged <strong>and</strong> placedis very simple, so the code is written directly in the scripth<strong>and</strong>ler rather than in a function defined inCombatTracker.lua. Later, if you wanted to addoptions to the addon, such as allowing the user to lockthe frame in place, you could move this code into theLua file to consolidate everything.Each frame that has the movable attribute set to truecan call the self:StartMoving() method, whichcauses the frame to follow the mouse. As a result, allthat’s required to set up frame movement when draggedis to call this function.Stopping the frame from moving is a matter <strong>of</strong> callingself:StopMovingOrSizing(). This function als<strong>of</strong>lags the frame as user placed, meaning the next timethe addon loads, the frame will be put in the same placethe user dropped it. This is quite a h<strong>and</strong>y side effect.

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

Saved successfully!

Ooh no, something went wrong!