Rect.Contains() and scaled GUI

Using Rect.Contains() doesn’t work inside of a rotated or scaled GUI.matrix. I’m trying to find a solution for this as I have custom buttons that use Rect.Contains() to detect mouseovers, but I also need to scale the GUI matrix.

I figured that I can use Eric5h5’s PolyContainsPoint script found on the wiki:
http://www.unifycommunity.com/wiki/index.php?title=PolyContainsPoint

But I need to figure out how to apply the GUI matrix to the rect and get the points that define the poly.

If I could figure out how to get the rotation and scale components from the matrix I could transform the rect, but my understanding of matrices is lacking. I’m still researching, but if anyone has any pointers that would be excellent.

I don’t know how I missed Matrix4x4.MultiplyPoint3x4 in the documentation, but that was what I needed. Here is the helper function I created. Now I can draw the buttons and detect the rollover by translating the rectangle into a Vector2[ ] transformed by the GUI.matrix and plugging that into PolyContainsPoint.

If there is a more efficient way that would be great, but this works for now.

/**
* Transforms a rect into a an array of points based on a transformation matrix
*/
static function transformRect(rect:Rect, matrix:Matrix4x4):Vector2[] {
	var points:Array = new Array();
	var point:Vector3;
	
	// Top left
	point = matrix.MultiplyPoint3x4(Vector3(rect.x, rect.y, 1));
	points.Push(new Vector2(point.x, point.y));
	
	// Top right
	point = matrix.MultiplyPoint3x4(Vector3(rect.x + rect.width, rect.y, 1));
	points.Push(new Vector2(point.x, point.y));
	
	// Bottom right
	point = matrix.MultiplyPoint3x4(Vector3(rect.x + rect.width, rect.y + rect.height, 1));
	points.Push(new Vector2(point.x, point.y));
	
	// Bottom left
	point = matrix.MultiplyPoint3x4(Vector3(rect.x, rect.y + rect.height, 1));
	points.Push(new Vector2(point.x, point.y));
	
	return points.ToBuiltin(Vector2);
}

nice, thanks for posting