Skip to content

Commit

Permalink
Merge pull request #42 from scratchcpp/loading_progress
Browse files Browse the repository at this point in the history
Show loading progress
  • Loading branch information
adazem009 authored Dec 9, 2023
2 parents f78da3e + 3bf773f commit a81e0c6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
54 changes: 54 additions & 0 deletions ScratchCPPGui/ProjectPlayer.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import ScratchCPPGui

ProjectScene {
Expand All @@ -9,18 +11,30 @@ ProjectScene {
property alias turboMode: loader.turboMode
property alias cloneLimit: loader.cloneLimit
property alias spriteFencing: loader.spriteFencing
property bool showLoadingProgress: true
readonly property bool loading: priv.loading
readonly property int downloadedAssets: loader.downloadedAssets
readonly property int assetCount: loader.assetCount
signal loaded()
signal failedToLoad()

id: root
clip: true
onFileNameChanged: priv.loading = true;

QtObject {
id: priv
property bool loading: false
}

ProjectLoader {
id: loader
fileName: root.fileName
stageWidth: parent.width
stageHeight: parent.height
onLoadingFinished: {
priv.loading = false;

if(loadStatus)
loaded();
else
Expand Down Expand Up @@ -55,4 +69,44 @@ ProjectScene {
Component.onCompleted: modelData.renderedTarget = this
}
}

Loader {
anchors.fill: parent
active: showLoadingProgress && loading

sourceComponent: ColumnLayout {
anchors.fill: parent

Item { Layout.fillHeight: true }

BusyIndicator {
Layout.fillWidth: true
Layout.maximumWidth: 100
Layout.alignment: Qt.AlignHCenter
running: true
}

Label {
Layout.alignment: Qt.AlignHCenter
font.bold: true
font.pointSize: 12
text: {
if(loading)
return assetCount == downloadedAssets ? qsTr("Loading project...") : qsTr("Downloading assets... (%1 of %2)").arg(downloadedAssets).arg(assetCount);
else
return "";
}
}

ProgressBar {
Layout.fillWidth: true
from: 0
to: assetCount
value: downloadedAssets
indeterminate: assetCount == downloadedAssets
}

Item { Layout.fillHeight: true }
}
}
}
29 changes: 29 additions & 0 deletions ScratchCPPGui/projectloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ void runEventLoop(IEngine *engine)
ProjectLoader::ProjectLoader(QObject *parent) :
QObject(parent)
{
m_project.setDownloadProgressCallback([this](unsigned int finished, unsigned int all) {
if (finished != m_downloadedAssets) {
m_downloadedAssets = finished;
emit downloadedAssetsChanged();
}

if (all != m_assetCount) {
m_assetCount = all;
emit assetCountChanged();
}
});

initTimer();

// Update refresh rate when primary screen changes
Expand Down Expand Up @@ -66,6 +78,13 @@ void ProjectLoader::setFileName(const QString &newFileName)
m_project.setScratchVersion(ScratchVersion::Scratch3);
m_project.setFileName(m_fileName.toStdString());
m_loadStatus = false;

// TODO: Do not set these to 0 after libscratchcpp starts doing it itself
m_downloadedAssets = 0;
m_assetCount = 0;
emit downloadedAssetsChanged();
emit assetCountChanged();

emit loadStatusChanged();
emit fileNameChanged();

Expand Down Expand Up @@ -397,3 +416,13 @@ void ProjectLoader::setEventLoopEnabled(bool newEventLoopEnabled)
m_engineMutex.unlock();
emit eventLoopEnabledChanged();
}

unsigned int ProjectLoader::downloadedAssets() const
{
return m_downloadedAssets;
}

unsigned int ProjectLoader::assetCount() const
{
return m_assetCount;
}
10 changes: 10 additions & 0 deletions ScratchCPPGui/projectloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ProjectLoader : public QObject
Q_PROPERTY(int cloneLimit READ cloneLimit WRITE setCloneLimit NOTIFY cloneLimitChanged)
Q_PROPERTY(bool spriteFencing READ spriteFencing WRITE setSpriteFencing NOTIFY spriteFencingChanged)
Q_PROPERTY(bool eventLoopEnabled READ eventLoopEnabled WRITE setEventLoopEnabled NOTIFY eventLoopEnabledChanged)
Q_PROPERTY(unsigned int downloadedAssets READ downloadedAssets NOTIFY downloadedAssetsChanged)
Q_PROPERTY(unsigned int assetCount READ assetCount NOTIFY assetCountChanged)

public:
explicit ProjectLoader(QObject *parent = nullptr);
Expand Down Expand Up @@ -72,6 +74,10 @@ class ProjectLoader : public QObject
bool eventLoopEnabled() const;
void setEventLoopEnabled(bool newEventLoopEnabled);

unsigned int downloadedAssets() const;

unsigned int assetCount() const;

signals:
void fileNameChanged();
void loadStatusChanged();
Expand All @@ -86,6 +92,8 @@ class ProjectLoader : public QObject
void cloneLimitChanged();
void spriteFencingChanged();
void eventLoopEnabledChanged();
void downloadedAssetsChanged();
void assetCountChanged();

protected:
void timerEvent(QTimerEvent *event) override;
Expand Down Expand Up @@ -113,6 +121,8 @@ class ProjectLoader : public QObject
int m_cloneLimit = 300;
bool m_spriteFencing = true;
bool m_eventLoopEnabled = true;
std::atomic<unsigned int> m_downloadedAssets = 0;
std::atomic<unsigned int> m_assetCount = 0;
};

} // namespace scratchcppgui

0 comments on commit a81e0c6

Please sign in to comment.