GMWiki:Sandbox

From GMod Wiki

Jump to: navigation, search

Contents

Depreciated Functions

Don't call these.

Control Arguments Use Instead Notes
Header Labels This control no longer exists
TextBox String Label, String Command Panel:TextEntry(Label, Command)
Label String Text Panel:Help(Text)
CheckBox
Toggle
String Label, String Command Panel:CheckBox(label, command)
Slider String Label, String Command, String Type, Number min, Number max Panel:NumSlider(Label, Command, Min or 0, Max or 0, Type == "Float" and 2 or 0) See NumSlider's wiki page for more info.
MatSelect String Label, String ConVar, Number Height, Number ItemWidth, Number ItemHeight, Table Options Panel:MatSelect(ConVar, Options, AutoStretchToFullHeightAvailable, ItemWidth, ItemHeight) If AutoStretchToFullHeightAvailable is not set, it will default to 2 rows. Use SetNumRows on the MatSelect returned to change this if you wish to have more/less.
Button String Label, String Command Panel:Button(Label, Command, CommandArgumentOne, ...) AddControl uses LocalPlayer:ConCommand, Panel:Button uses RunConsoleCommand.

Shortcut Functions

These are for lazy people who don't want to create their derma themselves.

PropSelect

Control Arguments
PropSelect String Label, String ConVar, Number Height, Table Models
Table Structure Lua
 
Options = {
    Label = "My PropSelect";
    ConVar = "my_propselect";
    Height = 2;
    Models = {
        ["path/to/model.mdl"] = {
            cvar_name  = "value to set";
            cvar2_name = "Further Values";
        }
    };
};
 
 
if ( string.lower(control) == "propselect" ) then
    local ctrl = vgui.Create( "PropSelect", self )
    ctrl:ControlValues( data ) // Yack.
    self:AddPanel( ctrl )
    return ctrl
end
 
Notes
If Height isn't set, it defaults to 2.

When a model is selected ConVar is set to the model path, after which every cvar in the table for that model is set to the value requested. If you do not want any cvars set, put false instead of a table.


RopeMaterial

Control Arguments
RopeMaterial String ConVar
Table Structure Lua
 
Options = {
    ConVar = "my_ropematerial";
};
 
if ( string.lower(control) == "ropematerial" ) then
    local ctrl = vgui.Create( "RopeMaterial", self )
    ctrl:SetConVar( data.convar )
    self:AddPanel( ctrl )
    return ctrl
end
 
Notes
This will resize to fill all available space.

When a material is selected ConVar is set to it.


NumPad

Control Arguments
NumPad String Label, String Command, String Label2, String Command2
Table Structure Lua
 
Options = {
    Label    = "Left Numpad";
    Command  = "left_numpad_cvar";
    Label2   = "Right Numpad";
    Command2 = "right_numpad_cvar";
};
 
 
if ( string.lower(control) == "numpad" ) then
    local ctrl = vgui.Create( "CtrlNumPad", self )
        ctrl:SetConVar1( data.command )
        ctrl:SetConVar2( data.command2 )
        ctrl:SetLabel1( data.label )
        ctrl:SetLabel2( data.label2 )
    self:AddPanel( ctrl )
    return ctrl
end
 
Notes
If Command2 is nil or false, the second numpad does not show up.


Color

Control Arguments
Color String Red, String Green, String Blue, String Alpha
Table Structure Lua
 
Options = {
    Red   = "my_color_r";
    Blue  = "my_color_b";
    Green = "my_color_g";
    Alpha = "my_color_a";
};
 
 
if ( string.lower(control) == "color" ) then
    local ctrl = vgui.Create( "CtrlColor", self )
        ctrl:SetConVarR( data.red )
        ctrl:SetConVarG( data.green )
        ctrl:SetConVarB( data.blue )
        ctrl:SetConVarA( data.alpha )
    self:AddPanel( ctrl )
    return ctrl
end
 
Notes
Red, Blue, Green and Alpha should all be cvars (or concommands)


MaterialGallery

Control Arguments
MaterialGallery String ConVar, Number Width, Number Height, Number Rows, Table Options
Table Structure Lua
 
Options = {
    ConVar = "my_materialgallery";
    Width = 32;
    Height = 32;
    Rows = 4;
    Options = {
        {
            Material = "my/matgal/one";
            Value    = "One Tooltip";
            my_matgal_cvar1 = 7;
            my_matgal_cvar2 = 5;
        };
        {
            Material = "my/matgal/two";
            Value    = "Two Tooltip";
            my_matgal_cvar1 = 5;
            my_matgal_cvar2 = 7;
        };
    };
};
 
 
if ( string.lower(control) == "materialgallery" ) then
    local ctrl = vgui.Create( "MatSelect", self )
    ctrl:SetItemWidth( data.width or 32 )
    ctrl:SetItemHeight( data.height or 32 )
    ctrl:SetNumRows( data.rows or 4 )
    ctrl:SetConVar( data.convar or nil )
    for name, tab in pairs( data.options ) do
        local mat = tab.material
        local value = tab.value
        tab.material = nil
        tab.value = nil
        ctrl:AddMaterialEx( name, mat, value, tab )
    end
    self:AddPanel( ctrl )
    return ctrl
end
 
Notes
This insane monstrocity is what makes the faceposer's quick choices gallery work.

It works pretty much just like the PropSelect, apart from that instead of putting the material as the key, you put it as a subkey.
Width and Height default to 32, Rows to 4. ConVar is not required, as long as you have cvars set in your Options.


