Changeset 19844

Show
Ignore:
Timestamp:
02/28/07 19:22:29 (1 year ago)
Author:
patrick
Message:

Relocate code from InputViewCocoa? to gadget::InputAreaCocoa?. This is needed
because my plan to have VR Juggler OpenGL windows that receive keyboard/mouse
input using an NSOpenGLView that is a subview of InputViewCocoa? did not work
out. Overlapping views in Cocoa is not really supported and is definitely
not recommended. By moving code from InputViewCocoa? to gadget::InputAreaCocoa?,
it can be reused by the (yet to be committed) GlViewCocoa? class. The reuse is
not as elegant or as automatic, but at least it works.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • juggler/trunk/modules/gadgeteer/gadget/Devices/KeyboardMouseDevice/InputAreaCocoa.h

    r19828 r19844  
    120120   /** 
    121121    * Adds a new mouse motion event to the event queue for this input area. 
     122    * The mouse button is determined by converting the value returned from 
     123    * the NSEvent method -buttonNumber. 
     124    * 
     125    * @post A new event (gadget::MouseEvent) is added to the event queue. 
     126    * 
     127    * @param type  The type of button event (MouseButtonPressEvent or 
     128    *              MouseButtonReleaseEvent). 
     129    * @param event A pointer to the Cocoa event structure associated with the 
     130    *              button event. 
     131    */ 
     132   void addMouseButtonEvent(const gadget::EventType type, NSEvent* event); 
     133 
     134   /** 
     135    * Adds a new mouse motion event to the event queue for this input area. 
    122136    * 
    123137    * @post A new event (gadget::MouseEvent) is added to the event queue. 
     
    142156    */ 
    143157   void addMouseMoveEvent(NSEvent* event); 
     158 
     159   /** 
     160    * Transforms a change in the modifier flags into a key press or key 
     161    * release event. 
     162    * 
     163    * @post \c mLastModifiers holds the new set of pressed modifier keys. 
     164    */ 
     165   void flagsChanged(NSEvent* event); 
    144166   //@} 
    145167 
     
    148170 
    149171protected: 
     172   /** 
     173    * Translates the mouse button number into a gadget::Keys value. 
     174    */ 
     175   gadget::Keys getButtonFromNum(const int buttonNum) const; 
     176 
     177   /** 
     178    * Converts \p mask into the gadget::Keys corresponding value. The value of 
     179    * \p mask must be a mask value. 
     180    */ 
     181   gadget::Keys getKeyFromModifierMask(const unsigned int mask) const; 
     182 
    150183   void warpCursorToCenter(); 
    151184 
     
    205238   float mHeight;       /**< Input area height */ 
    206239 
     240   unsigned int mLastModifiers; /**< The current keyboard modifiers */ 
     241 
    207242   NSWindow* mCocoaWindow;      /**< The window for this input area. */ 
    208243   NSView*   mMainView;         /**< The window's view. */ 
  • juggler/trunk/modules/gadgeteer/gadget/Devices/KeyboardMouseDevice/InputAreaCocoa.mm

    r19836 r19844  
    5454   , mWidth(0) 
    5555   , mHeight(0) 
     56   , mLastModifiers(0) 
    5657   , mCocoaWindow(nil) 
    5758   , mMainView(nil) 
     
    118119} 
    119120 
     121void InputAreaCocoa::addMouseButtonEvent(const gadget::EventType type, 
     122                                         NSEvent* event) 
     123{ 
     124   addMouseButtonEvent(getButtonFromNum([event buttonNumber]), type, event); 
     125} 
     126 
    120127void InputAreaCocoa::addMouseButtonEvent(const gadget::Keys button, 
    121128                                         const gadget::EventType type, 
     
    133140} 
    134141 
     142 
    135143void InputAreaCocoa::addMouseMoveEvent(NSEvent* event) 
    136144{ 
     
    179187} 
    180188 
     189void InputAreaCocoa::flagsChanged(NSEvent* event) 
     190{ 
     191   const unsigned int cur_modifiers = [event modifierFlags]; 
     192 
     193   // Compare the new modifier set with the old to find out what modifier 
     194   // key has been pressed or released. 
     195   const unsigned int new_modifiers = ~mLastModifiers & cur_modifiers; 
     196   const unsigned int old_modifiers = mLastModifiers & ~cur_modifiers; 
     197 
     198   if ( new_modifiers != 0 ) 
     199   { 
     200      addModifierEvent(getKeyFromModifierMask(new_modifiers), 
     201                       gadget::KeyPressEvent, event); 
     202   } 
     203   else if ( old_modifiers != 0 ) 
     204   { 
     205      addModifierEvent(getKeyFromModifierMask(old_modifiers), 
     206                       gadget::KeyReleaseEvent, event); 
     207   } 
     208 
     209   mLastModifiers = cur_modifiers; 
     210} 
     211 
    181212void InputAreaCocoa::updateOriginAndSize(const float x, const float y, 
    182213                                         const float width, 
     
    187218   mWidth  = width; 
    188219   mHeight = height; 
     220} 
     221 
     222gadget::Keys InputAreaCocoa::getButtonFromNum(const int buttonNum) const 
     223{ 
     224   gadget::Keys button(gadget::NO_MBUTTON); 
     225 
     226   switch ( buttonNum ) 
     227   { 
     228      case 0: 
     229         button = gadget::MBUTTON1; 
     230         break; 
     231      case 1: 
     232         button = gadget::MBUTTON2; 
     233         break; 
     234      case 2: 
     235         button = gadget::MBUTTON3; 
     236         break; 
     237      case 3: 
     238         button = gadget::MBUTTON4; 
     239         break; 
     240      case 4: 
     241         button = gadget::MBUTTON5; 
     242         break; 
     243      case 5: 
     244         button = gadget::MBUTTON6; 
     245         break; 
     246      case 6: 
     247         button = gadget::MBUTTON7; 
     248         break; 
     249   } 
     250 
     251   return button; 
     252} 
     253 
     254gadget::Keys InputAreaCocoa::getKeyFromModifierMask(const unsigned int mask) 
     255   const 
     256{ 
     257   gadget::Keys key(gadget::KEY_NONE); 
     258 
     259   if ( mask & NSCommandKeyMask ) 
     260   { 
     261      key = gadget::KEY_COMMAND; 
     262   } 
     263   else if ( mask & NSAlternateKeyMask ) 
     264   { 
     265      key = gadget::KEY_ALT; 
     266   } 
     267   else if ( mask & NSControlKeyMask ) 
     268   { 
     269      key = gadget::KEY_CTRL; 
     270   } 
     271   else if ( mask & NSShiftKeyMask ) 
     272   { 
     273      key = gadget::KEY_SHIFT; 
     274   } 
     275 
     276   return key; 
    189277} 
    190278 
  • juggler/trunk/modules/gadgeteer/gadget/Devices/KeyboardMouseDevice/InputViewCocoa.h

    r19838 r19844  
    4949{ 
    5050   gadget::InputAreaCocoa* mInputArea;  /**< The Gadgeteer input area */ 
    51    unsigned int mLastModifiers;         /**< The current keyboard modifiers */ 
    5251   NSTrackingRectTag mTrackingRect; 
    5352} 
  • juggler/trunk/modules/gadgeteer/gadget/Devices/KeyboardMouseDevice/InputViewCocoa.mm

    r19838 r19844  
    2727#include <gadget/gadgetConfig.h> 
    2828 
    29 #import <Foundation/NSString.h> 
    3029#import <AppKit/NSWindow.h> 
    3130#import <AppKit/NSEvent.h> 
     
    4342 */ 
    4443@interface InputViewCocoa (PrivateMethods) 
    45    -(gadget::Keys) getButtonFromNum:(int) buttonNum; 
    46    -(void) flagsChanged:(NSEvent*) theEvent; 
    4744   -(void) clearTrackingRect; 
    4845   -(void) resetTrackingRect; 
    49    -(gadget::Keys) getKeyFromModifierMask:(unsigned int) mask; 
    5046@end 
    5147 
     
    5450             inputArea:(gadget::InputAreaCocoa*) inputArea 
    5551   { 
    56       mLastModifiers = 0; 
    5752      mInputArea = inputArea; 
     53      mTrackingRect = 0; 
    5854      return [self initWithFrame:frameRect]; 
    5955   } 
     
    212208   -(void) otherMouseDown:(NSEvent*) theEvent 
    213209   { 
    214       mInputArea->addMouseButtonEvent( 
    215          [self getButtonFromNum:[theEvent buttonNumber]], 
    216          gadget::MouseButtonPressEvent, theEvent 
    217       ); 
     210      mInputArea->addMouseButtonEvent(gadget::MouseButtonPressEvent, 
     211                                      theEvent); 
    218212   } 
    219213 
     
    223217   -(void) otherMouseUp:(NSEvent*) theEvent 
    224218   { 
    225       mInputArea->addMouseButtonEvent( 
    226          [self getButtonFromNum:[theEvent buttonNumber]], 
    227          gadget::MouseButtonReleaseEvent, theEvent 
    228       ); 
     219      mInputArea->addMouseButtonEvent(gadget::MouseButtonReleaseEvent, 
     220                                      theEvent); 
    229221   } 
    230222 
     
    252244      mInputArea->addKeyEvent(gadget::KeyReleaseEvent, theEvent); 
    253245   } 
     246 
     247   /** 
     248    * Responds to a change in the keyboard modifier flags. 
     249    */ 
     250   -(void) flagsChanged:(NSEvent*) theEvent 
     251   { 
     252      mInputArea->flagsChanged(theEvent); 
     253   } 
    254254   //@} 
    255  
    256    /** @name Private Methods */ 
    257    //@{ 
    258    /** 
    259     * Translates the mouse button number into a gadget::Keys value. 
    260     */ 
    261    -(gadget::Keys) getButtonFromNum:(int) buttonNum 
    262    { 
    263       gadget::Keys button(gadget::NO_MBUTTON); 
    264  
    265       switch ( buttonNum ) 
    266       { 
    267          case 0: 
    268             button = gadget::MBUTTON1; 
    269             break; 
    270          case 1: 
    271             button = gadget::MBUTTON2; 
    272             break; 
    273          case 2: 
    274             button = gadget::MBUTTON3; 
    275             break; 
    276          case 3: 
    277             button = gadget::MBUTTON4; 
    278             break; 
    279          case 4: 
    280             button = gadget::MBUTTON5; 
    281             break; 
    282          case 5: 
    283             button = gadget::MBUTTON6; 
    284             break; 
    285          case 6: 
    286             button = gadget::MBUTTON7; 
    287             break; 
    288       } 
    289  
    290       return button; 
    291    } 
    292  
    293    /** 
    294     * Transforms a change in the modifier flags into a key press or key 
    295     * release event. 
    296     * 
    297     * @post \c mLastModifiers holds the new set of pressed modifier keys. 
    298     */ 
    299    -(void) flagsChanged:(NSEvent*) theEvent 
    300    { 
    301       const unsigned int cur_modifiers = [theEvent modifierFlags]; 
    302  
    303       // Compare the new modifier set with the old to find out what modifier 
    304       // key has been pressed or released. 
    305       const unsigned int new_modifiers = ~mLastModifiers & cur_modifiers; 
    306       const unsigned int old_modifiers = mLastModifiers & ~cur_modifiers; 
    307  
    308       if ( new_modifiers != 0 ) 
    309       { 
    310          mInputArea->addModifierEvent( 
    311             [self getKeyFromModifierMask:new_modifiers], 
    312             gadget::KeyPressEvent, theEvent 
    313          ); 
    314       } 
    315       else if ( old_modifiers != 0 ) 
    316       { 
    317          mInputArea->addModifierEvent( 
    318             [self getKeyFromModifierMask:old_modifiers], 
    319             gadget::KeyReleaseEvent, theEvent 
    320          ); 
    321       } 
    322  
    323       mLastModifiers = cur_modifiers; 
    324    } 
    325255 
    326256   /** 
     
    338268   } 
    339269 
     270   /** @name Private Methods */ 
     271   //@{ 
    340272   /** 
    341273    * Removes the current tracking rectangle for this view (if it has one). 
     
    369301                               assumeInside:NO]; 
    370302   } 
    371  
    372    /** 
    373     * Converts \p modifiers into the gadget::Keys corresponding value. The 
    374     * value of modifiers must be a mask value. 
    375     */ 
    376    -(gadget::Keys) getKeyFromModifierMask:(unsigned int) mask 
    377    { 
    378       gadget::Keys key(gadget::KEY_NONE); 
    379  
    380       if ( mask & NSCommandKeyMask ) 
    381       { 
    382          key = gadget::KEY_COMMAND; 
    383       } 
    384       else if ( mask & NSAlternateKeyMask ) 
    385       { 
    386          key = gadget::KEY_ALT; 
    387       } 
    388       else if ( mask & NSControlKeyMask ) 
    389       { 
    390          key = gadget::KEY_CTRL; 
    391       } 
    392       else if ( mask & NSShiftKeyMask ) 
    393       { 
    394          key = gadget::KEY_SHIFT; 
    395       } 
    396  
    397       return key; 
    398    } 
    399303   //@} 
    400304@end