Thread Tools Display Modes
01-29-23, 03:48 AM   #1
Shateiel
A Defias Bandit
Join Date: Jan 2023
Posts: 2
help me configure target/player frames fade in/outs

Hello there,

I don't have any clue about coding. Just want PlayerFrame and TargetFrame to fade in/out in some conditions so I've searched for an addon, they mostly do more than i want like ElvUI. I can't use those compilation AddOns because i am using some others for each particular one like; KuiNameplates for nameplates, Bartender4 for action bars, Bagnon for inventory etc.

So I've searched for codes to use them in addon.bool.no to be able to implement it to my game. Found a code as this;

Code:
PlayerFrame:SetAlpha(0.05)
EventRegistry:RegisterCallback("PLAYER_REGEN_DISABLED", function() 
	PlayerFrame:SetAlpha(1)
end)

EventRegistry:RegisterCallback("PLAYER_REGEN_ENABLED", function() 
	PlayerFrame:SetAlpha(0.05)
end)

PlayerFrame:HookScript("OnEnter", function(self)
	self:SetAlpha(1)
end)

PlayerFrame:HookScript("OnLeave", function(self)
	if not InCombatLockdown() then
		self:SetAlpha(0.05)
	end
end)
This was working perfectly fine then I thought "why don't i use this also for Target Frame and added some codes with trial and error and it happened to be like;

Code:
PlayerFrame:SetAlpha(0.05)
EventRegistry:RegisterCallback("PLAYER_REGEN_DISABLED", function() 
	PlayerFrame:SetAlpha(1)
end)

EventRegistry:RegisterCallback("PLAYER_REGEN_ENABLED", function() 
	PlayerFrame:SetAlpha(0.05)
end)

PlayerFrame:HookScript("OnEnter", function(self)
	self:SetAlpha(1)
end)

PlayerFrame:HookScript("OnLeave", function(self)
	if not InCombatLockdown() then
		self:SetAlpha(0.05)
	end
end)

TargetFrame:SetAlpha(0.2)
EventRegistry:RegisterCallback("PLAYER_REGEN_DISABLED", function() 
	TargetFrame:SetAlpha(1)
end)

EventRegistry:RegisterCallback("PLAYER_REGEN_ENABLED", function() 
	TargetFrame:SetAlpha(0.2)
end)

TargetFrame:HookScript("OnEnter", function(self)
	self:SetAlpha(1)
end)

TargetFrame:HookScript("OnLeave", function(self)
	if not InCombatLockdown() then
		self:SetAlpha(0.2)
	end
end)
This was also working perfectly fine but there was a problem which is PlayerFrame and TargetFrame was not fading in when i target myself out of combat. I still couldn't figure it out how I can implement it in this addon. Tried a lot of code groups for "PLAYER_CHANGED_TARGET" and "UNIT_TARGET" with if UnitIsUnit conditions. They didn't work. Tried some HookScript with "OnEvent" tags, that didn't work neither. maybe it was just me who doesn't know anything about coding and made some mistakes on punctuation marks etc. but I quite learn quick and made it work for this code here;

Code:
TargetFrame:HookScript("OnShow", function(self)
    if UnitIsUnit("targettarget", "player") then
      self:SetAlpha(1)
    else
      self:SetAlpha(0.2)
    end
end)
Which makes TargetFrame's Alpha to stay at value 1.0 but i can't use it for PlayerFrame because PlayerFrame doesn't goes to "hide" situation even its Alpha value is (0.05).

So my problem with PlayerFrame still exists. I just want its Alpha to be on value(1) when i target myself to be able to see my health etc. because i am playing the game almost out of ui and things appear on my screen when I am in combat like this;



if you look carefully you can see my PlayerFrame, action bars etc. there on alpha value(0.05). They pop up when I hover my cursor over them. like this;


into this on combat;



Thanks for any helps in advance.

Last edited by Shateiel : 01-29-23 at 03:55 AM. Reason: Addition and extra information about hovering the cursor over
  Reply With Quote
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,324
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
01-30-23, 08:33 PM   #3
Shateiel
A Defias Bandit
Join Date: Jan 2023
Posts: 2
Thanks for insightful and kind reply which helps me to understand the coding more.

Originally Posted by SDPhantom View Post

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.
I tried without any addons but it didn't work without any neither. I just can't make PLAYER_TARGET_CHANGED event to work for playerframe.

Also, at first; copy-pasted all of the codes you provided, it does nothing. Should I add something ? this "local" code is whole new thing for me, sorry if the question is a stupid one.
  Reply With Quote
01-30-23, 09:20 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,324
I had a semicolon in a bad spot. I ended up rewriting the code to use OnUpdate because each UnitFrame has a lot of children that made the OnEnter/OnLeave scripts fire erratically.

Lua Code:
  1. --  Configuration
  2. local ShowAlpha=1;
  3. local HideAlpha=0.05;
  4.  
  5. --  Visibility logic
  6. local function UnitFrame_UpdateVisibility(self)
  7.     self:SetAlpha(
  8.         (
  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. PlayerFrame:HookScript("OnUpdate",UnitFrame_UpdateVisibility);
  17. TargetFrame:HookScript("OnUpdate",UnitFrame_UpdateVisibility);


For future reference, script errors are turned off by default. You can enable them by typing this command into chat.
Code:
/console scriptErrors 1
Also local limits the visibility of a function/variable to a specific blocks of code referred to as its "scope". This is in contrast to "globals", which can be accessed by anyone. It's recommended to use locals whenever possible to prevent accidental overwriting of another code's variables or the same happening to yours.
__________________
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 : 02-01-23 at 08:33 PM. Reason: removed excess end parenthesis from :SetAlpha() call
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Search/Requests » help me configure target/player frames fade in/outs


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off