From 6664972fbf9bcfbf32ce11b409cc929aa70a2695 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 15 Jul 2023 11:22:00 +0200 Subject: [PATCH 1/6] Add NvCloth for macOS --- .../Platforms/Mac/Binaries/ThirdParty/ARM64/libNvCloth.a | 3 +++ .../Platforms/Mac/Binaries/ThirdParty/x64/libNvCloth.a | 3 +++ Source/ThirdParty/NvCloth/NvCloth.Build.cs | 2 ++ Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs | 9 +++++++++ 4 files changed, 17 insertions(+) create mode 100644 Source/Platforms/Mac/Binaries/ThirdParty/ARM64/libNvCloth.a create mode 100644 Source/Platforms/Mac/Binaries/ThirdParty/x64/libNvCloth.a diff --git a/Source/Platforms/Mac/Binaries/ThirdParty/ARM64/libNvCloth.a b/Source/Platforms/Mac/Binaries/ThirdParty/ARM64/libNvCloth.a new file mode 100644 index 000000000..569db20de --- /dev/null +++ b/Source/Platforms/Mac/Binaries/ThirdParty/ARM64/libNvCloth.a @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4ac9b222da70871221c00cb4a143ae9a6400244e92f321f2cc1313aa999f9bc9 +size 301400 diff --git a/Source/Platforms/Mac/Binaries/ThirdParty/x64/libNvCloth.a b/Source/Platforms/Mac/Binaries/ThirdParty/x64/libNvCloth.a new file mode 100644 index 000000000..69e23cabf --- /dev/null +++ b/Source/Platforms/Mac/Binaries/ThirdParty/x64/libNvCloth.a @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8ca814f6aae7ee644da11440dfa5dacc0c70cb8ca25f6de70f6e6983f36bb907 +size 367504 diff --git a/Source/ThirdParty/NvCloth/NvCloth.Build.cs b/Source/ThirdParty/NvCloth/NvCloth.Build.cs index ea2bc02c8..3be1e903e 100644 --- a/Source/ThirdParty/NvCloth/NvCloth.Build.cs +++ b/Source/ThirdParty/NvCloth/NvCloth.Build.cs @@ -35,6 +35,8 @@ public class NvCloth : DepsModule case TargetPlatform.PS4: case TargetPlatform.PS5: case TargetPlatform.Android: + case TargetPlatform.Mac: + case TargetPlatform.iOS: libName = "NvCloth"; break; case TargetPlatform.Switch: diff --git a/Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs b/Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs index 7288add63..23f4f6a27 100644 --- a/Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs +++ b/Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs @@ -82,6 +82,10 @@ namespace Flax.Deps.Dependencies case TargetPlatform.Android: Build(options, platform, TargetArchitecture.ARM64); break; + case TargetPlatform.Mac: + Build(options, platform, TargetArchitecture.x64); + Build(options, platform, TargetArchitecture.ARM64); + break; } } @@ -140,6 +144,11 @@ namespace Flax.Deps.Dependencies envVars.Add("PM_ANDROIDNDK_PATH", AndroidNdk.Instance.RootPath); } break; + case TargetPlatform.Mac: + cmakeArgs += " -DTARGET_BUILD_PLATFORM=mac"; + cmakeName = "mac"; + binariesPrefix = "lib"; + break; default: throw new InvalidPlatformException(platform); } var cmakeFolder = Path.Combine(nvCloth, "compiler", "cmake", cmakeName); From 18fb7fb849eaa52254e3f7e192b9cfb6213115df Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 15 Jul 2023 11:27:26 +0200 Subject: [PATCH 2/6] Add NvCloth for iOS --- Source/Engine/Graphics/Models/SkinnedModelLOD.cpp | 1 + .../Platforms/iOS/Binaries/ThirdParty/ARM64/libNvCloth.a | 3 +++ Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs | 8 ++++++++ 3 files changed, 12 insertions(+) create mode 100644 Source/Platforms/iOS/Binaries/ThirdParty/ARM64/libNvCloth.a diff --git a/Source/Engine/Graphics/Models/SkinnedModelLOD.cpp b/Source/Engine/Graphics/Models/SkinnedModelLOD.cpp index 0e2b60aea..ada7d89f2 100644 --- a/Source/Engine/Graphics/Models/SkinnedModelLOD.cpp +++ b/Source/Engine/Graphics/Models/SkinnedModelLOD.cpp @@ -3,6 +3,7 @@ #include "SkinnedModelLOD.h" #include "MeshDeformation.h" #include "Engine/Core/Log.h" +#include "Engine/Core/Math/Transform.h" #include "Engine/Graphics/GPUDevice.h" #include "Engine/Content/Assets/Model.h" #include "Engine/Serialization/MemoryReadStream.h" diff --git a/Source/Platforms/iOS/Binaries/ThirdParty/ARM64/libNvCloth.a b/Source/Platforms/iOS/Binaries/ThirdParty/ARM64/libNvCloth.a new file mode 100644 index 000000000..63187c05f --- /dev/null +++ b/Source/Platforms/iOS/Binaries/ThirdParty/ARM64/libNvCloth.a @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5257892706e3ec0360fc4a323f09c0b30fa3991f4b9d4bce56a77f2b5fe0eb6 +size 300696 diff --git a/Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs b/Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs index 23f4f6a27..b096eef24 100644 --- a/Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs +++ b/Source/Tools/Flax.Build/Deps/Dependencies/NvCloth.cs @@ -86,6 +86,9 @@ namespace Flax.Deps.Dependencies Build(options, platform, TargetArchitecture.x64); Build(options, platform, TargetArchitecture.ARM64); break; + case TargetPlatform.iOS: + Build(options, platform, TargetArchitecture.ARM64); + break; } } @@ -149,6 +152,11 @@ namespace Flax.Deps.Dependencies cmakeName = "mac"; binariesPrefix = "lib"; break; + case TargetPlatform.iOS: + cmakeArgs += " -DTARGET_BUILD_PLATFORM=ios"; + cmakeName = "ios"; + binariesPrefix = "lib"; + break; default: throw new InvalidPlatformException(platform); } var cmakeFolder = Path.Combine(nvCloth, "compiler", "cmake", cmakeName); From a7e436412c8c937a88d35446ed9e1ef2846dfefe Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 15 Jul 2023 14:01:56 +0200 Subject: [PATCH 3/6] Fix mouse cursor setting on macOS to properly handle screen scale --- Source/Engine/Platform/Mac/MacPlatform.cpp | 7 ++++--- Source/Engine/Platform/Mac/MacWindow.cpp | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Platform/Mac/MacPlatform.cpp b/Source/Engine/Platform/Mac/MacPlatform.cpp index 04f928bba..15b526f02 100644 --- a/Source/Engine/Platform/Mac/MacPlatform.cpp +++ b/Source/Engine/Platform/Mac/MacPlatform.cpp @@ -301,15 +301,16 @@ Float2 MacPlatform::GetMousePosition() CGEventRef event = CGEventCreate(nullptr); CGPoint cursor = CGEventGetLocation(event); CFRelease(event); - return Float2((float)cursor.x, (float)cursor.y); + return Float2((float)cursor.x, (float)cursor.y) * MacPlatform::ScreenScale; } void MacPlatform::SetMousePosition(const Float2& pos) { CGPoint cursor; - cursor.x = (CGFloat)pos.X; - cursor.y = (CGFloat)pos.Y; + cursor.x = (CGFloat)(pos.X / MacPlatform::ScreenScale); + cursor.y = (CGFloat)(pos.Y / MacPlatform::ScreenScale); CGWarpMouseCursorPosition(cursor); + CGAssociateMouseAndMouseCursorPosition(true); } Float2 MacPlatform::GetDesktopSize() diff --git a/Source/Engine/Platform/Mac/MacWindow.cpp b/Source/Engine/Platform/Mac/MacWindow.cpp index 90bb07279..c55f36c7c 100644 --- a/Source/Engine/Platform/Mac/MacWindow.cpp +++ b/Source/Engine/Platform/Mac/MacWindow.cpp @@ -643,7 +643,6 @@ MacWindow::MacWindow(const CreateWindowSettings& settings) // TODO: impl StartPosition for MacWindow // TODO: impl Fullscreen for MacWindow // TODO: impl ShowInTaskbar for MacWindow - // TODO: impl AllowInput for MacWindow // TODO: impl IsTopmost for MacWindow } From 8f4ada25555c0dbc181e38b15178a1cb36a6d1df Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 15 Jul 2023 14:42:54 +0200 Subject: [PATCH 4/6] Add macOS message box with buttons --- Source/Engine/Platform/Mac/MacPlatform.cpp | 80 +++++++++++++++++++--- 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/Source/Engine/Platform/Mac/MacPlatform.cpp b/Source/Engine/Platform/Mac/MacPlatform.cpp index 15b526f02..46b978e1d 100644 --- a/Source/Engine/Platform/Mac/MacPlatform.cpp +++ b/Source/Engine/Platform/Mac/MacPlatform.cpp @@ -56,36 +56,94 @@ DialogResult MessageBox::Show(Window* parent, const StringView& text, const Stri { if (CommandLine::Options.Headless) return DialogResult::None; - CFStringRef textRef = AppleUtils::ToString(text); - CFStringRef captionRef = AppleUtils::ToString(caption); - CFOptionFlags flags = 0; + NSAlert* alert = [[NSAlert alloc] init]; + ASSERT(alert); switch (buttons) { case MessageBoxButtons::AbortRetryIgnore: + [alert addButtonWithTitle:@"Abort"]; + [alert addButtonWithTitle:@"Retry"]; + [alert addButtonWithTitle:@"Ignore"]; + break; + case MessageBoxButtons::OK: + [alert addButtonWithTitle:@"OK"]; + break; case MessageBoxButtons::OKCancel: + [alert addButtonWithTitle:@"OK"]; + [alert addButtonWithTitle:@"Cancel"]; + break; case MessageBoxButtons::RetryCancel: + [alert addButtonWithTitle:@"Retry"]; + [alert addButtonWithTitle:@"Cancel"]; + break; case MessageBoxButtons::YesNo: + [alert addButtonWithTitle:@"Yes"]; + [alert addButtonWithTitle:@"No"]; + break; case MessageBoxButtons::YesNoCancel: - flags |= kCFUserNotificationCancelResponse; + [alert addButtonWithTitle:@"Yes"]; + [alert addButtonWithTitle:@"No"]; + [alert addButtonWithTitle:@"Cancel"]; break; } switch (icon) { case MessageBoxIcon::Information: - flags |= kCFUserNotificationNoteAlertLevel; + [alert setAlertStyle:NSAlertStyleCritical]; break; case MessageBoxIcon::Error: case MessageBoxIcon::Stop: - flags |= kCFUserNotificationStopAlertLevel; + [alert setAlertStyle:NSAlertStyleInformational]; break; case MessageBoxIcon::Warning: - flags |= kCFUserNotificationCautionAlertLevel; + [alert setAlertStyle:NSAlertStyleWarning]; break; } - SInt32 result = CFUserNotificationDisplayNotice(0, flags, nullptr, nullptr, nullptr, captionRef, textRef, nullptr); - CFRelease(captionRef); - CFRelease(textRef); - return DialogResult::OK; + [alert setMessageText:(NSString*)AppleUtils::ToString(caption)]; + [alert setInformativeText:(NSString*)AppleUtils::ToString(text)]; + NSInteger button = [alert runModal]; + DialogResult result = DialogResult::OK; + switch (buttons) + { + case MessageBoxButtons::AbortRetryIgnore: + if (button == NSAlertFirstButtonReturn) + result = DialogResult::Abort; + else if (button == NSAlertSecondButtonReturn) + result = DialogResult::Retry; + else + result = DialogResult::Ignore; + break; + case MessageBoxButtons::OK: + result = DialogResult::OK; + break; + case MessageBoxButtons::OKCancel: + if (button == NSAlertFirstButtonReturn) + result = DialogResult::OK; + else + result = DialogResult::Cancel; + break; + case MessageBoxButtons::RetryCancel: + if (button == NSAlertFirstButtonReturn) + result = DialogResult::Retry; + else + result = DialogResult::Cancel; + break; + case MessageBoxButtons::YesNo: + if (button == NSAlertFirstButtonReturn) + result = DialogResult::Yes; + else + result = DialogResult::No; + break; + case MessageBoxButtons::YesNoCancel: + if (button == NSAlertFirstButtonReturn) + result = DialogResult::Yes; + else if (button == NSAlertSecondButtonReturn) + result = DialogResult::No; + else + result = DialogResult::Cancel; + break; + } + return result; } Float2 AppleUtils::PosToCoca(const Float2& pos) From 4a0235785b140f5c034e52aa6f66628a4a3ac881 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 15 Jul 2023 15:00:55 +0200 Subject: [PATCH 5/6] Add control/command/option keys handling on macOS --- Source/Engine/Platform/Mac/MacWindow.cpp | 29 ++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Platform/Mac/MacWindow.cpp b/Source/Engine/Platform/Mac/MacWindow.cpp index c55f36c7c..a52c11324 100644 --- a/Source/Engine/Platform/Mac/MacWindow.cpp +++ b/Source/Engine/Platform/Mac/MacWindow.cpp @@ -75,8 +75,8 @@ KeyboardKeys GetKey(NSEvent* event) case 0x33: return KeyboardKeys::Delete; //case 0x34: case 0x35: return KeyboardKeys::Escape; - //case 0x36: - //case 0x37: Command + case 0x36: return KeyboardKeys::Control; // Command (right) + case 0x37: return KeyboardKeys::Control; // Command (left) case 0x38: return KeyboardKeys::Shift; case 0x39: return KeyboardKeys::Capital; case 0x3A: return KeyboardKeys::Alt; @@ -411,6 +411,31 @@ static void ConvertNSRect(NSScreen *screen, NSRect *r) Input::Keyboard->OnKeyUp(key); } +- (void)flagsChanged:(NSEvent*)event +{ + int32 modMask; + int32 keyCode = [event keyCode]; + if (keyCode == 0x36 || keyCode == 0x37) + modMask = NSEventModifierFlagCommand; + else if (keyCode == 0x38 || keyCode == 0x3c) + modMask = NSEventModifierFlagShift; + else if (keyCode == 0x3a || keyCode == 0x3d) + modMask = NSEventModifierFlagOption; + else if (keyCode == 0x3b || keyCode == 0x3e) + modMask = NSEventModifierFlagControl; + else + return; + KeyboardKeys key = GetKey(event); + if (key != KeyboardKeys::None) + { + int32 modifierFlags = [event modifierFlags]; + if ((modifierFlags & modMask) == modMask) + Input::Keyboard->OnKeyDown(key); + else + Input::Keyboard->OnKeyUp(key); + } +} + - (void)scrollWheel:(NSEvent*)event { Float2 mousePos = GetMousePosition(Window, event); From 1ae89c75452cb82ae207b94cd75127bb9e3b5fad Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 15 Jul 2023 15:07:52 +0200 Subject: [PATCH 6/6] Fix various keyboard handling on macOS --- Source/Engine/Platform/Mac/MacWindow.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Engine/Platform/Mac/MacWindow.cpp b/Source/Engine/Platform/Mac/MacWindow.cpp index a52c11324..aeee93e37 100644 --- a/Source/Engine/Platform/Mac/MacWindow.cpp +++ b/Source/Engine/Platform/Mac/MacWindow.cpp @@ -72,7 +72,7 @@ KeyboardKeys GetKey(NSEvent* event) case 0x30: return KeyboardKeys::Tab; case 0x31: return KeyboardKeys::Spacebar; case 0x32: return KeyboardKeys::BackQuote; - case 0x33: return KeyboardKeys::Delete; + case 0x33: return KeyboardKeys::Backspace; //case 0x34: case 0x35: return KeyboardKeys::Escape; case 0x36: return KeyboardKeys::Control; // Command (right) @@ -378,7 +378,7 @@ static void ConvertNSRect(NSScreen *screen, NSRect *r) { KeyboardKeys key = GetKey(event); if (key != KeyboardKeys::None) - Input::Keyboard->OnKeyDown(key); + Input::Keyboard->OnKeyDown(key, Window); // Send a text input event switch (key) @@ -400,7 +400,7 @@ static void ConvertNSRect(NSScreen *screen, NSRect *r) if (length >= 16) length = 15; [text getCharacters:buffer range:NSMakeRange(0, length)]; - Input::Keyboard->OnCharInput((Char)buffer[0]); + Input::Keyboard->OnCharInput((Char)buffer[0], Window); } } @@ -408,7 +408,7 @@ static void ConvertNSRect(NSScreen *screen, NSRect *r) { KeyboardKeys key = GetKey(event); if (key != KeyboardKeys::None) - Input::Keyboard->OnKeyUp(key); + Input::Keyboard->OnKeyUp(key, Window); } - (void)flagsChanged:(NSEvent*)event @@ -430,9 +430,9 @@ static void ConvertNSRect(NSScreen *screen, NSRect *r) { int32 modifierFlags = [event modifierFlags]; if ((modifierFlags & modMask) == modMask) - Input::Keyboard->OnKeyDown(key); + Input::Keyboard->OnKeyDown(key, Window); else - Input::Keyboard->OnKeyUp(key); + Input::Keyboard->OnKeyUp(key, Window); } }