Add player view rectangle option

This commit is contained in:
Marcus Huderle 2019-01-08 18:04:48 -06:00
parent 3f88072981
commit 4d088766a0
13 changed files with 115 additions and 11 deletions

View file

@ -2589,6 +2589,7 @@
<addaction name="actionZoom_In"/>
<addaction name="actionZoom_Out"/>
<addaction name="actionBetter_Cursors"/>
<addaction name="actionPlayer_View_Rectangle"/>
</widget>
<widget class="QMenu" name="menuTools">
<property name="title">
@ -2808,6 +2809,17 @@
<string>About Porymap...</string>
</property>
</action>
<action name="actionPlayer_View_Rectangle">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Player View Rectangle</string>
</property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Show the player's view rectangle on the map based on the cursor's position.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>

View file

@ -20,6 +20,7 @@
#include "collisionpixmapitem.h"
#include "mappixmapitem.h"
#include "settings.h"
#include "playerviewrect.h"
class DraggablePixmapItem;
class MetatilesPixmapItem;
@ -94,6 +95,7 @@ public:
QGraphicsItemGroup *events_group = nullptr;
QList<QGraphicsPixmapItem*> borderItems;
QList<QGraphicsLineItem*> gridLines;
PlayerViewRect *playerViewRect = nullptr;
QGraphicsScene *scene_metatiles = nullptr;
QGraphicsScene *scene_current_metatile_selection = nullptr;

View file

@ -74,6 +74,7 @@ private slots:
void on_actionZoom_In_triggered();
void on_actionZoom_Out_triggered();
void on_actionBetter_Cursors_triggered();
void on_actionPlayer_View_Rectangle_triggered();
void on_actionPencil_triggered();
void on_actionPointer_triggered();
void on_actionFlood_Fill_triggered();

View file

@ -10,6 +10,7 @@ public:
bool smartPathsEnabled;
bool betterCursors;
QCursor mapCursor;
bool playerViewRectEnabled;
};
#endif // SETTINGS_H

View file

@ -13,8 +13,10 @@ public:
this->map = map_;
this->metatileSelector = metatileSelector;
this->settings = settings;
this->paintingEnabled = true;
setAcceptHoverEvents(true);
}
bool paintingEnabled;
Map *map;
MetatileSelector *metatileSelector;
Settings *settings;

View file

@ -0,0 +1,35 @@
#ifndef PLAYERVIEWRECT_H
#define PLAYERVIEWRECT_H
#include <QGraphicsItem>
#include <QPainter>
class PlayerViewRect : public QGraphicsItem
{
public:
PlayerViewRect(bool *enabled);
QRectF boundingRect() const override
{
qreal penWidth = 4;
return QRectF(-penWidth,
-penWidth,
30 * 8 + penWidth * 2,
20 * 8 + penWidth * 2);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) override
{
int width = 30 * 8;
int height = 20 * 8;
painter->setPen(QColor(0xff, 0xff, 0xff));
painter->drawRect(-2, -2, width + 3, height + 3);
painter->setPen(QColor(0, 0, 0));
painter->drawRect(-3, -3, width + 5, height + 5);
painter->drawRect(-1, -1, width + 1, height + 1);
}
void updateLocation(int x, int y);
bool *enabled;
};
#endif // PLAYERVIEWRECT_H

View file

@ -45,6 +45,7 @@ SOURCES += src/core/block.cpp \
src/ui/noscrollcombobox.cpp \
src/ui/noscrollspinbox.cpp \
src/ui/paletteeditor.cpp \
src/ui/playerviewrect.cpp \
src/ui/selectablepixmapitem.cpp \
src/ui/tileseteditor.cpp \
src/ui/tileseteditormetatileselector.cpp \
@ -91,6 +92,7 @@ HEADERS += include/core/block.h \
include/ui/noscrollcombobox.h \
include/ui/noscrollspinbox.h \
include/ui/paletteeditor.h \
include/ui/playerviewrect.h \
include/ui/selectablepixmapitem.h \
include/ui/tileseteditor.h \
include/ui/tileseteditormetatileselector.h \

