Advertisement
Guest User

CCEventDispatcher.mm

a guest
May 30th, 2013
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.00 KB | None | 0 0
  1. /*
  2.  * cocos2d for iPhone: http://www.cocos2d-iphone.org
  3.  *
  4.  * Copyright (c) 2010 Ricardo Quesada
  5.  * Copyright (c) 2011 Zynga Inc.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in
  15.  * all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23.  * THE SOFTWARE.
  24.  */
  25.  
  26. // Only compile this code on Mac. These files should not be included on your iOS project.
  27. // But in case they are included, it won't be compiled.
  28. #import <Availability.h>
  29.  
  30. #import "CCEventDispatcher.h"
  31. #import "ccConfig.h"
  32. #include "support/data_support/utlist.h"
  33. #include "CCDirector.h"
  34.  
  35. //NS_CC_BEGIN;
  36. static CCEventDispatcher *sharedDispatcher = nil;
  37.  
  38. enum  {
  39.     // mouse
  40.     kCCImplementsMouseDown          = 1 << 0,
  41.     kCCImplementsMouseMoved         = 1 << 1,
  42.     kCCImplementsMouseDragged       = 1 << 2,  
  43.     kCCImplementsMouseUp            = 1 << 3,
  44.     kCCImplementsRightMouseDown     = 1 << 4,
  45.     kCCImplementsRightMouseDragged  = 1 << 5,
  46.     kCCImplementsRightMouseUp       = 1 << 6,
  47.     kCCImplementsOtherMouseDown     = 1 << 7,
  48.     kCCImplementsOtherMouseDragged  = 1 << 8,
  49.     kCCImplementsOtherMouseUp       = 1 << 9,
  50.     kCCImplementsScrollWheel        = 1 << 10,
  51.     kCCImplementsMouseEntered       = 1 << 11,
  52.     kCCImplementsMouseExited        = 1 << 12,
  53.  
  54.     kCCImplementsTouchesBegan       = 1 << 13,
  55.     kCCImplementsTouchesMoved       = 1 << 14,
  56.     kCCImplementsTouchesEnded       = 1 << 15,
  57.     kCCImplementsTouchesCancelled           = 1 << 16,
  58.  
  59.     // keyboard
  60.     kCCImplementsKeyUp              = 1 << 0,
  61.     kCCImplementsKeyDown            = 1 << 1,
  62.     kCCImplementsFlagsChanged       = 1 << 2,
  63. };
  64.  
  65. // MAC KEYS
  66.  
  67. enum {
  68.     kVK_ANSI_A                    = 0x00,
  69.     kVK_ANSI_S                    = 0x01,
  70.     kVK_ANSI_D                    = 0x02,
  71.     kVK_ANSI_F                    = 0x03,
  72.     kVK_ANSI_H                    = 0x04,
  73.     kVK_ANSI_G                    = 0x05,
  74.     kVK_ANSI_Z                    = 0x06,
  75.     kVK_ANSI_X                    = 0x07,
  76.     kVK_ANSI_C                    = 0x08,
  77.     kVK_ANSI_V                    = 0x09,
  78.     kVK_ANSI_B                    = 0x0B,
  79.     kVK_ANSI_Q                    = 0x0C,
  80.     kVK_ANSI_W                    = 0x0D,
  81.     kVK_ANSI_E                    = 0x0E,
  82.     kVK_ANSI_R                    = 0x0F,
  83.     kVK_ANSI_Y                    = 0x10,
  84.     kVK_ANSI_T                    = 0x11,
  85.     kVK_ANSI_1                    = 0x12,
  86.     kVK_ANSI_2                    = 0x13,
  87.     kVK_ANSI_3                    = 0x14,
  88.     kVK_ANSI_4                    = 0x15,
  89.     kVK_ANSI_6                    = 0x16,
  90.     kVK_ANSI_5                    = 0x17,
  91.     kVK_ANSI_Equal                = 0x18,
  92.     kVK_ANSI_9                    = 0x19,
  93.     kVK_ANSI_7                    = 0x1A,
  94.     kVK_ANSI_Minus                = 0x1B,
  95.     kVK_ANSI_8                    = 0x1C,
  96.     kVK_ANSI_0                    = 0x1D,
  97.     kVK_ANSI_RightBracket         = 0x1E,
  98.     kVK_ANSI_O                    = 0x1F,
  99.     kVK_ANSI_U                    = 0x20,
  100.     kVK_ANSI_LeftBracket          = 0x21,
  101.     kVK_ANSI_I                    = 0x22,
  102.     kVK_ANSI_P                    = 0x23,
  103.     kVK_ANSI_L                    = 0x25,
  104.     kVK_ANSI_J                    = 0x26,
  105.     kVK_ANSI_Quote                = 0x27,
  106.     kVK_ANSI_K                    = 0x28,
  107.     kVK_ANSI_Semicolon            = 0x29,
  108.     kVK_ANSI_Backslash            = 0x2A,
  109.     kVK_ANSI_Comma                = 0x2B,
  110.     kVK_ANSI_Slash                = 0x2C,
  111.     kVK_ANSI_N                    = 0x2D,
  112.     kVK_ANSI_M                    = 0x2E,
  113.     kVK_ANSI_Period               = 0x2F,
  114.     kVK_ANSI_Grave                = 0x32,
  115.     kVK_ANSI_KeypadDecimal        = 0x41,
  116.     kVK_ANSI_KeypadMultiply       = 0x43,
  117.     kVK_ANSI_KeypadPlus           = 0x45,
  118.     kVK_ANSI_KeypadClear          = 0x47,
  119.     kVK_ANSI_KeypadDivide         = 0x4B,
  120.     kVK_ANSI_KeypadEnter          = 0x4C,
  121.     kVK_ANSI_KeypadMinus          = 0x4E,
  122.     kVK_ANSI_KeypadEquals         = 0x51,
  123.     kVK_ANSI_Keypad0              = 0x52,
  124.     kVK_ANSI_Keypad1              = 0x53,
  125.     kVK_ANSI_Keypad2              = 0x54,
  126.     kVK_ANSI_Keypad3              = 0x55,
  127.     kVK_ANSI_Keypad4              = 0x56,
  128.     kVK_ANSI_Keypad5              = 0x57,
  129.     kVK_ANSI_Keypad6              = 0x58,
  130.     kVK_ANSI_Keypad7              = 0x59,
  131.     kVK_ANSI_Keypad8              = 0x5B,
  132.     kVK_ANSI_Keypad9              = 0x5C
  133. };
  134.  
  135. /* keycodes for keys that are independent of keyboard layout*/
  136. enum {
  137.     kVK_Return                    = 0x24,
  138.     kVK_Tab                       = 0x30,
  139.     kVK_Space                     = 0x31,
  140.     kVK_Delete                    = 0x33,
  141.     kVK_Escape                    = 0x35,
  142.     kVK_Command                   = 0x37,
  143.     kVK_Shift                     = 0x38,
  144.     kVK_CapsLock                  = 0x39,
  145.     kVK_Option                    = 0x3A,
  146.     kVK_Control                   = 0x3B,
  147.     kVK_RightShift                = 0x3C,
  148.     kVK_RightOption               = 0x3D,
  149.     kVK_RightControl              = 0x3E,
  150.     kVK_Function                  = 0x3F,
  151.     kVK_F17                       = 0x40,
  152.     kVK_VolumeUp                  = 0x48,
  153.     kVK_VolumeDown                = 0x49,
  154.     kVK_Mute                      = 0x4A,
  155.     kVK_F18                       = 0x4F,
  156.     kVK_F19                       = 0x50,
  157.     kVK_F20                       = 0x5A,
  158.     kVK_F5                        = 0x60,
  159.     kVK_F6                        = 0x61,
  160.     kVK_F7                        = 0x62,
  161.     kVK_F3                        = 0x63,
  162.     kVK_F8                        = 0x64,
  163.     kVK_F9                        = 0x65,
  164.     kVK_F11                       = 0x67,
  165.     kVK_F13                       = 0x69,
  166.     kVK_F16                       = 0x6A,
  167.     kVK_F14                       = 0x6B,
  168.     kVK_F10                       = 0x6D,
  169.     kVK_F12                       = 0x6F,
  170.     kVK_F15                       = 0x71,
  171.     kVK_Help                      = 0x72,
  172.     kVK_Home                      = 0x73,
  173.     kVK_PageUp                    = 0x74,
  174.     kVK_ForwardDelete             = 0x75,
  175.     kVK_F4                        = 0x76,
  176.     kVK_End                       = 0x77,
  177.     kVK_F2                        = 0x78,
  178.     kVK_PageDown                  = 0x79,
  179.     kVK_F1                        = 0x7A,
  180.     kVK_LeftArrow                 = 0x7B,
  181.     kVK_RightArrow                = 0x7C,
  182.     kVK_DownArrow                 = 0x7D,
  183.     kVK_UpArrow                   = 0x7E
  184. };
  185.  
  186. // MAC KEYS
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. typedef struct _listEntry
  194. {
  195.     struct  _listEntry  *prev, *next;
  196.     id                  delegate;
  197.     NSInteger           priority;
  198.     NSUInteger          flags;
  199. } tListEntry;
  200.  
  201.  
  202. #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD
  203.  
  204. #define QUEUE_EVENT_MAX 128
  205. struct _eventQueue {
  206.     SEL     selector;
  207.     NSEvent *event;
  208. };
  209.  
  210. static struct   _eventQueue eventQueue[QUEUE_EVENT_MAX];
  211. static int      eventQueueCount;
  212.  
  213. #endif // CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD
  214.  
  215.  
  216. @implementation CCEventDispatcher
  217.  
  218. @synthesize dispatchEvents=dispatchEvents_;
  219.  
  220.  
  221. +(CCEventDispatcher*) sharedDispatcher
  222. {
  223.     @synchronized(self) {
  224.         if (sharedDispatcher == nil)
  225.             sharedDispatcher = [[self alloc] init]; // assignment not done here
  226.     }
  227.     return sharedDispatcher;
  228. }
  229.  
  230. +(id) allocWithZone:(NSZone *)zone
  231. {
  232.     @synchronized(self) {
  233.         NSAssert(sharedDispatcher == nil, @"Attempted to allocate a second instance of a singleton.");
  234.         return [super allocWithZone:zone];
  235.     }
  236.     return nil; // on subsequent allocation attempts return nil
  237. }
  238.  
  239. -(id) init
  240. {
  241.     if( (self = [super init]) )
  242.     {
  243.         // events enabled by default
  244.         dispatchEvents_ = YES;
  245.  
  246.         // delegates
  247.         keyboardDelegates_ = NULL;
  248.         mouseDelegates_ = NULL;
  249.                 touchDelegates_ = NULL;
  250.        
  251. #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD
  252.         eventQueueCount = 0;
  253. #endif
  254.     }
  255.    
  256.     return self;
  257. }
  258.  
  259. - (void) dealloc
  260. {
  261.     [super dealloc];
  262. }
  263.  
  264. #pragma mark CCEventDispatcher - add / remove delegates
  265.  
  266. -(void) addDelegate:(id)delegate priority:(NSInteger)priority flags:(NSUInteger)flags list:(tListEntry**)list
  267. {
  268.     tListEntry *listElement = (tListEntry *)malloc( sizeof(*listElement) );
  269.    
  270.     listElement->delegate = [delegate retain];
  271.     listElement->priority = priority;
  272.     listElement->flags = flags;
  273.     listElement->next = listElement->prev = NULL;
  274.    
  275.     // empty list ?
  276.     if( ! *list ) {
  277.         DL_APPEND( *list, listElement );
  278.        
  279.     } else {
  280.         BOOL added = NO;       
  281.        
  282.         for( tListEntry *elem = *list; elem ; elem = elem->next ) {
  283.             if( priority < elem->priority ) {
  284.                
  285.                 if( elem == *list )
  286.                     DL_PREPEND(*list, listElement);
  287.                 else {
  288.                     listElement->next = elem;
  289.                     listElement->prev = elem->prev;
  290.                    
  291.                     elem->prev->next = listElement;
  292.                     elem->prev = listElement;
  293.                 }
  294.                
  295.                 added = YES;
  296.                 break;
  297.             }
  298.         }
  299.        
  300.         // Not added? priority has the higher value. Append it.
  301.         if( !added )
  302.             DL_APPEND(*list, listElement);
  303.     }
  304. }
  305.  
  306. -(void) removeDelegate:(id)delegate fromList:(tListEntry**)list
  307. {
  308.     tListEntry *entry, *tmp;
  309.    
  310.     // updates with priority < 0
  311.     DL_FOREACH_SAFE( *list, entry, tmp ) {
  312.         if( entry->delegate == delegate ) {
  313.             DL_DELETE( *list, entry );
  314.             [delegate release];
  315.             free(entry);
  316.             break;
  317.         }
  318.     }
  319. }
  320.  
  321. -(void) removeAllDelegatesFromList:(tListEntry**)list
  322. {
  323.     tListEntry *entry, *tmp;
  324.  
  325.     DL_FOREACH_SAFE( *list, entry, tmp ) {
  326.         DL_DELETE( *list, entry );
  327.         free(entry);
  328.     }
  329. }
  330.  
  331.  
  332. -(void) addMouseDelegate:(id<CCMouseEventDelegate>) delegate priority:(NSInteger)priority
  333. {
  334.     NSUInteger flags = 0;
  335.    
  336.     flags |= ( [delegate respondsToSelector:@selector(ccMouseDown:)] ? kCCImplementsMouseDown : 0 );
  337.     flags |= ( [delegate respondsToSelector:@selector(ccMouseDragged:)] ? kCCImplementsMouseDragged : 0 );
  338.     flags |= ( [delegate respondsToSelector:@selector(ccMouseMoved:)] ? kCCImplementsMouseMoved : 0 );
  339.     flags |= ( [delegate respondsToSelector:@selector(ccMouseUp:)] ? kCCImplementsMouseUp : 0 );
  340.  
  341.     flags |= ( [delegate respondsToSelector:@selector(ccRightMouseDown:)] ? kCCImplementsRightMouseDown : 0 );
  342.     flags |= ( [delegate respondsToSelector:@selector(ccRightMouseDragged:)] ? kCCImplementsRightMouseDragged : 0 );
  343.     flags |= ( [delegate respondsToSelector:@selector(ccRightMouseUp:)] ? kCCImplementsRightMouseUp : 0 );
  344.  
  345.     flags |= ( [delegate respondsToSelector:@selector(ccOtherMouseDown:)] ? kCCImplementsOtherMouseDown : 0 );
  346.     flags |= ( [delegate respondsToSelector:@selector(ccOtherMouseDragged:)] ? kCCImplementsOtherMouseDragged : 0 );
  347.     flags |= ( [delegate respondsToSelector:@selector(ccOtherMouseUp:)] ? kCCImplementsOtherMouseUp : 0 );
  348.  
  349.     flags |= ( [delegate respondsToSelector:@selector(ccMouseEntered:)] ? kCCImplementsMouseEntered : 0 );
  350.     flags |= ( [delegate respondsToSelector:@selector(ccMouseExited:)] ? kCCImplementsMouseExited : 0 );
  351.  
  352.     flags |= ( [delegate respondsToSelector:@selector(ccScrollWheel:)] ? kCCImplementsScrollWheel : 0 );
  353.  
  354.     [self addDelegate:delegate priority:priority flags:flags list:&mouseDelegates_];
  355. }
  356.  
  357. -(void) removeMouseDelegate:(id) delegate
  358. {
  359.     [self removeDelegate:delegate fromList:&mouseDelegates_];
  360. }
  361.  
  362. -(void) removeAllMouseDelegates
  363. {
  364.     [self removeAllDelegatesFromList:&mouseDelegates_];
  365. }
  366.  
  367. -(void) addKeyboardDelegate:(id<CCKeyboardEventDelegate>) delegate priority:(NSInteger)priority
  368. {
  369.     NSUInteger flags = 0;
  370.    
  371.     flags |= ( [delegate respondsToSelector:@selector(ccKeyUp:)] ? kCCImplementsKeyUp : 0 );
  372.     flags |= ( [delegate respondsToSelector:@selector(ccKeyDown:)] ? kCCImplementsKeyDown : 0 );
  373.     flags |= ( [delegate respondsToSelector:@selector(ccFlagsChanged:)] ? kCCImplementsFlagsChanged : 0 );
  374.    
  375.     [self addDelegate:delegate priority:priority flags:flags list:&keyboardDelegates_];
  376. }
  377.  
  378. -(void) removeKeyboardDelegate:(id) delegate
  379. {
  380.     [self removeDelegate:delegate fromList:&keyboardDelegates_];
  381. }
  382.  
  383. -(void) removeAllKeyboardDelegates
  384. {
  385.     [self removeAllDelegatesFromList:&keyboardDelegates_];
  386. }
  387.  
  388. -(void) addTouchDelegate:(id<CCTouchEventDelegate>) delegate priority:(NSInteger)priority
  389. {
  390.     NSUInteger flags = 0;
  391.    
  392.     flags |= ( [delegate respondsToSelector:@selector(ccTouchesBeganWithEvent:)] ? kCCImplementsTouchesBegan : 0 );
  393.     flags |= ( [delegate respondsToSelector:@selector(ccTouchesMovedWithEvent:)] ? kCCImplementsTouchesMoved : 0 );
  394.     flags |= ( [delegate respondsToSelector:@selector(ccTouchesEndedWithEvent:)] ? kCCImplementsTouchesEnded : 0 );
  395.     flags |= ( [delegate respondsToSelector:@selector(ccTouchesCancelledWithEvent:)] ? kCCImplementsTouchesCancelled : 0 );
  396.    
  397.     [self addDelegate:delegate priority:priority flags:flags list:&touchDelegates_];
  398. }
  399.  
  400. -(void) removeTouchDelegate:(id) delegate
  401. {
  402.     [self removeDelegate:delegate fromList:&touchDelegates_];
  403. }
  404.  
  405. -(void) removeAllTouchDelegates
  406. {
  407.     [self removeAllDelegatesFromList:&touchDelegates_];
  408. }
  409.  
  410.  
  411. #pragma mark CCEventDispatcher - Mouse events
  412. //
  413. // Mouse events
  414. //
  415.  
  416. //
  417. // Left
  418. //
  419. - (void)mouseDown:(NSEvent *)event
  420. {
  421.     if( dispatchEvents_ ) {
  422.         tListEntry *entry, *tmp;
  423.  
  424.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  425.             if ( entry->flags & kCCImplementsMouseDown ) {
  426.                 void *swallows = [entry->delegate performSelector:@selector(ccMouseDown:) withObject:event];
  427.                 if( swallows )
  428.                     break;
  429.             }
  430.         }
  431.     }
  432. }
  433.  
  434. - (void)mouseMoved:(NSEvent *)event
  435. {
  436.     if( dispatchEvents_ ) {
  437.         tListEntry *entry, *tmp;
  438.        
  439.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  440.             if ( entry->flags & kCCImplementsMouseMoved ) {
  441.                 void *swallows = [entry->delegate performSelector:@selector(ccMouseMoved:) withObject:event];
  442.                 if( swallows )
  443.                     break;
  444.             }
  445.         }
  446.     }
  447. }
  448.  
  449. - (void)mouseDragged:(NSEvent *)event
  450. {
  451.     if( dispatchEvents_ ) {
  452.         tListEntry *entry, *tmp;
  453.        
  454.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  455.             if ( entry->flags & kCCImplementsMouseDragged ) {
  456.                 void *swallows = [entry->delegate performSelector:@selector(ccMouseDragged:) withObject:event];
  457.                 if( swallows )
  458.                     break;
  459.             }
  460.         }
  461.     }
  462. }
  463.  
  464. - (void)mouseUp:(NSEvent *)event
  465. {
  466.     if( dispatchEvents_ ) {
  467.         tListEntry *entry, *tmp;
  468.        
  469.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  470.             if ( entry->flags & kCCImplementsMouseUp ) {
  471.                 void *swallows = [entry->delegate performSelector:@selector(ccMouseUp:) withObject:event];
  472.                 if( swallows )
  473.                     break;
  474.             }
  475.         }
  476.     }
  477. }
  478.  
  479. //
  480. // Mouse Right
  481. //
  482. - (void)rightMouseDown:(NSEvent *)event
  483. {
  484.     if( dispatchEvents_ ) {
  485.         tListEntry *entry, *tmp;
  486.        
  487.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  488.             if ( entry->flags & kCCImplementsRightMouseDown ) {
  489.                 void *swallows = [entry->delegate performSelector:@selector(ccRightMouseDown:) withObject:event];
  490.                 if( swallows )
  491.                     break;
  492.             }
  493.         }
  494.     }
  495. }
  496.  
  497. - (void)rightMouseDragged:(NSEvent *)event
  498. {
  499.     if( dispatchEvents_ ) {
  500.         tListEntry *entry, *tmp;
  501.        
  502.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  503.             if ( entry->flags & kCCImplementsRightMouseDragged ) {
  504.                 void *swallows = [entry->delegate performSelector:@selector(ccRightMouseDragged:) withObject:event];
  505.                 if( swallows )
  506.                     break;
  507.             }
  508.         }
  509.     }
  510. }
  511.  
  512. - (void)rightMouseUp:(NSEvent *)event
  513. {
  514.     if( dispatchEvents_ ) {
  515.         tListEntry *entry, *tmp;
  516.        
  517.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  518.             if ( entry->flags & kCCImplementsRightMouseUp ) {
  519.                 void *swallows = [entry->delegate performSelector:@selector(ccRightMouseUp:) withObject:event];
  520.                 if( swallows )
  521.                     break;
  522.             }
  523.         }
  524.     }
  525. }
  526.  
  527. //
  528. // Mouse Other
  529. //
  530. - (void)otherMouseDown:(NSEvent *)event
  531. {
  532.     if( dispatchEvents_ ) {
  533.         tListEntry *entry, *tmp;
  534.        
  535.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  536.             if ( entry->flags & kCCImplementsOtherMouseDown ) {
  537.                 void *swallows = [entry->delegate performSelector:@selector(ccOtherMouseDown:) withObject:event];
  538.                 if( swallows )
  539.                     break;
  540.             }
  541.         }
  542.     }
  543. }
  544.  
  545. - (void)otherMouseDragged:(NSEvent *)event
  546. {
  547.     if( dispatchEvents_ ) {
  548.         tListEntry *entry, *tmp;
  549.        
  550.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  551.             if ( entry->flags & kCCImplementsOtherMouseDragged ) {
  552.                 void *swallows = [entry->delegate performSelector:@selector(ccOtherMouseDragged:) withObject:event];
  553.                 if( swallows )
  554.                     break;
  555.             }
  556.         }
  557.     }
  558. }
  559.  
  560. - (void)otherMouseUp:(NSEvent *)event
  561. {
  562.     if( dispatchEvents_ ) {
  563.         tListEntry *entry, *tmp;
  564.        
  565.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  566.             if ( entry->flags & kCCImplementsOtherMouseUp ) {
  567.                 void *swallows = [entry->delegate performSelector:@selector(ccOtherMouseUp:) withObject:event];
  568.                 if( swallows )
  569.                     break;
  570.             }
  571.         }
  572.     }
  573. }
  574.  
  575. //
  576. // Scroll Wheel
  577. //
  578. - (void)scrollWheel:(NSEvent *)event
  579. {
  580.     if( dispatchEvents_ ) {
  581.         tListEntry *entry, *tmp;
  582.        
  583.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  584.             if ( entry->flags & kCCImplementsScrollWheel ) {
  585.                 void *swallows = [entry->delegate performSelector:@selector(ccScrollWheel:) withObject:event];
  586.                 if( swallows )
  587.                     break;
  588.             }
  589.         }
  590.     }
  591. }
  592.  
  593. //
  594. // Mouse enter / exit
  595. - (void)mouseExited:(NSEvent *)event
  596. {
  597.     if( dispatchEvents_ ) {
  598.         tListEntry *entry, *tmp;
  599.        
  600.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  601.             if ( entry->flags & kCCImplementsMouseEntered ) {
  602.                 void *swallows = [entry->delegate performSelector:@selector(ccMouseEntered:) withObject:event];
  603.                 if( swallows )
  604.                     break;
  605.             }
  606.         }
  607.     }  
  608. }
  609.  
  610. - (void)mouseEntered:(NSEvent *)event
  611. {
  612.     if( dispatchEvents_ ) {
  613.         tListEntry *entry, *tmp;
  614.        
  615.         DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) {
  616.             if ( entry->flags & kCCImplementsMouseExited) {
  617.                 void *swallows = [entry->delegate performSelector:@selector(ccMouseExited:) withObject:event];
  618.                 if( swallows )
  619.                     break;
  620.             }
  621.         }
  622.     }  
  623. }
  624.  
  625.  
  626. #pragma mark CCEventDispatcher - Keyboard events
  627.  
  628. // Keyboard events
  629. - (void)keyDown:(NSEvent *)event
  630. {
  631.    
  632.     // HACK STARTS
  633.     switch ([event keyCode]) {
  634.         case kVK_UpArrow:     cocos2d::CCDirector::sharedDirector()->upArrowKeyDown = true;    break;
  635.         case kVK_DownArrow:    cocos2d::CCDirector::sharedDirector()->downArrowKeyDown = true;   break;
  636.         case kVK_LeftArrow:     cocos2d::CCDirector::sharedDirector()->leftArrowKeyDown = true;    break;
  637.         case kVK_RightArrow:    cocos2d::CCDirector::sharedDirector()->rightArrowKeyDown = true;   break;
  638.         case kVK_Space:         cocos2d::CCDirector::sharedDirector()->spaceBarDown = true;        break;
  639.         case kVK_ANSI_B:         cocos2d::CCDirector::sharedDirector()->bKeyDown = true;        break;
  640.            
  641.         case kVK_ANSI_1:
  642.             cocos2d::CCDirector::sharedDirector()->numberOneDown = true;
  643.             break;
  644.            
  645.         case kVK_ANSI_2:
  646.             cocos2d::CCDirector::sharedDirector()->numberTwoDown = true;
  647.             break;
  648.            
  649.         case kVK_ANSI_3:
  650.             cocos2d::CCDirector::sharedDirector()->numberThreeDown = true;
  651.             break;
  652.            
  653.         case kVK_ANSI_4:
  654.             cocos2d::CCDirector::sharedDirector()->numberFourDown = true;
  655.             break;
  656.         case kVK_ANSI_5:
  657.             cocos2d::CCDirector::sharedDirector()->numberFiveDown = true;
  658.             break;
  659.            
  660.         case kVK_ANSI_6:
  661.             cocos2d::CCDirector::sharedDirector()->numberSixDown = true;
  662.             break;
  663.     }
  664.     // HACK ENDS
  665.    
  666.    
  667.    
  668.     if( dispatchEvents_ ) {
  669.         tListEntry *entry, *tmp;
  670.        
  671.         DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) {
  672.             if ( entry->flags & kCCImplementsKeyDown ) {
  673.                 void *swallows = [entry->delegate performSelector:@selector(ccKeyDown:) withObject:event];
  674.                 if( swallows )
  675.                     break;
  676.             }
  677.         }
  678.     }
  679. }
  680.  
  681. - (void)keyUp:(NSEvent *)event
  682. {
  683.     // HACK STARTS
  684.     switch ([event keyCode]) {
  685.         case kVK_UpArrow:     cocos2d::CCDirector::sharedDirector()->upArrowKeyDown = false;    break;
  686.         case kVK_DownArrow:    cocos2d::CCDirector::sharedDirector()->downArrowKeyDown = false;   break;
  687.         case kVK_LeftArrow:     cocos2d::CCDirector::sharedDirector()->leftArrowKeyDown = false;    break;
  688.         case kVK_RightArrow:    cocos2d::CCDirector::sharedDirector()->rightArrowKeyDown = false;   break;
  689.         case kVK_Space:         cocos2d::CCDirector::sharedDirector()->spaceBarDown = false;        break;
  690.         case kVK_ANSI_B:         cocos2d::CCDirector::sharedDirector()->bKeyDown = false;        break;
  691.            
  692.         case kVK_ANSI_1:
  693.             cocos2d::CCDirector::sharedDirector()->numberOneDown = false;
  694.             cocos2d::CCDirector::sharedDirector()->numberOnePressed = true;
  695.             break;
  696.            
  697.         case kVK_ANSI_2:
  698.             cocos2d::CCDirector::sharedDirector()->numberTwoDown = false;
  699.             cocos2d::CCDirector::sharedDirector()->numberTwoPressed = true;
  700.             break;
  701.            
  702.         case kVK_ANSI_3:
  703.             cocos2d::CCDirector::sharedDirector()->numberThreeDown = false;
  704.             cocos2d::CCDirector::sharedDirector()->numberThreePressed = true;
  705.             break;
  706.            
  707.         case kVK_ANSI_4:
  708.             cocos2d::CCDirector::sharedDirector()->numberFourDown = false;
  709.             cocos2d::CCDirector::sharedDirector()->numberFourPressed = true;
  710.             break;
  711.         case kVK_ANSI_5:
  712.             cocos2d::CCDirector::sharedDirector()->numberFiveDown = false;
  713.             cocos2d::CCDirector::sharedDirector()->numberFivePressed = true;
  714.             break;
  715.            
  716.         case kVK_ANSI_6:
  717.             cocos2d::CCDirector::sharedDirector()->numberSixDown = false;
  718.             cocos2d::CCDirector::sharedDirector()->numberSixPressed = true;
  719.             break;
  720.     }
  721.     // HACK ENDS
  722.    
  723.    
  724.     if( dispatchEvents_ ) {
  725.         tListEntry *entry, *tmp;
  726.        
  727.         DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) {
  728.             if ( entry->flags & kCCImplementsKeyUp ) {
  729.                 void *swallows = [entry->delegate performSelector:@selector(ccKeyUp:) withObject:event];
  730.                 if( swallows )
  731.                     break;
  732.             }
  733.         }
  734.     }
  735. }
  736.  
  737. - (void)flagsChanged:(NSEvent *)event
  738. {
  739.     if( dispatchEvents_ ) {
  740.         tListEntry *entry, *tmp;
  741.        
  742.         DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) {
  743.             if ( entry->flags & kCCImplementsFlagsChanged ) {
  744.                 void *swallows = [entry->delegate performSelector:@selector(ccFlagsChanged:) withObject:event];
  745.                 if( swallows )
  746.                     break;
  747.             }
  748.         }
  749.     }
  750. }
  751.  
  752.  
  753. #pragma mark CCEventDispatcher - Touch events
  754.  
  755. - (void)touchesBeganWithEvent:(NSEvent *)event
  756. {
  757.     if( dispatchEvents_ ) {
  758.         tListEntry *entry, *tmp;
  759.        
  760.         DL_FOREACH_SAFE( touchDelegates_, entry, tmp ) {
  761.             if ( entry->flags & kCCImplementsTouchesBegan) {
  762.                 void *swallows = [entry->delegate performSelector:@selector(ccTouchesBeganWithEvent:) withObject:event];
  763.                 if( swallows )
  764.                     break;
  765.             }
  766.         }
  767.     }  
  768. }
  769.  
  770. - (void)touchesMovedWithEvent:(NSEvent *)event
  771. {
  772.     if( dispatchEvents_ ) {
  773.         tListEntry *entry, *tmp;
  774.        
  775.         DL_FOREACH_SAFE( touchDelegates_, entry, tmp ) {
  776.             if ( entry->flags & kCCImplementsTouchesMoved) {
  777.                 void *swallows = [entry->delegate performSelector:@selector(ccTouchesMovedWithEvent:) withObject:event];
  778.                 if( swallows )
  779.                     break;
  780.             }
  781.         }
  782.     }  
  783. }
  784.  
  785. - (void)touchesEndedWithEvent:(NSEvent *)event
  786. {
  787.     if( dispatchEvents_ ) {
  788.         tListEntry *entry, *tmp;
  789.        
  790.         DL_FOREACH_SAFE( touchDelegates_, entry, tmp ) {
  791.             if ( entry->flags & kCCImplementsTouchesEnded) {
  792.                 void *swallows = [entry->delegate performSelector:@selector(ccTouchesEndedWithEvent:) withObject:event];
  793.                 if( swallows )
  794.                     break;
  795.             }
  796.         }
  797.     }  
  798. }
  799.  
  800. - (void)touchesCancelledWithEvent:(NSEvent *)event
  801. {
  802.     if( dispatchEvents_ ) {
  803.         tListEntry *entry, *tmp;
  804.        
  805.         DL_FOREACH_SAFE( touchDelegates_, entry, tmp ) {
  806.             if ( entry->flags & kCCImplementsTouchesCancelled) {
  807.                 void *swallows = [entry->delegate performSelector:@selector(ccTouchesCancelledWithEvent:) withObject:event];
  808.                 if( swallows )
  809.                     break;
  810.             }
  811.         }
  812.     }  
  813. }
  814.  
  815.  
  816. #pragma mark CCEventDispatcher - queue events
  817.  
  818. #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD
  819. -(void) queueEvent:(NSEvent*)event selector:(SEL)selector
  820. {
  821.     NSAssert( eventQueueCount < QUEUE_EVENT_MAX, @"CCEventDispatcher: recompile. Increment QUEUE_EVENT_MAX value");
  822.  
  823.     @synchronized (self) {
  824.         eventQueue[eventQueueCount].selector = selector;
  825.         eventQueue[eventQueueCount].event = [event copy];
  826.        
  827.         eventQueueCount++;
  828.     }
  829. }
  830.  
  831. -(void) dispatchQueuedEvents
  832. {
  833.     @synchronized (self) {
  834.         for( int i=0; i < eventQueueCount; i++ ) {
  835.             SEL sel = eventQueue[i].selector;
  836.             NSEvent *event = eventQueue[i].event;
  837.            
  838.             [self performSelector:sel withObject:event];
  839.            
  840.             [event release];
  841.         }
  842.        
  843.         eventQueueCount = 0;
  844.     }
  845. }
  846. #endif // CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD
  847.  
  848. //NS_CC_END;
  849. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement