Skip to content

Instantly share code, notes, and snippets.

@Meorawr
Last active December 24, 2023 11:51
Show Gist options
  • Save Meorawr/a82111c48761a48b1aa837aff8447162 to your computer and use it in GitHub Desktop.
Save Meorawr/a82111c48761a48b1aa837aff8447162 to your computer and use it in GitHub Desktop.
ScrollBox Selection Demo
local SelectionDemoListItemMixin = CreateFromMixins(CallbackRegistryMixin);
SelectionDemoListItemMixin:GenerateCallbackEvents(
{
"OnClick",
}
);
function SelectionDemoListItemMixin:OnLoad()
CallbackRegistryMixin.OnLoad(self);
self:SetScript("OnClick", self.OnClick);
end
function SelectionDemoListItemMixin:OnClick()
self:TriggerEvent("OnClick", self);
end
function SelectionDemoListItemMixin:Init(elementData)
self:SetText(elementData.text);
self:SetSelected(elementData.selected);
end
function SelectionDemoListItemMixin:SetSelected(selected)
self:SetEnabled(not selected);
end
---
local SelectionDemoListMixin = CreateFromMixins(CallbackRegistryMixin);
SelectionDemoListMixin:GenerateCallbackEvents(
{
"OnSelectionChanged",
}
);
function SelectionDemoListMixin:OnLoad()
CallbackRegistryMixin.OnLoad(self)
self.scrollView = CreateScrollBoxListLinearView();
self.scrollView:SetElementInitializer("UIPanelButtonTemplate", GenerateClosure(self.OnElementInitialize, self));
self.scrollView:SetElementResetter(GenerateClosure(self.OnElementReset, self));
self.scrollBox = CreateFrame("Frame", nil, self, "WowScrollBoxList");
self.scrollBox:SetPoint("TOPLEFT");
self.scrollBox:SetPoint("BOTTOMRIGHT", -24, 0);
self.scrollBar = CreateFrame("EventFrame", nil, self, "MinimalScrollBar");
self.scrollBar:SetPoint("TOPLEFT", self.scrollBox, "TOPRIGHT", 8, 0);
self.scrollBar:SetPoint("BOTTOMLEFT", self.scrollBox, "BOTTOMRIGHT", 8, 0);
ScrollUtil.InitScrollBoxListWithScrollBar(self.scrollBox, self.scrollBar, self.scrollView);
self.selectionBehavior = ScrollUtil.AddSelectionBehavior(self.scrollBox);
self.selectionBehavior:RegisterCallback("OnSelectionChanged", self.OnElementSelectionChanged, self);
end
function SelectionDemoListMixin:OnElementInitialize(element, elementData)
if not element.OnLoad then
Mixin(element, SelectionDemoListItemMixin);
element:OnLoad();
end
element:Init(elementData);
element:SetSelected(self.selectionBehavior:IsSelected(element));
element:RegisterCallback("OnClick", self.OnElementClicked, self);
end
function SelectionDemoListMixin:OnElementReset(element)
element:UnregisterCallback("OnClick", self);
end
function SelectionDemoListMixin:OnElementClicked(element)
self.selectionBehavior:Select(element);
end
function SelectionDemoListMixin:OnElementSelectionChanged(elementData, selected)
-- Trigger a visual update on the item that was just [de]selected prior
-- to notifying listeners of the change in selection.
local element = self.scrollView:FindFrame(elementData);
if element then
element:SetSelected(selected);
end
-- The below is set up such that we'll only notify listeners of our
-- *new* selection, and not that we've deselected something in order
-- to select something new first.
if selected then
self:TriggerEvent("OnSelectionChanged", elementData, selected);
end
end
function SelectionDemoListMixin:GetDataProvider()
return self.scrollView:GetDataProvider();
end
function SelectionDemoListMixin:SetDataProvider(dataProvider)
self.scrollView:SetDataProvider(dataProvider);
end
---
local SelectionDemoContentMixin = {};
function SelectionDemoContentMixin:SetContent(elementData)
self:SetText(elementData.content);
end
---
local SelectionDemoMixin = {};
function SelectionDemoMixin:OnLoad()
self.dataProvider = CreateDataProvider(
{
{ text = "Button 1", content = "Hello from page 1!" },
{ text = "Button 2", content = "Hello from page 2!" },
{ text = "Button 3", content = "Hello from page 3!" },
{ text = "Button 4", content = "Hello from page 4!" },
{ text = "Button 5", content = "Hello from page 5!" },
{ text = "Button 6", content = "Hello from page 6!" },
{ text = "Button 7", content = "Hello from page 7!" },
{ text = "Button 8", content = "Hello from page 8!" },
{ text = "Button 9", content = "Hello from page 9!" },
{ text = "Button 10", content = "Hello from page 10!" },
{ text = "Button 11", content = "Hello from page 11!" },
{ text = "Button 12", content = "Hello from page 12!" },
}
);
self.listView = Mixin(CreateFrame("Frame", nil, self), SelectionDemoListMixin);
self.listView:OnLoad();
self.listView:SetDataProvider(self.dataProvider);
self.listView:SetPoint("TOPLEFT");
self.listView:SetPoint("BOTTOMLEFT");
self.listView:SetWidth(200);
self.listView:RegisterCallback("OnSelectionChanged", self.OnListSelectionChanged, self);
self.contentView = Mixin(self:CreateFontString(nil, "OVERLAY", "GameFontNormal"), SelectionDemoContentMixin);
self.contentView:SetPoint("LEFT", self.listView, "RIGHT", 40, 0);
end
function SelectionDemoMixin:OnListSelectionChanged(elementData)
self.contentView:SetContent(elementData);
end
---
SelectionDemo = Mixin(CreateFrame("Frame", nil, UIParent), SelectionDemoMixin);
SelectionDemo:SetPoint("CENTER");
SelectionDemo:SetSize(400, 132);
SelectionDemo:OnLoad();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment