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