DButton.OnMousePressed

From GMod Wiki

Jump to: navigation, search
Function
Syntax DButton.OnMousePressed( (DButton) button )
Description:
If this is set to a function, it will be called when the button is pressed ( the main difference between this and DButton.DoClick is that the function is called before the button is released).
Returns: nil
In Object: DButton
Realm: NewerClient.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=DButton.OnMousePressed]DButton.OnMousePressed [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]



Examples

DescriptionWhen the button is pressed, the text in the button will change to "Pressed". Then when release, the text will change to "Not Pressed".
Used onNewerClient.png
Code
 
Frame = vgui.Create( "DFrame" )
	Frame:SetSize( 150, 75 )
	Frame:Center()
	Frame:SetTitle( "Title" )
	Frame:MakePopup()
 
button = vgui.Create( "DButton", Frame )
	button:SetSize( 100, 25 )
	button:SetPos( 25, 25 )
	button:SetText( "Press Me" )
	button.OnMousePressed = function()
		button:SetText( "Pressed" )
	end
 
	button.OnMouseReleased = function()
		button:SetText( "Not Pressed" )
	end
 
OutputN/A


DescriptionA Practical Application. When Pressed, The Button Becomes Green, Then Changes Red When Released.
Used onNewerClient.png
Code
 
Frame = vgui.Create("DFrame")  -- make a frame
	Frame:SetSize(150, 75)
	Frame:Center() -- places the frame in the center of your screen
	Frame:SetTitle( "Frame Title" ) 
	Frame:MakePopup() -- activates the cursor so you can click stuff
 
button = vgui.Create("DButton", Frame) -- create a DButton, parent it to Frame
	button.Colour = Color(100, 100, 100, 255) -- Assign The Button This Variable, So We Can Change The Colour Without Having A Complex Paint Function.
	button:SetText( "Press Me" ) -- initial text on the button
	button:SetPos(5, 25) -- position relative to the frames top left corner
	button:SetSize( 100, 25 ) -- set the buttons dimentions
 
	button.Paint = function() 
		surface.SetDrawColor(button.Colour) -- anything we draw will be the colour saved in button.Colour
		surface.DrawRect(0, 0, button:GetWide(), button:GetTall()) -- create a rectangle that covers the button
	end
 
	button.OnMousePressed = function() 
		button.Colour = Color(0, 200, 0, 255) -- give the variable this green colour, because the Paint function is updated every frame, the button will change to green
	end
 
	button.OnMouseReleased = function() 
		button.Colour = Color(200, 0, 0, 255) -- and when released, the button turns red
	end
 
 
OutputA Button That Turns Green While Clicked, And Red When Not
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox