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:
-- This line make sure you can write code in an isolated environment
-- and use the features provided by the my Scorpio lib
Scorpio "DerangementPetBattle" ""
-- define a template class based on the frame
class "DeePetBattleAura" (function(_ENV)
inherit "Frame"
local function OnEnter(self)
-- DeePetBattleAura_OnEnter
-- Use GetChild to access the child elements
self:GetChild("Duration"):Show()
end
local function OnLeave(self)
-- DeePetBattleAura_OnLeave
-- Also can use the name to access the child if no conflict
self.Duration:Hide()
end
-- The __Template__ is used to declare the child elements with type
__Template__{
Icon = Texture,
Duration = FontString,
DebuffBorder = Texture,
}
function __ctor(self)
-- The constructor will be used to do the init
-- Bind the event handlers
self.OnEnter = self.OnEnter + OnEnter
self.OnLeave = self.OnLeave + OnLeave
end
end)
class "DeePetBattleAbilityButton" (function(_ENV)
inherit "Button"
-- The __new is used to create the widget object
function __new(_, name, parent)
-- Since your template is fixed, should be done here
return CreateFrame("Button", nil, parent, "PetBattleActionButtonTemplate")
end
__Template__{
-- Skip
}
function __ctor(self)
end
end)
-- The Style settings works like the xml
-- but it's very simple to create a new skin
-- and apply to all widget objects
Style.UpdateSkin("Default", {
[DeePetBattleAura] = {
Icon = {
drawLayer = "ARTWORK",
size = Size(30, 30),
location = { Anchor("CENTER") }
},
Duration = {
drawLayer = "OVERLAY",
fontObject = GameFontHighlight,
justifyH = "CENTER",
justifyV = "TOP",
height = 16,
location = {
Anchor("TOPLEFT", 0, 0, nil, "BOTTOMLEFT"),
Anchor("TOPRIGHT", 0, 0, nil, "BOTTOMRIGHT"),
}
},
DebuffBorder = {
drawLayer = "OVERLAY",
file = [[Interface\Buttons\UI-Debuff-Overlays]],
size = Size(33, 32),
location = { Anchor("CENTER", 0, 0, "Icon") },
texCoords = RectType(0.296875, 0.5703125, 0, 0.515625),
color = Color(1, 0, 0),
}
}
})
-- The usage is the same to object created by CreateFrame
-- The diff is only the object is created by class
local aura = DeePetBattleAura("Aura1", UIParent)
The doc can be found at
github.com/kurapica/Scorpio, if you want give it a try.