Skip to content

Commit

Permalink
Everywhere: Add coordinates of current cursor position to QStatusBar
Browse files Browse the repository at this point in the history
This patch adds coordinates of current cursor position to QStatusBar
while displaying them in two different colors either cursor is on
an image or it is not.
  • Loading branch information
tetektoza committed Jul 31, 2024
1 parent 3ca9b79 commit 025ac0e
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 12 deletions.
10 changes: 8 additions & 2 deletions source/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ void MainWindow::openFile(const OpenAsParam &params)
QObject::connect(this->trnUniqueWidget, &PaletteWidget::clearRootBorder, this->trnWidget, &PaletteWidget::clearBorder);

if (isTileset) {
this->levelCelView = new LevelCelView(this->undoStack);
this->levelCelView = new LevelCelView(this->undoStack, this);
this->levelCelView->initialize(this->gfx, this->min, this->til, this->sol, this->amp);

// Refresh CEL view if a PAL or TRN is modified
Expand All @@ -651,7 +651,7 @@ void MainWindow::openFile(const OpenAsParam &params)
}
// Otherwise build a CelView
else {
this->celView = new CelView(this->undoStack);
this->celView = new CelView(this->undoStack, this);
this->celView->initialize(this->gfx);

// Refresh CEL view if a PAL or TRN is modified
Expand Down Expand Up @@ -778,6 +778,12 @@ void MainWindow::openPalFiles(QStringList filePaths, PaletteWidget *widget)
this->ui->statusBar->clearMessage();
}

void MainWindow::updateStatusBar(const QString& status, const QString& styleSheet)
{
this->ui->statusBar->setStyleSheet(styleSheet);
this->ui->statusBar->showMessage(status);
}

void MainWindow::saveFile(const QString &gfxPath)
{
this->ui->statusBar->showMessage("Saving...");
Expand Down
1 change: 1 addition & 0 deletions source/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class MainWindow : public QMainWindow {

void nextPaletteCycle(D1PAL_CYCLE_TYPE type);
void resetPaletteCycle();
void updateStatusBar(const QString& status, const QString& styleSheet);

QString getLastFilePath();
QString fileDialog(FILE_DIALOG_MODE mode, const char *title, const char *filter);
Expand Down
54 changes: 44 additions & 10 deletions source/views/celview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QMenu>
#include <QMessageBox>
#include <QMimeData>
#include <utility>

#include "ui_celview.h"
#include "undostack/framecmds.h"
Expand Down Expand Up @@ -40,6 +41,34 @@ void CelScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
emit this->framePixelClicked(x, y);
}

void CelScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
bool isInImage = false;
auto *levelcelview = dynamic_cast<LevelCelView *>(view);
if (levelcelview != nullptr) {
if (levelcelview->checkImageType(event->scenePos().x(), event->scenePos().y()) != IMAGE_TYPE::NONE)
isInImage = true;
}
else {
isInImage = dynamic_cast<CelView *>(view)->isInImage(event->scenePos().x(), event->scenePos().y());
}

if (isInImage) {
dynamic_cast<MainWindow *>(view->window())->updateStatusBar(QString::fromStdString(
std::to_string(static_cast<int>(event->scenePos().x())) +
", " +
std::to_string(static_cast<int>(event->scenePos().y()))),
"color: rgb(0, 0, 0);");
return;
}

dynamic_cast<MainWindow *>(view->window())->updateStatusBar(QString::fromStdString(
std::to_string(static_cast<int>(event->scenePos().x())) +
", " +
std::to_string(static_cast<int>(event->scenePos().y()))),
"color: rgb(160, 160, 160);");
}

void CelScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
this->dragMoveEvent(event);
Expand Down Expand Up @@ -73,7 +102,7 @@ void CelScene::contextMenuEvent(QContextMenuEvent *event)

CelView::CelView(std::shared_ptr<UndoStack> us, QWidget *parent)
: QWidget(parent)
, undoStack(us)
, undoStack(std::move(us))
, ui(new Ui::CelView())
, celScene(new CelScene(this))
{
Expand Down Expand Up @@ -121,18 +150,23 @@ int CelView::getCurrentFrameIndex()
return this->currentFrameIndex;
}

void CelView::framePixelClicked(unsigned x, unsigned y)
bool CelView::isInImage(unsigned int x, unsigned int y) const
{
int frameIndex = this->currentFrameIndex;
const int frameIndex = this->currentFrameIndex;

const int tx = x - CEL_SCENE_SPACING;
const int ty = y - CEL_SCENE_SPACING;

return ((ty > 0 && ty < this->gfx->getFrameHeight(frameIndex)) /* Coordinates are above/below image */
&& (tx > 0 && tx < this->gfx->getFrameWidth(frameIndex) /* Coordinates are to the left/right of the image */));
}

