mirror of
https://github.com/overte-org/overte.git
synced 2025-04-23 18:13:29 +02:00
change script timer setup to match HTML dom standard
This commit is contained in:
parent
d9cfaec4f0
commit
9dd8c11e1e
3 changed files with 50 additions and 12 deletions
|
@ -1,5 +1,8 @@
|
|||
var timer = new Timer();
|
||||
timer.interval = 1000;
|
||||
timer.singleShot = true; // set this is you only want the timer to fire once
|
||||
timer.timeout.connect(function() { print("TIMER FIRED!"); });
|
||||
timer.start();
|
||||
var one_timer = Script.setTimeout(function() { print("One time timer fired!"); }, 1000);
|
||||
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,9 +105,6 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents) {
|
|||
return true;
|
||||
}
|
||||
|
||||
Q_SCRIPT_DECLARE_QMETAOBJECT(AudioInjectorOptions, QObject*)
|
||||
Q_SCRIPT_DECLARE_QMETAOBJECT(QTimer, QObject*)
|
||||
|
||||
void ScriptEngine::init() {
|
||||
if (_isInitialized) {
|
||||
return; // only initialize once
|
||||
|
@ -134,9 +131,6 @@ void ScriptEngine::init() {
|
|||
|
||||
QScriptValue injectionOptionValue = _engine.scriptValueFromQMetaObject<AudioInjectorOptions>();
|
||||
_engine.globalObject().setProperty("AudioInjectionOptions", injectionOptionValue);
|
||||
|
||||
QScriptValue timerValue = _engine.scriptValueFromQMetaObject<QTimer>();
|
||||
_engine.globalObject().setProperty("Timer", timerValue);
|
||||
|
||||
registerGlobalObject("Script", this);
|
||||
registerGlobalObject("Audio", &_audioScriptingInterface);
|
||||
|
@ -302,4 +296,38 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
void 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);
|
||||
|
||||
newTimer->start(intervalMS);
|
||||
}
|
||||
|
||||
void ScriptEngine::setInterval(const QScriptValue& function, int intervalMS) {
|
||||
setupTimerWithInterval(function, intervalMS, false);
|
||||
}
|
||||
|
||||
void ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) {
|
||||
setupTimerWithInterval(function, timeoutMS, true);
|
||||
}
|
||||
|
||||
|
|
|
@ -59,12 +59,17 @@ 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
|
||||
|
||||
void setInterval(const QScriptValue& function, int intervalMS);
|
||||
void setTimeout(const QScriptValue& function, int timeoutMS);
|
||||
|
||||
signals:
|
||||
void willSendAudioDataCallback();
|
||||
|
@ -73,15 +78,17 @@ signals:
|
|||
void finished(const QString& fileNameString);
|
||||
|
||||
protected:
|
||||
|
||||
QString _scriptContents;
|
||||
bool _isFinished;
|
||||
bool _isRunning;
|
||||
bool _isInitialized;
|
||||
QScriptEngine _engine;
|
||||
bool _isAvatar;
|
||||
QHash<QTimer*, QScriptValue> _timerFunctionMap;
|
||||
|
||||
private:
|
||||
void setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot);
|
||||
|
||||
static VoxelsScriptingInterface _voxelsScriptingInterface;
|
||||
static ParticlesScriptingInterface _particlesScriptingInterface;
|
||||
AbstractControllerScriptingInterface* _controllerScriptingInterface;
|
||||
|
|
Loading…
Reference in a new issue