SuperTuxKart
multitouch_device.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2013-2015 SuperTuxKart-Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (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 Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_MULTITOUCH_DEVICE_HPP
20 #define HEADER_MULTITOUCH_DEVICE_HPP
21 
22 #include <array>
23 #include <map>
24 #include <vector>
25 
26 #include "input/input_device.hpp"
27 #include "utils/types.hpp"
28 #include "IEventReceiver.h"
29 
30 #define NUMBER_OF_MULTI_TOUCHES 10
31 
32 enum MultitouchButtonType
33 {
34  BUTTON_STEERING,
35  BUTTON_UP_DOWN,
36  BUTTON_FIRE,
37  BUTTON_NITRO,
38  BUTTON_SKIDDING,
39  BUTTON_LOOK_BACKWARDS,
40  BUTTON_RESCUE,
41  BUTTON_ESCAPE,
42  BUTTON_UP,
43  BUTTON_DOWN,
44  BUTTON_LEFT,
45  BUTTON_RIGHT,
46  BUTTON_CUSTOM
47 };
48 
50 {
51  int id;
52  bool touched;
53  int x;
54  int y;
55 };
56 
58 {
59  MultitouchButtonType type;
60  PlayerAction action;
61  bool pressed;
62  unsigned int event_id;
63  int x;
64  int y;
65  int width;
66  int height;
67  float axis_x;
68  float axis_y;
69  unsigned int id;
70  void (*callback)(unsigned int, bool);
71 };
72 
73 class Controller;
74 
76 {
77 private:
79  std::vector<MultitouchButton*> m_buttons;
80 
81  Controller* m_controller;
82 
85  float m_deadzone;
86 
89 
92 
93  float m_orientation;
94  uint64_t m_gyro_time;
95 
97  IrrlichtDevice* m_irrlicht_device;
98 
99  float getSteeringFactor(float value, float sensitivity);
100  void handleControls(MultitouchButton* button);
101  bool isGameRunning();
102 
103  std::map<unsigned, std::vector<MultitouchButton*> > m_affected_linked_buttons;
104 
105 public:
107  std::array<MultitouchEvent, NUMBER_OF_MULTI_TOUCHES> m_events;
108 
110  virtual ~MultitouchDevice();
111 
113  bool processAndMapInput(Input::InputType type, const int id,
114  InputManager::InputDriverMode mode,
115  PlayerAction *action, int* value = NULL)
116  {return true;}
117 
118  unsigned int getActiveTouchesCount();
119 
120  void addButton(MultitouchButtonType type, int x, int y, int width,
121  int height, void (*callback)(unsigned int, bool) = NULL);
122  void clearButtons();
123  void reset();
124 
126  unsigned int getButtonsCount() { return (unsigned int)m_buttons.size();}
127 
129  MultitouchButton* getButton(unsigned int i) {return m_buttons.at(i);}
130 
131  void activateAccelerometer();
133  bool isAccelerometerActive();
134 
135  void activateGyroscope();
136  void deactivateGyroscope();
137  bool isGyroscopeActive();
138 
139  void updateAxisX(float value);
140  void updateAxisY(float value);
141  float getOrientation();
142  void updateOrientationFromAccelerometer(float x, float y);
143  void updateOrientationFromGyroscope(float z);
144  void updateDeviceState(unsigned int event_id);
145  void updateController();
146  void updateConfigParams();
147 
148 }; // MultitouchDevice
149 
150 #endif
This is the base class for kart controller - that can be a player or a a robot.
Definition: controller.hpp:46
base class for input devices
Definition: input_device.hpp:48
Definition: multitouch_device.hpp:76
bool isAccelerometerActive()
Get accelerometer state.
Definition: multitouch_device.cpp:208
MultitouchDevice()
The multitouch device constructor.
Definition: multitouch_device.cpp:35
float m_deadzone
The parameter that is used for steering button and determines dead area in a center of button.
Definition: multitouch_device.hpp:85
bool processAndMapInput(Input::InputType type, const int id, InputManager::InputDriverMode mode, PlayerAction *action, int *value=NULL)
Unused function.
Definition: multitouch_device.hpp:113
virtual ~MultitouchDevice()
The multitouch device destructor.
Definition: multitouch_device.cpp:51
void deactivateGyroscope()
Deativates gyroscope.
Definition: multitouch_device.cpp:229
std::vector< MultitouchButton * > m_buttons
The list of pointers to all created buttons.
Definition: multitouch_device.hpp:79
void activateGyroscope()
Activates gyroscope.
Definition: multitouch_device.cpp:216
void clearButtons()
Deletes all previously created buttons.
Definition: multitouch_device.cpp:146
bool isGyroscopeActive()
Get gyroscope state.
Definition: multitouch_device.cpp:241
void updateConfigParams()
Updates config parameters i.e.
Definition: multitouch_device.cpp:422
float getOrientation()
Returns device orientation Z angle, in radians, where 0 is landscape orientation parallel to the floo...
Definition: multitouch_device.cpp:511
void updateDeviceState(unsigned int event_id)
The function that is executed when touch event occurs.
Definition: multitouch_device.cpp:251
float getSteeringFactor(float value, float sensitivity)
Helper function that returns a steering factor for steering button.
Definition: multitouch_device.cpp:439
void activateAccelerometer()
Activates accelerometer.
Definition: multitouch_device.cpp:185
MultitouchButton * getButton(unsigned int i)
Returns pointer to the selected button.
Definition: multitouch_device.hpp:129
void reset()
Sets all buttons and events to default state.
Definition: multitouch_device.cpp:159
float m_sensitivity_y
A parameter in range that determines the sensitivity for y axis.
Definition: multitouch_device.hpp:91
void deactivateAccelerometer()
Deativates accelerometer.
Definition: multitouch_device.cpp:196
std::array< MultitouchEvent, NUMBER_OF_MULTI_TOUCHES > m_events
The array that contains data for all multitouch input events.
Definition: multitouch_device.hpp:107
void updateOrientationFromGyroscope(float z)
Update device orientation from the gyroscope measurements.
Definition: multitouch_device.cpp:568
void handleControls(MultitouchButton *button)
Sends proper action for player controller depending on the button type and state.
Definition: multitouch_device.cpp:609
void updateOrientationFromAccelerometer(float x, float y)
Update device orientation from the accelerometer measurements.
Definition: multitouch_device.cpp:523
unsigned int getButtonsCount()
Returns the number of created buttons.
Definition: multitouch_device.hpp:126
float m_sensitivity_x
A parameter in range that determines the sensitivity for x axis.
Definition: multitouch_device.hpp:88
void addButton(MultitouchButtonType type, int x, int y, int width, int height, void(*callback)(unsigned int, bool)=NULL)
Creates a button of specified type and position.
Definition: multitouch_device.cpp:84
unsigned int getActiveTouchesCount()
Returns a number of fingers that are currently in use.
Definition: multitouch_device.cpp:59
IrrlichtDevice * m_irrlicht_device
Pointer to the irrlicht device.
Definition: multitouch_device.hpp:97
PlayerAction
types of input events / what actions the players can do
Definition: input.hpp:117
Definition: multitouch_device.hpp:58
Definition: multitouch_device.hpp:50
Declares the general types that are used by the network.