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 d9cf40baa3..cdd9337210 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 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 011519f6ee..cec8c520c6 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -55,6 +55,8 @@ #include #include #include +#include +#include #include #include #include @@ -67,14 +69,16 @@ #include #include "Application.h" -#include "ui/DataWebDialog.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 +91,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; @@ -136,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), @@ -172,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,6 +839,10 @@ bool Application::event(QEvent* event) { return false; } + if (HFActionEvent::types().contains(event->type())) { + _controllerScriptingInterface.handleMetaEvent(static_cast(event)); + } + return QApplication::event(event); } @@ -1109,9 +1117,23 @@ void Application::keyPressEvent(QKeyEvent* event) { case Qt::Key_Equal: _myAvatar->resetSize(); break; - case Qt::Key_Escape: - OculusManager::abandonCalibration(); + case Qt::Key_Space: { + // this starts an HFActionEvent + HFActionEvent startActionEvent(HFActionEvent::startType(), getViewportCenter()); + sendEvent(this, &startActionEvent); + break; + } + case Qt::Key_Escape: { + OculusManager::abandonCalibration(); + + // this starts the HFCancelEvent + HFBackEvent startBackEvent(HFBackEvent::startType()); + sendEvent(this, &startBackEvent); + + break; + } + default: event->ignore(); break; @@ -1182,6 +1204,20 @@ 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()); + sendEvent(this, &endBackEvent); + + break; + } default: event->ignore(); break; @@ -1254,6 +1290,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(this, &actionEvent); } else if (event->button() == Qt::RightButton) { // right click items here @@ -1274,12 +1314,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(this, &actionEvent); } } } diff --git a/interface/src/Application.h b/interface/src/Application.h index feb9ee8fc3..8cd321a6b3 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -284,6 +284,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/renderer/GeometryCache.cpp b/interface/src/renderer/GeometryCache.cpp index 107dac62f5..c3b0a58263 100644 --- a/interface/src/renderer/GeometryCache.cpp +++ b/interface/src/renderer/GeometryCache.cpp @@ -612,6 +612,31 @@ 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.diffuseTextureName == name) { + part.diffuseTexture = + Application::getInstance()->getTextureCache()->getTexture(url, DEFAULT_TEXTURE, + _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); + } + } + } +} + /// Reads geometry in a worker thread. class GeometryReader : public QRunnable { public: @@ -727,18 +752,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.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.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.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 e0ada10e5b..4ed6c9943d 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(); @@ -136,10 +138,13 @@ private: /// The state associated with a single mesh part. class NetworkMeshPart { -public: +public: + QString diffuseTextureName; QSharedPointer diffuseTexture; + QString normalTextureName; QSharedPointer normalTexture; + QString specularTextureName; QSharedPointer specularTexture; bool isTranslucent() const; diff --git a/interface/src/renderer/Model.h b/interface/src/renderer/Model.h index b6c9987807..7df8f27605 100644 --- a/interface/src/renderer/Model.h +++ b/interface/src/renderer/Model.h @@ -187,6 +187,9 @@ public: void inverseKinematics(int jointIndex, glm::vec3 position, const glm::quat& rotation, float priority); + Q_INVOKABLE void setTextureWithNameToURL(const QString& name, const QUrl& url) + { _geometry->setTextureWithNameToURL(name, url); } + protected: QSharedPointer _geometry; diff --git a/interface/src/renderer/TextureCache.h b/interface/src/renderer/TextureCache.h index 11ce312fa3..fd9131fe23 100644 --- a/interface/src/renderer/TextureCache.h +++ b/interface/src/renderer/TextureCache.h @@ -156,7 +156,6 @@ protected: virtual void imageLoaded(const QImage& image); private: - TextureType _type; bool _translucent; QColor _averageColor; diff --git a/interface/src/scripting/ControllerScriptingInterface.cpp b/interface/src/scripting/ControllerScriptingInterface.cpp index 1ca99ed2c5..bcfe844668 100644 --- a/interface/src/scripting/ControllerScriptingInterface.cpp +++ b/interface/src/scripting/ControllerScriptingInterface.cpp @@ -10,18 +10,33 @@ // #include +#include + #include "Application.h" +#include "devices/MotionTracker.h" #include "devices/SixenseManager.h" #include "ControllerScriptingInterface.h" -#include "devices/MotionTracker.h" + ControllerScriptingInterface::ControllerScriptingInterface() : _mouseCaptured(false), _touchCaptured(false), _wheelCaptured(false) { + } +void ControllerScriptingInterface::handleMetaEvent(HFMetaEvent* event) { + if (event->type() == HFActionEvent::startType()) { + emit actionStartEvent(static_cast(*event)); + } else if (event->type() == HFActionEvent::endType()) { + emit actionEndEvent(static_cast(*event)); + } else if (event->type() == HFBackEvent::startType()) { + emit backStartEvent(); + } else if (event->type() == HFBackEvent::endType()) { + emit backEndEvent(); + } +} const PalmData* ControllerScriptingInterface::getPrimaryPalm() const { int leftPalmIndex, rightPalmIndex; diff --git a/interface/src/scripting/ControllerScriptingInterface.h b/interface/src/scripting/ControllerScriptingInterface.h index 62ef2e9b24..29921008e6 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 { @@ -56,6 +56,8 @@ public: ControllerScriptingInterface(); void emitKeyPressEvent(QKeyEvent* event) { emit keyPressEvent(KeyEvent(*event)); } void emitKeyReleaseEvent(QKeyEvent* event) { emit keyReleaseEvent(KeyEvent(*event)); } + + void handleMetaEvent(HFMetaEvent* 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/interface/src/scripting/JoystickScriptingInterface.cpp b/interface/src/scripting/JoystickScriptingInterface.cpp index ced063c8fe..f2c7ef4579 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,8 +18,12 @@ #undef main #endif +#include +#include #include +#include "Application.h" + #include "JoystickScriptingInterface.h" #ifdef HAVE_SDL2 @@ -108,6 +113,28 @@ void JoystickScriptingInterface::update() { if (joystick) { joystick->handleButtonEvent(event.cbutton); } + + if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK) { + // 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) { SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which); diff --git a/interface/src/ui/overlays/ModelOverlay.cpp b/interface/src/ui/overlays/ModelOverlay.cpp index 12f54f02d9..0169bf40cd 100644 --- a/interface/src/ui/overlays/ModelOverlay.cpp +++ b/interface/src/ui/overlays/ModelOverlay.cpp @@ -102,6 +102,20 @@ 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; + + QMetaObject::invokeMethod(&_model, "setTextureWithNameToURL", Qt::AutoConnection, + Q_ARG(const QString&, key), + Q_ARG(const QUrl&, newTextureURL)); + } + } if (properties.property("position").isValid()) { _updateModel = true; 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; }; diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index 78e36a3740..11755add8e 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -17,7 +17,12 @@ #include #include -#include "EventTypes.h" +#include "HFActionEvent.h" +#include "KeyEvent.h" +#include "MouseEvent.h" +#include "SpatialEvent.h" +#include "TouchEvent.h" +#include "WheelEvent.h" class AbstractInputController : public QObject { Q_OBJECT @@ -88,6 +93,12 @@ public slots: signals: void keyPressEvent(const KeyEvent& event); void keyReleaseEvent(const KeyEvent& event); + + void actionStartEvent(const HFActionEvent& event); + void actionEndEvent(const HFActionEvent& event); + + void backStartEvent(); + void backEndEvent(); 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 de3ec231ae..ce3c82e121 100644 --- a/libraries/script-engine/src/EventTypes.cpp +++ b/libraries/script-engine/src/EventTypes.cpp @@ -9,640 +9,20 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include -#include +#include "HFActionEvent.h" +#include "KeyEvent.h" +#include "MouseEvent.h" +#include "SpatialEvent.h" +#include "TouchEvent.h" +#include "WheelEvent.h" + #include "EventTypes.h" - void registerEventTypes(QScriptEngine* engine) { - qScriptRegisterMetaType(engine, keyEventToScriptValue, keyEventFromScriptValue); - 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), - 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), - 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), - 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(), - 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, HFActionEvent::toScriptValue, HFActionEvent::fromScriptValue); + qScriptRegisterMetaType(engine, KeyEvent::toScriptValue, KeyEvent::fromScriptValue); + qScriptRegisterMetaType(engine, MouseEvent::toScriptValue, MouseEvent::fromScriptValue); + qScriptRegisterMetaType(engine, TouchEvent::toScriptValue, TouchEvent::fromScriptValue); + qScriptRegisterMetaType(engine, WheelEvent::toScriptValue, WheelEvent::fromScriptValue); + qScriptRegisterMetaType(engine, SpatialEvent::toScriptValue, SpatialEvent::fromScriptValue); } diff --git a/libraries/script-engine/src/EventTypes.h b/libraries/script-engine/src/EventTypes.h index 959292039b..fc808ea560 100644 --- a/libraries/script-engine/src/EventTypes.h +++ b/libraries/script-engine/src/EventTypes.h @@ -12,139 +12,8 @@ #ifndef hifi_EventTypes_h #define hifi_EventTypes_h -#include -#include - -#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(); - 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(); - 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(); - 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(); - SpatialEvent(const SpatialEvent& other); - - glm::vec3 locTranslation; - glm::quat locRotation; - glm::vec3 absTranslation; - glm::quat absRotation; - -private: -}; - - -Q_DECLARE_METATYPE(KeyEvent) -Q_DECLARE_METATYPE(MouseEvent) -Q_DECLARE_METATYPE(TouchEvent) -Q_DECLARE_METATYPE(WheelEvent) -Q_DECLARE_METATYPE(SpatialEvent) +#include 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); - -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); - -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/HFActionEvent.cpp b/libraries/script-engine/src/HFActionEvent.cpp new file mode 100644 index 0000000000..8a966a3fb3 --- /dev/null +++ b/libraries/script-engine/src/HFActionEvent.cpp @@ -0,0 +1,41 @@ +// +// HFActionEvent.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 "HFActionEvent.h" + +HFActionEvent::HFActionEvent(QEvent::Type type, const QPointF& localPosition) : + HFMetaEvent(type), + localPosition(localPosition) +{ + +} + +QEvent::Type HFActionEvent::startType() { + static QEvent::Type startType = HFMetaEvent::newEventType(); + return startType; +} + +QEvent::Type HFActionEvent::endType() { + static QEvent::Type endType = HFMetaEvent::newEventType(); + 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 new file mode 100644 index 0000000000..c094161053 --- /dev/null +++ b/libraries/script-engine/src/HFActionEvent.h @@ -0,0 +1,35 @@ +// +// HFActionEvent.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_HFActionEvent_h +#define hifi_HFActionEvent_h + +#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(); + + 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/HFBackEvent.cpp b/libraries/script-engine/src/HFBackEvent.cpp new file mode 100644 index 0000000000..c67b2e3431 --- /dev/null +++ b/libraries/script-engine/src/HFBackEvent.cpp @@ -0,0 +1,28 @@ +// +// HFBackEvent.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 "HFBackEvent.h" + +HFBackEvent::HFBackEvent(QEvent::Type type) : + HFMetaEvent(type) +{ + +} + +QEvent::Type HFBackEvent::startType() { + static QEvent::Type startType = HFMetaEvent::newEventType(); + return startType; +} + +QEvent::Type HFBackEvent::endType() { + static QEvent::Type endType = HFMetaEvent::newEventType(); + return endType; +} diff --git a/libraries/script-engine/src/HFBackEvent.h b/libraries/script-engine/src/HFBackEvent.h new file mode 100644 index 0000000000..bb7b348ea7 --- /dev/null +++ b/libraries/script-engine/src/HFBackEvent.h @@ -0,0 +1,29 @@ +// +// HFBackEvent.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_HFBackEvent_h +#define hifi_HFBackEvent_h + +#include +#include + +#include "HFMetaEvent.h" + +class HFBackEvent : public HFMetaEvent { +public: + HFBackEvent() {}; + HFBackEvent(QEvent::Type type); + + static QEvent::Type startType(); + static QEvent::Type endType(); +}; + +#endif // hifi_HFBackEvent_h \ No newline at end of file diff --git a/libraries/script-engine/src/HFMetaEvent.cpp b/libraries/script-engine/src/HFMetaEvent.cpp new file mode 100644 index 0000000000..f06c349996 --- /dev/null +++ b/libraries/script-engine/src/HFMetaEvent.cpp @@ -0,0 +1,20 @@ +// +// HFMetaEvent.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 "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/libraries/script-engine/src/HFMetaEvent.h b/libraries/script-engine/src/HFMetaEvent.h new file mode 100644 index 0000000000..2fd71b8a3b --- /dev/null +++ b/libraries/script-engine/src/HFMetaEvent.h @@ -0,0 +1,28 @@ +// +// HFMetaEvent.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_HFMetaEvent_h +#define hifi_HFMetaEvent_h + +#include + +class HFMetaEvent : public QEvent { +public: + HFMetaEvent() : QEvent(HFMetaEvent::newEventType()) {}; + 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 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; 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 diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 660a6ec560..fb98124fc9 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -35,13 +35,15 @@ #include "AnimationObject.h" #include "ArrayBufferViewClass.h" #include "DataViewClass.h" +#include "EventTypes.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; 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 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 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