1 #include "MultitouchMouse.h"
2 #include "TouchControlsConfig.h"
3 #include <math.h>
4
5 #define TAP_SPEED 10
6 #define LONG_PRESS_SPEED 40
7
8 using namespace touchcontrols;
9
MultitouchMouse(std::string tag,RectF pos,std::string image_filename)10 MultitouchMouse::MultitouchMouse(std::string tag,RectF pos,std::string image_filename):
11 ControlSuper(TC_TYPE_MULTITOUCHMOUSE,tag,pos)
12 {
13 image = image_filename;
14 id = -1;
15 id2 = -1;
16 glLines = new GLLines(2);
17 hideGraphics = false;
18 updateSize();
19
20 };
21
setHideGraphics(bool v)22 void MultitouchMouse::setHideGraphics(bool v)
23 {
24 hideGraphics = v;
25 }
26
resetOutput()27 void MultitouchMouse::resetOutput(){
28 reset();
29 }
30
updateSize()31 void MultitouchMouse::updateSize()
32 {
33
34 //glRect.resize(controlPos.right - controlPos.left, controlPos.bottom - controlPos.top);
35 glRect.resize(0.1, 0.16);
36
37
38 glLines->vertices[0] = controlPos.left;
39 glLines->vertices[1] = -controlPos.top;
40 glLines->vertices[3] = controlPos.right;
41 glLines->vertices[4] = -controlPos.top;
42
43
44 glLines->vertices[6] = controlPos.right ;
45 glLines->vertices[7] = -controlPos.top;
46 glLines->vertices[9] = controlPos.right;
47 glLines->vertices[10] = -controlPos.bottom ;
48 }
49
distancePoints(PointF p1,PointF p2)50 float MultitouchMouse::distancePoints(PointF p1,PointF p2)
51 {
52 float dist = ((p1.x - p2.x) * (p1.x - p2.x)) + ((p1.y - p2.y) * (p1.y - p2.y));
53 dist = sqrt(dist);
54 return dist;
55 }
56
processPointer(int action,int pid,float x,float y)57 bool MultitouchMouse::processPointer(int action, int pid, float x, float y)
58 {
59 if (action == P_DOWN)
60 {
61 if (id == -1) //Only process if not active
62 {
63 if (controlPos.contains(x, y))
64 {
65 id = pid;
66
67 last.x = x;
68 last.y = y;
69 anchor.x = x;
70 anchor.y = y;
71
72 tapCounter = 0;
73
74 signal_action.emit(MULTITOUCHMOUSE_DOWN,last.x,last.y,0,0);
75 return true;
76 }
77 }
78 else //second finger down
79 {
80 if (controlPos.contains(x, y))
81 {
82 id2 = pid;
83 last2.x = x;
84 last2.y = y;
85 signal_action.emit(MULTITOUCHMOUSE_2_DOWN,last2.x,last2.y,0,0);
86 }
87 }
88 return false;
89 }
90 else if (action == P_UP)
91 {
92 if (id == pid)
93 {
94
95 //Simple check to see if finger moved very much
96 if ((tapCounter < TAP_SPEED) &&
97 (distancePoints(anchor,last) < 0.05))
98 {
99 signal_action.emit(MULTITOUCHMOUSE_TAP,last.x,last.y,0,0);
100 }
101 signal_action.emit(MULTITOUCHMOUSE_UP,last.x,last.y,0,0);
102 signal_action.emit(MULTITOUCHMOUSE_2_UP,last.x,last.y,0,0);
103 reset();
104 return true;
105 }
106 else if (id2 == pid)
107 {
108 id2 = -1;
109 signal_action.emit(MULTITOUCHMOUSE_2_UP,x,y,0,0);
110 return true;
111 }
112 return false;
113 }
114 else if(action == P_MOVE)
115 {
116 if ((pid == id) && (id2 == -1)) //One finger moving
117 {
118
119 float dx = last.x - x;
120 float dy = last.y - y;
121
122 last.x = x;
123 last.y = y;
124
125 signal_action.emit(MULTITOUCHMOUSE_MOVE,last.x,last.y,dx,dy);
126
127 return true;
128 }
129 else if (((pid == id) && (id2 != -1)) || (pid == id2)) //2 fingers down and one of them moving
130 {
131 float old_dist = distancePoints(last,last2);
132
133 if (pid == id)
134 {
135 last.x = x;
136 last.y = y;
137 }
138 else
139 {
140 last2.x = x;
141 last2.y = y;
142 }
143 float new_dist = distancePoints(last,last2);
144
145 float zoom = new_dist - old_dist;
146 signal_action.emit(MULTITOUCHMOUSE_ZOOM,zoom,0,0,0);
147
148 }
149 return false;
150 }
151 }
152
initGL()153 bool MultitouchMouse::initGL()
154 {
155 int x,y;
156 glTex = loadTextureFromPNG(image,x,y);
157 }
158
drawGL(bool editor)159 bool MultitouchMouse::drawGL(bool editor)
160 {
161 //drawLines(0,0,*glLines);
162
163 //drawRect(glTex,controlPos.left,controlPos.top,glRect);
164 if (!hideGraphics)
165 {
166 if (id != -1)
167 drawRect(glTex,last.x-glRect.width/2,last.y-glRect.height/2,glRect);
168 else
169 drawRect(glTex,controlPos.left+controlPos.width()/2-glRect.width/2,controlPos.top+controlPos.height()/2-glRect.height/2,glRect);
170
171 }
172
173 tapCounter++;
174
175 if ((id != -1) && (id2 == -1)) //One finger down
176 {
177 if ((tapCounter == LONG_PRESS_SPEED) &&
178 (distancePoints(anchor,last) < 0.08))
179 {
180 signal_action.emit(MULTITOUCHMOUSE_LONG_PRESS,last.x,last.y,0,0);
181 }
182 }
183 //LOGTOUCH("state = %d, counter = %d",doubleTapState,doubleTapCounter);
184 }
185
reset()186 void MultitouchMouse::reset()
187 {
188 id = -1;
189 id2 = -1;
190
191 //signal_action.emit(MULTITOUCHMOUSE_MOVE,fingerPos.x,fingerPos.y,valueRel.x,valueRel.y);
192
193 }
194 /*
195 void MultitouchMouse::calcNewValue()
196 {
197 float dx = last.x - fingerPos.x;
198 float dy = last.y - fingerPos.y;
199 valueRel.x = dx;
200 valueRel.y = dy;
201 last.x = fingerPos.x;
202 last.y = fingerPos.y;
203
204
205 dx = anchor.x - fingerPos.x;
206 dy = anchor.y - fingerPos.y;
207
208
209 doUpdate();
210
211 }
212
213 void MultitouchMouse::doUpdate()
214 {
215 //LOGTOUCH("xT = %f yT = %f,xJ = %f yJ = %f",valueTouch.x,valueTouch.y,valueJoy.x ,valueJoy.y);
216 signal_action.emit(MULTITOUCHMOUSE_MOVE,fingerPos.x,fingerPos.y,valueRel.x,valueRel.y);
217 }
218 */
219
saveXML(TiXmlDocument & doc)220 void MultitouchMouse::saveXML(TiXmlDocument &doc)
221 {
222 TiXmlElement * root = new TiXmlElement(tag);
223 doc.LinkEndChild( root );
224
225 ControlSuper::saveXML(*root);
226 }
227
loadXML(TiXmlDocument & doc)228 void MultitouchMouse::loadXML(TiXmlDocument &doc)
229 {
230 TiXmlHandle hDoc(&doc);
231 TiXmlElement* pElem=hDoc.FirstChild( tag ).Element();
232
233 if (!pElem) //Check exists, if not just leave as default
234 return;
235
236 ControlSuper::loadXML(*pElem);
237 }
238