diff --git a/examples/gun.js b/examples/gun.js index 7e486ee92d..b93131c0f7 100644 --- a/examples/gun.js +++ b/examples/gun.js @@ -92,7 +92,8 @@ function checkController() { " print('voxelColor=' + voxelColor.red + ', ' + voxelColor.green + ', ' + voxelColor.blue + '\\n'); " + " var myColor = Particle.getColor();" + " print('myColor=' + myColor.red + ', ' + myColor.green + ', ' + myColor.blue + '\\n'); " + - " Particle.setColor(voxelColor); " + + " var newProps = { color: voxelColor }; " + + " Particle.setProperties(newProps); " + " var voxelAt = voxel.getPosition();" + " var voxelScale = voxel.getScale();" + " Voxels.eraseVoxel(voxelAt.x, voxelAt.y, voxelAt.z, voxelScale); " + diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 30b298332c..5d30d8b991 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1519,16 +1519,18 @@ void Application::shootParticle() { QString script( " function collisionWithVoxel(voxel) { " " print('collisionWithVoxel(voxel)... '); " - " print('myID=' + Particle.getID() + '\\n'); " + " print('myID=' + Particle.getID()); " " var voxelColor = voxel.getColor();" - " print('voxelColor=' + voxelColor.red + ', ' + voxelColor.green + ', ' + voxelColor.blue + '\\n'); " + " print('voxelColor=' + voxelColor.red + ', ' + voxelColor.green + ', ' + voxelColor.blue ); " " var myColor = Particle.getColor();" - " print('myColor=' + myColor.red + ', ' + myColor.green + ', ' + myColor.blue + '\\n'); " - " Particle.setColor(voxelColor); " + " print('myColor=' + myColor.red + ', ' + myColor.green + ', ' + myColor.blue ); " + " var newProps = { color: voxelColor }; " + " print('about to call Particle.setProperties()'); " + " Particle.setProperties(newProps); " " var voxelAt = voxel.getPosition();" " var voxelScale = voxel.getScale();" " Voxels.eraseVoxel(voxelAt.x, voxelAt.y, voxelAt.z, voxelScale); " - " print('Voxels.eraseVoxel(' + voxelAt.x + ', ' + voxelAt.y + ', ' + voxelAt.z + ', ' + voxelScale + ')... \\n'); " + " print('Voxels.eraseVoxel(' + voxelAt.x + ', ' + voxelAt.y + ', ' + voxelAt.z + ', ' + voxelScale + ')...'); " " } " " Particle.collisionWithVoxel.connect(collisionWithVoxel); " ); diff --git a/libraries/particles/src/Particle.cpp b/libraries/particles/src/Particle.cpp index d989bcb01b..e3c1247176 100644 --- a/libraries/particles/src/Particle.cpp +++ b/libraries/particles/src/Particle.cpp @@ -641,6 +641,40 @@ void Particle::copyChangedProperties(const Particle& other) { setAge(age); } +ParticleProperties Particle::getProperties() const { + ParticleProperties properties; + properties.copyFromParticle(*this); + return properties; +} + +void Particle::setProperties(const ParticleProperties& properties) { + properties.copyToParticle(*this); +} + +ParticleProperties::ParticleProperties() : + _position(0), + _color(), + _radius(0), + _velocity(0), + _gravity(DEFAULT_GRAVITY), + _damping(DEFAULT_DAMPING), + _lifetime(DEFAULT_LIFETIME), + _script(""), + _inHand(false), + _shouldDie(false), + + _positionChanged(false), + _colorChanged(false), + _radiusChanged(false), + _velocityChanged(false), + _gravityChanged(false), + _dampingChanged(false), + _lifetimeChanged(false), + _scriptChanged(false), + _inHandChanged(false), + _shouldDieChanged(false) +{ +} QScriptValue ParticleProperties::copyToScriptValue(QScriptEngine* engine) const { QScriptValue properties = engine->newObject(); @@ -866,3 +900,11 @@ void ParticleProperties::copyFromParticle(const Particle& particle) { _inHandChanged = false; _shouldDieChanged = false; } + +QScriptValue ParticlePropertiesToScriptValue(QScriptEngine* engine, const ParticleProperties& properties) { + return properties.copyToScriptValue(engine); +} + +void ParticlePropertiesFromScriptValue(const QScriptValue &object, ParticleProperties& properties) { + properties.copyFromScriptValue(object); +} diff --git a/libraries/particles/src/Particle.h b/libraries/particles/src/Particle.h index 1c1c47dbe9..5eace8daff 100644 --- a/libraries/particles/src/Particle.h +++ b/libraries/particles/src/Particle.h @@ -23,7 +23,8 @@ class VoxelsScriptingInterface; class ParticlesScriptingInterface; class VoxelEditPacketSender; class ParticleEditPacketSender; - +class ParticleProperties; +class Particle; const uint32_t NEW_PARTICLE = 0xFFFFFFFF; const uint32_t UNKNOWN_TOKEN = 0xFFFFFFFF; @@ -51,6 +52,42 @@ const QString DEFAULT_SCRIPT(""); const bool IN_HAND = true; // it's in a hand const bool NOT_IN_HAND = !IN_HAND; // it's not in a hand +/// Used in scripting to set/get the complete set of particle properties via JavaScript hashes/QScriptValues +class ParticleProperties { +public: + ParticleProperties(); + + QScriptValue copyToScriptValue(QScriptEngine* engine) const; + void copyFromScriptValue(const QScriptValue& object); + + void copyToParticle(Particle& particle) const; + void copyFromParticle(const Particle& particle); + +private: + glm::vec3 _position; + xColor _color; + float _radius; + glm::vec3 _velocity; + glm::vec3 _gravity; + float _damping; + float _lifetime; + QString _script; + bool _inHand; + bool _shouldDie; + + bool _positionChanged; + bool _colorChanged; + bool _radiusChanged; + bool _velocityChanged; + bool _gravityChanged; + bool _dampingChanged; + bool _lifetimeChanged; + bool _scriptChanged; + bool _inHandChanged; + bool _shouldDieChanged; +}; + + class Particle { public: @@ -77,6 +114,7 @@ public: bool getInHand() const { return _inHand; } float getDamping() const { return _damping; } float getLifetime() const { return _lifetime; } + ParticleProperties getProperties() const; /// The last updated/simulated time of this particle from the time perspective of the authoritative server/source uint64_t getLastUpdated() const { return _lastUpdated; } @@ -110,6 +148,7 @@ public: void setLifetime(float value) { _lifetime = value; } void setScript(QString updateScript) { _script = updateScript; } void setCreatorTokenID(uint32_t creatorTokenID) { _creatorTokenID = creatorTokenID; } + void setProperties(const ParticleProperties& properties); bool appendParticleData(OctreePacketData* packetData) const; int readParticleDataFromBuffer(const unsigned char* data, int bytesLeftToRead, ReadBitstreamToTreeParams& args); @@ -196,6 +235,7 @@ public slots: bool getShouldDie() { return _particle->getShouldDie(); } float getAge() const { return _particle->getAge(); } float getLifetime() const { return _particle->getLifetime(); } + ParticleProperties getProperties() const { return _particle->getProperties(); } void setPosition(glm::vec3 value) { _particle->setPosition(value); } void setVelocity(glm::vec3 value) { _particle->setVelocity(value); } @@ -206,6 +246,7 @@ public slots: void setShouldDie(bool value) { _particle->setShouldDie(value); } void setScript(const QString& script) { _particle->setScript(script); } void setLifetime(float value) const { return _particle->setLifetime(value); } + void setProperties(const ParticleProperties& properties) { return _particle->setProperties(properties); } signals: void update(); @@ -216,41 +257,11 @@ private: Particle* _particle; }; +Q_DECLARE_METATYPE(ParticleProperties); -class ParticleProperties : public QObject { - Q_OBJECT -public: - ParticleProperties(); - QScriptValue copyToScriptValue(QScriptEngine* engine) const; - void copyFromScriptValue(const QScriptValue& object); - - void copyToParticle(Particle& particle) const; - void copyFromParticle(const Particle& particle); - -private: - glm::vec3 _position; - xColor _color; - float _radius; - glm::vec3 _velocity; - glm::vec3 _gravity; - float _damping; - float _lifetime; - QString _script; - bool _inHand; - bool _shouldDie; - - bool _positionChanged; - bool _colorChanged; - bool _radiusChanged; - bool _velocityChanged; - bool _gravityChanged; - bool _dampingChanged; - bool _lifetimeChanged; - bool _scriptChanged; - bool _inHandChanged; - bool _shouldDieChanged; -}; +QScriptValue ParticlePropertiesToScriptValue(QScriptEngine* engine, const ParticleProperties& properties); +void ParticlePropertiesFromScriptValue(const QScriptValue &object, ParticleProperties& properties); #endif /* defined(__hifi__Particle__) */ diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index a98a8654e4..28fd07d158 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -99,6 +99,7 @@ void ScriptEngine::init() { // register meta-type for glm::vec3 conversions registerMetaTypes(&_engine); + qScriptRegisterMetaType(&_engine, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue); QScriptValue agentValue = _engine.newQObject(this); _engine.globalObject().setProperty("Agent", agentValue); diff --git a/libraries/shared/src/RegisteredMetaTypes.cpp b/libraries/shared/src/RegisteredMetaTypes.cpp index 9a099b4171..9d2eec6b40 100644 --- a/libraries/shared/src/RegisteredMetaTypes.cpp +++ b/libraries/shared/src/RegisteredMetaTypes.cpp @@ -56,3 +56,4 @@ void xColorFromScriptValue(const QScriptValue &object, xColor& color) { color.green = object.property("green").toVariant().toInt(); color.blue = object.property("blue").toVariant().toInt(); } +