From 7882d50c5fc81aa17860263e03e9724a89f294a5 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 28 Jan 2014 14:58:20 -0800 Subject: [PATCH] hacking on controller events --- libraries/script-engine/src/EventTypes.cpp | 86 ++++++++++++++++++++++ libraries/script-engine/src/EventTypes.h | 62 ++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 libraries/script-engine/src/EventTypes.cpp create mode 100644 libraries/script-engine/src/EventTypes.h diff --git a/libraries/script-engine/src/EventTypes.cpp b/libraries/script-engine/src/EventTypes.cpp new file mode 100644 index 0000000000..e11fa72495 --- /dev/null +++ b/libraries/script-engine/src/EventTypes.cpp @@ -0,0 +1,86 @@ +// +// EventTypes.cpp +// hifi +// +// Created by Brad Hefta-Gaub on 1/28/14. +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. +// +// Used to register meta-types with Qt for very various event types so that they can be exposed to our +// scripting engine + +#include "EventTypes.h" + +void registerEventTypes(QScriptEngine* engine) { + qScriptRegisterMetaType(engine, keyEventToScriptValue, keyEventFromScriptValue); + qScriptRegisterMetaType(engine, mouseEventToScriptValue, mouseEventFromScriptValue); + qScriptRegisterMetaType(engine, touchEventToScriptValue, touchEventFromScriptValue); + qScriptRegisterMetaType(engine, wheelEventToScriptValue, wheelEventFromScriptValue); +} + +QScriptValue keyEventToScriptValue(QScriptEngine* engine, const KeyEvent& event) { + QScriptValue obj = engine->newObject(); + /* + obj.setProperty("key", event.key()); + obj.setProperty("isAutoRepeat", event.isAutoRepeat()); + + bool isShifted = event.modifiers().testFlag(Qt::ShiftModifier); + bool isMeta = event.modifiers().testFlag(Qt::ControlModifier); + + obj.setProperty("isShifted", isShifted); + obj.setProperty("isMeta", isMeta); + */ + + return obj; +} + +void keyEventFromScriptValue(const QScriptValue &object, KeyEvent& event) { + // nothing for now... +} + +QScriptValue mouseEventToScriptValue(QScriptEngine* engine, const MouseEvent& event) { + QScriptValue obj = engine->newObject(); + obj.setProperty("x", event.x()); + obj.setProperty("y", event.y()); + return obj; +} + +void mouseEventFromScriptValue(const QScriptValue &object, MouseEvent& event) { + // nothing for now... +} + +QScriptValue touchEventToScriptValue(QScriptEngine* engine, const TouchEvent& event) { + QScriptValue obj = engine->newObject(); + + // convert the touch points into an average + const QList& tPoints = event.touchPoints(); + float touchAvgX = 0.0f; + float touchAvgY = 0.0f; + int numTouches = tPoints.count(); + if (numTouches > 1) { + for (int i = 0; i < numTouches; ++i) { + touchAvgX += tPoints[i].pos().x(); + touchAvgY += tPoints[i].pos().y(); + } + touchAvgX /= (float)(numTouches); + touchAvgY /= (float)(numTouches); + } + + obj.setProperty("averageX", touchAvgX); + obj.setProperty("averageY", touchAvgY); + return obj; +} + +void touchEventFromScriptValue(const QScriptValue &object, TouchEvent& event) { + // nothing for now... +} + +QScriptValue wheelEventToScriptValue(QScriptEngine* engine, const WheelEvent& event) { + QScriptValue obj = engine->newObject(); + obj.setProperty("x", event.x()); + obj.setProperty("y", event.y()); + return obj; +} + +void wheelEventFromScriptValue(const QScriptValue &object, WheelEvent& event) { + // nothing for now... +} diff --git a/libraries/script-engine/src/EventTypes.h b/libraries/script-engine/src/EventTypes.h new file mode 100644 index 0000000000..5ed014f09a --- /dev/null +++ b/libraries/script-engine/src/EventTypes.h @@ -0,0 +1,62 @@ +// +// EventTypes.h +// hifi +// +// Created by Brad Hefta-Gaub on 1/28/14. +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. +// + +#ifndef __hifi_EventTypes_h__ +#define __hifi_EventTypes_h__ + +#include + +#include + +#include +#include +#include +#include + +class KeyEvent { +public: + KeyEvent() { }; + KeyEvent(QKeyEvent* other) { }; +}; + +class MouseEvent { +public: + MouseEvent() { }; +}; + +class TouchEvent { +public: + TouchEvent() { }; +}; + +class WheelEvent { +public: + WheelEvent() { }; +}; + + +Q_DECLARE_METATYPE(KeyEvent) +Q_DECLARE_METATYPE(MouseEvent) +Q_DECLARE_METATYPE(TouchEvent) +Q_DECLARE_METATYPE(WheelEvent) + +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); + +#endif // __hifi_EventTypes_h__