Panda3D
buttonHandle.I
1 // Filename: buttonHandle.I
2 // Created by: drose (01Mar00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: ButtonHandle::Constructor
18 // Access: Published
19 // Description: The default constructor must do nothing, because we
20 // can't guarantee ordering of static initializers. If
21 // the constructor tried to initialize its value, it
22 // might happen after the value had already been set
23 // previously by another static initializer!
24 ////////////////////////////////////////////////////////////////////
25 INLINE ButtonHandle::
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: ButtonHandle::Constructor
31 // Access: Published
32 // Description: Constructs a ButtonHandle with the corresponding
33 // index number, which may have been returned by an
34 // earlier call to ButtonHandle::get_index().
35 ////////////////////////////////////////////////////////////////////
36 INLINE ButtonHandle::
37 ButtonHandle(int index) : _index(index) {
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: ButtonHandle::Copy Constructor
42 // Access: Published
43 // Description:
44 ////////////////////////////////////////////////////////////////////
45 INLINE ButtonHandle::
46 ButtonHandle(const ButtonHandle &copy) : _index(copy._index) {
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: ButtonHandle::Equality Operator
51 // Access: Published
52 // Description:
53 ////////////////////////////////////////////////////////////////////
54 INLINE bool ButtonHandle::
55 operator == (const ButtonHandle &other) const {
56  return (_index == other._index);
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: ButtonHandle::Inequality Operator
61 // Access: Published
62 // Description:
63 ////////////////////////////////////////////////////////////////////
64 INLINE bool ButtonHandle::
65 operator != (const ButtonHandle &other) const {
66  return (_index != other._index);
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: ButtonHandle::Ordering Operator
71 // Access: Published
72 // Description:
73 ////////////////////////////////////////////////////////////////////
74 INLINE bool ButtonHandle::
75 operator < (const ButtonHandle &other) const {
76  return (_index < other._index);
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: ButtonHandle::Ordering Operator
81 // Access: Published
82 // Description:
83 ////////////////////////////////////////////////////////////////////
84 INLINE bool ButtonHandle::
85 operator <= (const ButtonHandle &other) const {
86  return (_index <= other._index);
87 }
88 
89 ////////////////////////////////////////////////////////////////////
90 // Function: ButtonHandle::Ordering Operator
91 // Access: Published
92 // Description:
93 ////////////////////////////////////////////////////////////////////
94 INLINE bool ButtonHandle::
95 operator > (const ButtonHandle &other) const {
96  return (_index > other._index);
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: ButtonHandle::Ordering Operator
101 // Access: Published
102 // Description:
103 ////////////////////////////////////////////////////////////////////
104 INLINE bool ButtonHandle::
105 operator >= (const ButtonHandle &other) const {
106  return (_index >= other._index);
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: ButtonHandle::compare_to
111 // Access: Published
112 // Description: Sorts ButtonHandles arbitrarily (according to <, >,
113 // etc.). Returns a number less than 0 if this type
114 // sorts before the other one, greater than zero if it
115 // sorts after, 0 if they are equivalent.
116 ////////////////////////////////////////////////////////////////////
117 INLINE int ButtonHandle::
118 compare_to(const ButtonHandle &other) const {
119  return _index - other._index;
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: ButtonHandle::get_hash
124 // Access: Published
125 // Description: Returns a hash code suitable for phash_map.
126 ////////////////////////////////////////////////////////////////////
127 INLINE size_t ButtonHandle::
128 get_hash() const {
129  return (size_t)_index;
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: ButtonHandle::has_ascii_equivalent
134 // Access: Published
135 // Description: Returns true if the button was created with an ASCII
136 // equivalent code (e.g. for a standard keyboard
137 // button).
138 ////////////////////////////////////////////////////////////////////
139 INLINE bool ButtonHandle::
141  return (_index > 0 && _index < 128);
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: ButtonHandle::get_ascii_equivalent
146 // Access: Published
147 // Description: Returns the character code associated with the
148 // button, or '\0' if no ASCII code was associated.
149 ////////////////////////////////////////////////////////////////////
150 INLINE char ButtonHandle::
152  return has_ascii_equivalent() ? (char)_index : '\0';
153 }
154 
155 ////////////////////////////////////////////////////////////////////
156 // Function: ButtonHandle::matches
157 // Access: Published
158 // Description: Returns true if this ButtonHandle is the same as the
159 // other one, or if the other one is an alias for this
160 // one. (Does not return true if this button is an
161 // alias for the other one, however.)
162 //
163 // This is a more general comparison than operator ==.
164 ////////////////////////////////////////////////////////////////////
165 INLINE bool ButtonHandle::
166 matches(const ButtonHandle &other) const {
167  return ((*this) == other ||
168  (other != ButtonHandle::none() &&
169  get_alias() == other));
170 }
171 
172 ////////////////////////////////////////////////////////////////////
173 // Function: ButtonHandle::get_index
174 // Access: Published
175 // Description: Returns the integer index associated with this
176 // ButtonHandle. Each different ButtonHandle will have a
177 // different index. However, you probably shouldn't be
178 // using this method; you should just treat the
179 // ButtonHandles as opaque classes. This is provided
180 // for the convenience of non-C++ scripting languages to
181 // build a hashtable of ButtonHandles.
182 ////////////////////////////////////////////////////////////////////
183 INLINE int ButtonHandle::
184 get_index() const {
185  return _index;
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function: ButtonHandle::output
190 // Access: Published
191 // Description:
192 ////////////////////////////////////////////////////////////////////
193 INLINE void ButtonHandle::
194 output(ostream &out) const {
195  out << get_name();
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: ButtonHandle::none
200 // Access: Published, Static
201 // Description: Returns a special zero-valued ButtonHandle that is
202 // used to indicate no button.
203 ////////////////////////////////////////////////////////////////////
205 none() {
206  return _none;
207 }
208 
209 ////////////////////////////////////////////////////////////////////
210 // Function: ButtonHandle::operator bool
211 // Access: Published
212 // Description: ButtonHandle::none() evaluates to false, everything
213 // else evaluates to true.
214 ////////////////////////////////////////////////////////////////////
215 INLINE ButtonHandle::
216 operator bool () const {
217  return (_index != 0);
218 }
static ButtonHandle none()
Returns a special zero-valued ButtonHandle that is used to indicate no button.
Definition: buttonHandle.I:205
size_t get_hash() const
Returns a hash code suitable for phash_map.
Definition: buttonHandle.I:128
int compare_to(const ButtonHandle &other) const
Sorts ButtonHandles arbitrarily (according to <, >, etc.).
Definition: buttonHandle.I:118
ButtonHandle()
The default constructor must do nothing, because we can&#39;t guarantee ordering of static initializers...
Definition: buttonHandle.I:26
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:28
ButtonHandle get_alias() const
Returns the alias (alternate name) associated with the button, if any, or ButtonHandle::none() if the...
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:166
int get_index() const
Returns the integer index associated with this ButtonHandle.
Definition: buttonHandle.I:184
bool has_ascii_equivalent() const
Returns true if the button was created with an ASCII equivalent code (e.g.
Definition: buttonHandle.I:140
string get_name() const
Returns the name of the button.
char get_ascii_equivalent() const
Returns the character code associated with the button, or &#39;\0&#39; if no ASCII code was associated...
Definition: buttonHandle.I:151