From 136a65a75e3dbec4bfa5f87aa0094baa5ad4065e Mon Sep 17 00:00:00 2001 From: Zol Date: Fri, 6 Feb 2026 22:59:26 +0100 Subject: [PATCH] add option to hide resource bars when mounted --- Localization/enUS.lua | 1 + Modules/ResourceBars/ResourceBars.lua | 60 +++++++++++++++++++ Modules/ResourceBars/ResourceBars_Options.lua | 48 +++++++++++++++ visibility.lua | 30 +++++++++- 4 files changed, 138 insertions(+), 1 deletion(-) diff --git a/Localization/enUS.lua b/Localization/enUS.lua index 16d6da9..fd7a45e 100644 --- a/Localization/enUS.lua +++ b/Localization/enUS.lua @@ -396,6 +396,7 @@ L["ASSIGN_ENTRY_TO_VIEWER_DESC"] = "Assign entry to viewer" L["VISIBILITY"] = "Visibility" L["VISIBILITY_DESC"] = "When to show all CDM viewers. Tick the states where the bar should be visible; untick to hide in that state. Default: shown everywhere." +L["RESOURCE_BARS_VISIBILITY_DESC"] = "Hide resource bars based on mount status. Also works for druid Travel Form." L["VISIBILITY_COMBAT"] = "Combat" L["VISIBILITY_COMBAT_DESC"] = "Show only in combat, only out of combat, or both (leave both unchecked)." L["SHOW_IN_COMBAT"] = "Show in combat" diff --git a/Modules/ResourceBars/ResourceBars.lua b/Modules/ResourceBars/ResourceBars.lua index a0d240b..f1b939e 100644 --- a/Modules/ResourceBars/ResourceBars.lua +++ b/Modules/ResourceBars/ResourceBars.lua @@ -64,6 +64,10 @@ local DEFAULTS_SEGMENTED = { local defaults = { enabled = true, throttleInterval = 0.1, + visibility = { + hideWhenMounted = false, + hideWhenMountedWhen = "both", + }, resourceColours = {}, classColours = {}, resourceBarAnchorConfig = nil, @@ -186,6 +190,13 @@ function module:OnEnable() self:RegisterPowerEvents() -- Listen for UI scale changes to refresh pixel-perfect elements self:RegisterMessage("TavernUI_UIScaleChanged", "OnUIScaleChanged") + -- Register visibility callback for mount/vehicle hiding + if TavernUI.Visibility and TavernUI.Visibility.RegisterCallback and not self._visibilityCallbackId then + self._visibilityCallbackId = TavernUI.Visibility.RegisterCallback(function() + if self:IsEnabled() then self:OnVisibilityChanged() end + end) + end + self:OnVisibilityChanged() end function module:OnUIScaleChanged() @@ -195,10 +206,16 @@ end function module:OnDisable() self:UnregisterAllEvents() self:ClearAllBars() + if self._visibilityCallbackId and TavernUI.Visibility then + TavernUI.Visibility.UnregisterCallback(self._visibilityCallbackId) + self._visibilityCallbackId = nil + end end function module:OnProfileChanged() + self._hiddenByVisibility = nil self:RebuildActiveBars() + self:OnVisibilityChanged() end function module:OnPlayerEnteringWorld() @@ -215,6 +232,44 @@ function module:OnShapeshiftFormChanged() end end +function module:ShouldHideByVisibility() + local Visibility = TavernUI and TavernUI.Visibility + if not Visibility then return false end + + -- Check ResourceBars' own visibility config + local config = self:GetSetting("visibility") + if config and not Visibility.ShouldShow(config) then return true end + + -- Also respect uCDM's mount visibility so one toggle hides everything + local ucdm = TavernUI:GetModule("uCDM", true) + if ucdm and ucdm.GetSetting then + local ucdmConfig = ucdm:GetSetting("general.visibility") + if ucdmConfig and ucdmConfig.hideWhenMounted then + local mountConfig = { + hideWhenMounted = ucdmConfig.hideWhenMounted, + hideWhenMountedWhen = ucdmConfig.hideWhenMountedWhen, + } + if not Visibility.ShouldShow(mountConfig) then return true end + end + end + + return false +end + +function module:OnVisibilityChanged() + if self:ShouldHideByVisibility() then + for _, bar in pairs(bars) do + if bar.Hide then bar:Hide() end + end + self._hiddenByVisibility = true + elseif self._hiddenByVisibility then + self._hiddenByVisibility = nil + for barId in pairs(bars) do + self:UpdateBar(barId) + end + end +end + function module:RebuildActiveBars() if not self:IsEnabled() then return @@ -543,6 +598,11 @@ function module:UpdateBar(barId) return end + if self._hiddenByVisibility then + bars[barId]:Hide() + return + end + bars[barId].config = self:GetBarConfig(barId) local config = bars[barId].config if config[CONSTANTS.KEY_ENABLED] == false then diff --git a/Modules/ResourceBars/ResourceBars_Options.lua b/Modules/ResourceBars/ResourceBars_Options.lua index b7880f9..f95ddf2 100644 --- a/Modules/ResourceBars/ResourceBars_Options.lua +++ b/Modules/ResourceBars/ResourceBars_Options.lua @@ -1438,6 +1438,10 @@ local function BuildClassColoursOptions() end function Options:Initialize() + local function onVisibilitySettingChanged() + if module.OnVisibilityChanged then module:OnVisibilityChanged() end + end + local args = { general = { type = "group", @@ -1476,6 +1480,50 @@ function Options:Initialize() module:SetSetting("throttleInterval", value) end, }, + visibilityHeader = { + type = "header", + name = L["VISIBILITY"], + order = 10, + }, + visibilityDesc = { + type = "description", + name = L["RESOURCE_BARS_VISIBILITY_DESC"], + order = 11, + fontSize = "small", + }, + hideWhenMounted = { + type = "toggle", + name = L["HIDE_WHEN_MOUNTED"], + order = 12, + get = function() + return module:GetSetting("visibility.hideWhenMounted", false) + end, + set = function(_, value) + module:SetSetting("visibility.hideWhenMounted", value) + onVisibilitySettingChanged() + end, + }, + hideWhenMountedWhen = { + type = "select", + name = L["HIDE_WHEN_MOUNTED_WHEN"], + desc = L["HIDE_WHEN_MOUNTED_WHEN_DESC"], + order = 13, + values = { + both = L["VISIBILITY_WHEN_BOTH"], + grounded = L["VISIBILITY_WHEN_GROUNDED"], + flying = L["VISIBILITY_WHEN_FLYING"], + }, + get = function() + return module:GetSetting("visibility.hideWhenMountedWhen", "both") + end, + set = function(_, value) + module:SetSetting("visibility.hideWhenMountedWhen", value) + onVisibilitySettingChanged() + end, + disabled = function() + return not module:GetSetting("visibility.hideWhenMounted", false) + end, + }, }, }, health = { diff --git a/visibility.lua b/visibility.lua index ecb287e..e3fef4a 100644 --- a/visibility.lua +++ b/visibility.lua @@ -13,6 +13,19 @@ local lastMounted, lastFlying local pollTimer = nil local POLL_INTERVAL = 0.5 +local playerClass +local TRAVEL_FORM_SPELL_ID = 783 +local MOUNT_FORM_SPELL_ID = 210053 + +local function IsInDruidTravelForm() + if not playerClass then + playerClass = select(2, UnitClass("player")) + end + if playerClass ~= "DRUID" then return false end + return C_UnitAuras.GetPlayerAuraBySpellID(TRAVEL_FORM_SPELL_ID) ~= nil + or C_UnitAuras.GetPlayerAuraBySpellID(MOUNT_FORM_SPELL_ID) ~= nil +end + local function UpdateCache() cache.inCombat = UnitAffectingCombat("player") cache.hasTarget = UnitExists("target") @@ -24,7 +37,7 @@ local function UpdateCache() cache.groupRaid = IsInRaid() cache.instanceType = select(2, GetInstanceInfo()) or "none" cache.role = UnitGroupRolesAssigned("player") or "NONE" - cache.mounted = IsMounted() + cache.mounted = IsMounted() or IsInDruidTravelForm() cache.flying = IsFlying() cache.inVehicle = UnitInVehicle("player") end @@ -146,11 +159,26 @@ function Visibility.Initialize() eventFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA") eventFrame:RegisterEvent("GROUP_ROSTER_UPDATE") eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD") + eventFrame:RegisterEvent("UPDATE_SHAPESHIFT_FORM") eventFrame:RegisterUnitEvent("UNIT_FLAGS", "player") + eventFrame:RegisterUnitEvent("UNIT_AURA", "player") eventFrame:SetScript("OnEvent", function(f, event, arg1) if event == "UNIT_ENTERED_VEHICLE" or event == "UNIT_EXITED_VEHICLE" then if arg1 ~= "player" then return end end + -- UNIT_AURA fires frequently; only check mount/flying state, skip full cache update + if event == "UNIT_AURA" then + local mounted = IsMounted() or IsInDruidTravelForm() + local flying = IsFlying() + if mounted ~= cache.mounted or flying ~= cache.flying then + cache.mounted = mounted + cache.flying = flying + lastMounted, lastFlying = mounted, flying + SyncMountPoll() + NotifyCallbacks() + end + return + end OnEvent(f, event) end) UpdateCache()