Thread Tools Display Modes
11-05-20, 11:19 PM   #1
jdb5457
A Kobold Labourer
Join Date: Nov 2020
Posts: 1
Transforming ".xml" to ".lua"

Hello friends,

I'm having trouble turning a ".xml" code ".lua".
XML code is ...

Code:
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/ ..\FrameXML\UI.xsd">
        <Script file="DerangementPetBattleCooldowns.lua"/>
    
	<Frame name="DeePetBattleAuraTemplate" virtual="true">
		<Size x="33" y="32"/>
		<Layers>
			<Layer level="ARTWORK">
				<Texture parentKey="Icon">
					<Size x="30" y="30"/>
					<Anchors>
						<Anchor point="CENTER" relativeKey="$parent" relativePoint="CENTER" x="0" y="0"/>
					</Anchors>
				</Texture>
			</Layer>
			<Layer level="OVERLAY">
				<FontString parentKey="Duration" inherits="GameFontHighlight" justifyH="CENTER" justifyV="TOP">
					<Anchors>
						<Anchor point="TOPLEFT" relativePoint="BOTTOMLEFT" x="0" y="0"/>
						<Anchor point="TOPRIGHT" relativePoint="BOTTOMRIGHT" x="0" y="0"/>
						<Anchor point="BOTTOM" relativePoint="BOTTOM" x="0" y="-16"/>
					</Anchors>
				</FontString>
				<Texture parentKey="DebuffBorder" file="Interface\Buttons\UI-Debuff-Overlays">
					<Size>
						<AbsDimension x="33" y="32"/>
					</Size>
					<Anchors>
						<Anchor point="CENTER" relativeKey="$parent.Icon" relativePoint="CENTER"/>
					</Anchors>
					<TexCoords left="0.296875" right="0.5703125" top="0" bottom="0.515625"/>
					<Color r="1" g="0" b="0"/>
				</Texture>
			</Layer>
		</Layers>
		<Scripts>
			<OnEnter function="DeePetBattleAura_OnEnter"/>
			<OnLeave function="DeePetBattleAura_OnLeave"/>
		</Scripts>
	</Frame>
		
    	<Button name="DeePetBattleAbilityButtonTemplate" inherits="PetBattleActionButtonTemplate" motionScriptsWhileDisabled="true" virtual="true">
		<Frames>    <!-- these auxiliary frames are used only as anchor points, and to check which half-button the mouse is over -->
		    <Frame parentKey="topHalf">
                        <Anchors>
		            <Anchor point="TOPLEFT"/>
		            <Anchor point="BOTTOMRIGHT" relativePoint="RIGHT"/>
		        </Anchors>
		        <Scripts>
                            <OnEnter function="DeePetBattleAbilityButton_topHalf_OnEnter"/>
                            <OnLeave function="DeePetBattleAbilityButton_OnLeave"/>
		        </Scripts>
		    </Frame>
		    <Frame parentKey="bottomHalf">
                        <Anchors>
		            <Anchor point="TOPLEFT" relativePoint="LEFT"/>
		            <Anchor point="BOTTOMRIGHT"/>
		        </Anchors>
		    </Frame>
		</Frames>

		<Layers>
                    <Layer level="BACKGROUND" textureSubLevel="1">
                    	<Texture parentKey="Icon2">
                    	    <TexCoords left="0" right="1" top="0" bottom="0.5" />
                            <Anchors>
                                <Anchor point="TOPLEFT"/>
                                <Anchor point="BOTTOMRIGHT" relativePoint="RIGHT"/>
                            </Anchors>
                    	</Texture>
                    </Layer>

                    <Layer level="BORDER">
                        <Texture parentKey="topHalfBorder" file="Interface\Buttons\UI-Quickslot2">
                            <TexCoords left="0.1953125" right="0.7890625" top="0.625" bottom="0.7734375" />
                            <Anchors>
                                <Anchor point="TOPLEFT" relativePoint="LEFT" relativeKey="$parent.topHalf"/>
                                <Anchor point="BOTTOMRIGHT" relativePoint="RIGHT"/>
                            </Anchors>
                        </Texture>      
                        <Texture parentKey="bottomHalfBorder" file="Interface\Buttons\UI-Quickslot2">
                            <TexCoords left="0.1953125" right="0.7890625" top="0.7734375" bottom="0.625" />
                            <Anchors>
                                <Anchor point="TOPLEFT" relativePoint="LEFT"/>
                                <Anchor point="BOTTOMRIGHT" relativePoint="RIGHT" relativeKey="$parent.bottomHalf"/>
                            </Anchors>
                        </Texture>                          
                    </Layer>

                    <Layer level="OVERLAY" textureSubLevel="-1">
                        <FontString parentKey="Duration" inherits="GameFontHighlightLarge" justifyH="CENTER" justifyV="TOP">
                            <Anchors>
                                <Anchor point="LEFT"/>
                                <Anchor point="RIGHT"/>
                                <Anchor point="TOP" relativePoint="BOTTOM" x="0" y="-2"/>
                                <Anchor point="BOTTOM" relativePoint="BOTTOM" x="0" y="-18"/>
                            </Anchors>
                        </FontString>
                        <Texture parentKey="AuraBorder" file="Interface\Buttons\UI-Debuff-Overlays">
                            <Size>
                                <AbsDimension x="57" y="55"/>
                            </Size>
                            <Anchors>
                                <Anchor point="CENTER" relativeKey="$parent.Icon" relativePoint="CENTER"/>
                            </Anchors>
			    <TexCoords left="0.296875" right="0.5703125" top="0" bottom="0.515625"/>
                        </Texture>
                    </Layer>

                    <Layer level="OVERLAY">
                    	<Texture parentKey="BetterIcon2" file="Interface\PetBattles\BattleBar-AbilityBadge-Weak" hidden="true">
                    		<Size x="32" y="32"/>
                    		<Anchors>
                    			<Anchor point="TOPRIGHT" x="9" y="9"/>
                    		</Anchors>
                    	</Texture>
                    </Layer>
		</Layers>

		<Scripts>
                    <OnLoad function="DeePetBattleAbilityButton_OnLoad"/>
                    <OnEvent function="DeePetBattleAbilityButton_OnEvent"/>
                    <OnEnter function="DeePetBattleAbilityButton_OnEnter"/>
                    <OnLeave function="DeePetBattleAbilityButton_OnLeave"/>
		</Scripts>

                <NormalTexture parentKey="NormalTexture" file="Interface\Buttons\UI-Quickslot2">
                    <TexCoords left="0.1953125" right="0.7890625" top="0.1953125" bottom="0.7890625" />
                </NormalTexture>
                
		<PushedTexture file="Interface\Buttons\UI-Quickslot2">
                    <TexCoords left="0.1953125" right="0.7890625" top="0.1953125" bottom="0.7890625" />
                </PushedTexture>
                
		<HighlightTexture/>
	</Button>
	
	<Frame name="DeePetBattleAbilityGroupTemplate" virtual="true">
	    <Frames>
	        <Button parentKey="Button1" inherits="DeePetBattleAbilityButtonTemplate">
	            <Anchors>
	                <Anchor point="LEFT" relativePoint="LEFT"/>
	            </Anchors>
	        </Button>	        
	        <Button parentKey="Button2" inherits="DeePetBattleAbilityButtonTemplate">
	            <Anchors>
	                <Anchor point="CENTER" relativePoint="CENTER"/>
	            </Anchors>
	        </Button>	        
	        <Button parentKey="Button3" inherits="DeePetBattleAbilityButtonTemplate">
	            <Anchors>
	                <Anchor point="RIGHT" relativePoint="RIGHT"/>
	            </Anchors>
	        </Button>
	    </Frames>
	    
	    <Scripts>
    		<OnLoad function="DeePetBattleAbilityGroup_OnLoad"/>
            </Scripts>
	</Frame>
	
	<Frame name="DeePetBattlePetTemplate" virtual="true">
	    <Size>
                <AbsDimension x="115" y="36"/>
            </Size>
            
	    <Frames>
	        <Frame parentKey="Abilities" inherits="DeePetBattleAbilityGroupTemplate" setAllPoints="true"/>
	    </Frames>
	    
            <Scripts>
                <OnLoad function="DeePetBattlePet_OnLoad"/>
                <OnEvent function="DeePetBattlePet_OnEvent"/>
    		<OnShow function="DeePetBattlePet_OnShow"/>
            </Scripts>
	</Frame>
	
	<Frame name="DeePetBattleAllyPetTemplate" inherits="DeePetBattlePetTemplate" virtual="true">
	    <Frames>
	        <Frame parentKey="Auras">
                    <Frames>
                        <Frame parentkey="NextFrame" inherits="DeePetBattleAuraTemplate" hidden="true">
                            <Anchors>
            	                <Anchor point="RIGHT" relativePoint="RIGHT" x="0" y="0"/>
            	            </Anchors>
                        </Frame>
                    </Frames>
                    
                    <Anchors>
                        <Anchor point="TOPRIGHT" relativePoint="TOPLEFT" relativeKey="$parent.Abilities" x="-6" y="0"/>
                        <Anchor point="BOTTOMRIGHT" relativePoint="BOTTOMLEFT" relativeKey="$parent.Abilities" x="-6" y="0"/>
                        <Anchor point="LEFT" relativePoint="LEFT" relativeTo="UIParent"/>
                    </Anchors>
                </Frame>
	    </Frames>
	</Frame>
	
	<Frame name="DeePetBattleEnemyPetTemplate" inherits="DeePetBattlePetTemplate" virtual="true">
	    <Frames>
	        <Frame parentKey="Auras">
                    <Frames>
                        <Frame parentkey="NextFrame" inherits="DeePetBattleAuraTemplate" hidden="true">
                            <Anchors>
            	                <Anchor point="LEFT" relativePoint="LEFT" x="0" y="0"/>
            	            </Anchors>
                        </Frame>
                    </Frames>
                    
                    <Anchors>
                        <Anchor point="TOPLEFT" relativePoint="TOPRIGHT" relativeKey="$parent.Abilities" x="6" y="0"/>
                        <Anchor point="BOTTOMLEFT" relativePoint="BOTTOMRIGHT" relativeKey="$parent.Abilities" x="6" y="0"/>
                        <Anchor point="RIGHT" relativePoint="RIGHT" relativeTo="UIParent"/>
                    </Anchors>
                </Frame>
	    </Frames>
	</Frame>
	

	<Frame name="DeePetBattleFrame" parent="PetBattleFrame">
	    <Scripts>
	        <OnLoad function="DeePetBattleFrame_OnLoad"/>
	        <OnEvent function="DeePetBattleFrame_OnEvent"/>
	        <OnShow function="DeePetBattleFrame_OnShow"/>
	    </Scripts>
	    <Frames>
	        <Frame parentkey="Ally1" inherits="DeePetBattlePetTemplate">
	            <Anchors>
	                <Anchor point="TOPLEFT" relativeKey="$parent.$parent.ActiveAlly" relativePoint="TOPRIGHT" x="30" y="2"/>
	            </Anchors>
	            <Scripts>
	                <OnLoad>
	                    DeePetBattlePet_OnLoad(self, LE_BATTLE_PET_ALLY, 1);
	                </OnLoad>
	            </Scripts>
	        </Frame>
	        
	        <Frame parentkey="Ally2" inherits="DeePetBattleAllyPetTemplate">
	            <Anchors>
	                <Anchor point="RIGHT" relativeKey="$parent.$parent.Ally2" relativePoint="LEFT" x="-7" y="0"/>
	            </Anchors>
	            <Scripts>
	                <OnLoad>
	                    DeePetBattlePet_OnLoad(self, LE_BATTLE_PET_ALLY, 2);
	                </OnLoad>
	            </Scripts>
	        </Frame>
	        
	        <Frame parentkey="Ally3" inherits="DeePetBattleAllyPetTemplate">
	            <Anchors>
	                <Anchor point="RIGHT" relativeKey="$parent.$parent.Ally3" relativePoint="LEFT" x="-7" y="0"/>
	            </Anchors>
	            <Scripts>
	                <OnLoad>
	                    DeePetBattlePet_OnLoad(self, LE_BATTLE_PET_ALLY, 3);
	                </OnLoad>
	            </Scripts>
	        </Frame>
	        
	        
	        <Frame parentkey="Enemy1" inherits="DeePetBattlePetTemplate">
	            <Anchors>
	                <Anchor point="TOPRIGHT" relativeKey="$parent.$parent.ActiveEnemy" relativePoint="TOPLEFT" x="-30" y="2"/>
	            </Anchors>
	            <Scripts>
	                <OnLoad>
	                    DeePetBattlePet_OnLoad(self, LE_BATTLE_PET_ENEMY, 1);
	                </OnLoad>
	            </Scripts>
	        </Frame>
	        
	        <Frame parentkey="Enemy2" inherits="DeePetBattleEnemyPetTemplate">
	            <Anchors>
	                <Anchor point="LEFT" relativeKey="$parent.$parent.Enemy2" relativePoint="RIGHT" x="7" y="0"/>
	            </Anchors>
	            <Scripts>
	                <OnLoad>
	                    DeePetBattlePet_OnLoad(self, LE_BATTLE_PET_ENEMY, 2);
	                </OnLoad>
	            </Scripts>
	        </Frame>
	        
	        <Frame parentkey="Enemy3" inherits="DeePetBattleEnemyPetTemplate">
	            <Anchors>
	                <Anchor point="LEFT" relativeKey="$parent.$parent.Enemy3" relativePoint="RIGHT" x="7" y="0"/>
	            </Anchors>
	            <Scripts>
	                <OnLoad>
	                    DeePetBattlePet_OnLoad(self, LE_BATTLE_PET_ENEMY, 3);
	                </OnLoad>
	            </Scripts>
	        </Frame>
	        
	    </Frames>	        
	</Frame>
