cgv
Loading...
Searching...
No Matches
event.cxx
1#include "event.h"
2
3#include <cgv/utils/scan.h>
4#include <iomanip>
5namespace cgv {
6 namespace gui {
7
8#define MAX_MOD_LENGTH 5
9static int nr_mods = 4;
10static const char* mod_names[4] = { "Shift+", "Alt+", "Ctrl+", "Meta+" };
11static EventModifier mod_ids[4] = { EM_SHIFT, EM_ALT,EM_CTRL, EM_META };
12
13#define MAX_TOG_LENGTH 10
14static int nr_togs = 3;
15static const char* tog_names[3] = { "CapsLock", "NumLock", "ScrollLock" };
17
18
19// construct event from its kind
20event::event(unsigned int _kind, unsigned char _modifiers, unsigned char _toggle_keys, double _time)
21 : kind(_kind), flags(0), modifiers(_modifiers), toggle_keys(_toggle_keys), time(_time)
22{
23}
24
25// virtual destructor for events
27{
28}
29
32{
33 return 0;
34}
35
36// convert a modifier combination into a readable string.
37std::string get_modifier_string(EventModifier modifiers)
38{
39 std::string res;
40 for (int mi=0; mi<nr_mods; ++mi)
41 if ( (modifiers&mod_ids[mi]) != 0)
42 res += mod_names[mi];
43 return res;
44}
45
46// convert a toggle key combination into a readable string separated by '+' signs, i.e. "CapsLock+NumLock"
48{
49 std::string res;
50 for (int ti=0; ti<nr_togs; ++ti)
51 if ( (toggle_keys&tog_ids[ti]) != 0) {
52 if (!res.empty())
53 res += '+';
54 res += tog_names[ti];
55 }
56 return res;
57}
58
59unsigned char stream_in_toggle_keys(std::istream& is)
60{
61 int c = is.peek();
62 if (c > 255)
63 return 0;
65 int ti;
66 for (ti = 0; ti < nr_togs; ++ti)
67 if (tog_names[ti][0] == c)
68 break;
69 if (ti == nr_togs)
70 return 0;
71
72 int n = (int)std::string(tog_names[ti]).size();
73 int buffer[MAX_TOG_LENGTH];
74 for (int i=1; i<n; ++i) {
75 buffer[i-1] = is.get();
76 c = is.peek();
77 if (c > 255)
78 return 0;
80 if (tog_names[ti][i] != c) {
81 for (int j = i; j > 0; ) {
82 --j;
83 is.putback(buffer[j]);
84 }
85 return 0;
86 }
87 }
88 is.get();
89 return tog_ids[ti];
90}
91
92
93unsigned char stream_in_modifier(std::istream& is)
94{
95 int c = is.peek();
96 if (c > 255)
97 return 0;
99 int mi;
100 for (mi = 0; mi < nr_mods; ++mi)
101 if (mod_names[mi][0] == c)
102 break;
103 if (mi == nr_mods)
104 return 0;
105
106 int n = (int)std::string(mod_names[mi]).size();
107 int buffer[MAX_MOD_LENGTH];
108 for (int i=1; i<n; ++i) {
109 buffer[i-1] = is.get();
110 c = is.peek();
111 if (c > 255)
112 return 0;
114 if (mod_names[mi][i] != c) {
115 for (int j = i; j > 0; ) {
116 --j;
117 is.putback(buffer[j]);
118 }
119 return 0;
120 }
121 }
122 is.get();
123 return mod_ids[mi];
124}
125
126unsigned char stream_in_modifiers(std::istream& is)
127{
128 unsigned char modifiers = 0;
129 do {
130 unsigned char m = stream_in_modifier(is);
131 if (m == 0)
132 break;
133 modifiers |= m;
134 } while (true);
135 return modifiers;
136}
137
138// write to stream
139void event::stream_out(std::ostream& os) const
140{
141 const char* flag_strs[] = {
142 "Multi", "Drag&Drop", "Gamepad", "VR"
143 };
144 EventFlags flags[] = {
145 EF_MULTI,
146 EF_DND,
147 EF_PAD,
148 EF_VR
149 };
150 const char* kind_strs[] = { "none", "key", "mouse", "throttle", "stick", "pose" };
151
152 os << kind_strs[kind] << "[" << std::fixed << std::showpoint << std::setprecision(3) << time << "] ";
153 if (get_toggle_keys() != 0) {
154 os << "(";
155 bool last = false;
156 if ((get_toggle_keys()&ETK_CAPS_LOCK)!=0) {
157 os << "Caps_Lock";
158 last = true;
159 }
160 if ((get_toggle_keys()&ETK_NUM_LOCK)!=0) {
161 if (last)
162 os << ",";
163 os << "Num_Lock";
164 last = true;
165 }
166 if ((get_toggle_keys()&ETK_SCROLL_LOCK)!=0) {
167 if (last)
168 os << ",";
169 os << "Scroll_Lock";
170 last = true;
171 }
172 os << ") ";
173 }
175 if (get_flags() != EF_NONE) {
176 os << "{";
177 bool first = true;
178 for (unsigned i = 0; i < 4; ++i) {
179 if ((get_flags()&flags[i]) != 0) {
180 if (first)
181 first = false;
182 else
183 os << '|';
184 os << flag_strs[i];
185 }
186 }
187 os << "}";
188 }
189}
190// read from stream
191void event::stream_in(std::istream&)
192{
193 std::cerr << "event::stream_in not implemented yet" << std::endl;
194}
195
196// set the kind of the event
197void event::set_kind(unsigned char _kind)
198{
199 kind = _kind;
200}
201// return, what kind of event this is
202unsigned int event::get_kind() const
203{
204 return kind;
205}
206
208void event::set_flags(unsigned char _flags)
209{
210 flags = _flags;
211}
212
214unsigned event::get_flags() const
215{
216 return flags;
217}
218
219// set the state of the toggle keys
220void event::set_toggle_keys(unsigned char _toggle_keys)
221{
222 toggle_keys = _toggle_keys;
223}
224
225// return the state of the toggle keys
226unsigned char event::get_toggle_keys() const
227{
228 return toggle_keys;
229}
230
231// set the modifiers
232void event::set_modifiers(unsigned char _modifiers)
233{
234 modifiers = _modifiers;
235}
236// return the active modifiers
237unsigned char event::get_modifiers() const
238{
239 return modifiers;
240}
241// set the time of the event
242void event::set_time(const double& _time)
243{
244 time = _time;
245}
246// return the time of the event in seconds
247double event::get_time() const
248{
249 return time;
250}
251
252unsigned char& ref_current_modifiers()
253{
254 static unsigned char mods = 0;
255 return mods;
256}
257
258
259 }
260}
261
262#include <cgv/config/lib_end.h>
double get_time() const
return the time of the event in seconds
Definition event.cxx:247
void set_toggle_keys(unsigned char _toggle_keys)
set the state of the toggle keys
Definition event.cxx:220
void set_modifiers(unsigned char _modifiers)
set the modifiers
Definition event.cxx:232
virtual void * get_device_id() const
return the device id, by default returns 0
Definition event.cxx:31
unsigned get_kind() const
return, what kind of event this is, typically a value from the EventId enum
Definition event.cxx:202
unsigned char get_toggle_keys() const
return the state of the toggle keys as values from EventToggleKeys combined with a logical or-operati...
Definition event.cxx:226
virtual void stream_out(std::ostream &os) const
write to stream
Definition event.cxx:139
unsigned char kind
store which kind of event we have
Definition event.h:62
unsigned char toggle_keys
store the active toggle keys
Definition event.h:68
void set_kind(unsigned char _kind)
set the kind of the event
Definition event.cxx:197
unsigned char modifiers
store the active modifiers
Definition event.h:66
unsigned get_flags() const
return the event flags
Definition event.cxx:214
virtual ~event()
virtual destructor for events
Definition event.cxx:26
double time
store the time of the event in seconds
Definition event.h:70
unsigned char flags
store event flags
Definition event.h:64
unsigned char get_modifiers() const
return the active modifiers as values from EventModifier combined with a logical or-operation
Definition event.cxx:237
virtual void stream_in(std::istream &is)
read from stream
Definition event.cxx:191
void set_flags(unsigned char _flags)
return the set the event flags
Definition event.cxx:208
event(unsigned int _kind=EID_NONE, unsigned char _modifiers=0, unsigned char _toggle_keys=0, double _time=0)
construct event from its kind
Definition event.cxx:20
void set_time(const double &_time)
set the time of the event
Definition event.cxx:242
unsigned char stream_in_modifiers(std::istream &is)
read modifiers in string format from a stream and set the passed reference to EventModifier s ored to...
Definition event.cxx:126
std::string get_modifier_string(EventModifier modifiers)
convert a modifier combination into a readable string ending on a '+' sign if not empty,...
Definition event.cxx:37
unsigned char & ref_current_modifiers()
read out the current modifiers
Definition event.cxx:252
EventToggleKeys
define constants for toggle keys
Definition event.h:50
@ ETK_NUM_LOCK
num lock
Definition event.h:52
@ ETK_SCROLL_LOCK
scroll lock
Definition event.h:53
@ ETK_CAPS_LOCK
caps lock
Definition event.h:51
EventModifier
define constants for event modifiers
Definition event.h:41
@ EM_SHIFT
shift modifier
Definition event.h:42
@ EM_ALT
alt modifier
Definition event.h:43
@ EM_META
meta modifier (windows or mac key)
Definition event.h:45
@ EM_CTRL
ctrl modifier
Definition event.h:44
std::string get_toggle_keys_string(EventToggleKeys toggle_keys)
convert a toggle key combination into a readable string separated by '+' signs, i....
Definition event.cxx:47
EventFlags
flags
Definition event.h:30
@ EF_DND
whether mouse has a drag and drop target attached
Definition event.h:33
@ EF_MULTI
whether event is tagged with id of the device that generated the event
Definition event.h:32
@ EF_VR
whether event is from VR kit
Definition event.h:35
@ EF_PAD
whether event is from gamepad
Definition event.h:34
char to_lower(char c)
convert char to lower case
Definition scan.cxx:39
char to_upper(char c)
convert char to upper case
Definition scan.cxx:106
the cgv namespace
Definition print.h:11
Helper functions to process strings.