Assuming your GUI elements are simple shapes like rectangles, detecting if your mouse is on the element becomes fairly trivial. Here's the pseudo code:
function IsCursorInRectangle[mouse,rectangle]
Is mouse.x > rectangle.x
is mouse.x < rectangle.x + rectangle.width
is mouse.y > rectangle.y
is mouse.y >< rectangle.y + rectangle.height
return true;
return false;
This is a simple example of how to check it in C.
#include <stdio.h>
struct Coordinate
{
int m_nPositionX;
int m_nPositionY;
};
struct GUIElement
{
struct Coordinate m_csPosition;
int m_nRecWidth;
int m_nRecHeight;
};
int IsCursorOnElement(struct GUIElement* pgElement, struct Coordinate* pcCursor)
{
if(pcCursor->m_nPositionX > pgElement->m_csPosition.m_nPositionX &&
pcCursor->m_nPositionX < pgElement->m_csPosition.m_nPositionX + pgElement->m_nRecWidth &&
pcCursor->m_nPositionY > pgElement->m_csPosition.m_nPositionY &&
pcCursor->m_nPositionY < pgElement->m_csPosition.m_nPositionY + pgElement->m_nRecHeight)
{
return 1;
}
return 0;
}
int main(void)
{
struct GUIElement gsButton = {{100,100},50,50};
struct Coordinate csCursor = {105,120};
if(IsCursorOnElement(&gsButton, &csCursor))
{
puts("We're inside the element");
}
else
{
puts("We're outside the element");
}
return 0;
}
If you want to do other shapes like circles, you would need the center x,y of the circle + it's radius.
Here's a resource you can check out . https://www.jeffreythompson.org/collision-detection/point-rect.php