</Ui>
It's so difficult I can't do anything. Anyone who can help, if not all, a little?
  Reply With Quote
11-05-20, 11:40 PM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
As far as I know you can't create templates in code.

The rest can be gleaned from browsing the wowpedia site for script code examples.

Here is the HOWTO page that provides a few examples of what you might want to do in addons, there are a few frame related examples which will hopefully get you started.
https://wow.gamepedia.com/HOWTOs

And this link gets you to the CreateFrame page with some additional examples:
https://wow.gamepedia.com/API_CreateFrame


Is there a reason you need this in lua format ?
__________________
  Reply With Quote
11-06-20, 09:49 AM   #3
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
There is nothing wrong with using XML. If someone else has already created a template you want to use then it's a waste of effort duplicating it in any fashion. Once the widget has been created, it all ends up being the same in-game no matter the method used "read its parts and glue it together".

That said, you can use lua functions to build up widgets just as you would in XML. The "virtual" attribute means "Don't create unless specifically used" just like a function does nothing unless called.

Lua Code:
  1. local function DPBAuraTemplate(base, parent, name)
  2.     local f = CreateFrame(base, "$parent"..name, parent)
  3.     f.Icon = f:CreateTexture("parentIcon", "ARTWORK")
  4.     f.Duration = f:CreateFontstring("$parentDuration")
  5.     --- add the rest of the elements, position, size etc.
  6.    
  7.     return f -- return the frame
  8. end
  9.  
  10. local HelpAura1 = DPBAuraTemplate("Button", MyUnitFrame, "Aura1")
  11.  
  12. local HarmAura1 = DPBAuraTemplate("Frame", MyUnitFrame, "Aura21")
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-06-20 at 10:03 AM.
  Reply With Quote
