keybindingscontainer.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //container for various subgroups and their elements
  2. class KeybindingsContainer extends ScriptedWidgetEventHandler
  3. {
  4. protected const int NO_SORTING = -1;
  5. protected Widget m_Root;
  6. protected KeybindingsMenu m_Menu;
  7. protected ScrollWidget m_Scroll;
  8. protected ref map<int, ref ElementArray> m_KeyWidgetElements; //<input_action_id,<KeybindingElementNew>>
  9. protected int m_CurrentSettingKeyIndex = -1;
  10. protected int m_CurrentSettingAlternateKeyIndex = -1;
  11. protected ref array<Widget> m_Subgroups;
  12. void KeybindingsContainer( int index, Input input, Widget parent, KeybindingsMenu menu )
  13. {
  14. m_KeyWidgetElements = new map<int, ref ElementArray>;
  15. m_Menu = menu;
  16. m_Root = GetGame().GetWorkspace().CreateWidgets( GetLayoutName(), parent );
  17. m_Scroll = ScrollWidget.Cast(m_Root.FindAnyWidget("group_scroll"));
  18. Widget container = m_Root.FindAnyWidget( "group_scroll" );
  19. CreateSubgroups(container,input);
  20. container.Update();
  21. m_Root.SetHandler( this );
  22. }
  23. string GetLayoutName()
  24. {
  25. return "gui/layouts/new_ui/options/keybindings_selectors/keybinding_container.layout";
  26. }
  27. void OnSelectKBPreset( int index )
  28. {
  29. GetUApi().PresetSelect( index );
  30. ReloadElements();
  31. GetUApi().Export();
  32. GetUApi().SaveInputPresetMiscData();
  33. }
  34. void ReloadElements()
  35. {
  36. foreach ( ElementArray elements : m_KeyWidgetElements )
  37. {
  38. foreach (KeybindingElementNew element : elements)
  39. {
  40. element.Reload();
  41. }
  42. }
  43. }
  44. void AddSubgroup( int sort_index, Widget parent, Input input )
  45. {
  46. Widget subgroup = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/new_ui/options/keybindings_selectors/keybinding_subgroup.layout", parent );
  47. Widget subgroup_content = subgroup.FindAnyWidget( "subgroup_content" );
  48. TIntArray input_actions;
  49. if (sort_index != NO_SORTING)
  50. {
  51. input_actions = InputUtils.GetInputActionSortingMap().Get(sort_index);
  52. }
  53. else
  54. {
  55. input_actions = InputUtils.GetUnsortedInputActions();
  56. }
  57. for (int i = 0; i < input_actions.Count(); i++)
  58. {
  59. AddElement( input_actions[i], subgroup_content, input );
  60. }
  61. m_Subgroups.Insert(subgroup);
  62. subgroup_content.Update();
  63. }
  64. void CreateSubgroups( Widget parent, Input input )
  65. {
  66. m_Subgroups = new array<Widget>;
  67. int sort_count = InputUtils.GetInputActionSortingMap().Count();
  68. for (int i = 0; i < sort_count; i++)
  69. {
  70. if (InputUtils.GetInputActionSortingMap().GetElement(i) && InputUtils.GetInputActionSortingMap().GetElement(i).Count() > 0)
  71. {
  72. AddSubgroup(InputUtils.GetInputActionSortingMap().GetKey(i),parent,input);
  73. }
  74. }
  75. //any unssorted inputs go here
  76. if (InputUtils.GetUnsortedInputActions() && InputUtils.GetUnsortedInputActions().Count() > 0)
  77. {
  78. AddSubgroup(NO_SORTING,parent,input);
  79. }
  80. }
  81. void AddElement( int index, Widget parent, Input input )
  82. {
  83. //create array if needed
  84. if (!m_KeyWidgetElements.Get(index))
  85. {
  86. ElementArray elements = new ElementArray;
  87. m_KeyWidgetElements.Insert( index, elements );
  88. }
  89. KeybindingElementNew element = new KeybindingElementNew( index, parent, this );
  90. m_KeyWidgetElements.Get(index).Insert(element);
  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. foreach (KeybindingElementNew element : m_KeyWidgetElements.Get( m_CurrentSettingKeyIndex ))
  115. {
  116. element.CancelEnteringKeybind();
  117. }
  118. m_CurrentSettingKeyIndex = -1;
  119. }
  120. }
  121. void StartEnteringAlternateKeybind( int key_index )
  122. {
  123. m_CurrentSettingKeyIndex = -1;
  124. m_CurrentSettingAlternateKeyIndex = key_index;
  125. m_Menu.StartEnteringAlternateKeybind( key_index );
  126. }
  127. void CancelEnteringAlternateKeybind()
  128. {
  129. if ( m_CurrentSettingAlternateKeyIndex != -1 )
  130. {
  131. foreach (KeybindingElementNew element : m_KeyWidgetElements.Get( m_CurrentSettingAlternateKeyIndex ))
  132. {
  133. element.CancelEnteringKeybind();
  134. }
  135. m_CurrentSettingAlternateKeyIndex = -1;
  136. }
  137. }
  138. //! is anything changed?
  139. bool IsChanged()
  140. {
  141. foreach (ElementArray elements : m_KeyWidgetElements)
  142. {
  143. foreach (KeybindingElementNew element : elements)
  144. {
  145. if (element.IsChanged() || element.IsAlternateChanged())
  146. {
  147. return true;
  148. }
  149. }
  150. }
  151. return false;
  152. }
  153. void Apply()
  154. {
  155. UAInputAPI ua_api = GetUApi();
  156. int input_index = 0;
  157. foreach (ElementArray elements : m_KeyWidgetElements)
  158. {
  159. foreach (int element_index, KeybindingElementNew element : elements)
  160. {
  161. if (element_index == 0)
  162. {
  163. UAInput input = ua_api.GetInputByID( m_KeyWidgetElements.GetKey(input_index) );
  164. int i;
  165. if ( element.IsChanged() )
  166. {
  167. array<int> new_keys = element.GetChangedBinds();
  168. input.ClearDeviceBind(EUAINPUT_DEVICE_KEYBOARDMOUSE);
  169. if ( new_keys.Count() > 0 )
  170. {
  171. input.BindComboByHash( new_keys.Get( 0 ) );
  172. for( i = 1; i < new_keys.Count(); i++ )
  173. {
  174. input.BindComboByHash( new_keys.Get( i ) );
  175. }
  176. }
  177. }
  178. if ( element.IsAlternateChanged() )
  179. {
  180. array<int> new_alt_keys = element.GetChangedAlternateBinds();
  181. if ( input.AlternativeCount() == 0 )
  182. {
  183. input.AddAlternative();
  184. }
  185. input.ClearDeviceBind(EUAINPUT_DEVICE_CONTROLLER);
  186. if ( new_alt_keys.Count() > 0 )
  187. {
  188. input.BindComboByHash( new_alt_keys.Get( 0 ) );
  189. for( i = 1; i < new_alt_keys.Count(); i++ )
  190. {
  191. input.BindComboByHash( new_alt_keys.Get( i ) );
  192. }
  193. }
  194. }
  195. }
  196. element.Reload();
  197. }
  198. input_index++;
  199. }
  200. }
  201. void Reset(bool forced = false)
  202. {
  203. foreach ( ElementArray elements : m_KeyWidgetElements )
  204. {
  205. foreach (KeybindingElementNew element : elements)
  206. {
  207. if ( element.IsChanged() || element.IsAlternateChanged() || forced )
  208. {
  209. element.Reload();
  210. }
  211. }
  212. }
  213. }
  214. void Update( float timeslice )
  215. {
  216. if ( m_CurrentSettingKeyIndex != -1 || m_CurrentSettingAlternateKeyIndex != -1 )
  217. {
  218. UAInputAPI ua_api = GetUApi();
  219. if ( ua_api.DeterminePressedButton() != 0 )
  220. {
  221. string name;
  222. string text;
  223. ref array<int> new_keybinds = new array<int>;
  224. // remove previous backlit
  225. GetDayZGame().GetBacklit().KeybindingClear();
  226. for( int i = 0; i < ua_api.DeterminedCount(); ++i )
  227. {
  228. int kb_id = ua_api.GetDetermined( i );
  229. new_keybinds.Insert( kb_id );
  230. // light up specific key
  231. GetDayZGame().GetBacklit().KeybindingShow(kb_id);
  232. }
  233. if ( m_CurrentSettingKeyIndex != -1 )
  234. {
  235. m_Menu.ConfirmKeybindEntry( new_keybinds );
  236. foreach (KeybindingElementNew element : m_KeyWidgetElements.Get( m_CurrentSettingKeyIndex ))
  237. {
  238. element.Reload( new_keybinds, false );
  239. }
  240. m_CurrentSettingKeyIndex = -1;
  241. }
  242. else if ( m_CurrentSettingAlternateKeyIndex != -1 )
  243. {
  244. m_Menu.ConfirmAlternateKeybindEntry( new_keybinds );
  245. foreach (KeybindingElementNew elementAlternate : m_KeyWidgetElements.Get( m_CurrentSettingAlternateKeyIndex ))
  246. {
  247. elementAlternate.Reload( new_keybinds, true );
  248. }
  249. m_CurrentSettingAlternateKeyIndex = -1;
  250. }
  251. }
  252. }
  253. }
  254. void SwitchSubgroup(int index)
  255. {
  256. Widget w;
  257. for (int i = 0; i < m_Subgroups.Count(); i++)
  258. {
  259. w = m_Subgroups[i];
  260. w.Show(index == i);
  261. }
  262. m_Scroll.VScrollToPos01(0);
  263. }
  264. }
  265. typedef array<ref KeybindingElementNew> ElementArray;