API IfMouseIsOver Question

I kind of want to make an addon that will hide some UI elements unless I’m mousing over them. The example google showed me is:

if (MouseIsOver(MinimapCluster)) then
– do something
end

I’m guessing that something like this is the direction I need to be going in:

ObjectiveTrackerFrame:Hide()
if (MouseIsOver(ObjectiveTrackerFrame)) then
ObjectiveTrackerFrame:Show(); else
ObjectiveTrackerFrame:Hide(); end

How difficult would it be to expand this with different functions (?) to work the same for the minimap and chat as well? And is there a way to do this to addon frames such as VuhDo, possibly with a UnitAffectingCombat(“player”) failsafe?

I’m guessing that the addon would look something like this:

ObjectiveTrackerFrame:Hide()
MinimapCluster:Hide()
ChatFrame:Hide() // to hide everything on startup

if (MouseIsOver(ObjectiveTrackerFrame)) then
ObjectiveTrackerFrame:Show(); else
ObjectiveTrackerFrame:Hide(); end

if (MouseIsOver(MinimapCluster)) then
MinimapCluster:Show(); else
MinimapCluster:Hide(); end

if (MouseIsOver(ChatFrame)) then
ChatFrame:Show(); else
ChatFrame:Hide(); end

Thanks in advance for putting up with my wall of text and probably horrible code <3

I recommend doing a SetAlpha rather than a Show/Hide.

In principle, yes you can do what you’re after. But in practice some default UI elements like the objective tracker has secure buttons and you will taint them and cause undesirable behavior in combat. Show/Hide is a protected action on secure frames. (Otherwise you could stack all your raid unit frames in one spot and show who needs healed, etc.)

Unlike Show/Hide, SetAlpha is pretty safe to apply to even secure frames. Though of course be careful if they do their own alpha stuff. (Which may happen with the ObjectiveTrackerFrame, but I’m not sure.)

local f = CreateFrame("Frame")
f.objTrackerShown = true
f.objTrackerTimer = 0
f:SetScript("OnUpdate",function(self,elapsed)
  f.objTrackerTimer = f.objTrackerTimer + elapsed
  if f.objTrackerTimer > 0.5 then
    f.objTrackerTimer = 0
    local isOver = MouseIsOver(ObjectiveTrackerFrame)
    if isOver and not f.objTrackerShown then
      f.objTrackerShown = true
      ObjectiveTrackerFrame:SetAlpha(1)
    elseif not isOver and f.objTrackerShown then
      f.objTrackerShown = false
      ObjectiveTrackerFrame:SetAlpha(0)
    end
  end
end)
2 Likes

also, can someone confirm, pretty sure if you hide a frame it will go “inactive”, and it wont register any mouse activity, so using alpha instead is a way better option as the frame remains “active”

Hiding a frame will stop its OnUpdate script but won’t stop it being “detected” by MouseIsOver.

See the Details section of:
https://wow.gamepedia.com/API_MouseIsOver