11-06-20, 04:00 PM   #4
DahkCeles
A Cliff Giant
 
DahkCeles's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2020
Posts: 73
In certain narrow cases, XML is 'mandatory' if you need to absolutely need to create an XML template. Examples would be pins on the 8.0.1 world map; I don't think that can be done without an XML file (that is just three lines long).

Otherwise... you just have to go from this paradigm:

Code:
<Frame name="MyTemplate" virtual="true" hidden="true">
    <Scripts>
        <OnShow>
            print("see me now?")
        </OnShow>
    </Script>
    <!-- insert other stuff here to control its appearance -->
</Frame>
<Frame name="MyFrame1" implements="MyTemplate" id="1"></Frame>
<Frame name="MyFrame2" implements="MyTemplate" id="2"></Frame>
<Frame name="MyFrame3" implements="MyTemplate" id="3"></Frame>

To this one:

Lua Code:
  1. local function createMyFrame(id)
  2.   local frame = CreateFrame("Frame")
  3.   local frame:Hide()
  4.   frame:SetID(id)
  5.   frame:SetScript("OnShow", function()
  6.     print("see me now?")
  7.   end)
  8.   -- insert other stuff here to control its appearance
  9.   return frame
  10. end
  11.  
  12. local myFrame1 = createMyFrame(1)
  13. local myFrame2 = createMyFrame(2)
  14. local myFrame3 = createMyFrame(3)


