Skip to content

Commit

Permalink
Moved Scrollable and Base Frame Creation to UIElements.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsj02 committed May 21, 2024
1 parent 7c958b1 commit e6deaa6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 64 deletions.
76 changes: 28 additions & 48 deletions Core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,11 @@ local function itemListInitializer(frame, data)
end

local function createFrame()
---@class GemsFrame : Frame
---@field CloseButton Button
---@field SetTitle fun(self:GemsFrame, title:string)
---@field Inset Frame
---@field TopTileStreaks Frame
local gems = CreateFrame("Frame", nil, CharacterFrame, "ButtonFrameTemplate")
gems:SetTitle(const.ADDON_NAME)
local gems = uiElements:CreateBaseFrame(CharacterFrame, {
title = const.ADDON_NAME,
width = 300
})
gems:RegisterEvent("BAG_UPDATE_DELAYED")
gems:SetWidth(300)

local frameToggle = CreateFrame("Frame", nil, CharacterFrame)
frameToggle:SetFrameStrata("HIGH")
Expand Down Expand Up @@ -168,14 +164,6 @@ local function createFrame()
settings:UpdateSetting("show_frame", not settings:GetSetting("show_frame"))
end)

ButtonFrameTemplate_HidePortrait(gems)
gems.CloseButton:Hide()
gems.Inset:ClearAllPoints()
gems.Inset:SetPoint("TOP", 0, -65)
gems.Inset:SetPoint("BOTTOM", 0, 35)
gems.Inset:SetPoint("LEFT", 20, 0)
gems.Inset:SetPoint("RIGHT", -20, 0)

