From GMod Wiki
Derma Control: RadioButton |
Parent Class: | Panel |
Subclasses: | None |
Description: |
A radio button. |
Description | A crude wrapper over the default VGUI RadioButton control. |
Used on | |
Code | local LuaRadioButton = {}
function LuaRadioButton:Init()
self.selected = false
self.group = {}
end
function LuaRadioButton:OnMousePressed()
if not self.selected then
-- Run our DoClick method if it exists
if self.DoClick != nil then self.DoClick() end
self:SetSelected(true)
end
end
function LuaRadioButton:SetSelected(doSelect)
if doSelect then
for i, button in ipairs(self.group) do
button:SetSelected(false)
end
self.selected = true
-- Cause the VGUI to select this button and deselect the others
self:PostMessage("PressButton", "f", 0)
else
self.selected = false
end
end
function LuaRadioButton:GetSelected()
return self.selected
end
function LuaRadioButton:SetGroup(group)
self.group = group
end
vgui.Register("LuaRadioButton", LuaRadioButton, "RadioButton")
|
Output | N/A |
Usage
Gives the user a simple choice between two options.
Description | Creates two radio buttons, using the simple wrapper from above. |
Used on | |
Code |
local frame = vgui.Create("Frame")
frame.Button1 = vgui.Create("LuaRadioButton", frame)
frame.Button1:SetPos(50,50)
frame.Button1:SetText("magic")
frame.Button1:SizeToContents()
frame.Button2 = vgui.Create("LuaRadioButton", frame)
frame.Button2:SetPos(100,50)
frame.Button2:SetText("more magic")
frame.Button2:SizeToContents()
-- Set the group for the buttons, so they know to deselect the others in the group when clicked
local group = {frame.Button1, frame.Button2}
frame.Button1:SetGroup(group)
frame.Button2:SetGroup(group)
-- Select the "more magic" option by default
frame.Button2:SetSelected(true)
-- Demonstration of checking whether button is selected
if frame.Button1:GetSelected() then
print("Button 1 is selected.")
else
print("Button 1 is not selected.")
end
if frame.Button2:GetSelected() then
print("Button 2 is selected.")
else
print("Button 2 is not selected.")
end
-- Set an additional function to run when the button is clicked
frame.Button1.DoClick = function() print("magic!") end
frame.Button2.DoClick = function() print("more magic!") end
frame:SetSize( ScrW()*0.25, ScrH()*0.25 )
frame:Center()
frame:SetTitle( "RadioButton Example" )
frame:ShowCloseButton( true )
frame:SetVisible( true )
frame:MakePopup()
|
Output | "Button 1 is not selected. Button 2 is selected." is output to the console.
Additional text will be output each time the selection changes. |
Methods
P.RadioButton:SetValue
P.RadioButton:GetValue
P.RadioButton:SetText
P.RadioButton:GetText
Additional Notes
- Garry's Mod does not include the functionality to use these controls directly. The following is a crude but effective wrapper that registers a usable radio button class.