mirror of
https://github.com/overte-org/overte.git
synced 2025-04-23 12:35:30 +02:00
stop timers when the script does, add cleanup methods
This commit is contained in:
parent
9dd8c11e1e
commit
e2842ab7a4
3 changed files with 29 additions and 13 deletions
|
@ -1,4 +1,4 @@
|
|||
var one_timer = Script.setTimeout(function() { print("One time timer fired!"); }, 1000);
|
||||
var one_timer = Script.setTimeout(function() { print("One time timer fired!"); }, 10000);
|
||||
var multiple_timer = Script.setInterval(function() { print("Repeating timer fired!"); }, 1000);
|
||||
|
||||
// this would stop a scheduled single shot timer
|
||||
|
|
|
@ -312,22 +312,35 @@ void ScriptEngine::timerFired() {
|
|||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot) {
|
||||
QObject* ScriptEngine::setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot) {
|
||||
// create the timer, add it to the map, and start it
|
||||
QTimer* newTimer = new QTimer(this);
|
||||
connect(newTimer, &QTimer::timeout, this, &ScriptEngine::timerFired);
|
||||
_timerFunctionMap.insert(newTimer, function);
|
||||
|
||||
newTimer->setSingleShot(isSingleShot);
|
||||
|
||||
connect(newTimer, &QTimer::timeout, this, &ScriptEngine::timerFired);
|
||||
|
||||
// make sure the timer stops when the script does
|
||||
connect(this, &ScriptEngine::scriptEnding, newTimer, &QTimer::stop);
|
||||
|
||||
_timerFunctionMap.insert(newTimer, function);
|
||||
|
||||
newTimer->start(intervalMS);
|
||||
return newTimer;
|
||||
}
|
||||
|
||||
void ScriptEngine::setInterval(const QScriptValue& function, int intervalMS) {
|
||||
setupTimerWithInterval(function, intervalMS, false);
|
||||
QObject* ScriptEngine::setInterval(const QScriptValue& function, int intervalMS) {
|
||||
return setupTimerWithInterval(function, intervalMS, false);
|
||||
}
|
||||
|
||||
void ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) {
|
||||
setupTimerWithInterval(function, timeoutMS, true);
|
||||
QObject* ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) {
|
||||
return setupTimerWithInterval(function, timeoutMS, true);
|
||||
}
|
||||
|
||||
void ScriptEngine::stopTimer(QTimer *timer) {
|
||||
if (_timerFunctionMap.contains(timer)) {
|
||||
timer->stop();
|
||||
_timerFunctionMap.remove(timer);
|
||||
delete timer;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -68,9 +68,11 @@ public slots:
|
|||
void stop();
|
||||
void evaluate(); /// initializes the engine, and evaluates the script, but then returns control to caller
|
||||
|
||||
void setInterval(const QScriptValue& function, int intervalMS);
|
||||
void setTimeout(const QScriptValue& function, int timeoutMS);
|
||||
|
||||
QObject* setInterval(const QScriptValue& function, int intervalMS);
|
||||
QObject* setTimeout(const QScriptValue& function, int timeoutMS);
|
||||
void clearInterval(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); }
|
||||
void clearTimeout(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); }
|
||||
|
||||
signals:
|
||||
void willSendAudioDataCallback();
|
||||
void willSendVisualDataCallback();
|
||||
|
@ -87,7 +89,8 @@ protected:
|
|||
QHash<QTimer*, QScriptValue> _timerFunctionMap;
|
||||
|
||||
private:
|
||||
void setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot);
|
||||
QObject* setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot);
|
||||
void stopTimer(QTimer* timer);
|
||||
|
||||
static VoxelsScriptingInterface _voxelsScriptingInterface;
|
||||
static ParticlesScriptingInterface _particlesScriptingInterface;
|
||||
|
|
Loading…
Reference in a new issue