summaryrefslogtreecommitdiffstats
path: root/labb8/lib/StanfordCPPLib/gevents.cpp
blob: 72043d67d982425f75452468ac9a434cf6d7e13e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * File: gevents.cpp
 * -----------------
 * This file implements the machine-independent functions for the classes
 * in the gevents.h interface.  The actual functions for receiving events
 * from the environment are implemented in the platform package.
 */

/*
 * Implementation notes:
 * ---------------------
 * The classes in this hierarchy are defined in an unusual way
 * for C++ in that all instance variables are part of the top-level
 * class.  The advantage of this design is that the GEvent class
 * then has all of the memory it needs to store an event of any of
 * its subclasses.
 */

#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
#include "error.h"
#include "gevents.h"
#include "gtimer.h"
#include "gtypes.h"
#include "map.h"
#include "platform.h"
using namespace std;

/* Global variables */

static Platform *pp = getPlatform();

/* Implementation of GEvent class */

GEvent::GEvent() {
   eventClass = NULL_EVENT;
   eventType = 0;
   valid = false;
   modifiers = 0;
}

EventClassType GEvent::getEventClass() const {
   return eventClass;
}

EventType GEvent::getEventType() const {
   return EventType(eventType);
}

double GEvent::getEventTime() const {
   return eventTime;
}

int GEvent::getModifiers() const {
   return modifiers;
}

string GEvent::toString() const {
   if (eventClass == 0) {
      return "GEvent(NULL)";
   } else if (eventClass == WINDOW_EVENT) {
      GWindowEvent windowEvent(*this);
      return (&windowEvent)->toString();
   } else if (eventClass == ACTION_EVENT) {
      GActionEvent actionEvent(*this);
      return (&actionEvent)->toString();
   } else if (eventClass == MOUSE_EVENT) {
      GMouseEvent mouseEvent(*this);
      return (&mouseEvent)->toString();
   } else if (eventClass == KEY_EVENT) {
      GKeyEvent keyEvent(*this);
      return (&keyEvent)->toString();
   } else if (eventClass == TIMER_EVENT) {
      GTimerEvent timerEvent(*this);
      return (&timerEvent)->toString();
   } else {
      return "GEvent(?)";
   }
}

bool GEvent::isValid() {
   return valid;
}

void GEvent::setEventTime(double time) {
   eventTime = time;
}

void GEvent::setModifiers(int modifiers) {
   this->modifiers = modifiers;
}

GWindowEvent::GWindowEvent() {
   valid = false;
}

GWindowEvent::GWindowEvent(GEvent e) {
   valid = e.valid && e.eventClass == WINDOW_EVENT;
   if (valid) {
      eventClass = e.eventClass;
      eventType = e.eventType;
      modifiers = e.modifiers;
      eventTime = e.eventTime;
      gwd = e.gwd;
   }
}

GWindowEvent::GWindowEvent(EventType type, const GWindow & gw) {
   this->eventClass = WINDOW_EVENT;
   this->eventType = int(type);
   this->gwd = gw.gwd;
   valid = true;
}

GWindow GWindowEvent::getGWindow() const {
   return GWindow(gwd);
}

string GWindowEvent::toString() const {
   if (!valid) return "GWindowEvent(?)";
   ostringstream os;
   os << "GWindowEvent:";
   switch (eventType) {
    case WINDOW_CLOSED:      os << "WINDOW_CLOSED";       break;
    case WINDOW_RESIZED:     os << "WINDOW_RESIZED";      break;
   }
   os << "()";
   return os.str();
}

GActionEvent::GActionEvent() {
   valid = false;
}

GActionEvent::GActionEvent(GEvent e) {
   valid = e.valid && e.eventClass == ACTION_EVENT;
   if (valid) {
      eventClass = e.eventClass;
      eventType = e.eventType;
      modifiers = e.modifiers;
      eventTime = e.eventTime;
      source = e.source;
      actionCommand = e.actionCommand;
   }
}

GActionEvent::GActionEvent(EventType type, GObject *source,
                           string actionCommand) {
   this->eventClass = ACTION_EVENT;
   this->eventType = int(type);
   this->source = source;
   this->actionCommand = actionCommand;
   valid = true;
}

GObject *GActionEvent::getSource() const {
   return source;
}

string GActionEvent::getActionCommand() const {
   if (!valid) error("getActionCommand: Event is not valid");
   return actionCommand;
}

string GActionEvent::toString() const {
   if (!valid) return "GActionEvent(?)";
   ostringstream os;
   os << "GActionEvent:ACTION_PERFORMED(" << actionCommand << ")";
   return os.str();
}

