macCursorController.mm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <Carbon/Carbon.h>
  23. #include <Cocoa/Cocoa.h>
  24. #include "windowManager/mac/macWindow.h"
  25. #include "windowManager/mac/macCursorController.h"
  26. void MacCursorController::setCursorPosition(S32 x, S32 y)
  27. {
  28. MacWindow* macWindow = dynamic_cast<MacWindow*>(mOwner);
  29. if(!macWindow || !macWindow->isVisible())
  30. return;
  31. CGPoint pt = { x, y };
  32. CGWarpMouseCursorPosition(pt);
  33. macWindow->_skipAnotherMouseEvent();
  34. }
  35. void MacCursorController::getCursorPosition( Point2I &point )
  36. {
  37. NSPoint pos = [NSEvent mouseLocation];
  38. point.x = pos.x;
  39. point.y = pos.y;
  40. //what does this do?? comment??
  41. MacWindow* macWindow = static_cast<MacWindow*>(mOwner);
  42. CGRect bounds = macWindow->getDisplayBounds();
  43. CGRect mainbounds = macWindow->getMainDisplayBounds();
  44. F32 offsetY = mainbounds.size.height - (bounds.size.height + bounds.origin.y);
  45. point.y = bounds.size.height + offsetY - point.y;
  46. }
  47. void MacCursorController::setCursorVisible(bool visible)
  48. {
  49. visible ? [NSCursor unhide] : [NSCursor hide];
  50. }
  51. bool MacCursorController::isCursorVisible()
  52. {
  53. return CGCursorIsVisible();
  54. }
  55. // a repository of custom cursors.
  56. @interface TorqueCursors : NSObject { }
  57. +(NSCursor*)resizeAll;
  58. +(NSCursor*)resizeNWSE;
  59. +(NSCursor*)resizeNESW;
  60. @end
  61. @implementation TorqueCursors
  62. +(NSCursor*)resizeAll
  63. {
  64. static NSCursor* cur = nil;
  65. if(!cur)
  66. cur = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"resizeAll"] hotSpot:NSMakePoint(8, 8)];
  67. return cur;
  68. }
  69. +(NSCursor*)resizeNWSE
  70. {
  71. static NSCursor* cur = nil;
  72. if(!cur)
  73. cur = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"resizeNWSE"] hotSpot:NSMakePoint(8, 8)];
  74. return cur;
  75. }
  76. +(NSCursor*)resizeNESW
  77. {
  78. static NSCursor* cur = nil;
  79. if(!cur)
  80. cur = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"resizeNESW"] hotSpot:NSMakePoint(8, 8)];
  81. return cur;
  82. }
  83. @end
  84. void MacCursorController::setCursorShape(U32 cursorID)
  85. {
  86. NSCursor *cur;
  87. switch(cursorID)
  88. {
  89. case PlatformCursorController::curArrow:
  90. [[NSCursor arrowCursor] set];
  91. break;
  92. case PlatformCursorController::curWait:
  93. // hack: black-sheep carbon call
  94. SetThemeCursor(kThemeWatchCursor);
  95. break;
  96. case PlatformCursorController::curPlus:
  97. [[NSCursor crosshairCursor] set];
  98. break;
  99. case PlatformCursorController::curResizeVert:
  100. [[NSCursor resizeLeftRightCursor] set];
  101. break;
  102. case PlatformCursorController::curIBeam:
  103. [[NSCursor IBeamCursor] set];
  104. break;
  105. case PlatformCursorController::curResizeAll:
  106. cur = [TorqueCursors resizeAll];
  107. [cur set];
  108. break;
  109. case PlatformCursorController::curResizeNESW:
  110. [[TorqueCursors resizeNESW] set];
  111. break;
  112. case PlatformCursorController::curResizeNWSE:
  113. [[TorqueCursors resizeNWSE] set];
  114. break;
  115. case PlatformCursorController::curResizeHorz:
  116. [[NSCursor resizeUpDownCursor] set];
  117. break;
  118. // This sets an appropriate value for the standard hand cursor.
  119. // In AFX this is used for rollover feedback.
  120. case PlatformCursorController::curHand:
  121. [[NSCursor pointingHandCursor] set];
  122. break;
  123. }
  124. }
  125. void MacCursorController::setCursorShape( const UTF8 *fileName, bool reload )
  126. {
  127. //TODO: this is untested code
  128. NSString* strFileName = [ NSString stringWithUTF8String: fileName ];
  129. // Load image file.
  130. NSImage* image = [ NSImage alloc ];
  131. if( [ image initWithContentsOfFile: strFileName ] == nil )
  132. return;
  133. // Allocate cursor.
  134. NSCursor* cursor = [ NSCursor alloc ];
  135. [ cursor initWithImage: image hotSpot: NSMakePoint( 0.5, 0.5 ) ];
  136. }
  137. U32 MacCursorController::getDoubleClickTime()
  138. {
  139. return GetDblTime() / 60.0f * 1000.0f;
  140. }
  141. S32 MacCursorController::getDoubleClickWidth()
  142. {
  143. // This is an arbitrary value.
  144. return 10;
  145. }
  146. S32 MacCursorController::getDoubleClickHeight()
  147. {
  148. return getDoubleClickWidth();
  149. }