+ Reply to Thread
Results 1 to 4 of 4

Thread: Repetable set of frames

  1. #1
    Rift Disciple
    Join Date
    Jul 2016
    Posts
    126

    Question Repetable set of frames

    I want to create a template for a repetable set of frames that i want to have lots of. For example:
    http://i.imgur.com/AnuXEMT.png

    The image contains 4 frames:
    Background, Strip, Texture and a Text.

    Can i create a template somehow in lua, so that i can simply access the frames by FrameName.Text, FrameName.Background, and so on, while keeping all the functions related to the singular frames? So that i can create a variable of this template, and then Frame.Background:SetPoint() the whole thing somewhere, then change the text with Frame.Text:SetText("LoL"), and all the other functions.

    This is a very C++ struct way, but i couldn't find a way to create structs in lua, since you dont define variable types in lua and it works differently from C++. (<3)

    If its not possible with a template-like solution, how would i go about achieving that, if i dont know how many of the frame sets i will need?
    Last edited by Alleja zaviel; 12-06-2016 at 02:05 PM.
    Quote Originally Posted by TrionBrasse View Post
    Accept that the egregious and obvious mistake you want fixed may be implemented by design and may not be a mistake at all.
    If we see it as an egregious and obvious mistake, maybe there should be more communication to tell us that it is not?
    Alleja Imalla Illvana Mjolln Ketllina <Servants of None> @zaviel

  2. #2
    Rift Disciple
    Join Date
    Sep 2013
    Posts
    131

    Default Using Metatables to Implement Classes

    This link has all of the information you need: http://lua-users.org/wiki/LuaClassesWithMetatable. Here's a fairly rough example:

    Code:
    local MyFrame = { -- you don't even need to define anything here but it makes it easier to see what members MyFrame has
    	Background,
    	Strip,
    	Texture,
    	Text,
    }
    
    function MyFrame:Create(name, parent)
    	local myFrame = UI.CreateFrame("Frame", name, parent)
    	myframe.Background = UI.CreateFrame("Frame", name .. ".Background", myframe)
    	myframe.Strip = UI.CreateFrame("Frame", name .. ".Strip", myframe)
    	myframe.Texture = UI.CreateFrame("Frame", name .. ".Texture", myframe)
    	myframe.Text = UI.CreateFrame("Frame", name .. ".Text", myframe)
    	
    	myframe.Background:SetAllPoints()
    	myFrame.Strip:SetPoint("TOPLEFT", myFrame, "TOPLEFT")
    	myFrame.Texture:SetPoint("TOPCENTER", myFrame.Strip, "BOTTOMCENTER", 0, 20) -- pin texture to bottom of strip
    	myFrame.Text:SetPoint("TOPLEFT", myFrame.Strip, "TOPLEFT", 10, 5) -- pin text to top-left of strip
    	-- do other stuff such as resizing and coloring
    	
    	-- functions
    	function myFrame:SetTitle(text)
    		self.Text:SetText(text)
    	end
    	
    	-- magic
    	setmetatable(myFrame, self)
    	self.__index = self
    
    	return myFrame
    end
    
    Usage
    
    local function DoSomething()
    	local frame1 = MyFrame:Create("Frame1", SomePreviouslyDefinedUIContext)
    	local frame2 = MyFrame:Create("Frame2", SomePreviouslyDefinedUIContext)
    	
    	frame1:SetTitle("Hello")
    	frame2:SetTitle("World")
    	
    	frame1:SetPoint("TOPLEFT", UIParent, "TOPLEFT")
    	frame2:SetPoint("TOPRIGHT", UIParent, "TOPRIGHT")
    end
    Last edited by Pancakedrawer; 12-06-2016 at 10:38 PM.

  3. #3
    Rift Disciple
    Join Date
    Jul 2016
    Posts
    126

    Default

    Thank you, that looks like exactly what i want. Will look into it and play around with it later.
    Quote Originally Posted by TrionBrasse View Post
    Accept that the egregious and obvious mistake you want fixed may be implemented by design and may not be a mistake at all.
    If we see it as an egregious and obvious mistake, maybe there should be more communication to tell us that it is not?
    Alleja Imalla Illvana Mjolln Ketllina <Servants of None> @zaviel

  4. #4
    General of Telara
    Join Date
    Mar 2014
    Posts
    952

    Default

    If I'm not totally mistaken, then frames already have a metatable, that's where all the functions you see in the API docs reside. So it's probably not a good idea to set a new one like this.

    However, you don't need the metatable to store a Text frame in a parent frame and do SomeFrame.Text:SetText(...), or to store a function like SetTitle() in every instance as in the Create() function example above.

    So, the above example doesn't actually use the metatable at all. You could store the SetTitle() and other "class member" functions there so you don't have to assign them individually to every instance, though as said, be aware of existing metatables of things not created by your own code.
    Dimension Addon 'Tinker Tools' is now available on Curse!
    Or grab the latest development code on GitHub.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts