Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kkthnx committed Nov 17, 2024
1 parent d36fc7b commit 2bac834
Show file tree
Hide file tree
Showing 49 changed files with 1,885 additions and 1,614 deletions.
6 changes: 4 additions & 2 deletions KkthnxUI/Libraries/cargBags/base-add/bags.sieve.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ local Container = cargBags.classes.Container
@param bags <BagType>
]]
function Container:SetBags(bags)
if(cargBags.ParseBags) then
if cargBags.ParseBags then
bags = cargBags:ParseBags(bags)
end

if(not bags) then return end
if not bags then
return
end

self.implementation.bagToContainer = self.implementation.bagToContainer or {}
local b2c = self.implementation.bagToContainer
Expand Down
30 changes: 18 additions & 12 deletions KkthnxUI/Libraries/cargBags/base-add/filters.sieve.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ end
Empties the filter table
]]
function FilterSet:Empty()
for k in pairs(self.funcs) do self.funcs[k] = nil end
for k in pairs(self.params) do self.params[k] = nil end
for k in pairs(self.chained) do self.chained[k] = nil end
for k in pairs(self.funcs) do
self.funcs[k] = nil
end
for k in pairs(self.params) do
self.params[k] = nil
end
for k in pairs(self.chained) do
self.chained[k] = nil
end
end

--[[!
Expand All @@ -67,7 +73,7 @@ end
@param flag <bool> whether the filter is enabled (-1: inverted) [optional]
]]
function FilterSet:SetExtended(filter, param, flag)
if(not flag and param) then
if not flag and param then
flag = true
end

Expand All @@ -81,7 +87,7 @@ end
@param ... <function> a list of filters
]]
function FilterSet:SetMultiple(flag, ...)
for i=1, select("#", ...) do
for i = 1, select("#", ...) do
local filter = select(i, ...)
self:Set(filter, flag)
end
Expand All @@ -107,14 +113,14 @@ function FilterSet:Check(item)
-- check own filters
for filter, flag in pairs(funcs) do
local result = filter(item, params[filter])
if((flag == true and not result) or (flag == -1 and result)) then
if (flag == true and not result) or (flag == -1 and result) then
return nil
end
end

-- check filters of chained sets
for tbl in pairs(self.chained) do
if(not tbl:Check(item)) then
if not tbl:Check(item) then
return nil
end
end
Expand All @@ -129,7 +135,7 @@ end
]]
function Implementation:GetContainerForItem(item)
for _, container in ipairs(self.contByID) do
if(not container.filters or container.filters:Check(item)) then
if not container.filters or container.filters:Check(item) then
return container
end
end
Expand All @@ -138,13 +144,13 @@ end
--[[
Simple function shortcuts for Containers
]]
for name, func in pairs{
for name, func in pairs({
["SetFilter"] = "Set",
["SetExtendedFilter"] = "SetExtended",
["SetMultipleFilters"] = "SetMultiple",
["ChainFilters"] = "Chain",
["CheckFilters"]= "Check",
} do
["CheckFilters"] = "Check",
}) do
Container[name] = function(self, ...)
self.filters = self.filters or FilterSet:New()
self.filters[func](self.filters, ...)
Expand All @@ -163,4 +169,4 @@ function Container:FilterForFunction(func, filters)
local result = filters:Check(button:GetInfo())
func(button, result)
end
end
end
44 changes: 29 additions & 15 deletions KkthnxUI/Libraries/cargBags/base/container.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ local cargBags = ns.cargBags
]]
local Container = cargBags:NewClass("Container", nil, "Button")

local mt_bags = {__index=function(self, bagID)
self[bagID] = CreateFrame("Frame", nil, self.container)
self[bagID]:SetID(bagID)
return self[bagID]
end}
local mt_bags = {
__index = function(self, bagID)
self[bagID] = CreateFrame("Frame", nil, self.container)
self[bagID]:SetID(bagID)
return self[bagID]
end,
}

--[[!
Creates a new instance of the class
Expand All @@ -41,19 +43,21 @@ end}
]]
function Container:New(name, ...)
local implName = self.implementation.name
local container = setmetatable(CreateFrame("Button", implName..name), self.__index)
local container = setmetatable(CreateFrame("Button", implName .. name), self.__index)

