WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Simple How to Hide and Show Window Help? (https://www.wowinterface.com/forums/showthread.php?t=50568)

Zireko 11-23-14 10:00 PM

Simple How to Hide and Show Window Help?
 
I'm simply trying to understand how to show and hide a from with a right mouse click. Here is the code where I think I don't have it correct. I'm remembering how I did this with Elder Scrolls Online so I was trying it this way and mixed it with a way to do it in wow that I've found online. However it's not working.

The Code for Hidding and showing the frame with right mouse click to my button.

Lua Code:
  1. local frame = CreateFrame("Button", ZBarOptionFrame, UIParent)
  2.  
  3. ZBarOptionFrame:RegisterForClicks("AnyUp")
  4. ZBarOptionFrame:SetScrpit("OnClick", function(self, button, ...)
  5.     if (button == "RightButton") then
  6.         if ZBarOptionFrame:IsShown() then
  7.             ZBarOptionFrame:SetHidden(false)
  8.         else
  9.             ZBarOptionFrame:SetHidden(true)
  10.         end
  11.     end
  12. end)

Here is the entire lua code I have so far remember I'm still working on this so it's not finished at all yet.

Lua Code:
  1. --Want to add Show Hide function for each window. ( barQWindow, barAWindow, barMWindow, barCWindow )
  2. --With the Show Hide I want to add this in an addon options window.
  3. --Want to add a lock frame for each window.
  4. --With lock frame I want to add this in an addon options window.
  5.  
  6. --Create a button using lua that pulls up a window for ZBar Options.
  7.  
  8. -- creates a generic button in the middle of the screen --
  9. ZBarButton = CreateFrame("Button","ZBarButton",UIParent,"UIPanelButtonTemplate")
  10. ZBarButton:SetPoint("CENTER",0,0)
  11. ZBarButton:SetWidth(30)
  12. ZBarButton:SetHeight(30)
  13. ZBarButton:SetText("ZB")
  14. ZBarButton:SetMovable(true)
  15. ZBarButton:RegisterForDrag("LeftButton")
  16. ZBarButton:SetScript("OnDragStart",ZBarButton.StartMoving)
  17. ZBarButton:SetScript("OnDragStop",ZBarButton.StopMovingOrSizing)
  18.  
  19. ZBarOptionFrame = CreateFrame("Frame")
  20. ZBarOptionFrame:ClearAllPoints()
  21. ZBarOptionFrame:SetBackdrop(StaticPopup1:GetBackdrop())
  22. ZBarOptionFrame:SetHeight(300)
  23. ZBarOptionFrame:SetWidth(300)
  24. ZBarOptionFrame:SetHidden(true)
  25.  
  26. ZBarOptionFrame.text = ZBarOptionFrame:CreateFontString(nil, "BACKGROUND", "GameFontNormal")
  27. ZBarOptionFrame.text:SetAllPoints()
  28. ZBarOptionFrame.text:SetText("ZBar Options Verison 1.0")
  29. ZBarOptionFrame:SetPoint("CENTER", 0, 0)
  30.  
  31. local frame = CreateFrame("Button", ZBarOptionFrame, UIParent)
  32.  
  33. ZBarOptionFrame:RegisterForClicks("AnyUp")
  34. ZBarOptionFrame:SetScrpit("OnClick", function(self, button, ...)
  35.     if (button == "RightButton") then
  36.         if ZBarOptionFrame:IsShown() then
  37.             ZBarOptionFrame:SetHidden(false)
  38.         else
  39.             ZBarOptionFrame:SetHidden(true)
  40.         end
  41.     end
  42. end)

Please if you write code back to me show it completely with all information and deep examples. I'm still very new to coding.

Seerah 11-23-14 11:22 PM

You should be getting an error. Make sure that you have Lua errors shown in your Interface Options. Also, install an error catching addons (BugSack is an example) to catch errors that happen early in the loading process.

frame:SetHidden(false) is API from ESO. You'll need to use the frame API provided by WoW.

frame:Hide()
frame:Show()

/edit: you also have a typo on line 34 ;)

Seerah 11-23-14 11:27 PM

