MouseMacOS.mm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <Input/MacOS/MouseMacOS.h>
  6. #include <Window/ApplicationWindowMacOS.h>
  7. #import <Cocoa/Cocoa.h>
  8. #import <GameController/GameController.h>
  9. // This class receives mouse connect callbacks
  10. @interface MouseDelegate : NSObject
  11. @end
  12. @implementation MouseDelegate
  13. {
  14. MouseMacOS *mMouse;
  15. }
  16. - (MouseDelegate *)init:(MouseMacOS *)mouse
  17. {
  18. mMouse = mouse;
  19. return self;
  20. }
  21. - (void)mouseDidConnect:(NSNotification *)notification
  22. {
  23. GCMouse *mouse = (GCMouse *)notification.object;
  24. if (mouse == nil)
  25. return;
  26. GCMouseInput *mouseInput = mouse.mouseInput;
  27. if (mouseInput == nil)
  28. return;
  29. __block MouseDelegate *weakSelf = self;
  30. mouseInput.mouseMovedHandler = ^(GCMouseInput *mouse, float deltaX, float deltaY) {
  31. MouseDelegate *strongSelf = weakSelf;
  32. if (strongSelf == nil)
  33. return;
  34. strongSelf->mMouse->OnMouseDelta(deltaX, -deltaY);
  35. };
  36. mouseInput.leftButton.pressedChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
  37. MouseDelegate *strongSelf = weakSelf;
  38. if (strongSelf == nil)
  39. return;
  40. strongSelf->mMouse->SetLeftPressed(pressed);
  41. };
  42. mouseInput.rightButton.pressedChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
  43. MouseDelegate *strongSelf = weakSelf;
  44. if (strongSelf == nil)
  45. return;
  46. strongSelf->mMouse->SetRightPressed(pressed);
  47. };
  48. mouseInput.middleButton.pressedChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
  49. MouseDelegate *strongSelf = weakSelf;
  50. if (strongSelf == nil)
  51. return;
  52. strongSelf->mMouse->SetMiddlePressed(pressed);
  53. };
  54. }
  55. @end
  56. bool MouseMacOS::Initialize(ApplicationWindow *inWindow)
  57. {
  58. mWindow = static_cast<ApplicationWindowMacOS *>(inWindow);
  59. // Install listener for mouse move callbacks
  60. mWindow->SetMouseMovedCallback([this](int inX, int inY) { OnMouseMoved(inX, inY); });
  61. // Install listener for mouse delta callbacks (will work also when mouse is outside the window or at the edge of the screen)
  62. MouseDelegate *delegate = [[MouseDelegate alloc] init: this];
  63. [NSNotificationCenter.defaultCenter addObserver: delegate selector: @selector(mouseDidConnect:) name: GCMouseDidConnectNotification object:nil];
  64. return true;
  65. }
  66. void MouseMacOS::Shutdown()
  67. {
  68. if (mWindow != nullptr)
  69. {
  70. mWindow->SetMouseMovedCallback({});
  71. mWindow = nullptr;
  72. }
  73. }
  74. void MouseMacOS::Poll()
  75. {
  76. mDeltaX = mDeltaXAcc;
  77. mDeltaY = mDeltaYAcc;
  78. mDeltaXAcc = 0;
  79. mDeltaYAcc = 0;
  80. }