For an example, here is how you would replicate your first (smaller) XML file in Lua alone:
Code:
local frame = CreateFrame("Frame", "MyAddonFrame", UIParent) -- [1]
frame:SetSize(184, 96) -- [2]
frame:SetPoint("CENTER", -8, 52)
frame:SetBackdrop({
bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background", tile = true, tileSize = 32,
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border", edgeSize = 32,
insets = { left = 11, right = 12, top = 12, bottom = 11 }
})
local button = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate") -- [3]
button:SetText("Button1")
button:SetSize(75, 23)
button:SetScript("OnClick", function(self, mouseButton)
-- [4]
end)
frame.button = button -- [5]
local text = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
text:SetText("Hello, World!")
text:SetSize(80, 20)
text:SetPoint("TOPLEFT", 50, -12)
frame.text = text -- [6]
Notes:
[1] The
name value you provide when creating a frame is set as a global variable, and all addons (plus the default UI code) share the same global namespace. A global name like "Frame1" is bad for several reasons. First, it's likely to conflict with globals (intentional or otherwise) from other addons or the default UI. Second, when users are trying to figure out which addon a frame belongs to (for example, if they want to move the frame, they need to know where to look for options) just seeing "Frame1" in a /fstack tooltip doesn't help them. A better name would be something like "MyAddonFrame" where "MyAddon" is the name of your addon -- this is both helpful to users, and unlikely to conflict with anyone else's code.
[2] object:SetSize(width, height) is shorthand for object:SetWidth(width) and object:SetHeight(height).
[3] Typically only your main frame should have a global name, and its child frames and regions should not have global names. The exception to this rule is when inheriting from a Blizzard template that expects a name. However, the "UIPanelButtonTemplate" template doesn't expect a name (none of its child regions are named, and none of its scripts identify it or its child regions with :GetName() calls) this button doesn't need one.
[4] Put the contents of the "Button1_OnClick" function here, and avoid such generic global function names in the future. The stuff in [1] applies equally to all globals, regardless of whether they're the names of frames, the names of functions, or anything else.
[5] This will let you refer to "frame.button" later instead of needing the "local button" to be in scope or needing to do a global lookup on the button's name if it had one. If you're never going to do anything with the button from other parts of the code, you can just leave this line out.
[6] Same as [5].