Lua Controlled Escape Menus

Made this a while ago, decided to fix it up and release it. Here you go:

[lua]//Lua controlled escape menus
//By PortalGod.

ESC = {}

local MenuParent
local CallFromMenu = false
local HasOpened = false
local ShouldUseMenu = true
local MenuOpen = false
local LMenuOpen = false
local Dialogs = {
disconnect = “disconnect”,
benchmark = “openbenchmarkdialog”,
changegame = “openchangegamedialog”,
createmultiplayergame = “opencreatemultiplayergamedialog”,
custommaps = “opencustommapsdialog”,
friends = “openfriendsdialog”,
gamemenu = “opengamemenu”,
loadcommentary = “openloadcommentarydialog”,
loaddemo = “openloaddemodialog”,
loadgame = “openloadgamedialog”,
newgame = “opennewgamedialog”,
options = “openoptionsdialog”,
playerlist = “openplayerlistdialog”,
savegame = “opensavegamedialog”,
serverbrowser = “openserverbrowser”,
quit = “quit”,
quitnoconfirm = “quitnoconfirm”,
resumegame = “resumegame”
}

function ESC.Register(menu)
if(type(menu) == “Panel”) then
MenuParent = menu
else
error(“that’s not a panel you cockass”)
end
if(MenuParent:IsVisible()) then MenuParent:SetVisible(false) end
end

function ESC.Unregister(b)
if LMenuOpen then
LMenuOpen = false
MenuParent:SetVisible(false)
gui.EnableScreenClicker(false)
end
if b then
MenuParent:Close()
end
MenuParent = nil
end

function ESC.GetMenu()
return MenuParent
end

function ESC.MenuExists()
return not MenuParent == nil
end

function ESC.DisableMenu()
ShouldUseMenu = false
end

function ESC.EnableMenu()
ShouldUseMenu = true
end

function ESC.MenuEnabled()
return ShouldUseMenu and not MenuParent == nil
end

function ESC.IsMenuOpen()
return LMenuOpen
end

function ESC.IsDefaultMenuOpen()
return MenuOpen
end

function ESC.OpenDefaultMenu()
CallFromMenu = true
HasOpened = false
timer.Simple(0, RunConsoleCommand, “gameui_activate”)
end

function ESC.OpenConsole()
if not MenuOpen then
ESC.OpenDefaultMenu()
end
RunConsoleCommand(“showconsole”)
end

function ESC.RunCommand(dlg)
dlg = dlg:lower()
if not(Dialogs[dlg] or table.KeyFromValue(Dialogs, dlg)) then
Error(“pick a real command you blueberry”)
else
if(Dialogs[dlg]) then
RunConsoleCommand(“gamemenucommand”, Dialogs[dlg])
else
RunConsoleCommand(“gamemnuecommand”, dlg)
end
ESC.OpenDefaultMenu() //:frowning:
end
end

local function HUDPaint()
if(MenuParent and ShouldUseMenu) then
if(HasOpened and CallFromMenu) then
CallFromMenu = false
HasOpened = false
end
end
MenuOpen = false
if(LMenuOpen) then
return
end
end
hook.Add(“HUDPaint”, “_HP_lcem”, HUDPaint)

local Elements = {“CHudCrosshair”, “CHudBattery”, “CHudSuitPower”, “CHudAmmo”, “CHudSecondaryAmmo”, “CHudChat”, “CHudCloseCaption”, “CHudMessage”, “CHudWeaponSelection”, “CHudDamageIndicator”, “CHudZoom”}
local function HUDShouldDraw(thing)
if(table.HasValue(Elements, thing)) then
return not LMenuOpen
end
end
hook.Add(“HUDShouldDraw”, “_HSD_lcem”, HUDShouldDraw)

local function PostRenderVGUI()
if(MenuParent and ShouldUseMenu) then
if(MenuOpen) then
if(CallFromMenu) then
if not(HasOpened) then
HasOpened = true
end
else
RunConsoleCommand(“cancelselect”)
LMenuOpen = not LMenuOpen
MenuParent:SetVisible(LMenuOpen)
gui.EnableScreenClicker(LMenuOpen)
end
end
end
MenuOpen = true
end
hook.Add(“PostRenderVGUI”, “_PRVGUI_lcem”, PostRenderVGUI)

function PlayerBindPress(Player, Bind, Pressed)
if(LMenuOpen) then
if(string.find(Bind, “toggleconsole”)) then
ESC.OpenConsole()
end
if not(string.find(Bind, “cancelselect”)) then
return true
end
end
end

hook.Add(“PlayerBindPress”, “_PBP_lcem”, PlayerBindPress)[/lua]

Usage:


ESC.Register(panel menu)	-	hook your panel or w/e
ESC.Unregister(bool delete)	-	unhook your panel or w/e (bool is to delete)
ESC.GetMenu()			- 	returns the current escape menu replacement
ESC.MenuExists()		-	returns whether or not there's an active 
					replacement
ESC.DisableMenu()		-	disable any active replacement
ESC.EnableMenu()		-	enable a replacement set with ESC.Register
ESC.MenuEnabled()		-	returns whether the replacement is enabled
					(returns false if there is no replacement)
ESC.IsMenuOpen()		-	returns whether the replacement is open
ESC.IsDefaultMenuOpen()		-	returns whether the default menu is open
ESC.OpenDefaultMenu()		-	opens the default escape menu regardless 
					of whether there's a replacement or not
ESC.OpenConsole()		-	opens the developer console
ESC.RunCommand(str command)	- 	runs a command


list of commands to run:
	disconnect
	benchmark
	changegame
	createmultiplayergame
	custommaps
	friends
	gamemenu
	loadcommentary
	loaddemo
	loadgame
	newgame
	options
	playerlist
	savegame
	serverbrowser
	quit
	quitnoconfirm
	resumegame

Note that this is an extremely hacky way of doing this.

Example use:

http://puu.sh/crpX

^ all those buttons and whatnot are functional

Have fun you crazy kids.

Damn you, my stomach hurts now…

Good work, I remember not being able to figure out how to do this when I needed it.

bump

[editline]7th January 2012[/editline]

I’ll release this one as an example after I clean it up a bit

whoaaa it’s like rock band

Make a custom options dialogue :stuck_out_tongue: