From e852a7f730b4c6c87d6bdc28675181b7339f9988 Mon Sep 17 00:00:00 2001 From: urnati Date: Tue, 16 Jan 2024 12:19:57 -0500 Subject: [PATCH] - Ammo : Combine Wrath and CE; fix for #1380; refactor to remove no longer needed events & reduce cycles --- TitanAmmo/TitanAmmo_Vanilla.toc | 6 +- TitanAmmo/TitanAmmo_Wrath.toc | 6 +- TitanAmmo/TitanClassicAmmo.lua | 411 +++++++++++++++++++++---------------- TitanAmmo/TitanClassicAmmo.xml | 16 -- TitanAmmo/TitanClassicAmmo_CE.lua | 339 ------------------------------ TitanAmmo/TitanClassicAmmo_CE.xml | 16 -- 6 files changed, 245 insertions(+), 549 deletions(-) delete mode 100644 TitanAmmo/TitanClassicAmmo.xml delete mode 100644 TitanAmmo/TitanClassicAmmo_CE.lua delete mode 100644 TitanAmmo/TitanClassicAmmo_CE.xml diff --git a/TitanAmmo/TitanAmmo_Vanilla.toc b/TitanAmmo/TitanAmmo_Vanilla.toc index 660b099..42d7525 100644 --- a/TitanAmmo/TitanAmmo_Vanilla.toc +++ b/TitanAmmo/TitanAmmo_Vanilla.toc @@ -1,9 +1,9 @@ ## Interface: 11500 -## Title: Titan Panel [|cffeda55fAmmo|r] |cff00aa008.0.2|r -## Version: 8.0.2 +## Title: Titan Panel [|cffeda55fAmmo|r] |cff00aa008.0.1|r +## Version: 8.0.1 ## Notes: Adds an ammo monitor to Titan Panel - Classic versions only ## Author: Titan Panel Development Team (http://www.titanpanel.org) ## SavedVariables: ## OptionalDeps: ## Dependencies: TitanClassic -TitanClassicAmmo_CE.xml \ No newline at end of file +TitanClassicAmmo.lua \ No newline at end of file diff --git a/TitanAmmo/TitanAmmo_Wrath.toc b/TitanAmmo/TitanAmmo_Wrath.toc index c8aa34e..bb46973 100644 --- a/TitanAmmo/TitanAmmo_Wrath.toc +++ b/TitanAmmo/TitanAmmo_Wrath.toc @@ -1,9 +1,9 @@ ## Interface: 30403 -## Title: Titan Panel [|cffeda55fAmmo|r] |cff00aa008.0.2|r -## Version: 8.0.2 +## Title: Titan Panel [|cffeda55fAmmo|r] |cff00aa008.0.1|r +## Version: 8.0.1 ## Notes: Adds an ammo monitor to Titan Panel - Classic versions only ## Author: Titan Panel Development Team (http://www.titanpanel.org) ## SavedVariables: ## OptionalDeps: ## Dependencies: TitanClassic -TitanClassicAmmo.xml \ No newline at end of file +TitanClassicAmmo.lua \ No newline at end of file diff --git a/TitanAmmo/TitanClassicAmmo.lua b/TitanAmmo/TitanClassicAmmo.lua index ed139c2..5c1a98b 100644 --- a/TitanAmmo/TitanClassicAmmo.lua +++ b/TitanAmmo/TitanClassicAmmo.lua @@ -4,17 +4,21 @@ -- * -- * By: Titan Panel Development Team -- ************************************************************************** +-- 2024 Jan - Combined Classic Era and Wrath into one version -- 2019 Aug - reverted and updated for Classic -- -- This will track the count of ammo (bows and guns) or thrown (knives) equipped. -- Ammo is placed in the 'ammo' slot where Blizzard counts ALL of that *type of ammo* -- regardless of where it is in your bags. --- Thrown is placed in the actual weapon slot where Blizzard counts ALL of that *type of thrown*. +-- Thrown is placed in the actual display.weapon slot where Blizzard counts ALL of that *type of thrown*. -- This forces a different routine to be used so the ammo must always be checked for type and count. +-- +-- Note: Thrown has no durability. Not sure when Blizz implemented this --]] -- ******************************** Constants ******************************* local _G = getfenv(0); local TITAN_AMMO_ID = "Ammo"; +local TITAN_BUTTON = "TitanPanel"..TITAN_AMMO_ID.."Button" local SHOOT_STACK = 200 local ARROW_STACK = 200 @@ -27,6 +31,7 @@ local LIM_BAD = .5 local BOW = "INVTYPE_RANGED" local GUN = "INVTYPE_RANGEDRIGHT" local THROWN = "INVTYPE_THROWN" +local NO_RANGED = "Nothing_Equipped" local TITAN_AMMO_THRESHOLD_TABLE = { -- Use ammo stack and threshold limits above to calc colored text ["INVTYPE_RANGEDRIGHT"] = { @@ -43,28 +48,74 @@ local TITAN_AMMO_THRESHOLD_TABLE = { -- Use ammo stack and threshold limits abov }, }; +local PLACE = { -- for debug output + ["INVTYPE_RANGED"] = "Bow", + ["INVTYPE_RANGEDRIGHT"] = "Gun", + ["INVTYPE_THROWN"] = "Throw", + } + -- ******************************** Variables ******************************* -local class = select(2, UnitClass("player")) local ammoSlotID = GetInventorySlotInfo("AmmoSlot") local rangedSlotID = GetInventorySlotInfo("RangedSlot") -local ammo_count = 0; -local ammo_type = ""; -local ammo_name = "" -local weapon = ""; -local weapon_type = ""; -local ammo_show = false -- show plugin based on class + +-- Info to show on the plugin +local display = { + ammo_count = 0, + ammo_type = "", + ammo_name = "", -- L["TITAN_AMMO_BUTTON_NOAMMO"] + weapon = "", + weapon_type = "", + } local L = LibStub("AceLocale-3.0"):GetLocale("TitanClassic", true) + +local debug_flow = false + -- ******************************** Functions ******************************* +--[[ local +-- ************************************************************************** +-- NAME : debug_msg(Message) +-- DESC : Debug function to print message to chat frame +-- VARS : Message = message to print to chat frame +-- ************************************************************************** +--]] +local function debug_msg(Message) + local msg = "" + local stamp = date("%H:%M:%S") -- date("%m/%d/%y %H:%M:%S") + local milli = GetTime() -- seconds with millisecond precision (float) + milli = string.format("%0.2F", milli - math.modf(milli)) + msg = msg..TitanUtils_GetGoldText(stamp..milli.." "..TITAN_AMMO_ID..": ") + msg = msg..TitanUtils_GetGreenText(Message) + DEFAULT_CHAT_FRAME:AddMessage(msg) +-- DEFAULT_CHAT_FRAME:AddMessage(TITAN_AMMO_ID..": " .. Message, 1.00, 0.49, 0.04) +end + local function ClrAmmoInfo() - ammo_count = 0; - ammo_type = L["TITAN_AMMO_BUTTON_NOAMMO"]; - ammo_name = L["TITAN_AMMO_BUTTON_NOAMMO"] - ammo_show = false + display.ammo_count = 0; + display.ammo_type = NO_RANGED + display.ammo_name = "" + display.weapon = "" + display.weapon_type = "" end + local function GetItemLink(rangedSlotID) return GetInventoryItemLink("player", rangedSlotID) end + +local function IsAmmoClass() + local class = select(2, UnitClass("player")) + local res = false + if class == "ROGUE" + or class == "WARRIOR" + or class == "HUNTER" + then + res = true + else + res = false + end + return res +end + local function IsThrown(loc) local res = false if loc == "INVTYPE_THROWN" then @@ -72,6 +123,7 @@ local function IsThrown(loc) end return res end + local function IsAmmo(loc) local res = false if loc == "INVTYPE_RANGED" or loc == "INVTYPE_RANGEDRIGHT" then @@ -82,64 +134,91 @@ end local function GetAmmoCount() local ammo = ""; - local w = "--"; - local wt = "--"; + local wpn = "--"; + local wpnt = "--"; local weap = GetInventoryItemID("player", rangedSlotID) if weap == nil then ---[[ -print("GetWeaponInfo" -.." Not Found" -) ---]] + -- nothing in slot + ammo = NO_RANGED else local itemName, itemLink, itemQuality, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, sellPrice, classID, subclassID, bindType, expacID, setID, isCraftingReagent = GetItemInfo(weap) - w = itemName - wt = itemSubType + wpn = itemName + wpnt = itemSubType ammo = itemEquipLoc -- set ammo name and count if IsThrown(ammo) then -- throwing knives, etc - ammo_name = itemName or _G["UNKNOWN"] - if ammo_name == _G["UNKNOWN"] then - ammo_count = 0 + display.ammo_name = itemName or UNKNOWN + if display.ammo_name == UNKNOWN then + display.ammo_count = 0 else - ammo_count = GetInventoryItemCount("player", rangedSlotID) or ammo_count + display.ammo_count = GetInventoryItemCount("player", rangedSlotID) or display.ammo_count end - else -- bullets or arrows - ammo_name = select(1, GetItemInfo(GetInventoryItemID("player", ammoSlotID))) or _G["UNKNOWN"] - if ammo_name == _G["UNKNOWN"] then - ammo_count = 0 + elseif IsAmmo(ammo) then + display.ammo_name = select(1, GetItemInfo(GetInventoryItemID("player", ammoSlotID))) or UNKNOWN + if display.ammo_name == UNKNOWN then + display.ammo_count = 0 else - ammo_count = GetInventoryItemCount("player", ammoSlotID) or ammo_count + display.ammo_count = GetInventoryItemCount("player", ammoSlotID) or display.ammo_count end + else -- bullets or arrows + display.ammo_name = UNKNOWN + display.ammo_count = 0 end ---[[ -print("GetWeaponInfo" ---.." '"..tostring(weap).."'" -.." '"..tostring(w).."'" ---.." '"..tostring(wt).."'" ---.." '"..tostring(ammo).."'" -.." '"..tostring(ammo_name).."'" -.." '"..tostring(ammo_count).."'" -) ---]] end - -- Set display variables - weapon = w - weapon_type = wt - ammo_type = ammo + -- Set variables + display.weapon = wpn + display.weapon_type = wpnt + display.ammo_type = ammo + if debug_flow then + local msg = + "Count" + .." "..tostring(PLACE[ammo]).."" + .." '"..tostring(wpnt).."'" + .." '"..tostring(wpn).."'" + debug_msg(msg) + else + -- not requested + end end +local function Events(action, reason) +--[[ +- Thrown has no durability so do not register for that event +- Remove ACTIONBAR_HIDEGRID; this is triggered when dragging an item to actionbar + Not sure why this was implemented - use of event changed? +--]] + if action == "register" then + TitanPanelAmmoButton:RegisterEvent("UNIT_INVENTORY_CHANGED") + TitanPanelAmmoButton:RegisterEvent("MERCHANT_CLOSED") + elseif action == "unregister" then + TitanPanelAmmoButton:UnregisterEvent("UNIT_INVENTORY_CHANGED") + TitanPanelAmmoButton:UnregisterEvent("MERCHANT_CLOSED") + else + -- action unknown ??? + end + + if debug_flow then + local msg = + "Events" + .." "..tostring(reason).."" + debug_msg(msg) + else + -- not requested + end +end +--[[ -- ************************************************************************** -- NAME : TitanPanelAmmoButton_OnLoad() -- DESC : Registers the plugin upon it loading -- ************************************************************************** +--]] function TitanPanelAmmoButton_OnLoad(self) self.registry = { id = TITAN_AMMO_ID, @@ -166,160 +245,67 @@ function TitanPanelAmmoButton_OnLoad(self) ShowAmmoName = false, DisplayOnRightSide = false, } - }; - - if class == "ROGUE" - or class == "WARRIOR" - or class == "HUNTER" - then - self:SetScript("OnEvent", function(_, event, arg1, arg2, ...) ---[[ -print("OnEvent" -.." '"..tostring(event).."'" -.." '"..tostring(arg1).."'" -.." '"..tostring(arg2).."'" -) ---]] - if event == "PLAYER_ENTERING_WORLD" then - if arg1 == true then -- login - TitanPanelAmmoButton_PLAYER_LOGIN() - end - if arg2 == true then -- reload / zoning - TitanPanelAmmoButton_MERCHANT_CLOSED() - end - elseif event == "UNIT_INVENTORY_CHANGED" then - TitanPanelAmmoButton_UNIT_INVENTORY_CHANGED(arg1, ...) - elseif event == "UPDATE_INVENTORY_DURABILITY" then - TitanPanelAmmoButton_UPDATE_INVENTORY_DURABILITY() - elseif event == "MERCHANT_CLOSED" then - TitanPanelAmmoButton_MERCHANT_CLOSED() - elseif event == "ACTIONBAR_HIDEGRID" then -- in case ammo is dropped into char slot - TitanPanelAmmoButton_ACTIONBAR_HIDEGRID() - end - end) - - TitanPanelAmmoButton:RegisterEvent("PLAYER_ENTERING_WORLD") - - else - ClrAmmoInfo() - end -end - -function TitanPanelAmmoButton_PLAYER_LOGIN() - ammo_show = true - GetAmmoCount() - - if IsThrown(ammo_type) then - TitanPanelAmmoButton:RegisterEvent("UPDATE_INVENTORY_DURABILITY") - elseif IsAmmo(ammo_type) then - TitanPanelAmmoButton:RegisterEvent("ACTIONBAR_HIDEGRID") - end - TitanPanelAmmoButton:RegisterEvent("UNIT_INVENTORY_CHANGED") - TitanPanelAmmoButton:RegisterEvent("MERCHANT_CLOSED") - TitanPanelAmmoButton:RegisterEvent("PLAYER_ENTERING_WORLD") - - TitanPanelButton_UpdateButton(TITAN_AMMO_ID); -end - -function TitanPanelAmmoButton_UNIT_INVENTORY_CHANGED(arg1, ...) - if arg1 == "player" then - TitanPanelAmmoUpdateDisplay(); - end -end - -function TitanPanelAmmoButton_UPDATE_INVENTORY_DURABILITY() - TitanPanelAmmoUpdateDisplay(); -end - -function TitanPanelAmmoButton_MERCHANT_CLOSED() - TitanPanelAmmoUpdateDisplay(); -end - -function TitanPanelAmmoButton_ACTIONBAR_HIDEGRID() - local prev = 0 - TitanPanelAmmoButton:SetScript("OnUpdate", function(_, e) - prev = prev + e - if prev > 2 then - TitanPanelAmmoButton:SetScript("OnUpdate", nil) - TitanPanelAmmoUpdateDisplay(); - end - end) -end - -function TitanPanelAmmoUpdateDisplay() - -- Manual Display update in case the rangedSlot it switched - GetAmmoCount() - - -- Setup the events based on ammo type - if IsThrown(ammo_type) then - if not TitanPanelAmmoButton:IsEventRegistered("UPDATE_INVENTORY_DURABILITY") then - TitanPanelAmmoButton:RegisterEvent("UPDATE_INVENTORY_DURABILITY") - end - if TitanPanelAmmoButton:IsEventRegistered("ACTIONBAR_HIDEGRID") then - TitanPanelAmmoButton:UnregisterEvent("ACTIONBAR_HIDEGRID") - TitanPanelAmmoButton:SetScript("OnUpdate", nil) - end - elseif IsAmmo(ammo_type) then - if TitanPanelAmmoButton:IsEventRegistered("UPDATE_INVENTORY_DURABILITY") then - TitanPanelAmmoButton:UnregisterEvent("UPDATE_INVENTORY_DURABILITY") - end - if not TitanPanelAmmoButton:IsEventRegistered("ACTIONBAR_HIDEGRID") then - TitanPanelAmmoButton:RegisterEvent("ACTIONBAR_HIDEGRID") - end - end - - TitanPanelButton_UpdateButton(TITAN_AMMO_ID); + } end +--[[ -- ************************************************************************** -- NAME : TitanPanelAmmoButton_GetButtonText(id) --- DESC : Calculate ammo/thrown logic then display data on button +-- DESC : Calculate ammo/thrown logic then put on button -- VARS : id = button ID -- ************************************************************************** +--]] function TitanPanelAmmoButton_GetButtonText(id) local labelText, ammoText, ammoRichText, color; - if (IsThrown(ammo_type)) then + if (IsThrown(display.ammo_type)) then labelText = L["TITAN_AMMO_BUTTON_LABEL_THROWN"]; - ammoText = format(L["TITAN_AMMO_FORMAT"], ammo_count); - labelText = weapon_type.." : " - if TitanGetVar(TITAN_AMMO_ID, "ShowAmmoName") and ammo_name ~= "" then - ammoText = ammoText.."|cffffff9a".." ("..ammo_name..")".."|r" + ammoText = format(L["TITAN_AMMO_FORMAT"], display.ammo_count); + labelText = display.weapon_type.." : " + if TitanGetVar(TITAN_AMMO_ID, "ShowAmmoName") and display.ammo_name ~= "" then + ammoText = ammoText.."|cffffff9a".." ("..display.ammo_name..")".."|r" end - elseif IsAmmo(ammo_type) then + elseif IsAmmo(display.ammo_type) then labelText = L["TITAN_AMMO_BUTTON_LABEL_AMMO"]; - labelText = weapon_type.." : " - ammoText = format(L["TITAN_AMMO_FORMAT"], ammo_count); - if TitanGetVar(TITAN_AMMO_ID, "ShowAmmoName") and ammo_name ~= "" then - ammoText = ammoText.."|cffffff9a".." ("..ammo_name..")".."|r" + labelText = display.weapon_type.." : " + ammoText = format(L["TITAN_AMMO_FORMAT"], display.ammo_count); + if TitanGetVar(TITAN_AMMO_ID, "ShowAmmoName") and display.ammo_name ~= "" then + ammoText = ammoText.."|cffffff9a".." ("..display.ammo_name..")".."|r" end else ClrAmmoInfo() - ammoText = ammo_name - labelText = weapon_type.."" + ammoText = display.ammo_name + labelText = display.weapon_type.."" end ---[[ -print("_GetButtonText" -.." '"..tostring(weapon_type).."'" -.." '"..tostring(ammo_type).."'" -) ---]] if (TitanGetVar(TITAN_AMMO_ID, "ShowColoredText")) then - color = TitanUtils_GetThresholdColor(TITAN_AMMO_THRESHOLD_TABLE[ammo_type], ammo_count); + color = TitanUtils_GetThresholdColor(TITAN_AMMO_THRESHOLD_TABLE[display.ammo_type], display.ammo_count); ammoRichText = TitanUtils_GetColoredText(ammoText, color); else ammoRichText = TitanUtils_GetHighlightText(ammoText); end + if debug_flow then + local msg = + "Btn_Text" + .." '"..tostring(display.weapon_type).."'" + .." '"..tostring(PLACE[display.ammo_type]).."'" + .." '"..tostring(ammoRichText).."'" + debug_msg(msg) + else + -- not requested + end + return labelText, ammoRichText; end +--[[ -- ************************************************************************** -- NAME : TitanPanelRightClickMenu_PrepareAmmoMenu() -- DESC : Display rightclick menu options -- ************************************************************************** +--]] function TitanPanelRightClickMenu_PrepareAmmoMenu() TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_AMMO_ID].menuText); @@ -344,14 +330,95 @@ end function TitanPanelAmmoButton_GetTooltipText() local txt = "" - if IsThrown(ammo_type) then - txt = txt - ..tostring(weapon).."" + if IsThrown(display.ammo_type) then + txt = txt + ..tostring(display.weapon).."" else txt = txt - ..tostring(weapon).."\n" - ..tostring(ammo_name).."" + ..tostring(display.weapon).."\n" + ..tostring(display.ammo_name).."" end return txt end +function OnShow() + ClrAmmoInfo() + + if IsAmmoClass() then + -- No need to start events and consume cycles if no ammo + GetAmmoCount() + + Events("register", "OnShow") + else + -- Just set the default text on button + -- for a class w/o ammo + end + + TitanPanelButton_UpdateButton(TITAN_AMMO_ID); +end + +function OnHide() + ClrAmmoInfo() + + Events("unregister", "OnHide") +end + +local function UpdateDisplay() + GetAmmoCount() + + TitanPanelButton_UpdateButton(TITAN_AMMO_ID); +end + +--[[ +-- ************************************************************************** +-- NAME : TitanPanelAmmoButton_OnEvent() +-- DESC : React to any registered Events +-- ************************************************************************** +--]] +function TitanPanelAmmoButton_OnEvent(self, event, arg1, arg2, ...) + if event == "PLAYER_ENTERING_WORLD" then + if arg1 == true then -- login + EnterWorld() + end + if arg2 == true then -- reload / zoning + UpdateDisplay(); + end + elseif event == "UNIT_INVENTORY_CHANGED" then + if arg1 == "player" then + UpdateDisplay(); + end + elseif event == "MERCHANT_CLOSED" then + UpdateDisplay(); + end +end + +-- ====== Create needed frames +local function Create_Frames() + if _G[TITAN_BUTTON] then + return -- if already created + end + + -- general container frame + local f = CreateFrame("Frame", nil, UIParent) +-- f:Hide() + + -- Titan plugin button + local window = CreateFrame("Button", TITAN_BUTTON, f, "TitanPanelComboTemplate") + window:SetFrameStrata("FULLSCREEN") + -- Using SetScript("OnLoad", does not work + TitanPanelAmmoButton_OnLoad(window); +-- TitanPanelButton_OnLoad(window); -- Titan XML template calls this... + + window:SetScript("OnEvent", function(self, event, ...) + TitanPanelAmmoButton_OnEvent(self, event, ...) + end) + window:SetScript("OnShow", function(self, button) + OnShow(self) + end) + window:SetScript("OnHide", function(self, button) + OnHide(self) + end) + +end + +Create_Frames() -- do the work diff --git a/TitanAmmo/TitanClassicAmmo.xml b/TitanAmmo/TitanClassicAmmo.xml deleted file mode 100644 index f9c6707..0000000 --- a/TitanAmmo/TitanClassicAmmo.xml +++ /dev/null @@ -1,16 +0,0 @@ - -