ControlsXPad360 Class
This class handles XBox 360 game pads input. Right now these game pads are supported only on Windows. It provides access to the following controller properties:
- Buttons represent digital buttons on the controller and can be either pressed or unpressed. Just in case with mouse/keyboard controls, they can be mapped to the specific controls states, to bind input events and programmed actions. You can either bind buttons in the code via setStateButton() or let the user assign the button for a control state via getStateEvent().
- D-pad is represented as four directional buttons (left, right, up and down). They are handled in the same way as other buttons.
- Thumbsticks are left and right analog directional sticks on the controller. They are queried via getLeftX() and getLeftY() for the left thumbstick and getRightX() and getRightY() for the right one. Thumbsticks also act as two digital buttons when pressed (i.e. the stick is "clicked" in). In this case, they are handled in the same way as other buttons.
- Triggers are two analog controls that can be queried for their states via getLeftTrigger() and getRightTrigger().
- Vibration motors allow to set vibration effects. The left motor is a low-frequency rumble motor, while the right motor is a high-frequency one. The amount of vibration for each of the motors can be set via setLeftMotor() and setRightMotor().
Input data from analog controls (thumbsticks and triggers) can be additionally filtered via setFilter() to provide smooth interpolation between frames and avoid jerks.
A controller should update the physical state of all its input controls (i.e. check whether the user has pressed the button, for example) each frame using updateEvents().
The code below shows how you can bind an action (i.e. a control state) to one of the controller buttons and also check for thumbstick movement to move the player accordingly.
ControlsXPad360 xpad; // controls for handling Xbox controller input
PlayerActor player;
int move_right() {
// The player moves to the right.
return 1;
}
int fire() {
// The player fires.
return 1;
}
int init() {
// Create Xbox controls with one controller.
xpad = new ControlsXPad360(1);
// Bind the fire action of the player to X button.
xpad.setStateButton(CONTROLS_STATE_FIRE, CONTROLS_XPAD360_BUTTON_X);
}
int update() {
// Update controller input (states of all buttons and axes).
xpad.updateEvents();
// Move to the right when the user pushes the left thumbstick
// to the right. Light tapping will be ignored.
if (xpad.getLeftX() > 0.5f) {
move_right();
}
if(xpad.getState(CONTROLS_STATE_FIRE)) {
fire();
}
}
ControlsXPad360 Class
Members
ControlsXPad360 ()
ControlsXPad360 (int num)
Constructor. Creates a new XBox 360 game pad.Arguments
- int num - Game pad number (up to four game pads are supported).
int getButton (int button)
Returns a button state (pressed or not pressed).Arguments
- int button - Button (one of the CONTROLS_XPAD360_BUTTON_* variables).
Return value
1 if the button is pressed; otherwise, 0.const char * getStateName (int state)
Returns the name of a given state.Arguments
- int state - State (one of the CONTROLS_STATE_* variables).
Return value
State name.int restoreState (const Ptr<Stream> & stream)
Arguments
- const Ptr<Stream> & stream
Return value
void setStateButton (int state, int button)
Sets a game pad button that switches a given state on and off.Arguments
- int state - State (one of the CONTROLS_STATE_* variables).
- int button - Button that switches the state (one of the CONTROLS_XPAD360_BUTTON_* variables).
float getLeftY ()
Returns a state value of the left thumbstick along the Y-axis. When a thumbstick is in the center position, this value is zero. Negative values indicate down; positive values indicate up.Return value
Value in range [-1; 1].float getLeftTrigger ()
Returns an axis state value (i.e. the position) of the left trigger. Zero means the trigger is not pressed; 1 means the trigger is pressed to the maximum.Return value
Value in range [0; 1].float getLeftX ()
Returns a state value of the left thumbstick along the X-axis. When a thumbstick is in the center position, this value is zero. Negative values indicate left; positive values indicate right.Return value
Value in range [-1; 1].float getRightX ()
Returns a state value of the right thumbstick along the X-axis. When a thumbstick is in the center position, this value is zero. Negative values indicate down; positive values indicate up.Return value
Value in range [-1; 1].int getStateButton (int state)
Returns a game pad button that switches a given state on and off.Arguments
- int state - State (one of the CONTROLS_STATE_* variables).
Return value
Button that switches the state (one of the CONTROLS_XPAD360_BUTTON_* variables):- BUTTON_A = 0
- BUTTON_B
- BUTTON_X
- BUTTON_Y
- BUTTON_BACK
- BUTTON_START
- BUTTON_DPAD_UP
- BUTTON_DPAD_DOWN
- BUTTON_DPAD_LEFT
- BUTTON_DPAD_RIGHT
- BUTTON_THUMB_LEFT
- BUTTON_THUMB_RIGHT
- BUTTON_SHOULDER_LEFT
- BUTTON_SHOULDER_RIGHT
int getNumber ()
Returns the game pad number (up to four game pads are supported).Return value
Game pad number.float getFilter ()
Returns a filter value used to correct the current state of the analog axis (thumbsticks and triggers) relative to the previous one:- Filter value of 0 means there is no interpolation and the current value is not corrected.
- Filter value of 1 means the previous state is used instead of the current one.
Return value
Filter value for interpolation between axis states.int isAvailable ()
Checks if the game pad is available.Return value
1 if the game pad is available; otherwise, 0.int isStateEvent ()
Returns a value indicating if a game pad button is successfully assigned to a state.Return value
1 if a button is already assigned; otherwise, 0.int clearButton (int button)
Returns a button state and clears it to 0 (not pressed).Arguments
- int button - Button (one of the CONTROLS_XPAD360_BUTTON_* variables).
Return value
1 if the button is pressed, and this function was not called previously in the current frame; otherwise, 0.void getStateEvent (int state)
Lets the user assign a game pad button to a given state.Arguments
- int state - State (one of the CONTROLS_STATE_* variables), to which a button is going to be assigned.
void setRightMotor (float speed)
Sets the amount of vibration for the right motor (a high-frequency motor).Arguments
- float speed - Speed of motor rotation in range [0; 1].
const char * getName ()
Returns the name of the game pad.Return value
One of the following:- GamePad
- Wheel
- Arcade Stick
- Flight Stick
- Dance Pad
- Guitar
- Drum Kit
- Unknown
int updateEvents ()
Scans a game pad (synchronizes states of game pad buttons and thumbsticks with the controller itself). Should be called each frame.Return value
0 if a game pad is not found or updateEvents() already has been called for the frame; otherwise 1.void setFilter (float filter)
Sets a filter value used to correct the current state of the analog axis relative to the previous one. Axis states are interpolated for thumbsticks and triggers.Arguments
- float filter - Filter value for interpolation between axis states. The provided value is clamped to a range [0;1].
- Filter value of 0 means there is no interpolation and the current value is not corrected.
- Filter value of 1 means the previous state is used instead of the current one.
float getRightTrigger ()
Returns an axis state value (i.e. the position) of the right trigger. Zero means the trigger is not pressed; 1 means the trigger is pressed to the maximum.Return value
Value in range [0; 1].void setLeftMotor (float speed)
Sets the amount of vibration for the left motor (a low-frequency motor).Arguments
- float speed - Speed of motor rotation in range [0; 1].
int saveState (const Ptr<Stream> & stream)
Arguments
- const Ptr<Stream> & stream
Return value
float getRightY ()
Returns a state value of the right thumbstick along the Y-axis. When a thumbstick is in the center position, this value is zero. Negative values indicate down; positive values indicate up.Return value
Value in range [-1; 1].int BUTTON_A
Description
Button A.int BUTTON_B
Description
Button B.int BUTTON_X
Description
Button X.int BUTTON_Y
Description
Button Y.int BUTTON_BACK
Description
Button "Back".int BUTTON_START
Description
Button "Start".int BUTTON_DPAD_UP
Description
Button "Up".int BUTTON_DPAD_DOWN
Description
Button "Down".int BUTTON_DPAD_LEFT
Description
Button "Left".int BUTTON_DPAD_RIGHT
Description
Button "Right".int BUTTON_THUMB_LEFT
Description
Left thumbstick button.int BUTTON_THUMB_RIGHT
Description
Right thumbstick button.int BUTTON_SHOULDER_LEFT
Description
Left shoulder (bumper) button.int BUTTON_SHOULDER_RIGHT
Description
Right shoulder (bumper) button.int NUM_BUTTONS
Description
Number of buttons on Xbox 360 game pad.Last update: 03.07.2017
Помогите сделать статью лучше
Была ли эта статья полезной?
(или выберите слово/фразу и нажмите Ctrl+Enter