Thread Tools Display Modes
02-06-12, 02:54 AM   #1
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Convert xml to lua

Can someone convert the following to LUA? Im not really sure whats going on here in xml... i went all lua early on... now this looks odd to me.

Code:
<?xml version="1.0"?>
<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/">
  <Frame name="AudioFrames" hidden="true" virtual="true" parent="UIParent">
    <!-- set the parameters for the layers of the frame -->
    <Backdrop bgFile="Interface\TutorialFrame\TutorialFrameBackground" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true">
      <EdgeSize>
        <AbsValue val="16"/>
      </EdgeSize>
      <TileSize>
        <AbsValue val="32"/>
      </TileSize>
      <BackgroundInsets>
        <AbsInset left="5" right="5" top="5" bottom="5"/>
      </BackgroundInsets>
    </Backdrop>
  </Frame>
  <Frame>
    <Frames>
      <Frame name="Audio_UI_Control" enableMouse="true" movable="true" hidden="false" inherits="AudioFrames">
        <Size>
          <AbsDimension x="20" y="100"/>
        </Size>
        <Anchors>
          <Anchor point="LEFT"/>
        </Anchors>
        <Scripts>
		<OnMouseUp>
		 if ( this.isMoving ) then
		  this:StopMovingOrSizing();
		  this.isMoving = false;
		 end
		</OnMouseUp>
		<OnMouseDown>
		 if ( ( ( not this.isLocked ) or ( this.isLocked == 0 ) ) and ( arg1 == "LeftButton" ) ) then
		  this:StartMoving();
		  this.isMoving = true;
		 end
		</OnMouseDown>
		<OnHide>
		 if ( this.isMoving ) then
		  this:StopMovingOrSizing();
		  this.isMoving = false;
		 end
		</OnHide>
		</Scripts>

        <Frames>
        
          
          <Frame name="$parent_Volume">
            <Anchors>
              <Anchor point="BOTTOMLEFT" relativeTo="$parent" relativePoint="BOTTOMLEFT">
                <Offset>
                  <AbsDimension x="5" y="5"/>
                </Offset>
              </Anchor>
              <Anchor point="RIGHT" relativeTo="$parent" relativePoint="RIGHT">
                <Offset>
                  <AbsDimension x="-5" y="-5"/>
                </Offset>
              </Anchor>
            </Anchors>
            <Layers>
              <Layer level="BACKGROUND">
                <Texture name="$parent_Background" setAllPoints="true">
                  <!-- Texture has blue color wit 50% alpha -->
                  <Color r="0" g="1" b="1" a="0.5"/>
                </Texture>
              </Layer>
            </Layers>
         
          </Frame>
          
          
          <Button name="$parent_VolumeMask">
            <Anchors>
              <Anchor point="BOTTOMLEFT" relativeTo="$parent" relativePoint="BOTTOMLEFT">
                <Offset>
                  <AbsDimension x="5" y="5"/>
                </Offset>
              </Anchor>
              <Anchor point="TOPRIGHT" relativeTo="$parent" relativePoint="TOPRIGHT">
                <Offset>
                  <AbsDimension x="-5" y="-5"/>
                </Offset>
              </Anchor>
            </Anchors>
            <Layers>
              <Layer level="ARTWORK">
                <Texture name="$parent_Background" setAllPoints="true">
                  <!-- Texture has blue color wit 50% alpha -->
                  <Color r="1" g="0" b="0" a="0.2"/>
                </Texture>
              </Layer>
            </Layers>
    
          </Button>
          
        </Frames>
      </Frame>
      
      
      
      
      <CheckButton name="Audio_UI_Mute" hidden="false">
        <Size>
          <AbsDimension y="20"/>
        </Size>
        <Anchors>
          <Anchor point="TOPLEFT" relativeTo="Audio_UI_Control" relativePoint="BOTTOMLEFT"/>
          <Anchor point="TOPRIGHT" relativeTo="Audio_UI_Control" relativePoint="BOTTOMRIGHT"/>
        </Anchors>
        <NormalTexture name="$parent_Background" file="Interface\COMMON\VOICECHAT-SPEAKER">
        </NormalTexture>
        <CheckedTexture name="$parent_Background" file="Interface\COMMON\VOICECHAT-MUTED">
        </CheckedTexture>
      </CheckButton>
      
    </Frames>
    <Scripts>
      <OnLoad>
        Audio_OnLoad()
      </OnLoad>
    </Scripts>
  </Frame>
</Ui>
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
02-06-12, 06:15 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
That XML shouldn't even produce any visible frames... everything is parented to one frame that has no name (so no practical way to access it from code) and no position/dimensions (so it won't be shown anywhere, and neither will its children).

Also, that XML defined a template that was only used once, and did nothing besides set a backdrop for frames inheriting it, so I deemed it pointless and removed it.

Otherwise, nothing changed. Several things did not make sense, or seemed vaguely wrong; those have been noted with comments in the code:

