src/gl/partrenderer.cpp

changeset 232
8efa3a33172e
parent 231
a9bf6bab5ea2
child 234
87ee9824210b
equal deleted inserted replaced
231:a9bf6bab5ea2 232:8efa3a33172e
41 model{model}, 41 model{model},
42 documents{documents}, 42 documents{documents},
43 colorTable{colorTable} 43 colorTable{colorTable}
44 { 44 {
45 this->setMouseTracking(true); 45 this->setMouseTracking(true);
46 this->setFocusPolicy(Qt::WheelFocus);
46 connect(model, &Model::rowsInserted, [&]{ 47 connect(model, &Model::rowsInserted, [&]{
47 this->needBuild = true; 48 this->needBuild = true;
48 }); 49 });
49 connect(model, &Model::rowsRemoved, [&]{ this->needBuild = true; }); 50 connect(model, &Model::rowsRemoved, [&]{ this->needBuild = true; });
50 const auto updateLayerMvpMatrix = [this]{ 51 const auto updateLayerMvpMatrix = [this]{
292 } 293 }
293 } 294 }
294 295
295 void PartRenderer::mouseMoveEvent(QMouseEvent* event) 296 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
296 { 297 {
297 const bool left = event->buttons() & Qt::LeftButton; 298 if (not this->frozen) {
298 const QPoint move = event->pos() - this->lastMousePosition; 299 const bool left = event->buttons() & Qt::LeftButton;
299 this->totalMouseMove += move.manhattanLength(); 300 const QPoint move = event->pos() - this->lastMousePosition;
300 if (left and not move.isNull()) 301 this->totalMouseMove += move.manhattanLength();
301 { 302 if (left and not move.isNull())
302 // q_x is the rotation of the brick along the vertical y-axis, because turning the 303 {
303 // vertical axis causes horizontal (=x) rotation. Likewise q_y is the rotation of the 304 // q_x is the rotation of the brick along the vertical y-axis, because turning the
304 // brick along the horizontal x-axis, which causes vertical rotation. 305 // vertical axis causes horizontal (=x) rotation. Likewise q_y is the rotation of the
305 const auto scalar = 0.006f; 306 // brick along the horizontal x-axis, which causes vertical rotation.
306 const float move_x = static_cast<float>(move.x()); 307 const auto scalar = 0.006f;
307 const float move_y = static_cast<float>(move.y()); 308 const float move_x = static_cast<float>(move.x());
308 const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0}); 309 const float move_y = static_cast<float>(move.y());
309 const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0}); 310 const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0});
310 this->modelQuaternion = q_x * q_y * this->modelQuaternion; 311 const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0});
311 this->updateModelMatrix(); 312 this->modelQuaternion = q_x * q_y * this->modelQuaternion;
312 } 313 this->updateModelMatrix();
313 this->lastMousePosition = event->pos(); 314 }
314 for (RenderLayer* layer : this->activeRenderLayers) { 315 this->lastMousePosition = event->pos();
315 layer->mouseMoved(event); 316 for (RenderLayer* layer : this->activeRenderLayers) {
316 } 317 layer->mouseMoved(event);
317 this->update(); 318 }
319 this->update();
320 }
318 } 321 }
319 322
320 void PartRenderer::mousePressEvent(QMouseEvent* event) 323 void PartRenderer::mousePressEvent(QMouseEvent* event)
321 { 324 {
322 this->totalMouseMove = 0; 325 if (not this->frozen) {
323 this->lastMousePosition = event->pos(); 326 this->totalMouseMove = 0;
327 this->lastMousePosition = event->pos();
328 }
324 } 329 }
325 330
326 void PartRenderer::mouseReleaseEvent(QMouseEvent* event) 331 void PartRenderer::mouseReleaseEvent(QMouseEvent* event)
327 { 332 {
328 if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0) 333 if (not frozen and this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
329 { 334 {
330 for (RenderLayer* layer : this->activeRenderLayers) { 335 for (RenderLayer* layer : this->activeRenderLayers) {
331 layer->mouseClick(event); 336 layer->mouseClick(event);
332 } 337 }
333 this->update(); 338 this->update();
334 } 339 }
335 } 340 }
336 341
342 void PartRenderer::keyReleaseEvent(QKeyEvent* event)
343 {
344 if (event->key() == Qt::Key_Pause) {
345 this->frozen = not this->frozen;
346 }
347 }
348
337 void PartRenderer::wheelEvent(QWheelEvent* event) 349 void PartRenderer::wheelEvent(QWheelEvent* event)
338 { 350 {
339 static constexpr double WHEEL_STEP = 1 / 1000.0; 351 if (not this->frozen) {
340 const double move = (-event->angleDelta().y()) * WHEEL_STEP; 352 static constexpr double WHEEL_STEP = 1 / 1000.0;
341 this->zoom = std::clamp(this->zoom + move, MIN_ZOOM, MAX_ZOOM); 353 const double move = (-event->angleDelta().y()) * WHEEL_STEP;
342 this->updateViewMatrix(); 354 this->zoom = std::clamp(this->zoom + move, MIN_ZOOM, MAX_ZOOM);
343 this->update(); 355 this->updateViewMatrix();
356 this->update();
357 }
344 } 358 }
345 359
346 void PartRenderer::addRenderLayer(RenderLayer* layer) 360 void PartRenderer::addRenderLayer(RenderLayer* layer)
347 { 361 {
348 this->activeRenderLayers.push_back(layer); 362 this->activeRenderLayers.push_back(layer);

mercurial