Moving a Box with the Mouse

BlitzPlus Forums/BlitzPlus Beginners Area/Moving a Box with the Mouse

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Perhaps this is of some help to you.

Graphics 640,480,32,2

	; setup type to use for button control
	Type BOX
		Field X#, Y#, WIDTH, HEIGHT, IMAGE, DRAGGED
	End Type

	; create an image to use as a button
	temp = CreateImage( 128, 64 )
	SetBuffer ImageBuffer( temp )
	Color 200,200,200
	Rect 0,0,128,64
	Color 255,255,255
	Rect 0,0,128,4
	Rect 0,0,4,60
	Color 100,100,100
	Rect 124,4,4,60
	Rect 0,60,128,4
	Color 10,10,10
	Text 24,24,"TEST BUTTON"

	; create 5 buttons
	For i = 0 To 4
		box = newBox( Rnd( 100, 500 ), Rnd( 100, 400 ), temp )
	Next


SetBuffer BackBuffer()
While Not KeyHit(1)
	Cls

	drawBoxes()
	updateBoxes()

	Flip
Wend
End

Function newBox( X#, Y#, IMAGE )
	b.box = New box
	b\x# = X#
	b\y# = Y#
	b\width = ImageWidth( IMAGE )
	b\height = ImageHeight( IMAGE )
	b\image = IMAGE

	b\dragged = False
End Function

Function updateBoxes()
	MD = MouseDown( 1 )
	MH = MouseHit( 1 )
	MX = MouseX()
	MY = MouseY()
	MXS = MouseXSpeed()
	MYS = MouseYSpeed()
	If MD Then
		For b.box = Each box
			If MH And MX => b\x# And MX < (b\x#+b\width) And MY => b\y# And MY < b\y#+b\height Then b\dragged = True
			If b\dragged Then
				b\x# = b\x# + MXS
				b\y# = b\y# + MYS
			End If
		Next
	Else
		For b.box = Each box
			b\dragged = False
		Next
	End If
End Function

Function drawBoxes()
	For b.box = Each box
		DrawBlock b\image, b\x#, b\y#
	Next
End Function