Skip to content

Your Custom Widgets

wutname1 edited this page May 12, 2019 · 1 revision

Don't like flat buttons? Have idea for a custom widget? No problem!

You have 2 options here:

1. Mix StdUi with blizzard code.

There is absolutely no problem with that. For example:

local window = StdUi:Window(UIParent, 'Title', 300, 300);
window:SetPoint('CENTER');

local button = CreateFrame('Button', nil, window, 'UIPanelButtonTemplate'); -- window as parent
button:SetSize(80 ,22);
button:SetText('Button');
button:SetPoint('CENTER');
button:SetScript('OnClick', function()
    print('Test');
end);

local flat = StdUi:Button(window, 100, 20, 'Flat button'); -- window as parent
StdUi:GlueBelow(flat, button, 0, -5, 'CENTER'); -- make it display below first button
flat:SetScript('OnClick', function()
    print('Test');
end);

2. Register reusable widget

StdUi provides a way to register your own widgets in StdUi namespace. However keep in mind that overriding widgets is not possible. So either make sure to prefix your widget names or create new StdUi instance like this:

local StdUi = LibStub('StdUi'):NewInstance();

Now create your widget

-- self must be first argument
-- self is current instance of StdUi
StdUi:RegisterWidget('MyPanelButton', function(self, parent, width, height, text) 
    local button = CreateFrame('Button', nil, parent, 'UIPanelButtonTemplate');
    self:InitWidget(button);
    self:SetObjSize(button, width, height);
    -- your other code
    button:SetText(text);
    return button;
end);

RegisterWidget will return true if registering widget succeeds and false if it already exists within StdUi namespace. And use it:

local window = StdUi:Window(UIParent, 'Title', 300, 300);
window:SetPoint('CENTER');

-- when you use your widget in OOP way (:), you should not provide self argument
local nflat = StdUi:MyPanelButton(window, 150, 20, 'NOT flat button');
nflat:SetPoint('CENTER');

local nflat2 = StdUi:MyPanelButton(window, 150, 20, 'NOT flat button 2');
StdUi:GlueBelow(nflat2, nflat);

local nflat3 = StdUi:MyPanelButton(window, 150, 20, 'NOT flat button 3');
StdUi:GlueBelow(nflat3, nflat2);