From 7a183cfa367db01413c001306741d06e1826d077 Mon Sep 17 00:00:00 2001 From: Treeki Date: Fri, 3 Aug 2012 05:16:52 +0200 Subject: might as well push all this. a massive amount of changes --- lyt/archivepackage.cpp | 129 +++++++++++++++++++++++++++++------------------ lyt/archivepackage.h | 30 ++++------- lyt/bounding.cpp | 1 + lyt/directorypackage.cpp | 99 ++++++++++++------------------------ lyt/directorypackage.h | 33 +++++------- lyt/layout.h | 2 +- lyt/packagebase.cpp | 24 ++++++++- lyt/packagebase.h | 62 ++++++++++++++++++----- lyt/pane.cpp | 33 ++++++++++-- lyt/pane.h | 20 +++++++- lyt/picture.cpp | 1 + lyt/textbox.cpp | 1 + lyt/window.cpp | 1 + 13 files changed, 259 insertions(+), 177 deletions(-) (limited to 'lyt') diff --git a/lyt/archivepackage.cpp b/lyt/archivepackage.cpp index eaabbf2..acb92d2 100644 --- a/lyt/archivepackage.cpp +++ b/lyt/archivepackage.cpp @@ -19,11 +19,11 @@ #include -LYTArchivePackage::LYTArchivePackage() : LYTPackageBase() { +LYTArchivePackage::LYTArchivePackage(QObject *parent) : LYTPackageBase(parent) { m_archive = new WiiArchiveU8; } -LYTArchivePackage::LYTArchivePackage(QString filename) : LYTPackageBase() { +LYTArchivePackage::LYTArchivePackage(QString filename, QObject *parent) : LYTPackageBase(parent) { m_filename = filename; QFile file(filename); @@ -47,11 +47,14 @@ WiiArchiveU8 *LYTArchivePackage::archive() const { QString LYTArchivePackage::filename() const { return m_filename; } +void LYTArchivePackage::setFilename(QString path) { + m_filename = path; +} -QStringList LYTArchivePackage::listSubDirIfExists(QString dirName) const { - WiiFSObject *obj = this->m_archive->root.resolvePath(dirName); +QStringList LYTArchivePackage::list(ItemType type) const { + WiiFSObject *obj = this->m_archive->root.resolvePath(defaultPathForItemType(type, true)); if (obj && obj->isDirectory()) { QStringList output; @@ -66,9 +69,9 @@ QStringList LYTArchivePackage::listSubDirIfExists(QString dirName) const { return QStringList(); } - -QByteArray LYTArchivePackage::getFileFromSubDirIfExists(QString dirName, QString fileName) const { - WiiFSObject *obj = this->m_archive->root.resolvePath(QString("%1/%2").arg(dirName, fileName)); +QByteArray LYTArchivePackage::get(ItemType type, const QString &name) const { + QString dirName = defaultPathForItemType(type, true); + WiiFSObject *obj = this->m_archive->root.resolvePath(QString("%1/%2").arg(dirName, name)); if (obj && obj->isFile()) { return ((WiiFile*)obj)->data; @@ -77,71 +80,97 @@ QByteArray LYTArchivePackage::getFileFromSubDirIfExists(QString dirName, QString return QByteArray(); } +bool LYTArchivePackage::write(ItemType type, const QString &name, const QByteArray &data) { + if (name.isEmpty()) + return false; -bool LYTArchivePackage::writeFileToSubDir(QString dirName, QString fileName, QByteArray data) { - WiiFSObject *obj = this->m_archive->root.resolvePath(QString("%1/%2").arg(dirName, fileName)); + WiiFSObject *rootDir = this->m_archive->root.findByName("arc", false); + if (!rootDir) { + rootDir = new WiiDirectory; + rootDir->name = "arc"; + m_archive->root.addChild(rootDir); + } + if (!rootDir->isDirectory()) + return false; - if (obj && obj->isFile()) { - ((WiiFile*)obj)->data = data; - return true; - } + QString dirName = defaultPathForItemType(type, false); + WiiFSObject *dir = ((WiiDirectory*)rootDir)->findByName(dirName, false); - return false; -} + if (!dir) { + dir = new WiiDirectory; + dir->name = dirName; + ((WiiDirectory*)rootDir)->addChild(dir); + } + if (!dir->isDirectory()) + return false; + WiiFSObject *obj = ((WiiDirectory*)dir)->findByName(name, false); + if (obj && obj->isFile()) { + emit aboutToModifyFile(type, name); + ((WiiFile*)obj)->data = data; -QStringList LYTArchivePackage::listAnims() const { - return this->listSubDirIfExists("arc/anim"); -} + emit fileWasModified(type, name); -QStringList LYTArchivePackage::listLayouts() const { - return this->listSubDirIfExists("arc/blyt"); -} + return true; -QStringList LYTArchivePackage::listTextures() const { - return this->listSubDirIfExists("arc/timg"); -} + } else if (!obj) { + emit aboutToAddFile(type, name); -QStringList LYTArchivePackage::listFonts() const { - return this->listSubDirIfExists("arc/font"); -} + WiiFile *newFile = new WiiFile; + newFile->name = name; + newFile->data = data; + ((WiiDirectory*)dir)->addChild(newFile); + emit fileWasAdded(type, name); + return true; + } -QByteArray LYTArchivePackage::getAnim(QString name) const { - return this->getFileFromSubDirIfExists("arc/anim", name); + return false; } -QByteArray LYTArchivePackage::getLayout(QString name) const { - return this->getFileFromSubDirIfExists("arc/blyt", name); -} +bool LYTArchivePackage::remove(ItemType type, const QString &name) { + WiiFSObject *obj = this->m_archive->root.resolvePath(defaultPathForItemType(type, true)); -QByteArray LYTArchivePackage::getTexture(QString name) const { - return this->getFileFromSubDirIfExists("arc/timg", name); -} + if (obj && obj->isDirectory()) { + WiiDirectory *dir = (WiiDirectory*)obj; + + WiiFSObject *what = dir->findByName(name, false); + if (what && what->isFile()) { + emit aboutToRemoveFile(type, name); + dir->removeChild(what); + emit fileWasRemoved(type, name); + return true; + } + } -QByteArray LYTArchivePackage::getFont(QString name) const { - return this->getFileFromSubDirIfExists("arc/font", name); + return false; } +bool LYTArchivePackage::rename(ItemType type, const QString &from, const QString &to) { + if (to.isEmpty()) + return false; + WiiFSObject *obj = this->m_archive->root.resolvePath(defaultPathForItemType(type, true)); -bool LYTArchivePackage::writeAnim(QString name, QByteArray data) { - return this->writeFileToSubDir("arc/anim", name, data); -} - -bool LYTArchivePackage::writeLayout(QString name, QByteArray data) { - return this->writeFileToSubDir("arc/blyt", name, data); -} - -bool LYTArchivePackage::writeTexture(QString name, QByteArray data) { - return this->writeFileToSubDir("arc/timg", name, data); -} + if (obj && obj->isDirectory()) { + WiiDirectory *dir = (WiiDirectory*)obj; + + WiiFSObject *what = dir->findByName(from, false); + if (what && what->isFile()) { + WiiFSObject *conflict = dir->findByName(to, false); + if (!conflict) { + emit aboutToRenameFile(type, from, to); + what->name = to; + emit fileWasRenamed(type, from, to); + return true; + } + } + } -bool LYTArchivePackage::writeFont(QString name, QByteArray data) { - return this->writeFileToSubDir("arc/font", name, data); + return false; } diff --git a/lyt/archivepackage.h b/lyt/archivepackage.h index e2d5f25..786d962 100644 --- a/lyt/archivepackage.h +++ b/lyt/archivepackage.h @@ -22,39 +22,29 @@ #include "wii/archiveu8.h" class LYTArchivePackage : public LYTPackageBase { + Q_OBJECT public: - LYTArchivePackage(); - LYTArchivePackage(QString filename); + LYTArchivePackage(QObject *parent = 0); + LYTArchivePackage(QString filename, QObject *parent = 0); ~LYTArchivePackage(); - QStringList listAnims() const; - QStringList listLayouts() const; - QStringList listTextures() const; - QStringList listFonts() const; - - QByteArray getAnim(QString name) const; - QByteArray getLayout(QString name) const; - QByteArray getTexture(QString name) const; - QByteArray getFont(QString name) const; - - bool writeAnim(QString name, QByteArray data); - bool writeLayout(QString name, QByteArray data); - bool writeTexture(QString name, QByteArray data); - bool writeFont(QString name, QByteArray data); + QStringList list(ItemType type) const; + QByteArray get(ItemType type, const QString &name) const; + bool write(ItemType type, const QString &name, const QByteArray &data); + bool rename(ItemType type, const QString &from, const QString &to); + bool remove(ItemType type, const QString &name); + bool needsExplicitSave() const { return true; } bool savePackage(); QString description() const; WiiArchiveU8 *archive() const; QString filename() const; + void setFilename(QString path); protected: - QStringList listSubDirIfExists(QString dirName) const; - QByteArray getFileFromSubDirIfExists(QString dirName, QString fileName) const; - bool writeFileToSubDir(QString dirName, QString fileName, QByteArray data); - WiiArchiveU8 *m_archive; QString m_filename; }; diff --git a/lyt/bounding.cpp b/lyt/bounding.cpp index ce506b3..de61840 100644 --- a/lyt/bounding.cpp +++ b/lyt/bounding.cpp @@ -21,6 +21,7 @@ LYTBounding::LYTBounding(LYTLayout &layout) : LYTPane(layout) { + m_type = BoundingType; } diff --git a/lyt/directorypackage.cpp b/lyt/directorypackage.cpp index dd8c39d..803cb80 100644 --- a/lyt/directorypackage.cpp +++ b/lyt/directorypackage.cpp @@ -19,29 +19,30 @@ #include -LYTDirectoryPackage::LYTDirectoryPackage(QString path) : LYTPackageBase() { +LYTDirectoryPackage::LYTDirectoryPackage(QString path, QObject *parent) : LYTPackageBase(parent) { + qWarning("LYTDirectoryPackage is currently unmaintained, you probably shouldn't use it"); + QDir fix_path(path); this->m_path = fix_path.absolutePath(); } -QStringList LYTDirectoryPackage::listSubDirIfExists(QString dirName) const { +QStringList LYTDirectoryPackage::list(ItemType type) const { QDir search(m_path); - if (search.cd(dirName)) { + if (search.cd(defaultPathForItemType(type))) { return search.entryList(); } return QStringList(); } - -QByteArray LYTDirectoryPackage::getFileFromSubDirIfExists(QString dirName, QString fileName) const { +QByteArray LYTDirectoryPackage::get(ItemType type, const QString &name) const { QDir search(m_path); - if (search.cd(dirName)) { - QFile file(search.absoluteFilePath(fileName)); + if (search.cd(defaultPathForItemType(type))) { + QFile file(search.absoluteFilePath(name)); if (file.open(QFile::ReadOnly)) { return file.readAll(); @@ -51,76 +52,40 @@ QByteArray LYTDirectoryPackage::getFileFromSubDirIfExists(QString dirName, QStri return QByteArray(); } - -bool LYTDirectoryPackage::writeFileToSubDir(QString dirName, QString fileName, QByteArray data) { +bool LYTDirectoryPackage::write(ItemType type, const QString &name, const QByteArray &data) { QDir search(m_path); + QString dirName = defaultPathForItemType(type); - if (search.cd(dirName)) { - QFile file(search.absoluteFilePath(fileName)); - - if (file.open(QFile::WriteOnly)) { - if (file.write(data) != -1) { - return true; - } - } - } - - return false; -} + if (!search.cd(dirName)) { + if (!search.mkdir(dirName)) + return false; + if (!search.cd(dirName)) + return false; + } + QFile file(search.absoluteFilePath(name)); + if (file.open(QFile::WriteOnly)) { + if (file.write(data) != -1) { + return true; + } + } - -QStringList LYTDirectoryPackage::listAnims() const { - return this->listSubDirIfExists("anim"); -} - -QStringList LYTDirectoryPackage::listLayouts() const { - return this->listSubDirIfExists("blyt"); -} - -QStringList LYTDirectoryPackage::listTextures() const { - return this->listSubDirIfExists("timg"); + return false; } -QStringList LYTDirectoryPackage::listFonts() const { - return this->listSubDirIfExists("font"); -} - - - -QByteArray LYTDirectoryPackage::getAnim(QString name) const { - return this->getFileFromSubDirIfExists("anim", name); -} - -QByteArray LYTDirectoryPackage::getLayout(QString name) const { - return this->getFileFromSubDirIfExists("blyt", name); -} - -QByteArray LYTDirectoryPackage::getTexture(QString name) const { - return this->getFileFromSubDirIfExists("timg", name); -} - -QByteArray LYTDirectoryPackage::getFont(QString name) const { - return this->getFileFromSubDirIfExists("font", name); -} - - +bool LYTDirectoryPackage::remove(ItemType type, const QString &name) { + QDir search(m_path); -bool LYTDirectoryPackage::writeAnim(QString name, QByteArray data) { - return this->writeFileToSubDir("anim", name, data); -} + if (search.cd(defaultPathForItemType(type))) { + QFile file(search.absoluteFilePath(name)); -bool LYTDirectoryPackage::writeLayout(QString name, QByteArray data) { - return this->writeFileToSubDir("blyt", name, data); -} - -bool LYTDirectoryPackage::writeTexture(QString name, QByteArray data) { - return this->writeFileToSubDir("timg", name, data); -} + if (file.open(QFile::WriteOnly)) { + return file.remove(); + } + } -bool LYTDirectoryPackage::writeFont(QString name, QByteArray data) { - return this->writeFileToSubDir("font", name, data); + return false; } diff --git a/lyt/directorypackage.h b/lyt/directorypackage.h index df8a50e..0d6ea19 100644 --- a/lyt/directorypackage.h +++ b/lyt/directorypackage.h @@ -15,40 +15,33 @@ along with this program. If not, see . *******************************************************************************/ +// Currently unmaintained. +// What this needs to be fixed up: +// -- Implement rename() +// -- Make it emit signals +// -- Add support to the LayoutStudio UI + #ifndef LYTDIRECTORYPACKAGE_H #define LYTDIRECTORYPACKAGE_H #include "packagebase.h" class LYTDirectoryPackage : public LYTPackageBase { + Q_OBJECT public: - LYTDirectoryPackage(QString path); - - QStringList listAnims() const; - QStringList listLayouts() const; - QStringList listTextures() const; - QStringList listFonts() const; - - QByteArray getAnim(QString name) const; - QByteArray getLayout(QString name) const; - QByteArray getTexture(QString name) const; - QByteArray getFont(QString name) const; + LYTDirectoryPackage(QString path, QObject *parent = 0); - bool writeAnim(QString name, QByteArray data); - bool writeLayout(QString name, QByteArray data); - bool writeTexture(QString name, QByteArray data); - bool writeFont(QString name, QByteArray data); + QStringList list(ItemType type) const; + QByteArray get(ItemType type, const QString &name) const; + bool write(ItemType type, const QString &name, const QByteArray &data); + bool remove(ItemType type, const QString &name); + bool needsExplicitSave() const { return false; } bool savePackage(); QString description() const; QString path() const; protected: - QStringList listSubDirIfExists(QString dirName) const; - QByteArray getFileFromSubDirIfExists(QString dirName, QString fileName) const; - bool writeFileToSubDir(QString dirName, QString fileName, QByteArray data); - - QString m_path; }; diff --git a/lyt/layout.h b/lyt/layout.h index 092be44..c051842 100644 --- a/lyt/layout.h +++ b/lyt/layout.h @@ -57,6 +57,7 @@ public: LYTPane *rootPane; QList groups; + QStringList generateTextureRefs() const; protected: @@ -77,7 +78,6 @@ protected: LYTPane *createPaneObj(LYTBinaryFileSection §ion); - QStringList generateTextureRefs() const; QStringList generateFontRefs() const; void writeMat1(LYTBinaryFileSection §ion) const; diff --git a/lyt/packagebase.cpp b/lyt/packagebase.cpp index e186435..5855688 100644 --- a/lyt/packagebase.cpp +++ b/lyt/packagebase.cpp @@ -17,9 +17,31 @@ #include "packagebase.h" -LYTPackageBase::LYTPackageBase() { +LYTPackageBase::LYTPackageBase(QObject *parent) : QObject(parent) { // do nothing } LYTPackageBase::~LYTPackageBase() { } + + + +QString LYTPackageBase::defaultPathForItemType(ItemType type, bool withArc) { + switch (type) { + case Layout: + return withArc ? "arc/blyt" : "blyt"; + case Animation: + return withArc ? "arc/anim" : "anim"; + case Texture: + return withArc ? "arc/timg" : "timg"; + case Font: + return withArc ? "arc/font" : "font"; + default: + return QString(); + } +} + + +QByteArray LYTPackageBase::createSkeletonItem(ItemType type) { + return QByteArray(); +} diff --git a/lyt/packagebase.h b/lyt/packagebase.h index 6129e0b..ac11b92 100644 --- a/lyt/packagebase.h +++ b/lyt/packagebase.h @@ -20,30 +20,64 @@ #include #include +#include -class LYTPackageBase { +class LYTPackageBase : public QObject { + Q_OBJECT public: - LYTPackageBase(); + LYTPackageBase(QObject *parent = 0); virtual ~LYTPackageBase(); - virtual QStringList listAnims() const = 0; - virtual QStringList listLayouts() const = 0; - virtual QStringList listTextures() const = 0; - virtual QStringList listFonts() const = 0; + enum ItemType { + Layout = 1, + Animation, + Texture, + Font + }; - virtual QByteArray getAnim(QString name) const = 0; - virtual QByteArray getLayout(QString name) const = 0; - virtual QByteArray getTexture(QString name) const = 0; - virtual QByteArray getFont(QString name) const = 0; + static QString defaultPathForItemType(ItemType type, bool withArc=false); + static QByteArray createSkeletonItem(ItemType type); - virtual bool writeAnim(QString name, QByteArray data) = 0; - virtual bool writeLayout(QString name, QByteArray data) = 0; - virtual bool writeTexture(QString name, QByteArray data) = 0; - virtual bool writeFont(QString name, QByteArray data) = 0; + virtual QStringList list(ItemType type) const = 0; + virtual QByteArray get(ItemType type, const QString &name) const = 0; + virtual bool write(ItemType type, const QString &name, const QByteArray &data) = 0; + virtual bool rename(ItemType type, const QString &from, const QString &to) = 0; + virtual bool remove(ItemType type, const QString &name) = 0; + // Shortcuts +#define MakeThing(THING, ENUMVAL) \ + QStringList list##THING##s () const { return list(ENUMVAL); } \ + QByteArray get##THING (const QString &name) const \ + { return get(ENUMVAL, name); } \ + \ + bool write##THING (const QString &name, const QByteArray &data) \ + { return write(ENUMVAL, name, data); } \ + \ + bool remove##THING(const QString &name) { return remove(ENUMVAL, name); } + + // Use it + MakeThing(Layout, Layout) + MakeThing(Anim, Animation) + MakeThing(Texture, Texture) + MakeThing(Font, Font) + +#undef MakeThing + + virtual bool needsExplicitSave() const = 0; virtual bool savePackage() = 0; virtual QString description() const = 0; + +signals: + void aboutToAddFile(LYTPackageBase::ItemType type, QString name); + void aboutToRemoveFile(LYTPackageBase::ItemType type, QString name); + void aboutToRenameFile(LYTPackageBase::ItemType type, QString from, QString to); + void aboutToModifyFile(LYTPackageBase::ItemType type, QString name); + + void fileWasAdded(LYTPackageBase::ItemType type, QString name); + void fileWasRemoved(LYTPackageBase::ItemType type, QString name); + void fileWasRenamed(LYTPackageBase::ItemType type, QString from, QString to); + void fileWasModified(LYTPackageBase::ItemType type, QString name); }; #endif // LYTPACKAGEBASE_H diff --git a/lyt/pane.cpp b/lyt/pane.cpp index 7ae768a..3a4abd0 100644 --- a/lyt/pane.cpp +++ b/lyt/pane.cpp @@ -20,6 +20,7 @@ LYTPane::LYTPane(LYTLayout &layout) : m_layout(layout) { this->parent = 0; + m_type = PaneType; } LYTPane::~LYTPane() { @@ -59,7 +60,7 @@ void LYTPane::dumpToDebug(bool showHeading) const { qDebug() << "- Rotation:" << xRot << "," << yRot << "," << zRot; qDebug() << "- Scale:" << xScale << "," << yScale; qDebug() << "- Size:" << width << "x" << height; - qDebug() << "- Flags:" << flags << "- Origin:" << origin; + qDebug() << "- Flags:" << flags << "- Origin:" << horzOrigin << "," << vertOrigin; qDebug() << "- Alpha:" << alpha << "- Userdata:" << userdata; } @@ -67,7 +68,7 @@ void LYTPane::dumpToDebug(bool showHeading) const { void LYTPane::writeToDataStream(QDataStream &out) const { out << (quint8)flags; - out << (quint8)origin; + out << (quint8)((int)horzOrigin + ((int)vertOrigin * 3)); out << (quint8)alpha; WritePadding(1, out); @@ -88,7 +89,10 @@ void LYTPane::writeToDataStream(QDataStream &out) const { void LYTPane::readFromDataStream(QDataStream &in) { in >> (quint8&)flags; - in >> (quint8&)origin; + quint8 rawOrigin; + in >> rawOrigin; + horzOrigin = (OriginType)(rawOrigin % 3); + vertOrigin = (OriginType)(rawOrigin / 3); in >> (quint8&)alpha; in.skipRawData(1); // padding @@ -115,3 +119,26 @@ void LYTPane::addFontRefsToList(QStringList &list) const { p->addFontRefsToList(list); } } + + +float LYTPane::drawnVertexX() const { + switch (horzOrigin) { + case Center: + return -width / 2.0f; + case Right: + return -width; + default: + return 0.0f; + } +} + +float LYTPane::drawnVertexY() const { + switch (vertOrigin) { + case Center: + return height / 2.0f; + case Bottom: + return height; + default: + return 0.0f; + } +} diff --git a/lyt/pane.h b/lyt/pane.h index 8b80029..0d9936b 100644 --- a/lyt/pane.h +++ b/lyt/pane.h @@ -35,6 +35,10 @@ public: virtual Magic magic() const; + enum PaneTypes { + PaneType = 0, PictureType, TextBoxType, WindowType, BoundingType + }; + virtual void writeToDataStream(QDataStream &out) const; virtual void readFromDataStream(QDataStream &in); @@ -50,7 +54,18 @@ public: QList children; quint8 flags; - quint8 origin; + + enum OriginType { + Left = 0, Top = 0, + Center = 1, + Right = 2, Bottom = 2 + }; + OriginType horzOrigin; + OriginType vertOrigin; + + float drawnVertexX() const; + float drawnVertexY() const; + quint8 alpha; QString name; @@ -70,8 +85,11 @@ public: float width; float height; + PaneTypes type() const { return m_type; } + protected: LYTLayout &m_layout; + PaneTypes m_type; }; diff --git a/lyt/picture.cpp b/lyt/picture.cpp index 8cd5f1f..facd5ca 100644 --- a/lyt/picture.cpp +++ b/lyt/picture.cpp @@ -21,6 +21,7 @@ LYTPicture::LYTPicture(LYTLayout &layout) : LYTPane(layout) { + m_type = PictureType; } diff --git a/lyt/textbox.cpp b/lyt/textbox.cpp index e1b640e..e943963 100644 --- a/lyt/textbox.cpp +++ b/lyt/textbox.cpp @@ -21,6 +21,7 @@ LYTTextBox::LYTTextBox(LYTLayout &layout) : LYTPane(layout) { + m_type = TextBoxType; } diff --git a/lyt/window.cpp b/lyt/window.cpp index 52d2db9..096f747 100644 --- a/lyt/window.cpp +++ b/lyt/window.cpp @@ -50,6 +50,7 @@ void LYTWindowFrame::dumpToDebug() const { LYTWindow::LYTWindow(LYTLayout &layout) : LYTPane(layout) { + m_type = WindowType; } LYTWindow::~LYTWindow() { -- cgit v1.2.3