Tscope5
mouse02.c
////////////////////////////////////////////////////////////////////////////////
//
// __ ______
// / /_______________ ____ ___ / ____/
// / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
// / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
// \__/____/\___/\____/ .___/\___/ /_____/
// /_/
//
// mouse02.c
// - Draw a number of mouse buttons on the screen.
// - Wait until one of the buttons is pressed.
////////////////////////////////////////////////////////////////////////////////
#include <tscope5.h>
#include <math.h>
// coordinates of the mouse buttons
#define NBUTTONS 12
struct {
double x, x1, x2;
double y, y1, y2;
} button[NBUTTONS];
int main()
{
// we need to install a display first
ts5_set_display_size(600.0, 600.0);
ts5_set_coordinate_scale(TS5_RELATIVE_COORDINATES);
// compute the coordinates of the mouse buttons
int i;
for (i=0; i<NBUTTONS; i++) {
double angle;
angle = i * 2 * TS5_PI / NBUTTONS;
button[i].x = cos(angle) * 0.8;
button[i].y = sin(angle) * 0.8;
button[i].x1 = button[i].x - 0.1;
button[i].x2 = button[i].x + 0.1;
button[i].y1 = button[i].y - 0.1;
button[i].y2 = button[i].y + 0.1;
}
// draw the buttons
for (i=0; i<NBUTTONS; i++) {
ts5_draw_rectangle(button[i].x1, button[i].y1, button[i].x2, button[i].y2);
ts5_printf(button[i].x, button[i].y, "%d", i+1);
}
// define the mouse as a response button
// check whether the mouse cursor
// is within one of the button's area
// and whether the button is pressed
int r;
double x, y;
int button_pressed = 0;
do {
r = ts5_check_response(NULL, NULL);
if (r) {
for (i=0; i<NBUTTONS; i++) {
if (x>button[i].x1 && x<button[i].x2 && y>button[i].y1 && y<button[i].y2) {
button_pressed = i+1;
}
}
}
} while (!button_pressed);
// print some info on the screen
ts5_printf(0.0, 0.5, "%d", button_pressed);
// wait for a click
return 0;
}