RenderViewport_mac.mm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <QAbstractNativeEventFilter>
  9. #include <QApplication>
  10. #include <Carbon/Carbon.h>
  11. #include <Cocoa/Cocoa.h>
  12. class HiddenMouseNativeEventFilter : public QAbstractNativeEventFilter
  13. {
  14. public:
  15. bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override
  16. {
  17. assert(eventType == "mac_generic_NSEvent");
  18. NSEvent *event = (NSEvent *)message;
  19. switch ([event type])
  20. {
  21. case NSEventTypeRightMouseDragged:
  22. sendFakeMouseMoveEvent([event deltaX], [event deltaY], Qt::RightButton);
  23. return true;
  24. case NSEventTypeOtherMouseDragged:
  25. sendFakeMouseMoveEvent([event deltaX], [event deltaY], Qt::MiddleButton);
  26. return true;
  27. default:
  28. return false;
  29. }
  30. }
  31. void sendFakeMouseMoveEvent(int deltaX, int deltaY, Qt::MouseButtons buttons)
  32. {
  33. assert(m_target);
  34. QMetaObject::invokeMethod(m_target, "InjectFakeMouseMove", Q_ARG(int, deltaX), Q_ARG(int, deltaY), Q_ARG(Qt::MouseButtons, buttons));
  35. }
  36. QObject *m_target = nullptr;
  37. };
  38. Q_GLOBAL_STATIC(HiddenMouseNativeEventFilter, eventFilter)
  39. void StopFixedCursorMode()
  40. {
  41. qApp->removeNativeEventFilter(eventFilter());
  42. eventFilter()->m_target = nullptr;
  43. CGAssociateMouseAndMouseCursorPosition(true);
  44. }
  45. void StartFixedCursorMode(QObject* viewport)
  46. {
  47. CGAssociateMouseAndMouseCursorPosition(false);
  48. eventFilter()->m_target = viewport;
  49. qApp->installNativeEventFilter(eventFilter());
  50. }