From 36dc1c66493ea72fc78e154b5beabaad8be6f76d Mon Sep 17 00:00:00 2001 From: GriffinR Date: Sun, 12 Dec 2021 12:19:59 -0500 Subject: [PATCH] Move map cursor UI and grid to foreground --- forms/mainwindow.ui | 2 +- include/editor.h | 2 ++ include/ui/cursortilerect.h | 10 +++--- include/ui/graphicsview.h | 10 ------ include/ui/mappixmapitem.h | 1 + include/ui/mapview.h | 26 +++++++++++++++ include/ui/movablerect.h | 7 ++-- src/editor.cpp | 61 ++++++++++++++++++---------------- src/mainwindow.cpp | 7 ++-- src/ui/cursortilerect.cpp | 8 ++--- src/ui/draggablepixmapitem.cpp | 2 +- src/ui/graphicsview.cpp | 47 ++++++++++++++++---------- src/ui/mappixmapitem.cpp | 10 ++++-- 13 files changed, 118 insertions(+), 75 deletions(-) create mode 100644 include/ui/mapview.h diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index 2bc1f591..5b45aac9 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -344,7 +344,7 @@ 0 - + 0 diff --git a/include/editor.h b/include/editor.h index 7aa06113..f17eb771 100644 --- a/include/editor.h +++ b/include/editor.h @@ -102,6 +102,8 @@ public: void duplicateSelectedEvents(); void redrawObject(DraggablePixmapItem *item); QList getObjects(); + void updateCursorRectPos(int x, int y); + void setCursorRectVisible(bool visible); QGraphicsScene *scene = nullptr; QGraphicsPixmapItem *current_view = nullptr; diff --git a/include/ui/cursortilerect.h b/include/ui/cursortilerect.h index 5d0c5f15..1ccb60f7 100644 --- a/include/ui/cursortilerect.h +++ b/include/ui/cursortilerect.h @@ -41,10 +41,10 @@ public: } painter->setPen(this->color); - painter->drawRect(-1, -1, width + 2, height + 2); + painter->drawRect(x() - 1, y() - 1, width + 2, height + 2); painter->setPen(QColor(0, 0, 0)); - painter->drawRect(-2, -2, width + 4, height + 4); - painter->drawRect(0, 0, width, height); + painter->drawRect(x() - 2, y() - 2, width + 4, height + 4); + painter->drawRect(x(), y(), width, height); } void initAnchor(int coordX, int coordY); void stopAnchor(); @@ -58,10 +58,10 @@ public: void stopSingleTileMode(); void updateLocation(int x, int y); void updateSelectionSize(int width, int height); - void setVisibility(bool visible); + void setActive(bool active); bool *enabled; private: - bool visible; + bool active; int width; int height; bool anchored; diff --git a/include/ui/graphicsview.h b/include/ui/graphicsview.h index 7c5cc2bc..76dac180 100644 --- a/include/ui/graphicsview.h +++ b/include/ui/graphicsview.h @@ -1,7 +1,6 @@ #ifndef GRAPHICSVIEW_H #define GRAPHICSVIEW_H -#include "overlay.h" #include #include @@ -12,23 +11,14 @@ class GraphicsView : public QGraphicsView public: GraphicsView() : QGraphicsView() {} GraphicsView(QWidget *parent) : QGraphicsView(parent) {} - Overlay * getOverlay(int layer); - void clearOverlays(); - void setOverlaysHidden(bool hidden); - void setOverlaysX(int x); - void setOverlaysY(int y); - void setOverlaysPosition(int x, int y); - void moveOverlays(int deltaX, int deltaY); public: // GraphicsView_Object object; Editor *editor; - QMap overlayMap; protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); - void drawForeground(QPainter *painter, const QRectF &rect); void moveEvent(QMoveEvent *event); }; diff --git a/include/ui/mappixmapitem.h b/include/ui/mappixmapitem.h index e9d999b4..0e9c4c0c 100644 --- a/include/ui/mappixmapitem.h +++ b/include/ui/mappixmapitem.h @@ -40,6 +40,7 @@ public: bool prevStraightPathState; int straight_path_initial_x; int straight_path_initial_y; + QPoint metatilePos; enum Axis { None = 0, X, diff --git a/include/ui/mapview.h b/include/ui/mapview.h new file mode 100644 index 00000000..a6c18aaa --- /dev/null +++ b/include/ui/mapview.h @@ -0,0 +1,26 @@ +#ifndef MAPVIEW_H +#define MAPVIEW_H + +#include "graphicsview.h" +#include "overlay.h" + +class MapView : public GraphicsView +{ +public: + MapView() : GraphicsView() {} + MapView(QWidget *parent) : GraphicsView(parent) {} + Overlay * getOverlay(int layer); + void clearOverlays(); + void setOverlaysHidden(bool hidden); + void setOverlaysX(int x); + void setOverlaysY(int y); + void setOverlaysPosition(int x, int y); + void moveOverlays(int deltaX, int deltaY); + +public: + QMap overlayMap; +protected: + void drawForeground(QPainter *painter, const QRectF &rect); +}; + +#endif // GRAPHICSVIEW_H diff --git a/include/ui/movablerect.h b/include/ui/movablerect.h index f73e360a..efd85847 100644 --- a/include/ui/movablerect.h +++ b/include/ui/movablerect.h @@ -20,11 +20,12 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override { + if (!(*enabled)) return; painter->setPen(this->color); - painter->drawRect(-2, -2, this->width + 3, this->height + 3); + painter->drawRect(x() - 2, y() - 2, this->width + 3, this->height + 3); painter->setPen(QColor(0, 0, 0)); - painter->drawRect(-3, -3, this->width + 5, this->height + 5); - painter->drawRect(-1, -1, this->width + 1, this->height + 1); + painter->drawRect(x() - 3, y() - 3, this->width + 5, this->height + 5); + painter->drawRect(x() - 1, y() - 1, this->width + 1, this->height + 1); } void updateLocation(int x, int y); bool *enabled; diff --git a/src/editor.cpp b/src/editor.cpp index 7d8b699e..54bb3c0a 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -96,7 +96,7 @@ void Editor::setEditingMap() { setBorderItemsVisible(ui->checkBox_ToggleBorder->isChecked()); setConnectionItemsVisible(false); this->cursorMapTileRect->stopSingleTileMode(); - this->cursorMapTileRect->setVisibility(true); + this->cursorMapTileRect->setActive(true); setMapEditingButtonsEnabled(true); } @@ -119,7 +119,7 @@ void Editor::setEditingCollision() { setBorderItemsVisible(ui->checkBox_ToggleBorder->isChecked()); setConnectionItemsVisible(false); this->cursorMapTileRect->setSingleTileMode(); - this->cursorMapTileRect->setVisibility(true); + this->cursorMapTileRect->setActive(true); setMapEditingButtonsEnabled(true); } @@ -142,7 +142,7 @@ void Editor::setEditingObjects() { setBorderItemsVisible(ui->checkBox_ToggleBorder->isChecked()); setConnectionItemsVisible(false); this->cursorMapTileRect->setSingleTileMode(); - this->cursorMapTileRect->setVisibility(false); + this->cursorMapTileRect->setActive(false); setMapEditingButtonsEnabled(false); } @@ -190,7 +190,7 @@ void Editor::setEditingConnections() { setBorderItemsVisible(true, 0.4); setConnectionItemsVisible(true); this->cursorMapTileRect->setSingleTileMode(); - this->cursorMapTileRect->setVisibility(false); + this->cursorMapTileRect->setActive(false); } void Editor::displayWildMonTables() { @@ -985,9 +985,26 @@ void Editor::scaleMapView(int s) { ui->graphicsView_Connections->setTransform(transform); } +void Editor::updateCursorRectPos(int x, int y) { + if (this->playerViewRect) + this->playerViewRect->updateLocation(x, y); + if (this->cursorMapTileRect) + this->cursorMapTileRect->updateLocation(x, y); + if (ui->graphicsView_Map->scene()) + ui->graphicsView_Map->scene()->update(); +} + +void Editor::setCursorRectVisible(bool visible) { + if (this->playerViewRect) + this->playerViewRect->setVisible(visible); + if (this->cursorMapTileRect) + this->cursorMapTileRect->setVisible(visible); + if (ui->graphicsView_Map->scene()) + ui->graphicsView_Map->scene()->update(); +} + void Editor::onHoveredMapMetatileChanged(const QPoint &pos) { - this->playerViewRect->updateLocation(pos.x(), pos.y()); - this->cursorMapTileRect->updateLocation(pos.x(), pos.y()); + this->updateCursorRectPos(pos.x(), pos.y()); if (map_item->paintingMode == MapPixmapItem::PaintMode::Metatiles && pos.x() >= 0 && pos.x() < map->getWidth() && pos.y() >= 0 && pos.y() < map->getHeight()) { int blockIndex = pos.y() * map->getWidth() + pos.x(); @@ -1007,8 +1024,7 @@ void Editor::onHoveredMapMetatileChanged(const QPoint &pos) { } void Editor::onHoveredMapMetatileCleared() { - this->playerViewRect->setVisible(false); - this->cursorMapTileRect->setVisible(false); + this->setCursorRectVisible(false); if (map_item->paintingMode == MapPixmapItem::PaintMode::Metatiles || map_item->paintingMode == MapPixmapItem::PaintMode::EventObjects) { this->ui->statusBar->clearMessage(); @@ -1016,8 +1032,7 @@ void Editor::onHoveredMapMetatileCleared() { } void Editor::onHoveredMapMovementPermissionChanged(int x, int y) { - this->playerViewRect->updateLocation(x, y); - this->cursorMapTileRect->updateLocation(x, y); + this->updateCursorRectPos(x, y); if (map_item->paintingMode == MapPixmapItem::PaintMode::Metatiles && x >= 0 && x < map->getWidth() && y >= 0 && y < map->getHeight()) { int blockIndex = y * map->getWidth() + x; @@ -1032,8 +1047,7 @@ void Editor::onHoveredMapMovementPermissionChanged(int x, int y) { } void Editor::onHoveredMapMovementPermissionCleared() { - this->playerViewRect->setVisible(false); - this->cursorMapTileRect->setVisible(false); + this->setCursorRectVisible(false); if (map_item->paintingMode == MapPixmapItem::PaintMode::Metatiles) { this->ui->statusBar->clearMessage(); } @@ -1243,8 +1257,6 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item } } } - this->playerViewRect->updateLocation(pos.x(), pos.y()); - this->cursorMapTileRect->updateLocation(pos.x(), pos.y()); } void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) { @@ -1291,8 +1303,6 @@ void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixm } item->shift(event); } - this->playerViewRect->updateLocation(pos.x(), pos.y()); - this->cursorMapTileRect->updateLocation(pos.x(), pos.y()); } bool Editor::displayMap() { @@ -1307,8 +1317,6 @@ bool Editor::displayMap() { if (map_item && scene) { scene->removeItem(map_item); delete map_item; - scene->removeItem(this->playerViewRect); - scene->removeItem(this->cursorMapTileRect); scene->removeItem(this->map_ruler); } @@ -1324,11 +1332,7 @@ bool Editor::displayMap() { displayMapGrid(); displayWildMonTables(); - this->playerViewRect->setZValue(1000); - this->cursorMapTileRect->setZValue(1001); - this->map_ruler->setZValue(1002); - scene->addItem(this->playerViewRect); - scene->addItem(this->cursorMapTileRect); + this->map_ruler->setZValue(1000); scene->addItem(this->map_ruler); if (map_item) { @@ -1670,14 +1674,13 @@ int Editor::getBorderDrawDistance(int dimension) { void Editor::onToggleGridClicked(bool checked) { porymapConfig.setShowGrid(checked); + if (ui->graphicsView_Map->scene()) + ui->graphicsView_Map->scene()->update(); } void Editor::displayMapGrid() { for (QGraphicsLineItem* item : gridLines) { - if (item && item->scene()) { - item->scene()->removeItem(item); - } - delete item; + if (item) delete item; } gridLines.clear(); ui->checkBox_ToggleGrid->disconnect(); @@ -1686,14 +1689,14 @@ void Editor::displayMapGrid() { int pixelHeight = map->getHeight() * 16; for (int i = 0; i <= map->getWidth(); i++) { int x = i * 16; - QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight); + QGraphicsLineItem *line = new QGraphicsLineItem(x, 0, x, pixelHeight); line->setVisible(ui->checkBox_ToggleGrid->isChecked()); gridLines.append(line); connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);}); } for (int j = 0; j <= map->getHeight(); j++) { int y = j * 16; - QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y); + QGraphicsLineItem *line = new QGraphicsLineItem(0, y, pixelWidth, y); line->setVisible(ui->checkBox_ToggleGrid->isChecked()); gridLines.append(line); connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);}); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 760fec54..95a9630d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1674,8 +1674,7 @@ void MainWindow::setMapViewTabInternal(int index) } else if (index == 1) { editor->setEditingCollision(); } - editor->playerViewRect->setVisible(false); - editor->cursorMapTileRect->setVisible(false); + editor->setCursorRectVisible(false); } void MainWindow::on_action_Exit_triggered() @@ -1738,6 +1737,7 @@ void MainWindow::on_actionPlayer_View_Rectangle_triggered() this->editor->settings->playerViewRectEnabled = enabled; if (this->editor->map_item->has_mouse) { this->editor->playerViewRect->setVisible(enabled); + ui->graphicsView_Map->scene()->update(); } } @@ -1747,7 +1747,8 @@ void MainWindow::on_actionCursor_Tile_Outline_triggered() porymapConfig.setShowCursorTile(enabled); this->editor->settings->cursorTileRectEnabled = enabled; if (this->editor->map_item->has_mouse) { - this->editor->cursorMapTileRect->setVisibility(enabled); + this->editor->cursorMapTileRect->setVisible(enabled); + ui->graphicsView_Map->scene()->update(); } } diff --git a/src/ui/cursortilerect.cpp b/src/ui/cursortilerect.cpp index e53ef7d2..5f0f9399 100644 --- a/src/ui/cursortilerect.cpp +++ b/src/ui/cursortilerect.cpp @@ -4,7 +4,7 @@ CursorTileRect::CursorTileRect(bool *enabled, QRgb color) { this->enabled = enabled; - this->visible = true; + this->active = true; this->color = color; this->width = 16; this->height = 16; @@ -19,9 +19,9 @@ CursorTileRect::CursorTileRect(bool *enabled, QRgb color) this->selectionHeight = 1; } -void CursorTileRect::setVisibility(bool visible) +void CursorTileRect::setActive(bool active) { - this->visible = visible; + this->active = active; } void CursorTileRect::initAnchor(int coordX, int coordY) @@ -105,5 +105,5 @@ void CursorTileRect::updateLocation(int coordX, int coordY) coordY = qMax(coordY, 0); this->setX(coordX * 16); this->setY(coordY * 16); - this->setVisible(*this->enabled && this->visible); + this->setVisible(*this->enabled && this->active); } diff --git a/src/ui/draggablepixmapitem.cpp b/src/ui/draggablepixmapitem.cpp index 1c6f771e..56c66209 100644 --- a/src/ui/draggablepixmapitem.cpp +++ b/src/ui/draggablepixmapitem.cpp @@ -82,8 +82,8 @@ void DraggablePixmapItem::moveTo(const QPoint &pos) { void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) { if (active) { QPoint pos = Metatile::coordFromPixmapCoord(mouse->scenePos()); - emit this->editor->map_item->hoveredMapMetatileChanged(pos); if (pos.x() != last_x || pos.y() != last_y) { + emit this->editor->map_item->hoveredMapMetatileChanged(pos); QList selectedEvents; if (editor->selected_events->contains(this)) { for (DraggablePixmapItem *item : *editor->selected_events) { diff --git a/src/ui/graphicsview.cpp b/src/ui/graphicsview.cpp index 80e61afa..2c8d9e20 100644 --- a/src/ui/graphicsview.cpp +++ b/src/ui/graphicsview.cpp @@ -1,4 +1,5 @@ #include "graphicsview.h" +#include "mapview.h" #include "editor.h" void GraphicsView::mousePressEvent(QMouseEvent *event) { @@ -16,12 +17,31 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event) { QGraphicsView::mouseReleaseEvent(event); } -void GraphicsView::drawForeground(QPainter *painter, const QRectF&) { - foreach (Overlay * overlay, this->overlayMap) - overlay->renderItems(painter); +void GraphicsView::moveEvent(QMoveEvent *event) { + QGraphicsView::moveEvent(event); + QLabel *label_MapRulerStatus = findChild("label_MapRulerStatus", Qt::FindDirectChildrenOnly); + if (label_MapRulerStatus && label_MapRulerStatus->isVisible()) + label_MapRulerStatus->move(mapToGlobal(QPoint(6, 6))); } -void GraphicsView::clearOverlays() { +void MapView::drawForeground(QPainter *painter, const QRectF&) { + foreach (Overlay * overlay, this->overlayMap) + overlay->renderItems(painter); + + if (!editor) return; + + QStyleOptionGraphicsItem option; + for (QGraphicsLineItem* line : editor->gridLines) { + if (line && line->isVisible()) + line->paint(painter, &option, this); + } + if (editor->playerViewRect && editor->playerViewRect->isVisible()) + editor->playerViewRect->paint(painter, &option, this); + if (editor->cursorMapTileRect && editor->cursorMapTileRect->isVisible()) + editor->cursorMapTileRect->paint(painter, &option, this); +} + +void MapView::clearOverlays() { foreach (Overlay * overlay, this->overlayMap) { overlay->clearItems(); delete overlay; @@ -29,32 +49,32 @@ void GraphicsView::clearOverlays() { this->overlayMap.clear(); } -void GraphicsView::setOverlaysHidden(bool hidden) { +void MapView::setOverlaysHidden(bool hidden) { foreach (Overlay * overlay, this->overlayMap) overlay->setHidden(hidden); } -void GraphicsView::setOverlaysX(int x) { +void MapView::setOverlaysX(int x) { foreach (Overlay * overlay, this->overlayMap) overlay->setX(x); } -void GraphicsView::setOverlaysY(int y) { +void MapView::setOverlaysY(int y) { foreach (Overlay * overlay, this->overlayMap) overlay->setY(y); } -void GraphicsView::setOverlaysPosition(int x, int y) { +void MapView::setOverlaysPosition(int x, int y) { foreach (Overlay * overlay, this->overlayMap) overlay->setPosition(x, y); } -void GraphicsView::moveOverlays(int deltaX, int deltaY) { +void MapView::moveOverlays(int deltaX, int deltaY) { foreach (Overlay * overlay, this->overlayMap) overlay->move(deltaX, deltaY); } -Overlay * GraphicsView::getOverlay(int layer) { +Overlay * MapView::getOverlay(int layer) { Overlay * overlay = this->overlayMap.value(layer, nullptr); if (!overlay) { overlay = new Overlay(); @@ -62,10 +82,3 @@ Overlay * GraphicsView::getOverlay(int layer) { } return overlay; } - -void GraphicsView::moveEvent(QMoveEvent *event) { - QGraphicsView::moveEvent(event); - QLabel *label_MapRulerStatus = findChild("label_MapRulerStatus", Qt::FindDirectChildrenOnly); - if (label_MapRulerStatus && label_MapRulerStatus->isVisible()) - label_MapRulerStatus->move(mapToGlobal(QPoint(6, 6))); -} diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index 442db844..512104d1 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -675,7 +675,10 @@ void MapPixmapItem::draw(bool ignoreCache) { void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { QPoint pos = Metatile::coordFromPixmapCoord(event->pos()); - emit this->hoveredMapMetatileChanged(pos); + if (pos != this->metatilePos) { + this->metatilePos = pos; + emit this->hoveredMapMetatileChanged(pos); + } if (this->settings->betterCursors && this->paintingMode != MapPixmapItem::PaintMode::Disabled) { setCursor(this->settings->mapCursor); } @@ -699,7 +702,10 @@ void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { } void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { QPoint pos = Metatile::coordFromPixmapCoord(event->pos()); - emit this->hoveredMapMetatileChanged(pos); + if (pos != this->metatilePos) { + this->metatilePos = pos; + emit this->hoveredMapMetatileChanged(pos); + } emit mouseEvent(event, this); } void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {