Quantcast

Added SETTINGS_UPDATE event call

Brandon Talbot [06-29-16 - 19:46]
Added SETTINGS_UPDATE event call
Added functionality to reset settings on event fire
Added setting bag category functionality
Filename
src/lua/cache/cache.lua
src/lua/controllers/bag.lua
src/lua/core.lua
src/lua/elements/item.lua
src/lua/event/eventManager.lua
src/lua/settings/settings.lua
diff --git a/src/lua/cache/cache.lua b/src/lua/cache/cache.lua
index a967be3..2253857 100644
--- a/src/lua/cache/cache.lua
+++ b/src/lua/cache/cache.lua
@@ -16,4 +16,16 @@ end
 function cache:GetBagItemContainer(name)
     self.bagContainers[name] = self.bagContainers[name] or ADDON.itemContainer(name, name ~= ADDON.utils.EMPTY_BAG_NAME, nil, name == ADDON.utils.EMPTY_BAG_NAME)
     return self.bagContainers[name]
+end
+
+function cache:UpdateSettings()
+    for _, slots in pairs(self.items) do
+        for _, item in pairs(slots) do
+            item:Setup()
+        end
+    end
+    for _, container in pairs(cache.bagContainers) do
+        container:Setup()
+        container:Arrange()
+    end
 end
\ No newline at end of file
diff --git a/src/lua/controllers/bag.lua b/src/lua/controllers/bag.lua
index aa9510b..cec2bbd 100644
--- a/src/lua/controllers/bag.lua
+++ b/src/lua/controllers/bag.lua
@@ -13,17 +13,23 @@ function bag:Init()
     self.frame.mainBar = ADDON.mainBar(self.frame)
     self.frame.mainBar:SetPoint('TOPRIGHT', self.frame, 'BOTTOMRIGHT', 0, -2)
     self.frame.mainBar:Show()
+    ADDON.eventManager:AddEvent(self, 'SETTINGS_UPDATE')
+
+    self.frame:HookScript('OnShow', function()
+        self:Register()
+    end)
+    self.frame:HookScript('OnHide', function()
+        self:UnRegister()
+    end)
 end

 function bag:Open()
-    self:Register()
     self:UpdateAllItems()
     self.frame.mainBar:Update()
     self.frame:Show()
 end

 function bag:Close()
-    self:UnRegister()
     self.frame:Hide()
 end

@@ -36,25 +42,37 @@ function bag:Toggle()
 end

 local function UpdateItemsForBag(self, bag, arrangeList)
-    for slot = 1, GetContainerNumSlots(bag) do
-        local item = ADDON.cache:GetItem(bag, slot)
-        local previousContainer
-        if item:GetParent() and item:GetParent().__class == ADDON.itemContainer.__class then
-            previousContainer = item:GetParent()
+    local count = GetContainerNumSlots(bag)
+    if count == 0 and ADDON.cache.items[bag] then
+        for _, item in pairs(ADDON.cache.items[bag]) do
+            if item:GetParent() and item:GetParent().__class == ADDON.itemContainer.__class then
+                local previousContainer = item:GetParent()
+                previousContainer:RemoveItem(item)
+                arrangeList[previousContainer] = true
+                item:Hide()
+            end
         end
+    else
+        for slot = 1, count do
+            local item = ADDON.cache:GetItem(bag, slot)
+            local previousContainer
+            if item:GetParent() and item:GetParent().__class == ADDON.itemContainer.__class then
+                previousContainer = item:GetParent()
+            end

-        item:Update()
+            item:Update()

-        local newContainer = ADDON.cache:GetBagItemContainer(ADDON.utils:GetItemContainerName(bag, slot))
-        self.frame:AddContainer(newContainer)
+            local newContainer = ADDON.cache:GetBagItemContainer(ADDON.utils:GetItemContainerName(bag, slot))
+            self.frame:AddContainer(newContainer)

-        if previousContainer ~= newContainer then
-            if previousContainer then
-                previousContainer:RemoveItem(item)
-                arrangeList[previousContainer] = true
+            if previousContainer ~= newContainer then
+                if previousContainer then
+                    previousContainer:RemoveItem(item)
+                    arrangeList[previousContainer] = true
+                end
+                newContainer:AddItem(item)
+                arrangeList[newContainer] = true
             end
-            newContainer:AddItem(item)
-            arrangeList[newContainer] = true
         end
     end
 end
