Showing the changes to libraries/script-engine/src/EventTypes.cpp (1 of 8 files). Show all
Unified · Split
| @@ -9,18 +9,29 @@ · 8 unmodified lines | ||
| 9 | 9 | // scripting engine |
| 10 | 10 | |
| 11 | 11 | #include <QDebug> |
| 12 | +#include <RegisteredMetaTypes.h> | |
| 12 | 13 | #include "EventTypes.h" |
| 13 | 14 | |
| 14 | 15 | |
| 15 | −KeyEvent::KeyEvent() { | |
| 16 | − key = 0; | |
| 17 | − text = QString(""); | |
| 18 | − isShifted = false; | |
| 19 | − isMeta = false; | |
| 20 | − isControl = false; | |
| 21 | − isValid = false; | |
| 16 | +void registerEventTypes(QScriptEngine* engine) { | |
| 17 | + qScriptRegisterMetaType(engine, keyEventToScriptValue, keyEventFromScriptValue); | |
| 18 | + qScriptRegisterMetaType(engine, mouseEventToScriptValue, mouseEventFromScriptValue); | |
| 19 | + qScriptRegisterMetaType(engine, touchEventToScriptValue, touchEventFromScriptValue); | |
| 20 | + qScriptRegisterMetaType(engine, wheelEventToScriptValue, wheelEventFromScriptValue); | |
| 22 | 21 | } |
| 23 | 22 | |
| 23 | +KeyEvent::KeyEvent() : | |
| 24 | + key(0), | |
| 25 | + text(""), | |
| 26 | + isShifted(false), | |
| 27 | + isControl(false), | |
| 28 | + isMeta(false), | |
| 29 | + isAlt(false), | |
| 30 | + isKeypad(false), | |
| 31 | + isValid(false) | |
| 32 | +{ | |
| 33 | +}; | |
| 34 | + | |
| 24 | 35 | |
| 25 | 36 | KeyEvent::KeyEvent(const QKeyEvent& event) { |
| 26 | 37 | key = event.key(); |
| @@ -73,43 +84,34 @@ KeyEvent::KeyEvent(const QKeyEvent& event) { · 46 unmodified lines | ||
| 73 | 84 | text = "DELETE"; |
| 74 | 85 | } else if (key == Qt::Key_Backspace) { |
| 75 | 86 | text = "BACKSPACE"; |
| 87 | + } else if (key == Qt::Key_Shift) { | |
| 88 | + text = "SHIFT"; | |
| 89 | + } else if (key == Qt::Key_Alt) { | |
| 90 | + text = "ALT"; | |
| 91 | + } else if (key == Qt::Key_Control) { | |
| 92 | + text = "CONTROL"; | |
| 93 | + } else if (key == Qt::Key_Meta) { | |
| 94 | + text = "META"; | |
| 95 | + } else if (key == Qt::Key_PageDown) { | |
| 96 | + text = "PAGE DOWN"; | |
| 97 | + } else if (key == Qt::Key_PageUp) { | |
| 98 | + text = "PAGE UP"; | |
| 99 | + } else if (key == Qt::Key_Home) { | |
| 100 | + text = "HOME"; | |
| 101 | + } else if (key == Qt::Key_End) { | |
| 102 | + text = "END"; | |
| 103 | + } else if (key == Qt::Key_Help) { | |
| 104 | + text = "HELP"; | |
| 76 | 105 | } |
| 77 | 106 | } |
| 78 | 107 | |
| 79 | −MouseEvent::MouseEvent(const QMouseEvent& event) { | |
| 80 | − x = event.x(); | |
| 81 | − y = event.y(); | |
| 82 | −} | |
| 83 | − | |
| 84 | −TouchEvent::TouchEvent(const QTouchEvent& event) { | |
| 85 | − // convert the touch points into an average | |
| 86 | − const QList<QTouchEvent::TouchPoint>& tPoints = event.touchPoints(); | |
| 87 | − float touchAvgX = 0.0f; | |
| 88 | − float touchAvgY = 0.0f; | |
| 89 | − int numTouches = tPoints.count(); | |
| 90 | − if (numTouches > 1) { | |
| 91 | − for (int i = 0; i < numTouches; ++i) { | |
| 92 | − touchAvgX += tPoints[i].pos().x(); | |
| 93 | − touchAvgY += tPoints[i].pos().y(); | |
| 94 | − } | |
| 95 | − touchAvgX /= (float)(numTouches); | |
| 96 | − touchAvgY /= (float)(numTouches); | |
| 97 | − } | |
| 98 | − x = touchAvgX; | |
| 99 | − y = touchAvgY; | |
| 100 | −} | |
| 101 | − | |
| 102 | −WheelEvent::WheelEvent(const QWheelEvent& event) { | |
| 103 | − x = event.x(); | |
| 104 | − y = event.y(); | |
| 105 | −} | |
| 106 | − | |
| 107 | − | |
| 108 | −void registerEventTypes(QScriptEngine* engine) { | |
| 109 | − qScriptRegisterMetaType(engine, keyEventToScriptValue, keyEventFromScriptValue); | |
| 110 | − qScriptRegisterMetaType(engine, mouseEventToScriptValue, mouseEventFromScriptValue); | |
| 111 | − qScriptRegisterMetaType(engine, touchEventToScriptValue, touchEventFromScriptValue); | |
| 112 | − qScriptRegisterMetaType(engine, wheelEventToScriptValue, wheelEventFromScriptValue); | |
| 108 | +bool KeyEvent::operator==(const KeyEvent& other) const { | |
| 109 | + return other.key == key | |
| 110 | + && other.isShifted == isShifted | |
| 111 | + && other.isControl == isControl | |
| 112 | + && other.isMeta == isMeta | |
| 113 | + && other.isAlt == isAlt | |
| 114 | + && other.isKeypad == isKeypad; | |
| 113 | 115 | } |
| 114 | 116 | |
| 115 | 117 | QScriptValue keyEventToScriptValue(QScriptEngine* engine, const KeyEvent& event) { |
| @@ -188,6 +190,24 @@ void keyEventFromScriptValue(const QScriptValue& object, KeyEvent& event) { · 72 unmodified lines | ||
| 188 | 190 | event.key = Qt::Key_Delete; |
| 189 | 191 | } else if (event.text.toUpper() == "BACKSPACE") { |
| 190 | 192 | event.key = Qt::Key_Backspace; |
| 193 | + } else if (event.text.toUpper() == "SHIFT") { | |
| 194 | + event.key = Qt::Key_Shift; | |
| 195 | + } else if (event.text.toUpper() == "ALT") { | |
| 196 | + event.key = Qt::Key_Alt; | |
| 197 | + } else if (event.text.toUpper() == "CONTROL") { | |
| 198 | + event.key = Qt::Key_Control; | |
| 199 | + } else if (event.text.toUpper() == "META") { | |
| 200 | + event.key = Qt::Key_Meta; | |
| 201 | + } else if (event.text.toUpper() == "PAGE DOWN") { | |
| 202 | + event.key = Qt::Key_PageDown; | |
| 203 | + } else if (event.text.toUpper() == "PAGE UP") { | |
| 204 | + event.key = Qt::Key_PageUp; | |
| 205 | + } else if (event.text.toUpper() == "HOME") { | |
| 206 | + event.key = Qt::Key_Home; | |
| 207 | + } else if (event.text.toUpper() == "END") { | |
| 208 | + event.key = Qt::Key_End; | |
| 209 | + } else if (event.text.toUpper() == "HELP") { | |
| 210 | + event.key = Qt::Key_Help; | |
| 191 | 211 | } else { |
| 192 | 212 | event.key = event.text.at(0).unicode(); |
| 193 | 213 | } |
| @@ -224,10 +244,67 @@ void keyEventFromScriptValue(const QScriptValue& object, KeyEvent& event) { · 30 unmodified lines | ||
| 224 | 244 | } |
| 225 | 245 | } |
| 226 | 246 | |
| 247 | +MouseEvent::MouseEvent() : | |
| 248 | + x(0), | |
| 249 | + y(0), | |
| 250 | + isLeftButton(false), | |
| 251 | + isRightButton(false), | |
| 252 | + isMiddleButton(false), | |
| 253 | + isShifted(false), | |
| 254 | + isControl(false), | |
| 255 | + isMeta(false), | |
| 256 | + isAlt(false) | |
| 257 | +{ | |
| 258 | +}; | |
| 259 | + | |
| 260 | + | |
| 261 | +MouseEvent::MouseEvent(const QMouseEvent& event) { | |
| 262 | + x = event.x(); | |
| 263 | + y = event.y(); | |
| 264 | + | |
| 265 | + // single button that caused the event | |
| 266 | + switch (event.button()) { | |
| 267 | + case Qt::LeftButton: | |
| 268 | + button = "LEFT"; | |
| 269 | + isLeftButton = true; | |
| 270 | + break; | |
| 271 | + case Qt::RightButton: | |
| 272 | + button = "RIGHT"; | |
| 273 | + isRightButton = true; | |
| 274 | + break; | |
| 275 | + case Qt::MiddleButton: | |
| 276 | + button = "MIDDLE"; | |
| 277 | + isMiddleButton = true; | |
| 278 | + break; | |
| 279 | + default: | |
| 280 | + button = "NONE"; | |
| 281 | + break; | |
| 282 | + } | |
| 283 | + // button pressed state | |
| 284 | + isLeftButton = isLeftButton || (event.buttons().testFlag(Qt::LeftButton)); | |
| 285 | + isRightButton = isRightButton || (event.buttons().testFlag(Qt::RightButton)); | |
| 286 | + isMiddleButton = isMiddleButton || (event.buttons().testFlag(Qt::MiddleButton)); | |
| 287 | + | |
| 288 | + // keyboard modifiers | |
| 289 | + isShifted = event.modifiers().testFlag(Qt::ShiftModifier); | |
| 290 | + isMeta = event.modifiers().testFlag(Qt::MetaModifier); | |
| 291 | + isControl = event.modifiers().testFlag(Qt::ControlModifier); | |
| 292 | + isAlt = event.modifiers().testFlag(Qt::AltModifier); | |
| 293 | +} | |
| 294 | + | |
| 227 | 295 | QScriptValue mouseEventToScriptValue(QScriptEngine* engine, const MouseEvent& event) { |
| 228 | 296 | QScriptValue obj = engine->newObject(); |
| 229 | 297 | obj.setProperty("x", event.x); |
| 230 | 298 | obj.setProperty("y", event.y); |
| 299 | + obj.setProperty("button", event.button); | |
| 300 | + obj.setProperty("isLeftButton", event.isLeftButton); | |
| 301 | + obj.setProperty("isRightButton", event.isRightButton); | |
| 302 | + obj.setProperty("isMiddleButton", event.isMiddleButton); | |
| 303 | + obj.setProperty("isShifted", event.isShifted); | |
| 304 | + obj.setProperty("isMeta", event.isMeta); | |
| 305 | + obj.setProperty("isControl", event.isControl); | |
| 306 | + obj.setProperty("isAlt", event.isAlt); | |
| 307 | + | |
| 231 | 308 | return obj; |
| 232 | 309 | } |
| 233 | 310 | |
| @@ -235,10 +312,76 @@ void mouseEventFromScriptValue(const QScriptValue& object, MouseEvent& event) { · 1 unmodified line | ||
| 235 | 312 | // nothing for now... |
| 236 | 313 | } |
| 237 | 314 | |
| 315 | +TouchEvent::TouchEvent() : | |
| 316 | + x(0), | |
| 317 | + y(0), | |
| 318 | + isPressed(false), | |
| 319 | + isMoved(false), | |
| 320 | + isStationary(false), | |
| 321 | + isReleased(false), | |
| 322 | + isShifted(false), | |
| 323 | + isControl(false), | |
| 324 | + isMeta(false), | |
| 325 | + isAlt(false), | |
| 326 | + points() | |
| 327 | +{ | |
| 328 | +}; | |
| 329 | + | |
| 330 | + | |
| 331 | +TouchEvent::TouchEvent(const QTouchEvent& event) { | |
| 332 | + // convert the touch points into an average | |
| 333 | + const QList<QTouchEvent::TouchPoint>& tPoints = event.touchPoints(); | |
| 334 | + float touchAvgX = 0.0f; | |
| 335 | + float touchAvgY = 0.0f; | |
| 336 | + int numTouches = tPoints.count(); | |
| 337 | + if (numTouches > 1) { | |
| 338 | + for (int i = 0; i < numTouches; ++i) { | |
| 339 | + touchAvgX += tPoints[i].pos().x(); | |
| 340 | + touchAvgY += tPoints[i].pos().y(); | |
| 341 | + | |
| 342 | + // add it to our points vector | |
| 343 | + glm::vec2 thisPoint(tPoints[i].pos().x(), tPoints[i].pos().y()); | |
| 344 | + points << thisPoint; | |
| 345 | + } | |
| 346 | + touchAvgX /= (float)(numTouches); | |
| 347 | + touchAvgY /= (float)(numTouches); | |
| 348 | + } | |
| 349 | + x = touchAvgX; | |
| 350 | + y = touchAvgY; | |
| 351 | + | |
| 352 | + isPressed = event.touchPointStates().testFlag(Qt::TouchPointPressed); | |
| 353 | + isMoved = event.touchPointStates().testFlag(Qt::TouchPointMoved); | |
| 354 | + isStationary = event.touchPointStates().testFlag(Qt::TouchPointStationary); | |
| 355 | + isReleased = event.touchPointStates().testFlag(Qt::TouchPointReleased); | |
| 356 | + | |
| 357 | + // keyboard modifiers | |
| 358 | + isShifted = event.modifiers().testFlag(Qt::ShiftModifier); | |
| 359 | + isMeta = event.modifiers().testFlag(Qt::MetaModifier); | |
| 360 | + isControl = event.modifiers().testFlag(Qt::ControlModifier); | |
| 361 | + isAlt = event.modifiers().testFlag(Qt::AltModifier); | |
| 362 | +} | |
| 363 | + | |
| 238 | 364 | QScriptValue touchEventToScriptValue(QScriptEngine* engine, const TouchEvent& event) { |
| 239 | 365 | QScriptValue obj = engine->newObject(); |
| 240 | 366 | obj.setProperty("x", event.x); |
| 241 | 367 | obj.setProperty("y", event.y); |
| 368 | + obj.setProperty("isPressed", event.isPressed); | |
| 369 | + obj.setProperty("isMoved", event.isMoved); | |
| 370 | + obj.setProperty("isStationary", event.isStationary); | |
| 371 | + obj.setProperty("isReleased", event.isReleased); | |
| 372 | + obj.setProperty("isShifted", event.isShifted); | |
| 373 | + obj.setProperty("isMeta", event.isMeta); | |
| 374 | + obj.setProperty("isControl", event.isControl); | |
| 375 | + obj.setProperty("isAlt", event.isAlt); | |
| 376 | + | |
| 377 | + QScriptValue pointsObj = engine->newArray(); | |
| 378 | + int index = 0; | |
| 379 | + foreach (glm::vec2 point, event.points) { | |
| 380 | + QScriptValue thisPoint = vec2toScriptValue(engine, point); | |
| 381 | + pointsObj.setProperty(index, thisPoint); | |
| 382 | + index++; | |
| 383 | + } | |
| 384 | + obj.setProperty("points", pointsObj); | |
| 242 | 385 | return obj; |
| 243 | 386 | } |
| 244 | 387 | |
| @@ -246,10 +389,58 @@ void touchEventFromScriptValue(const QScriptValue& object, TouchEvent& event) { · 1 unmodified line | ||
| 246 | 389 | // nothing for now... |
| 247 | 390 | } |
| 248 | 391 | |
| 392 | +WheelEvent::WheelEvent() : | |
| 393 | + x(0), | |
| 394 | + y(0), | |
| 395 | + delta(0), | |
| 396 | + orientation("UNKNOwN"), | |
| 397 | + isLeftButton(false), | |
| 398 | + isRightButton(false), | |
| 399 | + isMiddleButton(false), | |
| 400 | + isShifted(false), | |
| 401 | + isControl(false), | |
| 402 | + isMeta(false), | |
| 403 | + isAlt(false) | |
| 404 | +{ | |
| 405 | +}; | |
| 406 | + | |
| 407 | +WheelEvent::WheelEvent(const QWheelEvent& event) { | |
| 408 | + x = event.x(); | |
| 409 | + y = event.y(); | |
| 410 | + | |
| 411 | + delta = event.delta(); | |
| 412 | + if (event.orientation() == Qt::Horizontal) { | |
| 413 | + orientation = "HORIZONTAL"; | |
| 414 | + } else { | |
| 415 | + orientation = "VERTICAL"; | |
| 416 | + } | |
| 417 | + | |
| 418 | + // button pressed state | |
| 419 | + isLeftButton = (event.buttons().testFlag(Qt::LeftButton)); | |
| 420 | + isRightButton = (event.buttons().testFlag(Qt::RightButton)); | |
| 421 | + isMiddleButton = (event.buttons().testFlag(Qt::MiddleButton)); | |
| 422 | + | |
| 423 | + // keyboard modifiers | |
| 424 | + isShifted = event.modifiers().testFlag(Qt::ShiftModifier); | |
| 425 | + isMeta = event.modifiers().testFlag(Qt::MetaModifier); | |
| 426 | + isControl = event.modifiers().testFlag(Qt::ControlModifier); | |
| 427 | + isAlt = event.modifiers().testFlag(Qt::AltModifier); | |
| 428 | +} | |
| 429 | + | |
| 430 | + | |
| 249 | 431 | QScriptValue wheelEventToScriptValue(QScriptEngine* engine, const WheelEvent& event) { |
| 250 | 432 | QScriptValue obj = engine->newObject(); |
| 251 | 433 | obj.setProperty("x", event.x); |
| 252 | 434 | obj.setProperty("y", event.y); |
| 435 | + obj.setProperty("delta", event.delta); | |
| 436 | + obj.setProperty("orientation", event.orientation); | |
| 437 | + obj.setProperty("isLeftButton", event.isLeftButton); | |
| 438 | + obj.setProperty("isRightButton", event.isRightButton); | |
| 439 | + obj.setProperty("isMiddleButton", event.isMiddleButton); | |
| 440 | + obj.setProperty("isShifted", event.isShifted); | |
| 441 | + obj.setProperty("isMeta", event.isMeta); | |
| 442 | + obj.setProperty("isControl", event.isControl); | |
| 443 | + obj.setProperty("isAlt", event.isAlt); | |
| 253 | 444 | return obj; |
| 254 | 445 | } |
| 255 | 446 | |