pluginkeybinding.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * \defgroup MouseEvents Mouse Events
  3. * \desc constants for mouse events in PluginKeyBinding
  4. * @{
  5. */
  6. const int MB_EVENT_PRESS = 0;
  7. const int MB_EVENT_CLICK = 1;
  8. const int MB_EVENT_RELEASE = 2;
  9. const int MB_EVENT_DOUBLECLICK = 3;
  10. const int MB_EVENT_DRAG = 4;
  11. /** @}*/
  12. class PluginKeyBinding extends PluginBase
  13. {
  14. static PluginKeyBinding instance;
  15. // System Public Events
  16. void PluginKeyBinding()
  17. {
  18. if ( instance == null )
  19. {
  20. instance = this;
  21. }
  22. }
  23. //============================================
  24. // OnInit
  25. //============================================
  26. override void OnInit()
  27. {
  28. super.OnInit();
  29. m_KeyBindings = new array<ref KeyBinding>;
  30. m_MouseBindings = new array<ref MouseBinding>;
  31. m_MouseButtons = new array<ref MouseButtonInfo>;
  32. m_MouseButtons.Insert ( new MouseButtonInfo( MouseState.LEFT ) );
  33. m_MouseButtons.Insert ( new MouseButtonInfo( MouseState.RIGHT ) );
  34. m_MouseButtons.Insert ( new MouseButtonInfo( MouseState.MIDDLE ) );
  35. GetGame().GetUpdateQueue(CALL_CATEGORY_SYSTEM).Insert(this.OnFrame);
  36. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. //Keyboard Binds |UI_ID |Key1 |Key2 |Callback Plugin |Callback Function |Info Shrtcut |Info Description
  38. //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  39. // |constants.h |constants.h |constants.h |only plugin name |only function | |
  40. // |MENU_*** |KeyCode.KC_*** |KeyCode.KC_*** | |in plugin | |
  41. //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  42. #ifdef DEVELOPER
  43. RegisterKeyBind( MENU_ANY ,KeyCode.KC_LCONTROL ,KeyCode.KC_F1 ,"PluginDeveloper" ,"ToggleHelpScreen" ,"[LCTRL]+[F1]" ,"Show/Hide help screen");
  44. RegisterKeyBind( MENU_ANY ,KeyCode.KC_LCONTROL ,KeyCode.KC_F2 ,"PluginDeveloper" ,"ResetGUI" ,"[LCTRL]+[F2]" ,"Resets shown UI for testing purposes");
  45. RegisterKeyBind( MENU_NONE|MENU_SCRIPTCONSOLE ,KeyCode.KC_LCONTROL ,KeyCode.KC_GRAVE ,"PluginDeveloper" ,"ToggleScriptConsole" ,"[LCTRL]+[~]" ,"Show/Hide this script console");
  46. RegisterKeyBind( MENU_NONE|MENU_SCRIPTCONSOLE ,KeyCode.KC_RCONTROL ,KeyCode.KC_GRAVE ,"PluginDeveloper" ,"ToggleScriptConsole" ,"[RCTRL]+[~]" ,"Show/Hide this script console");
  47. RegisterKeyBind( MENU_NONE|MENU_SCENE_EDITOR ,KeyCode.KC_LCONTROL ,KeyCode.KC_TAB ,"PluginSceneManager" ,"EditorToggle" ,"[LCTRL]+[TAB]" ,"Show/Hide Scripted Scene Editor");
  48. RegisterKeyBind( MENU_NONE|MENU_SCENE_EDITOR ,KeyCode.KC_RCONTROL ,KeyCode.KC_TAB ,"PluginSceneManager" ,"EditorToggle" ,"[RCTRL]+[TAB]" ,"Show/Hide Scripted Scene Editor");
  49. RegisterKeyBind( MENU_NONE ,KeyCode.KC_LCONTROL ,KeyCode.KC_V ,"PluginDeveloper" ,"SpawnFromClipboard" ,"[Right Shift]" ,"Spawn item from clipboard");
  50. #endif
  51. #ifdef DIAG_DEVELOPER
  52. RegisterKeyBind( MENU_NONE ,KeyCode.KC_INSERT ,-1 ,"PluginDeveloper" ,"TeleportAtCursor" ,"[Insert]" ,"Teleport player to cursor position");
  53. RegisterKeyBind( MENU_NONE ,KeyCode.KC_HOME ,-1 ,"PluginDeveloper" ,"ToggleFreeCamera" ,"[Home]" ,"Toggle free camera and teleport player to its position");
  54. RegisterKeyBind( MENU_NONE ,KeyCode.KC_DIVIDE ,-1 ,"PluginDeveloper" ,"ToggleFreeCameraBackPos" ,"[Divide numeric]" ,"Toggle free camera, no player teleport");
  55. RegisterKeyBind( MENU_NONE ,KeyCode.KC_LCONTROL ,KeyCode.KC_NUMPAD9 ,"PluginDeveloperSync" ,"ToggleFocus" ,"[LCtrl]+[NUMPAD9]" ,"Toggle game focus");
  56. #endif
  57. #ifdef DEVELOPER
  58. RegisterKeyBind( MENU_NONE ,KeyCode.KC_LCONTROL ,KeyCode.KC_Z ,"PluginDeveloper" ,"SetDeveloperItemClient" ,"Starts watching the item" ,"Sets the target as Watched item for debug purposes");
  59. RegisterKeyBind( MENU_SCRIPTCONSOLE ,KeyCode.KC_PRIOR ,-1 ,"PluginDeveloper" ,"ScriptHistoryBack" ,"[Page Up]" ,"Debug Console => Script history back");
  60. RegisterKeyBind( MENU_SCRIPTCONSOLE ,KeyCode.KC_NEXT ,-1 ,"PluginDeveloper" ,"ScriptHistoryNext" ,"[Page Down]" ,"Debug Console => Script history next");
  61. RegisterKeyBind( MENU_SCENE_EDITOR ,KeyCode.KC_DELETE ,-1 ,"PluginSceneManager" ,"DeleteSelectedObject" ,"[Delete]" ,"Scene Editor => Delete current selected object");
  62. RegisterKeyBind( MENU_SCENE_EDITOR ,KeyCode.KC_LCONTROL ,KeyCode.KC_S ,"PluginSceneManager" ,"SceneSave" ,"[LCtrl]+[S]" ,"Scene Editor => Save Current Scene");
  63. RegisterKeyBind( MENU_SCENE_EDITOR ,KeyCode.KC_LCONTROL ,KeyCode.KC_D ,"PluginSceneManager" ,"SelectedObjectDuplicate" ,"[LCtrl]+[D]" ,"Scene Editor => Duplicate current selected object");
  64. RegisterKeyBind( MENU_SCENE_EDITOR ,KeyCode.KC_LCONTROL ,KeyCode.KC_F ,"PluginSceneManager" ,"SelectedObjectFocus" ,"[LCtrl]+[F]" ,"Scene Editor => Focus camera on selected object");
  65. RegisterKeyBind( MENU_SCENE_EDITOR ,KeyCode.KC_LCONTROL ,KeyCode.KC_E ,"PluginSceneManager" ,"RulerToggle" ,"[LCtrl]+[E]" ,"Scene Editor => Enable/Disable addition of ruler points");
  66. RegisterKeyBind( MENU_NONE ,KeyCode.KC_LCONTROL ,KeyCode.KC_PRIOR ,"PluginRecipesManager" ,"CallbackGenerateCache" ,"[LCtrl]+[BACKSPACE]" ,"PluginRecipesManager => Generate recipe cache and save it to file");
  67. RegisterKeyBind( MENU_ANY ,KeyCode.KC_LCONTROL ,KeyCode.KC_DECIMAL ,"PluginItemDiagnostic" ,"ToggleDebugWindowEvent" ,"[LCtrl]+[DECIMAL]" ,"Close the Item Debug window");
  68. RegisterKeyBind( MENU_ANY ,KeyCode.KC_LCONTROL ,KeyCode.KC_NUMPAD0 ,"PluginDayzPlayerDebug","ToggleDebugWindowEvent" ,"[LCtrl]+[NUMPAD0]" ,"Open/Close Player debug window");
  69. RegisterKeyBind( MENU_ANY ,KeyCode.KC_LCONTROL ,KeyCode.KC_DECIMAL ,"PluginDayzPlayerDebug","ToggleDebugWindowEventP" ,"[LCtrl]+[DECIMAL]" ,"Open/Close Player debug window");
  70. RegisterKeyBind( MENU_ANY ,KeyCode.KC_LCONTROL ,KeyCode.KC_I ,"PluginDayZInfectedDebug","ToggleDebugWindowEvent" ,"[LCtrl]+[NUMPAD1]" ,"Open/Close Infected debug window");
  71. RegisterKeyBind( MENU_ANY ,KeyCode.KC_LCONTROL ,KeyCode.KC_O ,"PluginDayZInfectedDebug","PossesInfected" ,"[LCtrl]+[NUMPAD1]" ,"Open/Close Infected debug window");
  72. RegisterKeyBind( MENU_NONE ,KeyCode.KC_LCONTROL ,KeyCode.KC_SCROLL ,"PluginNutritionDumper" ,"CheckInit" ,"[LCtrl]+[SCROLL LOCK]" ,"Enable Door Ruler");
  73. RegisterKeyBind( MENU_NONE ,KeyCode.KC_LCONTROL ,KeyCode.KC_RBRACKET,"PluginCharPlacement" ,"CheckInit" ,"[LCtrl]+[)]" ,"Enable CharPlacer");
  74. RegisterKeyBind( MENU_NONE|MENU_CAMERA_TOOLS ,KeyCode.KC_LCONTROL ,KeyCode.KC_K ,"PluginCameraTools" ,"ToggleCameraTools" ,"[LCtrl]+[K]" ,"Toggle Camera Recording Tools");
  75. RegisterKeyBind( MENU_CAMERA_TOOLS ,KeyCode.KC_LCONTROL ,KeyCode.KC_S ,"PluginCameraTools" ,"Save" ,"[X]" ,"Recording Tools => Start Recording Camera Path");
  76. RegisterKeyBind( MENU_CAMERA_TOOLS ,KeyCode.KC_P ,-1 ,"PluginCameraTools" ,"PlayPause" ,"[C]" ,"Recording Tools => Stop Recording Camera Path");
  77. RegisterKeyBind( MENU_CAMERA_TOOLS ,KeyCode.KC_LCONTROL ,KeyCode.KC_Q ,"PluginCameraTools" ,"AddKeyframe" ,"[C]" ,"Recording Tools => Add Camera Point");
  78. RegisterKeyBind( MENU_CAMERA_TOOLS ,KeyCode.KC_LCONTROL ,KeyCode.KC_E ,"PluginCameraTools" ,"DeleteKeyframe" ,"[V]" ,"Recording Tools => Play Camera Line");
  79. //--------------------------------------------------------------------------------------------------------------------------------------------------------------
  80. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. // Mouses Binds |UI_ID |Mouse Button |Mouse Event |Callback Plugin |Callback Function |Info Shrtcut |Info Description
  83. //----------------------------------------------------------------------------------------------------------------------------------------------------------------
  84. // |constants.h |MouseState.LEFT |MB_EVENT_CLICK |only plugin name |only function | |
  85. // |MENU_*** |MouseState.RIGHT |MB_EVENT_DOUBLECLICK | |in plugin | |
  86. // |MouseState.MIDDLE |MB_EVENT_DRAG | | | |
  87. // | |MB_EVENT_RELEASE | | | |
  88. //--------------------------------------------------------------------------------------------------------------------------------------------------------------
  89. RegisterMouseBind( MENU_SCENE_EDITOR ,MouseState.LEFT ,MB_EVENT_CLICK ,"PluginSceneManager" ,"Event_OnClick" ,"[LMB] Click" ,"Mission Editor => Select object in scene");
  90. RegisterMouseBind( MENU_SCENE_EDITOR ,MouseState.LEFT ,MB_EVENT_DOUBLECLICK ,"PluginSceneManager" ,"Event_OnDoubleClick" ,"[LMB] Double Click" ,"Mission Editor => Craete new object in scene");
  91. RegisterMouseBind( MENU_SCENE_EDITOR ,MouseState.LEFT ,MB_EVENT_DRAG ,"PluginSceneManager" ,"Event_OnDrag" ,"[LMB] Hold" ,"Mission Editor => Moving with selected object");
  92. RegisterMouseBind( MENU_SCENE_EDITOR ,MouseState.MIDDLE ,MB_EVENT_CLICK ,"PluginSceneManager" ,"Event_OnClickMiddle" ,"[MMB] Click" ,"Mission Editor => Link clicked object with selected object");
  93. RegisterMouseBind( MENU_SCENE_EDITOR ,MouseState.RIGHT ,MB_EVENT_PRESS ,"PluginSceneManager" ,"FreeCameraControlEnable" ,"[RMB] Hold" ,"Mission Editor => Enable camera movement");
  94. RegisterMouseBind( MENU_SCENE_EDITOR ,MouseState.RIGHT ,MB_EVENT_RELEASE ,"PluginSceneManager" ,"FreeCameraControlDisable" ,"[RMB] Release" ,"Mission Editor => Disable camera movement");
  95. RegisterMouseBind( MENU_NONE ,MouseState.LEFT ,MB_EVENT_CLICK ,"PluginDayZCreatureAIDebug","Event_OnClick" ,"[LMB] Click" ,"DayZCreature graph debug => Capture entity during capture mode");
  96. RegisterMouseBind( MENU_CAMERA_TOOLS ,MouseState.RIGHT ,MB_EVENT_RELEASE ,"PluginCameraTools" ,"CursorShow" ,"[LMB] Click" ,"DayZCreature graph debug => Capture entity during capture mode");
  97. // TO DO: (Boris V.) Remove the following commented-out mouse binds when weapon particles can be implemented properly. Also remove the events they call.
  98. //RegisterMouseBind( MENU_NONE ,MouseState.LEFT ,MB_EVENT_PRESS ,"PluginDeveloper" ,"MuzzleParticleTestStart" ,"[LMB] Hold" ,"Debug particle");
  99. //RegisterMouseBind( MENU_NONE ,MouseState.LEFT ,MB_EVENT_RELEASE ,"PluginDeveloper" ,"MuzzleParticleTestEnd" ,"[LMB] Release" ,"Debug particle2");
  100. #endif
  101. //--------------------------------------------------------------------------------------------------------------------------------------------------------------
  102. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  103. }
  104. //==========================================
  105. // OnInit (System Event)
  106. //==========================================
  107. override void OnDestroy()
  108. {
  109. GetGame().GetUpdateQueue(CALL_CATEGORY_SYSTEM).Remove(this.OnFrame);
  110. }
  111. //============================================
  112. // OnKeyPress
  113. //============================================
  114. void OnKeyPress(int key)
  115. {
  116. //Print("OnKeyPress "+ key );
  117. for ( int i = 0; i < m_KeyBindings.Count(); ++i )
  118. {
  119. KeyBinding key_binding = m_KeyBindings.Get(i);
  120. if ( !IsCurrentUIPageValid( key_binding.GetUIMenuID() ) )
  121. {
  122. continue;
  123. }
  124. bool key1_match = false;
  125. bool key2_match = false;
  126. if ( key_binding.GetKey1() > -1 )
  127. {
  128. if ( IsKeyPressed(key_binding.GetKey1()) )
  129. {
  130. // Key1 is down
  131. key1_match = true;
  132. }
  133. }
  134. else
  135. {
  136. // Key1 is not set
  137. key1_match = true;
  138. }
  139. if ( key_binding.GetKey2() > -1 )
  140. {
  141. if ( IsKeyPressed(key_binding.GetKey2()) )
  142. {
  143. // Key2 is down
  144. key2_match = true;
  145. }
  146. }
  147. else
  148. {
  149. // Key2 is not set
  150. key2_match = true;
  151. }
  152. if ( key1_match && key2_match )
  153. {
  154. //Log( "Call Method -> key press: " + itoa(key) + " " + key_binding.GetInfoDescription() );
  155. PluginBase m = GetPlugin(key_binding.GetCallbackTarget().ToType());
  156. GetGame().GameScript.CallFunction(m, key_binding.GetCallbackFunction(), NULL, 0);
  157. break;
  158. }
  159. }
  160. }
  161. //============================================
  162. // OnKeyRelease
  163. //============================================
  164. void OnKeyRelease(int key)
  165. {
  166. }
  167. //============================================
  168. // OnMouseButtonPress
  169. //============================================
  170. void OnMouseButtonPress(int button)
  171. {
  172. MouseButtonInfo button_info = GetMouseButtonInfo( button );
  173. if (button_info == NULL) return;
  174. button_info.Press();
  175. //Log("****************************");
  176. //Log(" MB_EVENT_PRESS");
  177. OnMouse( MB_EVENT_PRESS, button );
  178. }
  179. //============================================
  180. // OnMouseButtonRelease
  181. //============================================
  182. void OnMouseButtonRelease(int button)
  183. {
  184. MouseButtonInfo button_info = GetMouseButtonInfo( button );
  185. if (button_info == NULL) return;
  186. int time_curr = GetGame().GetTime();
  187. int time_last_press = button_info.GetTimeLastPress();
  188. int time_last_release = button_info.GetTimeLastRelease();
  189. int time_delta_press = time_curr - time_last_press;
  190. int time_delta_relase = time_curr - time_last_release;
  191. if ( time_delta_relase < DOUBLE_CLICK_TIME )
  192. {
  193. //Log(" DOUBLE_CLICK_TIME ");
  194. OnMouse( MB_EVENT_DOUBLECLICK, button );
  195. }
  196. else if ( time_delta_press < CLICK_TIME )
  197. {
  198. //Log(" MB_EVENT_CLICK ");
  199. OnMouse( MB_EVENT_CLICK, button );
  200. }
  201. //Log(" MB_EVENT_RELEASE");
  202. OnMouse( MB_EVENT_RELEASE, button );
  203. button_info.Release();
  204. }
  205. //============================================
  206. // OnFrame
  207. //============================================
  208. void OnFrame()
  209. {
  210. for ( int i = 0; i < m_MouseButtons.Count(); ++i )
  211. {
  212. MouseButtonInfo info = m_MouseButtons.Get(i);
  213. if ( info.IsButtonDown() )
  214. {
  215. int time_curr = GetGame().GetTime();
  216. int time_hold = info.GetTimeLastPress() + HOLD_CLICK_TIME_MIN;
  217. if ( time_hold < time_curr )
  218. {
  219. //Log("MB_EVENT_DRAG");
  220. OnMouse( MB_EVENT_DRAG, info.GetButtonID() );
  221. }
  222. }
  223. }
  224. }
  225. //--------------------------------------------
  226. // GetKeyBindings
  227. //--------------------------------------------
  228. array<ref KeyBinding> GetKeyBindings()
  229. {
  230. return m_KeyBindings;
  231. }
  232. //--------------------------------------------
  233. // GetMouseBindings
  234. //--------------------------------------------
  235. array<ref MouseBinding> GetMouseBindings()
  236. {
  237. return m_MouseBindings;
  238. }
  239. protected const int CLICK_TIME = 200; //ms
  240. protected const int DOUBLE_CLICK_TIME = 300; //ms
  241. protected const int HOLD_CLICK_TIME_MIN = 300; //ms
  242. protected ref array<ref KeyBinding> m_KeyBindings;
  243. protected ref array<ref MouseButtonInfo> m_MouseButtons;
  244. protected ref array<ref MouseBinding> m_MouseBindings;
  245. //--------------------------------------------
  246. // RegisterKeyBind
  247. //--------------------------------------------
  248. protected void RegisterKeyBind( int ui_id, int key_code1, int key_code2, string plugin_name, string fnc_name, string info_shortcut, string info_description )
  249. {
  250. m_KeyBindings.Insert( new KeyBinding(ui_id, key_code1, key_code2, plugin_name, fnc_name, info_shortcut, info_description) );
  251. }
  252. //--------------------------------------------
  253. // RegisterMouseBind
  254. //--------------------------------------------
  255. protected void RegisterMouseBind( int ui_id, int mouse_button, int mouse_event, string plugin_name, string fnc_name, string info_shortcut, string info_description )
  256. {
  257. m_MouseBindings.Insert( new MouseBinding(ui_id, mouse_button, mouse_event, plugin_name, fnc_name, info_shortcut, info_description) );
  258. }
  259. //--------------------------------------------
  260. // IsCurrentUIPageValid
  261. //--------------------------------------------
  262. protected bool IsCurrentUIPageValid( int ui_page_request )
  263. {
  264. int ui_page_current = MENU_NONE;
  265. if ( GetGame().GetUIManager().GetMenu() )
  266. {
  267. ui_page_current = GetGame().GetUIManager().GetMenu().GetID();
  268. }
  269. if ( ui_page_current < MENU_ANY || !CheckMask( ui_page_request, ui_page_current ) )
  270. {
  271. if ( CheckMask( ui_page_request, MENU_NONE ) || !CheckMask( ui_page_request, MENU_ANY ) )
  272. {
  273. return false;
  274. }
  275. }
  276. return true;
  277. }
  278. bool CheckMask( int source_mask, int target_mask )
  279. {
  280. if ( ( source_mask & target_mask ) == target_mask )
  281. {
  282. return true;
  283. }
  284. return false;
  285. }
  286. //--------------------------------------------
  287. // OnMouse
  288. //--------------------------------------------
  289. protected void OnMouse( int event_id, int button )
  290. {
  291. for ( int i = 0; i < m_MouseBindings.Count(); ++i )
  292. {
  293. MouseBinding mouse_binding = m_MouseBindings.Get(i);
  294. if ( mouse_binding.GetButtonID() == button && mouse_binding.GetMouseEvent() == event_id )
  295. {
  296. if ( IsCurrentUIPageValid( mouse_binding.GetUIMenuID() ) )
  297. {
  298. PluginBase m = GetPlugin(mouse_binding.GetCallbackTarget().ToType());
  299. GetGame().GameScript.CallFunction(m, mouse_binding.GetCallbackFunction(), NULL, 0);
  300. }
  301. }
  302. }
  303. }
  304. //--------------------------------------------
  305. // OnMouse
  306. //--------------------------------------------
  307. protected MouseButtonInfo GetMouseButtonInfo( int button )
  308. {
  309. for ( int i = 0; i < m_MouseButtons.Count(); ++i )
  310. {
  311. MouseButtonInfo info = m_MouseButtons.Get(i);
  312. if ( info.GetButtonID() == button )
  313. {
  314. return info;
  315. }
  316. }
  317. return NULL;
  318. }
  319. //--------------------------------------------
  320. // IsKeyPressed
  321. //--------------------------------------------
  322. protected bool IsKeyPressed(int key)
  323. {
  324. return ( KeyState(key) == 1 );
  325. }
  326. }