IsMouseInRing

From Multi Theft Auto: Wiki

This function checks if is the cursor is inside of a ring-/part. Best way to use it is with dxDrawCircle or circle / ring images.

Syntax

bool isMouseInRing ( float posX, float posY, float radius, float width, float startAngle, float stopAngle )

Required Arguments

  • posX: The center of the ring on the X-Axis.
  • posY: The center of the ring on the Y-Axis.
  • radius: The radius or the ring.

Optional Arguments

  • width: The width (thickness) of the ring you want to check. The radius means the center of the ring, so the width goes in and outside the ring.
  • startAngle: The degrees where you want to start the check.
  • stopAngle: The degrees where you want to stop the check.

Returns

Returns true if the mouse is inside the ring-/part, false otherwise.

Code

Click to collapse [-]
Client
function isMouseInRing(posX, posY, radius, width, startAngle, stopAngle)
    if isCursorShowing() then
	local SX, SY = guiGetScreenSize(); -- You can remove this line if you already got SX and SY for the screenSize
		
	if (not posX or not posY or not radius) then
	    outputDebugString("isMouseInRing: Required arguments are missing", 1);
	    return false
	end
		
	if not (width) then
	    width = SX / 50;
	end
		
	if not (startAngle) then
	    startAngle = 0;
	end
		
	if not (stopAngle) then
	    stopAngle = 359.99;
	end
		
        local cx, cy = getCursorPosition();
        local cx, cy = cx * SX, cy * SY;		
	local iMouseRot = findRotation(posX, posY, cx, cy) + 90;
		
	if (iMouseRot > 360) then
	    iMouseRot = iMouseRot - 360;
	end
		
	local diffX = math.max(cx, posX) - math.min(cx, posX); -- Calculate the X-Axis difference between mouse and ring center
	local diffY = math.max(cy, posY) - math.min(cy, posY); -- Calculate the Y-Axis difference between mouse and ring center	
	local iMouseDistance = math.sqrt(diffX * diffX + diffY * diffY); -- Get the distance in pixels between mouse and ring center

	if (startAngle > stopAngle) then -- Exchange start- and stop angle if startAngle is bigger then the stopAngle
	    local temp = startAngle;
	    startAngle = stopAngle;
	    stopAngle = temp;
	end

	if (iMouseRot >= startAngle and iMouseRot <= stopAngle and iMouseDistance <= radius + width and iMouseDistance >= radius - width) then
	    return true -- The mouse is inside the ring-/part
	else
	    return false -- It's somewhere else
	end
    end
    outputDebugString("isMouseInRing: Cursor is not showing!", 1); -- Remove this line if you know your cursor shouldn't always be showing.
    return false -- Cursor is not showing
end

By: Ceeser


Example

This Example code will check if the mouse in the part of the dxDrawCircle ring and the ring will change its color if so.

Click to expand [+]
Client


Notes

The shown example needs way more resources then a simple dxDrawRectangle function. Using it too often may cause performance issues.

The cursor must be visible to use this function. See showCursor.

findRotation is required to get the rotation of the cursor towards to the center of the ring.