View file

@ -17,6 +17,7 @@ Editor::Editor(Ui::MainWindow* ui)
this->ui = ui;
this->selected_events = new QList<DraggablePixmapItem*>;
this->settings = new Settings();
this->playerViewRect = new PlayerViewRect(&this->settings->playerViewRectEnabled);
}
void Editor::saveProject() {
@ -59,10 +60,10 @@ void Editor::closeProject() {
void Editor::setEditingMap() {
current_view = map_item;
if (map_item) {
map_item->paintingEnabled = true;
displayMapConnections();
map_item->draw();
map_item->setVisible(true);
map_item->setEnabled(true);
setConnectionsVisibility(ui->checkBox_ToggleBorder->isChecked());
}
if (collision_item) {
@ -84,6 +85,7 @@ void Editor::setEditingCollision() {
setConnectionsVisibility(ui->checkBox_ToggleBorder->isChecked());
}
if (map_item) {
map_item->paintingEnabled = true;
map_item->setVisible(false);
}
if (events_group) {
@ -99,8 +101,8 @@ void Editor::setEditingObjects() {
events_group->setVisible(true);
}
if (map_item) {
map_item->paintingEnabled = false;
map_item->setVisible(true);
map_item->setEnabled(false);
setConnectionsVisibility(ui->checkBox_ToggleBorder->isChecked());
}
if (collision_item) {
@ -113,9 +115,9 @@ void Editor::setEditingObjects() {
void Editor::setEditingConnections() {
current_view = map_item;
if (map_item) {
map_item->paintingEnabled = false;
map_item->draw();
map_item->setVisible(true);
map_item->setEnabled(false);
populateConnectionMapPickers();
ui->label_NumConnections->setText(QString::number(map->connections.length()));
setConnectionsVisibility(false);
@ -351,7 +353,8 @@ void Editor::onSelectedMetatilesChanged() {
}
void Editor::onHoveredMapMetatileChanged(int x, int y) {
if (x >= 0 && x < map->getWidth() && y >= 0 && y < map->getHeight()) {
this->playerViewRect->updateLocation(x, y);
if (map_item->paintingEnabled && x >= 0 && x < map->getWidth() && y >= 0 && y < map->getHeight()) {
int blockIndex = y * map->getWidth() + x;
int tile = map->layout->blockdata->blocks->at(blockIndex).tile;
this->ui->statusBar->showMessage(QString("X: %1, Y: %2, Metatile: 0x%3, Scale = %4x")
@ -363,11 +366,15 @@ void Editor::onHoveredMapMetatileChanged(int x, int y) {
}
void Editor::onHoveredMapMetatileCleared() {
this->ui->statusBar->clearMessage();
this->playerViewRect->setVisible(false);
if (map_item->paintingEnabled) {
this->ui->statusBar->clearMessage();
}
}
void Editor::onHoveredMapMovementPermissionChanged(int x, int y) {
if (x >= 0 && x < map->getWidth() && y >= 0 && y < map->getHeight()) {
this->playerViewRect->updateLocation(x, y);
if (map_item->paintingEnabled && x >= 0 && x < map->getWidth() && y >= 0 && y < map->getHeight()) {
int blockIndex = y * map->getWidth() + x;
uint16_t collision = map->layout->blockdata->blocks->at(blockIndex).collision;
uint16_t elevation = map->layout->blockdata->blocks->at(blockIndex).elevation;
@ -380,7 +387,10 @@ void Editor::onHoveredMapMovementPermissionChanged(int x, int y) {
}
void Editor::onHoveredMapMovementPermissionCleared() {
this->ui->statusBar->clearMessage();
this->playerViewRect->setVisible(false);
if (map_item->paintingEnabled) {
this->ui->statusBar->clearMessage();
}
}
QString Editor::getMovementPermissionText(uint16_t collision, uint16_t elevation){
@ -427,6 +437,14 @@ bool Editor::setMap(QString map_name) {
}
void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
if (!item->paintingEnabled) {
return;
}
QPointF pos = event->pos();
int x = static_cast<int>(pos.x()) / 16;
int y = static_cast<int>(pos.y()) / 16;
this->playerViewRect->updateLocation(x, y);
if (map_edit_mode == "paint") {
if (event->buttons() & Qt::RightButton) {
item->updateMetatileSelection(event);
@ -461,6 +479,14 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item
}
}
void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
if (!item->paintingEnabled) {
return;
}
QPointF pos = event->pos();
int x = static_cast<int>(pos.x()) / 16;
int y = static_cast<int>(pos.y()) / 16;
this->playerViewRect->updateLocation(x, y);
if (map_edit_mode == "paint") {
if (event->buttons() & Qt::RightButton) {
item->updateMovementPermissionSelection(event);
@ -513,6 +539,8 @@ void Editor::displayMap() {
displayMapConnections();
displayMapBorder();
displayMapGrid();
this->playerViewRect->setZValue(1000);
scene->addItem(this->playerViewRect);
if (map_item) {
map_item->setVisible(false);
@ -1042,6 +1070,7 @@ void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
if (active) {
int x = static_cast<int>(mouse->pos().x() + this->pos().x()) / 16;
int y = static_cast<int>(mouse->pos().y() + this->pos().y()) / 16;
this->editor->playerViewRect->updateLocation(x, y);
if (x != last_x || y != last_y) {
if (editor->selected_events->contains(this)) {
for (DraggablePixmapItem *item : *editor->selected_events) {

View file

@ -970,6 +970,11 @@ void MainWindow::on_actionBetter_Cursors_triggered() {
this->editor->settings->betterCursors = ui->actionBetter_Cursors->isChecked();
}
void MainWindow::on_actionPlayer_View_Rectangle_triggered()
{
this->editor->settings->playerViewRectEnabled = ui->actionPlayer_View_Rectangle->isChecked();
}
void MainWindow::on_actionPencil_triggered()
{
on_toolButton_Paint_clicked();

View file

@ -4,4 +4,5 @@ Settings::Settings()
{
this->smartPathsEnabled = false;
this->betterCursors = true;
this->playerViewRectEnabled = false;
}

View file

@ -4,13 +4,13 @@ void CollisionPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
int x = static_cast<int>(event->pos().x()) / 16;
int y = static_cast<int>(event->pos().y()) / 16;
emit this->hoveredMapMovementPermissionChanged(x, y);
if (this->settings->betterCursors){
if (this->settings->betterCursors && this->paintingEnabled) {
setCursor(this->settings->mapCursor);
}
}
void CollisionPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
emit this->hoveredMapMovementPermissionCleared();
if (this->settings->betterCursors){
if (this->settings->betterCursors && this->paintingEnabled){
unsetCursor();
}
}

View file

@ -499,13 +499,13 @@ void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
int x = static_cast<int>(event->pos().x()) / 16;
int y = static_cast<int>(event->pos().y()) / 16;
emit this->hoveredMapMetatileChanged(x, y);
if (this->settings->betterCursors){
if (this->settings->betterCursors && this->paintingEnabled) {
setCursor(this->settings->mapCursor);
}
}
void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
emit this->hoveredMapMetatileCleared();
if (this->settings->betterCursors){
if (this->settings->betterCursors && this->paintingEnabled) {
unsetCursor();
}
}

14
src/ui/playerviewrect.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "playerviewrect.h"
PlayerViewRect::PlayerViewRect(bool *enabled)
{
this->enabled = enabled;
this->setVisible(*enabled);
}
void PlayerViewRect::updateLocation(int x, int y)
{
this->setX((x * 16) - (14 * 8));
this->setY((y * 16) - (9 * 8));
this->setVisible(*this->enabled);
}