forked from trlsmax/imgui-vtk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vtkSDL2RenderWindowInteractor.cxx
362 lines (314 loc) · 9.38 KB
/
vtkSDL2RenderWindowInteractor.cxx
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSDL2RenderWindowInteractor.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "vtkHardwareWindow.h"
#include "vtkRenderWindow.h"
#include "vtkSDL2RenderWindowInteractor.h"
#include <SDL.h>
#ifdef __EMSCRIPTEN__
#include "emscripten.h"
#endif
#include "vtkStringArray.h"
#include "vtkActor.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkSDL2RenderWindowInteractor);
//------------------------------------------------------------------------------
// Construct object so that light follows camera motion.
vtkSDL2RenderWindowInteractor::vtkSDL2RenderWindowInteractor()
: StartedMessageLoop(false)
{
}
//------------------------------------------------------------------------------
vtkSDL2RenderWindowInteractor::~vtkSDL2RenderWindowInteractor() {}
//------------------------------------------------------------------------------
void vtkSDL2RenderWindowInteractor::ProcessEvents()
{
// No need to do anything if this is a 'mapped' interactor
if (!this->Enabled)
{
return;
}
SDL_Event event;
std::vector<SDL_Event> events;
// SDL generates continuous sequences of mouse motion events per frame,
// let use only last event of each sequence
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEMOTION && !events.empty() && events.back().type == event.type)
{
events.back() = event;
}
else
{
events.push_back(event);
}
}
for (SDL_Event ev : events)
{
if (this->ProcessEvent(&ev))
{
break;
}
}
}
bool vtkSDL2RenderWindowInteractor::ProcessEvent(void* arg)
{
SDL_Event* event = reinterpret_cast<SDL_Event*>(arg);
SDL_Keymod modstates = SDL_GetModState();
int alt = modstates & (KMOD_LALT | KMOD_RALT) ? 1 : 0;
int shift = modstates & (KMOD_LSHIFT | KMOD_RSHIFT) ? 1 : 0;
int ctrl = modstates & (KMOD_LCTRL | KMOD_RCTRL) ? 1 : 0;
switch (event->type)
{
case SDL_QUIT:
{
return true;
}
break;
case SDL_USEREVENT:
{
if (event->user.data1 == reinterpret_cast<void*>(vtkCommand::TimerEvent))
{
int tid = static_cast<int>(reinterpret_cast<int64_t>(event->user.data2));
auto iter = this->VTKToPlatformTimerMap.find(tid);
if (iter != this->VTKToPlatformTimerMap.end())
{
this->InvokeEvent(vtkCommand::TimerEvent, (void*)&tid);
int ptid = (*iter).second;
// Here we deal with one-shot versus repeating timers
if (this->IsOneShotTimer(tid))
{
SDL_RemoveTimer(ptid);
}
}
}
}
break;
case SDL_KEYDOWN:
{
// simplified, not fully implemented
std::string keyname = SDL_GetKeyName(event->key.keysym.sym);
if (keyname.size())
{
this->SetKeyEventInformation(ctrl, shift, keyname[0], event->key.repeat, keyname.c_str());
this->SetAltKey(alt);
this->InvokeEvent(vtkCommand::KeyPressEvent, nullptr);
}
}
break;
case SDL_KEYUP:
{
// simplified, not fully implemented
std::string keyname = SDL_GetKeyName(event->key.keysym.sym);
if (keyname.size())
{
this->SetKeyEventInformation(ctrl, shift, keyname[0], event->key.repeat, keyname.c_str());
this->SetAltKey(alt);
this->InvokeEvent(vtkCommand::KeyReleaseEvent, nullptr);
this->InvokeEvent(vtkCommand::CharEvent, nullptr);
}
}
break;
case SDL_MOUSEMOTION:
{
this->SetEventInformationFlipY(event->motion.x, event->motion.y, ctrl, shift);
this->SetAltKey(alt);
this->InvokeEvent(vtkCommand::MouseMoveEvent, nullptr);
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
this->SetEventInformationFlipY(event->button.x, event->button.y, ctrl, shift);
this->SetAltKey(alt);
int ev = -1;
switch (event->button.button)
{
case SDL_BUTTON_LEFT:
ev = event->button.state == SDL_PRESSED ? vtkCommand::LeftButtonPressEvent
: vtkCommand::LeftButtonReleaseEvent;
break;
case SDL_BUTTON_MIDDLE:
ev = event->button.state == SDL_PRESSED ? vtkCommand::MiddleButtonPressEvent
: vtkCommand::MiddleButtonReleaseEvent;
break;
case SDL_BUTTON_RIGHT:
ev = event->button.state == SDL_PRESSED ? vtkCommand::RightButtonPressEvent
: vtkCommand::RightButtonReleaseEvent;
break;
}
if (ev >= 0)
{
this->InvokeEvent(ev, nullptr);
}
}
break;
case SDL_MOUSEWHEEL:
{
this->SetControlKey(ctrl);
this->SetShiftKey(shift);
this->SetAltKey(alt);
int ev = event->wheel.y > 0 ? vtkCommand::MouseWheelForwardEvent
: vtkCommand::MouseWheelBackwardEvent;
this->InvokeEvent(ev, nullptr);
}
break;
case SDL_WINDOWEVENT:
{
switch (event->window.event)
{
case SDL_WINDOWEVENT_SIZE_CHANGED:
{
this->UpdateSize(event->window.data1, event->window.data2);
this->Render();
}
break;
case SDL_WINDOWEVENT_CLOSE:
{
this->TerminateApp();
}
break;
}
}
break;
}
return false;
}
namespace
{
void mainLoopCallback(void* arg)
{
vtkSDL2RenderWindowInteractor* iren = static_cast<vtkSDL2RenderWindowInteractor*>(arg);
iren->ProcessEvents();
}
}
//------------------------------------------------------------------------------
void vtkSDL2RenderWindowInteractor::StartEventLoop()
{
// No need to do anything if this is a 'mapped' interactor
if (!this->Enabled)
{
return;
}
this->StartedMessageLoop = 1;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg(&mainLoopCallback, (void*)this, 0, 1);
#else
while (!this->Done)
{
this->ProcessEvents();
}
#endif
}
//------------------------------------------------------------------------------
void vtkSDL2RenderWindowInteractor::AddEventHandler()
{
// No need to do anything if this is a 'mapped' interactor
if (!this->Enabled)
{
return;
}
this->StartedMessageLoop = 1;
this->Done = false;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg(&mainLoopCallback, (void*)this, 0, 0);
#endif
}
//------------------------------------------------------------------------------
// Begin processing keyboard strokes.
void vtkSDL2RenderWindowInteractor::Initialize()
{
vtkRenderWindow* ren;
int* size;
// make sure we have a RenderWindow and camera
if (!this->RenderWindow)
{
vtkErrorMacro(<< "No renderer defined!");
return;
}
if (this->Initialized)
{
return;
}
this->Initialized = 1;
// get the info we need from the RenderingWindow
ren = this->RenderWindow;
ren->Start();
ren->End();
size = ren->GetSize();
ren->GetPosition();
this->Enable();
this->Size[0] = size[0];
this->Size[1] = size[1];
}
//------------------------------------------------------------------------------
void vtkSDL2RenderWindowInteractor::TerminateApp(void)
{
this->Done = true;
#ifdef __EMSCRIPTEN__
// Only post a quit message if Start was called...
if (this->StartedMessageLoop)
{
emscripten_cancel_main_loop();
}
#endif
}
namespace
{
Uint32 timerCallback(Uint32 interval, void* param)
{
SDL_Event event;
SDL_UserEvent userevent;
userevent.type = SDL_USEREVENT;
userevent.code = 0;
userevent.data1 = reinterpret_cast<void*>(vtkCommand::TimerEvent);
userevent.data2 = param;
event.type = SDL_USEREVENT;
event.user = userevent;
SDL_PushEvent(&event);
return (interval);
}
}
//------------------------------------------------------------------------------
int vtkSDL2RenderWindowInteractor::InternalCreateTimer(
int timerId, int vtkNotUsed(timerType), unsigned long duration)
{
auto result = SDL_AddTimer(duration, timerCallback, reinterpret_cast<void*>(timerId));
this->VTKToPlatformTimerMap[timerId] = result;
return result;
}
//------------------------------------------------------------------------------
int vtkSDL2RenderWindowInteractor::InternalDestroyTimer(int platformTimerId)
{
int tid = this->GetVTKTimerId(platformTimerId);
auto i = this->VTKToPlatformTimerMap.find(tid);
this->VTKToPlatformTimerMap.erase(i);
return SDL_RemoveTimer(platformTimerId);
}
//------------------------------------------------------------------------------
void vtkSDL2RenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "StartedMessageLoop: " << this->StartedMessageLoop << endl;
}
//------------------------------------------------------------------------------
void vtkSDL2RenderWindowInteractor::ExitCallback()
{
if (this->HasObserver(vtkCommand::ExitEvent))
{
this->InvokeEvent(vtkCommand::ExitEvent, nullptr);
}
this->TerminateApp();
}