@@ -93,6 +111,15 @@ function bag:UnRegister()
     ADDON.eventManager:RemoveEvent(self, 'ITEM_LOCK_CHANGED')
 end

+function bag:UpdateSettings()
+    self.frame:Setup()
+    self.frame.mainBar:Setup()
+    self.frame.mainBar:Update()
+    if self.frame:IsVisible() then
+        self:Open()
+    end
+end
+
 function bag:BAG_UPDATE(bag)
     if bag >= 0 and bag <= NUM_BAG_SLOTS then
         self:UpdateAllItemsForBag(bag)
diff --git a/src/lua/core.lua b/src/lua/core.lua
index c4887fa..117952d 100644
--- a/src/lua/core.lua
+++ b/src/lua/core.lua
@@ -13,18 +13,29 @@ function core:ADDON_LOADED(name)

     ADDON.bag:Init()

+    ADDON.eventManager:AddEvent(self, "SETTINGS_UPDATE")
     ADDON.eventManager:RemoveEvent(self, 'ADDON_LOADED')
 end

+function core:SETTINGS_UPDATE()
+    ADDON.cache:UpdateSettings()
+    ADDON.bag:UpdateSettings()
+    ADDON.categoryDialog:Setup()
+end
+
 ADDON.eventManager:AddEvent(core, 'ADDON_LOADED')

 --region Slash Commands

 SLASH_DJBAGS1, SLASH_DJBAGS2 = '/db', '/djbags'; -- 3.
 function SlashCmdList.DJBAGS(msg, editbox) -- 4.
---    ADDON.bag:Open()
-ADDON.categoryDialog(12)
-ADDON.eventManager:AddEvent('MyRandonEvent')
+ADDON.bag:Open()
+end
+
+SLASH_TDJBAGS1, SLASH_TDJBAGS2 = '/tt', '/ttt'; -- 3.
+function SlashCmdList.TDJBAGS(msg, editbox) -- 4.
+ADDON.settings.item.size = 50
+ADDON.eventManager:FireEvent('SETTINGS_UPDATE')
 end

 SLASH_RELOAD1 = '/rl'
diff --git a/src/lua/elements/item.lua b/src/lua/elements/item.lua
index 19f980a..f62f17d 100644
--- a/src/lua/elements/item.lua
+++ b/src/lua/elements/item.lua
@@ -47,6 +47,8 @@ function item:Init()
     self.button.cooldown = _G[self.button:GetName() .. "Cooldown"]

     self.button:Show()
+
+    self.button:HookScript('OnClick', self.OnClick)
 end

 function item:Setup()
@@ -175,10 +177,10 @@ function item:UpdateLock()
     SetItemButtonDesaturated(self.button, locked);
 end

-function item:OnClick()
-    if self.id then
-        if IsAltKeyDown() and IsControlKeyDown() then
-            --            ADDON.categoryInput(self, self.id)
+function item:OnClick(button)
+    if self:GetParent().id then
+        if IsAltKeyDown() and button == 'LeftButton' then
+            ADDON.categoryDialog(self:GetParent().id)
         end
     end
 end
diff --git a/src/lua/event/eventManager.lua b/src/lua/event/eventManager.lua
index 7540ae4..573a8fc 100644
--- a/src/lua/event/eventManager.lua
+++ b/src/lua/event/eventManager.lua
@@ -15,7 +15,7 @@ function eventFrame:FireEvent(event, ...)
     end
 end

-eventFrame:SetScript("OnEvent", eventFrame.AddEvent)
+eventFrame:SetScript("OnEvent", eventFrame.FireEvent)

 function eventFrame:AddEvent(obj, event)
     if not obj[event] then return end
diff --git a/src/lua/settings/settings.lua b/src/lua/settings/settings.lua
index 52a6385..b55fa58 100644
--- a/src/lua/settings/settings.lua
+++ b/src/lua/settings/settings.lua
@@ -8,6 +8,21 @@ settings.__index = settings

 function settings:Init()
     self:GetCharacterSettings()
+
+    ADDON.eventManager:AddEvent("SETTING_UPDATE")
+end
+
+function settings:SETTINGS_UPDATE()
+    local realm = GetRealmName()
+    local player = UnitName("player")
+
+    if not DJBagsConfig then
+        DJBagsConfig = {}
+    end
+    if not DJBagsConfig[realm] then
+        DJBagsConfig[realm] = {}
+    end
+    --    DJBagsConfig[player] = ADDON.settings
 end

 function settings:GetCharacterSettings()