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

[Do Not Merge] Pencil2D ASCII Edition #1208

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add functional ASCII image renderer
  • Loading branch information
scribblemaniac committed Apr 1, 2019
commit 4b850741c28169bf1d0f8783cd59e73783d54b7b
9 changes: 6 additions & 3 deletions app/app.pro
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ HEADERS += \
src/doubleprogressdialog.h \
src/colorslider.h \
src/checkupdatesdialog.h \
src/consolewindow.h
src/consolewindow.h \
src/AsciiPreviewDialog.h

SOURCES += \
src/main.cpp \
Expand Down Expand Up @@ -91,7 +92,8 @@ SOURCES += \
src/doubleprogressdialog.cpp \
src/colorslider.cpp \
src/checkupdatesdialog.cpp \
src/consolewindow.cpp
src/consolewindow.cpp \
src/AsciiPreviewDialog.cpp

FORMS += \
ui/mainwindow2.ui \
Expand All @@ -114,7 +116,8 @@ FORMS += \
ui/filespage.ui \
ui/toolspage.ui \
ui/toolboxwidget.ui \
ui/consolewindow.ui
ui/consolewindow.ui \
src/AsciiPreviewDialog.ui



Expand Down
14 changes: 14 additions & 0 deletions app/src/AsciiPreviewDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "AsciiPreviewDialog.h"
#include "ui_AsciiPreviewDialog.h"

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

AsciiPreviewDialog::~AsciiPreviewDialog()
{
delete ui;
}
22 changes: 22 additions & 0 deletions app/src/AsciiPreviewDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef ASCIIPREVIEWDIALOG_H
#define ASCIIPREVIEWDIALOG_H

#include <QDialog>

namespace Ui {
class AsciiPreviewDialog;
}

class AsciiPreviewDialog : public QDialog
{
Q_OBJECT

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

private:
Ui::AsciiPreviewDialog *ui;
};

#endif // ASCIIPREVIEWDIALOG_H
18 changes: 18 additions & 0 deletions app/src/AsciiPreviewDialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<ui version="4.0">
<class>AsciiPreviewDialog</class>
<widget class="QDialog" name="AsciiPreviewDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
</widget>
<resources/>
<connections/>
</ui>
73 changes: 72 additions & 1 deletion app/src/consolewindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#include "toolmanager.h"
#include "pointerevent.h"
#include "viewmanager.h"
#include "colormanager.h"
#include "layermanager.h"
#include "layercamera.h"
#include "layerbitmap.h"
#include "asciiimage.h"

ConsoleWindow::ConsoleWindow(QWidget *parent) :
QMainWindow(parent),
Expand All @@ -27,7 +32,18 @@ ConsoleWindow::ConsoleWindow(QWidget *parent) :
ui->prompt->setFocus();

mMainWindow = new MainWindow2(this);
mMainWindow->show();
mMainWindow->mEditor->color()->setColor(Qt::black);
LayerManager *lm = mMainWindow->mEditor->layers();
mCamLayer = lm->createCameraLayer("ASCII Camera");
mCamLayer->setViewRect(QRect(mMainWindow->mEditor->view()->mapScreenToCanvas(QPoint(0, 0)).toPoint(), QSize(50, 50)));
LayerCamera *curCam;
while ((curCam = static_cast<LayerCamera*>(lm->getLastCameraLayer())) != mCamLayer)
{
lm->deleteLayer(lm->getIndex(curCam));
}
mDrawingLayer = lm->createBitmapLayer("ASCII Bitmap");
lm->setCurrentLayer(mDrawingLayer);

//mMainWindow->mEditor->view()->setCanvasSize(QSize(100, 100));

// Play music
Expand Down Expand Up @@ -184,6 +200,30 @@ void ConsoleWindow::runCommand()
print(tr("Cannot plug in %1").arg(command.mid(tr("plug in ").size())));
}
}
else if (command == "iddqd")
{
// A debug command to display the main window
if (mMainWindow->isVisible())
{
mMainWindow->hide();
}
else
{
mMainWindow->show();
}
}
else if (command == tr("render"))
{
printPaper(QStringList() << "50" << "50");
}
else if (command == tr("render "))
{
printPaper(command.mid(tr("render ").size()).split(" "));
}
else
{
// TODO
}
}

void ConsoleWindow::print(QString s)
Expand Down Expand Up @@ -300,6 +340,37 @@ void ConsoleWindow::printEquip(QString term, QString arg)
}
}

void ConsoleWindow::printPaper(QStringList args)
{
// Check for exactly two arguments (x, y)
if (args.size() != 2)
{
print(tr("Needs exactly needs x and y coordinates of where to start pressing. Use PRESS <x> <y>"));
return;
}

QSize cameraSize = mCamLayer->getViewSize();
int currentFrame = mMainWindow->mEditor->currentFrame();
QImage imageToExport(cameraSize, QImage::Format_ARGB32_Premultiplied);

QColor bgColor = Qt::white;
bgColor.setAlpha(0);
imageToExport.fill(bgColor);

//QTransform centralizeCamera;
//centralizeCamera.translate(cameraSize.width() / 2, cameraSize.height() / 2);

QPainter painter(&imageToExport);
painter.setWorldTransform(mCamLayer->getViewAtFrame(currentFrame));
painter.setWindow(QRect(0, 0, cameraSize.width(), cameraSize.height()));

mMainWindow->mEditor->object()->paintImage(painter, currentFrame, false, true);
imageToExport.save("/Users/connor/Downloads/ascii.png");

QString output = AsciiImage::convert(imageToExport);
print(output);
}

