WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   AddOn Search/Requests (https://www.wowinterface.com/forums/forumdisplay.php?f=6)
-   -   LUA: Hide default frames OOC and show if target (https://www.wowinterface.com/forums/showthread.php?t=52260)

Valixx 05-01-15 03:10 AM

LUA: Hide default frames OOC and show if target
 
Hello guys.

I need some help with the following. Here's what I need it ( the code ) to do:
  • Hide if out of combat ( this works so far )
  • show if I have a target ( this isn't )

Lua Code:
  1. local f = CreateFrame('frame', nil, nil, 'SecureHandlerStateTemplate')
  2. f:SetFrameRef('PlayerFrame', PlayerFrame)
  3. f:SetFrameRef('TargetFrame', TargetFrame)
  4. f:SetFrameRef('FocusFrame', FocusFrame)
  5. f:SetFrameRef('ComboFrame', ComboFrame)
  6. f:SetAttribute('_onstate-combat', [=[ -- Securely toggle visibility in combat
  7.     if newstate == 'show' then
  8.         self:GetFrameRef('PlayerFrame'):Show()
  9.         if UnitExists('target') then
  10.             self:GetFrameRef('TargetFrame'):Show()
  11.             self:GetFrameRef('PlayerFrame'):Show()
  12.         end
  13.         if UnitExists('focus') then
  14.             self:GetFrameRef('FocusFrame'):Show()
  15.         end
  16.     else
  17.         self:GetFrameRef('PlayerFrame'):Hide()
  18.         self:GetFrameRef('TargetFrame'):Hide()
  19.         self:GetFrameRef('FocusFrame'):Hide()
  20.         self:GetFrameRef('ComboFrame'):Hide()
  21.     end
  22. ]=])
  23. RegisterStateDriver(f, 'combat', '[combat] show; hide', '[target, exists] show')
  24.  
  25. local function HideFrame(self) -- Insecurely hide out of combat if shown
  26.     if not InCombatLockdown() or not UnitExists("target") then self:Hide() end
  27. end
  28. PlayerFrame:HookScript('OnShow', HideFrame)
  29. TargetFrame:HookScript('OnShow', HideFrame)
  30. FocusFrame:HookScript('OnShow', HideFrame)
  31. ComboFrame:HookScript('OnShow', HideFrame)
  32.  
  33. f:SetScript('OnEvent', function(self, event)
  34.     if GetComboPoints('player', 'target') > 0 then -- Show ComboFrame upon entering combat if we have points
  35.         ComboFrame:Show()
  36.     end
  37. end)
  38. f:RegisterEvent('PLAYER_REGEN_DISABLED')

semlar 05-01-15 03:21 AM

You have your target condition as a completely separate 4th argument for RegisterStateDriver, which only takes 3 arguments.
Lua Code:
  1. RegisterStateDriver(f, 'combat', '[combat] show; hide', '[target, exists] show')

You need to combine the macro conditions into one string that handles both situations.
Lua Code:
  1. RegisterStateDriver(f, 'combat', '[target,exists][combat] show; hide')

The other problem is that you're hooking all of their OnShow scripts to hide the frame 'if not InCombatLockdown() or not UnitExists("target")', which means it will always attempt to hide the frame if it's shown out of combat.

It should use "and" instead of "or" since you can't securely hide the frames in combat that way, however it should not be necessary to hide any of the frames this way if the state driver is working, so you might as well remove the hooks.

Valixx 05-01-15 03:33 AM

Thanks for answering. I'll see what I can do later... need to go and cook something fine :)

Allright, just seen your edit. I'll try that then.

I'll report back later. Thank you.

semlar 05-01-15 03:50 AM

You might also consider reparenting all of these frames to frame f and simply hiding and showing that rather than handling them separately, that way you won't have to worry about them being shown when they're not supposed to be.

If you choose to do that you'll want to make f a child of UIParent to keep the scales consistent.

Valixx 05-01-15 05:13 AM

The unitframes are working fine now. If there's a target and in combat, they are showing. Extactly what I wanted.

Lua Code:
  1. local f = CreateFrame('frame', nil, nil, 'SecureHandlerStateTemplate')
  2. f:SetFrameRef('PlayerFrame', PlayerFrame)
  3. f:SetFrameRef('TargetFrame', TargetFrame)
  4. f:SetFrameRef('FocusFrame', FocusFrame)
  5. f:SetFrameRef('ComboFrame', ComboFrame)
  6. f:SetAttribute('_onstate-combat', [=[ -- Securely toggle visibility in combat
  7.     if newstate == 'show' then
  8.         self:GetFrameRef('PlayerFrame'):Show()
  9.         if UnitExists('target') then
  10.             self:GetFrameRef('TargetFrame'):Show()
  11.             self:GetFrameRef('PlayerFrame'):Show()
  12.         end
  13.         if UnitExists('focus') then
  14.             self:GetFrameRef('FocusFrame'):Show()
  15.         end
  16.     else
  17.         self:GetFrameRef('PlayerFrame'):Hide()
  18.         self:GetFrameRef('TargetFrame'):Hide()
  19.         self:GetFrameRef('FocusFrame'):Hide()
  20.         self:GetFrameRef('ComboFrame'):Hide()
  21.     end
  22. ]=])
  23. RegisterStateDriver(f, 'combat', '[target,exists][combat] show; hide')
  24.  
  25. f:RegisterEvent('PLAYER_REGEN_DISABLED')

The second thing I wanted to try is to show the minimap only if in combat. This is what I got so far:

Lua Code:
  1. MinimapCluster:RegisterEvent("PLAYER_REGEN_ENABLED")
  2. MinimapCluster:RegisterEvent("PLAYER_REGEN_DISABLED")
  3. MinimapCluster:HookScript("OnEvent", function(self, event)
  4.     if event == "PLAYER_REGEN_ENABLED" then
  5.         self:Hide()
  6.     else
  7.         self:Show()
  8.     end
  9. end)

This is what happens after a /reload:

Out of combat -> Minimap is shown.
Entering combat -> Minimap is shown.
Leaving combat -> Minimap is hidden.

Any tips on how to hide it from the start ( login / reload )?

semlar 05-01-15 05:33 AM

Quote:

Originally Posted by Valixx (Post 308551)
Any tips on how to hide it from the start ( login / reload )?

Register event PLAYER_LOGIN and do something like self:SetShown(event == 'PLAYER_REGEN_DISABLED')

Valixx 05-01-15 06:11 AM

I tried so many things, it just won't work... still trying..

I even added commands and stuff, everything's working fine but the hide on login... Tried with ADDON_LOADED, PLAYER_ENTERING_WORLD and PLAYER_LOGIN.

semlar 05-01-15 06:33 AM

I don't know what you tried, but this works fine for me, at least as far as I understand what you're asking.
Lua Code:
  1. MinimapCluster:RegisterEvent("PLAYER_REGEN_ENABLED")
  2. MinimapCluster:RegisterEvent("PLAYER_REGEN_DISABLED")
  3. MinimapCluster:RegisterEvent("PLAYER_LOGIN")
  4. MinimapCluster:HookScript("OnEvent", function(self, event)
  5.     self:SetShown(event == "PLAYER_REGEN_DISABLED")
  6. end)

Valixx 05-01-15 07:22 AM

Got everything working now. I did the same thing to hide the BuffFrame and it works too.

Thanks for your help, I appreaciate it. :)


All times are GMT -6. The time now is 02:39 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI