From 047ee0a51353d22b7660feda83cd552c0d4965e6 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 21 Oct 2014 12:20:12 -0700 Subject: [PATCH 01/26] pipe texture names through to NetworkTexture --- interface/src/renderer/GeometryCache.cpp | 4 ++++ interface/src/renderer/GeometryCache.h | 2 +- interface/src/renderer/TextureCache.h | 5 ++++- libraries/fbx/src/FBXReader.cpp | 23 ++++++++++++++++------- libraries/fbx/src/FBXReader.h | 2 +- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/interface/src/renderer/GeometryCache.cpp b/interface/src/renderer/GeometryCache.cpp index 107dac62f5..c34a5a8d7a 100644 --- a/interface/src/renderer/GeometryCache.cpp +++ b/interface/src/renderer/GeometryCache.cpp @@ -405,6 +405,7 @@ void GeometryCache::renderGrid(int xDivisions, int yDivisions) { } QSharedPointer GeometryCache::getGeometry(const QUrl& url, const QUrl& fallback, bool delayLoad) { + qDebug() << "Getting a resource at" << url; return getResource(url, fallback, delayLoad).staticCast(); } @@ -727,18 +728,21 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) { networkPart.diffuseTexture = Application::getInstance()->getTextureCache()->getTexture( _textureBase.resolved(QUrl(part.diffuseTexture.filename)), DEFAULT_TEXTURE, mesh.isEye, part.diffuseTexture.content); + networkPart.diffuseTexture->setName(part.diffuseTexture.name); networkPart.diffuseTexture->setLoadPriorities(_loadPriorities); } if (!part.normalTexture.filename.isEmpty()) { networkPart.normalTexture = Application::getInstance()->getTextureCache()->getTexture( _textureBase.resolved(QUrl(part.normalTexture.filename)), NORMAL_TEXTURE, false, part.normalTexture.content); + networkPart.normalTexture->setName(part.normalTexture.name); networkPart.normalTexture->setLoadPriorities(_loadPriorities); } if (!part.specularTexture.filename.isEmpty()) { networkPart.specularTexture = Application::getInstance()->getTextureCache()->getTexture( _textureBase.resolved(QUrl(part.specularTexture.filename)), SPECULAR_TEXTURE, false, part.specularTexture.content); + networkPart.specularTexture->setName(part.specularTexture.name); networkPart.specularTexture->setLoadPriorities(_loadPriorities); } networkMesh.parts.append(networkPart); diff --git a/interface/src/renderer/GeometryCache.h b/interface/src/renderer/GeometryCache.h index e0ada10e5b..1f45825c61 100644 --- a/interface/src/renderer/GeometryCache.h +++ b/interface/src/renderer/GeometryCache.h @@ -136,7 +136,7 @@ private: /// The state associated with a single mesh part. class NetworkMeshPart { -public: +public: QSharedPointer diffuseTexture; QSharedPointer normalTexture; diff --git a/interface/src/renderer/TextureCache.h b/interface/src/renderer/TextureCache.h index 11ce312fa3..29740621e3 100644 --- a/interface/src/renderer/TextureCache.h +++ b/interface/src/renderer/TextureCache.h @@ -145,6 +145,9 @@ public: /// Returns the lazily-computed average texture color. const QColor& getAverageColor() const { return _averageColor; } + + const QString& getName() const { return _name; } + void setName(const QString& name) { _name = name; } protected: @@ -156,7 +159,7 @@ protected: virtual void imageLoaded(const QImage& image); private: - + QString _name; TextureType _type; bool _translucent; QColor _averageColor; diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index e735e2e47b..2a51a83ab8 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -984,10 +984,13 @@ public: QVector values; }; -FBXTexture getTexture(const QString& textureID, const QHash& textureFilenames, - const QHash& textureContent) { +FBXTexture getTexture(const QString& textureID, + const QHash& textureNames, + const QHash& textureFilenames, + const QHash& textureContent) { FBXTexture texture; texture.filename = textureFilenames.value(textureID); + texture.name = textureNames.value(textureID); texture.content = textureContent.value(texture.filename); return texture; } @@ -1012,6 +1015,7 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) QHash models; QHash clusters; QHash animationCurves; + QHash textureNames; QHash textureFilenames; QHash textureContent; QHash materials; @@ -1278,6 +1282,11 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) QByteArray filename = subobject.properties.at(0).toByteArray(); filename = filename.mid(qMax(filename.lastIndexOf('\\'), filename.lastIndexOf('/')) + 1); textureFilenames.insert(getID(object.properties), filename); + } else if (subobject.name == "TextureName") { + // trim the name from the timestamp + QString name = QString(subobject.properties.at(0).toByteArray()); + name = name.left(name.indexOf('[')); + textureNames.insert(getID(object.properties), name); } } } else if (object.name == "Video") { @@ -1612,12 +1621,12 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) FBXTexture diffuseTexture; QString diffuseTextureID = diffuseTextures.value(childID); if (!diffuseTextureID.isNull()) { - diffuseTexture = getTexture(diffuseTextureID, textureFilenames, textureContent); + diffuseTexture = getTexture(diffuseTextureID, textureNames, textureFilenames, textureContent); // FBX files generated by 3DSMax have an intermediate texture parent, apparently foreach (const QString& childTextureID, childMap.values(diffuseTextureID)) { if (textureFilenames.contains(childTextureID)) { - diffuseTexture = getTexture(diffuseTextureID, textureFilenames, textureContent); + diffuseTexture = getTexture(diffuseTextureID, textureNames, textureFilenames, textureContent); } } } @@ -1625,14 +1634,14 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) FBXTexture normalTexture; QString bumpTextureID = bumpTextures.value(childID); if (!bumpTextureID.isNull()) { - normalTexture = getTexture(bumpTextureID, textureFilenames, textureContent); + normalTexture = getTexture(bumpTextureID, textureNames, textureFilenames, textureContent); generateTangents = true; } FBXTexture specularTexture; QString specularTextureID = specularTextures.value(childID); if (!specularTextureID.isNull()) { - specularTexture = getTexture(specularTextureID, textureFilenames, textureContent); + specularTexture = getTexture(specularTextureID, textureNames, textureFilenames, textureContent); } for (int j = 0; j < extracted.partMaterialTextures.size(); j++) { @@ -1658,7 +1667,7 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) materialIndex++; } else if (textureFilenames.contains(childID)) { - FBXTexture texture = getTexture(childID, textureFilenames, textureContent); + FBXTexture texture = getTexture(childID, textureNames, textureFilenames, textureContent); for (int j = 0; j < extracted.partMaterialTextures.size(); j++) { int partTexture = extracted.partMaterialTextures.at(j).second; if (partTexture == textureIndex && !(partTexture == 0 && materialsHaveTextures)) { diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index cbf0cfcca6..49b0534438 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -95,7 +95,7 @@ public: /// A texture map in an FBX document. class FBXTexture { public: - + QString name; QByteArray filename; QByteArray content; }; From cc28fe958eccc7f46d3c16892aa1cd1b9afdb95a Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 21 Oct 2014 15:44:34 -0700 Subject: [PATCH 02/26] add a hack method to NetworkGeometry to change a material texture by name --- interface/src/renderer/GeometryCache.cpp | 18 ++++++++++++++++++ interface/src/renderer/GeometryCache.h | 2 ++ interface/src/renderer/Model.h | 3 +++ 3 files changed, 23 insertions(+) diff --git a/interface/src/renderer/GeometryCache.cpp b/interface/src/renderer/GeometryCache.cpp index c34a5a8d7a..35765c50b8 100644 --- a/interface/src/renderer/GeometryCache.cpp +++ b/interface/src/renderer/GeometryCache.cpp @@ -613,6 +613,24 @@ void NetworkGeometry::clearLoadPriority(const QPointer& owner) { } } +void NetworkGeometry::setTextureWithNameToURL(const QString& name, const QUrl& url) { + for (int i = 0; i < _meshes.size(); i++) { + NetworkMesh& mesh = _meshes[i]; + for (int j = 0; j < mesh.parts.size(); j++) { + NetworkMeshPart& part = mesh.parts[j]; + + QSharedPointer matchingTexture = QSharedPointer(); + if (part.diffuseTexture->getName() == name) { + part.diffuseTexture = + Application::getInstance()->getTextureCache()->getTexture(url, DEFAULT_TEXTURE, + _geometry.meshes[i].isEye, QByteArray()); + part.diffuseTexture->setName(name); + part.diffuseTexture->setLoadPriorities(_loadPriorities); + } + } + } +} + /// Reads geometry in a worker thread. class GeometryReader : public QRunnable { public: diff --git a/interface/src/renderer/GeometryCache.h b/interface/src/renderer/GeometryCache.h index 1f45825c61..461d622a02 100644 --- a/interface/src/renderer/GeometryCache.h +++ b/interface/src/renderer/GeometryCache.h @@ -107,6 +107,8 @@ public: virtual void setLoadPriorities(const QHash, float>& priorities); virtual void clearLoadPriority(const QPointer& owner); + void setTextureWithNameToURL(const QString& name, const QUrl& url); + protected: virtual void init(); diff --git a/interface/src/renderer/Model.h b/interface/src/renderer/Model.h index 86fa0c2b7a..3618d8103e 100644 --- a/interface/src/renderer/Model.h +++ b/interface/src/renderer/Model.h @@ -183,6 +183,9 @@ public: void inverseKinematics(int jointIndex, glm::vec3 position, const glm::quat& rotation, float priority); + void setTextureWithNameToURL(const QString& name, const QUrl& url) + { _geometry->setTextureWithNameToURL(name, url); } + protected: QSharedPointer _geometry; From fc39e827347694dc6b97f6a2b87e9994287603a6 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 21 Oct 2014 16:11:58 -0700 Subject: [PATCH 03/26] allow a texture change in ModelOverlay properties --- interface/src/ui/overlays/ModelOverlay.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/interface/src/ui/overlays/ModelOverlay.cpp b/interface/src/ui/overlays/ModelOverlay.cpp index 12f54f02d9..ac3d7b8a2b 100644 --- a/interface/src/ui/overlays/ModelOverlay.cpp +++ b/interface/src/ui/overlays/ModelOverlay.cpp @@ -102,6 +102,16 @@ void ModelOverlay::setProperties(const QScriptValue &properties) { } _updateModel = true; } + + QScriptValue texturesValue = properties.property("textures"); + if (texturesValue.isValid()) { + QVariantMap textureMap = texturesValue.toVariant().toMap(); + foreach(const QString& key, textureMap.keys()) { + QUrl newTextureURL = textureMap[key].toUrl(); + qDebug() << "Updating texture named" << key << "to texture at URL" << newTextureURL; + _model.setTextureWithNameToURL(key, newTextureURL); + } + } if (properties.property("position").isValid()) { _updateModel = true; From e97aef581a462dc4126372c000706a4b3bbe61fc Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 22 Oct 2014 12:07:00 -0700 Subject: [PATCH 04/26] use InvokeMethod so that texture change happens on the right thread --- interface/src/renderer/Model.h | 2 +- interface/src/ui/overlays/ModelOverlay.cpp | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/interface/src/renderer/Model.h b/interface/src/renderer/Model.h index 3618d8103e..272844ac4a 100644 --- a/interface/src/renderer/Model.h +++ b/interface/src/renderer/Model.h @@ -183,7 +183,7 @@ public: void inverseKinematics(int jointIndex, glm::vec3 position, const glm::quat& rotation, float priority); - void setTextureWithNameToURL(const QString& name, const QUrl& url) + Q_INVOKABLE void setTextureWithNameToURL(const QString& name, const QUrl& url) { _geometry->setTextureWithNameToURL(name, url); } protected: diff --git a/interface/src/ui/overlays/ModelOverlay.cpp b/interface/src/ui/overlays/ModelOverlay.cpp index ac3d7b8a2b..0169bf40cd 100644 --- a/interface/src/ui/overlays/ModelOverlay.cpp +++ b/interface/src/ui/overlays/ModelOverlay.cpp @@ -107,9 +107,13 @@ void ModelOverlay::setProperties(const QScriptValue &properties) { if (texturesValue.isValid()) { QVariantMap textureMap = texturesValue.toVariant().toMap(); foreach(const QString& key, textureMap.keys()) { + QUrl newTextureURL = textureMap[key].toUrl(); qDebug() << "Updating texture named" << key << "to texture at URL" << newTextureURL; - _model.setTextureWithNameToURL(key, newTextureURL); + + QMetaObject::invokeMethod(&_model, "setTextureWithNameToURL", Qt::AutoConnection, + Q_ARG(const QString&, key), + Q_ARG(const QUrl&, newTextureURL)); } } From b7ebc9e902a238a9ac810f94ec6ee63e822ceb6b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 10:51:48 -0700 Subject: [PATCH 05/26] add a class for custom HFActionEvent --- interface/src/HFActionEvent.cpp | 26 ++++++++++++++++++++++++++ interface/src/HFActionEvent.h | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 interface/src/HFActionEvent.cpp create mode 100644 interface/src/HFActionEvent.h diff --git a/interface/src/HFActionEvent.cpp b/interface/src/HFActionEvent.cpp new file mode 100644 index 0000000000..dfa08a94c5 --- /dev/null +++ b/interface/src/HFActionEvent.cpp @@ -0,0 +1,26 @@ +// +// HFActionEvent.cpp +// interface/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "HFActionEvent.h" + +HFActionEvent::HFActionEvent() : + QEvent(HFActionEvent::type()) +{ + +} + +QEvent::Type HFActionEvent::type() { + static QEvent::Type hfActionType = QEvent::None; + if (hfActionType == QEvent::None) { + hfActionType = static_cast(QEvent::registerEventType()); + } + return hfActionType; +} \ No newline at end of file diff --git a/interface/src/HFActionEvent.h b/interface/src/HFActionEvent.h new file mode 100644 index 0000000000..68bef6ca50 --- /dev/null +++ b/interface/src/HFActionEvent.h @@ -0,0 +1,24 @@ +// +// HFActionEvent.h +// interface/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_HFActionEvent_h +#define hifi_HFActionEvent_h + +#include + +class HFActionEvent : public QEvent { +public: + HFActionEvent(); + + static QEvent::Type type(); +}; + +#endif // hifi_HFActionEvent_h \ No newline at end of file From d09c9233d8b4d859992baa07c16d9e376e860fea Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 10:55:16 -0700 Subject: [PATCH 06/26] add position to HFActionEvent --- interface/src/HFActionEvent.cpp | 5 +++-- interface/src/HFActionEvent.h | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/interface/src/HFActionEvent.cpp b/interface/src/HFActionEvent.cpp index dfa08a94c5..6f98e2963f 100644 --- a/interface/src/HFActionEvent.cpp +++ b/interface/src/HFActionEvent.cpp @@ -11,8 +11,9 @@ #include "HFActionEvent.h" -HFActionEvent::HFActionEvent() : - QEvent(HFActionEvent::type()) +HFActionEvent::HFActionEvent(const QPointF& localPosition) : + QEvent(HFActionEvent::type()), + _localPosition(localPosition) { } diff --git a/interface/src/HFActionEvent.h b/interface/src/HFActionEvent.h index 68bef6ca50..19b6dc8469 100644 --- a/interface/src/HFActionEvent.h +++ b/interface/src/HFActionEvent.h @@ -16,9 +16,11 @@ class HFActionEvent : public QEvent { public: - HFActionEvent(); + HFActionEvent(const QPointF& localPosition); static QEvent::Type type(); +private: + QPointF _localPosition; }; #endif // hifi_HFActionEvent_h \ No newline at end of file From 55735a3f0dab52c233c4c5a9eb9d9ac08e96613a Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 11:11:23 -0700 Subject: [PATCH 07/26] add start and end to HFActionEvent --- interface/src/HFActionEvent.cpp | 21 ++++++++++++--------- interface/src/HFActionEvent.h | 5 +++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/interface/src/HFActionEvent.cpp b/interface/src/HFActionEvent.cpp index 6f98e2963f..2ab2001ba4 100644 --- a/interface/src/HFActionEvent.cpp +++ b/interface/src/HFActionEvent.cpp @@ -11,17 +11,20 @@ #include "HFActionEvent.h" -HFActionEvent::HFActionEvent(const QPointF& localPosition) : - QEvent(HFActionEvent::type()), +HFActionEvent::HFActionEvent(QEvent::Type type, const QPointF& localPosition) : + QEvent(type), _localPosition(localPosition) { } -QEvent::Type HFActionEvent::type() { - static QEvent::Type hfActionType = QEvent::None; - if (hfActionType == QEvent::None) { - hfActionType = static_cast(QEvent::registerEventType()); - } - return hfActionType; -} \ No newline at end of file +QEvent::Type HFActionEvent::startType() { + static QEvent::Type startType = static_cast(QEvent::registerEventType()); + return startType; +} + +QEvent::Type HFActionEvent::endType() { + static QEvent::Type endType = static_cast(QEvent::registerEventType()); + return endType; +} + diff --git a/interface/src/HFActionEvent.h b/interface/src/HFActionEvent.h index 19b6dc8469..2c7d323dc9 100644 --- a/interface/src/HFActionEvent.h +++ b/interface/src/HFActionEvent.h @@ -16,9 +16,10 @@ class HFActionEvent : public QEvent { public: - HFActionEvent(const QPointF& localPosition); + HFActionEvent(QEvent::Type type, const QPointF& localPosition); - static QEvent::Type type(); + static QEvent::Type startType(); + static QEvent::Type endType(); private: QPointF _localPosition; }; From 6626787ae59bd9082842d96769a7a43915a8e340 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 11:40:33 -0700 Subject: [PATCH 08/26] have mouse press/release fire HFActionEvent --- interface/src/Application.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index da7693ed7a..22371ce7d0 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -67,14 +67,17 @@ #include #include "Application.h" -#include "ui/DataWebDialog.h" +#include "HFActionEvent.h" #include "InterfaceVersion.h" #include "Menu.h" #include "ModelUploader.h" #include "Util.h" + +#include "devices/Leapmotion.h" #include "devices/MIDIManager.h" #include "devices/OculusManager.h" #include "devices/TV3DManager.h" + #include "renderer/ProgramObject.h" #include "scripting/AccountScriptingInterface.h" @@ -87,12 +90,12 @@ #include "scripting/SettingsScriptingInterface.h" #include "scripting/WindowScriptingInterface.h" +#include "ui/DataWebDialog.h" #include "ui/InfoView.h" #include "ui/Snapshot.h" #include "ui/Stats.h" #include "ui/TextRenderer.h" -#include "devices/Leapmotion.h" using namespace std; @@ -1239,6 +1242,10 @@ void Application::mousePressEvent(QMouseEvent* event, unsigned int deviceID) { // stop propagation return; } + + // nobody handled this - make it an action event on the _window object + HFActionEvent actionEvent(HFActionEvent::startType(), event->localPos()); + sendEvent(_window, &actionEvent); } else if (event->button() == Qt::RightButton) { // right click items here @@ -1259,12 +1266,17 @@ void Application::mouseReleaseEvent(QMouseEvent* event, unsigned int deviceID) { _mouseX = event->x(); _mouseY = event->y(); _mousePressed = false; + checkBandwidthMeterClick(); if (Menu::getInstance()->isOptionChecked(MenuOption::Stats)) { // let's set horizontal offset to give stats some margin to mirror int horizontalOffset = MIRROR_VIEW_WIDTH; Stats::getInstance()->checkClick(_mouseX, _mouseY, _mouseDragStartedX, _mouseDragStartedY, horizontalOffset); } + + // fire an action end event + HFActionEvent actionEvent(HFActionEvent::endType(), event->localPos()); + sendEvent(_window, &actionEvent); } } } From 0ddf665b86d4e6f6481817b7dd4a4bd83e218e5b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 12:56:48 -0700 Subject: [PATCH 09/26] add an HFMetaEvent class as super class for meta events --- interface/CMakeLists.txt | 2 +- interface/src/Application.cpp | 13 +++++----- interface/src/{ => events}/HFActionEvent.cpp | 8 +++--- interface/src/{ => events}/HFActionEvent.h | 6 ++--- interface/src/events/HFMetaEvent.cpp | 20 +++++++++++++++ interface/src/events/HFMetaEvent.h | 27 ++++++++++++++++++++ 6 files changed, 62 insertions(+), 14 deletions(-) rename interface/src/{ => events}/HFActionEvent.cpp (71%) rename interface/src/{ => events}/HFActionEvent.h (85%) create mode 100644 interface/src/events/HFMetaEvent.cpp create mode 100644 interface/src/events/HFMetaEvent.h diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 9c4eeff65b..0867f15ac8 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -45,7 +45,7 @@ configure_file(InterfaceVersion.h.in "${PROJECT_BINARY_DIR}/includes/InterfaceVe # grab the implementation and header files from src dirs file(GLOB INTERFACE_SRCS src/*.cpp src/*.h) -foreach(SUBDIR avatar devices renderer ui starfield location scripting voxels particles entities gpu) +foreach(SUBDIR avatar devices events renderer ui starfield location scripting voxels particles entities gpu) file(GLOB_RECURSE SUBDIR_SRCS src/${SUBDIR}/*.cpp src/${SUBDIR}/*.h) set(INTERFACE_SRCS ${INTERFACE_SRCS} "${SUBDIR_SRCS}") endforeach(SUBDIR) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 22371ce7d0..91ff7c3563 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -67,7 +67,6 @@ #include #include "Application.h" -#include "HFActionEvent.h" #include "InterfaceVersion.h" #include "Menu.h" #include "ModelUploader.h" @@ -78,6 +77,8 @@ #include "devices/OculusManager.h" #include "devices/TV3DManager.h" +#include "events/HFActionEvent.h" + #include "renderer/ProgramObject.h" #include "scripting/AccountScriptingInterface.h" @@ -139,6 +140,8 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : _glWidget(new GLCanvas()), _nodeThread(new QThread(this)), _datagramProcessor(), + _undoStack(), + _undoStackScriptingInterface(&_undoStack), _frameCount(0), _fps(60.0f), _justStarted(true), @@ -175,8 +178,6 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : _nodeBoundsDisplay(this), _previousScriptLocation(), _applicationOverlay(), - _undoStack(), - _undoStackScriptingInterface(&_undoStack), _runningScriptsWidget(NULL), _runningScriptsWidgetWasVisible(false), _trayIcon(new QSystemTrayIcon(_window)), @@ -835,7 +836,7 @@ bool Application::event(QEvent* event) { return false; } - + return QApplication::event(event); } @@ -1245,7 +1246,7 @@ void Application::mousePressEvent(QMouseEvent* event, unsigned int deviceID) { // nobody handled this - make it an action event on the _window object HFActionEvent actionEvent(HFActionEvent::startType(), event->localPos()); - sendEvent(_window, &actionEvent); + sendEvent(this, &actionEvent); } else if (event->button() == Qt::RightButton) { // right click items here @@ -1276,7 +1277,7 @@ void Application::mouseReleaseEvent(QMouseEvent* event, unsigned int deviceID) { // fire an action end event HFActionEvent actionEvent(HFActionEvent::endType(), event->localPos()); - sendEvent(_window, &actionEvent); + sendEvent(this, &actionEvent); } } } diff --git a/interface/src/HFActionEvent.cpp b/interface/src/events/HFActionEvent.cpp similarity index 71% rename from interface/src/HFActionEvent.cpp rename to interface/src/events/HFActionEvent.cpp index 2ab2001ba4..e393a2f332 100644 --- a/interface/src/HFActionEvent.cpp +++ b/interface/src/events/HFActionEvent.cpp @@ -1,6 +1,6 @@ // // HFActionEvent.cpp -// interface/src +// interface/src/events // // Created by Stephen Birarda on 2014-10-27. // Copyright 2014 High Fidelity, Inc. @@ -12,19 +12,19 @@ #include "HFActionEvent.h" HFActionEvent::HFActionEvent(QEvent::Type type, const QPointF& localPosition) : - QEvent(type), + HFMetaEvent(type), _localPosition(localPosition) { } QEvent::Type HFActionEvent::startType() { - static QEvent::Type startType = static_cast(QEvent::registerEventType()); + static QEvent::Type startType = HFMetaEvent::newEventType(); return startType; } QEvent::Type HFActionEvent::endType() { - static QEvent::Type endType = static_cast(QEvent::registerEventType()); + static QEvent::Type endType = HFMetaEvent::newEventType(); return endType; } diff --git a/interface/src/HFActionEvent.h b/interface/src/events/HFActionEvent.h similarity index 85% rename from interface/src/HFActionEvent.h rename to interface/src/events/HFActionEvent.h index 2c7d323dc9..3371312755 100644 --- a/interface/src/HFActionEvent.h +++ b/interface/src/events/HFActionEvent.h @@ -1,6 +1,6 @@ // // HFActionEvent.h -// interface/src +// interface/src/events // // Created by Stephen Birarda on 2014-10-27. // Copyright 2014 High Fidelity, Inc. @@ -12,9 +12,9 @@ #ifndef hifi_HFActionEvent_h #define hifi_HFActionEvent_h -#include +#include "HFMetaEvent.h" -class HFActionEvent : public QEvent { +class HFActionEvent : public HFMetaEvent { public: HFActionEvent(QEvent::Type type, const QPointF& localPosition); diff --git a/interface/src/events/HFMetaEvent.cpp b/interface/src/events/HFMetaEvent.cpp new file mode 100644 index 0000000000..c9d77868cd --- /dev/null +++ b/interface/src/events/HFMetaEvent.cpp @@ -0,0 +1,20 @@ +// +// HFMetaEvent.cpp +// interface/src/events +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "HFMetaEvent.h" + +QSet HFMetaEvent::_types = QSet(); + +QEvent::Type HFMetaEvent::newEventType() { + QEvent::Type newType = static_cast(QEvent::registerEventType()); + _types.insert(newType); + return newType; +} \ No newline at end of file diff --git a/interface/src/events/HFMetaEvent.h b/interface/src/events/HFMetaEvent.h new file mode 100644 index 0000000000..8fbdfab1fa --- /dev/null +++ b/interface/src/events/HFMetaEvent.h @@ -0,0 +1,27 @@ +// +// HFMetaEvent.h +// interface/src/events +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_HFMetaEvent_h +#define hifi_HFMetaEvent_h + +#include + +class HFMetaEvent : public QEvent { +public: + HFMetaEvent(QEvent::Type type) : QEvent(type) {}; + static const QSet& types() { return HFMetaEvent::_types; } +protected: + static QEvent::Type newEventType(); + + static QSet _types; +}; + +#endif // hifi_HFMetaEvent_h \ No newline at end of file From d38dd2c53a368f14d5dcc5b50d208d7f97359753 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 14:18:23 -0700 Subject: [PATCH 10/26] repairs for recursive source files, move HF meta events to SE --- cmake/macros/SetupHifiLibrary.cmake | 2 +- interface/CMakeLists.txt | 6 +----- interface/src/Application.cpp | 3 +-- interface/src/scripting/ControllerScriptingInterface.h | 2 +- .../src/AbstractControllerScriptingInterface.h | 3 +++ .../script-engine/src}/HFActionEvent.cpp | 0 .../events => libraries/script-engine/src}/HFActionEvent.h | 0 .../events => libraries/script-engine/src}/HFMetaEvent.cpp | 0 .../events => libraries/script-engine/src}/HFMetaEvent.h | 0 libraries/script-engine/src/ScriptEngine.cpp | 3 ++- 10 files changed, 9 insertions(+), 10 deletions(-) rename {interface/src/events => libraries/script-engine/src}/HFActionEvent.cpp (100%) rename {interface/src/events => libraries/script-engine/src}/HFActionEvent.h (100%) rename {interface/src/events => libraries/script-engine/src}/HFMetaEvent.cpp (100%) rename {interface/src/events => libraries/script-engine/src}/HFMetaEvent.h (100%) diff --git a/cmake/macros/SetupHifiLibrary.cmake b/cmake/macros/SetupHifiLibrary.cmake index 7bb85f68f5..950286ce45 100644 --- a/cmake/macros/SetupHifiLibrary.cmake +++ b/cmake/macros/SetupHifiLibrary.cmake @@ -12,7 +12,7 @@ macro(SETUP_HIFI_LIBRARY) project(${TARGET_NAME}) # grab the implemenation and header files - file(GLOB LIB_SRCS src/*.h src/*.cpp) + file(GLOB_RECURSE LIB_SRCS "src/*.h" "src/*.cpp") set(LIB_SRCS ${LIB_SRCS}) # create a library and set the property so it can be referenced later diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 0867f15ac8..c75d210103 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -44,11 +44,7 @@ configure_file(InterfaceConfig.h.in "${PROJECT_BINARY_DIR}/includes/InterfaceCon configure_file(InterfaceVersion.h.in "${PROJECT_BINARY_DIR}/includes/InterfaceVersion.h") # grab the implementation and header files from src dirs -file(GLOB INTERFACE_SRCS src/*.cpp src/*.h) -foreach(SUBDIR avatar devices events renderer ui starfield location scripting voxels particles entities gpu) - file(GLOB_RECURSE SUBDIR_SRCS src/${SUBDIR}/*.cpp src/${SUBDIR}/*.h) - set(INTERFACE_SRCS ${INTERFACE_SRCS} "${SUBDIR_SRCS}") -endforeach(SUBDIR) +file(GLOB_RECURSE INTERFACE_SRCS "src/*.cpp" "src/*.h") # Add SpeechRecognizer if on OS X, otherwise remove if (APPLE) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 91ff7c3563..81d8098002 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include #include @@ -77,8 +78,6 @@ #include "devices/OculusManager.h" #include "devices/TV3DManager.h" -#include "events/HFActionEvent.h" - #include "renderer/ProgramObject.h" #include "scripting/AccountScriptingInterface.h" diff --git a/interface/src/scripting/ControllerScriptingInterface.h b/interface/src/scripting/ControllerScriptingInterface.h index 62ef2e9b24..916c87d85c 100644 --- a/interface/src/scripting/ControllerScriptingInterface.h +++ b/interface/src/scripting/ControllerScriptingInterface.h @@ -46,7 +46,7 @@ private: signals: }; - + /// handles scripting of input controller commands from JS class ControllerScriptingInterface : public AbstractControllerScriptingInterface { diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index 78e36a3740..1e9647c5d5 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -88,6 +88,9 @@ public slots: signals: void keyPressEvent(const KeyEvent& event); void keyReleaseEvent(const KeyEvent& event); +// +// void actionStartEvent(const HFActionEvent& event); +// void actionEndEvent(const HFActionEvent& event); void mouseMoveEvent(const MouseEvent& event, unsigned int deviceID = 0); void mousePressEvent(const MouseEvent& event, unsigned int deviceID = 0); diff --git a/interface/src/events/HFActionEvent.cpp b/libraries/script-engine/src/HFActionEvent.cpp similarity index 100% rename from interface/src/events/HFActionEvent.cpp rename to libraries/script-engine/src/HFActionEvent.cpp diff --git a/interface/src/events/HFActionEvent.h b/libraries/script-engine/src/HFActionEvent.h similarity index 100% rename from interface/src/events/HFActionEvent.h rename to libraries/script-engine/src/HFActionEvent.h diff --git a/interface/src/events/HFMetaEvent.cpp b/libraries/script-engine/src/HFMetaEvent.cpp similarity index 100% rename from interface/src/events/HFMetaEvent.cpp rename to libraries/script-engine/src/HFMetaEvent.cpp diff --git a/interface/src/events/HFMetaEvent.h b/libraries/script-engine/src/HFMetaEvent.h similarity index 100% rename from interface/src/events/HFMetaEvent.h rename to libraries/script-engine/src/HFMetaEvent.h diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 660a6ec560..3ccd0a74c3 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -36,12 +36,13 @@ #include "ArrayBufferViewClass.h" #include "DataViewClass.h" #include "MenuItemProperties.h" -#include "MIDIEvent.h" #include "LocalVoxels.h" #include "ScriptEngine.h" #include "TypedArrays.h" #include "XMLHttpRequestClass.h" +#include "MIDIEvent.h" + VoxelsScriptingInterface ScriptEngine::_voxelsScriptingInterface; EntityScriptingInterface ScriptEngine::_entityScriptingInterface; From 5a390da509e1b19e74a42c826d518aa1f65731b5 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 14:24:32 -0700 Subject: [PATCH 11/26] move KeyEvent from EventTypes to its own file --- .../AbstractControllerScriptingInterface.h | 1 + libraries/script-engine/src/EventTypes.cpp | 277 +---------------- libraries/script-engine/src/EventTypes.h | 24 -- libraries/script-engine/src/KeyEvent.cpp | 290 ++++++++++++++++++ libraries/script-engine/src/KeyEvent.h | 41 +++ .../script-engine/src/MenuItemProperties.cpp | 2 +- .../script-engine/src/MenuItemProperties.h | 2 +- 7 files changed, 338 insertions(+), 299 deletions(-) create mode 100644 libraries/script-engine/src/KeyEvent.cpp create mode 100644 libraries/script-engine/src/KeyEvent.h diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index 1e9647c5d5..9f583b94d8 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -18,6 +18,7 @@ #include #include "EventTypes.h" +#include "KeyEvent.h" class AbstractInputController : public QObject { Q_OBJECT diff --git a/libraries/script-engine/src/EventTypes.cpp b/libraries/script-engine/src/EventTypes.cpp index de3ec231ae..49c82796a2 100644 --- a/libraries/script-engine/src/EventTypes.cpp +++ b/libraries/script-engine/src/EventTypes.cpp @@ -11,291 +11,22 @@ #include #include + +#include "KeyEvent.h" + #include "EventTypes.h" void registerEventTypes(QScriptEngine* engine) { - qScriptRegisterMetaType(engine, keyEventToScriptValue, keyEventFromScriptValue); + qScriptRegisterMetaType(engine, KeyEvent::toScriptValue, KeyEvent::fromScriptValue); qScriptRegisterMetaType(engine, mouseEventToScriptValue, mouseEventFromScriptValue); qScriptRegisterMetaType(engine, touchEventToScriptValue, touchEventFromScriptValue); qScriptRegisterMetaType(engine, wheelEventToScriptValue, wheelEventFromScriptValue); qScriptRegisterMetaType(engine, spatialEventToScriptValue, spatialEventFromScriptValue); } -KeyEvent::KeyEvent() : - key(0), - text(""), - isShifted(false), - isControl(false), - isMeta(false), - isAlt(false), - isKeypad(false), - isValid(false), - isAutoRepeat(false) -{ -}; -KeyEvent::KeyEvent(const QKeyEvent& event) { - key = event.key(); - text = event.text(); - isShifted = event.modifiers().testFlag(Qt::ShiftModifier); - 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; - isAutoRepeat = event.isAutoRepeat(); - - // 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_Space) { - text = "SPACE"; - } 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"; - } else if (key == Qt::Key_Shift) { - text = "SHIFT"; - } else if (key == Qt::Key_Alt) { - text = "ALT"; - } else if (key == Qt::Key_Control) { - text = "CONTROL"; - } else if (key == Qt::Key_Meta) { - text = "META"; - } else if (key == Qt::Key_PageDown) { - text = "PAGE DOWN"; - } else if (key == Qt::Key_PageUp) { - text = "PAGE UP"; - } else if (key == Qt::Key_Home) { - text = "HOME"; - } else if (key == Qt::Key_End) { - text = "END"; - } else if (key == Qt::Key_Help) { - text = "HELP"; - } else if (key == Qt::Key_CapsLock) { - text = "CAPS LOCK"; - } else if (key >= Qt::Key_A && key <= Qt::Key_Z && (isMeta || isControl || isAlt)) { - // this little bit of hackery will fix the text character keys like a-z in cases of control/alt/meta where - // qt doesn't always give you the key characters and will sometimes give you crazy non-printable characters - const int lowerCaseAdjust = 0x20; - QString unicode; - if (isShifted) { - text = QString(QChar(key)); - } else { - text = QString(QChar(key + lowerCaseAdjust)); - } - } -} - -bool KeyEvent::operator==(const KeyEvent& other) const { - return other.key == key - && other.isShifted == isShifted - && other.isControl == isControl - && other.isMeta == isMeta - && other.isAlt == isAlt - && other.isKeypad == isKeypad - && other.isAutoRepeat == isAutoRepeat; -} - - -KeyEvent::operator QKeySequence() const { - int resultCode = 0; - if (text.size() == 1 && text >= "a" && text <= "z") { - resultCode = text.toUpper().at(0).unicode(); - } else { - resultCode = key; - } - - if (isMeta) { - resultCode |= Qt::META; - } - if (isAlt) { - resultCode |= Qt::ALT; - } - if (isControl) { - resultCode |= Qt::CTRL; - } - if (isShifted) { - resultCode |= Qt::SHIFT; - } - return QKeySequence(resultCode); -} - -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); - obj.setProperty("isAutoRepeat", event.isAutoRepeat); - return obj; -} - -void keyEventFromScriptValue(const QScriptValue& object, KeyEvent& event) { - - event.isValid = false; // assume the worst - event.isMeta = object.property("isMeta").toVariant().toBool(); - event.isControl = object.property("isControl").toVariant().toBool(); - event.isAlt = object.property("isAlt").toVariant().toBool(); - event.isKeypad = object.property("isKeypad").toVariant().toBool(); - event.isAutoRepeat = object.property("isAutoRepeat").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() == "SPACE") { - event.key = Qt::Key_Space; - } 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 if (event.text.toUpper() == "SHIFT") { - event.key = Qt::Key_Shift; - } else if (event.text.toUpper() == "ALT") { - event.key = Qt::Key_Alt; - } else if (event.text.toUpper() == "CONTROL") { - event.key = Qt::Key_Control; - } else if (event.text.toUpper() == "META") { - event.key = Qt::Key_Meta; - } else if (event.text.toUpper() == "PAGE DOWN") { - event.key = Qt::Key_PageDown; - } else if (event.text.toUpper() == "PAGE UP") { - event.key = Qt::Key_PageUp; - } else if (event.text.toUpper() == "HOME") { - event.key = Qt::Key_Home; - } else if (event.text.toUpper() == "END") { - event.key = Qt::Key_End; - } else if (event.text.toUpper() == "HELP") { - event.key = Qt::Key_Help; - } else if (event.text.toUpper() == "CAPS LOCK") { - event.key = Qt::Key_CapsLock; - } else { - // Key values do not distinguish between uppercase and lowercase - // and use the uppercase key value. - event.key = event.text.toUpper().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 - << " event.isAutoRepeat=" << event.isAutoRepeat; - } -} - MouseEvent::MouseEvent() : x(0.0f), y(0.0f), diff --git a/libraries/script-engine/src/EventTypes.h b/libraries/script-engine/src/EventTypes.h index 959292039b..121c321cf6 100644 --- a/libraries/script-engine/src/EventTypes.h +++ b/libraries/script-engine/src/EventTypes.h @@ -17,31 +17,11 @@ #include -#include #include #include #include -class KeyEvent { -public: - KeyEvent(); - KeyEvent(const QKeyEvent& event); - bool operator==(const KeyEvent& other) const; - operator QKeySequence() const; - - int key; - QString text; - bool isShifted; - bool isControl; - bool isMeta; - bool isAlt; - bool isKeypad; - bool isValid; - bool isAutoRepeat; -}; - - class MouseEvent { public: MouseEvent(); @@ -123,8 +103,6 @@ public: private: }; - -Q_DECLARE_METATYPE(KeyEvent) Q_DECLARE_METATYPE(MouseEvent) Q_DECLARE_METATYPE(TouchEvent) Q_DECLARE_METATYPE(WheelEvent) @@ -132,8 +110,6 @@ Q_DECLARE_METATYPE(SpatialEvent) void registerEventTypes(QScriptEngine* engine); -QScriptValue keyEventToScriptValue(QScriptEngine* engine, const KeyEvent& event); -void keyEventFromScriptValue(const QScriptValue& object, KeyEvent& event); QScriptValue mouseEventToScriptValue(QScriptEngine* engine, const MouseEvent& event); void mouseEventFromScriptValue(const QScriptValue& object, MouseEvent& event); diff --git a/libraries/script-engine/src/KeyEvent.cpp b/libraries/script-engine/src/KeyEvent.cpp new file mode 100644 index 0000000000..b7564db1b6 --- /dev/null +++ b/libraries/script-engine/src/KeyEvent.cpp @@ -0,0 +1,290 @@ +// +// KeyEvent.cpp +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include + +#include "KeyEvent.h" + +KeyEvent::KeyEvent() : + key(0), + text(""), + isShifted(false), + isControl(false), + isMeta(false), + isAlt(false), + isKeypad(false), + isValid(false), + isAutoRepeat(false) +{ + +} + +KeyEvent::KeyEvent(const QKeyEvent& event) { + key = event.key(); + text = event.text(); + isShifted = event.modifiers().testFlag(Qt::ShiftModifier); + 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; + isAutoRepeat = event.isAutoRepeat(); + + // 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_Space) { + text = "SPACE"; + } 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"; + } else if (key == Qt::Key_Shift) { + text = "SHIFT"; + } else if (key == Qt::Key_Alt) { + text = "ALT"; + } else if (key == Qt::Key_Control) { + text = "CONTROL"; + } else if (key == Qt::Key_Meta) { + text = "META"; + } else if (key == Qt::Key_PageDown) { + text = "PAGE DOWN"; + } else if (key == Qt::Key_PageUp) { + text = "PAGE UP"; + } else if (key == Qt::Key_Home) { + text = "HOME"; + } else if (key == Qt::Key_End) { + text = "END"; + } else if (key == Qt::Key_Help) { + text = "HELP"; + } else if (key == Qt::Key_CapsLock) { + text = "CAPS LOCK"; + } else if (key >= Qt::Key_A && key <= Qt::Key_Z && (isMeta || isControl || isAlt)) { + // this little bit of hackery will fix the text character keys like a-z in cases of control/alt/meta where + // qt doesn't always give you the key characters and will sometimes give you crazy non-printable characters + const int lowerCaseAdjust = 0x20; + QString unicode; + if (isShifted) { + text = QString(QChar(key)); + } else { + text = QString(QChar(key + lowerCaseAdjust)); + } + } +} + +bool KeyEvent::operator==(const KeyEvent& other) const { + return other.key == key + && other.isShifted == isShifted + && other.isControl == isControl + && other.isMeta == isMeta + && other.isAlt == isAlt + && other.isKeypad == isKeypad + && other.isAutoRepeat == isAutoRepeat; +} + + +KeyEvent::operator QKeySequence() const { + int resultCode = 0; + if (text.size() == 1 && text >= "a" && text <= "z") { + resultCode = text.toUpper().at(0).unicode(); + } else { + resultCode = key; + } + + if (isMeta) { + resultCode |= Qt::META; + } + if (isAlt) { + resultCode |= Qt::ALT; + } + if (isControl) { + resultCode |= Qt::CTRL; + } + if (isShifted) { + resultCode |= Qt::SHIFT; + } + return QKeySequence(resultCode); +} + +QScriptValue KeyEvent::toScriptValue(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); + obj.setProperty("isAutoRepeat", event.isAutoRepeat); + return obj; +} + +void KeyEvent::fromScriptValue(const QScriptValue& object, KeyEvent& event) { + + event.isValid = false; // assume the worst + event.isMeta = object.property("isMeta").toVariant().toBool(); + event.isControl = object.property("isControl").toVariant().toBool(); + event.isAlt = object.property("isAlt").toVariant().toBool(); + event.isKeypad = object.property("isKeypad").toVariant().toBool(); + event.isAutoRepeat = object.property("isAutoRepeat").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() == "SPACE") { + event.key = Qt::Key_Space; + } 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 if (event.text.toUpper() == "SHIFT") { + event.key = Qt::Key_Shift; + } else if (event.text.toUpper() == "ALT") { + event.key = Qt::Key_Alt; + } else if (event.text.toUpper() == "CONTROL") { + event.key = Qt::Key_Control; + } else if (event.text.toUpper() == "META") { + event.key = Qt::Key_Meta; + } else if (event.text.toUpper() == "PAGE DOWN") { + event.key = Qt::Key_PageDown; + } else if (event.text.toUpper() == "PAGE UP") { + event.key = Qt::Key_PageUp; + } else if (event.text.toUpper() == "HOME") { + event.key = Qt::Key_Home; + } else if (event.text.toUpper() == "END") { + event.key = Qt::Key_End; + } else if (event.text.toUpper() == "HELP") { + event.key = Qt::Key_Help; + } else if (event.text.toUpper() == "CAPS LOCK") { + event.key = Qt::Key_CapsLock; + } else { + // Key values do not distinguish between uppercase and lowercase + // and use the uppercase key value. + event.key = event.text.toUpper().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 + << " event.isAutoRepeat=" << event.isAutoRepeat; + } +} + diff --git a/libraries/script-engine/src/KeyEvent.h b/libraries/script-engine/src/KeyEvent.h new file mode 100644 index 0000000000..bdadcec374 --- /dev/null +++ b/libraries/script-engine/src/KeyEvent.h @@ -0,0 +1,41 @@ +// +// KeyEvent.h +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_KeyEvent_h +#define hifi_KeyEvent_h + +#include +#include + +class KeyEvent { +public: + KeyEvent(); + KeyEvent(const QKeyEvent& event); + bool operator==(const KeyEvent& other) const; + operator QKeySequence() const; + + static QScriptValue toScriptValue(QScriptEngine* engine, const KeyEvent& event); + static void fromScriptValue(const QScriptValue& object, KeyEvent& event); + + int key; + QString text; + bool isShifted; + bool isControl; + bool isMeta; + bool isAlt; + bool isKeypad; + bool isValid; + bool isAutoRepeat; +}; + +Q_DECLARE_METATYPE(KeyEvent) + +#endif // hifi_KeyEvent_h \ No newline at end of file diff --git a/libraries/script-engine/src/MenuItemProperties.cpp b/libraries/script-engine/src/MenuItemProperties.cpp index c1f3e92447..97fbdef1fa 100644 --- a/libraries/script-engine/src/MenuItemProperties.cpp +++ b/libraries/script-engine/src/MenuItemProperties.cpp @@ -82,7 +82,7 @@ void menuItemPropertiesFromScriptValue(const QScriptValue& object, MenuItemPrope } else { QScriptValue shortcutKeyEventValue = object.property("shortcutKeyEvent"); if (shortcutKeyEventValue.isValid()) { - keyEventFromScriptValue(shortcutKeyEventValue, properties.shortcutKeyEvent); + KeyEvent::fromScriptValue(shortcutKeyEventValue, properties.shortcutKeyEvent); properties.shortcutKeySequence = properties.shortcutKeyEvent; } } diff --git a/libraries/script-engine/src/MenuItemProperties.h b/libraries/script-engine/src/MenuItemProperties.h index 9dd3c6107c..889fb3855a 100644 --- a/libraries/script-engine/src/MenuItemProperties.h +++ b/libraries/script-engine/src/MenuItemProperties.h @@ -14,7 +14,7 @@ #include -#include "EventTypes.h" +#include "KeyEvent.h" const int UNSPECIFIED_POSITION = -1; From 3aa7c3c7dc0f1fc6836fbefcd7aae49a7967acd3 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 14:30:20 -0700 Subject: [PATCH 12/26] move MouseEvent out of EventTypes into its own file --- .../AbstractControllerScriptingInterface.h | 1 + libraries/script-engine/src/EventTypes.cpp | 73 +--------------- libraries/script-engine/src/EventTypes.h | 21 ----- libraries/script-engine/src/MouseEvent.cpp | 83 +++++++++++++++++++ libraries/script-engine/src/MouseEvent.h | 40 +++++++++ 5 files changed, 126 insertions(+), 92 deletions(-) create mode 100644 libraries/script-engine/src/MouseEvent.cpp create mode 100644 libraries/script-engine/src/MouseEvent.h diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index 9f583b94d8..050a555ff5 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -19,6 +19,7 @@ #include "EventTypes.h" #include "KeyEvent.h" +#include "MouseEvent.h" class AbstractInputController : public QObject { Q_OBJECT diff --git a/libraries/script-engine/src/EventTypes.cpp b/libraries/script-engine/src/EventTypes.cpp index 49c82796a2..e70e3302a5 100644 --- a/libraries/script-engine/src/EventTypes.cpp +++ b/libraries/script-engine/src/EventTypes.cpp @@ -13,87 +13,18 @@ #include #include "KeyEvent.h" +#include "MouseEvent.h" #include "EventTypes.h" - void registerEventTypes(QScriptEngine* engine) { qScriptRegisterMetaType(engine, KeyEvent::toScriptValue, KeyEvent::fromScriptValue); - qScriptRegisterMetaType(engine, mouseEventToScriptValue, mouseEventFromScriptValue); + qScriptRegisterMetaType(engine, MouseEvent::toScriptValue, MouseEvent::fromScriptValue); qScriptRegisterMetaType(engine, touchEventToScriptValue, touchEventFromScriptValue); qScriptRegisterMetaType(engine, wheelEventToScriptValue, wheelEventFromScriptValue); qScriptRegisterMetaType(engine, spatialEventToScriptValue, spatialEventFromScriptValue); } - - -MouseEvent::MouseEvent() : - x(0.0f), - y(0.0f), - isLeftButton(false), - isRightButton(false), - isMiddleButton(false), - isShifted(false), - isControl(false), - isMeta(false), - isAlt(false) -{ -}; - - -MouseEvent::MouseEvent(const QMouseEvent& event, const unsigned int deviceID) : - x(event.x()), - y(event.y()), - deviceID(deviceID), - isLeftButton(event.buttons().testFlag(Qt::LeftButton)), - isRightButton(event.buttons().testFlag(Qt::RightButton)), - isMiddleButton(event.buttons().testFlag(Qt::MiddleButton)), - isShifted(event.modifiers().testFlag(Qt::ShiftModifier)), - isControl(event.modifiers().testFlag(Qt::ControlModifier)), - isMeta(event.modifiers().testFlag(Qt::MetaModifier)), - isAlt(event.modifiers().testFlag(Qt::AltModifier)) -{ - // single button that caused the event - switch (event.button()) { - case Qt::LeftButton: - button = "LEFT"; - isLeftButton = true; - break; - case Qt::RightButton: - button = "RIGHT"; - isRightButton = true; - break; - case Qt::MiddleButton: - button = "MIDDLE"; - isMiddleButton = true; - break; - default: - button = "NONE"; - break; - } -} - -QScriptValue mouseEventToScriptValue(QScriptEngine* engine, const MouseEvent& event) { - QScriptValue obj = engine->newObject(); - obj.setProperty("x", event.x); - obj.setProperty("y", event.y); - obj.setProperty("button", event.button); - obj.setProperty("deviceID", event.deviceID); - obj.setProperty("isLeftButton", event.isLeftButton); - obj.setProperty("isRightButton", event.isRightButton); - obj.setProperty("isMiddleButton", event.isMiddleButton); - obj.setProperty("isShifted", event.isShifted); - obj.setProperty("isMeta", event.isMeta); - obj.setProperty("isControl", event.isControl); - obj.setProperty("isAlt", event.isAlt); - - return obj; -} - -void mouseEventFromScriptValue(const QScriptValue& object, MouseEvent& event) { - // nothing for now... -} - TouchEvent::TouchEvent() : x(0.0f), y(0.0f), diff --git a/libraries/script-engine/src/EventTypes.h b/libraries/script-engine/src/EventTypes.h index 121c321cf6..0cec3fe20d 100644 --- a/libraries/script-engine/src/EventTypes.h +++ b/libraries/script-engine/src/EventTypes.h @@ -22,23 +22,6 @@ #include -class MouseEvent { -public: - MouseEvent(); - MouseEvent(const QMouseEvent& event, const unsigned int deviceID = 0); - int x; - int y; - unsigned int deviceID; - QString button; - bool isLeftButton; - bool isRightButton; - bool isMiddleButton; - bool isShifted; - bool isControl; - bool isMeta; - bool isAlt; -}; - class TouchEvent { public: TouchEvent(); @@ -103,7 +86,6 @@ public: private: }; -Q_DECLARE_METATYPE(MouseEvent) Q_DECLARE_METATYPE(TouchEvent) Q_DECLARE_METATYPE(WheelEvent) Q_DECLARE_METATYPE(SpatialEvent) @@ -111,9 +93,6 @@ Q_DECLARE_METATYPE(SpatialEvent) void registerEventTypes(QScriptEngine* engine); -QScriptValue mouseEventToScriptValue(QScriptEngine* engine, const MouseEvent& event); -void mouseEventFromScriptValue(const QScriptValue& object, MouseEvent& event); - QScriptValue touchEventToScriptValue(QScriptEngine* engine, const TouchEvent& event); void touchEventFromScriptValue(const QScriptValue& object, TouchEvent& event); diff --git a/libraries/script-engine/src/MouseEvent.cpp b/libraries/script-engine/src/MouseEvent.cpp new file mode 100644 index 0000000000..34b3eb693e --- /dev/null +++ b/libraries/script-engine/src/MouseEvent.cpp @@ -0,0 +1,83 @@ +// +// MouseEvent.cpp +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include + +#include "MouseEvent.h" + +MouseEvent::MouseEvent() : + x(0.0f), + y(0.0f), + isLeftButton(false), + isRightButton(false), + isMiddleButton(false), + isShifted(false), + isControl(false), + isMeta(false), + isAlt(false) +{ + +} + + +MouseEvent::MouseEvent(const QMouseEvent& event, const unsigned int deviceID) : + x(event.x()), + y(event.y()), + deviceID(deviceID), + isLeftButton(event.buttons().testFlag(Qt::LeftButton)), + isRightButton(event.buttons().testFlag(Qt::RightButton)), + isMiddleButton(event.buttons().testFlag(Qt::MiddleButton)), + isShifted(event.modifiers().testFlag(Qt::ShiftModifier)), + isControl(event.modifiers().testFlag(Qt::ControlModifier)), + isMeta(event.modifiers().testFlag(Qt::MetaModifier)), + isAlt(event.modifiers().testFlag(Qt::AltModifier)) +{ + // single button that caused the event + switch (event.button()) { + case Qt::LeftButton: + button = "LEFT"; + isLeftButton = true; + break; + case Qt::RightButton: + button = "RIGHT"; + isRightButton = true; + break; + case Qt::MiddleButton: + button = "MIDDLE"; + isMiddleButton = true; + break; + default: + button = "NONE"; + break; + } +} + +QScriptValue MouseEvent::toScriptValue(QScriptEngine* engine, const MouseEvent& event) { + QScriptValue obj = engine->newObject(); + obj.setProperty("x", event.x); + obj.setProperty("y", event.y); + obj.setProperty("button", event.button); + obj.setProperty("deviceID", event.deviceID); + obj.setProperty("isLeftButton", event.isLeftButton); + obj.setProperty("isRightButton", event.isRightButton); + obj.setProperty("isMiddleButton", event.isMiddleButton); + obj.setProperty("isShifted", event.isShifted); + obj.setProperty("isMeta", event.isMeta); + obj.setProperty("isControl", event.isControl); + obj.setProperty("isAlt", event.isAlt); + + return obj; +} + +void MouseEvent::fromScriptValue(const QScriptValue& object, MouseEvent& event) { + // nothing for now... +} \ No newline at end of file diff --git a/libraries/script-engine/src/MouseEvent.h b/libraries/script-engine/src/MouseEvent.h new file mode 100644 index 0000000000..7555f2ea5a --- /dev/null +++ b/libraries/script-engine/src/MouseEvent.h @@ -0,0 +1,40 @@ +// +// MouseEvent.h +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_MouseEvent_h +#define hifi_MouseEvent_h + +#include + +class MouseEvent { +public: + MouseEvent(); + MouseEvent(const QMouseEvent& event, const unsigned int deviceID = 0); + + static QScriptValue toScriptValue(QScriptEngine* engine, const MouseEvent& event); + static void fromScriptValue(const QScriptValue& object, MouseEvent& event); + + int x; + int y; + unsigned int deviceID; + QString button; + bool isLeftButton; + bool isRightButton; + bool isMiddleButton; + bool isShifted; + bool isControl; + bool isMeta; + bool isAlt; +}; + +Q_DECLARE_METATYPE(MouseEvent) + +#endif // hifi_MouseEvent_h \ No newline at end of file From e72409bf395e0e49879839d332fa7729e662039e Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 14:37:04 -0700 Subject: [PATCH 13/26] move TouchEvent out of EventTypes to its own file --- .../AbstractControllerScriptingInterface.h | 1 + libraries/script-engine/src/EventTypes.cpp | 195 +--------------- libraries/script-engine/src/EventTypes.h | 42 ---- libraries/script-engine/src/TouchEvent.cpp | 211 ++++++++++++++++++ libraries/script-engine/src/TouchEvent.h | 56 +++++ 5 files changed, 270 insertions(+), 235 deletions(-) create mode 100644 libraries/script-engine/src/TouchEvent.cpp create mode 100644 libraries/script-engine/src/TouchEvent.h diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index 050a555ff5..b594d07f3e 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -20,6 +20,7 @@ #include "EventTypes.h" #include "KeyEvent.h" #include "MouseEvent.h" +#include "TouchEvent.h" class AbstractInputController : public QObject { Q_OBJECT diff --git a/libraries/script-engine/src/EventTypes.cpp b/libraries/script-engine/src/EventTypes.cpp index e70e3302a5..0bb4d2fd9c 100644 --- a/libraries/script-engine/src/EventTypes.cpp +++ b/libraries/script-engine/src/EventTypes.cpp @@ -14,209 +14,18 @@ #include "KeyEvent.h" #include "MouseEvent.h" +#include "TouchEvent.h" #include "EventTypes.h" void registerEventTypes(QScriptEngine* engine) { qScriptRegisterMetaType(engine, KeyEvent::toScriptValue, KeyEvent::fromScriptValue); qScriptRegisterMetaType(engine, MouseEvent::toScriptValue, MouseEvent::fromScriptValue); - qScriptRegisterMetaType(engine, touchEventToScriptValue, touchEventFromScriptValue); + qScriptRegisterMetaType(engine, TouchEvent::toScriptValue, TouchEvent::fromScriptValue); qScriptRegisterMetaType(engine, wheelEventToScriptValue, wheelEventFromScriptValue); qScriptRegisterMetaType(engine, spatialEventToScriptValue, spatialEventFromScriptValue); } -TouchEvent::TouchEvent() : - x(0.0f), - y(0.0f), - isPressed(false), - isMoved(false), - isStationary(false), - isReleased(false), - isShifted(false), - isControl(false), - isMeta(false), - isAlt(false), - touchPoints(0), - points(), - radius(0.0f), - isPinching(false), - isPinchOpening(false), - angles(), - angle(0.0f), - deltaAngle(0.0f), - isRotating(false), - rotating("none") -{ -}; - -TouchEvent::TouchEvent(const QTouchEvent& event) : - // these values are not set by initWithQTouchEvent() because they only apply to comparing to other events - isPinching(false), - isPinchOpening(false), - deltaAngle(0.0f), - isRotating(false), - rotating("none") -{ - initWithQTouchEvent(event); -} - -TouchEvent::TouchEvent(const QTouchEvent& event, const TouchEvent& other) { - initWithQTouchEvent(event); - calculateMetaAttributes(other); -} - -// returns the angle (in degrees) between two points (note: 0 degrees is 'east') -float angleBetweenPoints(const glm::vec2& a, const glm::vec2& b ) { - glm::vec2 length = b - a; - float angle = DEGREES_PER_RADIAN * std::atan2(length.y, length.x); - if (angle < 0) { - angle += 360.0f; - }; - return angle; -} - -void TouchEvent::initWithQTouchEvent(const QTouchEvent& event) { - // convert the touch points into an average - const QList& tPoints = event.touchPoints(); - float touchAvgX = 0.0f; - float touchAvgY = 0.0f; - touchPoints = tPoints.count(); - if (touchPoints > 1) { - for (int i = 0; i < touchPoints; ++i) { - touchAvgX += tPoints[i].pos().x(); - touchAvgY += tPoints[i].pos().y(); - - // add it to our points vector - glm::vec2 thisPoint(tPoints[i].pos().x(), tPoints[i].pos().y()); - points << thisPoint; - } - touchAvgX /= (float)(touchPoints); - touchAvgY /= (float)(touchPoints); - } else { - // I'm not sure this should ever happen, why would Qt send us a touch event for only one point? - // maybe this happens in the case of a multi-touch where all but the last finger is released? - touchAvgX = tPoints[0].pos().x(); - touchAvgY = tPoints[0].pos().y(); - } - x = touchAvgX; - y = touchAvgY; - - // after calculating the center point (average touch point), determine the maximum radius - // also calculate the rotation angle for each point - float maxRadius = 0.0f; - glm::vec2 center(x,y); - for (int i = 0; i < touchPoints; ++i) { - glm::vec2 touchPoint(tPoints[i].pos().x(), tPoints[i].pos().y()); - float thisRadius = glm::distance(center,touchPoint); - if (thisRadius > maxRadius) { - maxRadius = thisRadius; - } - - // calculate the angle for this point - float thisAngle = angleBetweenPoints(center,touchPoint); - angles << thisAngle; - } - radius = maxRadius; - - // after calculating the angles for each touch point, determine the average angle - float totalAngle = 0.0f; - for (int i = 0; i < touchPoints; ++i) { - totalAngle += angles[i]; - } - angle = totalAngle/(float)touchPoints; - - isPressed = event.touchPointStates().testFlag(Qt::TouchPointPressed); - isMoved = event.touchPointStates().testFlag(Qt::TouchPointMoved); - isStationary = event.touchPointStates().testFlag(Qt::TouchPointStationary); - isReleased = event.touchPointStates().testFlag(Qt::TouchPointReleased); - - // keyboard modifiers - isShifted = event.modifiers().testFlag(Qt::ShiftModifier); - isMeta = event.modifiers().testFlag(Qt::MetaModifier); - isControl = event.modifiers().testFlag(Qt::ControlModifier); - isAlt = event.modifiers().testFlag(Qt::AltModifier); -} - -void TouchEvent::calculateMetaAttributes(const TouchEvent& other) { - // calculate comparative event attributes... - if (other.radius > radius) { - isPinching = true; - isPinchOpening = false; - } else if (other.radius < radius) { - isPinchOpening = true; - isPinching = false; - } else { - isPinching = other.isPinching; - isPinchOpening = other.isPinchOpening; - } - - // determine if the points are rotating... - // note: if the number of touch points change between events, then we don't consider ourselves to be rotating - if (touchPoints == other.touchPoints) { - deltaAngle = angle - other.angle; - if (other.angle < angle) { - isRotating = true; - rotating = "clockwise"; - } else if (other.angle > angle) { - isRotating = true; - rotating = "counterClockwise"; - } else { - isRotating = false; - rotating = "none"; - } - } else { - deltaAngle = 0.0f; - isRotating = false; - rotating = "none"; - } -} - - -QScriptValue touchEventToScriptValue(QScriptEngine* engine, const TouchEvent& event) { - QScriptValue obj = engine->newObject(); - obj.setProperty("x", event.x); - obj.setProperty("y", event.y); - obj.setProperty("isPressed", event.isPressed); - obj.setProperty("isMoved", event.isMoved); - obj.setProperty("isStationary", event.isStationary); - obj.setProperty("isReleased", event.isReleased); - obj.setProperty("isShifted", event.isShifted); - obj.setProperty("isMeta", event.isMeta); - obj.setProperty("isControl", event.isControl); - obj.setProperty("isAlt", event.isAlt); - obj.setProperty("touchPoints", event.touchPoints); - - QScriptValue pointsObj = engine->newArray(); - int index = 0; - foreach (glm::vec2 point, event.points) { - QScriptValue thisPoint = vec2toScriptValue(engine, point); - pointsObj.setProperty(index, thisPoint); - index++; - } - obj.setProperty("points", pointsObj); - obj.setProperty("radius", event.radius); - obj.setProperty("isPinching", event.isPinching); - obj.setProperty("isPinchOpening", event.isPinchOpening); - - obj.setProperty("angle", event.angle); - obj.setProperty("deltaAngle", event.deltaAngle); - QScriptValue anglesObj = engine->newArray(); - index = 0; - foreach (float angle, event.angles) { - anglesObj.setProperty(index, angle); - index++; - } - obj.setProperty("angles", anglesObj); - - obj.setProperty("isRotating", event.isRotating); - obj.setProperty("rotating", event.rotating); - return obj; -} - -void touchEventFromScriptValue(const QScriptValue& object, TouchEvent& event) { - // nothing for now... -} - WheelEvent::WheelEvent() : x(0.0f), y(0.0f), diff --git a/libraries/script-engine/src/EventTypes.h b/libraries/script-engine/src/EventTypes.h index 0cec3fe20d..54c2934c42 100644 --- a/libraries/script-engine/src/EventTypes.h +++ b/libraries/script-engine/src/EventTypes.h @@ -12,50 +12,12 @@ #ifndef hifi_EventTypes_h #define hifi_EventTypes_h -#include #include #include -#include -#include #include - -class TouchEvent { -public: - TouchEvent(); - TouchEvent(const QTouchEvent& event); - TouchEvent(const QTouchEvent& event, const TouchEvent& other); - - float x; - float y; - bool isPressed; - bool isMoved; - bool isStationary; - bool isReleased; - bool isShifted; - bool isControl; - bool isMeta; - bool isAlt; - int touchPoints; - QVector points; - float radius; - bool isPinching; - bool isPinchOpening; - - // angles are in degrees - QVector angles; // angle from center to each point - float angle; // the average of the angles - float deltaAngle; // the change in average angle from last event - bool isRotating; - QString rotating; - -private: - void initWithQTouchEvent(const QTouchEvent& event); - void calculateMetaAttributes(const TouchEvent& other); -}; - class WheelEvent { public: WheelEvent(); @@ -86,16 +48,12 @@ public: private: }; -Q_DECLARE_METATYPE(TouchEvent) Q_DECLARE_METATYPE(WheelEvent) Q_DECLARE_METATYPE(SpatialEvent) void registerEventTypes(QScriptEngine* engine); -QScriptValue touchEventToScriptValue(QScriptEngine* engine, const TouchEvent& event); -void touchEventFromScriptValue(const QScriptValue& object, TouchEvent& event); - QScriptValue wheelEventToScriptValue(QScriptEngine* engine, const WheelEvent& event); void wheelEventFromScriptValue(const QScriptValue& object, WheelEvent& event); diff --git a/libraries/script-engine/src/TouchEvent.cpp b/libraries/script-engine/src/TouchEvent.cpp new file mode 100644 index 0000000000..b1cbbc2f15 --- /dev/null +++ b/libraries/script-engine/src/TouchEvent.cpp @@ -0,0 +1,211 @@ +// +// TouchEvent.cpp +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include +#include + +#include +#include + +#include "TouchEvent.h" + +TouchEvent::TouchEvent() : + x(0.0f), + y(0.0f), + isPressed(false), + isMoved(false), + isStationary(false), + isReleased(false), + isShifted(false), + isControl(false), + isMeta(false), + isAlt(false), + touchPoints(0), + points(), + radius(0.0f), + isPinching(false), + isPinchOpening(false), + angles(), + angle(0.0f), + deltaAngle(0.0f), + isRotating(false), + rotating("none") +{ + +} + +TouchEvent::TouchEvent(const QTouchEvent& event) : + // these values are not set by initWithQTouchEvent() because they only apply to comparing to other events + isPinching(false), + isPinchOpening(false), + deltaAngle(0.0f), + isRotating(false), + rotating("none") +{ + initWithQTouchEvent(event); +} + +TouchEvent::TouchEvent(const QTouchEvent& event, const TouchEvent& other) { + initWithQTouchEvent(event); + calculateMetaAttributes(other); +} + +// returns the angle (in degrees) between two points (note: 0 degrees is 'east') +float angleBetweenPoints(const glm::vec2& a, const glm::vec2& b ) { + glm::vec2 length = b - a; + float angle = DEGREES_PER_RADIAN * std::atan2(length.y, length.x); + if (angle < 0) { + angle += 360.0f; + }; + return angle; +} + +void TouchEvent::initWithQTouchEvent(const QTouchEvent& event) { + // convert the touch points into an average + const QList& tPoints = event.touchPoints(); + float touchAvgX = 0.0f; + float touchAvgY = 0.0f; + touchPoints = tPoints.count(); + if (touchPoints > 1) { + for (int i = 0; i < touchPoints; ++i) { + touchAvgX += tPoints[i].pos().x(); + touchAvgY += tPoints[i].pos().y(); + + // add it to our points vector + glm::vec2 thisPoint(tPoints[i].pos().x(), tPoints[i].pos().y()); + points << thisPoint; + } + touchAvgX /= (float)(touchPoints); + touchAvgY /= (float)(touchPoints); + } else { + // I'm not sure this should ever happen, why would Qt send us a touch event for only one point? + // maybe this happens in the case of a multi-touch where all but the last finger is released? + touchAvgX = tPoints[0].pos().x(); + touchAvgY = tPoints[0].pos().y(); + } + x = touchAvgX; + y = touchAvgY; + + // after calculating the center point (average touch point), determine the maximum radius + // also calculate the rotation angle for each point + float maxRadius = 0.0f; + glm::vec2 center(x,y); + for (int i = 0; i < touchPoints; ++i) { + glm::vec2 touchPoint(tPoints[i].pos().x(), tPoints[i].pos().y()); + float thisRadius = glm::distance(center,touchPoint); + if (thisRadius > maxRadius) { + maxRadius = thisRadius; + } + + // calculate the angle for this point + float thisAngle = angleBetweenPoints(center,touchPoint); + angles << thisAngle; + } + radius = maxRadius; + + // after calculating the angles for each touch point, determine the average angle + float totalAngle = 0.0f; + for (int i = 0; i < touchPoints; ++i) { + totalAngle += angles[i]; + } + angle = totalAngle/(float)touchPoints; + + isPressed = event.touchPointStates().testFlag(Qt::TouchPointPressed); + isMoved = event.touchPointStates().testFlag(Qt::TouchPointMoved); + isStationary = event.touchPointStates().testFlag(Qt::TouchPointStationary); + isReleased = event.touchPointStates().testFlag(Qt::TouchPointReleased); + + // keyboard modifiers + isShifted = event.modifiers().testFlag(Qt::ShiftModifier); + isMeta = event.modifiers().testFlag(Qt::MetaModifier); + isControl = event.modifiers().testFlag(Qt::ControlModifier); + isAlt = event.modifiers().testFlag(Qt::AltModifier); +} + +void TouchEvent::calculateMetaAttributes(const TouchEvent& other) { + // calculate comparative event attributes... + if (other.radius > radius) { + isPinching = true; + isPinchOpening = false; + } else if (other.radius < radius) { + isPinchOpening = true; + isPinching = false; + } else { + isPinching = other.isPinching; + isPinchOpening = other.isPinchOpening; + } + + // determine if the points are rotating... + // note: if the number of touch points change between events, then we don't consider ourselves to be rotating + if (touchPoints == other.touchPoints) { + deltaAngle = angle - other.angle; + if (other.angle < angle) { + isRotating = true; + rotating = "clockwise"; + } else if (other.angle > angle) { + isRotating = true; + rotating = "counterClockwise"; + } else { + isRotating = false; + rotating = "none"; + } + } else { + deltaAngle = 0.0f; + isRotating = false; + rotating = "none"; + } +} + +QScriptValue TouchEvent::toScriptValue(QScriptEngine* engine, const TouchEvent& event) { + QScriptValue obj = engine->newObject(); + obj.setProperty("x", event.x); + obj.setProperty("y", event.y); + obj.setProperty("isPressed", event.isPressed); + obj.setProperty("isMoved", event.isMoved); + obj.setProperty("isStationary", event.isStationary); + obj.setProperty("isReleased", event.isReleased); + obj.setProperty("isShifted", event.isShifted); + obj.setProperty("isMeta", event.isMeta); + obj.setProperty("isControl", event.isControl); + obj.setProperty("isAlt", event.isAlt); + obj.setProperty("touchPoints", event.touchPoints); + + QScriptValue pointsObj = engine->newArray(); + int index = 0; + foreach (glm::vec2 point, event.points) { + QScriptValue thisPoint = vec2toScriptValue(engine, point); + pointsObj.setProperty(index, thisPoint); + index++; + } + obj.setProperty("points", pointsObj); + obj.setProperty("radius", event.radius); + obj.setProperty("isPinching", event.isPinching); + obj.setProperty("isPinchOpening", event.isPinchOpening); + + obj.setProperty("angle", event.angle); + obj.setProperty("deltaAngle", event.deltaAngle); + QScriptValue anglesObj = engine->newArray(); + index = 0; + foreach (float angle, event.angles) { + anglesObj.setProperty(index, angle); + index++; + } + obj.setProperty("angles", anglesObj); + + obj.setProperty("isRotating", event.isRotating); + obj.setProperty("rotating", event.rotating); + return obj; +} + +void TouchEvent::fromScriptValue(const QScriptValue& object, TouchEvent& event) { + // nothing for now... +} diff --git a/libraries/script-engine/src/TouchEvent.h b/libraries/script-engine/src/TouchEvent.h new file mode 100644 index 0000000000..9c1147fecf --- /dev/null +++ b/libraries/script-engine/src/TouchEvent.h @@ -0,0 +1,56 @@ +// +// TouchEvent.h +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_TouchEvent_h +#define hifi_TouchEvent_h + +#include + +class TouchEvent { +public: + TouchEvent(); + TouchEvent(const QTouchEvent& event); + TouchEvent(const QTouchEvent& event, const TouchEvent& other); + + static QScriptValue toScriptValue(QScriptEngine* engine, const TouchEvent& event); + static void fromScriptValue(const QScriptValue& object, TouchEvent& event); + + float x; + float y; + bool isPressed; + bool isMoved; + bool isStationary; + bool isReleased; + bool isShifted; + bool isControl; + bool isMeta; + bool isAlt; + int touchPoints; + QVector points; + float radius; + bool isPinching; + bool isPinchOpening; + + // angles are in degrees + QVector angles; // angle from center to each point + float angle; // the average of the angles + float deltaAngle; // the change in average angle from last event + bool isRotating; + QString rotating; + +private: + void initWithQTouchEvent(const QTouchEvent& event); + void calculateMetaAttributes(const TouchEvent& other); +}; + +Q_DECLARE_METATYPE(TouchEvent) + +#endif // hifi_TouchEvent_h \ No newline at end of file From 818c97fcafcce411e8d4b0c9a943f97a3916c968 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 14:41:23 -0700 Subject: [PATCH 14/26] move WheelEvent out of EventTypes into its own file --- .../AbstractControllerScriptingInterface.h | 1 + libraries/script-engine/src/EventTypes.cpp | 64 +--------------- libraries/script-engine/src/EventTypes.h | 21 ------ libraries/script-engine/src/WheelEvent.cpp | 75 +++++++++++++++++++ libraries/script-engine/src/WheelEvent.h | 40 ++++++++++ 5 files changed, 118 insertions(+), 83 deletions(-) create mode 100644 libraries/script-engine/src/WheelEvent.cpp create mode 100644 libraries/script-engine/src/WheelEvent.h diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index b594d07f3e..589cbbb089 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -21,6 +21,7 @@ #include "KeyEvent.h" #include "MouseEvent.h" #include "TouchEvent.h" +#include "WheelEvent.h" class AbstractInputController : public QObject { Q_OBJECT diff --git a/libraries/script-engine/src/EventTypes.cpp b/libraries/script-engine/src/EventTypes.cpp index 0bb4d2fd9c..e2a868eb8d 100644 --- a/libraries/script-engine/src/EventTypes.cpp +++ b/libraries/script-engine/src/EventTypes.cpp @@ -15,6 +15,7 @@ #include "KeyEvent.h" #include "MouseEvent.h" #include "TouchEvent.h" +#include "WheelEvent.h" #include "EventTypes.h" @@ -22,71 +23,10 @@ void registerEventTypes(QScriptEngine* engine) { qScriptRegisterMetaType(engine, KeyEvent::toScriptValue, KeyEvent::fromScriptValue); qScriptRegisterMetaType(engine, MouseEvent::toScriptValue, MouseEvent::fromScriptValue); qScriptRegisterMetaType(engine, TouchEvent::toScriptValue, TouchEvent::fromScriptValue); - qScriptRegisterMetaType(engine, wheelEventToScriptValue, wheelEventFromScriptValue); + qScriptRegisterMetaType(engine, WheelEvent::toScriptValue, WheelEvent::fromScriptValue); qScriptRegisterMetaType(engine, spatialEventToScriptValue, spatialEventFromScriptValue); } -WheelEvent::WheelEvent() : - x(0.0f), - y(0.0f), - delta(0.0f), - orientation("UNKNOwN"), - isLeftButton(false), - isRightButton(false), - isMiddleButton(false), - isShifted(false), - isControl(false), - isMeta(false), - isAlt(false) -{ -}; - -WheelEvent::WheelEvent(const QWheelEvent& event) { - x = event.x(); - y = event.y(); - - delta = event.delta(); - if (event.orientation() == Qt::Horizontal) { - orientation = "HORIZONTAL"; - } else { - orientation = "VERTICAL"; - } - - // button pressed state - isLeftButton = (event.buttons().testFlag(Qt::LeftButton)); - isRightButton = (event.buttons().testFlag(Qt::RightButton)); - isMiddleButton = (event.buttons().testFlag(Qt::MiddleButton)); - - // keyboard modifiers - isShifted = event.modifiers().testFlag(Qt::ShiftModifier); - isMeta = event.modifiers().testFlag(Qt::MetaModifier); - isControl = event.modifiers().testFlag(Qt::ControlModifier); - isAlt = event.modifiers().testFlag(Qt::AltModifier); -} - - -QScriptValue wheelEventToScriptValue(QScriptEngine* engine, const WheelEvent& event) { - QScriptValue obj = engine->newObject(); - obj.setProperty("x", event.x); - obj.setProperty("y", event.y); - obj.setProperty("delta", event.delta); - obj.setProperty("orientation", event.orientation); - obj.setProperty("isLeftButton", event.isLeftButton); - obj.setProperty("isRightButton", event.isRightButton); - obj.setProperty("isMiddleButton", event.isMiddleButton); - obj.setProperty("isShifted", event.isShifted); - obj.setProperty("isMeta", event.isMeta); - obj.setProperty("isControl", event.isControl); - obj.setProperty("isAlt", event.isAlt); - return obj; -} - -void wheelEventFromScriptValue(const QScriptValue& object, WheelEvent& event) { - // nothing for now... -} - - - SpatialEvent::SpatialEvent() : locTranslation(0.0f), locRotation(), diff --git a/libraries/script-engine/src/EventTypes.h b/libraries/script-engine/src/EventTypes.h index 54c2934c42..d08d704587 100644 --- a/libraries/script-engine/src/EventTypes.h +++ b/libraries/script-engine/src/EventTypes.h @@ -18,23 +18,6 @@ #include -class WheelEvent { -public: - WheelEvent(); - WheelEvent(const QWheelEvent& event); - int x; - int y; - int delta; - QString orientation; - bool isLeftButton; - bool isRightButton; - bool isMiddleButton; - bool isShifted; - bool isControl; - bool isMeta; - bool isAlt; -}; - class SpatialEvent { public: SpatialEvent(); @@ -48,15 +31,11 @@ public: private: }; -Q_DECLARE_METATYPE(WheelEvent) Q_DECLARE_METATYPE(SpatialEvent) void registerEventTypes(QScriptEngine* engine); -QScriptValue wheelEventToScriptValue(QScriptEngine* engine, const WheelEvent& event); -void wheelEventFromScriptValue(const QScriptValue& object, WheelEvent& event); - QScriptValue spatialEventToScriptValue(QScriptEngine* engine, const SpatialEvent& event); void spatialEventFromScriptValue(const QScriptValue& object, SpatialEvent& event); diff --git a/libraries/script-engine/src/WheelEvent.cpp b/libraries/script-engine/src/WheelEvent.cpp new file mode 100644 index 0000000000..b329ef58c8 --- /dev/null +++ b/libraries/script-engine/src/WheelEvent.cpp @@ -0,0 +1,75 @@ +// +// WheelEvent.cpp +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include + +#include "WheelEvent.h" + +WheelEvent::WheelEvent() : + x(0.0f), + y(0.0f), + delta(0.0f), + orientation("UNKNOwN"), + isLeftButton(false), + isRightButton(false), + isMiddleButton(false), + isShifted(false), + isControl(false), + isMeta(false), + isAlt(false) +{ + +} + +WheelEvent::WheelEvent(const QWheelEvent& event) { + x = event.x(); + y = event.y(); + + delta = event.delta(); + if (event.orientation() == Qt::Horizontal) { + orientation = "HORIZONTAL"; + } else { + orientation = "VERTICAL"; + } + + // button pressed state + isLeftButton = (event.buttons().testFlag(Qt::LeftButton)); + isRightButton = (event.buttons().testFlag(Qt::RightButton)); + isMiddleButton = (event.buttons().testFlag(Qt::MiddleButton)); + + // keyboard modifiers + isShifted = event.modifiers().testFlag(Qt::ShiftModifier); + isMeta = event.modifiers().testFlag(Qt::MetaModifier); + isControl = event.modifiers().testFlag(Qt::ControlModifier); + isAlt = event.modifiers().testFlag(Qt::AltModifier); +} + + +QScriptValue WheelEvent::toScriptValue(QScriptEngine* engine, const WheelEvent& event) { + QScriptValue obj = engine->newObject(); + obj.setProperty("x", event.x); + obj.setProperty("y", event.y); + obj.setProperty("delta", event.delta); + obj.setProperty("orientation", event.orientation); + obj.setProperty("isLeftButton", event.isLeftButton); + obj.setProperty("isRightButton", event.isRightButton); + obj.setProperty("isMiddleButton", event.isMiddleButton); + obj.setProperty("isShifted", event.isShifted); + obj.setProperty("isMeta", event.isMeta); + obj.setProperty("isControl", event.isControl); + obj.setProperty("isAlt", event.isAlt); + return obj; +} + +void WheelEvent::fromScriptValue(const QScriptValue& object, WheelEvent& event) { + // nothing for now... +} \ No newline at end of file diff --git a/libraries/script-engine/src/WheelEvent.h b/libraries/script-engine/src/WheelEvent.h new file mode 100644 index 0000000000..edac4bc3c3 --- /dev/null +++ b/libraries/script-engine/src/WheelEvent.h @@ -0,0 +1,40 @@ +// +// WheelEvent.h +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_WheelEvent_h +#define hifi_WheelEvent_h + +#include + +class WheelEvent { +public: + WheelEvent(); + WheelEvent(const QWheelEvent& event); + + static QScriptValue toScriptValue(QScriptEngine* engine, const WheelEvent& event); + static void fromScriptValue(const QScriptValue& object, WheelEvent& event); + + int x; + int y; + int delta; + QString orientation; + bool isLeftButton; + bool isRightButton; + bool isMiddleButton; + bool isShifted; + bool isControl; + bool isMeta; + bool isAlt; +}; + +Q_DECLARE_METATYPE(WheelEvent) + +#endif // hifi_WheelEvent_h \ No newline at end of file From 76493597dadf72e6bb1a5f7e6d91bbd055225296 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 14:47:44 -0700 Subject: [PATCH 15/26] move SpatialEvent from EventTypes to its own file --- .../AbstractControllerScriptingInterface.h | 1 + libraries/script-engine/src/EventTypes.cpp | 37 +-------------- libraries/script-engine/src/EventTypes.h | 25 +--------- libraries/script-engine/src/SpatialEvent.cpp | 46 +++++++++++++++++++ libraries/script-engine/src/SpatialEvent.h | 36 +++++++++++++++ 5 files changed, 86 insertions(+), 59 deletions(-) create mode 100644 libraries/script-engine/src/SpatialEvent.cpp create mode 100644 libraries/script-engine/src/SpatialEvent.h diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index 589cbbb089..a71ab5332b 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -20,6 +20,7 @@ #include "EventTypes.h" #include "KeyEvent.h" #include "MouseEvent.h" +#include "SpatialEvent.h" #include "TouchEvent.h" #include "WheelEvent.h" diff --git a/libraries/script-engine/src/EventTypes.cpp b/libraries/script-engine/src/EventTypes.cpp index e2a868eb8d..84838cfd33 100644 --- a/libraries/script-engine/src/EventTypes.cpp +++ b/libraries/script-engine/src/EventTypes.cpp @@ -9,11 +9,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include -#include - #include "KeyEvent.h" #include "MouseEvent.h" +#include "SpatialEvent.h" #include "TouchEvent.h" #include "WheelEvent.h" @@ -24,36 +22,5 @@ void registerEventTypes(QScriptEngine* engine) { qScriptRegisterMetaType(engine, MouseEvent::toScriptValue, MouseEvent::fromScriptValue); qScriptRegisterMetaType(engine, TouchEvent::toScriptValue, TouchEvent::fromScriptValue); qScriptRegisterMetaType(engine, WheelEvent::toScriptValue, WheelEvent::fromScriptValue); - qScriptRegisterMetaType(engine, spatialEventToScriptValue, spatialEventFromScriptValue); -} - -SpatialEvent::SpatialEvent() : - locTranslation(0.0f), - locRotation(), - absTranslation(0.0f), - absRotation() -{ -}; - -SpatialEvent::SpatialEvent(const SpatialEvent& event) { - locTranslation = event.locTranslation; - locRotation = event.locRotation; - absTranslation = event.absTranslation; - absRotation = event.absRotation; -} - - -QScriptValue spatialEventToScriptValue(QScriptEngine* engine, const SpatialEvent& event) { - QScriptValue obj = engine->newObject(); - - obj.setProperty("locTranslation", vec3toScriptValue(engine, event.locTranslation) ); - obj.setProperty("locRotation", quatToScriptValue(engine, event.locRotation) ); - obj.setProperty("absTranslation", vec3toScriptValue(engine, event.absTranslation) ); - obj.setProperty("absRotation", quatToScriptValue(engine, event.absRotation) ); - - return obj; -} - -void spatialEventFromScriptValue(const QScriptValue& object,SpatialEvent& event) { - // nothing for now... + qScriptRegisterMetaType(engine, SpatialEvent::toScriptValue, SpatialEvent::fromScriptValue); } diff --git a/libraries/script-engine/src/EventTypes.h b/libraries/script-engine/src/EventTypes.h index d08d704587..fc808ea560 100644 --- a/libraries/script-engine/src/EventTypes.h +++ b/libraries/script-engine/src/EventTypes.h @@ -12,31 +12,8 @@ #ifndef hifi_EventTypes_h #define hifi_EventTypes_h -#include - -#include - -#include - -class SpatialEvent { -public: - SpatialEvent(); - SpatialEvent(const SpatialEvent& other); - - glm::vec3 locTranslation; - glm::quat locRotation; - glm::vec3 absTranslation; - glm::quat absRotation; - -private: -}; - -Q_DECLARE_METATYPE(SpatialEvent) +#include void registerEventTypes(QScriptEngine* engine); - -QScriptValue spatialEventToScriptValue(QScriptEngine* engine, const SpatialEvent& event); -void spatialEventFromScriptValue(const QScriptValue& object, SpatialEvent& event); - #endif // hifi_EventTypes_h diff --git a/libraries/script-engine/src/SpatialEvent.cpp b/libraries/script-engine/src/SpatialEvent.cpp new file mode 100644 index 0000000000..f20a0c2b1e --- /dev/null +++ b/libraries/script-engine/src/SpatialEvent.cpp @@ -0,0 +1,46 @@ +// +// SpatialEvent.cpp +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include + +#include "SpatialEvent.h" + +SpatialEvent::SpatialEvent() : + locTranslation(0.0f), + locRotation(), + absTranslation(0.0f), + absRotation() +{ + +} + +SpatialEvent::SpatialEvent(const SpatialEvent& event) { + locTranslation = event.locTranslation; + locRotation = event.locRotation; + absTranslation = event.absTranslation; + absRotation = event.absRotation; +} + + +QScriptValue SpatialEvent::toScriptValue(QScriptEngine* engine, const SpatialEvent& event) { + QScriptValue obj = engine->newObject(); + + obj.setProperty("locTranslation", vec3toScriptValue(engine, event.locTranslation) ); + obj.setProperty("locRotation", quatToScriptValue(engine, event.locRotation) ); + obj.setProperty("absTranslation", vec3toScriptValue(engine, event.absTranslation) ); + obj.setProperty("absRotation", quatToScriptValue(engine, event.absRotation) ); + + return obj; +} + +void SpatialEvent::fromScriptValue(const QScriptValue& object,SpatialEvent& event) { + // nothing for now... +} \ No newline at end of file diff --git a/libraries/script-engine/src/SpatialEvent.h b/libraries/script-engine/src/SpatialEvent.h new file mode 100644 index 0000000000..e0fcc03824 --- /dev/null +++ b/libraries/script-engine/src/SpatialEvent.h @@ -0,0 +1,36 @@ +// +// SpatialEvent.h +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_SpatialEvent_h +#define hifi_SpatialEvent_h + +#include +#include + +#include + +class SpatialEvent { +public: + SpatialEvent(); + SpatialEvent(const SpatialEvent& other); + + static QScriptValue toScriptValue(QScriptEngine* engine, const SpatialEvent& event); + static void fromScriptValue(const QScriptValue& object, SpatialEvent& event); + + glm::vec3 locTranslation; + glm::quat locRotation; + glm::vec3 absTranslation; + glm::quat absRotation; +}; + +Q_DECLARE_METATYPE(SpatialEvent) + +#endif // hifi_SpatialEvent_h \ No newline at end of file From c3fe7c74976528dc31d2f100096fca44660f4001 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 15:21:47 -0700 Subject: [PATCH 16/26] send global HFActionEvent from ControllerScriptingInterface --- interface/src/Application.cpp | 4 ++++ .../src/scripting/ControllerScriptingInterface.cpp | 8 ++++++++ .../src/scripting/ControllerScriptingInterface.h | 4 ++++ .../src/AbstractControllerScriptingInterface.h | 8 ++++---- libraries/script-engine/src/EventTypes.cpp | 2 ++ libraries/script-engine/src/HFActionEvent.cpp | 13 ++++++++++++- libraries/script-engine/src/HFActionEvent.h | 12 ++++++++++-- libraries/script-engine/src/HFMetaEvent.h | 1 + libraries/script-engine/src/ScriptEngine.cpp | 1 + 9 files changed, 46 insertions(+), 7 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 81d8098002..02fdbbb534 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -835,6 +835,10 @@ bool Application::event(QEvent* event) { return false; } + + if (HFActionEvent::types().contains(event->type())) { + _controllerScriptingInterface.handleMetaEvent(static_cast(event)); + } return QApplication::event(event); } diff --git a/interface/src/scripting/ControllerScriptingInterface.cpp b/interface/src/scripting/ControllerScriptingInterface.cpp index f2e65a6e28..b2389e3df2 100644 --- a/interface/src/scripting/ControllerScriptingInterface.cpp +++ b/interface/src/scripting/ControllerScriptingInterface.cpp @@ -20,8 +20,16 @@ ControllerScriptingInterface::ControllerScriptingInterface() : _touchCaptured(false), _wheelCaptured(false) { + } +void ControllerScriptingInterface::handleMetaEvent(HFMetaEvent* event) { + if (event->type() == HFActionEvent::startType()) { + emitActionStartEvent(static_cast(event)); + } else if (event->type() == HFActionEvent::endType()) { + emitActionEndEvent(static_cast(event)); + } +} const PalmData* ControllerScriptingInterface::getPrimaryPalm() const { int leftPalmIndex, rightPalmIndex; diff --git a/interface/src/scripting/ControllerScriptingInterface.h b/interface/src/scripting/ControllerScriptingInterface.h index 916c87d85c..c38ac888e4 100644 --- a/interface/src/scripting/ControllerScriptingInterface.h +++ b/interface/src/scripting/ControllerScriptingInterface.h @@ -56,6 +56,10 @@ public: ControllerScriptingInterface(); void emitKeyPressEvent(QKeyEvent* event) { emit keyPressEvent(KeyEvent(*event)); } void emitKeyReleaseEvent(QKeyEvent* event) { emit keyReleaseEvent(KeyEvent(*event)); } + + void handleMetaEvent(HFMetaEvent* event); + void emitActionStartEvent(HFActionEvent* event) { emit actionStartEvent(*event); } + void emitActionEndEvent(HFActionEvent* event) { emit actionEndEvent(*event); } void emitMouseMoveEvent(QMouseEvent* event, unsigned int deviceID = 0) { emit mouseMoveEvent(MouseEvent(*event, deviceID)); } void emitMousePressEvent(QMouseEvent* event, unsigned int deviceID = 0) { emit mousePressEvent(MouseEvent(*event, deviceID)); } diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index a71ab5332b..cfb7d6bb89 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -17,7 +17,7 @@ #include #include -#include "EventTypes.h" +#include "HFActionEvent.h" #include "KeyEvent.h" #include "MouseEvent.h" #include "SpatialEvent.h" @@ -93,9 +93,9 @@ public slots: signals: void keyPressEvent(const KeyEvent& event); void keyReleaseEvent(const KeyEvent& event); -// -// void actionStartEvent(const HFActionEvent& event); -// void actionEndEvent(const HFActionEvent& event); + + void actionStartEvent(const HFActionEvent& event); + void actionEndEvent(const HFActionEvent& event); void mouseMoveEvent(const MouseEvent& event, unsigned int deviceID = 0); void mousePressEvent(const MouseEvent& event, unsigned int deviceID = 0); diff --git a/libraries/script-engine/src/EventTypes.cpp b/libraries/script-engine/src/EventTypes.cpp index 84838cfd33..ce3c82e121 100644 --- a/libraries/script-engine/src/EventTypes.cpp +++ b/libraries/script-engine/src/EventTypes.cpp @@ -9,6 +9,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include "HFActionEvent.h" #include "KeyEvent.h" #include "MouseEvent.h" #include "SpatialEvent.h" @@ -18,6 +19,7 @@ #include "EventTypes.h" void registerEventTypes(QScriptEngine* engine) { + qScriptRegisterMetaType(engine, HFActionEvent::toScriptValue, HFActionEvent::fromScriptValue); qScriptRegisterMetaType(engine, KeyEvent::toScriptValue, KeyEvent::fromScriptValue); qScriptRegisterMetaType(engine, MouseEvent::toScriptValue, MouseEvent::fromScriptValue); qScriptRegisterMetaType(engine, TouchEvent::toScriptValue, TouchEvent::fromScriptValue); diff --git a/libraries/script-engine/src/HFActionEvent.cpp b/libraries/script-engine/src/HFActionEvent.cpp index e393a2f332..e73b0787d2 100644 --- a/libraries/script-engine/src/HFActionEvent.cpp +++ b/libraries/script-engine/src/HFActionEvent.cpp @@ -13,7 +13,7 @@ HFActionEvent::HFActionEvent(QEvent::Type type, const QPointF& localPosition) : HFMetaEvent(type), - _localPosition(localPosition) + localPosition(localPosition) { } @@ -28,3 +28,14 @@ QEvent::Type HFActionEvent::endType() { return endType; } +QScriptValue HFActionEvent::toScriptValue(QScriptEngine* engine, const HFActionEvent& event) { + QScriptValue obj = engine->newObject(); + obj.setProperty("x", event.localPosition.x()); + obj.setProperty("y", event.localPosition.y()); + return obj; +} + +void HFActionEvent::fromScriptValue(const QScriptValue& object, HFActionEvent& event) { + // not yet implemented +} + diff --git a/libraries/script-engine/src/HFActionEvent.h b/libraries/script-engine/src/HFActionEvent.h index 3371312755..7b13160199 100644 --- a/libraries/script-engine/src/HFActionEvent.h +++ b/libraries/script-engine/src/HFActionEvent.h @@ -14,14 +14,22 @@ #include "HFMetaEvent.h" +#include + class HFActionEvent : public HFMetaEvent { public: + HFActionEvent() {}; HFActionEvent(QEvent::Type type, const QPointF& localPosition); static QEvent::Type startType(); static QEvent::Type endType(); -private: - QPointF _localPosition; + + static QScriptValue toScriptValue(QScriptEngine* engine, const HFActionEvent& event); + static void fromScriptValue(const QScriptValue& object, HFActionEvent& event); + + QPointF localPosition; }; +Q_DECLARE_METATYPE(HFActionEvent) + #endif // hifi_HFActionEvent_h \ No newline at end of file diff --git a/libraries/script-engine/src/HFMetaEvent.h b/libraries/script-engine/src/HFMetaEvent.h index 8fbdfab1fa..56db32d7aa 100644 --- a/libraries/script-engine/src/HFMetaEvent.h +++ b/libraries/script-engine/src/HFMetaEvent.h @@ -16,6 +16,7 @@ class HFMetaEvent : public QEvent { public: + HFMetaEvent() : QEvent(HFMetaEvent::newEventType()) {}; HFMetaEvent(QEvent::Type type) : QEvent(type) {}; static const QSet& types() { return HFMetaEvent::_types; } protected: diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 3ccd0a74c3..fb98124fc9 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -35,6 +35,7 @@ #include "AnimationObject.h" #include "ArrayBufferViewClass.h" #include "DataViewClass.h" +#include "EventTypes.h" #include "MenuItemProperties.h" #include "LocalVoxels.h" #include "ScriptEngine.h" From a12e0dd0dc02fae636facfdfc655ccb09c377942 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 15:27:27 -0700 Subject: [PATCH 17/26] fix HFActionEvent headers for new placement --- libraries/script-engine/src/HFActionEvent.cpp | 2 +- libraries/script-engine/src/HFActionEvent.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/script-engine/src/HFActionEvent.cpp b/libraries/script-engine/src/HFActionEvent.cpp index e73b0787d2..8a966a3fb3 100644 --- a/libraries/script-engine/src/HFActionEvent.cpp +++ b/libraries/script-engine/src/HFActionEvent.cpp @@ -1,6 +1,6 @@ // // HFActionEvent.cpp -// interface/src/events +// script-engine/src // // Created by Stephen Birarda on 2014-10-27. // Copyright 2014 High Fidelity, Inc. diff --git a/libraries/script-engine/src/HFActionEvent.h b/libraries/script-engine/src/HFActionEvent.h index 7b13160199..c094161053 100644 --- a/libraries/script-engine/src/HFActionEvent.h +++ b/libraries/script-engine/src/HFActionEvent.h @@ -1,6 +1,6 @@ // // HFActionEvent.h -// interface/src/events +// script-engine/src // // Created by Stephen Birarda on 2014-10-27. // Copyright 2014 High Fidelity, Inc. From 8556b8b95de299ec22455c391b466a03bd69a94b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 15:28:02 -0700 Subject: [PATCH 18/26] fix HFMetaEvent headers for new placement --- libraries/script-engine/src/HFMetaEvent.cpp | 2 +- libraries/script-engine/src/HFMetaEvent.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/script-engine/src/HFMetaEvent.cpp b/libraries/script-engine/src/HFMetaEvent.cpp index c9d77868cd..f06c349996 100644 --- a/libraries/script-engine/src/HFMetaEvent.cpp +++ b/libraries/script-engine/src/HFMetaEvent.cpp @@ -1,6 +1,6 @@ // // HFMetaEvent.cpp -// interface/src/events +// script-engine/src // // Created by Stephen Birarda on 2014-10-27. // Copyright 2014 High Fidelity, Inc. diff --git a/libraries/script-engine/src/HFMetaEvent.h b/libraries/script-engine/src/HFMetaEvent.h index 56db32d7aa..2fd71b8a3b 100644 --- a/libraries/script-engine/src/HFMetaEvent.h +++ b/libraries/script-engine/src/HFMetaEvent.h @@ -1,6 +1,6 @@ // // HFMetaEvent.h -// interface/src/events +// script-engine/src // // Created by Stephen Birarda on 2014-10-27. // Copyright 2014 High Fidelity, Inc. From 7b5115c35ff6e680336ddfa8d40863ba4c2cdc1d Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 15:37:31 -0700 Subject: [PATCH 19/26] add a new meta event, HFCancelEvent --- .../ControllerScriptingInterface.cpp | 13 +++++++-- .../scripting/ControllerScriptingInterface.h | 2 -- .../AbstractControllerScriptingInterface.h | 3 ++ libraries/script-engine/src/HFCancelEvent.cpp | 28 ++++++++++++++++++ libraries/script-engine/src/HFCancelEvent.h | 29 +++++++++++++++++++ 5 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 libraries/script-engine/src/HFCancelEvent.cpp create mode 100644 libraries/script-engine/src/HFCancelEvent.h diff --git a/interface/src/scripting/ControllerScriptingInterface.cpp b/interface/src/scripting/ControllerScriptingInterface.cpp index b2389e3df2..090bbea66e 100644 --- a/interface/src/scripting/ControllerScriptingInterface.cpp +++ b/interface/src/scripting/ControllerScriptingInterface.cpp @@ -10,10 +10,13 @@ // #include +#include + #include "Application.h" +#include "devices/MotionTracker.h" #include "devices/SixenseManager.h" #include "ControllerScriptingInterface.h" -#include "devices/MotionTracker.h" + ControllerScriptingInterface::ControllerScriptingInterface() : _mouseCaptured(false), @@ -25,9 +28,13 @@ ControllerScriptingInterface::ControllerScriptingInterface() : void ControllerScriptingInterface::handleMetaEvent(HFMetaEvent* event) { if (event->type() == HFActionEvent::startType()) { - emitActionStartEvent(static_cast(event)); + emit actionStartEvent(static_cast(*event)); } else if (event->type() == HFActionEvent::endType()) { - emitActionEndEvent(static_cast(event)); + emit actionEndEvent(static_cast(*event)); + } else if (event->type() == HFCancelEvent::startType()) { + emit cancelStartEvent(); + } else if (event->type() == HFCancelEvent::endType()) { + emit cancelEndEvent(); } } diff --git a/interface/src/scripting/ControllerScriptingInterface.h b/interface/src/scripting/ControllerScriptingInterface.h index c38ac888e4..29921008e6 100644 --- a/interface/src/scripting/ControllerScriptingInterface.h +++ b/interface/src/scripting/ControllerScriptingInterface.h @@ -58,8 +58,6 @@ public: void emitKeyReleaseEvent(QKeyEvent* event) { emit keyReleaseEvent(KeyEvent(*event)); } void handleMetaEvent(HFMetaEvent* event); - void emitActionStartEvent(HFActionEvent* event) { emit actionStartEvent(*event); } - void emitActionEndEvent(HFActionEvent* event) { emit actionEndEvent(*event); } void emitMouseMoveEvent(QMouseEvent* event, unsigned int deviceID = 0) { emit mouseMoveEvent(MouseEvent(*event, deviceID)); } void emitMousePressEvent(QMouseEvent* event, unsigned int deviceID = 0) { emit mousePressEvent(MouseEvent(*event, deviceID)); } diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index cfb7d6bb89..1eff64eb42 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -96,6 +96,9 @@ signals: void actionStartEvent(const HFActionEvent& event); void actionEndEvent(const HFActionEvent& event); + + void cancelStartEvent(); + void cancelEndEvent(); void mouseMoveEvent(const MouseEvent& event, unsigned int deviceID = 0); void mousePressEvent(const MouseEvent& event, unsigned int deviceID = 0); diff --git a/libraries/script-engine/src/HFCancelEvent.cpp b/libraries/script-engine/src/HFCancelEvent.cpp new file mode 100644 index 0000000000..f6462eea49 --- /dev/null +++ b/libraries/script-engine/src/HFCancelEvent.cpp @@ -0,0 +1,28 @@ +// +// HFCancelEvent.cpp +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "HFCancelEvent.h" + +HFCancelEvent::HFCancelEvent(QEvent::Type type) : + HFMetaEvent(type) +{ + +} + +QEvent::Type HFCancelEvent::startType() { + static QEvent::Type startType = HFMetaEvent::newEventType(); + return startType; +} + +QEvent::Type HFCancelEvent::endType() { + static QEvent::Type endType = HFMetaEvent::newEventType(); + return endType; +} diff --git a/libraries/script-engine/src/HFCancelEvent.h b/libraries/script-engine/src/HFCancelEvent.h new file mode 100644 index 0000000000..8e3a761296 --- /dev/null +++ b/libraries/script-engine/src/HFCancelEvent.h @@ -0,0 +1,29 @@ +// +// HFCancelEvent.h +// script-engine/src +// +// Created by Stephen Birarda on 2014-10-27. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_HFCancelEvent_h +#define hifi_HFCancelEvent_h + +#include +#include + +#include "HFMetaEvent.h" + +class HFCancelEvent : public HFMetaEvent { +public: + HFCancelEvent() {}; + HFCancelEvent(QEvent::Type type); + + static QEvent::Type startType(); + static QEvent::Type endType(); +}; + +#endif // hifi_HFCancelEvent_h \ No newline at end of file From 04e7e979aa8b1a7b14f439c953208230aa9c2830 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 15:42:40 -0700 Subject: [PATCH 20/26] bubble up HFCancelEvent from ESC key --- interface/src/Application.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 02fdbbb534..9b83e6211c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -1101,9 +1102,15 @@ void Application::keyPressEvent(QKeyEvent* event) { case Qt::Key_Equal: _myAvatar->resetSize(); break; - case Qt::Key_Escape: + case Qt::Key_Escape: { OculusManager::abandonCalibration(); + + // this fires the HFCancelEvent + HFCancelEvent startCancelEvent(HFCancelEvent::startType()); + sendEvent(this, &startCancelEvent); + break; + } default: event->ignore(); break; @@ -1174,6 +1181,13 @@ void Application::keyReleaseEvent(QKeyEvent* event) { case Qt::Key_Alt: _myAvatar->clearDriveKeys(); break; + case Qt::Key_Escape: { + // this ends the HFCancelEvent + HFCancelEvent endCancelEvent(HFCancelEvent::endType()); + sendEvent(this, &endCancelEvent); + + break; + } default: event->ignore(); break; From 8a9f2d172a638affb774f923c23c853d6a776a63 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 15:51:59 -0700 Subject: [PATCH 21/26] rename HFCancelEvent to HFBackEvent, fire from joystick --- interface/src/Application.cpp | 10 +++++----- .../src/scripting/ControllerScriptingInterface.cpp | 6 +++--- .../src/scripting/JoystickScriptingInterface.cpp | 13 +++++++++++++ .../src/{HFCancelEvent.cpp => HFBackEvent.cpp} | 10 +++++----- .../src/{HFCancelEvent.h => HFBackEvent.h} | 14 +++++++------- 5 files changed, 33 insertions(+), 20 deletions(-) rename libraries/script-engine/src/{HFCancelEvent.cpp => HFBackEvent.cpp} (72%) rename libraries/script-engine/src/{HFCancelEvent.h => HFBackEvent.h} (66%) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9b83e6211c..b1f694671e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -56,7 +56,7 @@ #include #include #include -#include +#include #include #include #include @@ -1106,8 +1106,8 @@ void Application::keyPressEvent(QKeyEvent* event) { OculusManager::abandonCalibration(); // this fires the HFCancelEvent - HFCancelEvent startCancelEvent(HFCancelEvent::startType()); - sendEvent(this, &startCancelEvent); + HFBackEvent startBackEvent(HFBackEvent::startType()); + sendEvent(this, &startBackEvent); break; } @@ -1183,8 +1183,8 @@ void Application::keyReleaseEvent(QKeyEvent* event) { break; case Qt::Key_Escape: { // this ends the HFCancelEvent - HFCancelEvent endCancelEvent(HFCancelEvent::endType()); - sendEvent(this, &endCancelEvent); + HFBackEvent endBackEvent(HFBackEvent::endType()); + sendEvent(this, &endBackEvent); break; } diff --git a/interface/src/scripting/ControllerScriptingInterface.cpp b/interface/src/scripting/ControllerScriptingInterface.cpp index 090bbea66e..d2cc847ab5 100644 --- a/interface/src/scripting/ControllerScriptingInterface.cpp +++ b/interface/src/scripting/ControllerScriptingInterface.cpp @@ -10,7 +10,7 @@ // #include -#include +#include #include "Application.h" #include "devices/MotionTracker.h" @@ -31,9 +31,9 @@ void ControllerScriptingInterface::handleMetaEvent(HFMetaEvent* event) { emit actionStartEvent(static_cast(*event)); } else if (event->type() == HFActionEvent::endType()) { emit actionEndEvent(static_cast(*event)); - } else if (event->type() == HFCancelEvent::startType()) { + } else if (event->type() == HFBackEvent::startType()) { emit cancelStartEvent(); - } else if (event->type() == HFCancelEvent::endType()) { + } else if (event->type() == HFBackEvent::endType()) { emit cancelEndEvent(); } } diff --git a/interface/src/scripting/JoystickScriptingInterface.cpp b/interface/src/scripting/JoystickScriptingInterface.cpp index ced063c8fe..9375f80045 100644 --- a/interface/src/scripting/JoystickScriptingInterface.cpp +++ b/interface/src/scripting/JoystickScriptingInterface.cpp @@ -9,6 +9,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include #include #include @@ -17,6 +18,7 @@ #undef main #endif +#include #include #include "JoystickScriptingInterface.h" @@ -108,6 +110,17 @@ void JoystickScriptingInterface::update() { if (joystick) { joystick->handleButtonEvent(event.cbutton); } + + if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK) { + // this will either start or stop a cancel event + QEvent::Type backType = (event.type == SDL_CONTROLLERBUTTONDOWN) + ? HFBackEvent::startType() + : HFBackEvent::endType(); + HFBackEvent backEvent(backType); + + qApp->sendEvent(qApp, &backEvent); + } + } else if (event.type == SDL_CONTROLLERDEVICEADDED) { SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which); diff --git a/libraries/script-engine/src/HFCancelEvent.cpp b/libraries/script-engine/src/HFBackEvent.cpp similarity index 72% rename from libraries/script-engine/src/HFCancelEvent.cpp rename to libraries/script-engine/src/HFBackEvent.cpp index f6462eea49..c67b2e3431 100644 --- a/libraries/script-engine/src/HFCancelEvent.cpp +++ b/libraries/script-engine/src/HFBackEvent.cpp @@ -1,5 +1,5 @@ // -// HFCancelEvent.cpp +// HFBackEvent.cpp // script-engine/src // // Created by Stephen Birarda on 2014-10-27. @@ -9,20 +9,20 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "HFCancelEvent.h" +#include "HFBackEvent.h" -HFCancelEvent::HFCancelEvent(QEvent::Type type) : +HFBackEvent::HFBackEvent(QEvent::Type type) : HFMetaEvent(type) { } -QEvent::Type HFCancelEvent::startType() { +QEvent::Type HFBackEvent::startType() { static QEvent::Type startType = HFMetaEvent::newEventType(); return startType; } -QEvent::Type HFCancelEvent::endType() { +QEvent::Type HFBackEvent::endType() { static QEvent::Type endType = HFMetaEvent::newEventType(); return endType; } diff --git a/libraries/script-engine/src/HFCancelEvent.h b/libraries/script-engine/src/HFBackEvent.h similarity index 66% rename from libraries/script-engine/src/HFCancelEvent.h rename to libraries/script-engine/src/HFBackEvent.h index 8e3a761296..bb7b348ea7 100644 --- a/libraries/script-engine/src/HFCancelEvent.h +++ b/libraries/script-engine/src/HFBackEvent.h @@ -1,5 +1,5 @@ // -// HFCancelEvent.h +// HFBackEvent.h // script-engine/src // // Created by Stephen Birarda on 2014-10-27. @@ -9,21 +9,21 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#ifndef hifi_HFCancelEvent_h -#define hifi_HFCancelEvent_h +#ifndef hifi_HFBackEvent_h +#define hifi_HFBackEvent_h #include #include #include "HFMetaEvent.h" -class HFCancelEvent : public HFMetaEvent { +class HFBackEvent : public HFMetaEvent { public: - HFCancelEvent() {}; - HFCancelEvent(QEvent::Type type); + HFBackEvent() {}; + HFBackEvent(QEvent::Type type); static QEvent::Type startType(); static QEvent::Type endType(); }; -#endif // hifi_HFCancelEvent_h \ No newline at end of file +#endif // hifi_HFBackEvent_h \ No newline at end of file From 8e6b6d529439ec21d15f8ca6cc0b6638baab630d Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 15:58:42 -0700 Subject: [PATCH 22/26] have controller A button fire action at center of screen --- interface/src/Application.h | 2 ++ .../src/scripting/JoystickScriptingInterface.cpp | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/interface/src/Application.h b/interface/src/Application.h index e85c4f4db5..9930f5debc 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -283,6 +283,8 @@ public: PointShader& getPointShader() { return _pointShader; } FileLogger* getLogger() { return _logger; } + QPointF getViewportCenter() const + { return QPointF(_glWidget->getDeviceWidth() / 2.0f, _glWidget->getDeviceHeight() / 2.0f); } glm::vec2 getViewportDimensions() const { return glm::vec2(_glWidget->getDeviceWidth(), _glWidget->getDeviceHeight()); } NodeToJurisdictionMap& getVoxelServerJurisdictions() { return _voxelServerJurisdictions; } NodeToJurisdictionMap& getEntityServerJurisdictions() { return _entityServerJurisdictions; } diff --git a/interface/src/scripting/JoystickScriptingInterface.cpp b/interface/src/scripting/JoystickScriptingInterface.cpp index 9375f80045..f2c7ef4579 100644 --- a/interface/src/scripting/JoystickScriptingInterface.cpp +++ b/interface/src/scripting/JoystickScriptingInterface.cpp @@ -18,9 +18,12 @@ #undef main #endif +#include #include #include +#include "Application.h" + #include "JoystickScriptingInterface.h" #ifdef HAVE_SDL2 @@ -112,13 +115,24 @@ void JoystickScriptingInterface::update() { } if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK) { - // this will either start or stop a cancel event + // this will either start or stop a global back event QEvent::Type backType = (event.type == SDL_CONTROLLERBUTTONDOWN) ? HFBackEvent::startType() : HFBackEvent::endType(); HFBackEvent backEvent(backType); qApp->sendEvent(qApp, &backEvent); + } else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A) { + // this will either start or stop a global action event + QEvent::Type actionType = (event.type == SDL_CONTROLLERBUTTONDOWN) + ? HFActionEvent::startType() + : HFActionEvent::endType(); + + // global action events fire in the center of the screen + QPointF centerPoint = Application::getInstance()->getViewportCenter(); + HFActionEvent actionEvent(actionType, centerPoint); + + qApp->sendEvent(qApp, &actionEvent); } } else if (event.type == SDL_CONTROLLERDEVICEADDED) { From bb652c078491365a295a967f3f23d6ad348aeb8e Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 16:01:39 -0700 Subject: [PATCH 23/26] rename signal in AbstractControllerScriptingInterface from cancel to back --- interface/src/scripting/ControllerScriptingInterface.cpp | 4 ++-- .../script-engine/src/AbstractControllerScriptingInterface.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/interface/src/scripting/ControllerScriptingInterface.cpp b/interface/src/scripting/ControllerScriptingInterface.cpp index d2cc847ab5..a330cbc611 100644 --- a/interface/src/scripting/ControllerScriptingInterface.cpp +++ b/interface/src/scripting/ControllerScriptingInterface.cpp @@ -32,9 +32,9 @@ void ControllerScriptingInterface::handleMetaEvent(HFMetaEvent* event) { } else if (event->type() == HFActionEvent::endType()) { emit actionEndEvent(static_cast(*event)); } else if (event->type() == HFBackEvent::startType()) { - emit cancelStartEvent(); + emit backStartEvent(); } else if (event->type() == HFBackEvent::endType()) { - emit cancelEndEvent(); + emit backEndEvent(); } } diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index 1eff64eb42..11755add8e 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -97,8 +97,8 @@ signals: void actionStartEvent(const HFActionEvent& event); void actionEndEvent(const HFActionEvent& event); - void cancelStartEvent(); - void cancelEndEvent(); + void backStartEvent(); + void backEndEvent(); void mouseMoveEvent(const MouseEvent& event, unsigned int deviceID = 0); void mousePressEvent(const MouseEvent& event, unsigned int deviceID = 0); From 209c12cb1ca3e3d619606339de5b5b0d85b63ab0 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 16:52:22 -0700 Subject: [PATCH 24/26] repairs for texture replacement by name --- interface/src/renderer/GeometryCache.cpp | 19 +++++++++++++------ interface/src/renderer/GeometryCache.h | 3 +++ interface/src/renderer/TextureCache.h | 4 ---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/interface/src/renderer/GeometryCache.cpp b/interface/src/renderer/GeometryCache.cpp index 35765c50b8..f1d4d712e5 100644 --- a/interface/src/renderer/GeometryCache.cpp +++ b/interface/src/renderer/GeometryCache.cpp @@ -620,12 +620,19 @@ void NetworkGeometry::setTextureWithNameToURL(const QString& name, const QUrl& u NetworkMeshPart& part = mesh.parts[j]; QSharedPointer matchingTexture = QSharedPointer(); - if (part.diffuseTexture->getName() == name) { + if (part.diffuseTextureName == name) { part.diffuseTexture = Application::getInstance()->getTextureCache()->getTexture(url, DEFAULT_TEXTURE, - _geometry.meshes[i].isEye, QByteArray()); - part.diffuseTexture->setName(name); + _geometry.meshes[i].isEye, QByteArray()); part.diffuseTexture->setLoadPriorities(_loadPriorities); + } else if (part.normalTextureName == name) { + part.normalTexture = Application::getInstance()->getTextureCache()->getTexture(url, DEFAULT_TEXTURE, + false, QByteArray()); + part.normalTexture->setLoadPriorities(_loadPriorities); + } else if (part.specularTextureName == name) { + part.specularTexture = Application::getInstance()->getTextureCache()->getTexture(url, DEFAULT_TEXTURE, + false, QByteArray()); + part.specularTexture->setLoadPriorities(_loadPriorities); } } } @@ -746,21 +753,21 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) { networkPart.diffuseTexture = Application::getInstance()->getTextureCache()->getTexture( _textureBase.resolved(QUrl(part.diffuseTexture.filename)), DEFAULT_TEXTURE, mesh.isEye, part.diffuseTexture.content); - networkPart.diffuseTexture->setName(part.diffuseTexture.name); + networkPart.diffuseTextureName = part.diffuseTexture.name; networkPart.diffuseTexture->setLoadPriorities(_loadPriorities); } if (!part.normalTexture.filename.isEmpty()) { networkPart.normalTexture = Application::getInstance()->getTextureCache()->getTexture( _textureBase.resolved(QUrl(part.normalTexture.filename)), NORMAL_TEXTURE, false, part.normalTexture.content); - networkPart.normalTexture->setName(part.normalTexture.name); + networkPart.normalTextureName = part.normalTexture.name; networkPart.normalTexture->setLoadPriorities(_loadPriorities); } if (!part.specularTexture.filename.isEmpty()) { networkPart.specularTexture = Application::getInstance()->getTextureCache()->getTexture( _textureBase.resolved(QUrl(part.specularTexture.filename)), SPECULAR_TEXTURE, false, part.specularTexture.content); - networkPart.specularTexture->setName(part.specularTexture.name); + networkPart.specularTextureName = part.specularTexture.name; networkPart.specularTexture->setLoadPriorities(_loadPriorities); } networkMesh.parts.append(networkPart); diff --git a/interface/src/renderer/GeometryCache.h b/interface/src/renderer/GeometryCache.h index 461d622a02..4ed6c9943d 100644 --- a/interface/src/renderer/GeometryCache.h +++ b/interface/src/renderer/GeometryCache.h @@ -140,8 +140,11 @@ private: class NetworkMeshPart { public: + QString diffuseTextureName; QSharedPointer diffuseTexture; + QString normalTextureName; QSharedPointer normalTexture; + QString specularTextureName; QSharedPointer specularTexture; bool isTranslucent() const; diff --git a/interface/src/renderer/TextureCache.h b/interface/src/renderer/TextureCache.h index 29740621e3..fd9131fe23 100644 --- a/interface/src/renderer/TextureCache.h +++ b/interface/src/renderer/TextureCache.h @@ -145,9 +145,6 @@ public: /// Returns the lazily-computed average texture color. const QColor& getAverageColor() const { return _averageColor; } - - const QString& getName() const { return _name; } - void setName(const QString& name) { _name = name; } protected: @@ -159,7 +156,6 @@ protected: virtual void imageLoaded(const QImage& image); private: - QString _name; TextureType _type; bool _translucent; QColor _averageColor; From 80d90147268092f4f354dd9897642b96802e7cec Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 16:54:28 -0700 Subject: [PATCH 25/26] have spacebar fire an action event at center of screen --- interface/src/Application.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index b1f694671e..b54c3145e8 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1102,15 +1102,23 @@ void Application::keyPressEvent(QKeyEvent* event) { case Qt::Key_Equal: _myAvatar->resetSize(); break; + case Qt::Key_Space: { + // this starts an HFActionEvent + HFActionEvent startActionEvent(HFActionEvent::startType(), getViewportCenter()); + sendEvent(this, &startActionEvent); + + break; + } case Qt::Key_Escape: { OculusManager::abandonCalibration(); - // this fires the HFCancelEvent + // this starts the HFCancelEvent HFBackEvent startBackEvent(HFBackEvent::startType()); sendEvent(this, &startBackEvent); break; } + default: event->ignore(); break; @@ -1181,6 +1189,13 @@ void Application::keyReleaseEvent(QKeyEvent* event) { case Qt::Key_Alt: _myAvatar->clearDriveKeys(); break; + case Qt::Key_Space: { + // this ends the HFActionEvent + HFActionEvent endActionEvent(HFActionEvent::endType(), getViewportCenter()); + sendEvent(this, &endActionEvent); + + break; + } case Qt::Key_Escape: { // this ends the HFCancelEvent HFBackEvent endBackEvent(HFBackEvent::endType()); From e2536dd3f85527166c3726b12f59f2c9fcc07a26 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 27 Oct 2014 16:55:24 -0700 Subject: [PATCH 26/26] remove a debug line --- interface/src/renderer/GeometryCache.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interface/src/renderer/GeometryCache.cpp b/interface/src/renderer/GeometryCache.cpp index f1d4d712e5..c3b0a58263 100644 --- a/interface/src/renderer/GeometryCache.cpp +++ b/interface/src/renderer/GeometryCache.cpp @@ -405,7 +405,6 @@ void GeometryCache::renderGrid(int xDivisions, int yDivisions) { } QSharedPointer GeometryCache::getGeometry(const QUrl& url, const QUrl& fallback, bool delayLoad) { - qDebug() << "Getting a resource at" << url; return getResource(url, fallback, delayLoad).staticCast(); }