Sunday, 18 May 2008

TouchShield: Did you just touch my RECT?

It's a good question: Was a touch press inside a particular area?



Using a helper function,

pointInRect();

We now can do stuff if an area is touched.

But first, how do we make a rectangle? Let's introduce you to a new type on the TouchShield,

LCD_RECT button;

This creates a rectangle named button. It is initialized this way,

button.left = 0; button.top = 0;

button.right = 80; button.bottom = 60;

Or if you're more hardcore and like shorthand,
LCD_RECT button = {0,0,80,60};

Either way you chose to create it, the rectangle is 80 wide by 60 tall, and starts at location (0,0).

Don't believe it's that easy and still skeptical? Then lets prove it by drawing it on the screen. To do this, we first need to open the Crayola box and grab a color,

COLOR blue = {0,0,255};


You can now draw it,

lcd_rect(button, blue, blue);


Now that we've got that covered, lets do something with our fancy pointInRect function.

As the function name tends to suggest, pointInRect needs a POINT and an LCD_RECT. We've got the LCD_RECT covered, now B.Y.O.P. (bring you're own POINT).

POINT p;


And call pointInRect like this,

if ( pointInRect(p, button) )
//button pressed, do something!


Let's put it all together and paint the button if it was touched. We'll do this by polling the touchscreen. Here is the (complete) code,

COLOR blue = {0,0,255};
LCD_RECT button = {0,0,80,60}; //initialized hardcore style
POINT p;

void setup() { ; }

void loop()
{

if ( touch_get_cursor(&p) )
{
if ( pointInRect(p, button) )
{
lcd_rect(button, blue, blue);
}
}

} //loop



See how simple yet powerful the pointInRect function can be?

0 comments:

Post a Comment