But I would echo Fizzlemizz's remarks... there's nothing wrong with leaving something in XML if its already done.

Therefore, I would only advise converting it to Lua if you were making a more substantial change in the process.
  Reply With Quote
11-06-20, 04:20 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
The only time you need to consume an xml template is if you require something to work with the secure environment but that can be done in xml or lua.

Intrinsics would need to be created in XML but calling a function mimics doing that also.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-06-20 at 04:23 PM.
  Reply With Quote
11-06-20, 05:03 PM   #6
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
I never thought of templates in xmls as the equivalent of functions that create frames .. *doh* ..

However, although I can't find the source, I am sure I read somewhere that xml files load faster than lua files. Of course I may have imagined it rofl. But it did make me convert some of my rewritten addons to xml based frames with lua handling the functionality. Whether small addons will notice the difference if this is true, I am sure bigger addons will.
__________________
  Reply With Quote
11-06-20, 05:42 PM   #7
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Optimised for XML in 2004 and 2020, the noticeable difference is probably minimal these days, unless you have something creating lots frames at runtime. The largest single "batch" of frames are created before you see your character in-game.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-08-20 at 01:16 AM.
  Reply With Quote
11-08-20, 11:54 AM   #8
DahkCeles
A Cliff Giant
 
DahkCeles's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2020
Posts: 73
"The only time you need to consume an xml template"

