Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Everywhere: Add coordinates of current cursor position to QStatusBar #146

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 35 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,25 @@ 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 +93,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 +141,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
64 changes: 51 additions & 13 deletions source/views/levelcelview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,45 @@ void LevelCelView::update()
this->tabFrameWidget->initialize(this, this->gfx);
}

/**
* @brief Checks in which image type the coordinates are currently in
*
* This function accepts coordinates and checks, based on them in which
* image type (frame, tile or megatile), the coordinates are currently in.
* This is currently useful for knowing where exactly user's cursor is.
*/
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 All @@ -199,19 +238,16 @@ void LevelCelView::framePixelClicked(unsigned x, unsigned y)

this->mode = TILESET_MODE::FREE;

if (x >= CEL_SCENE_SPACING && x < (celFrameWidth + CEL_SCENE_SPACING)
&& y >= CEL_SCENE_SPACING && y < (celFrameHeight + CEL_SCENE_SPACING)
&& this->gfx->getFrameCount() != 0) {
switch (this->checkImageType(x, y)) {
case IMAGE_TYPE::FRAME: {
// If CEL frame color is clicked, select it in the palette widgets
D1GfxFrame *frame = this->gfx->getFrame(this->currentFrameIndex);
index = frame->getPixel(x - CEL_SCENE_SPACING, y - CEL_SCENE_SPACING).getPaletteIndex();

emit this->colorIndexClicked(index);
} 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) {
break;
}
case IMAGE_TYPE::SUBTILE: {
this->mode = TILESET_MODE::SUBTILE;
// When a CEL frame is clicked in the subtile, display the corresponding CEL frame

Expand All @@ -230,11 +266,9 @@ void LevelCelView::framePixelClicked(unsigned x, unsigned y)
this->currentFrameIndex = frameIndex - 1;
this->displayFrame();
}
} 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) {
break;
}
case IMAGE_TYPE::TILE: {
this->mode = TILESET_MODE::TILE;
// When a subtile is clicked in the tile, display the corresponding subtile

Expand All @@ -249,6 +283,10 @@ void LevelCelView::framePixelClicked(unsigned x, unsigned y)
this->currentSubtileIndex = tilSubtiles.at(this->editIndex);
this->displayFrame();
}
break;
}
default:
break;
}

this->update();
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;
};
Loading