From 8b9f6813ca085246564a3d6e5557bc3bebb6b48b Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 30 Jan 2014 12:26:50 -0800 Subject: [PATCH 1/7] add easier support for key codes in JS by adding text property and auto-detecting isShifted --- examples/controllerExample.js | 101 +++++++++++++- libraries/script-engine/src/EventTypes.cpp | 155 ++++++++++++++++++++- libraries/script-engine/src/EventTypes.h | 11 +- 3 files changed, 260 insertions(+), 7 deletions(-) diff --git a/examples/controllerExample.js b/examples/controllerExample.js index c1b33b24a5..95561dc9dc 100644 --- a/examples/controllerExample.js +++ b/examples/controllerExample.js @@ -48,6 +48,15 @@ function checkController() { function keyPressEvent(event) { print("keyPressEvent event.key=" + event.key); + print("keyPressEvent event.text=" + event.text); + + print("keyPressEvent event.isShifted=" + event.isShifted); + print("keyPressEvent event.isControl=" + event.isControl); + print("keyPressEvent event.isMeta=" + event.isMeta); + print("keyPressEvent event.isAlt=" + event.isAlt); + print("keyPressEvent event.isKeypad=" + event.isKeypad); + + if (event.key == "A".charCodeAt(0)) { print("the A key was pressed"); } @@ -77,14 +86,102 @@ Agent.willSendVisualDataCallback.connect(checkController); // Map keyPress and mouse move events to our callbacks Controller.keyPressEvent.connect(keyPressEvent); -var AKeyEvent = { +var KeyEvent_A = { key: "A".charCodeAt(0), + text: "A", isShifted: false, isMeta: false }; +var KeyEvent_a = { + text: "a", + isShifted: false, + isMeta: false +}; + +var KeyEvent_a2 = { + key: "a".charCodeAt(0), + isShifted: false, + isMeta: false +}; + +var KeyEvent_a3 = { + text: "a" +}; + +var KeyEvent_A2 = { + text: "A" +}; + + +var KeyEvent_9 = { + text: "9" +}; + +var KeyEvent_Num = { + text: "#" +}; + +var KeyEvent_At = { + text: "@" +}; + +var KeyEvent_MetaAt = { + text: "@", + isMeta: true +}; + +var KeyEvent_Up = { + text: "up" +}; +var KeyEvent_Down = { + text: "down" +}; +var KeyEvent_Left = { + text: "left" +}; +var KeyEvent_Right = { + text: "right" +}; + // prevent the A key from going through to the application -Controller.captureKeyEvents(AKeyEvent); +print("KeyEvent_A"); +Controller.captureKeyEvents(KeyEvent_A); + +print("KeyEvent_A2"); +Controller.captureKeyEvents(KeyEvent_A2); + +print("KeyEvent_a"); +Controller.captureKeyEvents(KeyEvent_a); + +print("KeyEvent_a2"); +Controller.captureKeyEvents(KeyEvent_a2); + +print("KeyEvent_a3"); +Controller.captureKeyEvents(KeyEvent_a3); + +print("KeyEvent_9"); +Controller.captureKeyEvents(KeyEvent_9); + +print("KeyEvent_Num"); +Controller.captureKeyEvents(KeyEvent_Num); + +print("KeyEvent_At"); +Controller.captureKeyEvents(KeyEvent_At); + +print("KeyEvent_MetaAt"); +Controller.captureKeyEvents(KeyEvent_MetaAt); + +print("KeyEvent_Up"); +Controller.captureKeyEvents(KeyEvent_Up); +print("KeyEvent_Down"); +Controller.captureKeyEvents(KeyEvent_Down); +print("KeyEvent_Left"); +Controller.captureKeyEvents(KeyEvent_Left); +print("KeyEvent_Right"); +Controller.captureKeyEvents(KeyEvent_Right); + + Controller.mouseMoveEvent.connect(mouseMoveEvent); diff --git a/libraries/script-engine/src/EventTypes.cpp b/libraries/script-engine/src/EventTypes.cpp index 5c4ab7f2a7..ff27282b73 100644 --- a/libraries/script-engine/src/EventTypes.cpp +++ b/libraries/script-engine/src/EventTypes.cpp @@ -8,22 +8,72 @@ // Used to register meta-types with Qt for very various event types so that they can be exposed to our // scripting engine +#include #include "EventTypes.h" KeyEvent::KeyEvent() { key = 0; + text = QString(""); isShifted = false; isMeta = false; + isControl = false; isValid = false; } KeyEvent::KeyEvent(const QKeyEvent& event) { key = event.key(); + text = event.text(); isShifted = event.modifiers().testFlag(Qt::ShiftModifier); - isMeta = event.modifiers().testFlag(Qt::ControlModifier); + isMeta = event.modifiers().testFlag(Qt::MetaModifier); + isControl = event.modifiers().testFlag(Qt::ControlModifier); + isAlt = event.modifiers().testFlag(Qt::AltModifier); + isKeypad = event.modifiers().testFlag(Qt::KeypadModifier); isValid = true; + + // handle special text for special characters... + if (key == Qt::Key_F1) { + text = "F1"; + } else if (key == Qt::Key_F2) { + text = "F2"; + } else if (key == Qt::Key_F3) { + text = "F3"; + } else if (key == Qt::Key_F4) { + text = "F4"; + } else if (key == Qt::Key_F5) { + text = "F5"; + } else if (key == Qt::Key_F6) { + text = "F6"; + } else if (key == Qt::Key_F7) { + text = "F7"; + } else if (key == Qt::Key_F8) { + text = "F8"; + } else if (key == Qt::Key_F9) { + text = "F9"; + } else if (key == Qt::Key_F10) { + text = "F10"; + } else if (key == Qt::Key_F11) { + text = "F11"; + } else if (key == Qt::Key_F12) { + text = "F12"; + } else if (key == Qt::Key_Up) { + text = "UP"; + } else if (key == Qt::Key_Down) { + text = "DOWN"; + } else if (key == Qt::Key_Left) { + text = "LEFT"; + } else if (key == Qt::Key_Right) { + text = "RIGHT"; + } else if (key == Qt::Key_Escape) { + text = "ESC"; + } else if (key == Qt::Key_Tab) { + text = "TAB"; + } else if (key == Qt::Key_Delete) { + text = "DELETE"; + } else if (key == Qt::Key_Backspace) { + text = "BACKSPACE"; + } } MouseEvent::MouseEvent(const QMouseEvent& event) { @@ -65,16 +115,113 @@ void registerEventTypes(QScriptEngine* engine) { QScriptValue keyEventToScriptValue(QScriptEngine* engine, const KeyEvent& event) { QScriptValue obj = engine->newObject(); obj.setProperty("key", event.key); + obj.setProperty("text", event.text); obj.setProperty("isShifted", event.isShifted); obj.setProperty("isMeta", event.isMeta); + obj.setProperty("isControl", event.isControl); + obj.setProperty("isAlt", event.isAlt); + obj.setProperty("isKeypad", event.isKeypad); return obj; } void keyEventFromScriptValue(const QScriptValue &object, KeyEvent& event) { - event.key = object.property("key").toVariant().toInt(); - event.isShifted = object.property("isShifted").toVariant().toBool(); + + event.isValid = false; // assume the worst event.isMeta = object.property("isMeta").toVariant().toBool(); - event.isValid = object.property("key").isValid(); + event.isControl = object.property("isControl").toVariant().toBool(); + event.isAlt = object.property("isAlt").toVariant().toBool(); + event.isKeypad = object.property("isKeypad").toVariant().toBool(); + + QScriptValue key = object.property("key"); + if (key.isValid()) { + event.key = key.toVariant().toInt(); + event.text = QString(QChar(event.key)); + event.isValid = true; + } else { + QScriptValue text = object.property("text"); + if (text.isValid()) { + event.text = object.property("text").toVariant().toString(); + + // if the text is a special command, then map it here... + // TODO: come up with more elegant solution here, a map? is there a Qt function that gives nice names for keys? + if (event.text.toUpper() == "F1") { + event.key = Qt::Key_F1; + } else if (event.text.toUpper() == "F2") { + event.key = Qt::Key_F2; + } else if (event.text.toUpper() == "F3") { + event.key = Qt::Key_F3; + } else if (event.text.toUpper() == "F4") { + event.key = Qt::Key_F4; + } else if (event.text.toUpper() == "F5") { + event.key = Qt::Key_F5; + } else if (event.text.toUpper() == "F6") { + event.key = Qt::Key_F6; + } else if (event.text.toUpper() == "F7") { + event.key = Qt::Key_F7; + } else if (event.text.toUpper() == "F8") { + event.key = Qt::Key_F8; + } else if (event.text.toUpper() == "F9") { + event.key = Qt::Key_F9; + } else if (event.text.toUpper() == "F10") { + event.key = Qt::Key_F10; + } else if (event.text.toUpper() == "F11") { + event.key = Qt::Key_F11; + } else if (event.text.toUpper() == "F12") { + event.key = Qt::Key_F12; + } else if (event.text.toUpper() == "UP") { + event.key = Qt::Key_Up; + event.isKeypad = true; + } else if (event.text.toUpper() == "DOWN") { + event.key = Qt::Key_Down; + event.isKeypad = true; + } else if (event.text.toUpper() == "LEFT") { + event.key = Qt::Key_Left; + event.isKeypad = true; + } else if (event.text.toUpper() == "RIGHT") { + event.key = Qt::Key_Right; + event.isKeypad = true; + } else if (event.text.toUpper() == "ESC") { + event.key = Qt::Key_Escape; + } else if (event.text.toUpper() == "TAB") { + event.key = Qt::Key_Tab; + } else if (event.text.toUpper() == "DELETE") { + event.key = Qt::Key_Delete; + } else if (event.text.toUpper() == "BACKSPACE") { + event.key = Qt::Key_Backspace; + } else { + event.key = event.text.at(0).unicode(); + } + event.isValid = true; + } + } + + QScriptValue isShifted = object.property("isShifted"); + if (isShifted.isValid()) { + event.isShifted = isShifted.toVariant().toBool(); + } else { + // if no isShifted was included, get it from the text + QChar character = event.text.at(0); + if (character.isLetter() && character.isUpper()) { + event.isShifted = true; + } else { + // if it's a symbol, then attempt to detect shifted-ness + if (QString("~!@#$%^&*()_+{}|:\"<>?").contains(character)) { + event.isShifted = true; + } + } + } + + + const bool wantDebug = false; + if (wantDebug) { + qDebug() << "event.key=" << event.key + << " event.text=" << event.text + << " event.isShifted=" << event.isShifted + << " event.isControl=" << event.isControl + << " event.isMeta=" << event.isMeta + << " event.isAlt=" << event.isAlt + << " event.isKeypad=" << event.isKeypad; + } } QScriptValue mouseEventToScriptValue(QScriptEngine* engine, const MouseEvent& event) { diff --git a/libraries/script-engine/src/EventTypes.h b/libraries/script-engine/src/EventTypes.h index c3764b2619..81cba03cea 100644 --- a/libraries/script-engine/src/EventTypes.h +++ b/libraries/script-engine/src/EventTypes.h @@ -24,10 +24,19 @@ public: KeyEvent(); KeyEvent(const QKeyEvent& event); inline bool operator==(const KeyEvent& other) const { - return other.key == key && other.isShifted == isShifted && other.isMeta == isMeta; } + return other.key == key + && other.isShifted == isShifted + && other.isControl == isControl + && other.isMeta == isMeta + && other.isAlt == isAlt + && other.isKeypad == isKeypad; } int key; + QString text; bool isShifted; + bool isControl; bool isMeta; + bool isAlt; + bool isKeypad; bool isValid; }; From a6934605a456bd261e3196256428742045a56581 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 29 Jan 2014 20:55:12 -0800 Subject: [PATCH 2/7] implement a basic exampple of ride along with a particle --- examples/rideAlongWithAParticleExample.js | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/rideAlongWithAParticleExample.js diff --git a/examples/rideAlongWithAParticleExample.js b/examples/rideAlongWithAParticleExample.js new file mode 100644 index 0000000000..eca9fe7f4c --- /dev/null +++ b/examples/rideAlongWithAParticleExample.js @@ -0,0 +1,50 @@ +// +// rideAlongWithAParticleExample.js +// hifi +// +// Created by Brad Hefta-Gaub on 1/24/14. +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. +// +// This is an example script that demonstrates "finding" particles +// + +var iteration = 0; +var lengthOfRide = 2000; // in iterations + +var particleA = Particles.addParticle( + { + position: { x: 10, y: 0, z: 10 }, + velocity: { x: 5, y: 0, z: 5 }, + gravity: { x: 0, y: 0, z: 0 }, + radius : 0.1, + color: { red: 0, green: 255, blue: 0 }, + damping: 0, + lifetime: (lengthOfRide * 60) + 1 + }); + +function rideWithParticle() { + + if (iteration <= lengthOfRide) { + + // Check to see if we've been notified of the actual identity of the particles we created + if (!particleA.isKnownID) { + particleA = Particles.identifyParticle(particleA); + } + + var propertiesA = Particles.getParticleProperties(particleA); + var newPosition = propertiesA.position; + MyAvatar.position = { x: propertiesA.position.x, + y: propertiesA.position.y + 2, + z: propertiesA.position.z }; + } else { + Agent.stop(); + } + + iteration++; + print("iteration="+iteration); +} + + +// register the call back so it fires before each data send +Agent.willSendVisualDataCallback.connect(rideWithParticle); + From 3b17550d92594c48fbe84e4ca1b8210517487cf2 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 30 Jan 2014 16:36:26 -0800 Subject: [PATCH 3/7] make sure to flush edit queue on script shutdown --- libraries/particles/src/Particle.cpp | 14 +------------ .../src/ParticlesScriptingInterface.cpp | 19 +++++++---------- libraries/script-engine/src/ScriptEngine.cpp | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/libraries/particles/src/Particle.cpp b/libraries/particles/src/Particle.cpp index 557d4ca87f..c7afc49668 100644 --- a/libraries/particles/src/Particle.cpp +++ b/libraries/particles/src/Particle.cpp @@ -60,8 +60,6 @@ void Particle::handleAddParticleResponse(unsigned char* packetData , int packetL memcpy(&particleID, dataAt, sizeof(particleID)); dataAt += sizeof(particleID); - //qDebug() << "handleAddParticleResponse()... particleID=" << particleID << " creatorTokenID=" << creatorTokenID; - // add our token to id mapping _tokenIDsToIDs[creatorTokenID] = particleID; } @@ -362,8 +360,6 @@ Particle Particle::fromEditPacket(unsigned char* data, int length, int& processe dataAt += sizeof(editID); processedBytes += sizeof(editID); - //qDebug() << "editID:" << editID; - bool isNewParticle = (editID == NEW_PARTICLE); // special case for handling "new" particles @@ -412,7 +408,6 @@ Particle Particle::fromEditPacket(unsigned char* data, int length, int& processe memcpy(&packetContainsBits, dataAt, sizeof(packetContainsBits)); dataAt += sizeof(packetContainsBits); processedBytes += sizeof(packetContainsBits); - //qDebug() << "packetContainsBits:" << packetContainsBits; } @@ -529,7 +524,6 @@ Particle Particle::fromEditPacket(unsigned char* data, int length, int& processe if (wantDebugging) { qDebug("Particle::fromEditPacket()..."); qDebug() << " Particle id in packet:" << editID; - //qDebug() << " position: " << newParticle._position; newParticle.debugDump(); } @@ -736,8 +730,6 @@ bool Particle::encodeParticleEditMessageDetails(PACKET_TYPE command, ParticleID // cleanup delete[] octcode; - //qDebug() << "encoding... sizeOut:" << sizeOut; - return success; } @@ -825,12 +817,8 @@ void Particle::update(const uint64_t& now) { _lastUpdated = now; // calculate our default shouldDie state... then allow script to change it if it wants... - float speed = glm::length(_velocity); - bool isStopped = (speed < MIN_VALID_SPEED); - const uint64_t REALLY_OLD = 30 * USECS_PER_SECOND; // 30 seconds - bool isReallyOld = ((now - _created) > REALLY_OLD); bool isInHand = getInHand(); - bool shouldDie = (getAge() > getLifetime()) || getShouldDie() || (!isInHand && isStopped && isReallyOld); + bool shouldDie = (getAge() > getLifetime()) || getShouldDie(); setShouldDie(shouldDie); executeUpdateScripts(); // allow the javascript to alter our state diff --git a/libraries/particles/src/ParticlesScriptingInterface.cpp b/libraries/particles/src/ParticlesScriptingInterface.cpp index 0ef8f82b00..7f4c29cb55 100644 --- a/libraries/particles/src/ParticlesScriptingInterface.cpp +++ b/libraries/particles/src/ParticlesScriptingInterface.cpp @@ -112,22 +112,19 @@ void ParticlesScriptingInterface::deleteParticle(ParticleID particleID) { properties.setShouldDie(true); uint32_t actualID = particleID.id; + + // if the particle is unknown, attempt to look it up if (!particleID.isKnownID) { actualID = Particle::getIDfromCreatorTokenID(particleID.creatorTokenID); - - // hmmm... we kind of want to bail if someone attempts to edit an unknown - if (actualID == UNKNOWN_PARTICLE_ID) { - //qDebug() << "ParticlesScriptingInterface::deleteParticle(), bailing - unknown particle..."; - return; // bailing early - } } - particleID.id = actualID; - particleID.isKnownID = true; + // if at this point, we know the id, send the update to the particle server + if (actualID != UNKNOWN_PARTICLE_ID) { + particleID.id = actualID; + particleID.isKnownID = true; + queueParticleMessage(PACKET_TYPE_PARTICLE_ADD_OR_EDIT, particleID, properties); + } - //qDebug() << "ParticlesScriptingInterface::deleteParticle(), queueParticleMessage......"; - queueParticleMessage(PACKET_TYPE_PARTICLE_ADD_OR_EDIT, particleID, properties); - // If we have a local particle tree set, then also update it. if (_particleTree) { _particleTree->lockForWrite(); diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 9db1254f80..b69f69f5f9 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -261,6 +261,27 @@ void ScriptEngine::run() { } } emit scriptEnding(); + + if (_voxelsScriptingInterface.getVoxelPacketSender()->serversExist()) { + // release the queue of edit voxel messages. + _voxelsScriptingInterface.getVoxelPacketSender()->releaseQueuedMessages(); + + // since we're in non-threaded mode, call process so that the packets are sent + if (!_voxelsScriptingInterface.getVoxelPacketSender()->isThreaded()) { + _voxelsScriptingInterface.getVoxelPacketSender()->process(); + } + } + + if (_particlesScriptingInterface.getParticlePacketSender()->serversExist()) { + // release the queue of edit voxel messages. + _particlesScriptingInterface.getParticlePacketSender()->releaseQueuedMessages(); + + // since we're in non-threaded mode, call process so that the packets are sent + if (!_particlesScriptingInterface.getParticlePacketSender()->isThreaded()) { + _particlesScriptingInterface.getParticlePacketSender()->process(); + } + } + cleanMenuItems(); // If we were on a thread, then wait till it's done From 00e98ee42b9c278cfc04d147e10b803324bd8d94 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 31 Jan 2014 15:44:48 -0800 Subject: [PATCH 4/7] make tryLock actually return a bool --- libraries/octree/src/Octree.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 94acf90ab7..9b8921208b 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -240,9 +240,9 @@ public: // Octree does not currently handle its own locking, caller must use these to lock/unlock void lockForRead() { lock.lockForRead(); } - void tryLockForRead() { lock.tryLockForRead(); } + bool tryLockForRead() { return lock.tryLockForRead(); } void lockForWrite() { lock.lockForWrite(); } - void tryLockForWrite() { lock.tryLockForWrite(); } + bool tryLockForWrite() { return lock.tryLockForWrite(); } void unlock() { lock.unlock(); } unsigned long getOctreeElementsCount(); From 19f218f37c3683a8449744b5b0a399bc0c63d176 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 31 Jan 2014 15:45:31 -0800 Subject: [PATCH 5/7] fix typo --- libraries/particles/src/ParticleCollisionSystem.cpp | 8 ++++---- libraries/particles/src/ParticleCollisionSystem.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/particles/src/ParticleCollisionSystem.cpp b/libraries/particles/src/ParticleCollisionSystem.cpp index b5fe827ebb..e69632f379 100644 --- a/libraries/particles/src/ParticleCollisionSystem.cpp +++ b/libraries/particles/src/ParticleCollisionSystem.cpp @@ -68,12 +68,12 @@ void ParticleCollisionSystem::checkParticle(Particle* particle) { updateCollisionWithAvatars(particle); } -void ParticleCollisionSystem::emitGlobalParicleCollisionWithVoxel(Particle* particle, VoxelDetail* voxelDetails) { +void ParticleCollisionSystem::emitGlobalParticleCollisionWithVoxel(Particle* particle, VoxelDetail* voxelDetails) { ParticleID particleID = particle->getParticleID(); emit particleCollisionWithVoxel(particleID, *voxelDetails); } -void ParticleCollisionSystem::emitGlobalParicleCollisionWithParticle(Particle* particleA, Particle* particleB) { +void ParticleCollisionSystem::emitGlobalParticleCollisionWithParticle(Particle* particleA, Particle* particleB) { ParticleID idA = particleA->getParticleID(); ParticleID idB = particleB->getParticleID(); emit particleCollisionWithParticle(idA, idB); @@ -95,7 +95,7 @@ void ParticleCollisionSystem::updateCollisionWithVoxels(Particle* particle) { particle->collisionWithVoxel(voxelDetails); // let the global script run their collision scripts for particles if they have them - emitGlobalParicleCollisionWithVoxel(particle, voxelDetails); + emitGlobalParticleCollisionWithVoxel(particle, voxelDetails); updateCollisionSound(particle, collisionInfo._penetration, COLLISION_FREQUENCY); collisionInfo._penetration /= (float)(TREE_SCALE); @@ -124,7 +124,7 @@ void ParticleCollisionSystem::updateCollisionWithParticles(Particle* particleA) if (glm::dot(relativeVelocity, penetration) > 0.0f) { particleA->collisionWithParticle(particleB); particleB->collisionWithParticle(particleA); - emitGlobalParicleCollisionWithParticle(particleA, particleB); + emitGlobalParticleCollisionWithParticle(particleA, particleB); glm::vec3 axis = glm::normalize(penetration); glm::vec3 axialVelocity = glm::dot(relativeVelocity, axis) * axis; diff --git a/libraries/particles/src/ParticleCollisionSystem.h b/libraries/particles/src/ParticleCollisionSystem.h index 9baf9bfd05..c525d3ddfc 100644 --- a/libraries/particles/src/ParticleCollisionSystem.h +++ b/libraries/particles/src/ParticleCollisionSystem.h @@ -58,8 +58,8 @@ signals: private: static bool updateOperation(OctreeElement* element, void* extraData); - void emitGlobalParicleCollisionWithVoxel(Particle* particle, VoxelDetail* voxelDetails); - void emitGlobalParicleCollisionWithParticle(Particle* particleA, Particle* particleB); + void emitGlobalParticleCollisionWithVoxel(Particle* particle, VoxelDetail* voxelDetails); + void emitGlobalParticleCollisionWithParticle(Particle* particleA, Particle* particleB); ParticleEditPacketSender* _packetSender; ParticleTree* _particles; From 5b825ecd5b02ffe5999f0e1f448b85536b2570c7 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 31 Jan 2014 15:46:19 -0800 Subject: [PATCH 6/7] fix isKnownID --- libraries/particles/src/Particle.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/particles/src/Particle.cpp b/libraries/particles/src/Particle.cpp index a5ee38072f..12b59d28c9 100644 --- a/libraries/particles/src/Particle.cpp +++ b/libraries/particles/src/Particle.cpp @@ -1056,7 +1056,7 @@ QScriptValue ParticleProperties::copyToScriptValue(QScriptEngine* engine) const if (_idSet) { properties.setProperty("id", _id); - properties.setProperty("isKnownID", (_id == UNKNOWN_PARTICLE_ID)); + properties.setProperty("isKnownID", (_id != UNKNOWN_PARTICLE_ID)); } return properties; From 289be04f0f7fb58fd79a31454e2f300f9b29f1bc Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 31 Jan 2014 17:12:50 -0800 Subject: [PATCH 7/7] use tryLock in some cases where we don't want to block and pass already locked to the findParticleByID() in scripting interface --- interface/src/ParticleTreeRenderer.cpp | 9 +++++---- libraries/particles/src/ParticleCollisionSystem.cpp | 7 ++++--- .../particles/src/ParticlesScriptingInterface.cpp | 11 ++++++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/interface/src/ParticleTreeRenderer.cpp b/interface/src/ParticleTreeRenderer.cpp index 7c82f76aab..d96ac2beaa 100644 --- a/interface/src/ParticleTreeRenderer.cpp +++ b/interface/src/ParticleTreeRenderer.cpp @@ -32,10 +32,11 @@ void ParticleTreeRenderer::init() { void ParticleTreeRenderer::update() { if (_tree) { - ParticleTree* tree = (ParticleTree*)_tree; - _tree->lockForWrite(); - tree->update(); - _tree->unlock(); + ParticleTree* tree = static_cast(_tree); + if (tree->tryLockForWrite()) { + tree->update(); + tree->unlock(); + } } } diff --git a/libraries/particles/src/ParticleCollisionSystem.cpp b/libraries/particles/src/ParticleCollisionSystem.cpp index e69632f379..c718ab3ddc 100644 --- a/libraries/particles/src/ParticleCollisionSystem.cpp +++ b/libraries/particles/src/ParticleCollisionSystem.cpp @@ -56,9 +56,10 @@ bool ParticleCollisionSystem::updateOperation(OctreeElement* element, void* extr void ParticleCollisionSystem::update() { // update all particles - _particles->lockForWrite(); - _particles->recurseTreeWithOperation(updateOperation, this); - _particles->unlock(); + if (_particles->tryLockForWrite()) { + _particles->recurseTreeWithOperation(updateOperation, this); + _particles->unlock(); + } } diff --git a/libraries/particles/src/ParticlesScriptingInterface.cpp b/libraries/particles/src/ParticlesScriptingInterface.cpp index 73aaad4cc5..a25dde1b9e 100644 --- a/libraries/particles/src/ParticlesScriptingInterface.cpp +++ b/libraries/particles/src/ParticlesScriptingInterface.cpp @@ -43,6 +43,7 @@ ParticleID ParticlesScriptingInterface::addParticle(const ParticleProperties& pr ParticleID ParticlesScriptingInterface::identifyParticle(ParticleID particleID) { uint32_t actualID = particleID.id; + if (!particleID.isKnownID) { actualID = Particle::getIDfromCreatorTokenID(particleID.creatorTokenID); if (actualID == UNKNOWN_PARTICLE_ID) { @@ -65,8 +66,12 @@ ParticleProperties ParticlesScriptingInterface::getParticleProperties(ParticleID } if (_particleTree) { _particleTree->lockForRead(); - const Particle* particle = _particleTree->findParticleByID(identity.id); - results.copyFromParticle(*particle); + const Particle* particle = _particleTree->findParticleByID(identity.id, true); + if (particle) { + results.copyFromParticle(*particle); + } else { + results.setIsUnknownID(); + } _particleTree->unlock(); } @@ -123,7 +128,7 @@ void ParticlesScriptingInterface::deleteParticle(ParticleID particleID) { if (actualID != UNKNOWN_PARTICLE_ID) { particleID.id = actualID; particleID.isKnownID = true; - queueParticleMessage(PACKET_TYPE_PARTICLE_ADD_OR_EDIT, particleID, properties); + queueParticleMessage(PacketTypeParticleAddOrEdit, particleID, properties); } // If we have a local particle tree set, then also update it.