---

It can also be the case if you are interfacing with Blizzard's code that assumes a template. For example, the world map will acquire pins using the name of the template for each pin type.

Of course, you could build your own world map that overrides this -- but that's a lot more work! So I guess I cannot fairly say the XML is 'mandatory' but perhaps 'most appropriate' for certain situations.
  Reply With Quote
11-08-20, 08:20 PM   #9
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
To create template in Lua, we should use classes, it's not a simple job to fulfill all requirements.

I can do that with my PLoop and Scorpio Lib, but normally only authors with full OOP background can follow that. So this is only an example for the first two template.

Lua Code:
  1. -- This line make sure you can write code in an isolated environment
  2. -- and use the features provided by the my Scorpio lib
  3. Scorpio "DerangementPetBattle" ""
  4.  
  5. -- define a template class based on the frame
  6. class "DeePetBattleAura" (function(_ENV)
  7.     inherit "Frame"
  8.  
  9.     local function OnEnter(self)
  10.         -- DeePetBattleAura_OnEnter
  11.         -- Use GetChild to access the child elements
  12.         self:GetChild("Duration"):Show()
  13.     end
  14.  
  15.     local function OnLeave(self)
  16.         -- DeePetBattleAura_OnLeave
  17.         -- Also can use the name to access the child if no conflict
  18.         self.Duration:Hide()
  19.     end
  20.  
  21.     -- The __Template__ is used to declare the child elements with type
  22.     __Template__{
  23.         Icon = Texture,
  24.         Duration = FontString,
  25.         DebuffBorder = Texture,
  26.     }
  27.     function __ctor(self)
  28.         -- The constructor will be used to do the init
  29.         -- Bind the event handlers
  30.         self.OnEnter = self.OnEnter + OnEnter
  31.         self.OnLeave = self.OnLeave + OnLeave
  32.     end
  33. end)
  34.  
  35. class "DeePetBattleAbilityButton" (function(_ENV)
  36.     inherit "Button"
  37.  
  38.     -- The __new is used to create the widget object
  39.     function __new(_, name, parent)
  40.         -- Since your template is fixed, should be done here
  41.         return CreateFrame("Button",  nil, parent, "PetBattleActionButtonTemplate")
  42.     end
  43.  
  44.     __Template__{
  45.         -- Skip
  46.     }
  47.     function __ctor(self)
  48.     end
  49. end)
  50.  
  51. -- The Style settings works like the xml
  52. -- but it's very simple to create a new skin
  53. -- and apply to all widget objects
  54. Style.UpdateSkin("Default", {
  55.     [DeePetBattleAura] = {
  56.         Icon = {
  57.             drawLayer = "ARTWORK",
  58.             size = Size(30, 30),
  59.             location = { Anchor("CENTER") }
  60.         },
  61.         Duration = {
  62.             drawLayer = "OVERLAY",
  63.             fontObject = GameFontHighlight,
  64.             justifyH = "CENTER",
  65.             justifyV = "TOP",
  66.             height = 16,
  67.             location = {
  68.                 Anchor("TOPLEFT", 0, 0, nil, "BOTTOMLEFT"),
  69.                 Anchor("TOPRIGHT", 0, 0, nil, "BOTTOMRIGHT"),
  70.             }
  71.         },
  72.         DebuffBorder = {
  73.             drawLayer = "OVERLAY",
  74.             file = [[Interface\Buttons\UI-Debuff-Overlays]],
  75.             size = Size(33, 32),
  76.             location = { Anchor("CENTER", 0, 0, "Icon") },
  77.             texCoords = RectType(0.296875, 0.5703125, 0, 0.515625),
  78.             color = Color(1, 0, 0),
  79.         }
  80.     }
  81. })
  82.  
  83. -- The usage is the same to object created by CreateFrame
  84. -- The diff is only the object is created by class
  85. local aura = DeePetBattleAura("Aura1", UIParent)

The doc can be found at github.com/kurapica/Scorpio, if you want give it a try.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Transforming ".xml" to ".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