Showing the changes to libraries/script-engine/src/EventTypes.cpp (1 of 2 files). Show all
Unified · Split
| @@ -0,0 +1,86 @@ | ||
| 1 | +// | |
| 2 | +// EventTypes.cpp | |
| 3 | +// hifi | |
| 4 | +// | |
| 5 | +// Created by Brad Hefta-Gaub on 1/28/14. | |
| 6 | +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. | |
| 7 | +// | |
| 8 | +// Used to register meta-types with Qt for very various event types so that they can be exposed to our | |
| 9 | +// scripting engine | |
| 10 | + | |
| 11 | +#include "EventTypes.h" | |
| 12 | + | |
| 13 | +void registerEventTypes(QScriptEngine* engine) { | |
| 14 | + qScriptRegisterMetaType(engine, keyEventToScriptValue, keyEventFromScriptValue); | |
| 15 | + qScriptRegisterMetaType(engine, mouseEventToScriptValue, mouseEventFromScriptValue); | |
| 16 | + qScriptRegisterMetaType(engine, touchEventToScriptValue, touchEventFromScriptValue); | |
| 17 | + qScriptRegisterMetaType(engine, wheelEventToScriptValue, wheelEventFromScriptValue); | |
| 18 | +} | |
| 19 | + | |
| 20 | +QScriptValue keyEventToScriptValue(QScriptEngine* engine, const KeyEvent& event) { | |
| 21 | + QScriptValue obj = engine->newObject(); | |
| 22 | + /* | |
| 23 | + obj.setProperty("key", event.key()); | |
| 24 | + obj.setProperty("isAutoRepeat", event.isAutoRepeat()); | |
| 25 | + | |
| 26 | + bool isShifted = event.modifiers().testFlag(Qt::ShiftModifier); | |
| 27 | + bool isMeta = event.modifiers().testFlag(Qt::ControlModifier); | |
| 28 | + | |
| 29 | + obj.setProperty("isShifted", isShifted); | |
| 30 | + obj.setProperty("isMeta", isMeta); | |
| 31 | + */ | |
| 32 | + | |
| 33 | + return obj; | |
| 34 | +} | |
| 35 | + | |
| 36 | +void keyEventFromScriptValue(const QScriptValue &object, KeyEvent& event) { | |
| 37 | + // nothing for now... | |
| 38 | +} | |
| 39 | + | |
| 40 | +QScriptValue mouseEventToScriptValue(QScriptEngine* engine, const MouseEvent& event) { | |
| 41 | + QScriptValue obj = engine->newObject(); | |
| 42 | + obj.setProperty("x", event.x()); | |
| 43 | + obj.setProperty("y", event.y()); | |
| 44 | + return obj; | |
| 45 | +} | |
| 46 | + | |
| 47 | +void mouseEventFromScriptValue(const QScriptValue &object, MouseEvent& event) { | |
| 48 | + // nothing for now... | |
| 49 | +} | |
| 50 | + | |
| 51 | +QScriptValue touchEventToScriptValue(QScriptEngine* engine, const TouchEvent& event) { | |
| 52 | + QScriptValue obj = engine->newObject(); | |
| 53 | + | |
| 54 | + // convert the touch points into an average | |
| 55 | + const QList<QTouchEvent::TouchPoint>& tPoints = event.touchPoints(); | |
| 56 | + float touchAvgX = 0.0f; | |
| 57 | + float touchAvgY = 0.0f; | |
| 58 | + int numTouches = tPoints.count(); | |
| 59 | + if (numTouches > 1) { | |
| 60 | + for (int i = 0; i < numTouches; ++i) { | |
| 61 | + touchAvgX += tPoints[i].pos().x(); | |
| 62 | + touchAvgY += tPoints[i].pos().y(); | |
| 63 | + } | |
| 64 | + touchAvgX /= (float)(numTouches); | |
| 65 | + touchAvgY /= (float)(numTouches); | |
| 66 | + } | |
| 67 | + | |
| 68 | + obj.setProperty("averageX", touchAvgX); | |
| 69 | + obj.setProperty("averageY", touchAvgY); | |
| 70 | + return obj; | |
| 71 | +} | |
| 72 | + | |
| 73 | +void touchEventFromScriptValue(const QScriptValue &object, TouchEvent& event) { | |
| 74 | + // nothing for now... | |
| 75 | +} | |
| 76 | + | |
| 77 | +QScriptValue wheelEventToScriptValue(QScriptEngine* engine, const WheelEvent& event) { | |
| 78 | + QScriptValue obj = engine->newObject(); | |
| 79 | + obj.setProperty("x", event.x()); | |
| 80 | + obj.setProperty("y", event.y()); | |
| 81 | + return obj; | |
| 82 | +} | |
| 83 | + | |
| 84 | +void wheelEventFromScriptValue(const QScriptValue &object, WheelEvent& event) { | |
| 85 | + // nothing for now... | |
| 86 | +} | |