Skip to content

Commit

Permalink
Add turboMode property
Browse files Browse the repository at this point in the history
  • Loading branch information
adazem009 committed Nov 26, 2023
1 parent 8c83e05 commit 7ad8e10
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions ScratchCPPGui/ProjectPlayer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ProjectScene {
property int stageWidth: 480
property int stageHeight: 360
property alias fps: loader.fps
property alias turboMode: loader.turboMode

id: root
width: stageWidth
Expand Down
37 changes: 32 additions & 5 deletions ScratchCPPGui/projectloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,26 @@ void ProjectLoader::setFileName(const QString &newFileName)
m_loaded = m_project.load();
m_engine = m_project.engine().get();

m_engine->setFps(m_fps);

auto handler = std::bind(&ProjectLoader::emitTick, this);
m_engine->setRedrawHandler(std::function<void()>(handler));

// Delete old sprites
for (SpriteModel *sprite : m_sprites)
sprite->deleteLater();

m_sprites.clear();

if (!m_engine) {
emit fileNameChanged();
emit loadedChanged();
emit engineChanged();
emit spritesChanged();
return;
}

m_engine->setFps(m_fps);
m_engine->setTurboModeEnabled(m_turboMode);

auto handler = std::bind(&ProjectLoader::emitTick, this);
m_engine->setRedrawHandler(std::function<void()>(handler));

// Load targets
const auto &targets = m_engine->targets();

Expand Down Expand Up @@ -181,3 +190,21 @@ void ProjectLoader::setFps(double newFps)

emit fpsChanged();
}

bool ProjectLoader::turboMode() const
{
return m_turboMode;
}

void ProjectLoader::setTurboMode(bool newTurboMode)
{
if (m_turboMode == newTurboMode)
return;

m_turboMode = newTurboMode;

if (m_engine)
m_engine->setTurboModeEnabled(m_turboMode);

emit turboModeChanged();
}
7 changes: 7 additions & 0 deletions ScratchCPPGui/projectloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ProjectLoader : public QObject
Q_PROPERTY(StageModel *stage READ stage NOTIFY stageChanged)
Q_PROPERTY(QQmlListProperty<SpriteModel> sprites READ sprites NOTIFY spritesChanged)
Q_PROPERTY(double fps READ fps WRITE setFps NOTIFY fpsChanged)
Q_PROPERTY(bool turboMode READ turboMode WRITE setTurboMode NOTIFY turboModeChanged)

public:
explicit ProjectLoader(QObject *parent = nullptr);
Expand All @@ -49,6 +50,9 @@ class ProjectLoader : public QObject
double fps() const;
void setFps(double newFps);

bool turboMode() const;
void setTurboMode(bool newTurboMode);

signals:
void fileNameChanged();
void loadedChanged();
Expand All @@ -57,6 +61,8 @@ class ProjectLoader : public QObject
void spritesChanged();
void fpsChanged();

void turboModeChanged();

protected:
void timerEvent(QTimerEvent *event) override;

Expand All @@ -73,6 +79,7 @@ class ProjectLoader : public QObject
QList<SpriteModel *> m_sprites;
QFuture<void> m_eventLoop;
double m_fps = 30;
bool m_turboMode = false;
};

} // namespace scratchcppgui

0 comments on commit 7ad8e10

Please sign in to comment.