Oh, and another thing. Unless you ABSOLUTELY need your button to have a global name, accessible from outside of your addon, then don't bother giving it a global name. You can just do:
Lua Code:
  1. local frame = CreateFrame("Button", nil, UIParent)

If you DO want it to have a global name, then use the local you are assigning your button to as your reference in the Lua code. Locals are faster than global lookups.
Lua Code:
  1. frame:RegisterForClicks("AnyUp")
  2. frame:SetScript("OnClick", function(self, button, ...)
  3.     if (button == "RightButton") then
  4.         if frame:IsShown() then
  5.             frame:Hide()
  6.         else
  7.             frame:Show()
  8.         end
  9.     end
  10. end)

/edit: heh... just noticed that you had the :SetHidden() call backwards anyway :p

SDPhantom 11-24-14 12:06 AM

You can use frame:SetShown(). In combination with this, you can toggle a frame's visibility with the following line.
Code:

frame:SetShown(not frame:IsShown())

Zireko 11-24-14 10:37 AM

I have tried everything you've suggested also I've flipped things around re-named my frame and it's still not working. My button that I made in the code isn't working with a right click to show and hide the window. Any way here is the code as of right now. Still don't know what I'm doing wrong yet.

Lua Code:
  1. --Want to add Show Hide function for each window. ( barQWindow, barAWindow, barMWindow, barCWindow )
  2. --With the Show Hide I want to add this in an addon options window.
  3. --Want to add a lock frame for each window.
  4. --With lock frame I want to add this in an addon options window.
  5.  
  6. --Create a button using lua that pulls up a window for ZBar Options.
  7.  
  8. -- creates a generic button in the middle of the screen --
  9. ZBarButton = CreateFrame("Button","ZBarButton",UIParent,"UIPanelButtonTemplate")
  10. ZBarButton:SetPoint("CENTER",0,0)
  11. ZBarButton:SetWidth(30)
  12. ZBarButton:SetHeight(30)
  13. ZBarButton:SetText("ZB")
  14. ZBarButton:SetMovable(true)
  15. ZBarButton:RegisterForDrag("LeftButton")
  16. ZBarButton:SetScript("OnDragStart",ZBarButton.StartMoving)
  17. ZBarButton:SetScript("OnDragStop",ZBarButton.StopMovingOrSizing)
  18.  
  19. ZBarOptionFrame = CreateFrame("Frame")
  20. ZBarOptionFrame:ClearAllPoints()
  21. ZBarOptionFrame:SetBackdrop(StaticPopup1:GetBackdrop())
  22. ZBarOptionFrame:SetHeight(300)
  23. ZBarOptionFrame:SetWidth(300)
  24. ZBarOptionFrame:Hide(true)
  25.  
  26. ZBarOptionFrame.text = ZBarOptionFrame:CreateFontString(nil, "BACKGROUND", "GameFontNormal")
  27. ZBarOptionFrame.text:SetAllPoints()
  28. ZBarOptionFrame.text:SetText("ZBar Options Verison 1.0")
  29. ZBarOptionFrame:SetPoint("CENTER", 0, 0)
  30.  
  31. local frame = CreateFrame("Button", nil, UIParent)
  32.  
  33. frame:RegisterForClicks("AnyUp")
  34. frame:SetScript("OnClick", function(self, button, ...)
  35.     if (button == "RightButton") then
  36.         if frame:IsShown() then
  37.             frame:Hide()
  38.         else
  39.             frame:Show()
  40.         end
  41.     end
  42. end)

Rilgamon 11-24-14 11:22 AM

I'm a little confused.
You're creating two buttons and a frame. For the second button you set up OnClick to hide itself but nothing declaring how your button should look like. (Difficult to click again once it is hidden btw)
The first you set up to be dragable.

Which button is supposed to do what when why? :)

Zireko 11-24-14 08:05 PM

Ok here is how it goes I created a button that is movable and it can be clicked, then I created a window which is going to be my options window. Then the last part is what everyones been telling me to do to hide that options window. Now I'm showing my code because I know something is completely wrong.

What it's supposed to be is a simple button that can be right clicked to toggle my options window which in that window I'm going to add much more but right now I simply want to get this part working first. Hope this clears everything up. I'm just starting code and it's much easier for me when people explain where the code needs to go and why it needs to go there. In other words please just copy my code and put -- comments where something needs to be fixed, and tell me exactly how to fix it so I can better understand how this works exactly.

