From 5b182aa1794d1d6796405b54a96e5e8bc9f792a8 Mon Sep 17 00:00:00 2001 From: Antonio Lobato Date: Fri, 23 Feb 2024 10:19:12 -0800 Subject: [PATCH] Search Expanded (again!) (#212) * Added optional in-bag search for bank and bag. * Fixed a few bugs around categories not refreshing for bags. --- config/classic/config.lua | 14 +++++++++++ config/config.lua | 18 ++++++++++++-- config/itemlist.lua | 4 +-- core/constants.lua | 1 + core/database.lua | 10 ++++++++ data/categories.lua | 4 +-- frames/bag.lua | 40 +++++++++++++++++++++++++++--- frames/classic/bag.lua | 18 ++++++++++++++ frames/item.lua | 3 ++- frames/search.lua | 52 +++++++++++++++++++++++++++++++++++++-- 10 files changed, 152 insertions(+), 12 deletions(-) diff --git a/config/classic/config.lua b/config/classic/config.lua index 8cd48b66..5bf1ba08 100644 --- a/config/classic/config.lua +++ b/config/classic/config.lua @@ -30,6 +30,20 @@ function config:GetGeneralOptions() name = L:G("General"), order = 0, args = { + inBagSearch = { + type = "toggle", + width = "full", + order = 0, + name = L:G("Enable In-Bag Search"), + desc = L:G("If enabled, a search bar will appear at the top of your bags."), + get = function() + return DB:GetInBagSearch() + end, + set = function(_, value) + DB:SetInBagSearch(value) + events:SendMessage('search/SetInFrame', value) + end, + }, newItemTime = { type = "range", order = 2, diff --git a/config/config.lua b/config/config.lua index 401aaf06..297ff4cc 100644 --- a/config/config.lua +++ b/config/config.lua @@ -65,10 +65,24 @@ function config:GetGeneralOptions() name = L:G("General"), order = 0, args = { + inBagSearch = { + type = "toggle", + width = "full", + order = 0, + name = L:G("Enable In-Bag Search"), + desc = L:G("If enabled, a search bar will appear at the top of your bags."), + get = function() + return DB:GetInBagSearch() + end, + set = function(_, value) + DB:SetInBagSearch(value) + events:SendMessage('search/SetInFrame', value) + end, + }, showBagButton = { type = "toggle", width = "full", - order = 1, + order = 2, name = L:G("Show Blizzard Bag Button"), desc = L:G("Show or hide the default Blizzard bag button."), get = DB.GetShowBagButton, @@ -84,7 +98,7 @@ function config:GetGeneralOptions() }, newItemTime = { type = "range", - order = 2, + order = 3, name = L:G("New Item Duration"), desc = L:G("The time, in minutes, to consider an item a new item."), min = 0, diff --git a/config/itemlist.lua b/config/itemlist.lua index 28d9f24e..156a84bd 100644 --- a/config/itemlist.lua +++ b/config/itemlist.lua @@ -65,7 +65,7 @@ local function SetList(self, values) local list = self:GetUserData("values") DB:SaveItemToCategory(itemid, list.name) self:SetList(DB:GetItemCategory(list.name)) - items:RefreshAll() + items:FullRefreshAll() end if #itemList == 0 then @@ -108,7 +108,7 @@ local function SetList(self, values) local list = self:GetUserData("values") DB:DeleteItemFromCategory(v.itemInfo.itemID, list.name) self:SetList(DB:GetItemCategory(list.name)) - items:RefreshAll() + items:FullRefreshAll() end }}) end diff --git a/core/constants.lua b/core/constants.lua index 18dc4b29..627847d3 100644 --- a/core/constants.lua +++ b/core/constants.lua @@ -288,6 +288,7 @@ const.DATABASE_DEFAULTS = { enabled = true, showBagButton = true, debug = false, + inBagSearch = false, itemLevel = { [const.BAG_KIND.BACKPACK] = { enabled = true, diff --git a/core/database.lua b/core/database.lua index f6b6d052..4857dbe0 100644 --- a/core/database.lua +++ b/core/database.lua @@ -280,6 +280,16 @@ function DB:GetDebugMode() return DB.data.profile.debug end +---@param enabled boolean +function DB:SetInBagSearch(enabled) + DB.data.profile.inBagSearch = enabled +end + +---@return boolean +function DB:GetInBagSearch() + return DB.data.profile.inBagSearch +end + function DB:Migrate() --[[ Migration of the custom category filters from single filter to per-bag filter. diff --git a/data/categories.lua b/data/categories.lua index d309d58e..2b8508a4 100644 --- a/data/categories.lua +++ b/data/categories.lua @@ -110,7 +110,7 @@ end function categories:DeleteCategory(category) database:DeleteItemCategory(category) events:SendMessage('categories/Changed') - items:RefreshAll() + items:FullRefreshAll() end -- GetCustomCategory returns the custom category for an item, or nil if it doesn't have one. @@ -177,5 +177,5 @@ end -- reprocessed and re-categorized. function categories:ReprocessAllItems() wipe(self.itemsWithNoCategory) - items:RefreshAll() + items:FullRefreshAll() end diff --git a/frames/bag.lua b/frames/bag.lua index 2d4c4c81..b7ef2655 100644 --- a/frames/bag.lua +++ b/frames/bag.lua @@ -86,6 +86,7 @@ local search = addon:GetModule('Search') ---@field toRelease Item[] ---@field toReleaseSections Section[] ---@field views table +---@field searchBox SearchFrame bagFrame.bagProto = {} function bagFrame.bagProto:Show() @@ -240,14 +241,24 @@ function bagFrame.bagProto:ToggleReagentBank() self.isReagentBank = not self.isReagentBank if self.isReagentBank then BankFrame.selectedTab = 2 - self.frame:SetTitle(L:G("Reagent Bank")) + if self.searchBox.frame:IsShown() then + self.frame:SetTitle("") + self.searchBox.helpText:SetText(L:G("Search Reagent Bank")) + else + self.frame:SetTitle(L:G("Reagent Bank")) + end self.currentItemCount = -1 --self:ClearRecentItems() self:Wipe() items:RefreshReagentBank() else BankFrame.selectedTab = 1 - self.frame:SetTitle(L:G("Bank")) + if self.searchBox.frame:IsShown() then + self.frame:SetTitle("") + self.searchBox.helpText:SetText(L:G("Search Bank")) + else + self.frame:SetTitle(L:G("Bank")) + end self.currentItemCount = -1 --self:ClearRecentItems() self:Wipe() @@ -259,7 +270,12 @@ function bagFrame.bagProto:SwitchToBank() if self.kind == const.BAG_KIND.BACKPACK then return end self.isReagentBank = false BankFrame.selectedTab = 1 - self.frame:SetTitle(L:G("Bank")) + if self.searchBox.frame:IsShown() then + self.frame:SetTitle("") + self.searchBox.helpText:SetText(L:G("Search Bank")) + else + self.frame:SetTitle(L:G("Bank")) + end self:Wipe() end @@ -429,6 +445,15 @@ function bagFrame:Create(kind) search:Create(b.frame) end + local searchBox = search:CreateBox(kind, b.frame) + searchBox.frame:SetPoint("TOP", b.frame, "TOP", 0, -2) + searchBox.frame:SetSize(150, 20) + if database:GetInBagSearch() then + searchBox.frame:Show() + b.frame:SetTitle("") + end + b.searchBox = searchBox + if kind == const.BAG_KIND.BACKPACK then local currencyFrame = currency:Create(b.frame) currencyFrame:Hide() @@ -464,5 +489,14 @@ function bagFrame:Create(kind) events:BucketEvent('BAG_UPDATE_COOLDOWN',function(_) b:OnCooldown() end) end + events:RegisterMessage('search/SetInFrame', function (_, shown) + if shown then + b.searchBox.frame:Show() + b.frame:SetTitle("") + else + b.searchBox.frame:Hide() + b.frame:SetTitle(L:G(kind == const.BAG_KIND.BACKPACK and "Backpack" or "Bank")) + end + end) return b end diff --git a/frames/classic/bag.lua b/frames/classic/bag.lua index d6f24770..a3b72d9b 100644 --- a/frames/classic/bag.lua +++ b/frames/classic/bag.lua @@ -229,6 +229,15 @@ function bagFrame:Create(kind) search:Create(b.frame) end + local searchBox = search:CreateBox(kind, b.frame) + searchBox.frame:SetPoint("TOP", b.frame, "TOP", 0, -2) + searchBox.frame:SetSize(150, 20) + if database:GetInBagSearch() then + searchBox.frame:Show() + b.frame:SetTitle("") + end + b.searchBox = searchBox + -- Enable dragging of the bag frame. b.frame:SetMovable(true) b.frame:EnableMouse(true) @@ -259,5 +268,14 @@ function bagFrame:Create(kind) events:BucketEvent('BAG_UPDATE_COOLDOWN',function(_) b:OnCooldown() end) end + events:RegisterMessage('search/SetInFrame', function (_, shown) + if shown then + b.searchBox.frame:Show() + b.frame:SetTitle("") + else + b.searchBox.frame:Hide() + b.frame:SetTitle(L:G(kind == const.BAG_KIND.BACKPACK and "Backpack" or "Bank")) + end + end) return b end diff --git a/frames/item.lua b/frames/item.lua index eba223c0..dc0bddcd 100644 --- a/frames/item.lua +++ b/frames/item.lua @@ -97,9 +97,10 @@ local function matchFilter(filter, data) -- If no prefix is provided, assume the filter is a name or type filter. if value == nil then if + data.itemInfo.itemName and ( string.find(data.itemInfo.itemName:lower(), prefix, 1, true) or string.find(data.itemInfo.itemType:lower(), prefix, 1, true) or - string.find(data.itemInfo.itemSubType:lower(), prefix, 1, true) then + string.find(data.itemInfo.itemSubType:lower(), prefix, 1, true)) then return true end return false diff --git a/frames/search.lua b/frames/search.lua index c33e066b..9aa5fdbc 100644 --- a/frames/search.lua +++ b/frames/search.lua @@ -9,6 +9,9 @@ local animations = addon:GetModule('Animations') ---@class Events: AceModule local events = addon:GetModule('Events') +---@class Constants: AceModule +local const = addon:GetModule('Constants') + ---@class Search: AceModule ---@field searchFrame SearchFrame local search = addon:NewModule('Search') @@ -19,6 +22,7 @@ local search = addon:NewModule('Search') ---@field fadeOutGroup AnimationGroup ---@field textBox EditBox ---@field helpText FontString +---@field kind BagKind search.searchProto = {} -- BetterBags_ToggleSearch toggles the search view. This function is used in the @@ -53,8 +57,16 @@ function search.searchProto:UpdateSearch() else self.helpText:Hide() end - addon.Bags.Backpack:Search(text) - addon.Bags.Bank:Search(text) + if self.kind ~= nil then + if self.kind == const.BAG_KIND.BACKPACK then + addon.Bags.Backpack:Search(text) + else + addon.Bags.Bank:Search(text) + end + else + addon.Bags.Backpack:Search(text) + addon.Bags.Bank:Search(text) + end end function search:GetText() @@ -116,3 +128,39 @@ function search:Create(parent) end) return sf end + +---@param kind BagKind +---@param parent Frame +---@return SearchFrame +function search:CreateBox(kind, parent) + local sf = setmetatable({}, {__index = search.searchProto}) + sf.frame = CreateFrame("Frame", nil, parent) --[[@as Frame]] + sf.frame:SetFrameLevel(2000) + local textBox = CreateFrame("EditBox", nil, sf.frame, "BagSearchBoxTemplate") --[[@as SearchBox]] + textBox:SetFontObject("GameFontNormal") + textBox:SetTextColor(1, 1, 1, 1) + textBox:ClearFocus() + textBox:SetAutoFocus(false) + textBox:SetJustifyH("LEFT") + textBox:SetScript("OnEscapePressed", function(me) + ---@cast me +EditBox + me:ClearFocus() + end) + textBox:SetScript("OnTextChanged", function() + sf:UpdateSearch() + end) + textBox:SetAllPoints() + + sf.kind = kind + sf.helpText = textBox.Instructions + sf.textBox = textBox + if kind == const.BAG_KIND.BACKPACK then + sf.helpText:SetText("Search Backpack") + else + sf.helpText:SetText("Search Bank") + end + sf.helpText:ClearAllPoints() + sf.helpText:SetPoint("CENTER") + sf.frame:Hide() + return sf +end \ No newline at end of file