Panda3D
buttonHandle.I
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 buttonHandle.I
10  * @author drose
11  * @date 2000-03-01
12  */
13 
14 /**
15  * Constructs a ButtonHandle with the corresponding index number, which may
16  * have been returned by an earlier call to ButtonHandle::get_index().
17  */
18 constexpr ButtonHandle::
19 ButtonHandle(int index) : _index(index) {
20 }
21 
22 /**
23  *
24  */
25 INLINE bool ButtonHandle::
26 operator == (const ButtonHandle &other) const {
27  return (_index == other._index);
28 }
29 
30 /**
31  *
32  */
33 INLINE bool ButtonHandle::
34 operator != (const ButtonHandle &other) const {
35  return (_index != other._index);
36 }
37 
38 /**
39  *
40  */
41 INLINE bool ButtonHandle::
42 operator < (const ButtonHandle &other) const {
43  return (_index < other._index);
44 }
45 
46 /**
47  *
48  */
49 INLINE bool ButtonHandle::
50 operator <= (const ButtonHandle &other) const {
51  return (_index <= other._index);
52 }
53 
54 /**
55  *
56  */
57 INLINE bool ButtonHandle::
58 operator > (const ButtonHandle &other) const {
59  return (_index > other._index);
60 }
61 
62 /**
63  *
64  */
65 INLINE bool ButtonHandle::
66 operator >= (const ButtonHandle &other) const {
67  return (_index >= other._index);
68 }
69 
70 /**
71  * Sorts ButtonHandles arbitrarily (according to <, >, etc.). Returns a
72  * number less than 0 if this type sorts before the other one, greater than
73  * zero if it sorts after, 0 if they are equivalent.
74  */
75 INLINE int ButtonHandle::
76 compare_to(const ButtonHandle &other) const {
77  return _index - other._index;
78 }
79 
80 /**
81  * Returns a hash code suitable for phash_map.
82  */
83 INLINE size_t ButtonHandle::
84 get_hash() const {
85  return (size_t)_index;
86 }
87 
88 /**
89  * Returns true if the button was created with an ASCII equivalent code (e.g.
90  * for a standard keyboard button).
91  */
92 INLINE bool ButtonHandle::
93 has_ascii_equivalent() const {
94  return (_index > 0 && _index < 128);
95 }
96 
97 /**
98  * Returns the character code associated with the button, or '\0' if no ASCII
99  * code was associated.
100  */
101 INLINE char ButtonHandle::
102 get_ascii_equivalent() const {
103  return has_ascii_equivalent() ? (char)_index : '\0';
104 }
105 
106 /**
107  * Returns true if this ButtonHandle is the same as the other one, or if the
108  * other one is an alias for this one. (Does not return true if this button
109  * is an alias for the other one, however.)
110  *
111  * This is a more general comparison than operator ==.
112  */
113 INLINE bool ButtonHandle::
114 matches(const ButtonHandle &other) const {
115  return ((*this) == other ||
116  (other != ButtonHandle::none() &&
117  get_alias() == other));
118 }
119 
120 /**
121  * Returns the integer index associated with this ButtonHandle. Each
122  * different ButtonHandle will have a different index. However, you probably
123  * shouldn't be using this method; you should just treat the ButtonHandles as
124  * opaque classes. This is provided for the convenience of non-C++ scripting
125  * languages to build a hashtable of ButtonHandles.
126  */
127 constexpr int ButtonHandle::
128 get_index() const {
129  return _index;
130 }
131 
132 /**
133  *
134  */
135 INLINE void ButtonHandle::
136 output(std::ostream &out) const {
137  out << get_name();
138 }
139 
140 /**
141  * ButtonHandle::none() evaluates to false, everything else evaluates to true.
142  */
143 INLINE ButtonHandle::
144 operator bool () const {
145  return (_index != 0);
146 }
size_t get_hash() const
Returns a hash code suitable for phash_map.
Definition: buttonHandle.I:84
int compare_to(const ButtonHandle &other) const
Sorts ButtonHandles arbitrarily (according to <, >, etc.).
Definition: buttonHandle.I:76
std::string get_name(ButtonHandle button) const
Returns the name of the indicated button.
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:26
bool matches(const ButtonHandle &other) const
Returns true if this ButtonHandle is the same as the other one, or if the other one is an alias for t...
Definition: buttonHandle.I:114
has_ascii_equivalent
Returns true if the button was created with an ASCII equivalent code (e.g.
Definition: buttonHandle.h:63
ButtonHandle get_alias(ButtonHandle button) const
Returns the alias for the indicated button, or ButtonHandle::none() if the button has no specified al...