Thread Tools Display Modes
04/27/14, 03:22 PM   #1
elblobbo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 5
Delete a control

Hi,

I create label controls dynamically and they are zone based. Whenever I enter a new zone, I will create new label controls to add to my window, but I need to delete the previous zone's label controls first. It's been a long weekend, and I am new to both LUA and AddOns, but for the life of me I can't figure out how to delete the previous zone's label controls.


Thanks

Last edited by elblobbo : 04/27/14 at 06:03 PM.
  Reply With Quote
04/27/14, 04:14 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Don't continuously create more and more new labels. Reuse the one you have.
  Reply With Quote
04/27/14, 06:02 PM   #3
elblobbo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 5
Originally Posted by Seerah View Post
Don't continuously create more and more new labels. Reuse the one you have.
If the answer to my question is "No, there isn't a way", then sure, I can do that, but for my add/on, the number of labels can vary, depending on the zone, and the user's options for that zone, and it would be much cleaner to flush my labels, and create new ones when a user switches zones.

It's just the way I was taught how to code, a long, long time ago. Allocate the resources when you need them, and get rid of those resources when you don't.

If I need 20 labels for a particular zone based on the user options, and then go to another zone, and only need 4, I am not in the habit of keeping 16 controls around and just hiding them if I am not using them. I know I can do it that way, I just prefer not to.

I was just wondering if there was a way to delete controls, that's all.
  Reply With Quote
04/27/14, 06:28 PM   #4
Divona
 
Divona's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 12
Would set control to "nil" do the job?

myControl = nil
  Reply With Quote
04/27/14, 06:43 PM   #5
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
It's not documented in the wiki but it looks like what you need is the ZO_ControlPool.

Use \zgoo and take a look at that table to see how it works. You can pass a virtual template to it as well.

An example of it in use is in both my Gatherer addon as well as Shinni's HarvestMap addon and the CustomCompassPins library addon.

Take a browse through those and see if you can see how it will work in your situation.

As a taster, this is a portion of the control pool class in action in my addon. Half of the functionality is dealt with behind the scenes by ESO's ControlPool class but there are a few classes based off of it like ZO_MapPin if I remember rightly, although I couldn't use that sub class myself so stuck with the control pool for functionality. In the New() function the XRGACompassPin is my template control to use for this class and ZO_CompassContainer the parent. The "Pin" I suspect is a suffix to the internally generated control name which then has a number added to it. This last info is based on the one error I received that implied the generated pin control ended with Pin01.

Lua Code:
  1. local CompassPinManager = ZO_ControlPool:Subclass()
  2.  
  3. -- Create the compass manager pin using the template in the xml file
  4. function CompassPinManager:New( ... )
  5.     local CompassManager = ZO_ControlPool.New(self, "XRGACompassPin", ZO_CompassContainer, "Pin")
  6.     CompassManager:Initialize( ... )   
  7.     return CompassManager
  8. end
  9.  
  10. -- Initialise the new compass manager and link it to the map pin manager
  11. function CompassPinManager:Initialize(MapPinManager)
  12.     self.Data = {}
  13.     self.Layouts = {}
  14.     self.Filters = {}
  15.     self.MapPinManager = MapPinManager
  16. end
  17.  
  18. -- Create the requested pinType and set the layout
  19. function CompassPinManager:CreatePinType( pinType, pinLayout )
  20.     if not self.Data and not self.Layouts and not self.MapPinManager then return end
  21.     pinLayout.size = 20
  22.     self.Layouts[ pinType ] = pinLayout
  23.     self.Data[ pinType ] = {}
  24. end
  25.  
  26. -- Create the specific pin at the specified location
  27. function CompassPinManager:CreatePin( pinType, pinX, pinY, pinZ, pinRadius )
  28.     if not self.Data and not self.Layouts and not self.MapPinManager then return end
  29.     local pin, pinKey = self:AcquireObject()
  30.     table.insert( self.Data[ pinType ], pinKey )
  31.    
  32.     pin.x = pinX
  33.     pin.y = pinY
  34.     pin.z = pinZ
  35.     pin.radius = pinRadius
  36.     pin.type = pinType
  37.     local layout = self.Layouts[ pinType ]
  38.     local texture = pin:GetNamedChild( "Background" )
  39.     texture:SetTexture( layout.texture )
  40. end
  41.  
  42. -- Remove the selected pin type
  43. function CompassPinManager:RemovePins( pinType )
  44.     if not self.Data then return end
  45.     if not pinType then
  46.         self:ReleaseAllObjects()
  47.         for pinType, pinData in pairs( self.Data ) do
  48.             self.Data[ pinType ] = {}
  49.         end
  50.     else
  51.         for pinID, pinKey in pairs( self.Data[ pinType ] ) do
  52.             self:ReleaseObject( pinKey )
  53.         end
  54.         self.Data[ pinType ] = {}
  55.     end
  56. end

Once something similar to the above is set up a simple call to the following will allow you to use it:

local compassPinManager = CompassPinManager:New()

Then all you will need to use is the compassPinManager to work on a manager instance that will handle all objects of that 'type'. As you can see there are calls to AcquireObject and ReleaseObject. These will work behind the scenes to release and locate the next available object to use. There are probably more functions that can be customised as needed but I didn't need to use them all so I didn't use them.

If you are used to release and recreate you may be able to figure this functionality out pretty quickly based on the function names. Hopefully this will help you start your research off.
  Reply With Quote
04/27/14, 08:12 PM   #6
elblobbo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 5
Originally Posted by Divona View Post
Would set control to "nil" do the job?

myControl = nil
I thought about that, and assuming there is some sort of garbage collection, I would think it should work.
Thanks for the suggestion
  Reply With Quote
04/27/14, 08:13 PM   #7
elblobbo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 5
Originally Posted by Seerah View Post
Don't continuously create more and more new labels. Reuse the one you have.
BTW.. thanks for the reply and suggestion.
  Reply With Quote
04/27/14, 08:17 PM   #8
elblobbo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 5
Originally Posted by Xrystal View Post
It's not documented......
Wow.. thanks.. I'll definitely take a look. My control is just a fishing add/on, so nothing cool, but if you fish, it's kinda helpful. I noticed there are some upcoming changes on the PTS that might really help and make my add/on worthwhile so I have some time to mess with things waiting for the fishing changes to be pushed live so I can see how they affect my add/on and hopefully make it better.
  Reply With Quote
04/27/14, 10:16 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Yep. The pool will keep track of how many controls are in use and only create new ones when you need them. If you don't need a control and release it, it goes back into the pool for you to use later.

You cannot destroy a control once created. You can nil out any reference to it, and it will eventually be picked up by the garbage collector, but reusing your controls is the best way to do this.

Reduce, Reuse, Recycle.
  Reply With Quote
04/28/14, 02:37 PM   #10
Sideshow
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 36
This is the thread where I learned
http://www.esoui.com/forums/showthread.php?t=143
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » Delete a control


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off