SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLInputManager.cpp
Go to the documentation of this file.
1 /**
2  * \file SLInputManager.cpp
3  * \date January 2015
4  * \authors Marc Wacker, Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #include <SLInputManager.h>
11 #include <SLSceneView.h>
12 
13 //-----------------------------------------------------------------------------
14 /*! Sends any queued up system event's to their correct receiver and
15 polls all activated SLInputDevices.
16 
17 @note The event queue is similar to how Qt manages it's events. The main
18  difference is, that we don't use the SLInputEvent class outside of the
19  SLInputManager. The SLInputManager calls the correct SLSceneView input
20  handler functions directly. Also we don't allow for custom SLInputEvents.
21  This is the other main difference to the Qt event system.
22  The decision to go this route is simplicity for now.
23  It is totally sufficient for our use cases to provide the user with the
24  SLInputDevice interface to realize custom input.
25  However it has to be considered, that Qt also has many GUI related events
26  like MouseEnter, MouseLeave, Drag etc. For a sophisticated GUI
27  implementation the whole input management in SL would have to be reviewed.
28 */
30 {
31  // process system events first
32  SLbool consumedEvents = processQueuedEvents(sv);
33 
34  // process custom input devices
35  for (auto device : _devices)
36  consumedEvents |= device->poll();
37 
38  return consumedEvents;
39 }
40 //-----------------------------------------------------------------------------
41 /*! Add a new SLInputEvent to the event queue. The queue will be emtied when
42 a call to SLInputManager::pollEvents is made. The passed in SLInputEvents have
43 to be dynamically allocated by the user, the deallocation is handled by the
44 SLInputManager */
46 {
47  _systemEvents.push(e);
48 }
49 //-----------------------------------------------------------------------------
50 /*! Work off any queued up input event's and notify the correct receiver.
51 @note this is similar to the Qt QObject::event function.*/
53 {
55 
56  // flag if an event has been consumed by a receiver
57  SLbool eventConsumed = false;
58 
59  while (!q.empty())
60  {
61  const SLInputEvent* e;
62  {
63  e = q.front();
64  q.pop();
65  }
66 
67  if (sv)
68  {
69  switch (e->type)
70  {
72  {
73  const SLMouseEvent* me = (const SLMouseEvent*)e;
74  eventConsumed |= sv->onMouseMove(me->x, me->y);
75  }
76  break;
78  {
79  const SLMouseEvent* me = (const SLMouseEvent*)e;
80  eventConsumed |= sv->onMouseDown(me->button, me->x, me->y, me->modifier);
81  }
82  break;
84  {
85  const SLMouseEvent* me = (const SLMouseEvent*)e;
86  eventConsumed |= sv->onMouseUp(me->button, me->x, me->y, me->modifier);
87  }
88  break;
90  {
91  const SLMouseEvent* me = (const SLMouseEvent*)e;
92  eventConsumed |= sv->onDoubleClick(me->button, me->x, me->y, me->modifier);
93  }
94  break;
96  {
97  const SLMouseEvent* me = (const SLMouseEvent*)e;
98  eventConsumed |= sv->onMouseWheel(me->y, me->modifier);
99  }
100  break;
101 
103  {
104  const SLTouchEvent* te = (const SLTouchEvent*)e;
105  eventConsumed |= sv->onTouch2Move(te->x1, te->y1, te->x2, te->y2);
106  }
107  break;
109  {
110  const SLTouchEvent* te = (const SLTouchEvent*)e;
111  eventConsumed |= sv->onTouch2Down(te->x1, te->y1, te->x2, te->y2);
112  }
113  break;
115  {
116  const SLTouchEvent* te = (const SLTouchEvent*)e;
117  eventConsumed |= sv->onTouch2Up(te->x1, te->y1, te->x2, te->y2);
118  }
119  break;
120 
122  {
123  const SLKeyEvent* ke = (const SLKeyEvent*)e;
124  eventConsumed |= sv->onKeyPress(ke->key, ke->modifier);
125  }
126  break;
127  case SLInputEvent::KeyUp:
128  {
129  const SLKeyEvent* ke = (const SLKeyEvent*)e;
130  eventConsumed |= sv->onKeyRelease(ke->key, ke->modifier);
131  }
132  break;
134  {
135  const SLCharInputEvent* ce = (const SLCharInputEvent*)e;
136  eventConsumed |= sv->onCharInput(ce->character);
137  }
138  break;
139 
141  {
142  const SLResizeEvent* re = (const SLResizeEvent*)e;
143  sv->onResize(re->width, re->height);
144  }
145  break;
146 
148  {
150  SLstring path = re->path;
151 
152  if (!Utils::dirExists(path))
153  Utils::makeDirRecurse(path);
154  SLstring filename = "Screenshot_" + Utils::getDateTime2String() + ".png";
155  SLstring pathFilename = path + filename;
156  sv->saveFrameBufferAsImage(pathFilename, cv::Size(sv->scrW(), sv->scrH()));
157  }
158  break;
159 
160  default: break;
161  }
162  }
163 
164  delete e;
165  }
166 
167  return eventConsumed;
168 }
169 //-----------------------------------------------------------------------------
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
std::queue< const SLInputEvent * > SLQInputEvent
Definition: SLInputEvent.h:134
Specialized SLInput class for unicode character input.
Definition: SLInputEvent.h:116
Baseclass for all system input events.
Definition: SLInputEvent.h:25
enum SLInputEvent::Type type
concrete type of the event
SLbool pollAndProcessEvents(SLSceneView *sv)
SLVInputDevice _devices
list of activated SLInputDevices
SLQInputEvent _systemEvents
queue for known system events
SLbool processQueuedEvents(SLSceneView *sv)
void queueEvent(const SLInputEvent *e)
Specialized SLInput class for all keypress related input events.
Definition: SLInputEvent.h:67
SLKey modifier
Definition: SLInputEvent.h:70
Specialized SLInput class for all mouse related input events.
Definition: SLInputEvent.h:54
SLKey modifier
Definition: SLInputEvent.h:59
SLMouseButton button
Definition: SLInputEvent.h:58
Specialized SLInput class for window resize events.
Definition: SLInputEvent.h:101
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
virtual SLbool onDoubleClick(SLMouseButton button, SLint x, SLint y, SLKey mod)
virtual SLbool onCharInput(SLuint c)
void onResize(SLint width, SLint height)
virtual SLbool onMouseMove(SLint x, SLint y)
virtual SLbool onKeyRelease(SLKey key, SLKey mod)
void saveFrameBufferAsImage(SLstring pathFilename, cv::Size targetSize=cv::Size(-1, -1))
Saves after n wait frames the front frame buffer as a PNG image.
virtual SLbool onMouseWheel(SLint delta, SLKey mod)
virtual SLbool onTouch2Move(SLint scrX1, SLint scrY1, SLint scrX2, SLint scrY2)
virtual SLbool onMouseUp(SLMouseButton button, SLint scrX, SLint scrY, SLKey mod)
virtual SLbool onKeyPress(SLKey key, SLKey mod)
virtual SLbool onMouseDown(SLMouseButton button, SLint scrX, SLint scrY, SLKey mod)
void scrW(SLint scrW)
Definition: SLSceneView.h:147
virtual SLbool onTouch2Up(SLint scrX1, SLint scrY1, SLint scrX2, SLint scrY2)
void scrH(SLint scrH)
Definition: SLSceneView.h:148
virtual SLbool onTouch2Down(SLint scrX1, SLint scrY1, SLint scrX2, SLint scrY2)
Specialized SLInput class to trigger a screen capture.
Definition: SLInputEvent.h:126
Specialized SLInput class for touch related input events.
Definition: SLInputEvent.h:78
string getDateTime2String()
Returns local time as string like "20190213-154611".
Definition: Utils.cpp:289
bool dirExists(const string &path)
Returns true if a directory exists.
Definition: Utils.cpp:790
bool makeDirRecurse(std::string path)
Definition: Utils.cpp:826