Skip to content

Commit

Permalink
Add a global tileset scale option that affects all UI tiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
zrax committed Dec 13, 2020
1 parent 5658d6b commit 77fd41c
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 48 deletions.
9 changes: 6 additions & 3 deletions lib/libcc1/Tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ void CCETileset::drawAt(QPainter& painter, int x, int y, tile_t upper, tile_t lo

QPixmap CCETileset::getPixmap(tile_t tile) const
{
return (tile < ccl::NUM_TILE_TYPES)
? m_base[tile]
: m_base[ccl::Tile_UNUSED_20];
QPixmap img = (tile < ccl::NUM_TILE_TYPES)
? m_base[tile]
: m_base[ccl::Tile_UNUSED_20];
if (m_uiScale != 1.0)
return img.scaled(img.width() * m_uiScale, img.height() * m_uiScale);
return img;
}

QString CCETileset::TileName(tile_t tile)
Expand Down
9 changes: 7 additions & 2 deletions lib/libcc1/Tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ class CCETileset : public QObject {

public:
explicit CCETileset(QObject* parent = nullptr)
: QObject(parent), m_size()
: QObject(parent), m_size(), m_uiScale(1.0)
{ }

QString name() const { return m_name; }
QString description() const { return m_description; }
int size() const { return m_size; }
QSize qsize() const { return QSize(m_size, m_size); }

qreal uiScale() const { return m_uiScale; }
void setUiScale(qreal scale) { m_uiScale = scale; }
int uiSize() const { return m_size * m_uiScale; }
QSize iconSize() const { return QSize(uiSize(), uiSize()); }

bool load(const QString& filename);
QString filename() const { return m_filename; }
Expand All @@ -56,6 +60,7 @@ class CCETileset : public QObject {
QString m_name, m_filename;
QString m_description;
int m_size;
qreal m_uiScale;

QPixmap m_base[ccl::NUM_TILE_TYPES];
QPixmap m_overlay[ccl::NUM_TILE_TYPES];
Expand Down
3 changes: 2 additions & 1 deletion lib/libcc2/Tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,9 +1165,10 @@ void CC2ETileset::drawWires(QPainter& painter, int x, int y, uint32_t wireMask,

QIcon CC2ETileset::getIcon(const cc2::Tile* tile) const
{
QPixmap ico(m_size, m_size);
QPixmap ico(m_size * m_uiScale, m_size * m_uiScale);
if (tile) {
QPainter painter(&ico);
painter.scale(m_uiScale, m_uiScale);
draw(painter, 0, 0, tile, false);
painter.end();
}
Expand Down
9 changes: 7 additions & 2 deletions lib/libcc2/Tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ class CC2ETileset : public QObject {

public:
CC2ETileset(QObject* parent = nullptr)
: QObject(parent), m_size()
: QObject(parent), m_size(), m_uiScale(1.0)
{ }

QString name() const { return m_name; }
QString description() const { return m_description; }
int size() const { return m_size; }
QSize qsize() const { return QSize(m_size, m_size); }

qreal uiScale() const { return m_uiScale; }
void setUiScale(qreal scale) { m_uiScale = scale; }
int uiSize() const { return m_size * m_uiScale; }
QSize iconSize() const { return QSize(uiSize(), uiSize()); }

bool load(const QString& filename);
QString filename() const { return m_filename; }
Expand All @@ -124,6 +128,7 @@ class CC2ETileset : public QObject {
QString m_name, m_filename;
QString m_description;
int m_size;
qreal m_uiScale;

QPixmap m_gfx[cc2::NUM_GRAPHICS];

Expand Down
46 changes: 42 additions & 4 deletions src/CC2Edit/CC2Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,27 @@ CC2EditMain::CC2EditMain(QWidget* parent)
}
m_gamePropsDock->raise();

findTilesets();
m_scaleGroup = new QActionGroup(this);
QAction* scale1x = m_scaleGroup->addAction(tr("Original Size"));
scale1x->setData(1);
scale1x->setCheckable(true);
scale1x->setStatusTip(tr("Render tiles at 1X scale"));
QAction* scale2x = m_scaleGroup->addAction(tr("Scale 2X"));
scale2x->setData(2);
scale2x->setCheckable(true);
scale2x->setStatusTip(tr("Render tiles at 2X scale (for high DPI displays)"));

for (QAction* scaleAction : m_scaleGroup->actions()) {
int scale = scaleAction->data().toInt();
connect(scaleAction, &QAction::triggered, this, [this, scale] {
m_currentTileset->setUiScale(qreal(scale));
if (m_zoomFactor != 0.0)
setZoomFactor(m_zoomFactor);
emit tilesetChanged(m_currentTileset);
});
}

populateTilesets();
if (m_tilesetGroup->actions().size() == 0) {
QMessageBox::critical(this, tr("Error loading tilesets"),
tr("Error: No tilesets found. Please check your CCTools installation"),
Expand Down Expand Up @@ -1081,6 +1101,12 @@ CC2EditMain::CC2EditMain(QWidget* parent)
else
m_actions[ActionZoomCust]->setChecked(true);

const int tilesetScale = settings.value(QStringLiteral("TilesetScale"), 1).toInt();
for (QAction* scaleAction : m_scaleGroup->actions()) {
if (scaleAction->data().toInt() == tilesetScale)
scaleAction->activate(QAction::Trigger);
}

// Set default editor tiles
cc2::Tile defTile(cc2::Tile::Wall);
setLeftTile(defTile);
Expand Down Expand Up @@ -1420,7 +1446,7 @@ void CC2EditMain::populateRecentFiles()
});
}

void CC2EditMain::findTilesets()
void CC2EditMain::populateTilesets()
{
m_tilesetMenu->clear();

Expand Down Expand Up @@ -1464,6 +1490,10 @@ void CC2EditMain::findTilesets()
tilesets.removeDuplicates();
for (const QString& file : tilesets)
registerTileset(file);

m_tilesetMenu->addSeparator();
for (QAction* scaleAction : m_scaleGroup->actions())
m_tilesetMenu->addAction(scaleAction);
}

void CC2EditMain::loadTileset(CC2ETileset* tileset)
Expand Down Expand Up @@ -1518,7 +1548,7 @@ CC2EditorWidget* CC2EditMain::addEditor(cc2::Map* map, const QString& filename,
editor->setLeftTile(m_leftTile);
editor->setRightTile(m_rightTile);
if (m_zoomFactor != 0.0)
editor->setZoom(m_zoomFactor);
editor->setZoom(m_zoomFactor * m_currentTileset->uiScale());

if (filename.isEmpty()) {
m_editorTabs->addTab(scroll, tr("Untitled Map"));
Expand Down Expand Up @@ -1675,6 +1705,11 @@ void CC2EditMain::closeEvent(QCloseEvent* event)
settings.setValue(QStringLiteral("ViewMonsterPaths"),
m_actions[ActionViewMonsterPaths]->isChecked());
settings.setValue(QStringLiteral("TilesetName"), m_currentTileset->filename());

for (QAction* scaleAction : m_scaleGroup->actions()) {
if (scaleAction->isChecked())
settings.setValue(QStringLiteral("TilesetScale"), scaleAction->data().toInt());
}
}

void CC2EditMain::resizeEvent(QResizeEvent* event)
Expand Down Expand Up @@ -2348,11 +2383,14 @@ void CC2EditMain::onTilePicked(int x, int y)

void CC2EditMain::setZoomFactor(double zoom)
{
Q_ASSERT(m_currentTileset != nullptr);

m_zoomFactor = zoom;
const double adjustedZoom = zoom * m_currentTileset->uiScale();
for (int i = 0; i < m_editorTabs->count(); ++i) {
auto editor = getEditorAt(i);
if (editor)
editor->setZoom(m_zoomFactor);
editor->setZoom(adjustedZoom);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/CC2Edit/CC2Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CC2EditMain : public QMainWindow {
bool saveMap(cc2::Map* map, const QString& filename);
bool saveScript(const QString& script, const QString& filename);

void findTilesets();
void populateTilesets();
void loadTileset(CC2ETileset* tileset);

CC2EditorWidget* getEditorAt(int idx);
Expand Down Expand Up @@ -161,6 +161,7 @@ private slots:
QMenu* m_recentFiles;
QMenu* m_tilesetMenu;
QActionGroup* m_tilesetGroup;
QActionGroup* m_scaleGroup;
CC2ETileset* m_currentTileset;
ActionType m_savedDrawMode;
CC2EditorWidget::DrawMode m_currentDrawMode;
Expand Down
2 changes: 1 addition & 1 deletion src/CC2Edit/TileInspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ TileInspector::TileInspector(QWidget* parent)
void TileInspector::setTileset(CC2ETileset* tileset)
{
m_tileset = tileset;
m_layers->setIconSize(m_tileset->qsize());
m_layers->setIconSize(m_tileset->iconSize());
}

void TileInspector::loadTile(const cc2::Tile& tile)
Expand Down
16 changes: 9 additions & 7 deletions src/CC2Edit/TileWidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void LayerWidget::paintEvent(QPaintEvent* event)
return;

QPainter painter(this);
painter.scale(m_tileset->uiScale(), m_tileset->uiScale());
const int halfway = m_tileset->size() / 2;
m_tileset->drawAt(painter, halfway, halfway, &m_lower, false);
m_tileset->drawAt(painter, 0, 0, &m_upper, false);
Expand All @@ -80,7 +81,7 @@ const cc2::Tile* TileListWidget::tile(int index)

void TileListWidget::setTileImages(CC2ETileset* tileset)
{
setIconSize(tileset->qsize());
setIconSize(tileset->iconSize());
for (int i = 0; i < count(); ++i)
item(i)->setIcon(tileset->getIcon(tile(i)));
}
Expand Down Expand Up @@ -283,6 +284,7 @@ void BigTileWidget::paintEvent(QPaintEvent* event)
return;

QPainter painter(this);
painter.scale(m_tileset->uiScale(), m_tileset->uiScale());
int x = 0, y = 0;
for (const cc2::Tile& tile : tileList()) {
if (tile.type() != cc2::Tile::Invalid)
Expand All @@ -296,12 +298,12 @@ void BigTileWidget::paintEvent(QPaintEvent* event)

void BigTileWidget::mousePressEvent(QMouseEvent* event)
{
if (!m_tileset || event->x() >= (m_tileset->size() * 9))
if (!m_tileset || event->x() >= (m_tileset->uiSize() * 9))
return;

const auto& tiles = tileList();
size_t which = ((event->y() / m_tileset->size()) * 9)
+ (event->x() / m_tileset->size());
const int tileSize = m_tileset->uiSize();
size_t which = ((event->y() / tileSize) * 9) + (event->x() / tileSize);
if (which >= tiles.size() || tiles[which].type() == cc2::Tile::Invalid)
return;
if (event->button() == Qt::LeftButton)
Expand All @@ -313,14 +315,14 @@ void BigTileWidget::mousePressEvent(QMouseEvent* event)
void BigTileWidget::mouseMoveEvent(QMouseEvent* event)
{
QWidget::mouseMoveEvent(event);
if (!m_tileset || event->x() >= (m_tileset->size() * 9)) {
if (!m_tileset || event->x() >= (m_tileset->uiSize() * 9)) {
setToolTip(QString());
return;
}

const auto& tiles = tileList();
size_t which = ((event->y() / m_tileset->size()) * 9)
+ (event->x() / m_tileset->size());
const int tileSize = m_tileset->uiSize();
size_t which = ((event->y() / tileSize) * 9) + (event->x() / tileSize);
if (which >= tiles.size() || tiles[which].type() == cc2::Tile::Invalid) {
setToolTip(QString());
return;
Expand Down
5 changes: 3 additions & 2 deletions src/CC2Edit/TileWidgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class LayerWidget : public QFrame {
{
if (!m_tileset)
return QSize();
int size = (m_tileset->size() * 3) / 2;
const int size = (m_tileset->uiSize() * 3) / 2;
return QSize(size, size);
}

Expand Down Expand Up @@ -87,7 +87,8 @@ class BigTileWidget : public QWidget {

QSize sizeHint() const override
{
int tsetSize = m_tileset ? m_tileset->size() : 32;
const int tsetSize = m_tileset
? (m_tileset->size() * m_tileset->uiScale()) : 32;
return QSize(tsetSize * 9, tsetSize * 15);
}

Expand Down
44 changes: 40 additions & 4 deletions src/CCEdit/CCEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,27 @@ CCEditMain::CCEditMain(QWidget* parent)
}
m_levelManDock->raise();

findTilesets();
m_scaleGroup = new QActionGroup(this);
QAction* scale1x = m_scaleGroup->addAction(tr("Original Size"));
scale1x->setData(1);
scale1x->setCheckable(true);
scale1x->setStatusTip(tr("Render tiles at 1X scale"));
QAction* scale2x = m_scaleGroup->addAction(tr("Scale 2X"));
scale2x->setData(2);
scale2x->setCheckable(true);
scale2x->setStatusTip(tr("Render tiles at 2X scale (for high DPI displays)"));

for (QAction* scaleAction : m_scaleGroup->actions()) {
int scale = scaleAction->data().toInt();
connect(scaleAction, &QAction::triggered, this, [this, scale] {
m_currentTileset->setUiScale(qreal(scale));
if (m_zoomFactor != 0.0)
setZoomFactor(m_zoomFactor);
emit tilesetChanged(m_currentTileset);
});
}

populateTilesets();
if (m_tilesetGroup->actions().size() == 0) {
QMessageBox::critical(this, tr("Error loading tilesets"),
tr("Error: No tilesets found. Please check your CCTools installation"),
Expand Down Expand Up @@ -840,6 +860,12 @@ CCEditMain::CCEditMain(QWidget* parent)
else
m_actions[ActionZoomCust]->setChecked(true);

const int tilesetScale = settings.value(QStringLiteral("TilesetScale"), 1).toInt();
for (QAction* scaleAction : m_scaleGroup->actions()) {
if (scaleAction->data().toInt() == tilesetScale)
scaleAction->activate(QAction::Trigger);
}

setLeftTile(ccl::TileWall);
setRightTile(ccl::TileFloor);
onSelectLevel(-1);
Expand Down Expand Up @@ -1101,6 +1127,11 @@ void CCEditMain::closeEvent(QCloseEvent* event)
m_actions[ActionViewErrors]->isChecked());
settings.setValue(QStringLiteral("TilesetName"),
m_currentTileset->filename());

for (QAction* scaleAction : m_scaleGroup->actions()) {
if (scaleAction->isChecked())
settings.setValue(QStringLiteral("TilesetScale"), scaleAction->data().toInt());
}
}

void CCEditMain::resizeEvent(QResizeEvent* event)
Expand Down Expand Up @@ -1215,7 +1246,7 @@ void CCEditMain::registerTileset(const QString& filename)
m_tilesetGroup->addAction(menuItem);
}

void CCEditMain::findTilesets()
void CCEditMain::populateTilesets()
{
m_tilesetMenu->clear();

Expand Down Expand Up @@ -1259,6 +1290,10 @@ void CCEditMain::findTilesets()
tilesets.removeDuplicates();
for (const QString& file : tilesets)
registerTileset(file);

m_tilesetMenu->addSeparator();
for (QAction* scaleAction : m_scaleGroup->actions())
m_tilesetMenu->addAction(scaleAction);
}

void CCEditMain::loadLevel(int levelNum)
Expand Down Expand Up @@ -1339,7 +1374,7 @@ EditorWidget* CCEditMain::addEditor(ccl::LevelData* level)
editor->setLeftTile(m_leftTile);
editor->setRightTile(m_rightTile);
if (m_zoomFactor != 0.0)
editor->setZoom(m_zoomFactor);
editor->setZoom(m_zoomFactor * m_currentTileset->uiScale());
m_editorTabs->addFloatingTab(scroll, ccl::fromLatin1(level->name()));
resizeEvent(nullptr);

Expand Down Expand Up @@ -1914,8 +1949,9 @@ void CCEditMain::onViewErrorsToggled(bool view)
void CCEditMain::setZoomFactor(double zoom)
{
m_zoomFactor = zoom;
const double adjustedZoom = zoom * m_currentTileset->uiScale();
for (int i=0; i<m_editorTabs->count(); ++i)
getEditorAt(i)->setZoom(m_zoomFactor);
getEditorAt(i)->setZoom(adjustedZoom);
}

void CCEditMain::onZoomCust()
Expand Down
3 changes: 2 additions & 1 deletion src/CCEdit/CCEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CCEditMain : public QMainWindow {
void saveLevelset(const QString& filename);
bool closeLevelset();
void loadTileset(CCETileset* tileset);
void findTilesets();
void populateTilesets();

void loadLevel(int level);
void loadLevel(ccl::LevelData* levelPtr);
Expand Down Expand Up @@ -91,6 +91,7 @@ public slots:
QMenu* m_recentFiles;
QMenu* m_tilesetMenu;
QActionGroup* m_tilesetGroup;
QActionGroup* m_scaleGroup;
QUndoStack* m_undoStack;
EditorUndoCommand* m_undoCommand;
CCETileset* m_currentTileset;
Expand Down
Loading

0 comments on commit 77fd41c

Please sign in to comment.