diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2008-10-13 21:05:50 +0000 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2008-10-13 21:05:50 +0000 |
| commit | 460e7bdf3f6c1de69f41b02a16deb85522ae3c49 (patch) | |
| tree | 3b80ad00d5f021f63d3f890af2e017af314d2106 | |
| parent | b427a8cdf13aabe59af60acf0a4264d84ae3ff7a (diff) | |
| download | sigen-460e7bdf3f6c1de69f41b02a16deb85522ae3c49.tar.gz sigen-460e7bdf3f6c1de69f41b02a16deb85522ae3c49.tar.xz sigen-460e7bdf3f6c1de69f41b02a16deb85522ae3c49.zip | |
[FIX] Sprite no longer stores a QImage, but a QByteArray
[FIX] Preparing for move to a collaged map instead of a tiled map
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@278 6ecfd1a5-f3ed-3746-8530-beee90d26b22
| -rw-r--r-- | Changelog | 8 | ||||
| -rw-r--r-- | sigmod/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | sigmod/Matrix.h | 63 | ||||
| -rw-r--r-- | sigmod/Object.h | 18 | ||||
| -rw-r--r-- | sigmod/Script.cpp | 68 | ||||
| -rw-r--r-- | sigmod/Script.h | 46 | ||||
| -rw-r--r-- | sigmod/Sprite.cpp | 7 | ||||
| -rw-r--r-- | sigmod/Sprite.h | 8 | ||||
| -rw-r--r-- | sigmod/Tile.cpp | 3 | ||||
| -rw-r--r-- | sigmod/sigmod.pro | 1 | ||||
| -rw-r--r-- | sigmodr/BadgeUI.cpp | 3 | ||||
| -rw-r--r-- | sigmodr/MapUI.cpp | 6 | ||||
| -rw-r--r-- | sigmodr/SpeciesUI.cpp | 3 | ||||
| -rw-r--r-- | sigmodr/SpriteUI.cpp | 34 | ||||
| -rw-r--r-- | sigmodr/TileUI.cpp | 12 | ||||
| -rw-r--r-- | sigmodr/TilemapModel.cpp | 8 | ||||
| -rw-r--r-- | sigscript/SpriteWrapper.cpp | 6 | ||||
| -rw-r--r-- | sigscript/SpriteWrapper.h | 5 |
18 files changed, 203 insertions, 98 deletions
@@ -1,4 +1,12 @@ ----------------- +Rev: 278 +Date: 13 October 2008 +User: MathStuf +----------------- +[FIX] Sprite no longer stores a QImage, but a QByteArray +[FIX] Preparing for move to a collaged map instead of a tiled map + +----------------- Rev: 277 Date: 13 October 2008 User: MathStuf diff --git a/sigmod/CMakeLists.txt b/sigmod/CMakeLists.txt index cf1d12fa..7663c0c9 100644 --- a/sigmod/CMakeLists.txt +++ b/sigmod/CMakeLists.txt @@ -69,6 +69,7 @@ SET(sigmod_SRCS Nature.cpp Object.cpp Rules.cpp + Script.cpp Sigmod.cpp Skin.cpp Sound.cpp @@ -98,7 +99,6 @@ SET_TARGET_PROPERTIES(sigmod ) TARGET_LINK_LIBRARIES(sigmod ${QT_QTCORE_LIBRARY} - ${QT_QTGUI_LIBRARY} ${QT_QTXML_LIBRARY} ) diff --git a/sigmod/Matrix.h b/sigmod/Matrix.h index 16d3a5d0..1726804c 100644 --- a/sigmod/Matrix.h +++ b/sigmod/Matrix.h @@ -127,6 +127,27 @@ template<class T> class SIGMOD_EXPORT Matrix QVector<T> column(const int column) const; /** + * Get a matrix from within the matrix. + * + * \param top The top-most part of the matrix. + * \param left The left-most part of the matrix. + * \param height The height of the new matrix. + * \param width The width of the new matrix. + * \return The matrix. + */ + Matrix<T> mid(const int top, const int left, const int height, const int width) const; + /** + * Set a matrix from within the matrix. + * + * \param top The top-most part of the matrix. + * \param left The left-most part of the matrix. + * \param height The height of the new matrix. + * \param width The width of the new matrix. + * \param value The value to set the mid to. + */ + void setMid(const int top, const int left, const int height, const int width, const T& value); + + /** * \return The height of the matrix. */ int height() const; @@ -184,6 +205,7 @@ template<class T> void Matrix<T>::addColumn(const T& value) template<class T> void Matrix<T>::insertRow(const int row, const T& value) { + Q_ASSERT(0 <= row); Q_ASSERT(row <= height()); if (!height() || !width()) m_matrix.append(QVector<T>(1, value)); @@ -193,6 +215,7 @@ template<class T> void Matrix<T>::insertRow(const int row, const T& value) template<class T> void Matrix<T>::insertColumn(const int column, const T& value) { + Q_ASSERT(0 <= column); Q_ASSERT(column <= width()); if (!height() || !width()) m_matrix.append(QVector<T>(1, value)); @@ -205,6 +228,7 @@ template<class T> void Matrix<T>::insertColumn(const int column, const T& value) template<class T> void Matrix<T>::deleteRow(const int row) { + Q_ASSERT(0 <= row); Q_ASSERT(row < height()); if (height() == 1) m_matrix.clear(); @@ -214,6 +238,7 @@ template<class T> void Matrix<T>::deleteRow(const int row) template<class T> void Matrix<T>::deleteColumn(const int column) { + Q_ASSERT(0 <= column); Q_ASSERT(column < width()); if (width() == 1) m_matrix.clear(); @@ -243,12 +268,14 @@ template<class T> void Matrix<T>::resize(const int newHeight, const int newWidth template<class T> QVector<T> Matrix<T>::row(const int row) const { + Q_ASSERT(0 <= row); Q_ASSERT(row < height()); return m_matrix.at(row); } template<class T> QVector<T> Matrix<T>::column(const int column) const { + Q_ASSERT(0 <= column); Q_ASSERT(column < width()); QVector<T> col; foreach (QVector<T> row, m_matrix) @@ -256,6 +283,42 @@ template<class T> QVector<T> Matrix<T>::column(const int column) const return col; } +template<class T> Matrix<T> Matrix<T>::mid(const int top, const int left, const int height, const int width) const +{ + Q_ASSERT(0 <= top); + Q_ASSERT(top < height()); + Q_ASSERT(0 <= left); + Q_ASSERT(left < width()); + Q_ASSERT(0 <= height); + Q_ASSERT(top + height <= height()); + Q_ASSERT(0 <= width); + Q_ASSERT(left + width <= width()); + Matrix<T> matrix(height, width); + for (int i = 0; i < height; ++i) + { + for (int j = 0; j < width; ++j) + matrix(i, j) = operator()(top + i, left + j); + } + return matrix; +} + +template<class T> void Matrix<T>::setMid(const int top, const int left, const int height, const int width, const T& value) +{ + Q_ASSERT(0 <= top); + Q_ASSERT(top < height()); + Q_ASSERT(0 <= left); + Q_ASSERT(left < width()); + Q_ASSERT(0 <= height); + Q_ASSERT(top + height <= height()); + Q_ASSERT(0 <= width); + Q_ASSERT(left + width <= width()); + for (int i = 0; i < height; ++i) + { + for (int j = 0; j < width; ++j) + operator()(top + i, left + j) = value; + } +} + template<class T> int Matrix<T>::height() const { return m_matrix.size(); diff --git a/sigmod/Object.h b/sigmod/Object.h index efb20abb..6e0390a7 100644 --- a/sigmod/Object.h +++ b/sigmod/Object.h @@ -34,7 +34,6 @@ #include <QtCore/QPoint> #include <QtCore/QVariant> #include <QtCore/QVarLengthArray> -#include <QtGui/QImage> #include <QtXml/QDomElement> // C includes @@ -204,11 +203,6 @@ template<> inline void Object::loadValue<QPoint>(const QDomElement& xml, QPoint* value->setY(xml.attribute("y", "0").toInt()); } -template<> inline void Object::loadValue<QImage>(const QDomElement& xml, QImage* value) -{ - *value = QImage::fromData(QByteArray::fromBase64(xml.firstChild().toText().data().toUtf8())); -} - template<> inline void Object::loadValue<QByteArray>(const QDomElement& xml, QByteArray* value) { *value = QByteArray::fromBase64(xml.firstChild().toText().data().toUtf8()); @@ -305,18 +299,6 @@ template<> inline QDomElement Object::saveValue<QPoint>(const QString& name, con return element; } -template<> inline QDomElement Object::saveValue<QImage>(const QString& name, const QImage& value) -{ - QDomElement element = QDomDocument().createElement(name); - QByteArray bytes; - QBuffer* buffer = new QBuffer(&bytes); - buffer->open(QIODevice::WriteOnly); - value.save(buffer, "PNG", 0); - element.appendChild(QDomDocument().createTextNode(bytes.toBase64())); - delete buffer; - return element; -} - template<> inline QDomElement Object::saveValue<QByteArray>(const QString& name, const QByteArray& value) { QDomElement element = QDomDocument().createElement(name); diff --git a/sigmod/Script.cpp b/sigmod/Script.cpp new file mode 100644 index 00000000..55dc02b0 --- /dev/null +++ b/sigmod/Script.cpp @@ -0,0 +1,68 @@ +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * \file sigmod/Script.cpp + */ + +// Header include +#include "Script.h" + +Sigmod::Script::Script(const QString& interpreter, const QString& script) : + m_interpreter(interpreter), + m_script(script) +{ +} + +void Sigmod::Script::setInterpreter(const QString& interpreter) +{ + m_interpreter = interpreter; +} + +void Sigmod::Script::setScript(const QString& script) +{ + m_script = script; +} + +QString Sigmod::Script::interpreter() const +{ + return m_interpreter; +} + +QString Sigmod::Script::script() const +{ + return m_script; +} + +Sigmod::Script& Sigmod::Script::operator=(const Script& rhs) +{ + if (this == &rhs) + return *this; + m_interpreter = rhs.m_interpreter; + m_script = rhs.m_script; + return *this; +} + +bool Sigmod::Script::operator==(const Script& rhs) const +{ + return ((m_interpreter == rhs.m_interpreter) && (m_script == rhs.m_script)); +} + +bool Sigmod::Script::operator!=(const Script& rhs) const +{ + return !(*this == rhs); +} diff --git a/sigmod/Script.h b/sigmod/Script.h index c884aaf5..13c09aac 100644 --- a/sigmod/Script.h +++ b/sigmod/Script.h @@ -88,52 +88,6 @@ class SIGMOD_EXPORT Script QString m_interpreter; QString m_script; }; - -inline Script::Script(const QString& interpreter, const QString& script) : - m_interpreter(interpreter), - m_script(script) -{ -} - -inline void Script::setInterpreter(const QString& interpreter) -{ - m_interpreter = interpreter; -} - -inline void Script::setScript(const QString& script) -{ - m_script = script; -} - -inline QString Script::interpreter() const -{ - return m_interpreter; -} - -inline QString Script::script() const -{ - return m_script; -} - -inline Script& Script::operator=(const Script& rhs) -{ - if (this == &rhs) - return *this; - m_interpreter = rhs.m_interpreter; - m_script = rhs.m_script; - return *this; -} - -inline bool Script::operator==(const Script& rhs) const -{ - return ((m_interpreter == rhs.m_interpreter) && (m_script == rhs.m_script)); -} - -inline bool Script::operator!=(const Script& rhs) const -{ - return !(*this == rhs); -} - } Q_DECLARE_METATYPE(Sigmod::Script) diff --git a/sigmod/Sprite.cpp b/sigmod/Sprite.cpp index 0261385a..562a9a30 100644 --- a/sigmod/Sprite.cpp +++ b/sigmod/Sprite.cpp @@ -80,10 +80,9 @@ void Sigmod::Sprite::setName(const QString& name) CHECK(name); } -void Sigmod::Sprite::setSprite(const QImage& sprite) +void Sigmod::Sprite::setSprite(const QByteArray& sprite) { - m_sprite = sprite; - emit(changed()); + CHECK(sprite); } QString Sigmod::Sprite::name() const @@ -91,7 +90,7 @@ QString Sigmod::Sprite::name() const return m_name; } -QImage Sigmod::Sprite::sprite() const +QByteArray Sigmod::Sprite::sprite() const { return m_sprite; } diff --git a/sigmod/Sprite.h b/sigmod/Sprite.h index b11f33fd..13b2fc19 100644 --- a/sigmod/Sprite.h +++ b/sigmod/Sprite.h @@ -22,7 +22,7 @@ #include "Object.h" // Qt includes -#include <QtGui/QImage> +#include <QtCore/QByteArray> namespace Sigmod { @@ -46,15 +46,15 @@ class SIGMOD_EXPORT Sprite : public Object QDomElement save() const; void setName(const QString& name); - void setSprite(const QImage& sprite); + void setSprite(const QByteArray& sprite); QString name() const; - QImage sprite() const; + QByteArray sprite() const; Sprite& operator=(const Sprite& rhs); private: QString m_name; - QImage m_sprite; + QByteArray m_sprite; }; } diff --git a/sigmod/Tile.cpp b/sigmod/Tile.cpp index 31f51c6b..b5c9d6e0 100644 --- a/sigmod/Tile.cpp +++ b/sigmod/Tile.cpp @@ -92,9 +92,6 @@ void Sigmod::Tile::setSprite(const int sprite) { if (qobject_cast<const Sigmod*>(sigmod())->spriteIndex(sprite) == INT_MAX) emit(error(bounds("sprite"))); - // FIXME: remove if doing collaging - else if (qobject_cast<const Sigmod*>(sigmod())->spriteById(sprite)->sprite().size() != QSize(64, 64)) - emit(error("Sprite is the wrong dimensions")); else CHECK(sprite); } diff --git a/sigmod/sigmod.pro b/sigmod/sigmod.pro index 5d482a4c..4823b875 100644 --- a/sigmod/sigmod.pro +++ b/sigmod/sigmod.pro @@ -40,6 +40,7 @@ SOURCES += Ability.cpp \ Nature.cpp \ Object.cpp \ Rules.cpp \ + Script.cpp \ Sigmod.cpp \ Skin.cpp \ Sound.cpp \ diff --git a/sigmodr/BadgeUI.cpp b/sigmodr/BadgeUI.cpp index 7dc7e7f3..99032be9 100644 --- a/sigmodr/BadgeUI.cpp +++ b/sigmodr/BadgeUI.cpp @@ -55,7 +55,8 @@ void Sigmodr::BadgeUI::refreshGui() for (int i = 0; i < sigmod()->spriteCount(); ++i) { const Sigmod::Sprite* sprite = sigmod()->sprite(i); - const QPixmap& icon = QPixmap::fromImage(sprite->sprite()); + QPixmap icon; + icon.loadFromData(sprite->sprite()); maxHeight = qMax(maxHeight, icon.height()); maxWidth = qMax(maxWidth, icon.width()); varFace->addItem(icon, sprite->name(), sprite->id()); diff --git a/sigmodr/MapUI.cpp b/sigmodr/MapUI.cpp index cefb3ee2..a6b575e4 100644 --- a/sigmodr/MapUI.cpp +++ b/sigmodr/MapUI.cpp @@ -72,7 +72,11 @@ void Sigmodr::MapUI::refreshGui() if (sigmod()->spriteIndex(tile->sprite()) == INT_MAX) varTile->addItem(tile->name(), tile->id()); else - varTile->addItem(QPixmap::fromImage(sigmod()->spriteById(tile->sprite())->sprite()), tile->name(), tile->id()); + { + QPixmap icon; + icon.loadFromData(sigmod()->spriteById(tile->sprite())->sprite()); + varTile->addItem(icon, tile->name(), tile->id()); + } } varTile->blockSignals(blockedTile); varTile->setEnabled(false); diff --git a/sigmodr/SpeciesUI.cpp b/sigmodr/SpeciesUI.cpp index 0ad403e0..69e77757 100644 --- a/sigmodr/SpeciesUI.cpp +++ b/sigmodr/SpeciesUI.cpp @@ -98,7 +98,8 @@ void Sigmodr::SpeciesUI::refreshGui() for (int i = 0; i < sigmod()->spriteCount(); ++i) { const Sigmod::Sprite* sprite = sigmod()->sprite(i); - const QPixmap& icon = QPixmap::fromImage(sprite->sprite()); + QPixmap icon; + icon.loadFromData(sprite->sprite()); maxHeight = qMax(maxHeight, icon.height()); maxWidth = qMax(maxWidth, icon.width()); varMaleFront->addItem(icon, sprite->name(), sprite->id()); diff --git a/sigmodr/SpriteUI.cpp b/sigmodr/SpriteUI.cpp index 4edd38f1..acd249bb 100644 --- a/sigmodr/SpriteUI.cpp +++ b/sigmodr/SpriteUI.cpp @@ -47,7 +47,9 @@ void Sigmodr::SpriteUI::initGui() void Sigmodr::SpriteUI::setGui() { varName->setText(qobject_cast<Sigmod::Sprite*>(modified())->name()); - varSprite->setPixmap(QPixmap::fromImage(qobject_cast<Sigmod::Sprite*>(modified())->sprite())); + QPixmap icon; + icon.loadFromData(qobject_cast<Sigmod::Sprite*>(modified())->sprite()); + varSprite->setPixmap(icon); } void Sigmodr::SpriteUI::apply() @@ -81,23 +83,33 @@ void Sigmodr::SpriteUI::on_buttonBrowse_pressed() KUrl url = dialog->selectedFile(); if (url.isValid()) { + QString path; + bool load = true; + bool removeTempFile = false; if (url.isLocalFile()) - { - qobject_cast<Sigmod::Sprite*>(modified())->setSprite(QImage(url.path())); - setGui(); - } + path = url.path(); else { - QString path; if (KIO::NetAccess::download(url, path, this)) - { - qobject_cast<Sigmod::Sprite*>(modified())->setSprite(QImage(path)); - setGui(); - KIO::NetAccess::removeTempFile(path); - } + removeTempFile = true; else + { KMessageBox::error(this, KIO::NetAccess::lastErrorString(), "KIO Error"); + load = false; + } + } + if (load) + { + QImage image(url.path()); + QByteArray bytes; + QBuffer buffer(&bytes); + buffer.open(QIODevice::WriteOnly); + image.save(&buffer); + qobject_cast<Sigmod::Sprite*>(modified())->setSprite(bytes); + setGui(); } + if (removeTempFile) + KIO::NetAccess::removeTempFile(path); } else KMessageBox::error(this, "The URL is not valid", "Malformed URL"); diff --git a/sigmodr/TileUI.cpp b/sigmodr/TileUI.cpp index acab991f..f360f438 100644 --- a/sigmodr/TileUI.cpp +++ b/sigmodr/TileUI.cpp @@ -49,16 +49,22 @@ void Sigmodr::TileUI::initGui() void Sigmodr::TileUI::refreshGui() { + int maxHeight = 0; + int maxWidth = 0; const bool blocked = varSprite->blockSignals(true); varSprite->clear(); for (int i = 0; i < sigmod()->spriteCount(); ++i) { const Sigmod::Sprite* sprite = sigmod()->sprite(i); - if (sprite->sprite().size() == QSize(64, 64)) - varSprite->addItem(QPixmap::fromImage(sprite->sprite()), sprite->name(), sprite->id()); + QPixmap icon; + icon.loadFromData(sprite->sprite()); + maxHeight = qMax(maxHeight, icon.height()); + maxWidth = qMax(maxWidth, icon.width()); + varSprite->addItem(icon, sprite->name(), sprite->id()); } varSprite->blockSignals(blocked); - varSprite->setIconSize(QSize(64, 64)); + const QSize maxSize(maxWidth, maxHeight); + varSprite->setIconSize(maxSize); } void Sigmodr::TileUI::setGui() diff --git a/sigmodr/TilemapModel.cpp b/sigmodr/TilemapModel.cpp index 44940619..62424007 100644 --- a/sigmodr/TilemapModel.cpp +++ b/sigmodr/TilemapModel.cpp @@ -44,9 +44,13 @@ QVariant Sigmodr::TilemapModel::data(const QModelIndex& index, int role) const { const Sigmod::Tile* tile = m_sigmod->tile(tileIndex); if (m_sigmod->spriteIndex(tile->sprite()) == INT_MAX) - return QPixmap(64, 64); + return QPixmap(); else - return QPixmap::fromImage(m_sigmod->spriteById(tile->sprite())->sprite()); + { + QPixmap icon; + icon.loadFromData(m_sigmod->spriteById(tile->sprite())->sprite()); + return icon; + } } } else if (role == Qt::EditRole) diff --git a/sigscript/SpriteWrapper.cpp b/sigscript/SpriteWrapper.cpp index 4b972574..6a1c9614 100644 --- a/sigscript/SpriteWrapper.cpp +++ b/sigscript/SpriteWrapper.cpp @@ -39,7 +39,9 @@ QString Sigscript::SpriteWrapper::name() const return m_sprite->name(); } -QImage Sigscript::SpriteWrapper::sprite() const +QPixmap Sigscript::SpriteWrapper::sprite() const { - return m_sprite->sprite(); + QPixmap pixmap; + pixmap.loadFromData(m_sprite->sprite()); + return pixmap; } diff --git a/sigscript/SpriteWrapper.h b/sigscript/SpriteWrapper.h index 5d35563b..4451852e 100644 --- a/sigscript/SpriteWrapper.h +++ b/sigscript/SpriteWrapper.h @@ -24,6 +24,9 @@ // Sigmod includes #include "../sigmod/Sprite.h" +// Qt includes +#include <QtGui/QPixmap> + namespace Sigscript { class SIGSCRIPT_EXPORT SpriteWrapper : public ObjectWrapper @@ -34,7 +37,7 @@ class SIGSCRIPT_EXPORT SpriteWrapper : public ObjectWrapper static SpriteWrapper* create(const Sigmod::Sprite* sprite, SigmodWrapper* parent); Q_SCRIPTABLE QString name() const; - Q_SCRIPTABLE QImage sprite() const; + Q_SCRIPTABLE QPixmap sprite() const; private: SpriteWrapper(const Sigmod::Sprite* sprite, SigmodWrapper* parent); SpriteWrapper& operator=(const SpriteWrapper& rhs); |
