Quantcast How to create a Template using lua? - WoWInterface
Thread Tools Display Modes
04-06-13, 04:40 AM   #1
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
How to create a Template using lua?

Hi there guys.

Pretty much every guide i read and learned includes XML, however nowadays everyone prefers/recommends using lua only.

Creating a Template i XML is rather simple, however i would like to learn how to create one in lua.. I searched the web abit but couldn't seem to find any guides on how to do so. Can anyone give me an example of a Template made with lua.. Ty
  Reply With Quote
04-06-13, 05:05 AM   #2
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,289
Originally Posted by fRodzet View Post
Hi there guys.

Pretty much every guide i read and learned includes XML, however nowadays everyone prefers/recommends using lua only.

Creating a Template i XML is rather simple, however i would like to learn how to create one in lua.. I searched the web abit but couldn't seem to find any guides on how to do so. Can anyone give me an example of a Template made with lua.. Ty
I dont think you can do it atm.
  Reply With Quote
04-06-13, 05:19 AM   #3
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Resike View Post
I dont think you can do it atm.
So templates is XML only?

Well it was worth the try.. then imma stick to creating frames etc in lua and keep the xml file as low as possible by only creating templates in xml.
  Reply With Quote
04-06-13, 05:27 AM   #4
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,289
Originally Posted by fRodzet View Post
So templates is XML only?

Well it was worth the try.. then imma stick to creating frames etc in lua and keep the xml file as low as possible by only creating templates in xml.
Also you have to create the inhertited frame/button which points to the template in xml too.
  Reply With Quote
04-06-13, 05:31 AM   #5
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Resike View Post
Also you have to create the inhertited frame/button which points to the template in xml too.
True that is!

Creating Templates in XML is easy anyways, but for longer codes i think XML gets really messy and adding comments (<!-- xxx -->) in XML doesn't seem to make it more readable to me.
  Reply With Quote
04-06-13, 05:38 AM   #6
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,289
Originally Posted by fRodzet View Post
True that is!

Creating Templates in XML is easy anyways, but for longer codes i think XML gets really messy and adding comments (<!-- xxx -->) in XML doesn't seem to make it more readable to me.
True that, i recently formatted a 4000+ line xml file, was fun, but at the end i could do it with closed eyes too. Some addons even use 14000-15000+ line xml codes too.
  Reply With Quote
04-06-13, 05:50 AM   #7
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
Couldn't you create a function that creates the frame and then call on that function every time you create such a frame?

Lua Code:
  1. -- Creates an ordinary frame, just more awesome
  2. function CreateAwesomeFrame(name)
  3.     local f = CreateFrame("Frame", name, UIParent)
  4.     f:SetSize(128, 128) -- this is the really awesome bit
  5.     f:SetBackdrop({
  6.         bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
  7.         insets = {top = 0, left = 0, bottom = 0, right = 0},
  8.     })
  9.     f:SetBackdropColor(1, 1, 1, 0)
  10.     f:SetPoint("CENTER")
  11.    
  12.     return f
  13. end
  14.  
  15. local AwesomeFrame = CreateAwesomeFrame()
__________________
Oh, the simulated horror!
  Reply With Quote
04-06-13, 06:03 AM   #8
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,289
Originally Posted by Ailae View Post
Couldn't you create a function that creates the frame and then call on that function every time you create such a frame?

Lua Code:
  1. -- Creates an ordinary frame, just more awesome
  2. function CreateAwesomeFrame(name)
  3.     local f = CreateFrame("Frame", name, UIParent)
  4.     f:SetSize(128, 128) -- this is the really awesome bit
  5.     f:SetBackdrop({
  6.         bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
  7.         insets = {top = 0, left = 0, bottom = 0, right = 0},
  8.     })
  9.     f:SetBackdropColor(1, 1, 1, 0)
  10.     f:SetPoint("CENTER")
  11.    
  12.     return f
  13. end
  14.  
  15. local AwesomeFrame = CreateAwesomeFrame()
Intresting, but i'm not sure it would work with all UI elements.
  Reply With Quote
04-06-13, 06:22 AM   #9
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Resike View Post
True that, i recently formatted a 4000+ line xml file, was fun, but at the end i could do it with closed eyes too. Some addons even use 14000-15000+ line xml codes too.
Chill dude! Did your eyes pop out?
  Reply With Quote
04-06-13, 06:24 AM   #10
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Ailae View Post
Couldn't you create a function that creates the frame and then call on that function every time you create such a frame?

Lua Code:
  1. -- Creates an ordinary frame, just more awesome
  2. function CreateAwesomeFrame(name)
  3.     local f = CreateFrame("Frame", name, UIParent)
  4.     f:SetSize(128, 128) -- this is the really awesome bit
  5.     f:SetBackdrop({
  6.         bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
  7.         insets = {top = 0, left = 0, bottom = 0, right = 0},
  8.     })
  9.     f:SetBackdropColor(1, 1, 1, 0)
  10.     f:SetPoint("CENTER")
  11.    
  12.     return f
  13. end
  14.  
  15. local AwesomeFrame = CreateAwesomeFrame()
Imma try that when i get home!

Ill tell you if it works
  Reply With Quote
04-06-13, 12:48 PM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,703
Originally Posted by Ailae View Post
Couldn't you create a function that creates the frame and then call on that function every time you create such a frame?
Yes, we call that a frame factory or a factory function. You can see an example of its usage with my sStats addon. The core sStats addon has the factory function, while the display modules use it to create their frames.

However, iirc, if you want to create secure frames/buttons you need to use XML.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
04-06-13, 01:57 PM   #12
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
Originally Posted by Seerah View Post
Yes, we call that a frame factory or a factory function. You can see an example of its usage with my sStats addon. The core sStats addon has the factory function, while the display modules use it to create their frames.

