Panda3D
Loading...
Searching...
No Matches
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
21DEFINE_GAMEPAD_BUTTON_HANDLE(lstick)
22DEFINE_GAMEPAD_BUTTON_HANDLE(rstick)
23DEFINE_GAMEPAD_BUTTON_HANDLE(lshoulder)
24DEFINE_GAMEPAD_BUTTON_HANDLE(rshoulder)
25DEFINE_GAMEPAD_BUTTON_HANDLE(ltrigger)
26DEFINE_GAMEPAD_BUTTON_HANDLE(rtrigger)
27DEFINE_GAMEPAD_BUTTON_HANDLE(lgrip)
28DEFINE_GAMEPAD_BUTTON_HANDLE(rgrip)
29
30DEFINE_GAMEPAD_BUTTON_HANDLE(dpad_left)
31DEFINE_GAMEPAD_BUTTON_HANDLE(dpad_right)
32DEFINE_GAMEPAD_BUTTON_HANDLE(dpad_up)
33DEFINE_GAMEPAD_BUTTON_HANDLE(dpad_down)
34
35DEFINE_GAMEPAD_BUTTON_HANDLE(back)
36DEFINE_GAMEPAD_BUTTON_HANDLE(guide)
37DEFINE_GAMEPAD_BUTTON_HANDLE(start)
38
39DEFINE_GAMEPAD_BUTTON_HANDLE(next)
40DEFINE_GAMEPAD_BUTTON_HANDLE(previous)
41
42DEFINE_GAMEPAD_BUTTON_HANDLE(face_a)
43DEFINE_GAMEPAD_BUTTON_HANDLE(face_b)
44DEFINE_GAMEPAD_BUTTON_HANDLE(face_c)
45DEFINE_GAMEPAD_BUTTON_HANDLE(face_x)
46DEFINE_GAMEPAD_BUTTON_HANDLE(face_y)
47DEFINE_GAMEPAD_BUTTON_HANDLE(face_z)
48
49DEFINE_GAMEPAD_BUTTON_HANDLE(face_1)
50DEFINE_GAMEPAD_BUTTON_HANDLE(face_2)
51
52DEFINE_GAMEPAD_BUTTON_HANDLE(trigger)
53DEFINE_GAMEPAD_BUTTON_HANDLE(hat_up)
54DEFINE_GAMEPAD_BUTTON_HANDLE(hat_down)
55DEFINE_GAMEPAD_BUTTON_HANDLE(hat_left)
56DEFINE_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 */
64joystick(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 */
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.
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
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 ButtonRegistry * ptr()
Returns the pointer to the global ButtonRegistry object.
This class is just used as a convenient namespace for grouping all of these handy functions that retu...
static void init_gamepad_buttons()
This is intended to be called only once, by the static initialization performed in config_util....
This is our own Panda specialization on the default STL vector.
Definition pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.