container.name = name
container.buttons = {}
container.bags = setmetatable({container = container}, mt_bags)
container.bags = setmetatable({ container = container }, mt_bags)
container:ScheduleContentCallback()

container.implementation.contByName[name] = container -- Make this into pretty function?
table.insert(container.implementation.contByID, container)

container:SetParent(self.implementation)

if(container.OnCreate) then container:OnCreate(name, ...) end
if container.OnCreate then
container:OnCreate(name, ...)
end

return container
end
Expand All @@ -69,8 +73,12 @@ function Container:AddButton(button)
button:SetParent(self.bags[button.bagId])
self:ScheduleContentCallback()
table.insert(self.buttons, button)
if(button.OnAdd) then button:OnAdd(self) end
if(self.OnButtonAdd) then self:OnButtonAdd(button) end
if button.OnAdd then
button:OnAdd(self)
end
if self.OnButtonAdd then
self:OnButtonAdd(button)
end
end

--[[!
Expand All @@ -81,11 +89,15 @@ end
]]
function Container:RemoveButton(button)
for i, single in ipairs(self.buttons) do
if(button == single) then
if button == single then
self:ScheduleContentCallback()
button.container = nil
if(button.OnRemove) then button:OnRemove(self) end
if(self.OnButtonRemove) then self:OnButtonRemove(button) end
if button.OnRemove then
button:OnRemove(self)
end
if self.OnButtonRemove then
self:OnButtonRemove(button)
end
return table.remove(self.buttons, i)
end
end
Expand All @@ -94,12 +106,14 @@ end
--[[
@callback OnContentsChanged()
]]
local updater, scheduled = CreateFrame"Frame", {}
local updater, scheduled = CreateFrame("Frame"), {}
updater:Hide()
updater:SetScript("OnUpdate", function(self)
self:Hide()
for container in pairs(scheduled) do
if(container.OnContentsChanged) then container:OnContentsChanged() end
if container.OnContentsChanged then
container:OnContentsChanged()
end
scheduled[container] = nil
end
end)
Expand Down
83 changes: 50 additions & 33 deletions KkthnxUI/Libraries/cargBags/base/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,38 @@
class-generation, helper-functions and the Blizzard-replacement.
]]
local parent, ns = ...
local global = C_AddOns.GetAddOnMetadata(parent, 'X-cargBags')
local global = C_AddOns.GetAddOnMetadata(parent, "X-cargBags")

--- @class table
-- @name cargBags
-- This class provides the underlying fundamental functions, such as
-- class-generation, helper-functions and the Blizzard-replacement
local cargBags = CreateFrame("Button")


ns.cargBags = cargBags
if(global) then
if global then
_G[global] = cargBags
end

cargBags.classes = {} --- <table> Holds all classes by their name
cargBags.itemKeys = {} --- <table> Holds all ItemKeys by their name

local widgets = setmetatable({}, {__index = function(self, widget)
self[widget] = getmetatable(CreateFrame(widget))
return self[widget]
end})
local widgets = setmetatable({}, {
__index = function(self, widget)
self[widget] = getmetatable(CreateFrame(widget))
return self[widget]
end,
})

--- Creates a new class
-- @param name <string> The name of the class
-- @param parent <string> The class which should be inherited [optional]
-- @param widget <string> The widget type of the class
-- @return class <table> The prototype of the class
function cargBags:NewClass(name, parent, widget)
if(self.classes[name]) then return end
if self.classes[name] then
return
end
parent = parent and self.classes[parent]
local class = setmetatable({}, parent or (widget and widgets[widget]))
class.__index = class
Expand All @@ -70,9 +73,15 @@ function cargBags:GetImplementation(name)
return self.classes.Implementation:Get(name)
end

local function toggleBag(forceopen) cargBags.blizzard:Toggle(forceopen) end
local function toggleNoForce() cargBags.blizzard:Toggle() end
local function closeBag() cargBags.blizzard:Hide() end
local function toggleBag(forceopen)
cargBags.blizzard:Toggle(forceopen)
end
local function toggleNoForce()
cargBags.blizzard:Toggle()
end
local function closeBag()
cargBags.blizzard:Hide()
end

--- Overwrites Blizzards Bag-Toggle-Functions with the implementation's ones
-- @param name <string> The name of the implementation [optional]
Expand All @@ -85,11 +94,11 @@ function cargBags:ReplaceBlizzard(name)
ToggleBag = toggleNoForce
ToggleBackpack = toggleNoForce

