Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.9 KiB

  1. //====== Copyright 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef KEYBINDINGMAP_H
  7. #define KEYBINDINGMAP_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier1/utlvector.h"
  12. // more flexible than default pointers to members code required for casting member function pointers
  13. #pragma pointers_to_members( full_generality, virtual_inheritance )
  14. namespace vgui
  15. {
  16. class Panel;
  17. enum
  18. {
  19. MODIFIER_SHIFT = ( 1 << 0 ),
  20. MODIFIER_CONTROL = ( 1 << 1 ),
  21. MODIFIER_ALT = ( 1 << 2 ),
  22. };
  23. //-----------------------------------------------------------------------------
  24. // Purpose: An actual keybinding, where bindingname references a bindingmap mentioned below
  25. //-----------------------------------------------------------------------------
  26. struct BoundKey_t
  27. {
  28. BoundKey_t();
  29. BoundKey_t( const BoundKey_t& src );
  30. ~BoundKey_t();
  31. BoundKey_t& operator =( const BoundKey_t& src );
  32. bool isbuiltin; // whether this was by the #DECLARE macros or via code/parsing a config file
  33. char const *bindingname; // what it's bound to
  34. int keycode; // vgui keycode
  35. int modifiers; // which modifiers
  36. };
  37. //-----------------------------------------------------------------------------
  38. // Purpose: Single item in a message map
  39. // Contains the information to map a string message name with parameters
  40. // to a function call
  41. //-----------------------------------------------------------------------------
  42. struct KeyBindingMap_t
  43. {
  44. KeyBindingMap_t();
  45. KeyBindingMap_t( const KeyBindingMap_t& src );
  46. ~KeyBindingMap_t();
  47. char const *bindingname; // for the script file
  48. ALIGN16 MessageFunc_t func;
  49. char const *helpstring; // e.g., #KeybindingPasteHelp
  50. char const *docstring; // e.g., #KeybindingPasteHelp
  51. bool passive; // dispatch command, but still chain
  52. };
  53. #define DECLARE_KEYBINDINGMAP( className ) \
  54. static void KB_AddToMap \
  55. ( \
  56. char const *bindingname, \
  57. vgui::KeyCode defaultcode, \
  58. int default_modifiers, \
  59. vgui::MessageFunc_t function, \
  60. char const *helpstring, \
  61. char const *docstring, \
  62. bool passive \
  63. ) \
  64. { \
  65. vgui::PanelKeyBindingMap *map = vgui::FindOrAddPanelKeyBindingMap( GetPanelClassName() ); \
  66. \
  67. vgui::KeyBindingMap_t entry; \
  68. entry.bindingname = bindingname; \
  69. \
  70. entry.func = function; \
  71. \
  72. entry.helpstring = helpstring; \
  73. entry.docstring = docstring; \
  74. \
  75. entry.passive = passive; \
  76. \
  77. map->entries.AddToTail( entry ); \
  78. \
  79. vgui::BoundKey_t kb; \
  80. kb.isbuiltin = true; \
  81. kb.bindingname = bindingname; \
  82. kb.keycode = defaultcode; \
  83. kb.modifiers = default_modifiers; \
  84. map->defaultkeys.AddToTail( kb ); \
  85. map->boundkeys.AddToTail( kb ); \
  86. } \
  87. \
  88. static void KB_ChainToMap( void ) \
  89. { \
  90. static bool chained = false; \
  91. if ( chained ) \
  92. return; \
  93. chained = true; \
  94. vgui::PanelKeyBindingMap *map = vgui::FindOrAddPanelKeyBindingMap( GetPanelClassName() ); \
  95. map->pfnClassName = &GetPanelClassName; \
  96. if ( map && GetPanelBaseClassName() && GetPanelBaseClassName()[0] ) \
  97. { \
  98. map->baseMap = vgui::FindOrAddPanelKeyBindingMap( GetPanelBaseClassName() ); \
  99. } \
  100. } \
  101. \
  102. static void KB_AddBoundKey \
  103. ( \
  104. char const *bindingname, \
  105. int keycode, \
  106. int modifiers \
  107. ) \
  108. { \
  109. vgui::PanelKeyBindingMap *map = vgui::FindOrAddPanelKeyBindingMap( GetPanelClassName() ); \
  110. vgui::BoundKey_t kb; \
  111. kb.isbuiltin = true; \
  112. kb.bindingname = bindingname; \
  113. kb.keycode = keycode; \
  114. kb.modifiers = modifiers; \
  115. map->defaultkeys.AddToTail( kb ); \
  116. map->boundkeys.AddToTail( kb ); \
  117. } \
  118. \
  119. class className##_RegisterKBMap; \
  120. friend class className##_RegisterKBMap; \
  121. class className##_RegisterKBMap \
  122. { \
  123. public: \
  124. className##_RegisterKBMap() \
  125. { \
  126. className::KB_ChainToMap(); \
  127. } \
  128. }; \
  129. className##_RegisterKBMap m_RegisterClassKB; \
  130. \
  131. virtual vgui::PanelKeyBindingMap *GetKBMap() \
  132. { \
  133. static vgui::PanelKeyBindingMap *s_pMap = vgui::FindOrAddPanelKeyBindingMap( GetPanelClassName() ); \
  134. return s_pMap; \
  135. }
  136. #define _KBMapFuncCommonFunc( name, keycode, modifiers, function, help, doc, passive ) \
  137. class PanelKBMapFunc_##name; \
  138. friend class PanelKBMapFunc_##name; \
  139. class PanelKBMapFunc_##name \
  140. { \
  141. public: \
  142. static void InitVar() \
  143. { \
  144. static bool bAdded = false; \
  145. if ( !bAdded ) \
  146. { \
  147. bAdded = true; \
  148. KB_AddToMap( #name, keycode, modifiers, (vgui::MessageFunc_t)&ThisClass::function, help, doc, passive ); \
  149. } \
  150. } \
  151. PanelKBMapFunc_##name() \
  152. { \
  153. PanelKBMapFunc_##name::InitVar(); \
  154. } \
  155. }; \
  156. PanelKBMapFunc_##name m_##name##_register;
  157. #define _KBBindKeyCommon( name, keycode, modifiers, _classname ) \
  158. class PanelKBBindFunc_##_classname; \
  159. friend class PanelKBBindFunc_##_classname; \
  160. class PanelKBBindFunc_##_classname \
  161. { \
  162. public: \
  163. static void InitVar() \
  164. { \
  165. static bool bAdded = false; \
  166. if ( !bAdded ) \
  167. { \
  168. bAdded = true; \
  169. KB_AddBoundKey( #name, keycode, modifiers ); \
  170. } \
  171. } \
  172. PanelKBBindFunc_##_classname() \
  173. { \
  174. PanelKBBindFunc_##_classname::InitVar(); \
  175. } \
  176. }; \
  177. PanelKBBindFunc_##_classname m_##_classname##_bindkey_register;
  178. #define KEYBINDING_FUNC( name, keycode, modifiers, function, help, doc ) _KBMapFuncCommonFunc( name, keycode, modifiers, function, help, doc, false ); virtual void function()
  179. #define KEYBINDING_FUNC_NODECLARE( name, keycode, modifiers, function, help, doc ) _KBMapFuncCommonFunc( name, keycode, modifiers, function, help, doc, false );
  180. #define KEYBINDING_FUNC_PASSIVE( name, keycode, modifiers, function, help, doc ) _KBMapFuncCommonFunc( name, keycode, modifiers, function, help, doc, true ); virtual void function()
  181. #define KEYBINDING_FUNC_PASSIVE_NODECLARE( name, keycode, modifiers, function, help, doc ) _KBMapFuncCommonFunc( name, keycode, modifiers, function, help, doc, true );
  182. // For definding additional (non-default) keybindings
  183. #define KEYBINDING_ADDBINDING( name, keycode, modifiers ) _KBBindKeyCommon( name, keycode, modifiers, name );
  184. #define KEYBINDING_ADDBINDING_MULTIPLE( name, keycode, modifiers, _classname ) _KBBindKeyCommon( name, keycode, modifiers, _classname );
  185. // mapping, one per class
  186. struct PanelKeyBindingMap
  187. {
  188. PanelKeyBindingMap()
  189. {
  190. baseMap = NULL;
  191. pfnClassName = NULL;
  192. processed = false;
  193. }
  194. CUtlVector< KeyBindingMap_t > entries;
  195. bool processed;
  196. PanelKeyBindingMap *baseMap;
  197. CUtlVector< BoundKey_t > defaultkeys;
  198. CUtlVector< BoundKey_t > boundkeys;
  199. char const *(*pfnClassName)( void );
  200. };
  201. PanelKeyBindingMap *FindPanelKeyBindingMap( char const *className );
  202. PanelKeyBindingMap *FindOrAddPanelKeyBindingMap( char const *className );
  203. } // namespace vgui
  204. #endif // KEYBINDINGMAP_H