View Single Post
08-01-14, 03:33 AM   #7
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
Thanks Phanks.

I got a follow up question on that part that I need to get answered for my current project.

I have a model frame. Each model lives on the same canvas. The canvas can hold a lot of models (200+) in extreme cases.

How should I define functions that work on my model frame? Should they be defined on the canvas or can I add them to each model.

Currently my implementation is like this:
Lua Code:
  1. --canvas
  2.   local canvas = CreateFrame("Frame", nil, UIParent)
  3.   canvas.models = {}
  4.  
  5.   function canvas:ModelOnMouseDown(model,...)
  6.     print(model.name,...)
  7.     model:Reset()
  8.   end
  9.  
  10.   function canvas:ModelOnMouseUp(model,...)
  11.     print(model.name,...)
  12.   end
  13.  
  14.   function canvas:CreateModel(id)
  15.  
  16.     local m = CreateFrame("PlayerModel", nil, self)
  17.     m:EnableMouse(true)
  18.     m.id = id
  19.     m.name = "model"..id
  20.    
  21.     function m:Reset()
  22.       --stuff to reset model data
  23.       self:PortraitZoom(0)
  24.       print(self.name,"reseting values")
  25.     end    
  26.    
  27.     m:SetScript("OnMouseDown", self.ModelOnMouseDown)
  28.     m:SetScript("OnMouseUp", self.ModelOnMouseUp)  
  29.  
  30.     return m
  31.  
  32.   end
  33.  
  34.   for i=1, 50 do
  35.     canvas.models[i] = canvas:CreateModel(i)
  36.   end

My question is about model specific functions like m:Reset().

Should they be defined on the canvas aswell?

Currently I would define model specific funtions directly on the model. But I could create them on the canvas instead.

So the above would change to sth like this

Lua Code:
  1. --canvas
  2.   local canvas = CreateFrame("Frame", nil, UIParent)
  3.   canvas.models = {}
  4.  
  5.   function canvas:ModelOnMouseDown(model,...)
  6.     print(model.name,...)
  7.     self:ResetModel(model,...) --not sure if self is available in this context otherwise model:GetParent()
  8.   end
  9.  
  10.   function canvas:ResetModel(model,...)
  11.     --stuff to reset model data
  12.     model:PortraitZoom(0)
  13.     print(model.name,"reseting values")
  14.   end    
  15.  
  16.   function canvas:ModelOnMouseUp(model,...)
  17.     print(model.name,...)
  18.   end
  19.  
  20.   function canvas:CreateModel(id)
  21.  
  22.     local m = CreateFrame("PlayerModel", nil, self)
  23.     m:EnableMouse(true)
  24.     m.id = id
  25.     m.name = "model"..id
  26.    
  27.     m:SetScript("OnMouseDown", self.ModelOnMouseDown)
  28.     m:SetScript("OnMouseUp", self.ModelOnMouseUp)  
  29.  
  30.     return m
  31.  
  32.   end
  33.  
  34.   for i=1, 50 do
  35.     canvas.models[i] = canvas:CreateModel(i)
  36.   end

Which version is better and why?
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 08-01-14 at 03:42 AM.
  Reply With Quote