Jump to content

How to make a widget dragable


Recommended Posts

Hello,

i just cant find some code where is this easily done.

There is the Simple Health Bar DST mod which has a dragable heart but the code is way too complex for me.

I just want to make a regular Widget dragable with the right mouse button.

Does someone have experience with that?

Thanks

Link to comment
Share on other sites

Hey all,

i figured it out myself.

This is the code to move a widget by draging it with right mouse.

The only thing is that you have to stop dragging while the mouse is over your widget to stop the dragging.
But I think that can be fixed.
Just wanted to share the code if someone has the same problem
 

	self.mainbutton = self:AddChild(Widget("mainbutton"))
	--set some initial positions

	local dragging = false
	self.mainbutton.OnMouseButton = function(inst, button, down, x, y)
		print(self, button, down, x, y)
		if button == 1001 then
			if down then
				dragging = true
				local mousepos = TheInput:GetScreenPosition()
				self.dragPosDiff = self.mainbutton:GetPosition() - mousepos
			else
				dragging = false
			end
		end	
	end
	
	self.followhandler = TheInput:AddMoveHandler(function(x,y)
		if dragging then
			local pos
			if type(x) == "number" then
				pos = Vector3(x, y, 1)
			else
				pos = x
			end
			self.mainbutton:SetPosition(pos + self.dragPosDiff)
		end
	end)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...