move MouseEvent out of EventTypes into its own file

This commit is contained in:
Stephen Birarda 2014-10-27 14:30:20 -07:00
parent 5a390da509
commit 3aa7c3c7dc
5 changed files with 126 additions and 92 deletions

View file

@ -19,6 +19,7 @@
#include "EventTypes.h"
#include "KeyEvent.h"
#include "MouseEvent.h"
class AbstractInputController : public QObject {
Q_OBJECT

View file

@ -13,87 +13,18 @@
#include <RegisteredMetaTypes.h>
#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),

View file

@ -22,23 +22,6 @@
#include <QWheelEvent>
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);

View file

@ -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 <qscriptengine.h>
#include <qscriptvalue.h>
#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...
}

View file

@ -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 <QMouseEvent>
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