From 27e6f99debfed792ef0ad4a5fb95e51f3a0e070f Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 20 Feb 2015 16:54:58 -0800 Subject: [PATCH 1/5] use strings for shapeType for JS Entity properties --- examples/html/entityProperties.html | 8 ++-- .../entities/src/EntityItemProperties.cpp | 44 +++++++++++++++++-- libraries/entities/src/EntityItemProperties.h | 3 ++ .../entities/src/EntityItemPropertiesMacros.h | 9 ---- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/examples/html/entityProperties.html b/examples/html/entityProperties.html index 84a8d23a74..ad2b359e79 100644 --- a/examples/html/entityProperties.html +++ b/examples/html/entityProperties.html @@ -406,7 +406,7 @@ elModelAnimationFrame.addEventListener('change', createEmitNumberPropertyUpdateFunction('animationFrameIndex')); elModelAnimationSettings.addEventListener('change', createEmitTextPropertyUpdateFunction('animationSettings')); elModelTextures.addEventListener('change', createEmitTextPropertyUpdateFunction('textures')); - elModelShapeType.addEventListener('change', createEmitNumberPropertyUpdateFunction('shapeType')); + elModelShapeType.addEventListener('change', createEmitTextPropertyUpdateFunction('shapeType')); elTextText.addEventListener('change', createEmitTextPropertyUpdateFunction('text')); elTextLineHeight.addEventListener('change', createEmitNumberPropertyUpdateFunction('lineHeight')); @@ -671,9 +671,9 @@
Shape Type
diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 353aab0a64..7f37799bbc 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -10,6 +10,7 @@ // #include +#include #include #include @@ -170,6 +171,35 @@ void EntityItemProperties::setLastEdited(quint64 usecTime) { _lastEdited = usecTime > _created ? usecTime : _created; } +const char* shapeTypeNames[] = {"none", "box", "sphere"}; + +QString EntityItemProperties::getShapeTypeString() const { + return QString(shapeTypeNames[_shapeType]); +} + +QHash stringToShapeTypeLookup; + +void buildStringToShapeTypeLookup() { + stringToShapeTypeLookup["none"] = SHAPE_TYPE_NONE; + stringToShapeTypeLookup["box"] = SHAPE_TYPE_BOX; + stringToShapeTypeLookup["sphere"] = SHAPE_TYPE_SPHERE; +} + +void EntityItemProperties::setShapeTypeFromString(const QString& shapeName) { + if (stringToShapeTypeLookup.empty()) { + buildStringToShapeTypeLookup(); + } + auto shapeTypeItr = stringToShapeTypeLookup.find(shapeName.toLower()); + ShapeType newShapeType = SHAPE_TYPE_NONE; + if (shapeTypeItr != stringToShapeTypeLookup.end()) { + newShapeType = shapeTypeItr.value(); + } + if (newShapeType != _shapeType) { + _shapeType = newShapeType; + _shapeTypeChanged = true; + } +} + EntityPropertyFlags EntityItemProperties::getChangedProperties() const { EntityPropertyFlags changedProperties; @@ -270,7 +300,7 @@ QScriptValue EntityItemProperties::copyToScriptValue(QScriptEngine* engine) cons COPY_PROPERTY_TO_QSCRIPTVALUE(lineHeight); COPY_PROPERTY_TO_QSCRIPTVALUE_COLOR_GETTER(textColor, getTextColor()); COPY_PROPERTY_TO_QSCRIPTVALUE_COLOR_GETTER(backgroundColor, getBackgroundColor()); - COPY_PROPERTY_TO_QSCRIPTVALUE(shapeType); + properties.setProperty("shapeType", getShapeTypeString()); // Sitting properties support QScriptValue sittingPoints = engine->newObject(); @@ -303,8 +333,6 @@ QScriptValue EntityItemProperties::copyToScriptValue(QScriptEngine* engine) cons } void EntityItemProperties::copyFromScriptValue(const QScriptValue& object) { - - QScriptValue typeScriptValue = object.property("type"); if (typeScriptValue.isValid()) { setType(typeScriptValue.toVariant().toString()); @@ -350,7 +378,15 @@ void EntityItemProperties::copyFromScriptValue(const QScriptValue& object) { COPY_PROPERTY_FROM_QSCRIPTVALUE_FLOAT(lineHeight, setLineHeight); COPY_PROPERTY_FROM_QSCRIPTVALUE_COLOR(textColor, setTextColor); COPY_PROPERTY_FROM_QSCRIPTVALUE_COLOR(backgroundColor, setBackgroundColor); - COPY_PROPERTY_FROM_QSCRIPTVALUE_ENUM(shapeType, setShapeType, ShapeType); + + QScriptValue shapeType = object.property("shapeType"); + if (shapeType.isValid()) { + QString newValue = shapeType.toVariant().toString(); + if (_defaultSettings || newValue != getShapeTypeString()) { + setShapeTypeFromString(newValue); + } + } + _lastEdited = usecTimestampNow(); } diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index 51779d3f56..070b80f754 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -183,6 +183,9 @@ public: DEFINE_PROPERTY_REF(PROP_BACKGROUND_COLOR, BackgroundColor, backgroundColor, xColor); DEFINE_PROPERTY_REF(PROP_SHAPE_TYPE, ShapeType, shapeType, ShapeType); + QString getShapeTypeString() const; + void setShapeTypeFromString(const QString& shapeName); + public: float getMaxDimension() const { return glm::max(_dimensions.x, _dimensions.y, _dimensions.z); } diff --git a/libraries/entities/src/EntityItemPropertiesMacros.h b/libraries/entities/src/EntityItemPropertiesMacros.h index 592f808e1a..9cf16c7a05 100644 --- a/libraries/entities/src/EntityItemPropertiesMacros.h +++ b/libraries/entities/src/EntityItemPropertiesMacros.h @@ -180,15 +180,6 @@ #define COPY_PROPERTY_TO_QSCRIPTVALUE(P) \ properties.setProperty(#P, _##P); -#define COPY_PROPERTY_FROM_QSCRIPTVALUE_ENUM(P, S, E) \ - QScriptValue P = object.property(#P); \ - if (P.isValid()) { \ - E newValue = (E)(P.toVariant().toInt()); \ - if (_defaultSettings || newValue != _##P) { \ - S(newValue); \ - } \ - } - #define COPY_PROPERTY_FROM_QSCRIPTVALUE_FLOAT(P, S) \ QScriptValue P = object.property(#P); \ if (P.isValid()) { \ From 0fc4c732f7c339e7fe06bfa5e34ee1504f69c3b3 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 23 Feb 2015 10:13:47 -0800 Subject: [PATCH 2/5] use macro for copying shapeType string to script --- libraries/entities/src/EntityItemProperties.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 7f37799bbc..923eef667c 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -300,7 +300,7 @@ QScriptValue EntityItemProperties::copyToScriptValue(QScriptEngine* engine) cons COPY_PROPERTY_TO_QSCRIPTVALUE(lineHeight); COPY_PROPERTY_TO_QSCRIPTVALUE_COLOR_GETTER(textColor, getTextColor()); COPY_PROPERTY_TO_QSCRIPTVALUE_COLOR_GETTER(backgroundColor, getBackgroundColor()); - properties.setProperty("shapeType", getShapeTypeString()); + COPY_PROPERTY_TO_QSCRIPTVALUE_GETTER(shapeType, getShapeTypeString()); // Sitting properties support QScriptValue sittingPoints = engine->newObject(); From 2e1c1229152735f3b77a8e16b7c8324cd1d54579 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 23 Feb 2015 11:38:56 -0800 Subject: [PATCH 3/5] moved ShapeType boilerplate stuff macos --- .../entities/src/EntityItemProperties.cpp | 20 ++++++------------- libraries/entities/src/EntityItemProperties.h | 5 +---- .../entities/src/EntityItemPropertiesMacros.h | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 923eef667c..8a809dee72 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -173,10 +173,6 @@ void EntityItemProperties::setLastEdited(quint64 usecTime) { const char* shapeTypeNames[] = {"none", "box", "sphere"}; -QString EntityItemProperties::getShapeTypeString() const { - return QString(shapeTypeNames[_shapeType]); -} - QHash stringToShapeTypeLookup; void buildStringToShapeTypeLookup() { @@ -185,6 +181,10 @@ void buildStringToShapeTypeLookup() { stringToShapeTypeLookup["sphere"] = SHAPE_TYPE_SPHERE; } +QString EntityItemProperties::getShapeTypeAsString() const { + return QString(shapeTypeNames[_shapeType]); +} + void EntityItemProperties::setShapeTypeFromString(const QString& shapeName) { if (stringToShapeTypeLookup.empty()) { buildStringToShapeTypeLookup(); @@ -300,7 +300,7 @@ QScriptValue EntityItemProperties::copyToScriptValue(QScriptEngine* engine) cons COPY_PROPERTY_TO_QSCRIPTVALUE(lineHeight); COPY_PROPERTY_TO_QSCRIPTVALUE_COLOR_GETTER(textColor, getTextColor()); COPY_PROPERTY_TO_QSCRIPTVALUE_COLOR_GETTER(backgroundColor, getBackgroundColor()); - COPY_PROPERTY_TO_QSCRIPTVALUE_GETTER(shapeType, getShapeTypeString()); + COPY_PROPERTY_TO_QSCRIPTVALUE_GETTER(shapeType, getShapeTypeAsString()); // Sitting properties support QScriptValue sittingPoints = engine->newObject(); @@ -378,15 +378,7 @@ void EntityItemProperties::copyFromScriptValue(const QScriptValue& object) { COPY_PROPERTY_FROM_QSCRIPTVALUE_FLOAT(lineHeight, setLineHeight); COPY_PROPERTY_FROM_QSCRIPTVALUE_COLOR(textColor, setTextColor); COPY_PROPERTY_FROM_QSCRIPTVALUE_COLOR(backgroundColor, setBackgroundColor); - - QScriptValue shapeType = object.property("shapeType"); - if (shapeType.isValid()) { - QString newValue = shapeType.toVariant().toString(); - if (_defaultSettings || newValue != getShapeTypeString()) { - setShapeTypeFromString(newValue); - } - } - + COPY_PROPERTY_FROM_QSCRITPTVALUE_ENUM(shapeType, ShapeType); _lastEdited = usecTimestampNow(); } diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index 070b80f754..2391bcde84 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -181,10 +181,7 @@ public: DEFINE_PROPERTY(PROP_LINE_HEIGHT, LineHeight, lineHeight, float); DEFINE_PROPERTY_REF(PROP_TEXT_COLOR, TextColor, textColor, xColor); DEFINE_PROPERTY_REF(PROP_BACKGROUND_COLOR, BackgroundColor, backgroundColor, xColor); - DEFINE_PROPERTY_REF(PROP_SHAPE_TYPE, ShapeType, shapeType, ShapeType); - - QString getShapeTypeString() const; - void setShapeTypeFromString(const QString& shapeName); + DEFINE_PROPERTY_REF_ENUM(PROP_SHAPE_TYPE, ShapeType, shapeType, ShapeType); public: float getMaxDimension() const { return glm::max(_dimensions.x, _dimensions.y, _dimensions.z); } diff --git a/libraries/entities/src/EntityItemPropertiesMacros.h b/libraries/entities/src/EntityItemPropertiesMacros.h index 9cf16c7a05..5e04614656 100644 --- a/libraries/entities/src/EntityItemPropertiesMacros.h +++ b/libraries/entities/src/EntityItemPropertiesMacros.h @@ -271,6 +271,15 @@ } \ } \ } + +#define COPY_PROPERTY_FROM_QSCRITPTVALUE_ENUM(P, S) \ + QScriptValue P = object.property(#P); \ + if (P.isValid()) { \ + QString newValue = P.toVariant().toString(); \ + if (_defaultSettings || newValue != get##S##AsString()) { \ + set##S##FromString(newValue); \ + } \ + } #define CONSTRUCT_PROPERTY(n, V) \ _##n(V), \ @@ -312,6 +321,17 @@ T _##n; \ bool _##n##Changed; +#define DEFINE_PROPERTY_REF_ENUM(P, N, n, T) \ + public: \ + const T& get##N() const { return _##n; } \ + void set##N(const T& value) { _##n = value; _##n##Changed = true; } \ + bool n##Changed() const { return _##n##Changed; } \ + QString get##N##AsString() const; \ + void set##N##FromString(const QString& name); \ + private: \ + T _##n; \ + bool _##n##Changed; + #define DEBUG_PROPERTY_IF_CHANGED(D, P, N, n, x) \ if (P.n##Changed()) { \ D << " " << #n << ":" << P.get##N() << x << "\n"; \ From f2fc7815232a7ebbc2dcd6241a357cf661fb64fe Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 23 Feb 2015 11:42:36 -0800 Subject: [PATCH 4/5] change shapeType to use strings in billiards.js --- examples/example/games/billiards.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/example/games/billiards.js b/examples/example/games/billiards.js index cb30cc631f..d4bc71ba37 100644 --- a/examples/example/games/billiards.js +++ b/examples/example/games/billiards.js @@ -122,7 +122,7 @@ function makeBalls(pos) { gravity: { x: 0, y: GRAVITY, z: 0 }, ignoreCollisions: false, damping: 0.50, - shapeType: 2, + shapeType: "sphere", collisionsWillMove: true })); ballPosition.z += (BALL_SIZE + BALL_GAP) * SCALE; ballNumber++; @@ -143,7 +143,7 @@ function makeBalls(pos) { velocity: {x: 0, y: 0, z: 0 }, ignoreCollisions: false, damping: 0.50, - shapeType: 2, + shapeType: "sphere", collisionsWillMove: true }); } From 3cd0a70641da47813a643b7cd29d78b54826b60f Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 23 Feb 2015 14:35:44 -0800 Subject: [PATCH 5/5] store shapeType whenever it is explicitly changed --- libraries/entities/src/EntityItemProperties.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 8a809dee72..f3f84876ba 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -190,12 +190,8 @@ void EntityItemProperties::setShapeTypeFromString(const QString& shapeName) { buildStringToShapeTypeLookup(); } auto shapeTypeItr = stringToShapeTypeLookup.find(shapeName.toLower()); - ShapeType newShapeType = SHAPE_TYPE_NONE; if (shapeTypeItr != stringToShapeTypeLookup.end()) { - newShapeType = shapeTypeItr.value(); - } - if (newShapeType != _shapeType) { - _shapeType = newShapeType; + _shapeType = shapeTypeItr.value(); _shapeTypeChanged = true; } }