View Single Post
01-30-23, 03:49 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
It'll probably help to consolidate the logic into a single function that evaluates the entire list of conditions in which you want the UnitFrames to show. Additional "handlers" were added to link the events and mouse actions to the visibility logic.

Lua Code:
  1. --  Configuration
  2. local ShowAlpha=1;
  3. local HideAlpha=0.05;
  4.  
  5. --  Visibility logic
  6. local function UnitFrame_UpdateVisibility(self,force)
  7.     self:SetAlpha(
  8.         (force or (force==nil and (--   Allow sending true/false to override logic, leave as nil to allow
  9.             InCombatLockdown()--    Check combat
  10.         or  UnitIsUnit("target","player")-- Check targeting self
  11.         or  self:IsMouseOver()--    Check mouseover
  12.         ))) and ShowAlpha or HideAlpha
  13.     );
  14. end
  15.  
  16. --  Shared event handler
  17. local function EventRegistry_OnEvent(self,event,...)
  18.     local force=(event=="PLAYER_REGEN_DISABLED" or nil);--  InCombatLockdown() returns false when PLAYER_REGEN_DISABLED fires (we cast false to nil to allow the visibility logic to run)
  19.     UnitFrame_UpdateVisibility(PlayerFrame,force);
  20.     UnitFrame_UpdateVisibility(TargetFrame,force);
  21. end
  22.  
  23. EventRegistry:RegisterFrameEventAndCallback("PLAYER_REGEN_DISABLED",EventRegistry_OnEvent);
  24. EventRegistry:RegisterFrameEventAndCallback("PLAYER_REGEN_ENABLED",EventRegistry_OnEvent);
  25. EventRegistry:RegisterFrameEventAndCallback("PLAYER_TARGET_CHANGED",EventRegistry_OnEvent);
  26.  
  27. --  Shared mouse handler (This is meant to trim the arguments passed to the script so it doesn't mess with the visibility logic)
  28. local function UnitFrame_OnMouseMotion(self)
  29.     UnitFrame_UpdateVisibility(self);-- "force" argument is omitted, so it naturally gets nil
  30. end
  31.  
  32. PlayerFrame:HookScript("OnEnter",UnitFrame_OnMouseMotion);
  33. PlayerFrame:HookScript("OnLeave",UnitFrame_OnMouseMotion);
  34. TargetFrame:HookScript("OnEnter",UnitFrame_OnMouseMotion);
  35. TargetFrame:HookScript("OnLeave",UnitFrame_OnMouseMotion);



PS: EventRegistry can be a little confusing as its a shared system that can handle both internal callbacks and frame events. That being said, frame events require the event to be registered through EventRegistry:RegisterFrameEvent() in order for your callback registered with EventRegistry:RegisterCallback() to run. You can do both in a single call with EventRegistry:RegisterFrameEventAndCallback().

My guess is since this wasn't an issue for you, some other code (possibly not yours) is already using those events and had registered them properly. You shouldn't rely on this as it might not always be the case and EventRegistry runs a counter on how many functions are linked to any specific frame event. If those counters get out of sync by incomplete registrations, you may find yourself randomly no longer getting event triggers.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 01-30-23 at 08:38 PM.
  Reply With Quote