However, iirc, if you want to create secure frames/buttons you need to use XML.
Yeah, I too use them just like that. I find it much easier then mucking about with XML-files.

I'm fairly sure you don't even need to use XML for secure stuff either.
__________________
Oh, the simulated horror!
  Reply With Quote
04-06-13, 02:01 PM   #13
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 756
XML is not required for secure buttons/frames. You can use templates (which are xml).
http://www.wowpedia.org/SecureActionButtonTemplate
__________________
The cataclysm broke the world ... and the pandas could not fix it!

Last edited by Rilgamon : 04-06-13 at 02:04 PM.
  Reply With Quote
04-06-13, 06:17 PM   #14
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
The only time you need to use XML is if you are creating secure templates (virtual frames), and the only time you need to do that is if you are working with the secure header system -- for example, setting up a secure header for party frames where each unit frame will include an attached child unit frame for the party member's pet. See attached awesome MSPaint drawing for a better explanation.
Attached Thumbnails
Click image for larger version

Name:	childframetemplate.png
Views:	767
Size:	9.3 KB
ID:	7661  
__________________
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote
04-07-13, 10:00 AM   #15
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,289
Originally Posted by Phanx View Post
The only time you need to use XML is if you are creating secure templates (virtual frames), and the only time you need to do that is if you are working with the secure header system -- for example, setting up a secure header for party frames where each unit frame will include an attached child unit frame for the party member's pet. See attached awesome MSPaint drawing for a better explanation.
And how can you import blizzard templates by only using lua?
  Reply With Quote
04-07-13, 12:21 PM   #16
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,257
Originally Posted by Resike View Post
And how can you import blizzard templates by only using lua?
You use the 'inherit' parameter of CreateFrame("frameType"[, "frameName"[, parentFrame[, "inheritsFrame"[, id]]]])
  Reply With Quote
09-23-13, 07:50 PM   #17
asett
A Defias Bandit
Join Date: Aug 2013
Posts: 2
Template Named: "MyCloseButtonTemplate"
In Lua:
Code:
NoTemplate = CreateFrame
CreateFrame = function(arg1,arg2,arg3,arg4,...)
if arg4 == "MyCloseButtonTemplate" then
	local x = NoTemplate(arg1,arg2,arg3,nil,...) 
	x:SetScript("OnClick", function(self) self:GetParent():Hide() end)
	x:SetNormalTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Up")
	x:SetPushedTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Down")
	x:SetHighlightTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight")
	x:SetSize(32,32)
	x:SetFrameLevel(50);
	return x
else
return NoTemplate(arg1,arg2,arg3,arg4,...)
end
end
in XML

Code:
 <Button name="MyCloseButtonTemplate" virtual="true">
                <Size>
                        <AbsDimension x="32" y="32"/>
                </Size>
                <Scripts>
                        <OnClick>
                                HideUIPanel(this:GetParent());
                        </OnClick>
                </Scripts>
                <NormalTexture file="Interface\Buttons\UI-Panel-MinimizeButton-Up"/>
                <PushedTexture file="Interface\Buttons\UI-Panel-MinimizeButton-Down"/>
                <HighlightTexture file="Interface\Buttons\UI-Panel-MinimizeButton-Highlight" alphaMode="ADD"/>
 </Button>
  Reply With Quote
09-24-13, 04:23 AM   #18
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,739
Nifty. Are you sure that this is taint-secure?
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW) | TDMOG

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote
09-24-13, 04:51 PM   #19
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
It overwrites the global CreateFrame function with an insecure function, so it will definitely cause taint. I'd imagine this would cause major problems with the default raid frames, and probably any addons that use group headers, when someone joins your group in combat, for example.

There's not really any reason to fake a named template like that anyway, even if it could be done securely. Just write a standard factory function, as shown earlier in the thread.
__________________
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote
09-24-13, 06:53 PM   #20
asett
A Defias Bandit
Join Date: Aug 2013
Posts: 2
Originally Posted by Phanx View Post
There's not really any reason to fake a named template like that anyway, even if it could be done securely. Just write a standard factory function, as shown earlier in the thread.
Agree.
Templates are virtual xml frames and inheriting those frames is just copying their properties and scripts to your frame.
I am using my own function to create frames that allows me to customize my frames just like inheriting xml templates and "templates" are additional lua commands to the newly created frame. When you create "SimpleHTML" frame, you can even specify to automatically add font:

Lua Code:
  1. BuildFrame = function(Frame, Name, parent, xmlVirtualFrame, id, ...)
  2. local frame = CreateFrame(Frame, Name, parent, xmlVirtualFrame, id)
  3.  
  4. if Name == "SimpleHTML" then
  5.     frame:SetFont("Fonts\\FRIZQT__.TTF", 12)
  6. end
  7.  
  8. if tContains({...}, "MyMovingBarTemplate") then -- here you specify your own template
  9. frame:SetSize(self:GetParent():GetWidth(), 15) -- commands to edit your frame, I want to create template of moving bars for my frames
  10. frame:SetPoint("TOPLEFT", self:GetParent(), "TOPLEFT",0,0)
  11. frame:EnableMouse()
  12. frame:RegisterForDrag("LeftButton","RightButton")
  13. frame:SetScript("OnDragStart", function(self) self:GetParent():SetMovable();self:GetParent():StartMoving(); end)
  14. frame:SetScript("OnDragStop", function(self)  self:GetParent():StopMovingOrSizing(); end)
  15. end
  16. if tContains({...}, "MyCloseButtonTemplate") then
  17. -- another commands to edit a frame
  18. end
  19. return frame;
  20. end

I am beginner in lua and xml and I like this more than templates in xml.
You can inherit more than just one template.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How to create a Template using lua?

Thread Tools
Display Modes

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