keybindingsmenu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. class KeybindingsMenu extends UIScriptedMenu
  2. {
  3. protected TabberUI m_Tabber;
  4. protected ref DropdownPrefab m_KBDropdown; //DEPRECATED
  5. protected ref OptionSelectorMultistate m_PresetSelector;
  6. protected ref KeybindingsContainer m_GroupsContainer;
  7. protected ref array<ref KeybindingsGroup> m_Tabs; //DEPRECATED
  8. protected TextWidget m_Version;
  9. protected ButtonWidget m_Apply;
  10. protected ButtonWidget m_Back;
  11. protected ButtonWidget m_Undo;
  12. protected ButtonWidget m_Defaults;
  13. protected ButtonWidget m_HardReset;
  14. protected int m_CurrentSettingKeyIndex = -1;
  15. protected int m_CurrentSettingAlternateKeyIndex = -1;
  16. protected int m_OriginalPresetIndex;
  17. protected int m_TargetPresetIndex;
  18. protected ref array<int> m_SetKeybinds;
  19. const int MODAL_ID_BACK = 1337;
  20. const int MODAL_ID_DEFAULT = 100;
  21. const int MODAL_ID_DEFAULT_ALL = 101;
  22. const int MODAL_ID_PRESET_CHANGE = 200;
  23. const int MODAL_RESULT_DEFAULT_CURRENT = 0;
  24. const int MODAL_RESULT_DEFAULT_ALL = 1;
  25. override Widget Init()
  26. {
  27. Input input = GetGame().GetInput();
  28. layoutRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/options/pc/keybinding_menu.layout", null);
  29. m_Version = TextWidget.Cast(layoutRoot.FindAnyWidget("version"));
  30. m_Apply = ButtonWidget.Cast(layoutRoot.FindAnyWidget("apply"));
  31. m_Back = ButtonWidget.Cast(layoutRoot.FindAnyWidget("back"));
  32. m_Undo = ButtonWidget.Cast(layoutRoot.FindAnyWidget("undo"));
  33. m_Defaults = ButtonWidget.Cast(layoutRoot.FindAnyWidget("reset"));
  34. m_HardReset = ButtonWidget.Cast(layoutRoot.FindAnyWidget("reset_all"));
  35. layoutRoot.FindAnyWidget("Tabber").GetScript(m_Tabber);
  36. string version;
  37. GetGame().GetVersion(version);
  38. #ifdef PLATFORM_CONSOLE
  39. version = "#main_menu_version" + " " + version + " (" + g_Game.GetDatabaseID() + ")";
  40. #else
  41. version = "#main_menu_version" + " " + version;
  42. #endif
  43. m_Version.SetText(version);
  44. #ifdef PLATFORM_PS4
  45. string back = "circle";
  46. if (GetGame().GetInput().GetEnterButton() != GamepadButton.A)
  47. back = "cross";
  48. ImageWidget toolbar_b = ImageWidget.Cast(layoutRoot.FindAnyWidget("BackIcon"));
  49. toolbar_b.LoadImageFile(0, "set:playstation_buttons image:" + back);
  50. #endif
  51. InitInputSortingMap();
  52. CreateTabs();
  53. CreateGroupContainer();
  54. InitPresets(-1, layoutRoot.FindAnyWidget("group_header"), input);
  55. m_Tabber.m_OnTabSwitch.Insert(UpdateTabContent);
  56. m_Tabber.SelectTabControl(0);
  57. m_Tabber.SelectTabPanel(0);
  58. g_Game.SetKeyboardHandle(this);
  59. m_Tabber.RefreshTab(true);
  60. ColorDisabled(m_Apply);
  61. m_Apply.SetFlags(WidgetFlags.IGNOREPOINTER);
  62. ColorDisabled(m_Undo);
  63. m_Undo.SetFlags(WidgetFlags.IGNOREPOINTER);
  64. ColorWhite(m_Defaults, null);
  65. m_Defaults.ClearFlags(WidgetFlags.IGNOREPOINTER);
  66. return layoutRoot;
  67. }
  68. void CreateTabs()
  69. {
  70. int sort_count = InputUtils.GetInputActionSortingMap().Count();
  71. for (int i = 0; i < sort_count; i++)
  72. {
  73. if (InputUtils.GetInputActionSortingMap().GetElement(i) && InputUtils.GetInputActionSortingMap().GetElement(i).Count() > 0)
  74. {
  75. string group_name = GetUApi().SortingLocalization(InputUtils.GetInputActionSortingMap().GetKey(i));
  76. group_name = Widget.TranslateString("#" + group_name); //oof
  77. m_Tabber.AddTab(group_name);
  78. }
  79. }
  80. if (InputUtils.GetUnsortedInputActions() && InputUtils.GetUnsortedInputActions().Count() > 0)
  81. {
  82. m_Tabber.AddTab(Widget.TranslateString("#layout_pc_keybinding_unsorted"));
  83. }
  84. m_Tabber.DisableTabs(true);
  85. }
  86. void CreateGroupContainer()
  87. {
  88. m_GroupsContainer = new KeybindingsContainer(-1,GetGame().GetInput(),layoutRoot.FindAnyWidget("TabContentsHolder"),this);
  89. }
  90. void UpdateTabContent(int tab_index)
  91. {
  92. m_GroupsContainer.SwitchSubgroup(tab_index);
  93. }
  94. void ClearKeybind(int key_index)
  95. {
  96. ColorWhite(m_Apply, null);
  97. m_Apply.ClearFlags(WidgetFlags.IGNOREPOINTER);
  98. ColorWhite(m_Undo, null);
  99. m_Undo.ClearFlags(WidgetFlags.IGNOREPOINTER);
  100. }
  101. void ClearAlternativeKeybind(int key_index)
  102. {
  103. ColorWhite(m_Apply, null);
  104. m_Apply.ClearFlags(WidgetFlags.IGNOREPOINTER);
  105. ColorWhite(m_Undo, null);
  106. m_Undo.ClearFlags(WidgetFlags.IGNOREPOINTER);
  107. }
  108. void StartEnteringKeybind(int key_index)
  109. {
  110. m_CurrentSettingAlternateKeyIndex = -1;
  111. m_CurrentSettingKeyIndex = key_index;
  112. }
  113. void CancelEnteringKeybind()
  114. {
  115. m_GroupsContainer.CancelEnteringKeybind();
  116. m_CurrentSettingKeyIndex = -1;
  117. }
  118. void StartEnteringAlternateKeybind(int key_index)
  119. {
  120. m_CurrentSettingKeyIndex = -1;
  121. m_CurrentSettingAlternateKeyIndex = key_index;
  122. }
  123. void CancelEnteringAlternateKeybind()
  124. {
  125. m_GroupsContainer.CancelEnteringAlternateKeybind();
  126. m_CurrentSettingAlternateKeyIndex = -1;
  127. }
  128. void ConfirmKeybindEntry(TIntArray new_keys)
  129. {
  130. m_CurrentSettingKeyIndex = -1;
  131. ColorWhite(m_Apply, null);
  132. m_Apply.ClearFlags(WidgetFlags.IGNOREPOINTER);
  133. ColorWhite(m_Undo, null);
  134. m_Undo.ClearFlags(WidgetFlags.IGNOREPOINTER);
  135. }
  136. void ConfirmAlternateKeybindEntry(TIntArray new_keys)
  137. {
  138. m_CurrentSettingAlternateKeyIndex = -1;
  139. ColorWhite(m_Apply, null);
  140. m_Apply.ClearFlags(WidgetFlags.IGNOREPOINTER);
  141. ColorWhite(m_Undo, null);
  142. m_Undo.ClearFlags(WidgetFlags.IGNOREPOINTER);
  143. }
  144. override void Update(float timeslice)
  145. {
  146. if (GetUApi().GetInputByID(UAUIBack).LocalPress())
  147. {
  148. Back();
  149. }
  150. if (m_GroupsContainer)
  151. {
  152. m_GroupsContainer.Update(timeslice);
  153. }
  154. }
  155. override bool OnClick(Widget w, int x, int y, int button)
  156. {
  157. if (button == MouseState.LEFT)
  158. {
  159. if (w == m_Apply)
  160. {
  161. Apply();
  162. return true;
  163. }
  164. else if (w == m_Back)
  165. {
  166. Back();
  167. return true;
  168. }
  169. else if (w == m_Undo)
  170. {
  171. Reset();
  172. return true;
  173. }
  174. else if (w == m_Defaults)
  175. {
  176. SetToDefaults();
  177. return true;
  178. }
  179. else if (w == m_HardReset)
  180. {
  181. HardReset();
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. void Apply()
  188. {
  189. ColorDisabled(m_Apply);
  190. m_Apply.SetFlags(WidgetFlags.IGNOREPOINTER);
  191. ColorDisabled(m_Undo);
  192. m_Undo.SetFlags(WidgetFlags.IGNOREPOINTER);
  193. ColorWhite(m_Defaults, null);
  194. m_Defaults.ClearFlags(WidgetFlags.IGNOREPOINTER);
  195. m_GroupsContainer.Apply();
  196. // save input configuration
  197. GetUApi().Export();
  198. }
  199. void Back()
  200. {
  201. if (m_CurrentSettingKeyIndex != -1)
  202. {
  203. CancelEnteringKeybind();
  204. return;
  205. }
  206. if (m_CurrentSettingAlternateKeyIndex != -1)
  207. {
  208. CancelEnteringAlternateKeybind();
  209. return;
  210. }
  211. bool changed = m_GroupsContainer.IsChanged();
  212. if (changed)
  213. {
  214. g_Game.GetUIManager().ShowDialog("#main_menu_configure", "#main_menu_configure_desc", MODAL_ID_BACK, DBT_YESNO, DBB_YES, DMT_QUESTION, this);
  215. }
  216. else
  217. {
  218. GetGame().GetUIManager().Back();
  219. }
  220. }
  221. //! Undoes the unsaved changes and reverts to previous state. Does not reset to defaults!
  222. void Reset()
  223. {
  224. ColorDisabled(m_Apply);
  225. m_Apply.SetFlags(WidgetFlags.IGNOREPOINTER);
  226. ColorDisabled(m_Undo);
  227. m_Undo.SetFlags(WidgetFlags.IGNOREPOINTER);
  228. m_GroupsContainer.Reset();
  229. }
  230. void SetToDefaults()
  231. {
  232. g_Game.GetUIManager().ShowDialog("#menu_default_cap", "#menu_default_desc", MODAL_ID_DEFAULT, DBT_YESNO, DBB_YES, DMT_QUESTION, this);
  233. }
  234. void HardReset()
  235. {
  236. g_Game.GetUIManager().ShowDialog("#menu_default_cap", "#menu_default_all_desc", MODAL_ID_DEFAULT_ALL, DBT_YESNO, DBB_YES, DMT_QUESTION, this);
  237. }
  238. void PerformSetToDefaultsExt(int mode)
  239. {
  240. switch (mode)
  241. {
  242. case MODAL_RESULT_DEFAULT_CURRENT:
  243. GetUApi().PresetReset();
  244. break;
  245. case MODAL_RESULT_DEFAULT_ALL:
  246. GetUApi().Revert();
  247. break;
  248. }
  249. ColorDisabled(m_Apply);
  250. m_Apply.SetFlags(WidgetFlags.IGNOREPOINTER);
  251. ColorDisabled(m_Undo);
  252. m_Undo.SetFlags(WidgetFlags.IGNOREPOINTER);
  253. GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(GetGame().GetMission().RefreshExcludes);
  254. m_GroupsContainer.Reset(true);
  255. }
  256. override bool OnModalResult(Widget w, int x, int y, int code, int result)
  257. {
  258. if (code == MODAL_ID_BACK)
  259. {
  260. if (result == DBB_YES)
  261. {
  262. Reset();
  263. GetGame().GetUIManager().Back();
  264. }
  265. return true;
  266. }
  267. else if (code == MODAL_ID_DEFAULT)
  268. {
  269. if (result == DBB_YES)
  270. {
  271. PerformSetToDefaultsExt(MODAL_RESULT_DEFAULT_CURRENT);
  272. }
  273. return true;
  274. }
  275. else if (code == MODAL_ID_DEFAULT_ALL)
  276. {
  277. if (result == DBB_YES)
  278. {
  279. PerformSetToDefaultsExt(MODAL_RESULT_DEFAULT_ALL);
  280. }
  281. }
  282. else if (code == MODAL_ID_PRESET_CHANGE)
  283. {
  284. if (result == DBB_YES)
  285. {
  286. Reset();
  287. m_PresetSelector.PerformSetOption(m_TargetPresetIndex);
  288. }
  289. return true;
  290. }
  291. return false;
  292. }
  293. override void Refresh()
  294. {
  295. string version;
  296. GetGame().GetVersion(version);
  297. #ifdef PLATFORM_CONSOLE
  298. version = "#main_menu_version" + " " + version + " (" + g_Game.GetDatabaseID() + ")";
  299. #else
  300. version = "#main_menu_version" + " " + version;
  301. #endif
  302. m_Version.SetText(version);
  303. }
  304. override bool OnMouseEnter(Widget w, int x, int y)
  305. {
  306. if (w && IsFocusable(w))
  307. {
  308. ColorRed(w);
  309. return true;
  310. }
  311. return false;
  312. }
  313. override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  314. {
  315. if (w && IsFocusable(w))
  316. {
  317. if ((w.GetFlags() & WidgetFlags.DISABLED) || (w.GetFlags() & WidgetFlags.IGNOREPOINTER))
  318. {
  319. ColorDisabled(w);
  320. }
  321. else
  322. {
  323. ColorWhite(w, enterW);
  324. }
  325. return true;
  326. }
  327. return false;
  328. }
  329. override bool OnMouseWheel(Widget w, int x, int y, int wheel)
  330. {
  331. return super.OnMouseWheel(w, x, y, wheel);
  332. }
  333. override bool OnFocus(Widget w, int x, int y)
  334. {
  335. if (w && IsFocusable(w))
  336. {
  337. ColorRed(w);
  338. return true;
  339. }
  340. return false;
  341. }
  342. override bool OnFocusLost(Widget w, int x, int y)
  343. {
  344. if (w && IsFocusable(w))
  345. {
  346. if ((w.GetFlags() & WidgetFlags.DISABLED) || (w.GetFlags() & WidgetFlags.IGNOREPOINTER))
  347. {
  348. ColorDisabled(w);
  349. }
  350. else
  351. {
  352. ColorWhite(w, null);
  353. }
  354. return true;
  355. }
  356. return false;
  357. }
  358. bool IsFocusable(Widget w)
  359. {
  360. if (w)
  361. {
  362. return (w == m_Apply || w == m_Back || w == m_Undo || w == m_Defaults || w == m_HardReset);
  363. }
  364. return false;
  365. }
  366. //Coloring functions (Until WidgetStyles are useful)
  367. void ColorRed(Widget w)
  368. {
  369. SetFocus(w);
  370. ButtonWidget button = ButtonWidget.Cast(w);
  371. if (button && button != m_Apply)
  372. {
  373. button.SetTextColor(ARGB(255, 200, 0, 0));
  374. }
  375. }
  376. void ColorWhite(Widget w, Widget enterW)
  377. {
  378. #ifdef PLATFORM_WINDOWS
  379. SetFocus(null);
  380. #endif
  381. ButtonWidget button = ButtonWidget.Cast(w);
  382. if (button)
  383. {
  384. if (button.GetFlags() & WidgetFlags.DISABLED)
  385. {
  386. button.SetTextColor(ColorManager.COLOR_DISABLED_TEXT);
  387. }
  388. else
  389. {
  390. button.SetTextColor(ColorManager.COLOR_NORMAL_TEXT);
  391. }
  392. }
  393. }
  394. void ColorDisabled(Widget w)
  395. {
  396. #ifdef PLATFORM_WINDOWS
  397. SetFocus(null);
  398. #endif
  399. ButtonWidget button = ButtonWidget.Cast(w);
  400. if (button)
  401. {
  402. button.SetTextColor(ColorManager.COLOR_DISABLED_TEXT);
  403. }
  404. }
  405. protected void InitInputSortingMap()
  406. {
  407. InputUtils.InitInputMetadata();
  408. }
  409. void InitPresets(int index, Widget parent, Input input)
  410. {
  411. Widget kb_root = parent.FindAnyWidget("keyboard_dropown");
  412. if (kb_root)
  413. {
  414. kb_root.Show(false);
  415. }
  416. array<string> opt1 = new array<string>;
  417. string profile_text;
  418. for (int i = 0; i < input.GetProfilesCount(); i++)
  419. {
  420. input.GetProfileName(i, profile_text);
  421. opt1.Insert(profile_text);
  422. }
  423. int current_idx = input.GetCurrentProfile();
  424. m_OriginalPresetIndex = current_idx;
  425. m_PresetSelector = new OptionSelectorMultistate(layoutRoot.FindAnyWidget("profile_setting_option"), current_idx, null, false, opt1);
  426. m_PresetSelector.m_AttemptOptionChange.Insert(OnAttemptSelectPreset);
  427. m_PresetSelector.m_OptionChanged.Insert(OnSelectKBPreset);
  428. }
  429. void OnAttemptSelectPreset(int index)
  430. {
  431. bool changed = m_GroupsContainer.IsChanged() && m_OriginalPresetIndex != index;
  432. m_TargetPresetIndex = index;
  433. if (changed)
  434. {
  435. g_Game.GetUIManager().ShowDialog("#main_menu_configure", "#main_menu_configure_desc", MODAL_ID_PRESET_CHANGE, DBT_YESNO, DBB_YES, DMT_QUESTION, this);
  436. }
  437. m_PresetSelector.SetCanSwitch(!changed);
  438. }
  439. void OnSelectKBPreset(int index)
  440. {
  441. m_OriginalPresetIndex = index;
  442. m_GroupsContainer.OnSelectKBPreset(index);
  443. string profile_text;
  444. GetGame().GetInput().GetProfileName(index, profile_text);
  445. GetGame().GetMission().GetOnInputPresetChanged().Invoke();
  446. }
  447. //////////////////////////////////////////////////
  448. // OBSOLETE METHODS //
  449. //////////////////////////////////////////////////
  450. KeybindingsContainer GetCurrentTab()
  451. {
  452. return m_GroupsContainer;
  453. }
  454. void AddGroup(int index, Input input)
  455. {
  456. }
  457. //! deprecated, resets all (as before ~1.20)
  458. void PerformSetToDefaults()
  459. {
  460. PerformSetToDefaultsExt(MODAL_RESULT_DEFAULT_ALL);
  461. }
  462. //! deprecated
  463. void DeferredDefaultsInit()
  464. {
  465. }
  466. }