keybindingsgroup.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //! Deprecated
  2. class KeybindingsGroup extends ScriptedWidgetEventHandler
  3. {
  4. protected Widget m_Root;
  5. protected KeybindingsMenu m_Menu;
  6. protected ref map<int, ref KeybindingElement> m_KeyWidgets;
  7. protected int m_CurrentSettingKeyIndex = -1;
  8. protected int m_CurrentSettingAlternateKeyIndex = -1;
  9. protected ref DropdownPrefab m_KBDropdown;
  10. void KeybindingsGroup( int index, Input input, Widget parent, KeybindingsMenu menu )
  11. {
  12. m_KeyWidgets = new map<int, ref KeybindingElement>;
  13. m_Menu = menu;
  14. string group_name;
  15. input.GetActionGroupName( index, group_name );
  16. m_Root = GetGame().GetWorkspace().CreateWidgets( GetLayoutName(), parent );
  17. Widget subgroup = m_Root.FindAnyWidget( "group_content" );
  18. // for( int i = 0; i < 1; i++ )
  19. // {
  20. AddSubgroup( /*index, */subgroup, input );
  21. // }
  22. InitPresets( index, m_Root.FindAnyWidget( "group_header" ), input );
  23. subgroup.Update();
  24. m_Root.SetHandler( this );
  25. }
  26. string GetLayoutName()
  27. {
  28. return "gui/layouts/new_ui/options/keybindings_selectors/keybinding_group.layout";
  29. }
  30. void InitPresets( int index, Widget parent, Input input )
  31. {
  32. Widget kb_root = parent.FindAnyWidget( "keyboard_dropown" );
  33. string profile_text;
  34. input.GetProfileName( input.GetCurrentProfile(), profile_text );
  35. m_KBDropdown = new DropdownPrefab( kb_root, profile_text );
  36. m_KBDropdown.m_OnSelectItem.Insert( OnSelectKBPreset );
  37. for( int i = 0; i < input.GetProfilesCount(); i++ )
  38. {
  39. input.GetProfileName( i, profile_text );
  40. m_KBDropdown.AddElement( profile_text );
  41. }
  42. kb_root.Update();
  43. }
  44. void OnSelectKBPreset( int index )
  45. {
  46. string profile_text;
  47. GetGame().GetInput().GetProfileName( index, profile_text );
  48. m_KBDropdown.SetText( profile_text );
  49. GetGame().GetInput().SetProfile( index );
  50. ReloadProfiles();
  51. m_KBDropdown.Close();
  52. }
  53. void OnSelectConsolePreset( int index )
  54. {
  55. string profile_text;
  56. GetGame().GetInput().GetProfileName( index, profile_text );
  57. GetGame().GetInput().SetProfile( index );
  58. ReloadProfiles();
  59. }
  60. void ReloadProfiles()
  61. {
  62. foreach( int index, KeybindingElement element : m_KeyWidgets )
  63. {
  64. element.Reload();
  65. }
  66. }
  67. void AddSubgroup( /*int index, */Widget parent, Input input )
  68. {
  69. Widget subgroup = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/new_ui/options/keybindings_selectors/keybinding_subgroup.layout", parent );
  70. TextWidget subgroup_name = TextWidget.Cast( subgroup.FindAnyWidget( "subgroup_text" ) );
  71. subgroup_name.SetText( "TestSubgroup" );
  72. Widget subgroup_content = subgroup.FindAnyWidget( "subgroup_content" );
  73. TIntArray actions = new TIntArray;
  74. GetUApi().GetActiveInputs(actions);
  75. for( int i = 0; i < actions.Count(); i++ )
  76. {
  77. AddAction( actions.Get( i ), subgroup_content, input );
  78. }
  79. subgroup_content.Update();
  80. }
  81. void AddAction( int index, Widget parent, Input input )
  82. {
  83. m_KeyWidgets.Insert( index, new KeybindingElement( index, parent, this ) );
  84. }
  85. void ReloadAction( int index )
  86. {
  87. if( m_KeyWidgets.Contains( index ) )
  88. {
  89. m_KeyWidgets.Get( index ).Reload();
  90. }
  91. }
  92. bool IsEnteringKeyBind()
  93. {
  94. return ( m_CurrentSettingKeyIndex != -1 || m_CurrentSettingAlternateKeyIndex != -1 );
  95. }
  96. void ClearKeybind( int key_index )
  97. {
  98. m_Menu.ClearKeybind( key_index );
  99. }
  100. void ClearAlternativeKeybind( int key_index )
  101. {
  102. m_Menu.ClearAlternativeKeybind( key_index );
  103. }
  104. void StartEnteringKeybind( int key_index )
  105. {
  106. m_CurrentSettingAlternateKeyIndex = -1;
  107. m_CurrentSettingKeyIndex = key_index;
  108. m_Menu.StartEnteringKeybind( key_index );
  109. }
  110. void CancelEnteringKeybind()
  111. {
  112. if( m_CurrentSettingKeyIndex != -1 )
  113. {
  114. m_KeyWidgets.Get( m_CurrentSettingKeyIndex ).CancelEnteringKeybind();
  115. m_CurrentSettingKeyIndex = -1;
  116. }
  117. }
  118. void StartEnteringAlternateKeybind( int key_index )
  119. {
  120. m_CurrentSettingKeyIndex = -1;
  121. m_CurrentSettingAlternateKeyIndex = key_index;
  122. m_Menu.StartEnteringAlternateKeybind( key_index );
  123. }
  124. void CancelEnteringAlternateKeybind()
  125. {
  126. if( m_CurrentSettingAlternateKeyIndex != -1 )
  127. {
  128. m_KeyWidgets.Get( m_CurrentSettingAlternateKeyIndex ).CancelEnteringAlternateKeybind();
  129. m_CurrentSettingAlternateKeyIndex = -1;
  130. }
  131. }
  132. bool IsChanged()
  133. {
  134. foreach( int index, KeybindingElement element : m_KeyWidgets )
  135. {
  136. if( element.IsChanged() || element.IsAlternateChanged() )
  137. {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. void Apply()
  144. {
  145. UAInputAPI ua_api = GetUApi();
  146. foreach( int index, KeybindingElement element : m_KeyWidgets )
  147. {
  148. UAInput input = ua_api.GetInputByID( index );
  149. int i;
  150. if( element.IsChanged() )
  151. {
  152. array<int> new_keys = element.GetChangedBinds();
  153. /* if( input.AlternativeCount() == 0 )
  154. {
  155. input.AddAlternative();
  156. }
  157. else*/
  158. {
  159. input.ClearDeviceBind(EUAINPUT_DEVICE_KEYBOARDMOUSE);
  160. // input.SelectAlternative( 0 );
  161. // input.ClearAlternative( 0 );
  162. }
  163. if( new_keys.Count() > 0 )
  164. {
  165. input.BindComboByHash( new_keys.Get( 0 ) );
  166. for( i = 1; i < new_keys.Count(); i++ )
  167. {
  168. input.BindComboByHash( new_keys.Get( i ) );
  169. }
  170. }
  171. }
  172. if( element.IsAlternateChanged() )
  173. {
  174. array<int> new_alt_keys = element.GetChangedAlternateBinds();
  175. if( input.AlternativeCount() == 0 )
  176. {
  177. input.AddAlternative();
  178. }
  179. /* if( input.AlternativeCount() < 2 )
  180. {
  181. input.AddAlternative();
  182. }
  183. else*/
  184. {
  185. input.ClearDeviceBind(EUAINPUT_DEVICE_CONTROLLER);
  186. // input.SelectAlternative( 1 );
  187. // input.ClearAlternative( 1 );
  188. }
  189. if( new_alt_keys.Count() > 0 )
  190. {
  191. input.BindComboByHash( new_alt_keys.Get( 0 ) );
  192. for( i = 1; i < new_alt_keys.Count(); i++ )
  193. {
  194. input.BindComboByHash( new_alt_keys.Get( i ) );
  195. }
  196. }
  197. }
  198. element.Reload();
  199. }
  200. }
  201. //DEPRECATED
  202. void Reset()
  203. {
  204. ResetEx();
  205. }
  206. void ResetEx(bool forced = false)
  207. {
  208. foreach( int index, KeybindingElement element : m_KeyWidgets )
  209. {
  210. if( element.IsChanged() || element.IsAlternateChanged() || forced )
  211. {
  212. element.Reload();
  213. }
  214. }
  215. }
  216. void Update( float timeslice )
  217. {
  218. if( m_CurrentSettingKeyIndex != -1 || m_CurrentSettingAlternateKeyIndex != -1 )
  219. {
  220. UAInputAPI ua_api = GetUApi();
  221. if( ua_api.DeterminePressedButton() != 0 )
  222. {
  223. string name;
  224. string text;
  225. ref array<int> new_keybinds = new array<int>;
  226. // remove previous backlit
  227. GetDayZGame().GetBacklit().KeybindingClear();
  228. for( int i = 0; i < ua_api.DeterminedCount(); ++i )
  229. {
  230. int kb_id = ua_api.GetDetermined( i );
  231. new_keybinds.Insert( kb_id );
  232. // light up specific key
  233. GetDayZGame().GetBacklit().KeybindingShow(kb_id);
  234. }
  235. if( m_CurrentSettingKeyIndex != -1 )
  236. {
  237. m_Menu.ConfirmKeybindEntry( new_keybinds );
  238. m_KeyWidgets.Get( m_CurrentSettingKeyIndex ).Reload( new_keybinds, false );
  239. m_CurrentSettingKeyIndex = -1;
  240. }
  241. else if( m_CurrentSettingAlternateKeyIndex != -1 )
  242. {
  243. m_Menu.ConfirmAlternateKeybindEntry( new_keybinds );
  244. m_KeyWidgets.Get( m_CurrentSettingAlternateKeyIndex ).Reload( new_keybinds, true );
  245. m_CurrentSettingAlternateKeyIndex = -1;
  246. }
  247. }
  248. }
  249. }
  250. override bool OnMouseButtonDown( Widget w, int x, int y, int button )
  251. {
  252. if( !ButtonWidget.Cast( w ) )
  253. {
  254. m_KBDropdown.Close();
  255. }
  256. return false;
  257. }
  258. }