OpenAllBags = toggleBag -- Name is misleading, Blizz-function actually toggles bags
OpenAllBags = toggleBag -- Name is misleading, Blizz-function actually toggles bags
OpenBackpack = toggleBag -- Blizz does not provide toggling here
CloseAllBags = closeBag
CloseBackpack = closeBag
OpenBag = toggleBag -- fixed the loot won alert frame
OpenBag = toggleBag -- fixed the loot won alert frame

BankFrame:UnregisterAllEvents()
end
Expand All @@ -99,7 +108,7 @@ end
function cargBags:RegisterBlizzard(implementation)
self.blizzard = implementation

if(IsLoggedIn()) then
if IsLoggedIn() then
self:ReplaceBlizzard(self.blizzard)
else
self:RegisterEvent("PLAYER_LOGIN")
Expand All @@ -112,7 +121,7 @@ end
-- @param ... arguments of the event [optional]
function cargBags:FireEvent(force, event, ...)
for _, impl in pairs(self.classes.Implementation.instances) do
if(force or impl:IsShown()) then
if force or impl:IsShown() then
impl:OnEvent(event or "BAG_UPDATE", ...)
end
end
Expand All @@ -122,47 +131,53 @@ cargBags:RegisterEvent("BANKFRAME_OPENED")
cargBags:RegisterEvent("BANKFRAME_CLOSED")

cargBags:SetScript("OnEvent", function(self, event)
if(not self.blizzard) then return end
if not self.blizzard then
return
end

local impl = self.blizzard

if(event == "PLAYER_LOGIN") then
if event == "PLAYER_LOGIN" then
self:ReplaceBlizzard(impl)
elseif(event == "BANKFRAME_OPENED") then
elseif event == "BANKFRAME_OPENED" then
self.atBank = true

if(impl:IsShown()) then
if impl:IsShown() then
impl:OnEvent("BAG_UPDATE")
else
impl:Show()
end

if(impl.OnBankOpened) then
if impl.OnBankOpened then
impl:OnBankOpened()
end
elseif(event == "BANKFRAME_CLOSED") then
elseif event == "BANKFRAME_CLOSED" then
self.atBank = nil

if(impl:IsShown()) then
if impl:IsShown() then
impl:Hide()
end

if(impl.OnBankClosed) then
if impl.OnBankClosed then
impl:OnBankClosed()
end
end
end)

local handlerFuncs = setmetatable({}, {__index=function(self, handler)
self[handler] = function(self, ...) return self[handler] and self[handler](self, ...) end
return self[handler]
end})
local handlerFuncs = setmetatable({}, {
__index = function(self, handler)
self[handler] = function(self, ...)
return self[handler] and self[handler](self, ...)
end
return self[handler]
end,
})

--- Sets a number of script handlers by redirecting them to the members function, e.g. self:OnEvent(self, ...)
-- @param self <frame>
-- @param ... <string> A number of script handlers
function cargBags.SetScriptHandlers(self, ...)
for i=1, select("#", ...) do
for i = 1, select("#", ...) do
local handler = select(i, ...)
self:SetScript(handler, handlerFuncs[handler])
end
Expand All @@ -173,22 +188,24 @@ end
-- @param slotID <number>
-- @return bagSlot <number>
function cargBags.ToBagSlot(bagID, slotID)
return bagID*100+slotID
return bagID * 100 + slotID
end


--- Gets the bagID-slotID-pair of a bagSlot-index
-- @param bagSlot <number>
-- @return bagID <number>
-- @return bagSlot <number>
function cargBags.FromBagSlot(bagSlot)
return floor(bagSlot/100), bagSlot % 100
return floor(bagSlot / 100), bagSlot % 100
end

--- Creates a new item table which has access to ItemKeys
-- @return itemTable <table>
local m_item = {__index = function(i,k) return cargBags.itemKeys[k] and cargBags.itemKeys[k](i,k) end}
local m_item = {
__index = function(i, k)
return cargBags.itemKeys[k] and cargBags.itemKeys[k](i, k)
end,
}
function cargBags:NewItemTable()
return setmetatable({}, m_item)
end

Loading

0 comments on commit 2bac834

Please sign in to comment.