*

Control Arguments
* *
Table Structure Lua
 
Options = {
    -- ???
};
 
 
local control = vgui.Create( control, self )
if ( control ) then
    if ( control.ControlValues ) then
        control:ControlValues( data )
    end
    self:AddPanel( control )
    return control
end
 
Notes
If you provide a valid VGUI element's name, it will create that element and attach it to itself.

If that VGUI element has a ControlValues function, it will call the function with any options you provide it.


Insane Functions

The last two functions are somewhat odd. Perhaps it would have been easier if the various permutations had different names but oh well.
Note that due to minor oversight, ListBox controls will not be returned by the function.

ComboBox (With MenuButton set to 1)

Control Arguments
ComboBox (With MenuButton set to 1) Number MenuButton, String Folder, Table Options, Table CVars
Table Structure Lua
 
Options = {
    MenuButton = 1;
    Folder     = "my_thing";
    Options    = {
        ["Default"] = {
            my_thing_a = "7";
            my_thing_b = "5";
        };
        ["Backwards"] = {
            my_thing_a = "5";
            my_thing_b = "7";
        };
    };
    CVars = {
        "my_thing_a";
        "my_thing_b";
    };
};
 
 
if ( string.lower(control) == "combobox" ) then
    if ( tostring(data.menubutton) == "1" ) then
        local ctrl = vgui.Create( "ControlPresets", self )
        ctrl:SetPreset( data.folder )
        if ( data.options ) then
            for k, v in pairs( data.options ) do
                if ( k != "id" ) then // Some txt file configs still have an `ID'. But these are redundant now.
                    ctrl:AddOption( k, v )
                end
            end
        end
        if ( data.cvars ) then
            for k, v in pairs( data.cvars ) do
                ctrl:AddConVar( v )
            end
        end
        self:AddPanel( ctrl )
        return ctrl
    end
end
 
Notes
Despite it's name, this combobox combo is exclusively for storing presets. I will attempt to explain its complexities.

Folder indicates the subfolder of settings/presets where you wish to have your presets stored.
Options is a table of built in presets you wish to always be available to the user. Usually this includes a Default option, unless you hate your users.
CVars is a table where the values (not the keys this time) are the CVars you want to save whenever the user creates a new preset. These really should be the same as what you used for the Options table, or you're going to have problems.

ComboBox (With MenuButton set to 0)

With MenuButton set to 0, the ComboBox option behaves exactly like a listbox.

ListBox (With Height defined)

Control Arguments
ListBox (With Height defined) Number Height, String Label, Table Options
Table Structure Lua
 
Options = {
    Height  = 5;
    Label   = "My MultiLine ListBox";
    Options = {
        ["Option One"] = {
            my_mline_listbox_a = "7";
            my_mline_listbox_b = "5";
        };
        ["Option Two"] = {
            my_mline_listbox_a = "5";
            my_mline_listbox_b = "7";
        };
    };
};
 
 
if ( string.lower(control) == "listbox" ) then
    if ( data.height ) then
        local ctrl = vgui.Create( "DListView" )
        ctrl:SetMultiSelect( false )
        self:AddPanel( ctrl )
        ctrl:AddColumn( data.label or "unknown" )
        if ( data.options ) then
            for k, v in pairs( data.options ) do
                v.id = nil // Some txt file configs still have an `ID'. But these are redundant now.
                local line = ctrl:AddLine( k )
                line.data = v
                // This is kind of broken because it only checks one convar
                // instead of all of them. But this is legacy. It will do for now.
                for k, v in pairs( line.data ) do
                    if ( GetConVarString( k ) == v ) then
                        line:SetSelected( true )
                    end
                end
            end
        end
        ctrl:SetTall( data.height )
        ctrl:SortByColumn( 1, false )
        function ctrl:OnRowSelected( LineID, Line )
            for k, v in pairs( Line.data ) do
                RunConsoleCommand( k, v )
            end
        end
    else
        -- snip
    end
    return ctrl
end
 
Notes
Label in fact sets the column header, rather than providing an actual label.

Also, this element does not behave as you would except it to based on how all the other elements behave in terms of autoselection:
It will only select elements if the first convar matches, instead of all of them. It will also not update its selection if the convars change, whereas the others will.
It also will not (by default) allow users to select more than one option on the list, however it will potentially set more than one line as selected when created.
It is advisable to create your own subclass of DListView to replicate this.


ListBox (Without Height defined)

Control Arguments
ListBox (Without Height defined) Table Options
Table Structure Lua
 
Options = {
    Options = {
        ["Option One"] = {
            my_mline_listbox_a = "7";
            my_mline_listbox_b = "5";
        };
        ["Option Two"] = {
            my_mline_listbox_a = "5";
            my_mline_listbox_b = "7";
        };
    };
};
 
 
if ( string.lower(control) == "listbox" ) then
    if ( data.height ) then
        -- Snip
    else
        local ctrl = vgui.Create( "CtrlListBox", self )
        if ( data.options ) then
            for k, v in pairs( data.options ) do
                v.id = nil // Some txt file configs still have an `ID'. But these are redundant now.
                ctrl:AddOption( k, v )
            end
        end
        self:AddPanel( ctrl )
    end
    return ctrl
end
 
Notes
Amusingly, at the bottom of all the sillyness, this is the absolute simplest one. A simple listbox.
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox