Skip to content
João Cardoso edited this page Mar 30, 2017 · 3 revisions

Creating Classes

After including Poncho and LibStub in your add-on, you can retrieve the class constructor by calling:

local CreateClass = LibStub('Poncho-1.0')

The CreateClass method works much alike CreateFrame, except it accepts a final optional argument:

CreateClass(frameType, className, defaultParent, xmlTemplates, parentClass)

frameType

Defines the frameType of all frames belonging to this class, exactly as in CreateFrame.

className

Defines the global name of the class, so:

local MyClass = CreateClass('Frame', 'Banana')
local FirstBanana = MyClass()

Is the same as:

local MyClass = CreateClass('Frame', 'Banana')
local FirstBanana = Banana()

The global name of the frames is handled internally by Poncho, and depends on the name of the class.

defaultParent

Defines the default parent of the frames, which will be used unless explicitly set when creating the frame (see below).

xmlTemplates

Defines the xmlTemplates of all frames belonging to this class, exactly as in CreateFrame.

parentClass

Defines another Poncho class which this class will inherit from.

Creating/Releasing Frames

Creating frames is done by calling the class object itself:

local MyClass = CreateClass('Frame')
local FirstFrame = MyClass()

Frames can then be dealloc trough the Release method:

MyFrame:Release()

Class Events

On most object oriented languages, when creating classes, programmers may set two special methods: a constructor and a destructor. As it is not possible to actually destroy Frames in World of Warcraft, there are 3 special events available:

OnCreate

Called a single time for each frame - when it is truly created.

OnRelease

Called when a frame is "released" by the user, and placed on a "wait to be used again" pile.

On Acquire

Called each time the frame is requested by the user. Which means:

  • When is created, after OnCreate.
  • Whenever is pulled from the "wait" pile.

Wrapping Up

A final example of a simple class implementation. Any button belonging to this class will print a number when clicked, which is incremented as long as the button is acquired (active).

local Counter = LibStub('Poncho-1.0')('Button')

-- Construct / Destruct events
function Counter:OnCreate()
	self:SetScript('OnClick', self.Print)
end

function Counter:OnAcquire()
	self.count = 0
	self:SetScript('OnUpdate', self.Increment)
end

function Counter:OnRelease()
	self:SetScript('OnUpdate', nil)
end

-- Script
function Counter:Print()
	print(self.count)
end

function Counter:Increment()
	self.count = self.count + 1
end 

local MyButtonCounter = Counter(Minimap) -- parents it to the minimap