cLib.DToggle
Modern iOS/Android style toggle switch.
Basic Usage
local toggle = vgui.Create("cLib.DToggle", parent)
toggle:SetValue(true)
toggle.OnValueChanged = function(self, value)
print("Toggle:", value)
endMethods
| Method | Description |
|---|---|
SetValue(bool) | Set toggle state |
GetValue() | Get toggle state |
Toggle() | Toggle state |
SetOnColor(Color) | Color when ON |
SetOffColor(Color) | Color when OFF |
SetKnobColor(Color) | Knob color |
SetShowLabels(bool) | Show ON/OFF text |
Customization
local toggle = vgui.Create("cLib.DToggle", parent)
toggle:SetValue(false)
-- Custom colors
toggle:SetOnColor(Color(80, 200, 120)) -- Green when ON
toggle:SetOffColor(Color(100, 100, 105)) -- Gray when OFF
toggle:SetKnobColor(Color(255, 255, 255))
-- Show ON/OFF labels
toggle:SetShowLabels(true)cLib.DToggleLabel
Toggle with attached label (like checkbox).
local toggle = vgui.Create("cLib.DToggleLabel", parent)
toggle:SetText("Dark Mode")
toggle:SetValue(GetSetting("darkMode", false))
toggle:Dock(TOP)
toggle:DockMargin(0, cLib.Scale(10), 0, 0)
toggle.OnValueChanged = function(self, value)
SetSetting("darkMode", value)
RefreshTheme()
endMethods
| Method | Description |
|---|---|
SetText(string) | Set label text |
SetValue(bool) | Set toggle state |
GetValue() | Get toggle state |
SetTextColor(Color) | Label color |
SetLabelPosition(string) | "left" or "right" |
Settings Example
function CreateModernSettings(parent)
local scroll = vgui.Create("cLib.DScrollPanel", parent)
scroll:Dock(FILL)
local function AddToggle(text, setting, default)
local toggle = vgui.Create("cLib.DToggleLabel", scroll)
toggle:SetText(text)
toggle:SetValue(GetConVar(setting):GetBool())
toggle:Dock(TOP)
toggle:DockMargin(cLib.Scale(15), cLib.Scale(10), cLib.Scale(15), 0)
toggle.OnValueChanged = function(s, v)
RunConsoleCommand(setting, v and "1" or "0")
end
return toggle
end
-- Add toggles
AddToggle("Enable HUD", "myaddon_hud", true)
AddToggle("Show Notifications", "myaddon_notif", true)
AddToggle("Play Sounds", "myaddon_sounds", true)
AddToggle("Auto-save", "myaddon_autosave", true)
AddToggle("Developer Mode", "myaddon_dev", false)
end