• Close VGUI Panel by Console Command Bind
    16 replies, posted
Hello, I have a VGUI Panel which closes all well and good using a console command which I have then binded to a key in-game. However, I needed to get DTextEntry's working correctly on this panel so I had to use ':MakePopup()' - which now makes the DTextEntry's get focus and work. Now, because I've done this; Garry's Mod isn't recognising the bind which opens/closes the menu; making the menu non-closable using the bind. (Note: The console command the bind is attached to still works if accessed via console) Here is an example of the problem: [URL="http://pastebin.com/0yhANMwr"]http://pastebin.com/0yhANMwr[/URL] $5 to whoever comes up with a working solution to this problem ;)
Code? Specifically the concommand part..
I've added the code. The issue is likely something I'm doing stupid. :3
[QUOTE=Handsome Matt;45491678]Uhh if the DTextEntry has focus it's not going to trigger the bind... Just make the DTextEntry close on enter pressed or something instead?[/QUOTE] Hmmm, do you mean for the DTextEntry being in the state in which it allows text to be entered. If so, it is not in this state when the bind is pressed.
[QUOTE=gmelon;45496772]Hmmm, do you mean for the DTextEntry being in the state in which it allows text to be entered. If so, it is not in this state when the bind is pressed.[/QUOTE] ^This is me on another account - long story.
Here's what I use when developing VGUI: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_utilities/concommand_clearvgui.lua[/url] bind x clearvgui then if there are issues because of a typo, or whatever, it'll close all vgui. Useful to ensure your vgui will properly create itself if it doesn't exist. Hopefully it helps.
Sadly this isn't what I'm looking for. I have all the code working for closing the panel; but I am unable to run it using a bind. I've updated the original post with a worked out example of the problem you can play about with :)
Your example works... The only thing I'd recommend changing is: [code]local function CreateMenu() local baseMenu = vgui.Create("VGUI_MainPanel") concommand.Add("VGUI_ShowMenu", function() if ( !ispanel( baseMenu ) ) then // Create it baseMenu = vgui.Create("VGUI_MainPanel") end baseMenu:Open() end) concommand.Add("VGUI_HideMenu", function() if ( !ispanel( baseMenu ) ) then return; end baseMenu:Close() end) concommand.Add("VGUI_ToggleMenu", function() if ( !ispanel( baseMenu ) then // Create it baseMenu = vgui.Create("VGUI_MainPanel") end if baseMenu:IsVisible() then baseMenu:Close() else baseMenu:Open() end end) end hook.Add( "InitPostEntity", "CreateMenu", CreateMenu)[/code] Along with removing un-needed spaces, and double-tabs. Your example doesn't work when it comes to closing while open. Use this for that: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/open_menu_key_tap_open_and_key_hold_release_close.lua.html[/url] For the open key, you can use PlayerBindPress, or have the user bind a key; I'd recommend using a pre-determined key instead. The way I do it in my gm is using TAB to open a "quick-menu" whereby from there they can change weapons or open other menus, if the user holds tab beyond the tap-time then when they release hold the window closes; if they tap the key then it stays open until they tap the key again. Basically use function PANEL:Think( ) and test for the close-key there.
[QUOTE=Acecool;45497264]Your example works... The only thing I'd recommend changing is: [code]local function CreateMenu() local baseMenu = vgui.Create("VGUI_MainPanel") concommand.Add("VGUI_ShowMenu", function() if ( !ispanel( baseMenu ) ) then // Create it baseMenu = vgui.Create("VGUI_MainPanel") end baseMenu:Open() end) concommand.Add("VGUI_HideMenu", function() if ( !ispanel( baseMenu ) ) then return; end baseMenu:Close() end) concommand.Add("VGUI_ToggleMenu", function() if ( !ispanel( baseMenu ) then // Create it baseMenu = vgui.Create("VGUI_MainPanel") end if baseMenu:IsVisible() then baseMenu:Close() else baseMenu:Open() end end) end hook.Add( "InitPostEntity", "CreateMenu", CreateMenu)[/code] Along with removing un-needed spaces, and double-tabs. Your example doesn't work when it comes to closing while open. Use this for that: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/open_menu_key_tap_open_and_key_hold_release_close.lua.html[/url] Basically use function PANEL:Think( ) and test for the key there.[/QUOTE] Hmmm, the formatting seemed to have messed up when I copied and pasted it :3 Also, the issue is still persistent for me. When I try to close the panel once open using the toggle bind, nothing happens - yet it closes when I press the exit button or type in the console command into the console manually.
Yeah, use PANEL:Think to test for the key ( the bind won't work there unfortunately ) like the example I showed. Use this example for that: [url]https://dl.dropboxusercontent.com/u/...close.lua.html[/url] For the open key, you can use PlayerBindPress, or have the user bind a key; I'd recommend using a pre-determined key instead. The way I do it in my gm is using TAB to open a "quick-menu" whereby from there they can change weapons or open other menus, if the user holds tab beyond the tap-time then when they release hold the window closes; if they tap the key then it stays open until they tap the key again. Basically use function PANEL:Think( ) and test for the close-key there. You can use this in your PANEL:Think to get the bound key: input.LookupBinding( "vgui_togglemenu" ) which for me is: F6 and test for that key ( LocalPlayer( ):KeyDown( "KEY_" .. bound_key ) ) in place of TAB. If no key is bound then alert the user or use a default key.. [editline]25th July 2014[/editline] Edit: Here is the solution: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_definitions/bind_key_names.lua[/url] [code]function PANEL:Init() self.__OpenedAt = CurTime( ); -- Add this to init and open end function PANEL:Open() self.__OpenedAt = CurTime( ); -- like this self:SetVisible(true) self:MakePopup() end function PANEL:Think() // Prevent closing instantly if ( CurTime( ) - self.__OpenedAt < 0.25 ) then return; end local _p = LocalPlayer( ); local _key = "KEY_" .. input.LookupBinding( "vgui_togglemenu" ); _key = ( _key == "KEY_" ) && "KEY_G" || _key; -- use default key if key isn't set... if ( input.IsKeyDown( BIND_KEY_NUMBERS[ _key ] ) ) then self:Close( ); end end[/code] If you want to prevent the window closing if you HOLD the button, look at the tutorial I posted regarding tab: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/open_menu_key_tap_open_and_key_hold_release_close.lua.html[/url]
Just need to add a simple clause, "unless the main window has focus don't allow it".
Actually you are right; I didn't think of OnKeyCodePressed. But, you're still creating a lookup table!
Thanks Handsome Matt, I changed your example a little bit and got it working :) If you send me a private message, I'll happily send you the $5 if you want?? And cheers Ace for the help too :D
Sorry, you need to Log In to post a reply to this thread.