mirror of
https://github.com/overte-org/overte.git
synced 2025-04-21 08:04:01 +02:00
Merge pull request #1815 from birarda/script-timer
expose QTimer to the script engine
This commit is contained in:
commit
0bcbe485c0
3 changed files with 67 additions and 4 deletions
8
examples/timer.js
Normal file
8
examples/timer.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
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
|
||||
Script.clearTimeout(one_timer);
|
||||
|
||||
// this stops the repeating timer
|
||||
Script.clearInterval(multiple_timer);
|
|
@ -105,8 +105,6 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents) {
|
|||
return true;
|
||||
}
|
||||
|
||||
Q_SCRIPT_DECLARE_QMETAOBJECT(AudioInjectorOptions, QObject*)
|
||||
|
||||
void ScriptEngine::init() {
|
||||
if (_isInitialized) {
|
||||
return; // only initialize once
|
||||
|
@ -298,4 +296,51 @@ void ScriptEngine::stop() {
|
|||
_isFinished = true;
|
||||
}
|
||||
|
||||
void ScriptEngine::timerFired() {
|
||||
QTimer* callingTimer = reinterpret_cast<QTimer*>(sender());
|
||||
|
||||
// call the associated JS function, if it exists
|
||||
QScriptValue timerFunction = _timerFunctionMap.value(callingTimer);
|
||||
if (timerFunction.isValid()) {
|
||||
timerFunction.call();
|
||||
}
|
||||
|
||||
if (!callingTimer->isActive()) {
|
||||
// this timer is done, we can kill it
|
||||
qDebug() << "Deleting a single shot timer";
|
||||
delete callingTimer;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
QObject* ScriptEngine::setInterval(const QScriptValue& function, int intervalMS) {
|
||||
return setupTimerWithInterval(function, intervalMS, false);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -59,13 +59,20 @@ public:
|
|||
bool isAvatar() const { return _isAvatar; }
|
||||
|
||||
void setAvatarData(AvatarData* avatarData, const QString& objectName);
|
||||
|
||||
void timerFired();
|
||||
|
||||
public slots:
|
||||
void init();
|
||||
void run(); /// runs continuously until Agent.stop() is called
|
||||
void stop();
|
||||
void evaluate(); /// initializes the engine, and evaluates the script, but then returns control to caller
|
||||
|
||||
|
||||
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();
|
||||
|
@ -73,15 +80,18 @@ signals:
|
|||
void finished(const QString& fileNameString);
|
||||
|
||||
protected:
|
||||
|
||||
QString _scriptContents;
|
||||
bool _isFinished;
|
||||
bool _isRunning;
|
||||
bool _isInitialized;
|
||||
QScriptEngine _engine;
|
||||
bool _isAvatar;
|
||||
QHash<QTimer*, QScriptValue> _timerFunctionMap;
|
||||
|
||||
private:
|
||||
QObject* setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot);
|
||||
void stopTimer(QTimer* timer);
|
||||
|
||||
static VoxelsScriptingInterface _voxelsScriptingInterface;
|
||||
static ParticlesScriptingInterface _particlesScriptingInterface;
|
||||
AbstractControllerScriptingInterface* _controllerScriptingInterface;
|
||||
|
|
Loading…
Reference in a new issue