Skip to content

Instantly share code, notes, and snippets.

@Meorawr
Last active January 11, 2021 23:13
Show Gist options
  • Save Meorawr/2a09795bd94cd6b5d99b13a0c036a6b0 to your computer and use it in GitHub Desktop.
Save Meorawr/2a09795bd94cd6b5d99b13a0c036a6b0 to your computer and use it in GitHub Desktop.
Backdrop Polyfill Example
local BackdropTemplatePolyfillMixin = {};
function BackdropTemplatePolyfillMixin:OnBackdropLoaded()
if not self.backdropInfo then
return;
end
if not self.backdropInfo.edgeFile and not self.backdropInfo.bgFile then
self.backdropInfo = nil;
return;
end
self:ApplyBackdrop();
if self.backdropColor then
local r, g, b = self.backdropColor:GetRGB();
self:SetBackdropColor(r, g, b, self.backdropColorAlpha or 1);
end
if self.backdropBorderColor then
local r, g, b = self.backdropBorderColor:GetRGB();
self:SetBackdropBorderColor(r, g, b, self.backdropBorderColorAlpha or 1);
end
if self.backdropBorderBlendMode then
self:SetBackdropBorderBlendMode(self.backdropBorderBlendMode);
end
end
function BackdropTemplatePolyfillMixin:OnBackdropSizeChanged()
if self.backdropInfo then
self:SetupTextureCoordinates();
end
end
function BackdropTemplatePolyfillMixin:ApplyBackdrop()
-- The SetBackdrop call will implicitly reset the background and border
-- texture vertex colors to white, consistent across all client versions.
self:SetBackdrop(self.backdropInfo);
end
function BackdropTemplatePolyfillMixin:ClearBackdrop()
self:SetBackdrop(nil);
self.backdropInfo = nil;
end
function BackdropTemplatePolyfillMixin:GetEdgeSize()
return self.backdropInfo.edgeSize or 39;
end
function BackdropTemplatePolyfillMixin:HasBackdropInfo(backdropInfo)
return self.backdropInfo == backdropInfo;
end
function BackdropTemplatePolyfillMixin:SetBorderBlendMode()
-- Deliberate no-op as the pre-9.x API doesn't support this.
end
function BackdropTemplatePolyfillMixin:SetupPieceVisuals()
-- Deliberate no-op as backdrop internals are handled C-side pre-9.x.
end
function BackdropTemplatePolyfillMixin:SetupTextureCoordinates()
-- Deliberate no-op as texture coordinates are handled C-side pre-9.x.
end
AddOn_BackdropTemplateMixin = CreateFromMixins(BackdropTemplateMixin or BackdropTemplatePolyfillMixin);
ADDON_BACKDROP_DIALOG_20_20_5555 = {
bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileEdge = true,
tileSize = 20,
edgeSize = 20,
insets = { left = 5, right = 5, top = 5, bottom = 5 },
};
<Ui>
<Script file="BackdropTemplate.lua"/>
<Frame name="AddOn_BackdropTemplate" mixin="AddOn_BackdropTemplateMixin" virtual="true">
<Scripts>
<OnLoad method="OnBackdropLoaded"/>
<OnSizeChanged method="OnBackdropSizeChanged"/>
</Scripts>
</Frame>
<!-- Example usage: -->
<Frame name="TestFrame" parent="UIParent" inherits="AddOn_BackdropTemplate">
<KeyValues>
<KeyValue key="backdropInfo" value="ADDON_BACKDROP_DIALOG_20_20_5555" type="global"/>
</KeyValues>
<Size x="300" y="300"/>
<Anchors>
<Anchor point="CENTER"/>
</Anchors>
<Scripts>
<OnLoad inherit="prepend">
-- In case you need custom OnLoad behaviour, ensure that
-- the inherit attribute is specified above.
print("Loaded!");
</OnLoad>
</Scripts>
</Frame>
</Ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment