iDev

cocos2d mac에서 custom cursor 사용하기

KraZYeom 2012. 6. 5. 08:38
반응형

cocos2d에서 커스텀 커서를 사용하는 방법을 그냥 mac app에서 커스텀 커서를 사용하는 방법 처럼 하면 잘 안되길래.. http://www.cocos2d-iphone.org/forum/topic/12523 찾음. 



CCGLView.m 에 아래코드 추가.


-(NSCursor*) getCustomCursor{

    

    if (_cursor==nil){

        NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:@"cursor_normal" ofType:@"png"];

        NSImage *crsrImage = [[NSImage alloc] initWithContentsOfFile:pathToSettingsInBundle];

        _cursor = [[NSCursor alloc] initWithImage: crsrImage

                                          hotSpot:NSMakePoint(10, 10) ] ;

        

        [crsrImage release];

    }

    return _cursor;

    

}


-(void) doCursorUpdate{

    [[self getCustomCursor] set];

    //NSLog(@"CursorUpdate");

    

}


//Called each time the mouse enter in the tracking area

-(void)cursorUpdate:(NSEvent *)theEvent

{

    

    [self doCursorUpdate];

    

}


//call by  NSView

- (void)updateTrackingAreas {

    if (_trackingArea!=nil){

        [self removeTrackingArea:_trackingArea];

        [_trackingArea release];

        _trackingArea=nil;

    }

    //NSLog(@"updateTrackingAreas");

    NSTrackingAreaOptions trackingOptions =

    NSTrackingCursorUpdate |

    NSTrackingActiveInActiveApp;

    _trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:trackingOptions owner:self userInfo:nil];

    

    [self addTrackingArea:_trackingArea];

    

    //the only solution I found to show the cursor in FullScreen Mode

    // since logically cursorUpdate is not called in fullscreen mode

    [self performSelector:@selector(doCursorUpdate) withObject:nil afterDelay:0];

    

}


그런데 마우스 다운, 업의 커스텀 커서를 하기 위해서는 각각의 이벤트 메소드를 살짝 수정해주면 된다.

아래와 같이 마우스 다운, 업도 수정해주면 대략 완벽. 

- (void)mouseDown:(NSEvent *)theEvent

{

    NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:@"cursor_click" ofType:@"png"];

    NSImage *crsrImage = [[NSImage alloc] initWithContentsOfFile:pathToSettingsInBundle];

    _cursor = [[NSCursor alloc] initWithImage: crsrImage

                                      hotSpot:NSMakePoint(10, 10) ] ;

    [_cursor set];

DISPATCH_EVENT(theEvent, _cmd);

}


- (void)mouseUp:(NSEvent *)theEvent

{

    NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:@"cursor_normal" ofType:@"png"];

    NSImage *crsrImage = [[NSImage alloc] initWithContentsOfFile:pathToSettingsInBundle];

    _cursor = [[NSCursor alloc] initWithImage: crsrImage

                                      hotSpot:NSMakePoint(10, 10) ] ;

    [_cursor set];


DISPATCH_EVENT(theEvent, _cmd);

}


대충 수정해서 잘 사용하면 됨. 출근 시간이다!!! =ㅁ=; 


언제나 그렇듯이 내 머리가 나빠서 ... 적는 용도. ㅠㅠ 

반응형