WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Show/hide Frame on button/keybind? (https://www.wowinterface.com/forums/showthread.php?t=48469)

10leej 11-07-13 04:53 AM

Show/hide Frame on button/keybind?
 
So I want to make a frame that shows/hides on a toggle function when a button is clicked or a specific keybind is pressed. How would I go about doing this?

zork 11-07-13 07:07 AM

Not directly related but maybe it can help you out aswell:
http://wowpedia.org/API_IsModifierKeyDown
You could register the MODIFIER_STATE_CHANGED event and check if a modifier key is clicked.

10leej 11-07-13 10:46 AM

I did a bit of fiddling, unfortunately I'm away from the desktop so I can't test it but I think I can do something like this for the key press, I'm probably way off though.

Code:

frame:SetScript("OnMouseDown", function(self, btn)
if btn == "RightButton" then 
  IsShiftKeyDown() then frame:Show() end end)
  else
    frame:Hide()
end


Phanx 11-09-13 04:13 AM

Haha, that's probably the highest concentration of Lua syntax errors I've ever seen in so little code. :p

Anyway, I'm not exactly sure what you're trying to do, but even if you fix the syntax errors on your code, I don't think it's going to do it. For one, your code basically says "show the frame on shift-right click, hide it on any other type of click". For another, you're using that script on the frame itself, and you can't click on a frame that's hidden, so you'd never be able to re-show it.

Do you want to show the frame when the button/key is pressed, and hide it again when the button/key is released; or show the frame on the first click/press and hide it on the second click/press?

10leej 11-09-13 11:49 AM

I wanna show it when pressed hide when released.

Phanx 11-10-13 12:08 AM

If you want to use mouse buttons, that's fairly easy, though you'll have to think about what you want to click on to show the frame, since you can't click on it while hidden. You could either use a second frame as a small always-visible area for clicking (eg. a tab or button) or hook the OnMouseDown script for the WorldFrame and show your frame if the cursor was in a particular area.

Code:

WorldFrame:HookScript("OnMouseDown", function(self, button)
  if button == "RightButton" and MyFrame:IsMouseOver() then
      MyFrame:Show()
  end
end)
WorldFrame:HookScript("OnMouseUp", function(self, button)
  MyFrame:Hide()
end)

^ Note that I'm not actually sure that will work, since I don't know that :IsMouseOver() will works for hidden frames. If it doesn't, you'd need to manually determine the boundaries of your frame and check the coordinates of the cursor to make sure it was within those boundaries.

If you want to use keyboard keys, that's only really possible if you want to use Alt, Ctrl, Shift, or some combination thereof. The only way to detect other keys is to keyboard-enable your frame, which basically makes it behave like a focused editbox, intercepting all keyboard input and preventing the use of keybinds for anything else.

Code:

MyFrame:RegisterEvent("MODIFIER_STATE_CHANGED")
MyFrame:SetScript("OnEvent", function(self, event, key, pressed)
  if key == "LALT" or key == "RALT" then
      if pressed == 1 then
        self:Show()
      else
        self:Hide()
      end
  end
end)


10leej 11-10-13 09:59 AM

I should be able to just use UIParent for the frame shouldn't I?

Phanx 11-11-13 05:46 PM

For which frame?

You should absolutely not use SetScript on the UIParent, if that's what you mean, and you really shouldn't be registering new events on it either. Just create your own frame. Takes 1 line of code and avoids all the potential problems.


All times are GMT -6. The time now is 04:15 AM.

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