Root/src/touchscreen.cpp

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include "touchscreen.h"
22
23#include <fcntl.h>
24#include <unistd.h>
25
26
27Touchscreen::Touchscreen() {
28    calibrated = false;
29    wasPressed = false;
30    _handled = false;
31    x = 0;
32    y = 0;
33    startX = 0;
34    startY = 0;
35    event.x = 0;
36    event.y = 0;
37    event.pressure = 0;
38    ts_fd = 0;
39}
40
41Touchscreen::~Touchscreen() {
42    if (ts_fd > 0) {
43        close(ts_fd);
44    }
45}
46
47void Touchscreen::calibrate() {
48    if (event.pressure == 0) {
49        calibX = ((event.x - 200) * 320 / 3750) / 4;
50        calibY = (((event.y - 200) * 240 / 3750)) / 4;
51        calibrated = true;
52    }
53}
54
55bool Touchscreen::poll() {
56    wasPressed = pressed();
57    SDL_PumpEvents();
58    int mx, my;
59    if (SDL_GetMouseState(&mx,&my) && SDL_BUTTON(1)) {
60        x = mx;
61        y = my;
62        event.pressure = 1;
63    } else {
64        event.pressure = 0;
65    }
66    _handled = false;
67
68    if (!wasPressed && pressed()) {
69        startX = x;
70        startY = y;
71    }
72
73    return pressed();
74}
75
76bool Touchscreen::handled() {
77    return _handled;
78}
79
80void Touchscreen::setHandled() {
81    wasPressed = false;
82    _handled = true;
83}
84
85bool Touchscreen::pressed() {
86    return !_handled && event.pressure > 0;
87}
88
89bool Touchscreen::released() {
90    return !pressed() && wasPressed;
91}
92
93bool Touchscreen::inRect(int ix, int iy, int iw, int ih) {
94    return !_handled &&
95        (y >= iy) && (y <= iy + ih) && (x >= ix) && (x <= ix + iw);
96}
97
98bool Touchscreen::inRect(SDL_Rect r) {
99    return inRect(r.x, r.y, r.w, r.h);
100}
101
102bool Touchscreen::startedInRect(int ix, int iy, int iw, int ih) {
103    return !_handled &&
104        (startY >= iy) && (startY <= iy + ih) &&
105        (startX >= ix) && (startX <= ix + iw);
106}
107
108bool Touchscreen::startedInRect(SDL_Rect r) {
109    return startedInRect(r.x, r.y, r.w, r.h);
110}
111

Archive Download this file



interactive