1 #include "Mouse.h"
2 #include "TouchControlsConfig.h"
3 
4 #define TAP_SPEED 10
5 using namespace touchcontrols;
6 
Mouse(std::string tag,RectF pos,std::string image_filename)7 Mouse::Mouse(std::string tag,RectF pos,std::string image_filename):
8 		ControlSuper(TC_TYPE_MOUSE,tag,pos)
9 {
10 	image = image_filename;
11 	id = -1;
12 	id2 = -1;
13 	glLines = new GLLines(2);
14 	hideGraphics = false;
15 	updateSize();
16 	glitchFix = 0;
17 };
18 
setHideGraphics(bool v)19 void Mouse::setHideGraphics(bool v)
20 {
21 	hideGraphics = v;
22 }
23 
resetOutput()24 void Mouse::resetOutput(){
25     reset();
26 }
27 
updateSize()28 void Mouse::updateSize()
29 {
30 
31 	//glRect.resize(controlPos.right - controlPos.left, controlPos.bottom - controlPos.top);
32 	glRect.resize(0.1, 0.16);
33 
34 
35 	glLines->vertices[0] = controlPos.left;
36 	glLines->vertices[1] = -controlPos.top;
37 	glLines->vertices[3] = controlPos.right;
38 	glLines->vertices[4] = -controlPos.top;
39 
40 
41 	glLines->vertices[6] =  controlPos.right ;
42 	glLines->vertices[7] =  -controlPos.top;
43 	glLines->vertices[9] =  controlPos.right;
44 	glLines->vertices[10] = -controlPos.bottom ;
45 }
46 
getMS()47 double Mouse::getMS()
48 {
49     struct timeval tv;
50     gettimeofday(&tv, NULL);
51     return  (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
52 }
processPointer(int action,int pid,float x,float y)53 bool Mouse::processPointer(int action, int pid, float x, float y)
54 {
55 	if (action == P_DOWN)
56 	{
57 		if (id == -1) //Only process if not active
58 		{
59 			if (controlPos.contains(x, y))
60 			{
61 				id = pid;
62 
63 				glitchFix = 2; //Fix jumpy initial move
64 
65 				last.x = x;
66 				last.y = y;
67 				anchor.x = x;
68 				anchor.y = y;
69 				fingerPos.x = x;
70 				fingerPos.y = y;
71 				tapCounter = 0;
72 
73 				signal_action.emit(TOUCHMOUSE_DOWN,fingerPos.x,fingerPos.y,0,0);
74 				return true;
75 			}
76 		}
77 		else //second finger down
78 		{
79 			if (controlPos.contains(x, y))
80 			{
81 				id2 = pid;
82 				signal_action.emit(TOUCHMOUSE_UP,fingerPos.x,fingerPos.y,0,0); //This also up?...
83 				signal_action.emit(TOUCHMOUSE_2_DOWN,fingerPos.x,fingerPos.y,0,0);
84 			}
85 		}
86 		return false;
87 	}
88 	else if (action == P_UP)
89 	{
90 		if (id == pid)
91 		{
92 
93 			//Simple check to see if finger moved very much
94 			if ((tapCounter < TAP_SPEED) &&
95 					(((abs(anchor.x - fingerPos.x) + abs(anchor.y - fingerPos.y))) < 0.05))
96 			{
97 				signal_action.emit(TOUCHMOUSE_TAP,fingerPos.x,fingerPos.y,0,0);
98 			}
99 			signal_action.emit(TOUCHMOUSE_UP,fingerPos.x,fingerPos.y,0,0);
100 			signal_action.emit(TOUCHMOUSE_2_UP,fingerPos.x,fingerPos.y,0,0);
101 			reset();
102 			return true;
103 		}
104 		return false;
105 	}
106 	else if(action == P_MOVE)
107 	{
108 		if (pid == id) //Finger already down
109 		{
110 
111 			if (glitchFix) //Need to wait untill the values have changed at least once, otherwise inital jump
112 			{
113 				//LOGTOUCH("glitchFix %d",glitchFix);
114 				if ((last.x != x) || (last.y != y))
115 				{
116 					last.x = x;
117 					last.y = y;
118 					anchor.x = x;
119 					anchor.y = y;
120 					fingerPos.x = x;
121 					fingerPos.y = y;
122 					glitchFix --;
123 				}
124 			}
125 
126 
127 			if (!glitchFix)
128 			{
129 				fingerPos.x = x;
130 				fingerPos.y = y;
131 				calcNewValue();
132 			}
133 			return true;
134 		}
135 		return false;
136 	}
137 }
138 
initGL()139 bool Mouse::initGL()
140 {
141 	int x,y;
142 	glTex = loadTextureFromPNG(image,x,y);
143 }
144 
drawGL(bool editor)145 bool Mouse::drawGL(bool editor)
146 {
147 	//drawLines(0,0,*glLines);
148 
149 	//drawRect(glTex,controlPos.left,controlPos.top,glRect);
150 	if (!hideGraphics)
151 	{
152 		if (id != -1)
153 			drawRect(glTex,fingerPos.x-glRect.width/2,fingerPos.y-glRect.height/2,glRect);
154 		else
155 			drawRect(glTex,controlPos.left+controlPos.width()/2-glRect.width/2,controlPos.top+controlPos.height()/2-glRect.height/2,glRect);
156 
157 	}
158 
159 	tapCounter++;
160 	//LOGTOUCH("state = %d, counter = %d",doubleTapState,doubleTapCounter);
161 }
162 
reset()163 void Mouse::reset()
164 {
165 	id = -1;
166 	id2 = -1;
167 	valueRel.x = 0;
168 	valueRel.y = 0;
169 
170 	doUpdate();
171 
172 }
173 
calcNewValue()174 void Mouse::calcNewValue()
175 {
176 	float dx = last.x - fingerPos.x;
177 	float dy = last.y - fingerPos.y;
178 	valueRel.x = dx;
179 	valueRel.y = dy;
180 	last.x =  fingerPos.x;
181 	last.y = fingerPos.y;
182 
183 
184 	dx = anchor.x - fingerPos.x;
185 	dy = anchor.y - fingerPos.y;
186 
187 
188 	doUpdate();
189 
190 }
191 
doUpdate()192 void Mouse::doUpdate()
193 {
194 	//LOGTOUCH("xT = %f yT = %f,xJ = %f yJ = %f",valueTouch.x,valueTouch.y,valueJoy.x ,valueJoy.y);
195 	signal_action.emit(TOUCHMOUSE_MOVE,fingerPos.x,fingerPos.y,valueRel.x,valueRel.y);
196 }
197 
saveXML(TiXmlDocument & doc)198 void Mouse::saveXML(TiXmlDocument &doc)
199 {
200 	TiXmlElement * root = new TiXmlElement(tag);
201 	doc.LinkEndChild( root );
202 
203 	ControlSuper::saveXML(*root);
204 }
205 
loadXML(TiXmlDocument & doc)206 void Mouse::loadXML(TiXmlDocument &doc)
207 {
208 	TiXmlHandle hDoc(&doc);
209 	TiXmlElement* pElem=hDoc.FirstChild( tag ).Element();
210 
211 	if (!pElem) //Check exists, if not just leave as default
212 		return;
213 
214 	ControlSuper::loadXML(*pElem);
215 }
216