| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2015 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include <QApplication> |
| 17 | #include <QGraphicsItem> |
| 18 | #include <QMouseEvent> |
| 19 | #include <QStatusBar> |
| 20 | #include <QString> |
| 21 | #include <QTransform> |
| 22 | |
| 23 | #include "editorview.hpp" |
| 24 | |
| 25 | #include "draganddropstore.hpp" |
| 26 | #include "editormode.hpp" |
| 27 | #include "mediator.hpp" |
| 28 | #include "object.hpp" |
| 29 | #include "objectfactory.hpp" |
| 30 | #include "objectmodelloader.hpp" |
| 31 | #include "rotatedialog.hpp" |
| 32 | #include "targetnode.hpp" |
| 33 | #include "targetnodesizedlg.hpp" |
| 34 | #include "trackdata.hpp" |
| 35 | #include "tracktile.hpp" |
| 36 | |
| 37 | #include <cassert> |
| 38 | #include <cstdlib> |
| 39 | |
| 40 | EditorView::EditorView(Mediator & mediator) |
| 41 | : m_mediator(mediator) |
| 42 | { |
| 43 | createTileContextMenuActions(); |
| 44 | createObjectContextMenuActions(); |
| 45 | createTargetNodeContextMenuActions(); |
| 46 | } |
| 47 | |
| 48 | void EditorView::updateSceneRect() |
| 49 | { |
| 50 | const QRectF newSceneRect( |
| 51 | 0, |
| 52 | 0, |
| 53 | m_mediator.cols() * TrackTile::width(), |
| 54 | m_mediator.rows() * TrackTile::height()); |
| 55 | |
| 56 | setSceneRect(newSceneRect); |
| 57 | |
| 58 | assert(scene()); |
| 59 | scene()->setSceneRect(newSceneRect); |
| 60 | } |
| 61 | |
| 62 | void EditorView::mouseMoveEvent(QMouseEvent * event) |
| 63 | { |
| 64 | if (scene()) |
| 65 | { |
| 66 | const QPointF mappedPos = mapToScene(event->pos()); |
| 67 | if (auto tile = dynamic_cast<TrackTile *>(scene()->itemAt(mappedPos, QTransform()))) |
| 68 | { |
| 69 | tile->setActive(true); |
| 70 | } |
| 71 | |
| 72 | // Tile drag'n'drop active? |
| 73 | if (auto sourceTile = m_mediator.dadStore().dragAndDropSourceTile()) |
| 74 | { |
| 75 | sourceTile->setPos(mappedPos); |
| 76 | } |
| 77 | // Object drag'n'drop active? |
| 78 | else if (auto object = m_mediator.dadStore().dragAndDropObject()) |
| 79 | { |
| 80 | object->setLocation(mappedPos); |
| 81 | } |
| 82 | // Target node drag'n'drop active? |
| 83 | else if (auto tnode = m_mediator.dadStore().dragAndDropTargetNode()) |
| 84 | { |
| 85 | tnode->setLocation(mappedPos); |
| 86 | } |
| 87 | |
| 88 | m_mediator.updateCoordinates(mappedPos); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void EditorView::createTileContextMenuActions() |
| 93 | { |
| 94 | const QChar degreeSign(176); |
| 95 | const QString dummy1(QString(QWidget::tr("Rotate 90")) + degreeSign + QWidget::tr(" CW..")); |
| 96 | |
| 97 | auto rotate90CW = new QAction(dummy1, &m_tileContextMenu); |
| 98 | QObject::connect(rotate90CW, &QAction::triggered, [this]() { |
| 99 | if (TrackTile * tile = |
| 100 | dynamic_cast<TrackTile *>(scene()->itemAt(mapToScene(m_clickedPos), QTransform()))) |
| 101 | { |
| 102 | m_mediator.saveUndoPoint(); |
| 103 | tile->rotate90CW(); |
| 104 | } |
| 105 | }); |
| 106 | |
| 107 | const QString dummy2(QString(QWidget::tr("Rotate 90")) + degreeSign + QWidget::tr(" CCW..")); |
| 108 | |
| 109 | auto rotate90CCW = new QAction(dummy2, &m_tileContextMenu); |
| 110 | QObject::connect(rotate90CCW, &QAction::triggered, [this]() { |
| 111 | if (auto tile = |
| 112 | dynamic_cast<TrackTile *>(scene()->itemAt(mapToScene(m_clickedPos), QTransform()))) |
| 113 | { |
| 114 | m_mediator.saveUndoPoint(); |
| 115 | tile->rotate90CCW(); |
| 116 | } |
| 117 | }); |
| 118 | |
| 119 | m_clearComputerHint = new QAction(QWidget::tr("Clear computer hint"), &m_tileContextMenu); |
| 120 | QObject::connect(m_clearComputerHint, &QAction::triggered, [this]() { |
| 121 | m_mediator.saveUndoPoint(); |
| 122 | setComputerHint(TrackTileBase::ComputerHint::None); |
| 123 | }); |
| 124 | |
| 125 | m_setComputerHintBrakeHard = new QAction( |
| 126 | QWidget::tr("Set computer hint 'brake hard'.."), &m_tileContextMenu); |
| 127 | QObject::connect(m_setComputerHintBrakeHard, &QAction::triggered, [this]() { |
| 128 | m_mediator.saveUndoPoint(); |
| 129 | setComputerHint(TrackTileBase::ComputerHint::BrakeHard); |
| 130 | }); |
| 131 | |
| 132 | m_setComputerHintBrake = new QAction( |
| 133 | QWidget::tr("Set computer hint 'brake'.."), &m_tileContextMenu); |
| 134 | QObject::connect(m_setComputerHintBrake, &QAction::triggered, [this]() { |
| 135 | m_mediator.saveUndoPoint(); |
| 136 | setComputerHint(TrackTileBase::ComputerHint::Brake); |
| 137 | }); |
| 138 | |
| 139 | m_excludeFromMinimap = new QAction( |
| 140 | QWidget::tr("Exclude from minimap"), &m_tileContextMenu); |
| 141 | m_excludeFromMinimap->setCheckable(true); |
| 142 | QObject::connect(m_excludeFromMinimap, &QAction::changed, [this]() { |
| 143 | if (auto tile = dynamic_cast<TrackTile *>(scene()->itemAt(mapToScene(this->m_clickedPos), QTransform()))) |
| 144 | { |
| 145 | m_mediator.saveUndoPoint(); |
| 146 | tile->setExcludeFromMinimap(this->m_excludeFromMinimap->isChecked()); |
| 147 | } |
| 148 | }); |
| 149 | |
| 150 | auto insertRowBefore = new QAction( |
| 151 | QWidget::tr("Insert row before.."), &m_tileContextMenu); |
| 152 | QObject::connect(insertRowBefore, &QAction::triggered, [this]() { |
| 153 | m_mediator.saveUndoPoint(); |
| 154 | m_mediator.insertRowBefore(); |
| 155 | updateSceneRect(); |
| 156 | update(); |
| 157 | }); |
| 158 | |
| 159 | auto insertRowAfter = new QAction( |
| 160 | QWidget::tr("Insert row after.."), &m_tileContextMenu); |
| 161 | QObject::connect(insertRowAfter, &QAction::triggered, [this]() { |
| 162 | m_mediator.saveUndoPoint(); |
| 163 | m_mediator.insertRowAfter(); |
| 164 | updateSceneRect(); |
| 165 | update(); |
| 166 | }); |
| 167 | |
| 168 | m_deleteRow = new QAction( |
| 169 | QWidget::tr("Delete row.."), &m_tileContextMenu); |
| 170 | QObject::connect(m_deleteRow, &QAction::triggered, [this]() { |
| 171 | m_mediator.saveUndoPoint(); |
| 172 | m_mediator.deleteRow(); |
| 173 | updateSceneRect(); |
| 174 | update(); |
| 175 | }); |
| 176 | |
| 177 | auto insertColBefore = new QAction( |
| 178 | QWidget::tr("Insert column before.."), &m_tileContextMenu); |
| 179 | QObject::connect(insertColBefore, &QAction::triggered, [this]() { |
| 180 | m_mediator.saveUndoPoint(); |
| 181 | m_mediator.insertColumnBefore(); |
| 182 | updateSceneRect(); |
| 183 | update(); |
| 184 | }); |
| 185 | |
| 186 | auto insertColAfter = new QAction( |
| 187 | QWidget::tr("Insert column after.."), &m_tileContextMenu); |
| 188 | QObject::connect(insertColAfter, &QAction::triggered, [this]() { |
| 189 | m_mediator.saveUndoPoint(); |
| 190 | m_mediator.insertColumnAfter(); |
| 191 | updateSceneRect(); |
| 192 | update(); |
| 193 | }); |
| 194 | |
| 195 | m_deleteCol = new QAction( |
| 196 | QWidget::tr("Delete column.."), &m_tileContextMenu); |
| 197 | QObject::connect(m_deleteCol, &QAction::triggered, [this]() { |
| 198 | m_mediator.saveUndoPoint(); |
| 199 | m_mediator.deleteColumn(); |
| 200 | updateSceneRect(); |
| 201 | update(); |
| 202 | }); |
| 203 | |
| 204 | // Populate the menu |
| 205 | m_tileContextMenu.addAction(rotate90CW); |
| 206 | m_tileContextMenu.addAction(rotate90CCW); |
| 207 | m_tileContextMenu.addSeparator(); |
| 208 | m_tileContextMenu.addAction(m_clearComputerHint); |
| 209 | m_tileContextMenu.addAction(m_setComputerHintBrakeHard); |
| 210 | m_tileContextMenu.addAction(m_setComputerHintBrake); |
| 211 | m_tileContextMenu.addSeparator(); |
| 212 | m_tileContextMenu.addAction(m_excludeFromMinimap); |
| 213 | m_tileContextMenu.addSeparator(); |
| 214 | m_tileContextMenu.addAction(insertRowBefore); |
| 215 | m_tileContextMenu.addAction(insertRowAfter); |
| 216 | m_tileContextMenu.addAction(m_deleteRow); |
| 217 | m_tileContextMenu.addSeparator(); |
| 218 | m_tileContextMenu.addAction(insertColBefore); |
| 219 | m_tileContextMenu.addAction(insertColAfter); |
| 220 | m_tileContextMenu.addAction(m_deleteCol); |
| 221 | } |
| 222 | |
| 223 | void EditorView::createObjectContextMenuActions() |
| 224 | { |
| 225 | const QString dummy1(QString(QWidget::tr("Rotate.."))); |
| 226 | auto rotate = new QAction(dummy1, &m_tileContextMenu); |
| 227 | QObject::connect(rotate, &QAction::triggered, [this]() { |
| 228 | RotateDialog dialog; |
| 229 | if (dialog.exec() == QDialog::Accepted) |
| 230 | { |
| 231 | if (auto object = m_mediator.selectedObject()) |
| 232 | { |
| 233 | m_mediator.saveUndoPoint(); |
| 234 | |
| 235 | object->setRotation(static_cast<int>(dialog.angle() + object->rotation()) % 360); |
| 236 | } |
| 237 | } |
| 238 | }); |
| 239 | |
| 240 | const QString dummy2(QString(QWidget::tr("Force stationary"))); |
| 241 | m_forceStationaryAction = new QAction(dummy2, &m_tileContextMenu); |
| 242 | m_forceStationaryAction->setCheckable(true); |
| 243 | QObject::connect(m_forceStationaryAction, &QAction::changed, [this]() { |
| 244 | if (auto object = m_mediator.selectedObject()) |
| 245 | { |
| 246 | m_mediator.saveUndoPoint(); |
| 247 | object->setForceStationary(m_forceStationaryAction->isChecked()); |
| 248 | } |
| 249 | }); |
| 250 | |
| 251 | // Populate the menu |
| 252 | m_objectContextMenu.addAction(rotate); |
| 253 | m_objectContextMenu.addSeparator(); |
| 254 | m_objectContextMenu.addAction(m_forceStationaryAction); |
| 255 | } |
| 256 | |
| 257 | void EditorView::createTargetNodeContextMenuActions() |
| 258 | { |
| 259 | const QString dummy1(QString(QWidget::tr("Set size.."))); |
| 260 | auto setSize = new QAction(dummy1, &m_targetNodeContextMenu); |
| 261 | QObject::connect(setSize, &QAction::triggered, [this]() { |
| 262 | if (auto tnode = m_mediator.selectedTargetNode()) |
| 263 | { |
| 264 | TargetNodeSizeDlg dialog(tnode->size()); |
| 265 | if (dialog.exec() == QDialog::Accepted) |
| 266 | { |
| 267 | m_mediator.saveUndoPoint(); |
| 268 | |
| 269 | tnode->setSize(dialog.targetNodeSize()); |
| 270 | } |
| 271 | } |
| 272 | }); |
| 273 | |
| 274 | // Populate the menu |
| 275 | m_targetNodeContextMenu.addAction(setSize); |
| 276 | } |
| 277 | |
| 278 | void EditorView::mousePressEvent(QMouseEvent * event) |
| 279 | { |
| 280 | if (scene()) |
| 281 | { |
| 282 | m_clickedPos = event->pos(); |
| 283 | m_clickedScenePos = mapToScene(m_clickedPos); |
| 284 | |
| 285 | // Fetch all items at the location |
| 286 | QList<QGraphicsItem *> items = scene()->items(m_clickedScenePos, Qt::IntersectsItemShape, Qt::DescendingOrder); |
| 287 | |
| 288 | if (items.size()) |
| 289 | { |
| 290 | // Check if there's an object at the position and handle that. |
| 291 | // Otherwise it would be difficult to select objects that are surrounded |
| 292 | // by a target node area. |
| 293 | auto iter = items.begin(); |
| 294 | while (iter != items.end()) |
| 295 | { |
| 296 | auto item = *iter; |
| 297 | if (auto object = dynamic_cast<Object *>(item)) |
| 298 | { |
| 299 | handleMousePressEventOnObject(*event, *object); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | iter++; |
| 304 | } |
| 305 | |
| 306 | auto item = *items.begin(); |
| 307 | if (auto tnode = dynamic_cast<TargetNode *>(item)) |
| 308 | { |
| 309 | handleMousePressEventOnTargetNode(*event, *tnode); |
| 310 | } |
| 311 | else if (auto tile = dynamic_cast<TrackTile *>(item)) |
| 312 | { |
| 313 | handleMousePressEventOnTile(*event, *tile); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void EditorView::eraseObjectAtCurrentClickedPos() |
| 320 | { |
| 321 | m_mediator.saveUndoPoint(); |
| 322 | |
| 323 | // Fetch all items at the location |
| 324 | QList<QGraphicsItem *> items = scene()->items( |
| 325 | m_clickedScenePos, Qt::IntersectsItemShape, Qt::DescendingOrder); |
| 326 | |
| 327 | // We need to find the first object to be erased, because |
| 328 | // there might be also overlapping TargetNodes. |
| 329 | for (auto item : items) |
| 330 | { |
| 331 | if (auto object = dynamic_cast<Object *>(item)) |
| 332 | { |
| 333 | m_mediator.removeObject(*object); |
| 334 | m_mediator.setSelectedObject(nullptr); |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void EditorView::addCurrentToolBarObjectToScene() |
| 341 | { |
| 342 | m_mediator.addCurrentToolBarObjectToScene(m_clickedScenePos); |
| 343 | } |
| 344 | |
| 345 | void EditorView::handleMousePressEventOnObject(QMouseEvent & event, Object & object) |
| 346 | { |
| 347 | if (event.button() == Qt::RightButton) |
| 348 | { |
| 349 | handleRightButtonClickOnObject(object); |
| 350 | } |
| 351 | else if (event.button() == Qt::LeftButton) |
| 352 | { |
| 353 | // User is adding an object |
| 354 | if (m_mediator.mode() == EditorMode::AddObject) |
| 355 | { |
| 356 | addCurrentToolBarObjectToScene(); |
| 357 | } |
| 358 | // User is erasing an object |
| 359 | else if (m_mediator.mode() == EditorMode::EraseObject) |
| 360 | { |
| 361 | eraseObjectAtCurrentClickedPos(); |
| 362 | } |
| 363 | // Default actions |
| 364 | else |
| 365 | { |
| 366 | handleLeftButtonClickOnObject(object); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | QWidget::mousePressEvent(&event); |
| 371 | } |
| 372 | |
| 373 | void EditorView::handleMousePressEventOnTargetNode(QMouseEvent & event, TargetNode & tnode) |
| 374 | { |
| 375 | if (event.button() == Qt::RightButton) |
| 376 | { |
| 377 | handleRightButtonClickOnTargetNode(tnode); |
| 378 | } |
| 379 | else if (event.button() == Qt::LeftButton) |
| 380 | { |
| 381 | // User is adding an object |
| 382 | if (m_mediator.mode() == EditorMode::AddObject) |
| 383 | { |
| 384 | addCurrentToolBarObjectToScene(); |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | handleLeftButtonClickOnTargetNode(tnode); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | QWidget::mousePressEvent(&event); |
| 393 | } |
| 394 | |
| 395 | void EditorView::handleMousePressEventOnTile(QMouseEvent & event, TrackTile & tile) |
| 396 | { |
| 397 | tile.setActive(true); |
| 398 | |
| 399 | if (event.button() == Qt::RightButton) |
| 400 | { |
| 401 | handleRightButtonClickOnTile(tile); |
| 402 | } |
| 403 | else if (event.button() == Qt::LeftButton) |
| 404 | { |
| 405 | // User is adding an object |
| 406 | if (m_mediator.mode() == EditorMode::AddObject) |
| 407 | { |
| 408 | addCurrentToolBarObjectToScene(); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | handleLeftButtonClickOnTile(tile); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | QWidget::mousePressEvent(&event); |
| 417 | } |
| 418 | |
| 419 | void EditorView::handleLeftButtonClickOnObject(Object & object) |
| 420 | { |
| 421 | // User is initiating a drag'n'drop |
| 422 | if (m_mediator.mode() == EditorMode::None) |
| 423 | { |
| 424 | m_mediator.saveUndoPoint(); |
| 425 | |
| 426 | object.setZValue(object.zValue() + 1); |
| 427 | m_mediator.dadStore().setDragAndDropObject(&object); |
| 428 | m_mediator.setSelectedObject(&object); |
| 429 | |
| 430 | // Change cursor to the closed hand cursor. |
| 431 | QApplication::setOverrideCursor(QCursor(Qt::ClosedHandCursor)); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void EditorView::handleLeftButtonClickOnTargetNode(TargetNode & tnode) |
| 436 | { |
| 437 | // User is initiating a drag'n'drop |
| 438 | if (m_mediator.mode() == EditorMode::None) |
| 439 | { |
| 440 | m_mediator.saveUndoPoint(); |
| 441 | |
| 442 | tnode.setZValue(tnode.zValue() + 1); |
| 443 | m_mediator.dadStore().setDragAndDropTargetNode(&tnode); |
| 444 | |
| 445 | // Change cursor to the closed hand cursor. |
| 446 | QApplication::setOverrideCursor(QCursor(Qt::ClosedHandCursor)); |
| 447 | } |
| 448 | // It's not possible to make nodes overlap if not handled also here. |
| 449 | else if (m_mediator.mode() == EditorMode::SetRoute) |
| 450 | { |
| 451 | m_mediator.pushNewTargetNodeToRoute(m_clickedScenePos); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | void EditorView::handleLeftButtonClickOnTile(TrackTile & tile) |
| 456 | { |
| 457 | // User is defining the route |
| 458 | if (m_mediator.mode() == EditorMode::SetRoute) |
| 459 | { |
| 460 | m_mediator.pushNewTargetNodeToRoute(m_clickedScenePos); |
| 461 | } |
| 462 | |
| 463 | // User is setting the tile type |
| 464 | else if (m_mediator.mode() == EditorMode::SetTileType) |
| 465 | { |
| 466 | if (QAction * action = m_mediator.currentToolBarAction()) |
| 467 | { |
| 468 | if (const auto modifiers = QApplication::keyboardModifiers(); modifiers & Qt::ControlModifier) |
| 469 | { |
| 470 | if (QString typeToFill = tile.tileType(); typeToFill != action->data().toString()) |
| 471 | { |
| 472 | m_mediator.floodFill(tile, action, typeToFill); |
| 473 | } |
| 474 | } |
| 475 | else |
| 476 | { |
| 477 | changeTileType(tile, action); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | // User is initiating a drag'n'drop |
| 482 | else if (m_mediator.mode() == EditorMode::None) |
| 483 | { |
| 484 | tile.setZValue(tile.zValue() + 1); |
| 485 | m_mediator.dadStore().setDragAndDropSourceTile(&tile); |
| 486 | m_mediator.dadStore().setDragAndDropSourcePos(tile.pos()); |
| 487 | |
| 488 | // Change cursor to the closed hand cursor. |
| 489 | QApplication::setOverrideCursor(QCursor(Qt::ClosedHandCursor)); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void EditorView::openTileContextMenu(TrackTile & tile) |
| 494 | { |
| 495 | // Enable all hint actions by default |
| 496 | m_clearComputerHint->setEnabled(true); |
| 497 | m_setComputerHintBrakeHard->setEnabled(true); |
| 498 | m_setComputerHintBrake->setEnabled(true); |
| 499 | |
| 500 | switch (tile.computerHint()) |
| 501 | { |
| 502 | case TrackTileBase::ComputerHint::None: |
| 503 | m_clearComputerHint->setEnabled(false); |
| 504 | break; |
| 505 | case TrackTileBase::ComputerHint::BrakeHard: |
| 506 | m_setComputerHintBrakeHard->setEnabled(false); |
| 507 | break; |
| 508 | case TrackTileBase::ComputerHint::Brake: |
| 509 | m_setComputerHintBrake->setEnabled(false); |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | m_excludeFromMinimap->setChecked(tile.excludeFromMinimap()); |
| 514 | |
| 515 | m_deleteCol->setEnabled(m_mediator.cols()); |
| 516 | m_deleteRow->setEnabled(m_mediator.rows()); |
| 517 | |
| 518 | const QPoint globalPos = mapToGlobal(m_clickedPos); |
| 519 | m_tileContextMenu.exec(globalPos); |
| 520 | } |
| 521 | |
| 522 | void EditorView::handleRightButtonClickOnTile(TrackTile & tile) |
| 523 | { |
| 524 | openTileContextMenu(tile); |
| 525 | } |
| 526 | |
| 527 | void EditorView::openObjectContextMenu() |
| 528 | { |
| 529 | const QPoint globalPos = mapToGlobal(m_clickedPos); |
| 530 | m_objectContextMenu.exec(globalPos); |
| 531 | } |
| 532 | |
| 533 | void EditorView::handleRightButtonClickOnObject(Object & object) |
| 534 | { |
| 535 | m_mediator.setSelectedObject(&object); |
| 536 | |
| 537 | m_forceStationaryAction->setChecked(object.forceStationary()); |
| 538 | |
| 539 | openObjectContextMenu(); |
| 540 | } |
| 541 | |
| 542 | void EditorView::openTargetNodeContextMenu() |
| 543 | { |
| 544 | const QPoint globalPos = mapToGlobal(m_clickedPos); |
| 545 | m_targetNodeContextMenu.exec(globalPos); |
| 546 | } |
| 547 | |
| 548 | void EditorView::handleRightButtonClickOnTargetNode(TargetNode & tnode) |
| 549 | { |
| 550 | m_mediator.setSelectedTargetNode(&tnode); |
| 551 | |
| 552 | openTargetNodeContextMenu(); |
| 553 | } |
| 554 | |
| 555 | void EditorView::mouseReleaseEvent(QMouseEvent * event) |
| 556 | { |
| 557 | handleTileDragRelease(event); |
| 558 | handleObjectDragRelease(event); |
| 559 | handleTargetNodeDragRelease(event); |
| 560 | } |
| 561 | |
| 562 | void EditorView::keyPressEvent(QKeyEvent * event) |
| 563 | { |
| 564 | if (auto object = m_mediator.selectedObject()) |
| 565 | { |
| 566 | if (!event->isAutoRepeat()) |
| 567 | { |
| 568 | switch (event->key()) |
| 569 | { |
| 570 | case Qt::Key_Left: |
| 571 | m_mediator.saveUndoPoint(); |
| 572 | object->setLocation(QPointF(object->location().x() - 1, object->location().y())); |
| 573 | break; |
| 574 | case Qt::Key_Right: |
| 575 | m_mediator.saveUndoPoint(); |
| 576 | object->setLocation(QPointF(object->location().x() + 1, object->location().y())); |
| 577 | break; |
| 578 | case Qt::Key_Up: |
| 579 | m_mediator.saveUndoPoint(); |
| 580 | object->setLocation(QPointF(object->location().x(), object->location().y() - 1)); |
| 581 | break; |
| 582 | case Qt::Key_Down: |
| 583 | m_mediator.saveUndoPoint(); |
| 584 | object->setLocation(QPointF(object->location().x(), object->location().y() + 1)); |
| 585 | break; |
| 586 | default: |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void EditorView::handleTileDragRelease(QMouseEvent * event) |
| 594 | { |
| 595 | // Tile drag'n'drop active? |
| 596 | if (auto sourceTile = m_mediator.dadStore().dragAndDropSourceTile()) |
| 597 | { |
| 598 | // Determine the dest tile |
| 599 | auto destTile = sourceTile; |
| 600 | QList<QGraphicsItem *> items = scene()->items(mapToScene(event->pos())); |
| 601 | for (QGraphicsItem * item : items) |
| 602 | { |
| 603 | auto testTile = dynamic_cast<TrackTile *>(item); |
| 604 | if (testTile && testTile != sourceTile) |
| 605 | { |
| 606 | destTile = testTile; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // Swap tiles |
| 612 | sourceTile->swap(*destTile); |
| 613 | |
| 614 | // Restore position |
| 615 | sourceTile->setPos(m_mediator.dadStore().dragAndDropSourcePos()); |
| 616 | sourceTile->setZValue(sourceTile->zValue() - 1); |
| 617 | destTile->setZValue(sourceTile->zValue()); |
| 618 | |
| 619 | update(); |
| 620 | |
| 621 | m_mediator.dadStore().clear(); |
| 622 | |
| 623 | // Restore the cursor. |
| 624 | QApplication::restoreOverrideCursor(); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | void EditorView::handleObjectDragRelease(QMouseEvent * event) |
| 629 | { |
| 630 | // Object drag'n'drop active? |
| 631 | if (auto object = m_mediator.dadStore().dragAndDropObject()) |
| 632 | { |
| 633 | // Set the new position position |
| 634 | object->setLocation(mapToScene(event->pos())); |
| 635 | object->setZValue(object->zValue() - 1); |
| 636 | |
| 637 | update(); |
| 638 | |
| 639 | m_mediator.dadStore().clear(); |
| 640 | |
| 641 | // Restore the cursor. |
| 642 | QApplication::restoreOverrideCursor(); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | void EditorView::handleTargetNodeDragRelease(QMouseEvent * event) |
| 647 | { |
| 648 | // Target node drag'n'drop active? |
| 649 | if (auto tnode = m_mediator.dadStore().dragAndDropTargetNode()) |
| 650 | { |
| 651 | // Set the new position position |
| 652 | tnode->setLocation(mapToScene(event->pos())); |
| 653 | tnode->setZValue(tnode->zValue() - 1); |
| 654 | |
| 655 | update(); |
| 656 | |
| 657 | m_mediator.dadStore().clear(); |
| 658 | |
| 659 | // Restore the cursor. |
| 660 | QApplication::restoreOverrideCursor(); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | void EditorView::setComputerHint(TrackTileBase::ComputerHint hint) |
| 665 | { |
| 666 | if (auto tile = dynamic_cast<TrackTile *>(scene()->itemAt(mapToScene(m_clickedPos), QTransform()))) |
| 667 | { |
| 668 | m_mediator.saveUndoPoint(); |
| 669 | tile->setComputerHint(hint); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | void EditorView::changeTileType(TrackTile & tile, QAction * action) |
| 674 | { |
| 675 | m_mediator.saveUndoPoint(); |
| 676 | setTileType(tile, action); |
| 677 | } |
| 678 | |
| 679 | void EditorView::setTileType(TrackTile & tile, QAction * action) |
| 680 | { |
| 681 | tile.setTileType(action->data().toString()); |
| 682 | tile.setPixmap(action->icon().pixmap(TrackTile::width(), TrackTile::height())); |
| 683 | } |
| 684 | |
| 685 | void EditorView::wheelEvent(QWheelEvent * event) |
| 686 | { |
| 687 | if (event->modifiers() & Qt::ControlModifier) |
| 688 | { |
| 689 | int delta = 0; |
| 690 | |
| 691 | // High-precision devices (trackpads etc.) |
| 692 | if (!event->pixelDelta().isNull()) |
| 693 | { |
| 694 | delta = event->pixelDelta().y(); |
| 695 | } |
| 696 | else |
| 697 | { |
| 698 | // Classic mouse wheel (usually multiples of 120) |
| 699 | delta = event->angleDelta().y(); |
| 700 | } |
| 701 | |
| 702 | if (delta) |
| 703 | { |
| 704 | m_mediator.mouseWheelZoom(delta); |
| 705 | } |
| 706 | |
| 707 | // Do not pass event further if zoom was handled |
| 708 | event->accept(); |
| 709 | } |
| 710 | else |
| 711 | { |
| 712 | QGraphicsView::wheelEvent(event); |
| 713 | } |
| 714 | } |