diff --git a/GnomishVendorShrinker.lua b/GnomishVendorShrinker.lua index e784118..a56583b 100644 --- a/GnomishVendorShrinker.lua +++ b/GnomishVendorShrinker.lua @@ -7,111 +7,124 @@ local ItemSearch = LibStub('LibItemSearch-1.0') local NUMROWS, SCROLLSTEP = 14, 5 -for _,f in pairs{MerchantNextPageButton, MerchantPrevPageButton, MerchantPageText} do - f:Hide() - f.Show = f.Hide +local function Hide(frame) + frame:Hide() + frame.Show = frame.Hide end -local GVS = CreateFrame("frame", nil, MerchantFrame) -GVS:SetWidth(315) -GVS:SetHeight(294) -GVS:SetPoint("TOPLEFT", 8, -67) -GVS:SetScript("OnEvent", function(self, event, ...) - if self[event] then return self[event](self, event, ...) end -end) -GVS:Hide() +function ns.OnLoad() + Hide(MerchantNextPageButton) + Hide(MerchantPrevPageButton) + Hide(MerchantPageText) -local rows = {} -for i=1,NUMROWS do - local row = ns.NewMerchantItemFrame(GVS) + local GVS = CreateFrame("frame", nil, MerchantFrame) + GVS:SetWidth(315) + GVS:SetHeight(294) + GVS:SetPoint("TOPLEFT", 8, -67) + GVS:SetScript("OnEvent", function(self, event, ...) + if self[event] then return self[event](self, event, ...) end + end) + GVS:Hide() - if i == 1 then - row:SetPoint("TOPLEFT") - row:SetPoint("RIGHT", -19, 0) - else - row:SetPoint("TOPLEFT", rows[i-1], "BOTTOMLEFT") - row:SetPoint("RIGHT", rows[i-1]) + + local rows = {} + for i=1,NUMROWS do + local row = ns.NewMerchantItemFrame(GVS) + + if i == 1 then + row:SetPoint("TOPLEFT") + row:SetPoint("RIGHT", -19, 0) + else + row:SetPoint("TOPLEFT", rows[i-1], "BOTTOMLEFT") + row:SetPoint("RIGHT", rows[i-1]) + end + + rows[i] = row end - rows[i] = row -end - -local scrollbar = LibStub("tekKonfig-Scroll").new(GVS, 0, SCROLLSTEP) -local offset = 0 -local function Refresh() - local n = GetMerchantNumItems() - local row, n_searchmatch = 1, 0 - for i=1,n do - local link = GetMerchantItemLink(i) - if ItemSearch:Find(link, GVS.searchstring) then - if n_searchmatch >= offset and n_searchmatch < offset + NUMROWS then - rows[row]:SetValue(i) - row = row + 1 + local scrollbar = LibStub("tekKonfig-Scroll").new(GVS, 0, SCROLLSTEP) + local offset = 0 + local function Refresh() + local n = GetMerchantNumItems() + local row, n_searchmatch = 1, 0 + for i=1,n do + local link = GetMerchantItemLink(i) + if ItemSearch:Find(link, GVS.searchstring) then + if n_searchmatch >= offset and n_searchmatch < offset + NUMROWS then + rows[row]:SetValue(i) + row = row + 1 + end + n_searchmatch = n_searchmatch + 1 end - n_searchmatch = n_searchmatch + 1 + end + scrollbar:SetMinMaxValues(0, math.max(0, n_searchmatch - NUMROWS)) + for i=row,NUMROWS do + rows[i]:Hide() end end - scrollbar:SetMinMaxValues(0, math.max(0, n_searchmatch - NUMROWS)) - for i=row,NUMROWS do - rows[i]:Hide() + GVS.CURRENCY_DISPLAY_UPDATE = Refresh + GVS.BAG_UPDATE = Refresh + GVS.MERCHANT_UPDATE = Refresh + + + ns.MakeSearchField(GVS, Refresh) + + + local f = scrollbar:GetScript("OnValueChanged") + scrollbar:SetScript("OnValueChanged", function(self, value, ...) + offset = math.floor(value) + Refresh() + return f(self, value, ...) + end) + + + local offset = 0 + GVS:EnableMouseWheel(true) + GVS:SetScript("OnMouseWheel", function(self, value) + scrollbar:SetValue(scrollbar:GetValue() - value * SCROLLSTEP) + end) + GVS:SetScript("OnShow", function(self, noreset) + local max = math.max(0, GetMerchantNumItems() - NUMROWS) + scrollbar:SetMinMaxValues(0, max) + scrollbar:SetValue(noreset and math.min(scrollbar:GetValue(), max) or 0) + Refresh() + + GVS:RegisterEvent("BAG_UPDATE") + GVS:RegisterEvent("MERCHANT_UPDATE") + GVS:RegisterEvent("CURRENCY_DISPLAY_UPDATE") + end) + GVS:SetScript("OnHide", function() + GVS:UnregisterEvent("BAG_UPDATE") + GVS:UnregisterEvent("MERCHANT_UPDATE") + GVS:UnregisterEvent("CURRENCY_DISPLAY_UPDATE") + if StackSplitFrame:IsVisible() then StackSplitFrame:Hide() end + end) + + + -- Reanchor the buyback button, it acts weird when switching tabs otherwise... + MerchantBuyBackItem:ClearAllPoints() + MerchantBuyBackItem:SetPoint("BOTTOMRIGHT", -7, 33) + + + local function Show() + for i=1,12 do _G["MerchantItem"..i]:Hide() end + if GVS:IsShown() then GVS:GetScript("OnShow")(GVS, true) else GVS:Show() end end + hooksecurefunc("MerchantFrame_UpdateMerchantInfo", Show) + + + hooksecurefunc("MerchantFrame_UpdateBuybackInfo", function() + GVS:Hide() + for i=1,12 do _G["MerchantItem"..i]:Show() end + end) + + + if MerchantFrame:IsVisible() and MerchantFrame.selectedTab == 1 then Show() end + + + -- Clean up our frame factories + for i,v in pairs(ns) do if i:match("^New") then ns[i] = nil end end end -GVS.CURRENCY_DISPLAY_UPDATE = Refresh -GVS.BAG_UPDATE = Refresh -GVS.MERCHANT_UPDATE = Refresh - - -ns.MakeSearchField(GVS, Refresh) - - -local f = scrollbar:GetScript("OnValueChanged") -scrollbar:SetScript("OnValueChanged", function(self, value, ...) - offset = math.floor(value) - Refresh() - return f(self, value, ...) -end) - - -local offset = 0 -GVS:EnableMouseWheel(true) -GVS:SetScript("OnMouseWheel", function(self, value) scrollbar:SetValue(scrollbar:GetValue() - value * SCROLLSTEP) end) -GVS:SetScript("OnShow", function(self, noreset) - local max = math.max(0, GetMerchantNumItems() - NUMROWS) - scrollbar:SetMinMaxValues(0, max) - scrollbar:SetValue(noreset and math.min(scrollbar:GetValue(), max) or 0) - Refresh() - - GVS:RegisterEvent("BAG_UPDATE") - GVS:RegisterEvent("MERCHANT_UPDATE") - GVS:RegisterEvent("CURRENCY_DISPLAY_UPDATE") -end) -GVS:SetScript("OnHide", function() - GVS:UnregisterEvent("BAG_UPDATE") - GVS:UnregisterEvent("MERCHANT_UPDATE") - GVS:UnregisterEvent("CURRENCY_DISPLAY_UPDATE") - if StackSplitFrame:IsVisible() then StackSplitFrame:Hide() end -end) - - --- Reanchor the buyback button, it acts weird when switching tabs otherwise... -MerchantBuyBackItem:ClearAllPoints() -MerchantBuyBackItem:SetPoint("BOTTOMRIGHT", -7, 33) - - -local function Show() - for i=1,12 do _G["MerchantItem"..i]:Hide() end - if GVS:IsShown() then GVS:GetScript("OnShow")(GVS, true) else GVS:Show() end -end -hooksecurefunc("MerchantFrame_UpdateMerchantInfo", Show) - - -hooksecurefunc("MerchantFrame_UpdateBuybackInfo", function() - GVS:Hide() - for i=1,12 do _G["MerchantItem"..i]:Show() end -end) - - -if MerchantFrame:IsVisible() and MerchantFrame.selectedTab == 1 then Show() end diff --git a/GnomishVendorShrinker.toc b/GnomishVendorShrinker.toc index ef2c3b9..1053a13 100644 --- a/GnomishVendorShrinker.toc +++ b/GnomishVendorShrinker.toc @@ -10,6 +10,7 @@ ## X-LoadOn-Merchant: true ## X-LoadOn-InterfaceOptions: GnomishVendorShrinker +externals\events.lua externals\itemid.lua externals\size_to_fit.lua externals\tooltip_scanner.lua diff --git a/externals.txt b/externals.txt index 9035767..845b384 100644 --- a/externals.txt +++ b/externals.txt @@ -1,3 +1,4 @@ +events.lua itemid.lua size_to_fit.lua tooltip_scanner.lua diff --git a/externals/events.lua b/externals/events.lua new file mode 100644 index 0000000..57dee23 --- /dev/null +++ b/externals/events.lua @@ -0,0 +1,94 @@ + +local myname, ns = ... + + +local frame = CreateFrame("Frame") + + +function ns.RegisterEvent(event, func) + frame:RegisterEvent(event) + if func then ns[event] = func end +end + + +function ns.UnregisterEvent(event) + frame:UnregisterEvent(event) +end + + +function ns.UnregisterAllEvents() + frame:UnregisterAllEvents() +end + + +-- Handles special OnLogin code for when the PLAYER_LOGIN event is fired. +-- If our addon is loaded after that event is fired, then we call it immediately +-- after the OnLoad handler is processed. +local function ProcessOnLogin() + if ns.OnLogin then + ns.OnLogin() + ns.OnLogin = nil + end + + ProcessOnLogin = nil + if not ns.PLAYER_LOGIN then frame:UnregisterEvent("PLAYER_LOGIN") end +end + + +-- Handle special OnLoad code when our addon has loaded, if present +-- Also initializes the savedvar for us, if ns.dbname or ns.dbpcname is set +-- If ns.ADDON_LOADED is defined, the ADDON_LOADED event is not unregistered +local function ProcessOnLoad(arg1) + if arg1 ~= myname then return end + + if ns.dbname then + local defaults = ns.dbdefaults or {} + _G[ns.dbname] = setmetatable(_G[ns.dbname] or {}, {__index = defaults}) + ns.db = _G[ns.dbname] + end + + if ns.dbpcname then + local defaults = ns.dbpcdefaults or {} + _G[ns.dbpcname] = setmetatable(_G[ns.dbpcname] or {}, {__index = defaults}) + ns.dbpc = _G[ns.dbpcname] + end + + if ns.OnLoad then + ns.OnLoad() + ns.OnLoad = nil + end + + ProcessOnLoad = nil + if not ns.ADDON_LOADED then frame:UnregisterEvent("ADDON_LOADED") end + + if ns.dbdefaults or ns.dbpcdefaults then ns.RegisterEvent("PLAYER_LOGOUT") end + + if IsLoggedIn() then ProcessOnLogin() + else frame:RegisterEvent("PLAYER_LOGIN") end +end + + +-- Removes the default values from the db and dbpc as we're logging out +local function ProcessLogout() + if ns.dbdefaults then + for i,v in pairs(ns.dbdefaults) do + if ns.db[i] == v then ns.db[i] = nil end + end + end + + if ns.dbpcdefaults then + for i,v in pairs(ns.dbpcdefaults) do + if ns.dbpc[i] == v then ns.dbpc[i] = nil end + end + end +end + + +frame:RegisterEvent("ADDON_LOADED") +frame:SetScript("OnEvent", function(self, event, arg1, ...) + if ProcessOnLoad and event == "ADDON_LOADED" then ProcessOnLoad(arg1) end + if ProcessOnLogin and event == "PLAYER_LOGIN" then ProcessOnLogin() end + + if event == "PLAYER_LOGOUT" then ProcessLogout() end + if ns[event] then ns[event](event, arg1, ...) end +end) diff --git a/frames/AltCurrency.lua b/frames/AltCurrency.lua index 91f71e3..e42cc05 100644 --- a/frames/AltCurrency.lua +++ b/frames/AltCurrency.lua @@ -3,7 +3,7 @@ local myname, ns = ... local function NewItemFrame(self, i) - local item = ns.NewAltCurrencyItemFrame(self.parent) + local item = self.NewAltCurrencyItemFrame(self.parent) if i == 1 then item:SetPoint("LEFT") @@ -39,7 +39,11 @@ function ns.NewAltCurrencyFrame(parent) local frame = CreateFrame("Frame", nil, parent) frame:SetSize(1,1) - itemframesets[frame] = setmetatable({parent = frame}, MT) + local t = { + parent = frame, + NewAltCurrencyItemFrame = ns.NewAltCurrencyItemFrame, + } + itemframesets[frame] = setmetatable(t, MT) frame.SetValue = SetValue frame.SizeToFit = ns.SizeToFit