Phanx 11-24-14 08:45 PM

Let's call your two objects Frame and Button. When you click on Button, you want to toggle whether Frame is shown or hidden.

To do that, you need to set an OnClick script on Button, that checks whether Frame is hidden or shown, and then shows or hides Frame as needed.

The problem with your current code is that you are setting the OnClick script on Button, which is correct, but inside that script you are checking the visible status of Button, and hiding or showing Button, which is not correct -- you need to change the script to look at and toggle Frame, not Button.

Also, having your questions about the same body of code randomly split across two threads is fairly confusing; I'd suggest sticking to one thread.

Fizzlemizz 11-24-14 08:48 PM

Code:

local frame = CreateFrame("Button", nil, UIParent)
 
frame:RegisterForClicks("AnyUp")
frame:SetScript("OnClick", function(self, button, ...)
    if (button == "RightButton") then
        if frame:IsShown() then
            frame:Hide()
        else
            frame:Show()
        end
    end
end)

What you are doing here is creating a button you can't see and telling it to hide/show itself when clicked (well, it will only hide because once hidden it can't be clicked).

What you are looking for is something like:

Code:

local frame = CreateFrame("Button", nil, UIParent)
 -- Add a texture and size so you can see it then anchor it where ever
frame:RegisterForClicks("AnyUp")
frame:SetScript("OnClick", function(self, button, ...)
    if (button == "RightButton") then
        if ZBarOptionFrame:IsVisible() then
            ZBarOptionFrame:Hide()
        else
            ZBarOptionFrame:Show()
        end
    end
end)


SDPhantom 11-25-14 12:20 PM

My question is, why isn't the script set to run on ZBarButton? Why is there an anonymous button created to handle what the original button was intended for?

Here's my suggestion:
Lua Code:
  1. --  Local prototypes will make sure the OnClick handler will access the OptionsFrame set afterwards
  2. local BarButton,OptionsFrame
  3.  
  4. BarButton = CreateFrame("Button","ZBarButton",UIParent,"UIPanelButtonTemplate")
  5. BarButton:SetPoint("CENTER",0,0)
  6. BarButton:SetWidth(30)
  7. BarButton:SetHeight(30)
  8. BarButton:SetText("ZB")
  9. BarButton:SetMovable(true)
  10. BarButton:RegisterForClicks("AnyUp")
  11. BarButton:RegisterForDrag("LeftButton")
  12. BarButton:SetScript("OnClick", function(self, button, ...)
  13.     if (button == "RightButton") then
  14.         OptionsFrame:SetShown(not OptionsFrame:IsShown())
  15.     end
  16. end)
  17. BarButton:SetScript("OnDragStart",BarButton.StartMoving)
  18. BarButton:SetScript("OnDragStop",BarButton.StopMovingOrSizing)
  19.  
  20. OptionsFrame = CreateFrame("Frame","ZBarOptionsFrame",UIParent)
  21. OptionsFrame:SetPoint("CENTER", 0, 0)
  22. OptionsFrame:SetHeight(300)
  23. OptionsFrame:SetWidth(300)
  24. OptionsFrame:SetBackdrop(StaticPopup1:GetBackdrop())
  25. OptionsFrame:Hide()
  26.  
  27. OptionsFrame.text = ZBarOptionFrame:CreateFontString(nil, "BACKGROUND", "GameFontNormal")
  28. OptionsFrame.text:SetAllPoints()
  29. OptionsFrame.text:SetText("ZBar Options Verison 1.0")

Notes on changes:
  • The OnClick script has been moved to the main button. The dynamic button was removed.
  • Locals were used instead of globals to optimize code.
  • A meaningful name was given to the options frame.
    (There was a time when it was suggested main windows be named as to easily identify which addon created them.
    It's hard to identify what addon owns "table: 238983DF".)
  • OptionsFrame:ClearAllPoints() does nothing for a frame you just created as it has no points set.
  • OptionsFrame:Hide() doesn't take any arguments.


All times are GMT -6. The time now is 11:50 PM.

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