History

Added the new Running Scripts widget

Mohammed Nafees committed 12 years ago

Commit d85d4fe1 parent 89cb1ad14 files · +920 −339Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 14 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+50−81
@@ -29,7 +29,6 @@ · 28 unmodified lines
2929 #include "LocalVoxels.h"
3030 #include "ScriptEngine.h"
3131
32−int ScriptEngine::_scriptNumber = 1;
3332 VoxelsScriptingInterface ScriptEngine::_voxelsScriptingInterface;
3433 ParticlesScriptingInterface ScriptEngine::_particlesScriptingInterface;
3534
@@ -41,7 +40,7 @@ static QScriptValue soundConstructor(QScriptContext* context, QScriptEngine* eng · 5 unmodified lines
4140 }
4241
4342
44−ScriptEngine::ScriptEngine(const QString& scriptContents, bool wantMenuItems, const QString& fileNameString,
43+ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString,
4544 AbstractControllerScriptingInterface* controllerScriptingInterface) :
4645
4746 _scriptContents(scriptContents),
@@ -58,26 +57,15 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, bool wantMenuItems, co · 10 unmodified lines
5857 _numAvatarSoundSentBytes(0),
5958 _controllerScriptingInterface(controllerScriptingInterface),
6059 _avatarData(NULL),
61− _wantMenuItems(wantMenuItems),
62− _scriptMenuName(),
60+ _scriptName(),
6361 _fileNameString(fileNameString),
6462 _quatLibrary(),
6563 _vec3Library()
6664 {
67− // some clients will use these menu features
68− if (!fileNameString.isEmpty()) {
69− _scriptMenuName = "Stop ";
70− _scriptMenuName.append(qPrintable(fileNameString));
71− _scriptMenuName.append(QString(" [%1]").arg(_scriptNumber));
72− } else {
73− _scriptMenuName = "Stop Script ";
74− _scriptMenuName.append(_scriptNumber);
75− }
76− _scriptNumber++;
7765 }
7866
79−ScriptEngine::ScriptEngine(const QUrl& scriptURL, bool wantMenuItems,
80− AbstractControllerScriptingInterface* controllerScriptingInterface) :
67+ScriptEngine::ScriptEngine(const QUrl& scriptURL,
68+ AbstractControllerScriptingInterface* controllerScriptingInterface) :
8169 _scriptContents(),
8270 _isFinished(false),
8371 _isRunning(false),
@@ -92,32 +80,21 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, bool wantMenuItems, · 8 unmodified lines
9280 _numAvatarSoundSentBytes(0),
9381 _controllerScriptingInterface(controllerScriptingInterface),
9482 _avatarData(NULL),
95− _wantMenuItems(wantMenuItems),
96− _scriptMenuName(),
83+ _scriptName(),
9784 _fileNameString(),
9885 _quatLibrary(),
9986 _vec3Library()
10087 {
10188 QString scriptURLString = scriptURL.toString();
10289 _fileNameString = scriptURLString;
103− // some clients will use these menu features
104− if (!scriptURLString.isEmpty()) {
105− _scriptMenuName = "Stop ";
106− _scriptMenuName.append(qPrintable(scriptURLString));
107− _scriptMenuName.append(QString(" [%1]").arg(_scriptNumber));
108− } else {
109− _scriptMenuName = "Stop Script ";
110− _scriptMenuName.append(_scriptNumber);
111− }
112− _scriptNumber++;
113−
90+
11491 QUrl url(scriptURL);
115−
92+
11693 // if the scheme is empty, maybe they typed in a file, let's try
11794 if (url.scheme().isEmpty()) {
11895 url = QUrl::fromLocalFile(scriptURLString);
11996 }
120−
97+
12198 // ok, let's see if it's valid... and if so, load it
12299 if (url.isValid()) {
123100 if (url.scheme() == "file") {
@@ -144,16 +121,16 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, bool wantMenuItems, · 20 unmodified lines
144121
145122 void ScriptEngine::setIsAvatar(bool isAvatar) {
146123 _isAvatar = isAvatar;
147−
124+
148125 if (_isAvatar && !_avatarIdentityTimer) {
149126 // set up the avatar timers
150127 _avatarIdentityTimer = new QTimer(this);
151128 _avatarBillboardTimer = new QTimer(this);
152−
129+
153130 // connect our slot
154131 connect(_avatarIdentityTimer, &QTimer::timeout, this, &ScriptEngine::sendAvatarIdentityPacket);
155132 connect(_avatarBillboardTimer, &QTimer::timeout, this, &ScriptEngine::sendAvatarBillboardPacket);
156−
133+
157134 // start the timers
158135 _avatarIdentityTimer->start(AVATAR_IDENTITY_PACKET_SEND_INTERVAL_MSECS);
159136 _avatarBillboardTimer->start(AVATAR_BILLBOARD_PACKET_SEND_INTERVAL_MSECS);
@@ -162,20 +139,14 @@ void ScriptEngine::setIsAvatar(bool isAvatar) { · 2 unmodified lines
162139
163140 void ScriptEngine::setAvatarData(AvatarData* avatarData, const QString& objectName) {
164141 _avatarData = avatarData;
165−
142+
166143 // remove the old Avatar property, if it exists
167144 _engine.globalObject().setProperty(objectName, QScriptValue());
168−
145+
169146 // give the script engine the new Avatar script property
170147 registerGlobalObject(objectName, _avatarData);
171148 }
172149
173−void ScriptEngine::cleanupMenuItems() {
174− if (_wantMenuItems) {
175− emit cleanupMenuItem(_scriptMenuName);
176− }
177−}
178−
179150 bool ScriptEngine::setScriptContents(const QString& scriptContents, const QString& fileNameString) {
180151 if (_isRunning) {
181152 return false;
@@ -203,7 +174,7 @@ void ScriptEngine::init() { · 21 unmodified lines
203174 registerVoxelMetaTypes(&_engine);
204175 registerEventTypes(&_engine);
205176 registerMenuItemProperties(&_engine);
206−
177+
207178 qScriptRegisterMetaType(&_engine, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue);
208179 qScriptRegisterMetaType(&_engine, ParticleIDtoScriptValue, ParticleIDfromScriptValue);
209180 qScriptRegisterSequenceMetaType<QVector<ParticleID> >(&_engine);
@@ -216,7 +187,7 @@ void ScriptEngine::init() { · 6 unmodified lines
216187
217188 QScriptValue injectionOptionValue = _engine.scriptValueFromQMetaObject<AudioInjectorOptions>();
218189 _engine.globalObject().setProperty("AudioInjectionOptions", injectionOptionValue);
219−
190+
220191 QScriptValue localVoxelsValue = _engine.scriptValueFromQMetaObject<LocalVoxels>();
221192 _engine.globalObject().setProperty("LocalVoxels", localVoxelsValue);
222193
@@ -285,9 +256,9 @@ void ScriptEngine::run() { · 62 unmodified lines
285256 gettimeofday(&startTime, NULL);
286257
287258 int thisFrame = 0;
288−
259+
289260 NodeList* nodeList = NodeList::getInstance();
290−
261+
291262 qint64 lastUpdate = usecTimestampNow();
292263
293264 while (!_isFinished) {
@@ -325,36 +296,36 @@ void ScriptEngine::run() { · 31 unmodified lines
325296 _particlesScriptingInterface.getParticlePacketSender()->process();
326297 }
327298 }
328−
299+
329300 if (_isAvatar && _avatarData) {
330−
301+
331302 const int SCRIPT_AUDIO_BUFFER_SAMPLES = floor(((SCRIPT_DATA_CALLBACK_USECS * SAMPLE_RATE) / (1000 * 1000)) + 0.5);
332303 const int SCRIPT_AUDIO_BUFFER_BYTES = SCRIPT_AUDIO_BUFFER_SAMPLES * sizeof(int16_t);
333−
304+
334305 QByteArray avatarPacket = byteArrayWithPopulatedHeader(PacketTypeAvatarData);
335306 avatarPacket.append(_avatarData->toByteArray());
336−
307+
337308 nodeList->broadcastToNodes(avatarPacket, NodeSet() << NodeType::AvatarMixer);
338−
309+
339310 if (_isListeningToAudioStream || _avatarSound) {
340311 // if we have an avatar audio stream then send it out to our audio-mixer
341312 bool silentFrame = true;
342−
313+
343314 int16_t numAvailableSamples = SCRIPT_AUDIO_BUFFER_SAMPLES;
344315 const int16_t* nextSoundOutput = NULL;
345−
316+
346317 if (_avatarSound) {
347−
318+
348319 const QByteArray& soundByteArray = _avatarSound->getByteArray();
349320 nextSoundOutput = reinterpret_cast<const int16_t*>(soundByteArray.data()
350321 + _numAvatarSoundSentBytes);
351−
322+
352323 int numAvailableBytes = (soundByteArray.size() - _numAvatarSoundSentBytes) > SCRIPT_AUDIO_BUFFER_BYTES
353324 ? SCRIPT_AUDIO_BUFFER_BYTES
354325 : soundByteArray.size() - _numAvatarSoundSentBytes;
355326 numAvailableSamples = numAvailableBytes / sizeof(int16_t);
356−
357−
327+
328+
358329 // check if the all of the _numAvatarAudioBufferSamples to be sent are silence
359330 for (int i = 0; i < numAvailableSamples; ++i) {
360331 if (nextSoundOutput[i] != 0) {
@@ -362,7 +333,7 @@ void ScriptEngine::run() { · 1 unmodified line
362333 break;
363334 }
364335 }
365−
336+
366337 _numAvatarSoundSentBytes += numAvailableBytes;
367338 if (_numAvatarSoundSentBytes == soundByteArray.size()) {
368339 // we're done with this sound object - so set our pointer back to NULL
@@ -371,24 +342,24 @@ void ScriptEngine::run() { · 2 unmodified lines
371342 _numAvatarSoundSentBytes = 0;
372343 }
373344 }
374−
345+
375346 QByteArray audioPacket = byteArrayWithPopulatedHeader(silentFrame
376347 ? PacketTypeSilentAudioFrame
377348 : PacketTypeMicrophoneAudioNoEcho);
378−
349+
379350 QDataStream packetStream(&audioPacket, QIODevice::Append);
380−
351+
381352 // use the orientation and position of this avatar for the source of this audio
382353 packetStream.writeRawData(reinterpret_cast<const char*>(&_avatarData->getPosition()), sizeof(glm::vec3));
383354 glm::quat headOrientation = _avatarData->getHeadOrientation();
384355 packetStream.writeRawData(reinterpret_cast<const char*>(&headOrientation), sizeof(glm::quat));
385−
356+
386357 if (silentFrame) {
387358 if (!_isListeningToAudioStream) {
388359 // if we have a silent frame and we're not listening then just send nothing and break out of here
389360 break;
390361 }
391−
362+
392363 // write the number of silent samples so the audio-mixer can uphold timing
393364 packetStream.writeRawData(reinterpret_cast<const char*>(&SCRIPT_AUDIO_BUFFER_SAMPLES), sizeof(int16_t));
394365 } else if (nextSoundOutput) {
@@ -396,7 +367,7 @@ void ScriptEngine::run() { · 1 unmodified line
396367 packetStream.writeRawData(reinterpret_cast<const char*>(nextSoundOutput),
397368 numAvailableSamples * sizeof(int16_t));
398369 }
399−
370+
400371 nodeList->broadcastToNodes(audioPacket, NodeSet() << NodeType::AudioMixer);
401372 }
402373 }
@@ -412,10 +383,10 @@ void ScriptEngine::run() { · 9 unmodified lines
412383 }
413384 }
414385 emit scriptEnding();
415−
386+
416387 // kill the avatar identity timer
417388 delete _avatarIdentityTimer;
418−
389+
419390 if (_voxelsScriptingInterface.getVoxelPacketSender()->serversExist()) {
420391 // release the queue of edit voxel messages.
421392 _voxelsScriptingInterface.getVoxelPacketSender()->releaseQueuedMessages();
@@ -435,8 +406,6 @@ void ScriptEngine::run() { · 13 unmodified lines
435406 _particlesScriptingInterface.getParticlePacketSender()->process();
436407 }
437408 }
438−
439− cleanupMenuItems();
440409
441410 // If we were on a thread, then wait till it's done
442411 if (thread()) {
@@ -444,7 +413,7 @@ void ScriptEngine::run() { · 1 unmodified line
444413 }
445414
446415 emit finished(_fileNameString);
447−
416+
448417 _isRunning = false;
449418 }
450419
@@ -454,13 +423,13 @@ void ScriptEngine::stop() { · 3 unmodified lines
454423
455424 void ScriptEngine::timerFired() {
456425 QTimer* callingTimer = reinterpret_cast<QTimer*>(sender());
457−
426+
458427 // call the associated JS function, if it exists
459428 QScriptValue timerFunction = _timerFunctionMap.value(callingTimer);
460429 if (timerFunction.isValid()) {
461430 timerFunction.call();
462431 }
463−
432+
464433 if (!callingTimer->isActive()) {
465434 // this timer is done, we can kill it
466435 delete callingTimer;
@@ -471,14 +440,14 @@ QObject* ScriptEngine::setupTimerWithInterval(const QScriptValue& function, int · 4 unmodified lines
471440 // create the timer, add it to the map, and start it
472441 QTimer* newTimer = new QTimer(this);
473442 newTimer->setSingleShot(isSingleShot);
474−
443+
475444 connect(newTimer, &QTimer::timeout, this, &ScriptEngine::timerFired);
476−
445+
477446 // make sure the timer stops when the script does
478447 connect(this, &ScriptEngine::scriptEnding, newTimer, &QTimer::stop);
479−
448+
480449 _timerFunctionMap.insert(newTimer, function);
481−
450+
482451 newTimer->start(intervalMS);
483452 return newTimer;
484453 }
@@ -505,17 +474,17 @@ QUrl ScriptEngine::resolveInclude(const QString& include) const { · 20 unmodified lines
505474 if (!url.scheme().isEmpty()) {
506475 return url;
507476 }
508−
509− // we apparently weren't a fully qualified url, so, let's assume we're relative
477+
478+ // we apparently weren't a fully qualified url, so, let's assume we're relative
510479 // to the original URL of our script
511480 QUrl parentURL(_fileNameString);
512−
481+
513482 // if the parent URL's scheme is empty, then this is probably a local file...
514483 if (parentURL.scheme().isEmpty()) {
515484 parentURL = QUrl::fromLocalFile(_fileNameString);
516485 }
517−
518− // at this point we should have a legitimate fully qualified URL for our parent
486+
487+ // at this point we should have a legitimate fully qualified URL for our parent
519488 url = parentURL.resolved(url);
520489 return url;
521490 }
@@ -543,7 +512,7 @@ void ScriptEngine::include(const QString& includeFile) { · 21 unmodified lines
543512 loop.exec();
544513 includeContents = reply->readAll();
545514 }
546−
515+
547516 QScriptValue result = _engine.evaluate(includeContents);
548517 if (_engine.hasUncaughtException()) {
549518 int line = _engine.uncaughtExceptionLineNumber();