DButton-OnHold?

I have a button that needs to repeat a certain function only if it is currently getting held down by left-clicking.

Assuming buttonexample is a DButton, the example here:



buttonexample.DoClick = function()
	print("BUTTON WAS CLICKED")
end


Only works once-when you left-click the button. I’d like it to KEEP repeating until the button is released, but only have the repeating of this function run if the button was left-clicked, not right clicked… How would I do this?

What I’m doing now-



buttonexample.Paint = function( self, w, h )
//Painting code snipped
if self:IsDown() then
         print("BUTTON WAS CLICKED")
end


Works fine, but even if you RIGHT-CLICK the button and hold. I don’t want to completely disable right-clicking the button, I just want right-clicking and holding to print something else (with my actual code, it performs a different function).

[lua]
buttonexample.OnMousePressed = function(button,m)
if(m==MOUSE_LEFT) then
button.IsLDown = true
end
if(m==MOUSE_RIGHT) then
button.IsRDown = true
end
end

buttonexample.OnMouseReleased = function(button,m)
if(m==MOUSE_LEFT) then
button.IsLDown = false
end
if(m==MOUSE_RIGHT) then
button.IsRDown = false
end
end

buttonexample.Paint = function(self,w,h)
if(self.IsLDown) then
print(‘button was left clicked’)
end

if(self.IsRDown) then
	print('button was right clicked')
end

end

[/lua]

That’s the only way to do it as far as I know

Or you could make it a function so you don’t have to do that for every single button. (Above the paint)

Here’s what I like to do:

[LUA]
// Create an easy button
local function DermaBut(text,methodDown, methodUp)

local but = vgui.Create("DButton")
but:SetText("")

local col_cold, col_hot = Color(0, 0, 0, 0), Color(255, 90, 90, 100)
local rect_col, text_col = color_white, color_white

but.Paint = function(self, w, h)
	surface.SetDrawColor(self:IsHovered() and col_hot or col_cold)
	surface.DrawRect(0, 0, w, h)
		
	surface.SetDrawColor(rect_col)
	surface.DrawOutlinedRect(0, 0, w, h)

	draw.SimpleTextOutlined(text, "DermaDefault", w/2, h/2, text_col, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, color_black)
end

but.OnMousePressed = function()
	surface.PlaySound("UI/buttonclick.wav")
	methodDown()
end
but.OnMouseReleased = function()

	surface.PlaySound("UI/buttonclick.wav")
	methodUp()
end

return but

end

local function Menu()

local frame = vgui.Create("DFrame")
frame:SetSize(150, 75)
frame:Center()
frame:SetTitle("Button shit")
frame:MakePopup()

local button = DermaBut("Our awesome button", 
	function() print("Our mouse is down") end, // What to do when our mouse is down
	function() print("Our mouse is NOT down") end // What to do when our mouse is up
)
button:SetParent(frame)
button:SetSize(100,35)
button:SetPos(frame:GetWide()/2-button:GetWide()/2,45)

end
[/LUA]

And I’m just sitting here doing stupid stuff like this:



button.Paint = function( s, w, h )
	local color = MyColors.Default
	local offset = 0

	if input.IsMouseDown( MOUSE_LEFT ) then
		local mx, my = gui.MousePos()
		local sx, sy = s:ScreenToLocal( mx, my )
		if sx > 0 and sx < w and sy > 0 and sy < h then
			color = MyColors.ActivelyPressed
			offset = 1
		end
	elseif s:IsHovered() then
		color = MyColors.Hovered
	end
	
	-- Draw logic here
	-- Offset the box by +offset and draw with color
end


Heh.

What are you trying to accomplish with that? Lol

Use http://wiki.garrysmod.com/page/DButton/IsDown instead of that god-awful mess.



button.Paint = function(self, w, h)
	local color = MyColors.Default
	local offset = 0

	if self:IsDown() then
		color = MyColors.ActivelyPressed
		offset = 1
	elseif self:IsHovered() then
		color = MyColors.Hovered
	end	

	-- do your draw.RoundedBox here or whatever you're intending to, use color and offset as they're calculated for you.
end


Did you read any of the original post at all?

This might work, not able to test it right now. It might be better to track the button’s held down state as a boolean and then check that boolean in the paint method, rather than relying on a timer.


-- When creating the button, make a timer for it...
timer.Create("buttonTimer", 0, 0, function()
	print("Button is being held down with left mouse button")
end)

button.OnMousePressed = function(btn, m)
	if (m == MOUSE_LEFT) then
		Timer.start("buttonTimer")
	end	
end

button.OnMouseReleased = function(btn, m)
	if (m == MOUSE_LEFT) then
		Timer.start("buttonTimer")
	end
end

Huh. I never heard of Button.OnMousePressed and Button.OnMouseReleased. Thanks everyone!