| 1 | /* |
| 2 | The zlib/libpng License |
| 3 | |
| 4 | Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com) |
| 5 | |
| 6 | This software is provided 'as-is', without any express or implied warranty. In no event will |
| 7 | the authors be held liable for any damages arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, including commercial |
| 10 | applications, and to alter it and redistribute it freely, subject to the following |
| 11 | restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not claim that |
| 14 | you wrote the original software. If you use this software in a product, |
| 15 | an acknowledgment in the product documentation would be appreciated but is |
| 16 | not required. |
| 17 | |
| 18 | 2. Altered source versions must be plainly marked as such, and must not be |
| 19 | misrepresented as being the original software. |
| 20 | |
| 21 | 3. This notice may not be removed or altered from any source distribution. |
| 22 | */ |
| 23 | |
| 24 | #ifndef OIS_CocoaKeyboard_H |
| 25 | #define OIS_CocoaKeyboard_H |
| 26 | |
| 27 | #include "OISKeyboard.h" |
| 28 | #include "mac/CocoaHelpers.h" |
| 29 | |
| 30 | #include <list> |
| 31 | #include <Cocoa/Cocoa.h> |
| 32 | |
| 33 | @class CocoaKeyboardView; |
| 34 | |
| 35 | using namespace OIS; |
| 36 | using namespace std; |
| 37 | |
| 38 | namespace OIS |
| 39 | { |
| 40 | typedef class Cocoa_KeyStackEvent |
| 41 | { |
| 42 | friend class CocoaKeyboard; |
| 43 | |
| 44 | public: |
| 45 | Cocoa_KeyStackEvent( KeyEvent event, MacEventType type ) : Event(event), Type(type) {} |
| 46 | const MacEventType type() { return Type; } |
| 47 | const KeyEvent event() { return Event; } |
| 48 | private: |
| 49 | MacEventType Type; |
| 50 | KeyEvent Event; |
| 51 | } CocoaKeyStackEvent; |
| 52 | |
| 53 | class CocoaKeyboard : public Keyboard |
| 54 | { |
| 55 | public: |
| 56 | CocoaKeyboard( InputManager* creator, bool buffered, bool repeat ); |
| 57 | virtual ~CocoaKeyboard(); |
| 58 | |
| 59 | // Sets buffered mode |
| 60 | virtual void setBuffered( bool buffered ); |
| 61 | |
| 62 | // Unbuffered keydown check |
| 63 | virtual bool isKeyDown( KeyCode key ) const; |
| 64 | |
| 65 | // This will send listener events if buffered is on. |
| 66 | // Note that in the mac implementation, unbuffered input is |
| 67 | // automatically updated without calling this. |
| 68 | virtual void capture(); |
| 69 | |
| 70 | // Copies the current key buffer |
| 71 | virtual void copyKeyStates( char keys[256] ) const; |
| 72 | |
| 73 | // Returns a description of the given key |
| 74 | virtual std::string& getAsString( KeyCode key ); |
| 75 | |
| 76 | virtual Interface* queryInterface( Interface::IType type ) { return 0; } |
| 77 | |
| 78 | // Public but reserved for internal use: |
| 79 | virtual void _initialize(); |
| 80 | |
| 81 | unsigned int & _getModifiers() { return mModifiers; } |
| 82 | |
| 83 | protected: |
| 84 | CocoaKeyboardView *mResponder; |
| 85 | std::string getString; |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | typedef std::map<unsigned short, KeyCode> VirtualtoOIS_KeyMap; |
| 90 | typedef std::list<OIS::CocoaKeyStackEvent> eventStack; |
| 91 | |
| 92 | @interface CocoaKeyboardView : NSResponder |
| 93 | { |
| 94 | CocoaKeyboard *oisKeyboardObj; |
| 95 | VirtualtoOIS_KeyMap keyConversion; |
| 96 | |
| 97 | char KeyBuffer[256]; |
| 98 | NSUInteger prevModMask; |
| 99 | |
| 100 | // buffered events, fifo stack |
| 101 | eventStack pendingEvents; |
| 102 | bool useRepeat; |
| 103 | } |
| 104 | |
| 105 | - (void)setOISKeyboardObj:(CocoaKeyboard *)obj; |
| 106 | - (void)populateKeyConversion; |
| 107 | - (void)capture; |
| 108 | - (void)injectEvent:(KeyCode)kc eventTime:(unsigned int)time eventType:(MacEventType)type; |
| 109 | - (void)injectEvent:(KeyCode)kc eventTime:(unsigned int)time eventType:(MacEventType)type eventText:(unsigned int)txt; |
| 110 | - (void)copyKeyStates:(char [256])keys; |
| 111 | - (bool)isKeyDown:(KeyCode)key; |
| 112 | - (void)setUseRepeat:(bool)repeat; |
| 113 | - (VirtualtoOIS_KeyMap)keyConversionMap; |
| 114 | |
| 115 | @end |
| 116 | |
| 117 | #endif |