This example shows how to use the mouse input functions to detect if the user clicks on any of several rectangles.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#define WIDTH 100
#define HEIGHT 80
#define INTER 50
struct rectangle {
int l, t, r, b;
};
inline int inRectangle(int x, int y, struct rectangle r);
int main()
{
int i, scr, colors[5] = {MGS_CRED, MGS_CMAGENTA, MGS_CGREEN, MGS_CCYAN, MGS_CBLUE};
int x, y, selected = -1, pressed = 0;
struct rectangle rect[5];
exit(EXIT_FAILURE);
for(i = 0; i < 5; i++) {
rect[i].t = 180;
rect[i].b = 180 + HEIGHT;
rect[i].l = INTER + i * (INTER + WIDTH);
rect[i].r = rect[i].l + WIDTH;
}
puts("Press S to exit");
if (pressed) {
pressed = 0;
selected = -1;
}
else if (selected > -1)
if (!inRectangle(x, y, rect[selected]))
selected = -1;
}
pressed = 1;
for (i = 0; i < 5; i++)
if (inRectangle(x, y, rect[i])) {
selected = i;
break;
}
}
mgsPuts(scr, 37, 20,
"Click on any rectangle");
for(i = 0; i < 5; i++)
mgsRectangle(scr, rect[i].l, rect[i].t, rect[i].r, rect[i].b, (i == selected) ? colors[i] & 0x7F7F7F : colors[i]);
if (selected > -1) {
mgsSetFont(scr, 70, colors[selected], MGS_TRANSPARENT);
mgsPuts(scr, 130, 370,
"You clicked on the");
mgsPuts(scr, 90, 440,
"rectangle of this color");
mgsRectangle(scr, 10, 520, 790, 540, colors[selected] & 0x7F7F7F);
mgsRectangle(scr, 10, 350, 790, 370, colors[selected] & 0x7F7F7F);
}
}
exit(EXIT_SUCCESS);
}
inline int inRectangle(int x, int y, struct rectangle r)
{
return((x >= r.l) && (x <= r.r) && (y >= r.t) && (y <= r.b));
}