Jump to content

How to make a widget dragable

Recommended Posts


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?


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
				dragging = false
	self.followhandler = TheInput:AddMoveHandler(function(x,y)
		if dragging then
			local pos
			if type(x) == "number" then
				pos = Vector3(x, y, 1)
				pos = x
			self.mainbutton:SetPosition(pos + self.dragPosDiff)


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

  • Create New...