Skip to content

Commit

Permalink
Add support for importing symbols from fonts as a spritesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCWills committed Sep 13, 2024
1 parent 6278bf3 commit 5b0b88d
Show file tree
Hide file tree
Showing 7 changed files with 608 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(PROJECT_SOURCES
source/d1formats/d1til.cpp
source/d1formats/d1trn.cpp
source/dialogs/exportdialog.cpp
source/dialogs/importdialog.cpp
source/views/view.cpp
source/views/levelcelview.cpp
source/widgets/leveltabframewidget.cpp
Expand Down
120 changes: 120 additions & 0 deletions source/dialogs/importdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "importdialog.h"

#include <QColorDialog>
#include <QFileDialog>
#include <QFontDatabase>
#include <QImageWriter>
#include <QMessageBox>
#include <QPainter>
#include <algorithm>

#include "mainwindow.h"
#include "ui_importdialog.h"

ImportDialog::ImportDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::ImportDialog())
{
ui->setupUi(this);
}

ImportDialog::~ImportDialog()
{
delete ui;
}

void ImportDialog::initialize()
{
// - remember the last selected type
QComboBox *typeBox = this->ui->typeComboBox;
QString lastFmt = typeBox->currentText();
if (lastFmt.isEmpty()) {
lastFmt = "Font";
}
typeBox->clear();
typeBox->addItem("Font");
typeBox->setCurrentIndex(typeBox->findText(lastFmt));

setRenderColor(this->renderColor);
}

void ImportDialog::setRenderColor(QColor color)
{
QString red = QString::number(color.red());
QString green = QString::number(color.green());
QString blue = QString::number(color.blue());
QString styleSheet = QString("background: rgb(") + red + QString(",") + green + QString(",") + blue + QString(")");
ui->fontColorButton->setStyleSheet(styleSheet);
this->renderColor = color;
}

QString ImportDialog::getFileFormatExtension()
{
return "." + this->ui->typeComboBox->currentText().toLower();
}

void ImportDialog::on_inputFileBrowseButton_clicked()
{
QString selectedDirectory = QFileDialog::getOpenFileName(
this, "Select Font File", QString(), "Fonts (*.ttf *.otf)");

if (selectedDirectory.isEmpty())
return;

ui->inputFileEdit->setText(selectedDirectory);
}

void ImportDialog::on_fontSymbolsEdit_textChanged(const QString &text)
{
bool ok = false;
uint test = text.toUInt(&ok, 16);
if (!ok) {
ui->fontSymbolsRangeLabel->setText("Error");
return;
}

QString pad = text.toLower();
while (pad.size() < 2)
pad = "0" + pad;

QString start = "U+" + pad + "00";
QString end = "U+" + pad + "ff";
ui->fontSymbolsRangeLabel->setText(start + " - " + end);
}

void ImportDialog::on_fontColorButton_clicked()
{
QColor color = QColorDialog::getColor();
setRenderColor(color);
}

void ImportDialog::on_importButton_clicked()
{
if (ui->inputFileEdit->text() == "") {
QMessageBox::warning(this, "Warning", "Input file is missing, please choose an input folder.");
return;
}

try {
MainWindow *mainWindow = dynamic_cast<MainWindow *>(this->parent());
if (mainWindow == nullptr) {
QMessageBox::critical(this, "Error", "Window not found.");
return;
}

QString filePath = ui->inputFileEdit->text();
int pointSize = ui->fontSizeEdit->text().toInt();
uint symbolPrefix = ui->fontSymbolsEdit->text().toUInt() << 8;
mainWindow->openFontFile(filePath, this->renderColor, pointSize, symbolPrefix);
} catch (...) {
QMessageBox::critical(this, "Error", "Import Failed.");
return;
}

this->close();
}

void ImportDialog::on_importCancelButton_clicked()
{
this->close();
}
44 changes: 44 additions & 0 deletions source/dialogs/importdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <QDialog>
#include <QProgressDialog>

#include "d1formats/d1amp.h"
#include "d1formats/d1gfx.h"
#include "d1formats/d1min.h"
#include "d1formats/d1sol.h"
#include "d1formats/d1til.h"

namespace Ui {
class ImportDialog;
}

// subtiles per line if the output is groupped, an odd number to ensure it is not recognized as a flat tile
#define EXPORT_SUBTILES_PER_LINE 15

// frames per line if the output of a tileset-frames is groupped, an odd number to ensure it is not recognized as a flat tile or as subtiles
#define EXPORT_LVLFRAMES_PER_LINE 31

class ImportDialog : public QDialog {
Q_OBJECT

public:
explicit ImportDialog(QWidget *parent = nullptr);
~ImportDialog();

void initialize();

private slots:
void on_inputFileBrowseButton_clicked();
void on_fontSymbolsEdit_textChanged(const QString &text);
void on_fontColorButton_clicked();
void on_importButton_clicked();
void on_importCancelButton_clicked();

private:
void setRenderColor(QColor color);
QString getFileFormatExtension();

Ui::ImportDialog *ui;
QColor renderColor = QColor::fromRgb(204, 183, 117);
};
Loading

0 comments on commit 5b0b88d

Please sign in to comment.