Add position vector and color object script value examples

OverlayPropertyResult type is needed to successfully move QScriptValue
objects from QScriptEngine in Overlays to QScriptEngine in script.
This commit is contained in:
David Rowe 2014-11-12 14:33:52 -08:00
parent 4fa5447c85
commit 8c3bedae9d
8 changed files with 84 additions and 8 deletions

View file

@ -169,6 +169,19 @@ var clipboardPreview = Overlays.addOverlay("clipboard", {
// Demonstrate retrieving overlay properties
print("Text overlay text property value =\n" + Overlays.getProperty(text, "text"));
print("Text overlay unknown property vale =\n" + Overlays.getProperty(text, "unknown")); // value = undefined
var cubePosition = Overlays.getProperty(cube, "position");
print("Cube overlay position =\n"
+ "x: " + cubePosition.x + "\n"
+ "y: " + cubePosition.y + "\n"
+ "z: " + cubePosition.z
);
var cubeColor = Overlays.getProperty(cube, "color");
print("Cube overlay color =\n"
+ "red: " + cubeColor.red + "\n"
+ "green: " + cubeColor.green + "\n"
+ "blue: " + cubeColor.blue
);
print("Unknown overlay property =\n" + Overlays.getProperty(1000, "text")); // value = undefined
// When our script shuts down, we should clean up all of our overlays
function scriptEnding() {

View file

@ -3888,7 +3888,9 @@ void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scri
connect(scriptEngine, SIGNAL(loadScript(const QString&, bool)), this, SLOT(loadScript(const QString&, bool)));
scriptEngine->registerGlobalObject("Overlays", &_overlays);
qScriptRegisterMetaType(scriptEngine, RayToOverlayIntersectionResultToScriptValue, RayToOverlayIntersectionResultFromScriptValue);
qScriptRegisterMetaType(scriptEngine, OverlayPropertyResultToScriptValue, OverlayPropertyResultFromScriptValue);
qScriptRegisterMetaType(scriptEngine, RayToOverlayIntersectionResultToScriptValue,
RayToOverlayIntersectionResultFromScriptValue);
QScriptValue windowValue = scriptEngine->registerGlobalObject("Window", WindowScriptingInterface::getInstance());
scriptEngine->registerGetterSetter("location", LocationScriptingInterface::locationGetter,

View file

@ -118,6 +118,13 @@ void Base3DOverlay::setProperties(const QScriptValue& properties) {
}
}
QScriptValue Base3DOverlay::getProperty(const QString& property) {
if (property == "position") {
return vec3toScriptValue(_scriptEngine, _position);
}
return Overlay::getProperty(property);
}
bool Base3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face) const {
return false;

View file

@ -45,6 +45,7 @@ public:
void setIgnoreRayIntersection(bool value) { _ignoreRayIntersection = value; }
virtual void setProperties(const QScriptValue& properties);
virtual QScriptValue getProperty(const QString& property);
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) const;

View file

@ -39,8 +39,9 @@ Overlay::Overlay() :
{
}
void Overlay::init(QGLWidget* parent) {
void Overlay::init(QGLWidget* parent, QScriptEngine* scriptEngine) {
_parent = parent;
_scriptEngine = scriptEngine;
}
@ -105,6 +106,9 @@ void Overlay::setProperties(const QScriptValue& properties) {
}
QScriptValue Overlay::getProperty(const QString& property) {
if (property == "color") {
return xColorToScriptValue(_scriptEngine, _color);
}
return QScriptValue();
}

View file

@ -19,6 +19,7 @@
#include <QScriptValue>
#include <QString>
#include <RegisteredMetaTypes.h>
#include <SharedUtil.h> // for xColor
#include <RenderArgs.h>
@ -36,7 +37,7 @@ public:
Overlay();
~Overlay();
void init(QGLWidget* parent);
void init(QGLWidget* parent, QScriptEngine* scriptEngine);
virtual void update(float deltatime) {}
virtual void render(RenderArgs* args) = 0;
@ -101,6 +102,8 @@ protected:
xColor _color;
bool _visible; // should the overlay be drawn at all
Anchor _anchor;
QScriptEngine* _scriptEngine;
};

View file

@ -11,6 +11,7 @@
#include <limits>
#include <Application.h>
#include <Menu.h>
#include <QScriptValueIterator>
#include "BillboardOverlay.h"
#include "Circle3DOverlay.h"
@ -55,6 +56,7 @@ Overlays::~Overlays() {
void Overlays::init(QGLWidget* parent) {
_parent = parent;
_scriptEngine = new QScriptEngine();
}
void Overlays::update(float deltatime) {
@ -179,7 +181,7 @@ unsigned int Overlays::addOverlay(const QString& type, const QScriptValue& prope
}
unsigned int Overlays::addOverlay(Overlay* overlay) {
overlay->init(_parent);
overlay->init(_parent, _scriptEngine);
QWriteLocker lock(&_lock);
unsigned int thisID = _nextOverlayID;
@ -241,7 +243,8 @@ unsigned int Overlays::getOverlayAtPoint(const glm::vec2& point) {
return 0; // not found
}
QScriptValue Overlays::getProperty(unsigned int id, const QString& property) {
OverlayPropertyResult Overlays::getProperty(unsigned int id, const QString& property) {
OverlayPropertyResult result;
Overlay* thisOverlay = NULL;
QReadLocker lock(&_lock);
if (_overlays2D.contains(id)) {
@ -250,9 +253,39 @@ QScriptValue Overlays::getProperty(unsigned int id, const QString& property) {
thisOverlay = _overlays3D[id];
}
if (thisOverlay) {
return thisOverlay->getProperty(property);
result.value = thisOverlay->getProperty(property);
}
return QScriptValue();
return result;
}
OverlayPropertyResult::OverlayPropertyResult() :
value(QScriptValue())
{
}
QScriptValue OverlayPropertyResultToScriptValue(QScriptEngine* engine, const OverlayPropertyResult& result)
{
if (!result.value.isValid()) {
return QScriptValue::UndefinedValue;
}
QScriptValue object = engine->newObject();
if (result.value.isObject()) {
QScriptValueIterator it(result.value);
while (it.hasNext()) {
it.next();
object.setProperty(it.name(), QScriptValue(it.value().toString()));
}
} else {
object = result.value;
}
return object;
}
void OverlayPropertyResultFromScriptValue(const QScriptValue& value, OverlayPropertyResult& result)
{
result.value = value;
}
RayToOverlayIntersectionResult Overlays::findRayIntersection(const PickRay& ray) {

View file

@ -16,6 +16,17 @@
#include "Overlay.h"
class OverlayPropertyResult {
public:
OverlayPropertyResult();
QScriptValue value;
};
Q_DECLARE_METATYPE(OverlayPropertyResult);
QScriptValue OverlayPropertyResultToScriptValue(QScriptEngine* engine, const OverlayPropertyResult& value);
void OverlayPropertyResultFromScriptValue(const QScriptValue& object, OverlayPropertyResult& value);
class RayToOverlayIntersectionResult {
public:
RayToOverlayIntersectionResult();
@ -27,6 +38,7 @@ public:
QString extraInfo;
};
Q_DECLARE_METATYPE(RayToOverlayIntersectionResult);
QScriptValue RayToOverlayIntersectionResultToScriptValue(QScriptEngine* engine, const RayToOverlayIntersectionResult& value);
@ -60,7 +72,7 @@ public slots:
unsigned int getOverlayAtPoint(const glm::vec2& point);
/// returns the value of specified property, or null if there is no such property
QScriptValue getProperty(unsigned int id, const QString& property);
OverlayPropertyResult getProperty(unsigned int id, const QString& property);
/// returns details about the closest 3D Overlay hit by the pick ray
RayToOverlayIntersectionResult findRayIntersection(const PickRay& ray);
@ -76,6 +88,7 @@ private:
QGLWidget* _parent;
QReadWriteLock _lock;
QReadWriteLock _deleteLock;
QScriptEngine* _scriptEngine;
};