WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   XML to Lua? (https://www.wowinterface.com/forums/showthread.php?t=59841)

Codger 04-01-24 12:08 AM

XML to Lua?
 
So... I'm not very good with xml (old brain doesn't like it)
but I see a lot of UI examples in XML and I'm wondering if there is an easy way to convert them to lua code.

As an example I found this page https://us.forums.blizzard.com/en/wo...-xml/1488298/2

Gello posted some XML code and I'm trying to convert it into lua code.

Code:

<Ui>
    <Frame name="SimpleFrame" parent="UIParent" inherits="DefaultPanelTemplate">
        <Size x="150" y="150"/>
        <Anchors>
            <Anchor point="CENTER"/>
        </Anchors>
        <Frames>
            <Button parentKey="CloseButton" inherits="UIPanelCloseButtonDefaultAnchors"/>
                        <Frame parentKey="Inset" useParentLevel="true" inherits="InsetFrameTemplate">
                                <Anchors>
                                        <Anchor point="TOPLEFT" x="10" y="-26" />
                                        <Anchor point="BOTTOMRIGHT" x="-6" y="26" />
                                </Anchors>
                        </Frame>
            <Button parentKey="OkButton" inherits="UIPanelButtonTemplate" text="Okay">
                <Size x="90" y="22"/>
                <Anchors>
                    <Anchor point="BOTTOMRIGHT" x="-6" y="4"/>
                </Anchors>
                <Scripts>
                    <OnClick>
                        self:GetParent():Hide()
                    </OnClick>
                </Scripts>
            </Button>
        </Frames>
        <Scripts>
            <OnLoad>
                self.TitleContainer.TitleText:SetText("Simple Frame")
            </OnLoad>
        </Scripts>
    </Frame>
</Ui>
```
[/quote]

The frame part is simple enough but the button xml code has me baffled?
What would the lua code look like for the two buttons?

Codger 04-01-24 01:15 AM

Lua code for xml (found)
 
I went to ChatGPT https://chat.openai.com/ and asked them to convert the xml code to lua and the code below is the result.

Lua Code:
  1. local frame = CreateFrame("Frame", "SimpleFrame", UIParent, "BasicFrameTemplateWithInset")
  2. frame:SetSize(150, 150)
  3. frame:SetPoint("CENTER")
  4.  
  5. local closeButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
  6. closeButton:SetPoint("TOPRIGHT", -4, -4)
  7.  
  8. local insetFrame = CreateFrame("Frame", nil, frame, "InsetFrameTemplate")
  9. insetFrame:SetPoint("TOPLEFT", 10, -26)
  10. insetFrame:SetPoint("BOTTOMRIGHT", -6, 26)
  11.  
  12. local okButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
  13. okButton:SetText("Okay")
  14. okButton:SetSize(90, 22)
  15. okButton:SetPoint("BOTTOMRIGHT", -6, 4)
  16. okButton:SetScript("OnClick", function()
  17.     frame:Hide()
  18. end)
  19.  
  20. frame:SetScript("OnLoad", function()
  21.     frame.TitleText:SetText("Simple Frame")
  22. end)

Fizzlemizz 04-01-24 11:04 AM

In XML, where you see parentKey, that is convert to a key on the parent frame (funny that)
Code:

<Button parentKey="CloseButton" inherits="UIPanelCloseButtonDefaultAnchors"/>
Would become:
Lua Code:
  1. frame.CloseButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
  2. frame.CloseButton:SetPoint("TOPRIGHT", -4, -4)

Because the parent frame has a (not very unique) name you can access the child frame (CloseButton) via the parent's key

Lua Code:
  1. SimpleFrame.CloseButton:Click()
would be the same as the mouse clicking the close button

SDPhantom 04-01-24 06:15 PM

Quote:

Originally Posted by Codger (Post 343673)
I went to ChatGPT https://chat.openai.com/ and asked them to convert the xml code to lua and the code below is the result.

...

Lua Code:
  1. frame:SetScript("OnLoad", function()
  2.     frame.TitleText:SetText("Simple Frame")
  3. end)

The problem with this is OnLoad runs when CreateFrame() is called. Registering a script to it through Lua does nothing as it's never called again.

It's still a good idea to proofread any code GhatGPT spits out as it still makes mistakes. It sees parentKey and just shoves it in a local instead of assigning the object to that key in the parent's table, as Fizzlemizz pointed out. It also doesn't handle useParentLevel, which is to set the child's framelevel equal to its parent where by default, it's +1.


All times are GMT -6. The time now is 06:16 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI