Garry's Mod Wiki

Revision Difference

Panel:Command#510567

<function name="Command" parent="Panel" type="classfunc">⤶ <description>Sends an action command signal to the panel. The response is handled by <page>PANEL:ActionSignal</page>.</description>⤶ <realm>Client</realm>⤶ <args>⤶ <arg name="command" type="string">The command to send to the panel.</arg>⤶ </args>⤶ </function>⤶ ⤶ <example>⤶ <description>Defines a function which displays a dialog box that asks the player if they want to leave the server. Clicking the buttons fire panel commands that call the <page>PANEL:ActionSignal</page> which handles what action should be taken.</description>⤶ <code>⤶ function DisconnectDialog()⤶ ⤶ -- Remove existing dialog box⤶ if(DialogBox) then DialogBox:Remove() end⤶ ⤶ -- Font and message⤶ local font = "ChatFont"⤶ local msg = "Would you like to disconnect?"⤶ ⤶ -- Get the size of the text⤶ surface.SetFont(font)⤶ local msg_w, msg_h = surface.GetTextSize(msg)⤶ ⤶ -- Padding and button size⤶ local padding = 15⤶ local btn_w, btn_h = 35, 25⤶ ⤶ -- Calculate dialog box size⤶ local dialog_w = msg_w+(padding*2)⤶ local dialog_h = msg_h+(padding*3)+btn_h⤶ ⤶ -- Create the dialog box⤶ DialogBox = vgui.Create("DPanel")⤶ DialogBox:SetSize(dialog_w, dialog_h)⤶ DialogBox:Center()⤶ DialogBox:SetBackgroundColor(Color(64, 64, 92, 255))⤶ ⤶ -- Message⤶ local lbl = vgui.Create("DLabel", DialogBox)⤶ lbl:SetPos(padding, padding)⤶ lbl:SetSize(msg_w, msg_h)⤶ lbl:SetText(msg)⤶ lbl:SetFont(font)⤶ ⤶ -- Yes button⤶ local yes = vgui.Create("DButton", DialogBox)⤶ yes:SetPos((dialog_w/2)-btn_w-20, msg_h+padding*2)⤶ yes:SetSize(btn_w, btn_h)⤶ yes:SetText("Yes")⤶ yes:SetFont(font)⤶ yes.DoClick = function() DialogBox:Command("yes") end⤶ ⤶ -- No button⤶ local no = vgui.Create("DButton", DialogBox)⤶ no:SetPos((dialog_w/2)+20, msg_h+padding*2)⤶ no:SetSize(btn_w, btn_h)⤶ no:SetText("No")⤶ no:SetFont(font)⤶ no.DoClick = function() DialogBox:Command("no") end⤶ ⤶ -- Force mouse input⤶ DialogBox:MakePopup()⤶ ⤶ -- Hook fired when DialogBox:Command is used⤶ function DialogBox:ActionSignal(signalName, signalValue)⤶ ⤶ -- Thank the player and disconnect after 2 seconds⤶ if(signalName == "yes") then⤶ ⤶ chat.AddText(Color(192, 192, 224), "Thanks for playing "..LocalPlayer():Nick()..", come back soon!")⤶ ⤶ timer.Simple(2.0, function()⤶ RunConsoleCommand("disconnect")⤶ end)⤶ ⤶ self:Remove()⤶ ⤶ -- Remove the dialog box⤶ elseif(signalName == "no") then⤶ ⤶ self:Remove()⤶ ⤶ end⤶ ⤶ end⤶ ⤶ end⤶ </code>⤶ <output></output>⤶ ⤶ </example>