GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2018-06-01 10:04:22

Tsa05
Member
Registered: 2016-11-16
Posts: 13

draw_rectarray() Make rectangles, get mouse

Simple script for things like menus. You create an array with coordinates of rectangles; the script draws rectangles, then tells you which one the mouse is colliding with (last collision returned in multiple case).

Example:

Expanddraw_rectarray([0,0,100,50, 0,50,100,100, 0,100,100,150], 0)

Draws 3 filled rectangles.
If the mouse is within (0,0) to (100,50), the script returns 0
If the mouse is within (0,50) to (100,100), the script returns 1
If the mouse is within (0,100) to (100,150), the script returns 2

Expand///	@arg	{real}	array
///	@arg	{bool}	outline
///	@returns	The index of the rectangle currently intersected by mouse or -1
/// GMLscripts.com/license

/*
*	Part of SLIDGE Engine
*	Redistribution is permitted
*	Submitting to gmlscripts
*/
/*
*	Draws a rectangle for each set of coordinates in an array
*	Returns an array containing the # of each box that the mouse hit
*/
var A = argument0;
var O = argument1;
var retval = -1;
for(var i=0;i<array_length_1d(A); i+=4){
	draw_rectangle(A[i+0], A[i+1], A[i+2], A[i+3], O);
	if(point_in_rectangle(mouse_x, mouse_y, A[i+0], A[i+1], A[i+2], A[i+3])){
		retval[array_length_1d(retval)] = i div 4;
	}
}
return retval;

Last edited by Tsa05 (2018-06-24 19:15:34)

Offline

#2 2018-06-01 12:17:28

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: draw_rectarray() Make rectangles, get mouse

That's a nice idea. But something weird is going on here:

Expandretval[array_length_1d(retval)] = i div 4;

This seems to return an array/list of colliding rectangles, though I'm not sure if array_length_1d(-1) is valid.

Should it not just be this?

Expandretval = i div 4;

Abusing forum power since 1986.

Offline

#3 2018-06-24 17:38:19

Tsa05
Member
Registered: 2016-11-16
Posts: 13

Re: draw_rectarray() Make rectangles, get mouse

xot wrote:

That's a nice idea. But something weird is going on here:

Expandretval[array_length_1d(retval)] = i div 4;

This seems to return an array/list of colliding rectangles, though I'm not sure if array_length_1d(-1) is valid.

Should it not just be this?

Expandretval = i div 4;

Good point! The code does work; array_length_1d of a non-array returns 0....but that's not what the script says it does!~~
Fixing description. Basically, the idea is that sometimes more than one overlapping rectangle is "hit"; this script actually returns a list of all "hit" boxes!

Last edited by Tsa05 (2018-06-24 19:14:35)

Offline

Board footer

Powered by FluxBB