void ConsoleWindow::doPress(QStringList args)
{
// Check to make sure we haven't already pressed
Expand Down
5 changes: 5 additions & 0 deletions app/src/consolewindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class ConsoleWindow;

class MainWindow2;
class QMediaPlayer;
class LayerCamera;
class LayerBitmap;

class ConsoleWindow : public QMainWindow
{
Expand All @@ -22,6 +24,7 @@ class ConsoleWindow : public QMainWindow
void printHelp();
void printLook(QString arg);
void printEquip(QString term, QString arg);
void printPaper(QStringList args);

void doPress(QStringList args);
void doMove(QStringList args);
Expand All @@ -44,6 +47,8 @@ private slots:
int mDiscardedPaper = 0;
QPointF mCurrentPos;
bool mIsDrawing = false;
LayerCamera *mCamLayer;
LayerBitmap *mDrawingLayer;
};

#endif // CONSOLEWINDOW_H
6 changes: 4 additions & 2 deletions core_lib/core_lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ HEADERS += \
src/activeframepool.h \
src/external/platformhandler.h \
src/external/macosx/macosxnative.h \
src/util/pointerevent.h
src/util/pointerevent.h \
src/util/asciiimage.h


SOURCES += src/graphics/bitmap/bitmapimage.cpp \
Expand Down Expand Up @@ -161,7 +162,8 @@ SOURCES += src/graphics/bitmap/bitmapimage.cpp \
src/miniz.cpp \
src/qminiz.cpp \
src/activeframepool.cpp \
src/util/pointerevent.cpp
src/util/pointerevent.cpp \
src/util/asciiimage.cpp

FORMS += \
ui/camerapropertiesdialog.ui
Expand Down
4 changes: 2 additions & 2 deletions core_lib/src/managers/layermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ class LayerManager : public BaseManager
int animationLength(bool includeSounds = true);
void notifyAnimationLengthChanged();

int getIndex(Layer*) const;

Q_SIGNALS:
void currentLayerChanged(int index);
void layerCountChanged(int count);
void animationLengthChanged(int length);
void layerDeleted(int index);

private:
int getIndex(Layer*) const;

int mLastCameraLayerIdx = 0;
};

Expand Down
3 changes: 2 additions & 1 deletion core_lib/src/managers/viewmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ void ViewManager::flipVertical(bool b)
void ViewManager::setCanvasSize(QSize size)
{
mCanvasSize = size;
mCentre = QTransform::fromTranslate(mCanvasSize.width() / 2.f, mCanvasSize.height() / 2.f);
// Don't center on resize because the camera must stay in the top left
//mCentre = QTransform::fromTranslate(mCanvasSize.width() / 2.f, mCanvasSize.height() / 2.f);

updateViewTransforms();
Q_EMIT viewChanged();
Expand Down
5 changes: 5 additions & 0 deletions core_lib/src/structure/layercamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ QSize LayerCamera::getViewSize()
return viewRect.size();
}

void LayerCamera::setViewRect(QRect rect)
{
viewRect = rect;
}

void LayerCamera::loadImageAtFrame( int frameNumber, qreal dx, qreal dy, qreal rotate, qreal scale)
{
if ( keyExists( frameNumber ) )
Expand Down
2 changes: 2 additions & 0 deletions core_lib/src/structure/layercamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class LayerCamera : public Layer
QRect getViewRect();
QSize getViewSize();

void setViewRect(QRect rect);

signals:
void resolutionChanged();

Expand Down
41 changes: 41 additions & 0 deletions core_lib/src/util/asciiimage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "asciiimage.h"

#include <QImage>
#include <QColor>
#include <QtMath>
#include <QtDebug>

const QList<QChar> AsciiImage::ASCII_LIST = {'#','#','X','X','x','x','x','+','+','+','=','=','=','-','-','-',';',';',',',',','.','.','.'};

AsciiImage::AsciiImage()
{

}

QString AsciiImage::convert(QImage img)
{
QString output = "";

for(int y = 0; y < img.height(); y++)
{
QRgb *rgbLine = (QRgb *) img.scanLine(y);
for(int x = 0; x < img.width(); x++)
{
output.append(getPixel(rgbLine[x], qAlpha(rgbLine[x])));
}
output.append('\n');
}
return output;
}

QChar AsciiImage::getPixel(QColor c, ushort alpha)
{
if(!c.isValid() || alpha == 0)
{
return ' ';
}

// Algorithm and mappings from https://github.com/zachwill/asciifi/blob/master/static/js/application.js
return ASCII_LIST[qFloor(c.valueF() * ASCII_LIST.size())];
}

22 changes: 22 additions & 0 deletions core_lib/src/util/asciiimage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef ASCIIIMAGE_H
#define ASCIIIMAGE_H

#include <QtGlobal>

class QImage;
class QColor;
class QChar;

class AsciiImage
{
public:
AsciiImage();

static QString convert(QImage img);
static QChar getPixel(QColor c, ushort alpha);

private:
static const QList<QChar> ASCII_LIST;
};

#endif // ASCIIIMAGE_H