From b6471d887b9870d01f6e8bf23371c3482cb05679 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 18 Jan 2022 16:59:59 +0100 Subject: [PATCH] Add mouse cursors support for Mac --- Source/Engine/Platform/Mac/MacWindow.cpp | 67 +++++++++++++++++++++++- Source/Engine/Platform/Mac/MacWindow.h | 3 ++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Platform/Mac/MacWindow.cpp b/Source/Engine/Platform/Mac/MacWindow.cpp index d1eb7773a..cf045377a 100644 --- a/Source/Engine/Platform/Mac/MacWindow.cpp +++ b/Source/Engine/Platform/Mac/MacWindow.cpp @@ -10,6 +10,7 @@ #include "Engine/Input/Keyboard.h" #include "Engine/Graphics/RenderTask.h" #include +#include #include KeyboardKeys GetKey(NSEvent* event) @@ -308,12 +309,13 @@ Vector2 GetMousePosition(MacWindow* window, NSEvent* event) - (void)mouseEntered:(NSEvent*)event { IsMouseOver = true; + Window->SetIsMouseOver(true); } - (void)mouseExited:(NSEvent*)event { IsMouseOver = false; - Input::Mouse->OnMouseLeave(Window); + Window->SetIsMouseOver(false); } - (void)mouseDown:(NSEvent*)event @@ -483,6 +485,25 @@ void MacWindow::CheckForResize(float width, float height) } } +void MacWindow::SetIsMouseOver(bool value) +{ + if (_isMouseOver == value) + return; + _isMouseOver = value; + if (value) + { + // Refresh cursor typet + SetCursor(_cursor); + + } + else + { + Input::Mouse->OnMouseLeave(this); + if (_cursor == CursorType::Hidden) + [NSCursor unhide]; + } +} + void* MacWindow::GetNativePtr() const { return _window; @@ -592,4 +613,48 @@ void MacWindow::SetTitle(const StringView& title) [window setTitle:(__bridge NSString*)MacUtils::ToString(_title)]; } +void MacWindow::SetCursor(CursorType type) +{ + WindowBase::SetCursor(type); + if (!_isMouseOver) + return; + NSCursor* cursor = nullptr; + switch (type) + { + case CursorType::Cross: + cursor = [NSCursor crosshairCursor]; + break; + case CursorType::Hand: + cursor = [NSCursor pointingHandCursor]; + break; + case CursorType::IBeam: + cursor = [NSCursor IBeamCursor]; + break; + case CursorType::No: + cursor = [NSCursor operationNotAllowedCursor]; + break; + case CursorType::SizeAll: + case CursorType::SizeNESW: + case CursorType::SizeNWSE: + cursor = [NSCursor resizeUpDownCursor]; + break; + case CursorType::SizeNS: + cursor = [NSCursor resizeUpDownCursor]; + break; + case CursorType::SizeWE: + cursor = [NSCursor resizeLeftRightCursor]; + break; + case CursorType::Hidden: + [NSCursor hide]; + return; + default: + cursor = [NSCursor arrowCursor]; + break; + } + if (cursor) + { + [cursor set]; + } +} + #endif diff --git a/Source/Engine/Platform/Mac/MacWindow.h b/Source/Engine/Platform/Mac/MacWindow.h index 0693a8b5a..c5e98f643 100644 --- a/Source/Engine/Platform/Mac/MacWindow.h +++ b/Source/Engine/Platform/Mac/MacWindow.h @@ -16,6 +16,7 @@ private: Vector2 _clientSize; void* _window; + bool _isMouseOver = false; public: @@ -23,6 +24,7 @@ public: ~MacWindow(); void CheckForResize(float width, float height); + void SetIsMouseOver(bool value); public: @@ -40,6 +42,7 @@ public: void SetOpacity(float opacity) override; void Focus() override; void SetTitle(const StringView& title) override; + void SetCursor(CursorType type) override; }; #endif