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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkRemoteInteractionAdapter.h"
#include "vtkCommand.h"
#include "vtkLogger.h"
#include "vtkObjectFactory.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include <vtk_nlohmannjson.h>
#include VTK_NLOHMANN_JSON(json.hpp)
#include <unordered_map>
VTK_ABI_NAMESPACE_BEGIN
enum
{
WheelEvent = vtkCommand::UserEvent + 3000,
};
// map vtk-js event codes to vtkCommand events , for the ones that I didn't found a clear
// correspondence I used vtkCommand::NoEvent and left them unhandled. Taken from
// https://github.com/Kitware/vtk-js/blob/master/Sources/Rendering/Core/RenderWindowInteractor/index.js
using enum_type = int;
// clang-format off
const std::unordered_map< std::string, enum_type > EVENT_MAP {
{"StartAnimation" ,vtkCommand::NoEvent},
{"Animation" ,vtkCommand::NoEvent},
{"EndAnimation" ,vtkCommand::NoEvent},
{"PointerEnter" ,vtkCommand::EnterEvent},
{"PointerLeave" ,vtkCommand::LeaveEvent},
{"MouseEnter" ,vtkCommand::EnterEvent},
{"MouseLeave" ,vtkCommand::LeaveEvent},
{"StartMouseMove" ,vtkCommand::NoEvent},
{"MouseMove" ,vtkCommand::MouseMoveEvent},
{"EndMouseMove" ,vtkCommand::NoEvent},
{"LeftButtonPress" ,vtkCommand::LeftButtonPressEvent},
{"LeftButtonRelease" ,vtkCommand::LeftButtonReleaseEvent},
{"MiddleButtonPress" ,vtkCommand::MiddleButtonPressEvent},
{"MiddleButtonRelease" ,vtkCommand::MiddleButtonReleaseEvent},
{"RightButtonPress" ,vtkCommand::RightButtonPressEvent},
{"RightButtonRelease" ,vtkCommand::RightButtonReleaseEvent},
{"KeyPress" ,vtkCommand::KeyPressEvent},
{"KeyDown" ,vtkCommand::KeyPressEvent},
{"KeyUp" ,vtkCommand::KeyReleaseEvent},
{"StartMouseWheel" ,vtkCommand::NoEvent},
{"MouseWheel" ,WheelEvent},
{"EndMouseWheel" ,vtkCommand::NoEvent},
{"StartPinch" ,vtkCommand::StartPinchEvent},
{"Pinch" ,vtkCommand::PinchEvent},
{"EndPinch" ,vtkCommand::EndPinchEvent},
{"StartPan" ,vtkCommand::StartPanEvent},
{"Pan" ,vtkCommand::PanEvent},
{"EndPan" ,vtkCommand::EndPanEvent},
{"StartRotate" ,vtkCommand::StartRotateEvent},
{"Rotate" ,vtkCommand::RotateEvent},
{"EndRotate" ,vtkCommand::RenderEvent},
{"Button3D" ,vtkCommand::NoEvent},
{"Move3D" ,vtkCommand::NoEvent},
{"StartPointerLock" ,vtkCommand::NoEvent},
{"EndPointerLock" ,vtkCommand::NoEvent},
{"StartInteraction" ,vtkCommand::NoEvent},
{"Interaction" ,vtkCommand::NoEvent},
{"EndInteraction" ,vtkCommand::NoEvent},
{"AnimationFrameRateUpdate" ,vtkCommand::NoEvent}
};
// clang-format on
vtkSetObjectImplementationMacro(vtkRemoteInteractionAdapter, Interactor, vtkRenderWindowInteractor);
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkRemoteInteractionAdapter);
//----------------------------------------------------------------------------
vtkRemoteInteractionAdapter::vtkRemoteInteractionAdapter() = default;
//----------------------------------------------------------------------------
vtkRemoteInteractionAdapter::~vtkRemoteInteractionAdapter()
{
this->SetInteractor(nullptr);
}
//----------------------------------------------------------------------------
void vtkRemoteInteractionAdapter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
// based on QVTKInteractorAdapter::ProcessEvent(QEvent* e, vtkRenderWindowInteractor* iren)
bool vtkRemoteInteractionAdapter::ProcessEvent(vtkRenderWindowInteractor* iren,
const std::string& event_str, double devicePixelRatio, double devicePixelRatioTolerance)
{
if (!iren)
{
vtkLogF(ERROR, "Null interactor passed");
return false;
}
// the following events only happen if the interactor is enabled
if (!iren->GetEnabled())
{
return false;
}
try
{
nlohmann::json event = nlohmann::json::parse(event_str);
const std::string& type = event.at("type");
vtkLogF(TRACE, "event %s", event.dump(1).c_str());
const int eventType = EVENT_MAP.at(type);
switch (eventType)
{
case vtkCommand::EnterEvent:
case vtkCommand::LeaveEvent:
iren->InvokeEvent(eventType, (void*)&event);
break;
case vtkCommand::MouseMoveEvent:
case vtkCommand::LeftButtonPressEvent:
case vtkCommand::LeftButtonReleaseEvent:
case vtkCommand::RightButtonPressEvent:
case vtkCommand::RightButtonReleaseEvent:
case vtkCommand::MiddleButtonPressEvent:
case vtkCommand::MiddleButtonReleaseEvent:
{
const int x =
(event.at("x").get<double>() / event.at("w").get<double>() * devicePixelRatio +
devicePixelRatioTolerance) *
iren->GetRenderWindow()->GetSize()[0];
const int y =
(event.at("y").get<double>() / event.at("h").get<double>() * devicePixelRatio +
devicePixelRatioTolerance) *
iren->GetRenderWindow()->GetSize()[1];
const int ctrlKeyPressed = event.at("ctrlKey").get<int>();
const int altKeyPressed = event.at("altKey").get<int>();
const int shiftKeyPressed = event.at("shiftKey").get<int>();
iren->SetEventInformation(x, y, ctrlKeyPressed, shiftKeyPressed);
iren->SetAltKey(altKeyPressed);
iren->InvokeEvent(eventType, (void*)&event);
break;
}
case vtkCommand::KeyPressEvent:
case vtkCommand::KeyReleaseEvent:
{
const int ctrlKeyPressed = event.at("controlKey").get<int>();
const int altKeyPressed = event.at("altKey").get<int>();
const int shiftKeyPressed = event.at("shiftKey").get<int>();
const char asciiCode = event.at("keyCode").get<int>();
const std::string& key = event.at("key");
iren->SetKeyEventInformation(ctrlKeyPressed, shiftKeyPressed, asciiCode, 0, key.c_str());
iren->SetAltKey(altKeyPressed);
iren->InvokeEvent(eventType);
if (eventType == vtkCommand::KeyPressEvent && asciiCode != '\0') // TODO check comparson
{
iren->InvokeEvent(vtkCommand::CharEvent, (void*)&event);
}
break;
}
case WheelEvent:
{
const int x =
(event.at("x").get<double>() / event.at("w").get<double>() * devicePixelRatio +
devicePixelRatioTolerance) *
iren->GetRenderWindow()->GetSize()[0];
const int y =
(event.at("y").get<double>() / event.at("h").get<double>() * devicePixelRatio +
devicePixelRatioTolerance) *
iren->GetRenderWindow()->GetSize()[1];
const int ctrlKeyPressed = event.at("ctrlKey").get<int>();
const int altKeyPressed = event.at("altKey").get<int>();
const int shiftKeyPressed = event.at("shiftKey").get<int>();
iren->SetEventInformation(x, y, ctrlKeyPressed, shiftKeyPressed);
iren->SetAltKey(altKeyPressed);
static double accumulatedDelta = 0;
const double verticalDelta = event.at("spinY").get<double>();
accumulatedDelta += verticalDelta;
const double threshold = 1.0; // in vtk-js the value comes normalized
// invoke vtk event when accumulated delta passes the threshold
// Note: in javascript a forward (away from the user MouseWheelEvent is
// indicated with a negative value in contrast to Qt.
if (accumulatedDelta <= -threshold && verticalDelta != 0.0)
{
iren->InvokeEvent(vtkCommand::MouseWheelForwardEvent, (void*)&event);
accumulatedDelta = 0;
}
else if (accumulatedDelta >= threshold && verticalDelta != 0.0)
{
iren->InvokeEvent(vtkCommand::MouseWheelBackwardEvent, (void*)&event);
accumulatedDelta = 0;
}
break;
}
case vtkCommand::StartPinchEvent:
case vtkCommand::EndPinchEvent:
case vtkCommand::PinchEvent:
case vtkCommand::StartPanEvent:
case vtkCommand::EndPanEvent:
case vtkCommand::PanEvent:
case vtkCommand::StartRotateEvent:
case vtkCommand::EndRotateEvent:
case vtkCommand::RotateEvent:
{
// Store event information to restore after gesture is completed
int eventPosition[2];
iren->GetEventPosition(eventPosition);
int lastEventPosition[2];
iren->GetLastEventPosition(lastEventPosition);
// get center of positions for event
int position[2] = { 0, 0 };
for (const auto& item : event.at("positions"))
{
position[0] += item.at("x").get<double>() / event.at("w").get<double>();
position[1] += item.at("y").get<double>() / event.at("h").get<double>();
}
position[0] /= static_cast<int>(event.at("positions").size());
position[1] /= static_cast<int>(event.at("positions").size());
iren->SetEventInformation(position[0] * devicePixelRatio * devicePixelRatioTolerance,
position[1] * devicePixelRatio * devicePixelRatioTolerance);
if (eventType == vtkCommand::StartPinchEvent || eventType == vtkCommand::EndPinchEvent ||
eventType == vtkCommand::PinchEvent)
{
const double factor = event.at("factor").get<double>();
iren->SetScale(1.0);
iren->SetScale(factor);
}
else if (eventType == vtkCommand::StartPanEvent || eventType == vtkCommand::EndPanEvent ||
eventType == vtkCommand::PanEvent)
{
double translation[2] = { event.at("translation").at(0).get<double>(),
event.at("translation").at(1).get<double>() };
iren->SetTranslation(translation);
}
else if (eventType == vtkCommand::StartRotateEvent ||
eventType == vtkCommand::EndRotateEvent || eventType == vtkCommand::RotateEvent)
{
const double rotation = event.at("rotation").get<double>();
iren->SetRotation(rotation);
}
else
{
vtkLogF(ERROR, "Unexpected Event Type");
return false;
}
iren->InvokeEvent(eventType, (void*)&event);
}
break;
case vtkCommand::InteractionEvent:
case vtkCommand::StartInteractionEvent:
case vtkCommand::EndInteractionEvent:
case vtkCommand::NoEvent:
// nothing to do
break;
default:
vtkLogF(WARNING, "Unhandled event: %s", type.c_str());
break;
}
return true;
}
catch (std::out_of_range& e)
{
vtkLogF(ERROR, "Skipping Event: Unknown event type \n%s", e.what());
return false;
}
catch (nlohmann::json::out_of_range& e)
{
vtkLogF(ERROR, "Skipping Event \n%s", e.what());
return false;
}
}
//----------------------------------------------------------------------------
bool vtkRemoteInteractionAdapter::ProcessEvent(const std::string& event_str)
{
return ProcessEvent(
this->Interactor, event_str, this->DevicePixelRatio, this->DevicePixelRatioTolerance);
}
VTK_ABI_NAMESPACE_END
|