Panda3D
gamepadButton.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file gamepadButton.cxx
10  * @author rdb
11  * @date 2015-08-21
12  */
13 
14 #include "gamepadButton.h"
15 #include "buttonRegistry.h"
16 
17 #define DEFINE_GAMEPAD_BUTTON_HANDLE(KeyName) \
18  static ButtonHandle _##KeyName; \
19  ButtonHandle GamepadButton::KeyName() { return _##KeyName; }
20 
21 DEFINE_GAMEPAD_BUTTON_HANDLE(lstick)
22 DEFINE_GAMEPAD_BUTTON_HANDLE(rstick)
23 DEFINE_GAMEPAD_BUTTON_HANDLE(lshoulder)
24 DEFINE_GAMEPAD_BUTTON_HANDLE(rshoulder)
25 DEFINE_GAMEPAD_BUTTON_HANDLE(ltrigger)
26 DEFINE_GAMEPAD_BUTTON_HANDLE(rtrigger)
27 DEFINE_GAMEPAD_BUTTON_HANDLE(lgrip)
28 DEFINE_GAMEPAD_BUTTON_HANDLE(rgrip)
29 
30 DEFINE_GAMEPAD_BUTTON_HANDLE(dpad_left)
31 DEFINE_GAMEPAD_BUTTON_HANDLE(dpad_right)
32 DEFINE_GAMEPAD_BUTTON_HANDLE(dpad_up)
33 DEFINE_GAMEPAD_BUTTON_HANDLE(dpad_down)
34 
35 DEFINE_GAMEPAD_BUTTON_HANDLE(back)
36 DEFINE_GAMEPAD_BUTTON_HANDLE(guide)
37 DEFINE_GAMEPAD_BUTTON_HANDLE(start)
38 
39 DEFINE_GAMEPAD_BUTTON_HANDLE(next)
40 DEFINE_GAMEPAD_BUTTON_HANDLE(previous)
41 
42 DEFINE_GAMEPAD_BUTTON_HANDLE(face_a)
43 DEFINE_GAMEPAD_BUTTON_HANDLE(face_b)
44 DEFINE_GAMEPAD_BUTTON_HANDLE(face_c)
45 DEFINE_GAMEPAD_BUTTON_HANDLE(face_x)
46 DEFINE_GAMEPAD_BUTTON_HANDLE(face_y)
47 DEFINE_GAMEPAD_BUTTON_HANDLE(face_z)
48 
49 DEFINE_GAMEPAD_BUTTON_HANDLE(face_1)
50 DEFINE_GAMEPAD_BUTTON_HANDLE(face_2)
51 
52 DEFINE_GAMEPAD_BUTTON_HANDLE(trigger)
53 DEFINE_GAMEPAD_BUTTON_HANDLE(hat_up)
54 DEFINE_GAMEPAD_BUTTON_HANDLE(hat_down)
55 DEFINE_GAMEPAD_BUTTON_HANDLE(hat_left)
56 DEFINE_GAMEPAD_BUTTON_HANDLE(hat_right)
57 
58 /**
59  * Returns the ButtonHandle associated with the particular numbered joystick
60  * button (zero-based), if there is one, or ButtonHandle::none() if there is
61  * not.
62  */
64 joystick(int button_number) {
65  if (button_number >= 0) {
66  // "button1" does not exist, it is called "trigger" instead
67  static pvector<ButtonHandle> buttons(1, _trigger);
68  while ((size_t)button_number >= buttons.size()) {
69  char numstr[20];
70  sprintf(numstr, "joystick%d", (int)buttons.size() + 1);
71  ButtonHandle handle;
72  ButtonRegistry::ptr()->register_button(handle, numstr);
73  buttons.push_back(handle);
74  }
75  return buttons[button_number];
76  }
77  return ButtonHandle::none();
78 }
79 
80 /**
81  * This is intended to be called only once, by the static initialization
82  * performed in config_util.cxx.
83  */
84 void GamepadButton::
86  ButtonRegistry::ptr()->register_button(_lstick, "lstick");
87  ButtonRegistry::ptr()->register_button(_rstick, "rstick");
88  ButtonRegistry::ptr()->register_button(_lshoulder, "lshoulder");
89  ButtonRegistry::ptr()->register_button(_rshoulder, "rshoulder");
90  ButtonRegistry::ptr()->register_button(_ltrigger, "ltrigger");
91  ButtonRegistry::ptr()->register_button(_rtrigger, "rtrigger");
92  ButtonRegistry::ptr()->register_button(_lgrip, "lgrip");
93  ButtonRegistry::ptr()->register_button(_rgrip, "rgrip");
94 
95  ButtonRegistry::ptr()->register_button(_dpad_left, "dpad_left");
96  ButtonRegistry::ptr()->register_button(_dpad_right, "dpad_right");
97  ButtonRegistry::ptr()->register_button(_dpad_up, "dpad_up");
98  ButtonRegistry::ptr()->register_button(_dpad_down, "dpad_down");
99 
100  ButtonRegistry::ptr()->register_button(_back, "back");
101  ButtonRegistry::ptr()->register_button(_guide, "guide");
102  ButtonRegistry::ptr()->register_button(_start, "start");
103 
104  ButtonRegistry::ptr()->register_button(_next, "next");
105  ButtonRegistry::ptr()->register_button(_previous, "previous");
106 
107  ButtonRegistry::ptr()->register_button(_face_a, "face_a");
108  ButtonRegistry::ptr()->register_button(_face_b, "face_b");
109  ButtonRegistry::ptr()->register_button(_face_c, "face_c");
110  ButtonRegistry::ptr()->register_button(_face_x, "face_x");
111  ButtonRegistry::ptr()->register_button(_face_y, "face_y");
112  ButtonRegistry::ptr()->register_button(_face_z, "face_z");
113 
114  ButtonRegistry::ptr()->register_button(_face_1, "face_1");
115  ButtonRegistry::ptr()->register_button(_face_2, "face_2");
116 
117  ButtonRegistry::ptr()->register_button(_trigger, "trigger");
118  ButtonRegistry::ptr()->register_button(_hat_up, "hat_up");
119  ButtonRegistry::ptr()->register_button(_hat_down, "hat_down");
120  ButtonRegistry::ptr()->register_button(_hat_left, "hat_left");
121  ButtonRegistry::ptr()->register_button(_hat_right, "hat_right");
122 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:26
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
static ButtonRegistry * ptr()
Returns the pointer to the global ButtonRegistry object.
bool register_button(ButtonHandle &button_handle, const std::string &name, ButtonHandle alias=ButtonHandle::none(), char ascii_equivalent='\0')
Registers a new ButtonHandle with the indicated name, and if specified, the indicated ASCII equivalen...
static void init_gamepad_buttons()
This is intended to be called only once, by the static initialization performed in config_util....
static ButtonHandle joystick(int button_number)
Returns the ButtonHandle associated with the particular numbered joystick button (zero-based),...