forked from jinschoi/SphereBot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TouchscreenButton.cpp
78 lines (68 loc) · 1.94 KB
/
TouchscreenButton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
TouchscreenButton.cpp - Library for touchscreen button functions.
Created by Terence F. Golla, September 19, 2021
Released into the public domain.
*/
#include "TouchscreenButton.h"
// Constructor
TouchscreenButton::TouchscreenButton(unsigned int x, unsigned int y,
unsigned int width, unsigned int height, unsigned int tftWidth, unsigned int tftHeight,
unsigned int rotation, char *imageFilename,char *alternateImageFilename)
{
_x = x; // Horizontal offset.
_y = y; // Vertical offset.
_width = width;
_height = height;
_tftWidth = tftWidth;
_tftHeight = tftHeight;
_rotation = rotation;
_imageFilename = imageFilename;
_alternateImageFilename = alternateImageFilename;
}
void TouchscreenButton::Display(Adafruit_ImageReader &reader, Adafruit_SPITFT &tft)
{
ImageReturnCode stat = reader.drawBMP(_imageFilename, tft, _x, _y);
}
void TouchscreenButton::DisplayAlternate(Adafruit_ImageReader &reader, Adafruit_SPITFT &tft)
{
ImageReturnCode stat = reader.drawBMP(_alternateImageFilename, tft, _x, _y);
}
boolean TouchscreenButton::Touched(unsigned int x, unsigned int y)
{
// Adjust for the fact that the touch coordinates do not rotate like the image.
unsigned int adjustedX;
unsigned int adjustedY;
switch (_rotation)
{
case 1:
adjustedX = _tftWidth - y;
adjustedY = x;
break;
case 2:
adjustedX = x;
adjustedY = y;
break;
case 3:
adjustedX = y;
adjustedY = _tftHeight - x;
break;
case 4:
adjustedX = _tftWidth - y;
adjustedY = _tftHeight - x;
break;
}
#if DebugTouchscreenButton
Serial.print(adjustedX);
Serial.print("-");
Serial.println(adjustedY);
#endif
if (adjustedX < _x || adjustedX > (_x + _width - 1))
{
return false;
}
if (adjustedY < _y || adjustedY > (_y + _height - 1))
{
return false;
}
return true;
}