Lua Code:
  1. local frame = CreateFrame("Frame")
  2. -- Here you need to add some code to give this frame dimensions
  3. -- so it will be displayed. Either set dimensions explicitly using
  4. -- SetSize (or SetWidth + SetHeight) and then set one point with
  5. -- SetPoint, or set enough points with SetPoint to imply dimensions.
  6. -- It also needs a parent (probably UIParent), and you should
  7. -- probably give it a name to make it more easily accessed from
  8. -- other code.
  9.  
  10. do
  11.     -- Set up audio controls
  12.  
  13.     local SIMPLE_BACKDROP = { bgFile = [[Interface\BUTTONS\WHITE8X8]] }
  14.         -- Only define this table once, to avoid wasting memory.
  15.  
  16.     local audio = CreateFrame("Frame", "Audio_UI_Control", frame)
  17.     audio:SetPoint("LEFT")
  18.     audio:SetSize(20, 100)
  19.     audio:EnableMouse(true)
  20.     audio:SetMovable(true)
  21.     audio:RegisterForDrag("LeftButton")
  22.     audio:SetScript("OnDragStart", audio.StartMoving)
  23.     audio:SetScript("OnDragStop", audio.StopMovingOrSizing)
  24.     audio:SetScript("OnHide", audio.StopMovingOrSizing)
  25.     audio:SetBackdrop({
  26.         bgFile = [[Interface\TutorialFrame\TutorialFrameBackground]], tile = true, tileSize = 32,
  27.         edgeFile = [[Interface\DialogFrame\UI-DialogBox-Border]], edgeSize = 16,
  28.         insets = { left = 5, right = 5, top = 5, bottom = 5 }
  29.     })
  30.     frame.audio = audio
  31.  
  32.     local volume = CreateFrame("Frame", "$parent_Volume", audio)
  33.         -- Personally, I would not bother giving these child frames
  34.         -- global names, since they are accessible through "parent.key"
  35.         -- more efficiently; in this case, "Audio_UI_Control.volume".
  36.     volume:SetPoint("BOTTOMLEFT", 5, 5)
  37.     volume:SetPoint("RIGHT", -5, -5)
  38.     volume:SetBackdrop(SIMPLE_BACKDROP)
  39.     volume:SetBackdropColor(0, 1, 1, 0.5)
  40.     audio.volume = volume
  41.  
  42.     local mask = CreateFrame("Frame", "$parent_VolumeMask", audio)
  43.     mask:SetPoint("BOTTOMLEFT", 5, 5)
  44.     mask:SetPoint("TOPRIGHT", -5, -5)
  45.     mask:SetBackdrop(SIMPLE_BACKDROP)
  46.     mask:SetBackdropColor(1, 0, 0, 0.2)
  47.     audio.volumeMask = mask
  48. end
  49.  
  50. do
  51.     -- This seems like it should be parented to the audio frame, not
  52.     -- the anonymous parent frame; you should probably change "frame"
  53.     -- to "audio" in the lines below.
  54.  
  55.     local mute = CreateFrame("CheckButton", "Audio_UI_Mute", frame)
  56.     mute:SetPoint("TOPLEFT", frame, "BOTTOMLEFT")
  57.     mute:SetPoint("TOPRIGHT", frame, "BOTTOMRIGHT")
  58.     mute:SetHeight(20)
  59.     mute:SetNormalTexture([[Interface\COMMON\VOICECHAT-SPEAKER]])
  60.     mute:SetCheckedTexture([[Interface\COMMON\VOICECHAT-MUTED]])
  61.     frame.mute = mute
  62. end
  63.  
  64. -- OnLoad scripts do not work for frames created in Lua,
  65. -- so just call the function yourself:
  66. Audio_OnLoad()

Also, not tested in-game, so beware of typos.
  Reply With Quote
02-06-12, 07:11 AM   #3
Animor
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Mar 2011
Posts: 136
Phanx,

I take my helm off to you, for all your comprehensive answers in this forum.
  Reply With Quote
02-06-12, 07:20 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Anything to avoid doing work while I'm at work.
  Reply With Quote
02-06-12, 10:21 AM   #5
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Originally Posted by Phanx View Post
Anything to avoid doing work while I'm at work.
Haha

Thank you very much btw, makes much more sense now that its lua. It was really old code that may be part of what was wrong with it.
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
02-06-12, 12:19 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Pretty much any addon using XML for anything other than templates qualifies as "really old code", or at least code that has no excuse to be written at any point since all of the Lua frame methods were added in WoW 2.0 or something.
  Reply With Quote
02-06-12, 04:54 PM   #7
Animor
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Mar 2011
Posts: 136
btw, the book "World of Warcraft Programming" teaches to create almost everything with XML frames.
  Reply With Quote
02-06-12, 06:57 PM   #8
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Originally Posted by Animor View Post
btw, the book "World of Warcraft Programming" teaches to create almost everything with XML frames.
Not everything, it has a section on lua. At least the first edition did. I have not seen the 2nd edition but i would imagine they touch even more on lua in it. It has both, but your right its initial take is to use xml. I recommend the book i found it helpful when i got started and its still a good reference. I had it... its in storage now but thats another story.
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
02-06-12, 08:05 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, that book was originally published back in the Burning Crusade, and was most likely written well before that. It's the same with a lot of large, older addons that are still around... if it was originally written with XML, well, the XML still works, and it would be a lot of work to go back and rewrite it in pure Lua. I'm sure there's nothing wrong with the information in the book; it's just unnecessary extra stuff to learn.

Especially when you're just starting out with addon programming, I just don't see the point in having to learn two syntaxes when, in the vast majority of cases, XML is completely unnecessary and offers no real benefits over using Lua only. On the other hand, there are real benefits to using Lua only, such as the ability to create frames on demand, rather than only at load-time.

For example, if you're building a list-like UI, you may not know how many list items you will need. Using XML, you would have to pick a maximum number, and create that many list items at login. If the user never needed that many list items, then all those extra frames are just occupying memory for no reason. If the user needs more list items, they're out of luck. Using Lua, you can start out with no list items sitting in memory, and create them on the fly as they are needed; this way, the user can have as many list items as they need, and never has to dedicate resources to list items they don't need. You could, theoretically, define a "template" list item in XML, and then generate copies of it as needed in Lua, but if you're already doing that, why bother with the XML at all?

Plus, XML is ridiculously verbose... in this thread, the original XML is twice as many lines of code as its equivalent in pure Lua. Less scrolling = better readability.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Convert xml to lua


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