GMouseEvent::GMouseEvent() {
   valid = false;
}

GMouseEvent::GMouseEvent(GEvent e) {
   valid = e.valid && e.eventClass == MOUSE_EVENT;
   if (valid) {
      eventClass = e.eventClass;
      eventType = e.eventType;
      modifiers = e.modifiers;
      eventTime = e.eventTime;
      x = e.x;
      y = e.y;
   }
}

GMouseEvent::GMouseEvent(EventType type, const GWindow & gw,
                                         double x, double y) {
   this->eventClass = MOUSE_EVENT;
   this->eventType = int(type);
   this->gwd = gw.gwd;
   this->x = x;
   this->y = y;
   valid = true;
}

GWindow GMouseEvent::getGWindow() const {
   return GWindow(gwd);
}

double GMouseEvent::getX() const {
   if (!valid) error("getX: Event is not valid");
   return x;
}

double GMouseEvent::getY() const {
   if (!valid) error("getY: Event is not valid");
   return y;
}

string GMouseEvent::toString() const {
   if (!valid) return "GMouseEvent(?)";
   ostringstream os;
   os << "GMouseEvent:";
   switch (eventType) {
    case MOUSE_PRESSED:  os << "MOUSE_PRESSED";   break;
    case MOUSE_RELEASED: os << "MOUSE_RELEASED";  break;
    case MOUSE_CLICKED:  os << "MOUSE_CLICKED";   break;
    case MOUSE_MOVED:    os << "MOUSE_MOVED";     break;
    case MOUSE_DRAGGED:  os << "MOUSE_DRAGGED";   break;
   }
   os << "(" << x << ", " << y << ")";
   return os.str();
}

GKeyEvent::GKeyEvent() {
   valid = false;
}

GKeyEvent::GKeyEvent(GEvent e) {
   this->eventClass = KEY_EVENT;
   valid = e.valid && e.eventClass == KEY_EVENT;
   if (valid) {
      eventClass = e.eventClass;
      eventType = e.eventType;
      modifiers = e.modifiers;
      eventTime = e.eventTime;
      keyChar = e.keyChar;
      keyCode = e.keyCode;
   }
}

GKeyEvent::GKeyEvent(EventType type, const GWindow & gw,
                                     int keyChar, int keyCode) {
   this->eventClass = KEY_EVENT;
   this->eventType = int(type);
   this->gwd = gw.gwd;
   this->keyChar = keyChar;
   this->keyCode = keyCode;
   valid = true;
}

GWindow GKeyEvent::getGWindow() const {
   return GWindow(gwd);
}

char GKeyEvent::getKeyChar() const {
   if (!valid) error("getKey: Event is not valid");
   return char(keyChar);
   // Think about wide characters at some point
}

int GKeyEvent::getKeyCode() const {
   if (!valid) error("getKey: Event is not valid");
   return keyCode;
}

string GKeyEvent::toString() const {
   if (!valid) return "GKeyEvent(?)";
   ostringstream os;
   os << "GKeyEvent:";
   int ch = '\0';
   switch (eventType) {
    case KEY_PRESSED:  os << "KEY_PRESSED";   ch = keyCode; break;
    case KEY_RELEASED: os << "KEY_RELEASED";  ch = keyCode; break;
    case KEY_TYPED:    os << "KEY_TYPED";     ch = keyChar; break;
   }
   if (isprint(ch)) {
      os << "('" << char(ch) << "')";
   } else {
      os << oct << "('\\" << ch << "')";
   }
   return os.str();
}

/* Timer events */

GTimerEvent::GTimerEvent() {
   valid = false;
}

GTimerEvent::GTimerEvent(GEvent e) {
   valid = e.valid && e.eventClass == TIMER_EVENT;
   if (valid) {
      eventClass = e.eventClass;
      eventType = e.eventType;
      modifiers = e.modifiers;
      eventTime = e.eventTime;
      gtd = e.gtd;
   }
}

GTimerEvent::GTimerEvent(EventType type, const GTimer & timer) {
   this->eventClass = TIMER_EVENT;
   this->eventType = int(type);
   this->gtd = timer.gtd;
   valid = true;
}

GTimer GTimerEvent::getGTimer() const {
   return GTimer(gtd);
}

string GTimerEvent::toString() const {
   if (!valid) return "GTimerEvent(?)";
   return "GTimerEvent:TIMER_TICKED()";
}

/* Global event handlers */

void waitForClick() {
   waitForEvent(CLICK_EVENT);
}

GEvent waitForEvent(int mask) {
   return pp->waitForEvent(mask);
}

GEvent getNextEvent(int mask) {
   return pp->getNextEvent(mask);
}