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