int tx = x - CEL_SCENE_SPACING;
if (tx < 0 || tx >= this->gfx->getFrameWidth(frameIndex))
return; // click is left or right from the frame -> ignore
int ty = y - CEL_SCENE_SPACING;
if (ty < 0 || ty >= this->gfx->getFrameHeight(frameIndex))
return; // click is up or down from the frame -> ignore
void CelView::framePixelClicked(unsigned x, unsigned y)
{
if (!isInImage(x, y))
return;

int colorIndex = this->gfx->getFrame(frameIndex)->getPixel(tx, ty).getPaletteIndex();
int colorIndex = this->gfx->getFrame(this->currentFrameIndex)->getPixel(x - CEL_SCENE_SPACING, y - CEL_SCENE_SPACING).getPaletteIndex();

emit this->colorIndexClicked(colorIndex);
}
Expand Down
2 changes: 2 additions & 0 deletions source/views/celview.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CelScene : public QGraphicsScene {

private slots:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
Expand Down Expand Up @@ -66,6 +67,7 @@ class CelView : public QWidget {
void updateGroupIndex();

void displayFrame();
[[nodiscard]] bool isInImage(unsigned int x, unsigned int y) const;

signals:
void frameRefreshed();
Expand Down
34 changes: 34 additions & 0 deletions source/views/levelcelview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,40 @@ void LevelCelView::update()
this->tabFrameWidget->initialize(this, this->gfx);
}

IMAGE_TYPE LevelCelView::checkImageType(unsigned x, unsigned y)
{
unsigned celFrameWidth = MICRO_WIDTH; // this->gfx->getFrameWidth(this->currentFrameIndex);
unsigned subtileWidth = this->min->getSubtileWidth() * MICRO_WIDTH;
unsigned tileWidth = subtileWidth * TILE_WIDTH;

unsigned celFrameHeight = MICRO_HEIGHT; // this->gfx->getFrameHeight(this->currentFrameIndex);
unsigned subtileHeight = this->min->getSubtileHeight() * MICRO_HEIGHT;
unsigned subtileShiftY = subtileWidth / 4;
unsigned tileHeight = subtileHeight + 2 * subtileShiftY;

if (x >= CEL_SCENE_SPACING && x < (celFrameWidth + CEL_SCENE_SPACING)
&& y >= CEL_SCENE_SPACING && y < (celFrameHeight + CEL_SCENE_SPACING)
&& this->gfx->getFrameCount() != 0) {
return IMAGE_TYPE::FRAME;
}
else if (x >= (celFrameWidth + CEL_SCENE_SPACING * 2)
&& x < (celFrameWidth + subtileWidth + CEL_SCENE_SPACING * 2)
&& y >= CEL_SCENE_SPACING
&& y < (subtileHeight + CEL_SCENE_SPACING)
&& this->min->getSubtileCount() != 0) {
return IMAGE_TYPE::SUBTILE;
}
else if (x >= (celFrameWidth + subtileWidth + CEL_SCENE_SPACING * 3)
&& x < (celFrameWidth + subtileWidth + tileWidth + CEL_SCENE_SPACING * 3)
&& y >= CEL_SCENE_SPACING
&& y < (tileHeight + CEL_SCENE_SPACING)
&& this->til->getTileCount() != 0) {
return IMAGE_TYPE::TILE;
}

return IMAGE_TYPE::NONE;
}

void LevelCelView::framePixelClicked(unsigned x, unsigned y)
{
quint8 index = 0;
Expand Down
9 changes: 9 additions & 0 deletions source/views/levelcelview.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ enum class TILESET_MODE {
TILE, // Edit tile
};

enum class IMAGE_TYPE {
NONE,
FRAME,
SUBTILE,
TILE
};

class LevelCelView : public QWidget {
Q_OBJECT

Expand Down Expand Up @@ -94,6 +101,8 @@ class LevelCelView : public QWidget {

void displayFrame();

IMAGE_TYPE checkImageType(unsigned int x, unsigned int y);

private:
void update();
void collectFrameUsers(int frameIndex, QList<int> &users) const;
Expand Down
7 changes: 7 additions & 0 deletions source/views/view.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "view.h"
#include "mainwindow.h"

View::View(QWidget *parent)
: QGraphicsView(parent)
{
this->setMouseTracking(true);
}

void View::mousePressEvent(QMouseEvent *event)
Expand Down Expand Up @@ -50,3 +52,8 @@ void View::mouseReleaseEvent(QMouseEvent *event)
}
}
}

void View::leaveEvent(QEvent *event)
{
dynamic_cast<MainWindow *>(this->window())->updateStatusBar("", "color: rgb(0, 0, 0);");
}
1 change: 1 addition & 0 deletions source/views/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class View : public QGraphicsView {
private slots:
void mouseReleaseEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void leaveEvent(QEvent* event) override;
};

0 comments on commit 025ac0e

Please sign in to comment.