1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_TOUCHINPUT
19 #define INCLUDED_TOUCHINPUT
20 
21 #include "lib/input.h"
22 #include "maths/Vector2D.h"
23 #include "maths/Vector3D.h"
24 
25 /**
26  * Maps touch events (e.g. on Android touchscreen devices) onto mouse events
27  * and camera movement.
28  */
29 class CTouchInput
30 {
31 public:
32 	CTouchInput();
33 	~CTouchInput();
34 
35 	/**
36 	 * Returns whether the touch input mode is enabled for this device.
37 	 */
38 	bool IsEnabled();
39 
40 	InReaction HandleEvent(const SDL_Event_* ev);
41 
42 	/**
43 	 * Should be called once per frame to perform updates.
44 	 */
45 	void Frame();
46 
47 private:
48 	void OnFingerDown(int id, int x, int y);
49 	void OnFingerUp(int id, int x, int y);
50 	void OnFingerMotion(int id, int x, int y);
51 
52 	// Mouse emulation state:
53 	enum
54 	{
55 		MOUSE_INACTIVE,
56 		MOUSE_ACTIVATING,
57 		MOUSE_ACTIVE_UP,
58 		MOUSE_ACTIVE_DOWN
59 	};
60 	static const size_t MAX_MOUSE = 2;
61 	int m_MouseEmulateState[MAX_MOUSE];
62 	CVector2D m_MouseEmulateDownPos[MAX_MOUSE];
63 
64 	// Current finger state:
65 	static const size_t MAX_FINGERS = 2;
66 	bool m_Down[MAX_FINGERS];
67 	CVector2D m_Pos[MAX_FINGERS];
68 	CVector2D m_Prev[MAX_FINGERS];
69 
70 
71 	enum
72 	{
73 		STATE_INACTIVE,
74 		STATE_FIRST_TOUCH,
75 		STATE_PANNING,
76 		STATE_ZOOMING
77 	};
78 	int m_State;
79 
80 	double m_FirstTouchTime;
81 	CVector2D m_FirstTouchPos;
82 
83 	CVector3D m_PanFocus;
84 	float m_PanDist;
85 };
86 
87 extern CTouchInput g_TouchInput;
88 
89 extern InReaction touch_input_handler(const SDL_Event_* ev);
90 
91 #endif // INCLUDED_TOUCHINPUT
92