---@class SearchFrame : EditBox
---@field Instructions FontString
local search = CreateFrame("EditBox", nil, gems, "InputBoxInstructionsTemplate")
Expand Down Expand Up @@ -290,52 +278,45 @@ local function createFrame()
helpButton:SetScript("OnEnter", function(self)
HelpTip:Show(self, { text = helpText })
end)
helpButton:SetScript("OnLeave", function (self)
helpButton:SetScript("OnLeave", function(self)
HelpTip:Hide(self)
end)
helpButton:SetScript("OnClick", function (self)
helpButton:SetScript("OnClick", function(self)
if IsLeftShiftKeyDown() then
settings:UpdateSetting("show_helpframe", false)
end
end)
helpButton:SetPoint("TOPRIGHT", 25, 25)

---@class ScrollBox : Frame
---@field GetScrollPercentage fun(self:ScrollBox)
---@field SetScrollPercentage fun(self:ScrollBox, percentage:number)
local scrollBox = CreateFrame("Frame", nil, gems, "WowScrollBoxList")
scrollBox:SetAllPoints(gems.Inset)

---@class MinimalScrollBar : EventFrame
---@field SetHideIfUnscrollable fun(self:MinimalScrollBar, state:boolean)
local scrollBar = CreateFrame("EventFrame", nil, gems, "MinimalScrollBar")
scrollBar:SetPoint("TOPLEFT", scrollBox, "TOPRIGHT", 5, 0)
scrollBar:SetPoint("BOTTOMLEFT", scrollBox, "BOTTOMRIGHT")
scrollBar:SetHideIfUnscrollable(true)


local scrollView = CreateScrollBoxListLinearView()
scrollView:SetElementInitializer("BackDropTemplate", itemListInitializer)
ScrollUtil.InitScrollBoxListWithScrollBar(scrollBox, scrollBar, scrollView)
scrollView:SetElementExtent(25)
local insetAnchorPoints = {
CreateAnchor("TOPLEFT", gems.Inset, "TOPLEFT"),
CreateAnchor("BOTTOMRIGHT", gems.Inset, "BOTTOMRIGHT")
}
local scrollBox, scrollView = uiElements:CreateScrollable(gems, {
anchors = {
with_scroll_bar = insetAnchorPoints,
without_scroll_bar = insetAnchorPoints,
},
initializer = itemListInitializer,
element_height = 25,
template = "BackDropTemplate",
type = "LIST",
})

function scrollView:UpdateTree(data)
local function updateTree(data)
if not scrollBox:IsVisible() then return end
if not data then return end
local scrollPercent = scrollBox:GetScrollPercentage()
self:Flush()
local dataProvider = CreateDataProvider()
self:SetDataProvider(dataProvider)
scrollView:UpdateContentData({})
for socketType, socketTypeData in pairs(data) do
if #socketTypeData > 0 then
local typeInfo = gemUtil:GetSocketTypeInfo(socketType)
if typeInfo then
dataProvider:Insert({
scrollView:UpdateContentData({ {
text = typeInfo.name,
isHeader = true,
icon = typeInfo.icon,
index = 0
})
} }, true)
sort(socketTypeData, misc.ItemSorting)
for itemIndex, itemInfo in ipairs(socketTypeData) do
local cachedInfo = cache:GetItemInfo(itemInfo.itemID)
Expand All @@ -344,23 +325,22 @@ local function createFrame()
if itemInfo.gemType == "Prismatic" then
txt = gemUtil:GetGemStats(cachedInfo.description)
end
dataProvider:Insert({
scrollView:UpdateContentData({ {
id = itemInfo.itemID,
icon = cachedInfo.icon,
text = txt or "",
index = itemIndex,
info = itemInfo,
cachedInfo = cachedInfo,
})
} }, true)
end
end
end
end
scrollBox:SetScrollPercentage(scrollPercent or 1)
end

local function selectionTreeUpdate()
scrollView:UpdateTree(gemUtil:GetFilteredGems(dropDown.selection, search:GetText() or ""))
updateTree(gemUtil:GetFilteredGems(dropDown.selection, search:GetText() or ""))
end

dropDown:SetCallback("selectionCallback", selectionTreeUpdate)
Expand Down Expand Up @@ -400,7 +380,7 @@ local function createFrame()
end
end)
gems:SetScript("OnHide", function()
scrollView:UpdateTree({})
updateTree({})
end)
gems:SetScript("OnShow", function(self)
selectionTreeUpdate()
Expand Down
60 changes: 44 additions & 16 deletions UIElements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ function uiElements:CreateIcon(parent, data)
button:SetScript("OnUpdate", updateCooldown)
button:SetScript("OnEnter", iconHoverEnter)
button:SetScript("OnLeave", iconHoverLeave)
button:SetScript("OnHide", function ()
button:SetScript("OnHide", function()
button:EnableMouse(false)
end)
button:SetScript("OnShow", function ()
button:SetScript("OnShow", function()
button:EnableMouse(true)
updateSpellCount(button)
updateItemCountOrSlot(button)
Expand Down Expand Up @@ -364,18 +364,6 @@ function uiElements:CreateIcon(parent, data)
return button
end

---@class Anchor

---@param point FramePoint
---@param relativeTo? any
---@param relativePoint? FramePoint
---@param offsetX? uiUnit
---@param offsetY? uiUnit
---@return Anchor
---@overload fun(point: AnchorPoint, relativeTo?: any, ofsx?: number, ofsy?: number):Anchor
---@overload fun(point: AnchorPoint, ofsx?: number, ofsy?: number):Anchor
function CreateAnchor(point, relativeTo, relativePoint, offsetX, offsetY) return {} end

---@class ScrollableAnchorSettings
---@field with_scroll_bar Anchor
---@field without_scroll_bar Anchor
Expand Down Expand Up @@ -429,7 +417,7 @@ function uiElements:CreateScrollable(parent, data)
local fillWidth = (parent:GetWidth() - (data.elements_per_row - 1) * data.element_padding) /
data.elements_per_row
scrollView = CreateScrollBoxListGridView(data.elements_per_row, 0, 0, 0, 0, data.element_padding,
data.element_padding);
data.element_padding);
scrollView:SetElementInitializer(data.template, function(button, elementData)
button:SetSize(data.fill_width and fillWidth or data.element_height, data.element_height)
data.initializer(button, elementData)
Expand Down Expand Up @@ -460,6 +448,7 @@ function uiElements:CreateScrollable(parent, data)

function scrollView:UpdateContentData(contentData, keepOldData)
if not contentData then return end
if not scrollBox:IsVisible() then return end
local scrollPercent = scrollBox:GetScrollPercentage()
local dataProvider = self:GetDataProvider()
if not dataProvider then
Expand All @@ -477,4 +466,43 @@ function uiElements:CreateScrollable(parent, data)
end

return scrollBox, scrollView, scrollBar
end
end

---@class BaseFrameSettings
---@field width number?
---@field height number?
---@field points table?
---@field title string?
---@field showPortrait boolean?
---@field isClosable boolean?

---@param parent Frame
---@param data BaseFrameSettings
function uiElements:CreateBaseFrame(parent, data)
---@class BaseFrame : Frame
---@field CloseButton Button
---@field SetTitle fun(self:BaseFrame, title:string)
---@field Inset Frame
---@field TopTileStreaks Frame
local frame = CreateFrame("Frame", nil, parent, "ButtonFrameTemplate")
frame:SetTitle(data.title)
frame:SetSize(data.width or 100, data.height or 100)
if data.points then
for _, point in ipairs(data.points) do
frame:SetPoint(unpack(point))
end
end
if not data.showPortrait then
ButtonFrameTemplate_HidePortrait(frame)
end
if not data.isClosable then
frame.CloseButton:Hide()
end
frame.Inset:ClearAllPoints()
frame.Inset:SetPoint("TOP", 0, -65)
frame.Inset:SetPoint("BOTTOM", 0, 35)
frame.Inset:SetPoint("LEFT", 20, 0)
frame.Inset:SetPoint("RIGHT", -20, 0)

return frame
end
11 changes: 11 additions & 0 deletions extraAPI.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---@class Anchor

---@param point FramePoint
---@param relativeTo? any
---@param relativePoint? FramePoint
---@param offsetX? uiUnit
---@param offsetY? uiUnit
---@return Anchor
---@overload fun(point: AnchorPoint, relativeTo?: any, ofsx?: number, ofsy?: number):Anchor
---@overload fun(point: AnchorPoint, ofsx?: number, ofsy?: number):Anchor
function CreateAnchor(point, relativeTo, relativePoint, offsetX, offsetY) return {} end

0 comments on commit e6deaa6

Please sign in to comment.