View Single Post
08-01-14, 04:12 AM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You can reuse all of the functions, the reset function included. However, the way you're doing it is a bit odd, and actually your OnMouseUp/OnMouseDown scripts won't work; you've got an implied "self" as the first argument, and "model" as the first explicit argument, but the value passed in that place will actually be a string describing which mouse button was pressed/released. If you want "model" to work as you seem to want, you need to define those methods with a dot instead of a colon.

However, to keep things cleaner, I'd put all those functions in their own table, rather than attach them to the canvas (where they don't make any sense). Also, rather than creating 50 models up front, with (I assume) the possibility of more being created later, I'd just use a metatable to create them all on demand.

Code:
local canvas = CreateFrame("Frame", nil, UIParent)

local modelPrototype = {}

function modelPrototype:Reset()
	print(self.name, "resetting values")
	self:PortraitZoom(0)
end

function modelPrototype:OnMouseDown(button)
	print(self.name, button)
	self:Reset()
end

function modelPrototype:OnMouseUp(button)
	print(self.name, button)
end

canvas.models = setmetatable({}, { __index = function(t, id)
	local m = CreateFrame("PlayerModel", nil, canvas)
	m:EnableMouse(true)

	m.name = "model"..id
	m.id = id

	for k, v in pairs(modelPrototype) do
		m[k] = v
	end

	m:SetScript("OnMouseDown", m.OnMouseDown)
	m:SetScript("OnMouseUp", m.OnMouseUp)

	t[id] = m
	return m
end })
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote