PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llwindow/llkeyboard.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 148 lines | 88 code | 30 blank | 30 comment | 0 complexity | ac9fdae7fcbd3cf54c2ecbc209d81e20 MD5 | raw file
  1/** 
  2 * @file llkeyboard.h
  3 * @brief Handler for assignable key bindings
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_LLKEYBOARD_H
 28#define LL_LLKEYBOARD_H
 29
 30#include <map>
 31
 32#include "string_table.h"
 33#include "lltimer.h"
 34#include "indra_constants.h"
 35
 36enum EKeystate 
 37{
 38	KEYSTATE_DOWN,
 39	KEYSTATE_LEVEL,
 40	KEYSTATE_UP 
 41};
 42
 43typedef boost::function<void(EKeystate keystate)> LLKeyFunc;
 44typedef std::string (LLKeyStringTranslatorFunc)(const char *label);
 45	
 46enum EKeyboardInsertMode
 47{
 48	LL_KIM_INSERT,
 49	LL_KIM_OVERWRITE
 50};
 51
 52class LLKeyBinding
 53{
 54public:
 55	KEY				mKey;
 56	MASK			mMask;
 57// 	const char		*mName; // unused
 58	LLKeyFunc		mFunction;
 59};
 60
 61class LLWindowCallbacks;
 62
 63class LLKeyboard
 64{
 65public:
 66	typedef enum e_numpad_distinct
 67	{
 68		ND_NEVER,
 69		ND_NUMLOCK_OFF,
 70		ND_NUMLOCK_ON
 71	} ENumpadDistinct;
 72
 73public:
 74	LLKeyboard();
 75	virtual ~LLKeyboard();
 76
 77	void			resetKeys();
 78
 79
 80	F32				getCurKeyElapsedTime()	{ return getKeyDown(mCurScanKey) ? getKeyElapsedTime( mCurScanKey ) : 0.f; }
 81	F32				getCurKeyElapsedFrameCount()	{ return getKeyDown(mCurScanKey) ? (F32)getKeyElapsedFrameCount( mCurScanKey ) : 0.f; }
 82	BOOL			getKeyDown(const KEY key) { return mKeyLevel[key]; }
 83	BOOL			getKeyRepeated(const KEY key) { return mKeyRepeated[key]; }
 84
 85	BOOL			translateKey(const U16 os_key, KEY *translated_key);
 86	U16				inverseTranslateKey(const KEY translated_key);
 87	BOOL			handleTranslatedKeyUp(KEY translated_key, U32 translated_mask);		// Translated into "Linden" keycodes
 88	BOOL			handleTranslatedKeyDown(KEY translated_key, U32 translated_mask);	// Translated into "Linden" keycodes
 89
 90
 91	virtual BOOL	handleKeyUp(const U16 key, MASK mask) = 0;
 92	virtual BOOL	handleKeyDown(const U16 key, MASK mask) = 0;
 93
 94	// Asynchronously poll the control, alt, and shift keys and set the
 95	// appropriate internal key masks.
 96	virtual void	resetMaskKeys() = 0;
 97	virtual void	scanKeyboard() = 0;															// scans keyboard, calls functions as necessary
 98	// Mac must differentiate between Command = Control for keyboard events
 99	// and Command != Control for mouse events.
100	virtual MASK	currentMask(BOOL for_mouse_event) = 0;
101	virtual KEY		currentKey() { return mCurTranslatedKey; }
102
103	EKeyboardInsertMode getInsertMode()	{ return mInsertMode; }
104	void toggleInsertMode();
105
106	static BOOL		maskFromString(const std::string& str, MASK *mask);		// False on failure
107	static BOOL		keyFromString(const std::string& str, KEY *key);			// False on failure
108	static std::string stringFromKey(KEY key);
109	static std::string stringFromAccelerator( MASK accel_mask, KEY key );
110	e_numpad_distinct getNumpadDistinct() { return mNumpadDistinct; }
111	void setNumpadDistinct(e_numpad_distinct val) { mNumpadDistinct = val; }
112
113	void setCallbacks(LLWindowCallbacks *cbs) { mCallbacks = cbs; }
114	F32				getKeyElapsedTime( KEY key );  // Returns time in seconds since key was pressed.
115	S32				getKeyElapsedFrameCount( KEY key );  // Returns time in frames since key was pressed.
116
117	static void		setStringTranslatorFunc( LLKeyStringTranslatorFunc *trans_func );
118	
119protected:
120	void 			addKeyName(KEY key, const std::string& name);
121
122protected:
123	std::map<U16, KEY>	mTranslateKeyMap;		// Map of translations from OS keys to Linden KEYs
124	std::map<KEY, U16>	mInvTranslateKeyMap;	// Map of translations from Linden KEYs to OS keys
125	LLWindowCallbacks *mCallbacks;
126
127	LLTimer			mKeyLevelTimer[KEY_COUNT];	// Time since level was set
128	S32				mKeyLevelFrameCount[KEY_COUNT];	// Frames since level was set
129	BOOL			mKeyLevel[KEY_COUNT];		// Levels
130	BOOL			mKeyRepeated[KEY_COUNT];	// Key was repeated
131	BOOL			mKeyUp[KEY_COUNT];			// Up edge
132	BOOL			mKeyDown[KEY_COUNT];		// Down edge
133	KEY				mCurTranslatedKey;
134	KEY				mCurScanKey;		// Used during the scanKeyboard()
135
136	static LLKeyStringTranslatorFunc*	mStringTranslator;	// Used for l10n + PC/Mac/Linux accelerator labeling
137	
138	e_numpad_distinct mNumpadDistinct;
139
140	EKeyboardInsertMode mInsertMode;
141
142	static std::map<KEY,std::string> sKeysToNames;
143	static std::map<std::string,KEY> sNamesToKeys;
144};
145
146extern LLKeyboard *gKeyboard;
147
148#endif