From 8c0156bc7a684edca047941065fc620bee515c30 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Fri, 1 Mar 2019 22:34:46 +0930 Subject: [PATCH] Add example model: Bob Add menu item "Open Example", open a default example model, called "bob.ds3". Thanks to reddit user(bvanevery) for proposing this feature. --- CONTRIBUTORS | 3 +- dust3d.pro | 4 +- resources.qrc | 1 + resources/bob.ds3 | 140 +++++++++++++++++++++++++++++++++++++++++ src/documentwindow.cpp | 63 +++++++++++++++++++ src/documentwindow.h | 2 + 6 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 resources/bob.ds3 diff --git a/CONTRIBUTORS b/CONTRIBUTORS index a7542a20..f2505be1 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -23,4 +23,5 @@ vanous justStand Ruben Niculcea boynet -fornclake \ No newline at end of file +fornclake +bvanevery \ No newline at end of file diff --git a/dust3d.pro b/dust3d.pro index 1f69bd04..5529d381 100644 --- a/dust3d.pro +++ b/dust3d.pro @@ -12,10 +12,10 @@ macx { } isEmpty(HUMAN_VERSION) { - HUMAN_VERSION = "1.0.0-beta.15" + HUMAN_VERSION = "1.0.0-beta.16" } isEmpty(VERSION) { - VERSION = 1.0.0.15 + VERSION = 1.0.0.16 } REPOSITORY_URL = "https://github.com/huxingyi/dust3d" diff --git a/resources.qrc b/resources.qrc index ea6258e0..8774c318 100644 --- a/resources.qrc +++ b/resources.qrc @@ -7,6 +7,7 @@ resources/tree-branch-open.png resources/tree-vline.png resources/material-demo-model.ds3 + resources/bob.ds3 shaders/default.vert shaders/default.frag shaders/default.core.vert diff --git a/resources/bob.ds3 b/resources/bob.ds3 new file mode 100644 index 00000000..dbd04709 --- /dev/null +++ b/resources/bob.ds3 @@ -0,0 +1,140 @@ +DUST3D 1.0 xml 0000000133 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/documentwindow.cpp b/src/documentwindow.cpp index 7a8ba3a0..ea0c3e0e 100644 --- a/src/documentwindow.cpp +++ b/src/documentwindow.cpp @@ -344,6 +344,10 @@ DocumentWindow::DocumentWindow() : m_openAction = new QAction(tr("Open..."), this); connect(m_openAction, &QAction::triggered, this, &DocumentWindow::open, Qt::QueuedConnection); m_fileMenu->addAction(m_openAction); + + m_openExampleAction = new QAction(tr("Open Example"), this); + connect(m_openExampleAction, &QAction::triggered, this, &DocumentWindow::openExample, Qt::QueuedConnection); + m_fileMenu->addAction(m_openExampleAction); m_saveAction = new QAction(tr("Save"), this); connect(m_saveAction, &QAction::triggered, this, &DocumentWindow::save, Qt::QueuedConnection); @@ -1173,6 +1177,65 @@ void DocumentWindow::saveTo(const QString &saveAsFilename) QApplication::restoreOverrideCursor(); } +void DocumentWindow::openExample() +{ + if (!m_documentSaved) { + QMessageBox::StandardButton answer = QMessageBox::question(this, + APP_NAME, + tr("Do you really want to open example and lose the unsaved changes?"), + QMessageBox::Yes | QMessageBox::No); + if (answer != QMessageBox::Yes) + return; + } + + QApplication::setOverrideCursor(Qt::WaitCursor); + Ds3FileReader ds3Reader(":/resources/bob.ds3"); + + m_document->clearHistories(); + m_document->reset(); + m_document->saveSnapshot(); + + for (int i = 0; i < ds3Reader.items().size(); ++i) { + Ds3ReaderItem item = ds3Reader.items().at(i); + if (item.type == "asset") { + if (item.name.startsWith("images/")) { + QString filename = item.name.split("/")[1]; + QString imageIdString = filename.split(".")[0]; + QUuid imageId = QUuid(imageIdString); + if (!imageId.isNull()) { + QByteArray data; + ds3Reader.loadItem(item.name, &data); + QImage image = QImage::fromData(data, "PNG"); + (void)ImageForever::add(&image, imageId); + } + } + } + } + + for (int i = 0; i < ds3Reader.items().size(); ++i) { + Ds3ReaderItem item = ds3Reader.items().at(i); + if (item.type == "model") { + QByteArray data; + ds3Reader.loadItem(item.name, &data); + QXmlStreamReader stream(data); + Snapshot snapshot; + loadSkeletonFromXmlStream(&snapshot, stream); + m_document->fromSnapshot(snapshot); + m_document->saveSnapshot(); + } else if (item.type == "asset") { + if (item.name == "canvas.png") { + QByteArray data; + ds3Reader.loadItem(item.name, &data); + QImage image = QImage::fromData(data, "PNG"); + m_document->updateTurnaround(image); + } + } + } + QApplication::restoreOverrideCursor(); + + setCurrentFilename(""); +} + void DocumentWindow::open() { if (!m_documentSaved) { diff --git a/src/documentwindow.h b/src/documentwindow.h index 13e73771..7c4bfb5e 100644 --- a/src/documentwindow.h +++ b/src/documentwindow.h @@ -38,6 +38,7 @@ public slots: void save(); void saveTo(const QString &saveAsFilename); void open(); + void openExample(); void exportObjResult(); void exportGlbResult(); void exportFbxResult(); @@ -84,6 +85,7 @@ private: QAction *m_newWindowAction; QAction *m_newDocumentAction; QAction *m_openAction; + QAction *m_openExampleAction; QAction *m_saveAction; QAction *m_saveAsAction; QAction *m_saveAllAction;