Native handling of relative cursor coordinates for camera control

This commit is contained in:
Jack Andersen 2019-06-03 19:38:13 -10:00
parent 92723598fe
commit 12f4d5a267
5 changed files with 56 additions and 9 deletions

View File

@ -2,6 +2,7 @@
#include <Common/Math/MathUtil.h>
#include <Core/Render/CDrawUtil.h>
#include <Core/Render/CGraphics.h>
#include <Editor/MacOSExtras.h>
#include <QCursor>
@ -264,8 +265,13 @@ void CBasicViewport::ProcessInput()
if (IsMouseInputActive())
{
#ifdef __APPLE__
float XMovement = gpMouseDragCocoaEventFilter->claimX() * 0.01f;
float YMovement = gpMouseDragCocoaEventFilter->claimY() * 0.01f;
#else
float XMovement = (QCursor::pos().x() - mLastMousePos.x()) * 0.01f;
float YMovement = (QCursor::pos().y() - mLastMousePos.y()) * 0.01f;
#endif
if ((XMovement != 0) || (YMovement != 0))
{

25
src/Editor/MacOSExtras.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef MACOSEXTRAS_H
#define MACOSEXTRAS_H
#ifdef __APPLE__
#include <CoreGraphics/CGBase.h>
#include <QAbstractNativeEventFilter>
#include <QString>
void MacOSSetDarkAppearance();
QString MacOSPathToDolphinBinary();
class MouseDragCocoaEventFilter : public QAbstractNativeEventFilter
{
CGFloat mX = 0.0, mY = 0.0;
public:
bool nativeEventFilter(const QByteArray& eventType, void* message, long*) override;
CGFloat claimX() { CGFloat ret = mX; mX = 0.0; return ret; }
CGFloat claimY() { CGFloat ret = mY; mY = 0.0; return ret; }
};
extern MouseDragCocoaEventFilter* gpMouseDragCocoaEventFilter;
#endif // __APPLE__
#endif // MACOSEXTRAS_H

View File

@ -1,5 +1,5 @@
#import <AppKit/AppKit.h>
#include <QString>
#include "MacOSExtras.h"
void MacOSSetDarkAppearance()
{
@ -16,3 +16,22 @@ QString MacOSPathToDolphinBinary()
return QString::fromNSString(path) + QStringLiteral("/Contents/MacOS/Dolphin");
return QString();
}
/* Filter to accumulate relative coordinates of middle and right mouse drags for camera control. */
bool MouseDragCocoaEventFilter::nativeEventFilter(const QByteArray& eventType, void* message, long*)
{
if (eventType == "mac_generic_NSEvent")
{
NSEvent* event = static_cast<NSEvent*>(message);
NSEventType evType = event.type;
if (evType == NSEventTypeRightMouseDragged ||
(evType == NSEventTypeOtherMouseDragged && event.buttonNumber == 2))
{
mX += event.deltaX;
mY += event.deltaY;
}
}
return false;
}
MouseDragCocoaEventFilter* gpMouseDragCocoaEventFilter = nullptr;

View File

@ -1,4 +1,5 @@
#include "NDolphinIntegration.h"
#include "Editor/MacOSExtras.h"
#include "Editor/UICommon.h"
#include <QFileInfo>
@ -10,10 +11,6 @@
#undef MoveFile
#undef DeleteFile
#if defined(__APPLE__)
QString MacOSPathToDolphinBinary();
#endif
namespace NDolphinIntegration
{

View File

@ -1,5 +1,6 @@
#include "CEditorApplication.h"
#include "CUIRelay.h"
#include "MacOSExtras.h"
#include "UICommon.h"
#include <Common/Log.h>
@ -11,10 +12,6 @@
#include <QStyleFactory>
#include <QtGlobal>
#ifdef __APPLE__
void MacOSSetDarkAppearance();
#endif
// Redirect qDebug output to the log file
void QtLogRedirect(QtMsgType Type, const QMessageLogContext& /*rkContext*/, const QString& rkMessage)
{
@ -105,6 +102,9 @@ public:
SetupPalette();
#ifdef __APPLE__
MacOSSetDarkAppearance();
MouseDragCocoaEventFilter mouseDragCocoaEventFilter;
gpMouseDragCocoaEventFilter = &mouseDragCocoaEventFilter;
qApp->installNativeEventFilter(gpMouseDragCocoaEventFilter);
#endif
// Default OpenGL format