ApplicationWindowMacOS.mm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Window/ApplicationWindowMacOS.h>
  6. #import <MetalKit/MetalKit.h>
  7. // This class implements a metal view
  8. @interface MetalView : MTKView <MTKViewDelegate>
  9. @end
  10. @implementation MetalView
  11. {
  12. ApplicationWindowMacOS *mWindow;
  13. }
  14. - (MetalView *)init:(ApplicationWindowMacOS *)window
  15. {
  16. [super initWithFrame: NSMakeRect(0, 0, window->GetWindowWidth(), window->GetWindowHeight()) device: MTLCreateSystemDefaultDevice()];
  17. mWindow = window;
  18. self.delegate = self;
  19. return self;
  20. }
  21. - (bool)acceptsFirstResponder
  22. {
  23. return YES;
  24. }
  25. - (bool)canBecomeKeyView
  26. {
  27. return YES;
  28. }
  29. - (BOOL)isFlipped {
  30. return YES;
  31. }
  32. - (void)mouseMoved:(NSEvent *)event
  33. {
  34. NSPoint locationInView = [self convertPoint:event.locationInWindow fromView:nil];
  35. NSPoint locationInBacking = [self convertPointToBacking:locationInView];
  36. mWindow->OnMouseMoved(locationInBacking.x, -locationInBacking.y);
  37. }
  38. - (void)drawInMTKView:(MTKView *)view
  39. {
  40. mWindow->RenderCallback();
  41. }
  42. - (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size
  43. {
  44. mWindow->OnWindowResized(size.width, size.height);
  45. }
  46. @end
  47. // This is the main application delegate that tells us if we're starting / stopping
  48. @interface AppDelegate : NSObject <NSApplicationDelegate>
  49. @end
  50. @implementation AppDelegate
  51. -(void)applicationDidFinishLaunching:(NSNotification *)notification
  52. {
  53. // Add the Quit button to the first menu item on the toolbar
  54. NSMenu *app_menu = [[NSApp mainMenu] itemAtIndex: 0].submenu;
  55. NSMenuItem *quit_item = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
  56. [app_menu addItem:quit_item];
  57. }
  58. -(bool)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
  59. {
  60. // Close the app when the window is closed
  61. return YES;
  62. }
  63. @end
  64. void ApplicationWindowMacOS::Initialize()
  65. {
  66. // Create metal view
  67. MetalView *view = [[MetalView alloc] init: this];
  68. mMetalLayer = (CAMetalLayer *)view.layer;
  69. // Create window
  70. NSWindow *window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, mWindowWidth, mWindowHeight)
  71. styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable
  72. backing: NSBackingStoreBuffered
  73. defer: NO];
  74. window.contentView = view;
  75. [window setAcceptsMouseMovedEvents: YES];
  76. [window setTitle: @"TestFramework"];
  77. [window makeKeyAndOrderFront: nil];
  78. }
  79. void ApplicationWindowMacOS::MainLoop(ApplicationWindow::RenderCallback inRenderCallback)
  80. {
  81. mRenderCallback = inRenderCallback;
  82. @autoreleasepool
  83. {
  84. NSApplication *app = [NSApplication sharedApplication];
  85. AppDelegate *delegate = [[AppDelegate alloc] init];
  86. [app setDelegate:delegate];
  87. [app run];
  88. }
  89. }