-
Notifications
You must be signed in to change notification settings - Fork 1
Input and Sensors
By creating your game and extending gameplay::Game
, you'll be able to add all the required handlers of input events. Additionally, there are methods on gameplay::Game
to poll for the current sensor data. This architecture insulates you, as a developer, from the platform-specific details on handling keyboard, touch and mouse events, and from polling the accelerometer state. The following illustrates overridden methods to handle input events:
#include "gameplay.h"
using namespace gameplay;
class MyGame : public Game
{
// Input events
void keyEvent(Keyboard::KeyEvent evt, int key);
void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
bool mouseEvent(Mouse::MouseEvent evt, int x, int y);
void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad);
// Input methods
void displayKeyboard(bool display);
void registerGesture(Gesture::GestureEvent evt);
void getAccelerometerValues(float* pitch, float* roll);
// ...
};
Game::mouseEvent()
is called when a mouse event occurs.
Mouse events that are not consumed will be interpreted as a touch event. You can consume a mouse event by overridding Game::mouseEvent()
and returning true. This gives you the option to uniquely handle mouse events from touch events. Game::mouseEvent()
returns false by default.
Note that some mobile devices can use a Bluetooth mouse.
Game::setMouseCaptured()
can be used to enable mouse capture. While mouse capture is enabled, all mouse move events will then be delivered as deltas instead of absolute positions.
Game::setCursorVisible()
can be used to hide the mouse cursor.
Game::touchEvent()
is called when a touch event occurs. x
and y
are screen coordinates where the top left is 0,0
. Some platforms may allow you to touch outside the bounds of the screen so negative x
and y
values are possible.
You can enable multi-touch using Game::setMultiTouch()
. The Game::touchEvent()
parameter contactIndex
is used to differentiate the touch contacts. Do not assume that the contactIndex
values are sequential.
Game::keyEvent()
is called when a keyboard event occurs.
You can call Game::displayKeyboard()
to show or hide a virtual keyboard for platforms that support it.
Some platforms support gestures. Game::isGestureSupported()
can be used to determine which gestures are supported.
Game::registerGesture()
is used to register for a type of gesture. Once a gesture is registered, you will receive callbacks via Game::gestureSwipeEvent()
, Game::gesturePinchEvent()
or Game::gestureTapEvent()
.
You can call Game::getAccelerometerValues()
and pass in pointers to parameters that will be populated with the current sensor values for the accelerometer.
You can listen for gamepad events via Game::gamepadEvent(Gamepad* gamepad)
, this will return you a gameplay::Gamepad
instance that has been connected or disconected which you can manage yourself.
You can also use a game.config file to define a virtual gamepad from a UI form that would typically be used on mobile devices. The form contains joystick and button controls with data bindings to the appropriate button mappings defined in an enumeration in the Gamepad class.
Ex. game.config
gamepads
{
form = res/common/gamepad.form
}