controlsxboxnew.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. //used as UserID in the layout. Values assigned for convenience here
  2. enum EConsoleButtonsControls
  3. {
  4. INVALID = 0, //assumed unassigned value, ignored (dividers, formatting elements etc.)
  5. BUTTON_MENU = 1,
  6. BUTTON_VIEW = 2,
  7. BUTTON_A = 4,
  8. BUTTON_B = 8,
  9. BUTTON_X = 16,
  10. BUTTON_Y = 32,
  11. BUTTON_PAD_UP = 64,
  12. BUTTON_PAD_DOWN = 128,
  13. BUTTON_PAD_LEFT = 256,
  14. BUTTON_PAD_RIGHT = 512,
  15. BUTTON_SHOULDER_LEFT = 1024,
  16. BUTTON_SHOULDER_RIGHT = 2048,
  17. BUTTON_THUMB_LEFT = 4096,
  18. BUTTON_THUMB_RIGHT = 8192,
  19. BUTTON_TRIGGER_LEFT = 16384,
  20. BUTTON_TRIGGER_RIGHT = 32768,
  21. BUTTON_GROUP_RIGHT_SIDE_COMMON = 65536,
  22. BUTTON_GROUP_PAD_COMMON = 131072
  23. }
  24. typedef map<int,ref array<Widget>> TButtonPairingInfo; //<button_mask,<associated widgets on the respective side>>
  25. class ControlsXboxNew extends UIScriptedMenu
  26. {
  27. protected string m_BackButtonTextID;
  28. protected string m_NextPresetText;
  29. protected int m_CurrentTabIdx = -1;
  30. protected int m_CurrentPresetVariant = -1;
  31. protected ButtonWidget m_Back;
  32. protected ImageWidget m_ControlsLayoutImage;
  33. //-------------
  34. protected CanvasWidget m_CanvasWidget;
  35. protected TabberUI m_TabScript;
  36. protected Widget m_TabberWidget;
  37. protected Widget m_ControlsImage;
  38. protected Widget m_PlatformHolder; //controls container for selected platform
  39. protected Widget m_VariantWidget;
  40. protected ref map<int,Widget> m_ImageMarkerStructure;
  41. protected ref map<int,Widget> m_CategoryStructure;
  42. protected ref map<int,ref TButtonPairingInfo> m_AreasLR; //left/right area holders
  43. protected const int AREA_LEFT = 1;
  44. protected const int AREA_RIGHT = 2;
  45. protected const int PLATFORM_ADJUST_X1 = 1000;
  46. protected const int PLATFORM_ADJUST_PS = 2000;
  47. //============================================
  48. // ControlsXboxNew
  49. //============================================
  50. void ~ControlsXboxNew()
  51. {
  52. PPERequesterBank.GetRequester(PPERequesterBank.REQ_MENUEFFECTS).Stop();
  53. }
  54. protected void OnInputPresetChanged()
  55. {
  56. #ifdef PLATFORM_CONSOLE
  57. UpdateControlsElements();
  58. UpdateControlsElementVisibility();
  59. #endif
  60. }
  61. protected void OnInputDeviceChanged(EInputDeviceType pInputDeviceType)
  62. {
  63. bool mk = GetGame().GetInput().IsEnabledMouseAndKeyboard();
  64. bool mkServer = GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer();
  65. switch (pInputDeviceType)
  66. {
  67. case EInputDeviceType.CONTROLLER:
  68. if (mk && mkServer)
  69. {
  70. GetGame().GetUIManager().ShowUICursor(false);
  71. }
  72. break;
  73. default:
  74. if (GetGame().GetInput().IsEnabledMouseAndKeyboard())
  75. {
  76. GetGame().GetUIManager().ShowUICursor(true);
  77. }
  78. break;
  79. }
  80. UpdateControlsElementVisibility();
  81. }
  82. void Back()
  83. {
  84. GetGame().GetUIManager().Back();
  85. }
  86. void UpdateTabContent(int tab_index)
  87. {
  88. Widget w;
  89. //hide old
  90. if (m_CurrentTabIdx != -1)
  91. {
  92. m_VariantWidget.Show(false);
  93. while (m_VariantWidget.GetParent())
  94. {
  95. m_VariantWidget = m_VariantWidget.GetParent();
  96. m_VariantWidget.Show(false);
  97. }
  98. }
  99. //show new
  100. w = FindChildByID(m_CategoryStructure[tab_index],InputUtils.GetConsolePresetID());
  101. w.Show(true);
  102. m_VariantWidget = w;
  103. while (w.GetParent())
  104. {
  105. w = w.GetParent();
  106. w.Show(true);
  107. }
  108. DrawConnectingLines(tab_index);
  109. m_CurrentTabIdx = tab_index;
  110. }
  111. protected void DrawConnectingLines(int index)
  112. {
  113. //disconnected for now, to be finished
  114. return;
  115. m_CanvasWidget.Clear();
  116. //TODO drawing over nyah
  117. m_VariantWidget;
  118. m_AreasLR = new map<int,ref TButtonPairingInfo>;
  119. Widget wid_side; //left or right area
  120. Widget wid_spacer; //item in the L/R areas
  121. wid_side = m_VariantWidget.GetChildren();
  122. typename t = EConsoleButtonsControls;
  123. int side_idx;
  124. int enum_value;
  125. array<Widget> items_raw;
  126. array<Widget> items_filtered;
  127. while (wid_side)
  128. {
  129. TButtonPairingInfo button_mapping = new TButtonPairingInfo;
  130. side_idx = wid_side.GetUserID();
  131. wid_spacer = wid_side.GetChildren(); //dig into the side first..
  132. for (int i = 1; i < EnumTools.GetEnumSize(EConsoleButtonsControls); i++)
  133. {
  134. items_raw = new array<Widget>;
  135. items_filtered = new array<Widget>;
  136. t.GetVariableValue(null, i, enum_value); //TODO
  137. FindAllChildrenByID(wid_spacer,enum_value,items_raw);
  138. if (FilterByVisible(items_raw,items_filtered) > 0) //if there are any button-relevant items..
  139. {
  140. button_mapping.Insert(enum_value,items_filtered);
  141. }
  142. }
  143. m_AreasLR.Insert(side_idx,button_mapping);
  144. wid_side = wid_side.GetSibling();
  145. }
  146. }
  147. //============================================
  148. // Init
  149. //============================================
  150. override Widget Init()
  151. {
  152. m_CategoryStructure = new map<int,Widget>;
  153. m_ImageMarkerStructure = new map<int,Widget>;
  154. layoutRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/xbox/Controls_Screen.layout");
  155. #ifdef PLATFORM_XBOX
  156. m_ControlsImage = layoutRoot.FindAnyWidget("XboxControlsImage");
  157. #else
  158. #ifdef PLATFORM_PS4
  159. m_ControlsImage = layoutRoot.FindAnyWidget("PSControlsImage");
  160. #endif
  161. #endif
  162. m_ControlsImage.Show(true);
  163. m_TabberWidget = layoutRoot.FindAnyWidget("Tabber");
  164. m_TabberWidget.GetScript(m_TabScript);
  165. m_TabScript.m_OnTabSwitch.Insert(UpdateTabContent);
  166. m_CanvasWidget = CanvasWidget.Cast(layoutRoot.FindAnyWidget("CanvasUniversal"));
  167. m_Back = ButtonWidget.Cast(layoutRoot.FindAnyWidget("back"));
  168. UpdateToolbarText();
  169. UpdateControlsElements();
  170. UpdateControlsElementVisibility();
  171. PPERequester_MenuEffects requester;
  172. Class.CastTo(requester,PPERequesterBank.GetRequester(PPERequesterBank.REQ_MENUEFFECTS));
  173. requester.SetVignetteIntensity(0.6);
  174. ComposeData();
  175. UpdateTabContent(0);
  176. GetGame().GetMission().GetOnInputPresetChanged().Insert(OnInputPresetChanged);
  177. GetGame().GetMission().GetOnInputDeviceChanged().Insert(OnInputDeviceChanged);
  178. return layoutRoot;
  179. }
  180. override void OnShow()
  181. {
  182. super.OnShow();
  183. SetFocus(null);
  184. OnInputDeviceChanged(GetGame().GetInput().GetCurrentInputDevice());
  185. }
  186. override bool OnClick(Widget w, int x, int y, int button)
  187. {
  188. if (button == MouseState.LEFT)
  189. {
  190. if (w == m_Back)
  191. {
  192. Back();
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. override bool OnMouseEnter(Widget w, int x, int y)
  199. {
  200. if (IsFocusable(w))
  201. {
  202. ColorHighlight(w);
  203. return true;
  204. }
  205. return false;
  206. }
  207. override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  208. {
  209. if (IsFocusable(w))
  210. {
  211. ColorNormal(w);
  212. return true;
  213. }
  214. return false;
  215. }
  216. override bool OnFocus(Widget w, int x, int y)
  217. {
  218. if (IsFocusable(w))
  219. {
  220. ColorHighlight(w);
  221. return true;
  222. }
  223. return false;
  224. }
  225. override bool OnFocusLost(Widget w, int x, int y)
  226. {
  227. if (IsFocusable(w))
  228. {
  229. ColorNormal(w);
  230. return true;
  231. }
  232. return false;
  233. }
  234. bool IsFocusable(Widget w)
  235. {
  236. return (w && w == m_Back);
  237. }
  238. override void Update(float timeslice)
  239. {
  240. if (GetUApi().GetInputByID(UAUITabLeft).LocalPress())
  241. {
  242. m_TabScript.PreviousTab();
  243. }
  244. if (GetUApi().GetInputByID(UAUITabRight).LocalPress())
  245. {
  246. m_TabScript.NextTab();
  247. }
  248. if (GetUApi().GetInputByID(UAUIBack).LocalPress())
  249. {
  250. Back();
  251. }
  252. if (GetUApi().GetInputByID(UASwitchPreset).LocalPress())
  253. {
  254. PerformSwitchPreset();
  255. m_TabScript.RefreshTab();
  256. }
  257. }
  258. //! Inits data structure
  259. protected void ComposeData()
  260. {
  261. if (m_CategoryStructure)
  262. {
  263. m_CategoryStructure = null;
  264. m_CategoryStructure = new map<int,Widget>;
  265. }
  266. if (m_ImageMarkerStructure)
  267. {
  268. m_ImageMarkerStructure = null;
  269. m_ImageMarkerStructure = new map<int,Widget>;
  270. }
  271. Widget w = m_ControlsImage.GetChildren();
  272. m_ImageMarkerStructure.Set(w.GetUserID(),w);
  273. while (w.GetSibling())
  274. {
  275. w = w.GetSibling();
  276. m_ImageMarkerStructure.Set(w.GetUserID(),w); //inits button markers with their IDs
  277. }
  278. InputUtils.UpdateConsolePresetID();
  279. #ifdef PLATFORM_XBOX
  280. m_PlatformHolder = FindChildByID(layoutRoot,PLATFORM_ADJUST_X1);
  281. #else
  282. m_PlatformHolder = FindChildByID(layoutRoot,PLATFORM_ADJUST_PS);
  283. #endif
  284. //categories
  285. Widget category_widget = m_PlatformHolder.GetChildren();
  286. m_CategoryStructure.Set(category_widget.GetUserID(),category_widget);
  287. while (category_widget.GetSibling())
  288. {
  289. category_widget = category_widget.GetSibling();
  290. m_CategoryStructure.Set(category_widget.GetUserID(),category_widget);
  291. }
  292. }
  293. protected void PerformSwitchPreset()
  294. {
  295. Print("PerformSwitchPreset - 1");
  296. int index;
  297. string preset_text;
  298. UAInputAPI inputAPI = GetUApi();
  299. index = inputAPI.PresetCurrent() + 1;
  300. if (index >= inputAPI.PresetCount())
  301. {
  302. index = 0;
  303. }
  304. inputAPI.SupressNextFrame(true);
  305. inputAPI.PresetSelect(index);
  306. UpdateToolbarText();
  307. GetGame().GetMission().GetOnInputPresetChanged().Invoke();
  308. #ifdef PLATFORM_WINDOWS
  309. GetUApi().Export(); //works on emulated consoles (-xbox,-ps4)
  310. #else
  311. GetUApi().SaveInputPresetMiscData(); //default console functionality
  312. #endif
  313. InputUtils.UpdateConsolePresetID();
  314. UpdateControlsElements();
  315. }
  316. protected void UpdateToolbarText()
  317. {
  318. UAInputAPI inputAPI = GetUApi();
  319. int target_idx = inputAPI.PresetCurrent() + 1;
  320. int count = inputAPI.PresetCount();
  321. if (target_idx >= inputAPI.PresetCount())
  322. {
  323. target_idx = 0;
  324. }
  325. m_NextPresetText = inputAPI.PresetName(target_idx);
  326. if (m_NextPresetText == InputUtils.PRESET_OLD)
  327. {
  328. m_NextPresetText = "#STR_UAPRESET_ChangeTo_0";
  329. }
  330. else if (m_NextPresetText == InputUtils.PRESET_NEW)
  331. {
  332. m_NextPresetText = "#STR_UAPRESET_ChangeTo_1";
  333. }
  334. else
  335. {
  336. m_NextPresetText = "Invalid console preset name: " + m_NextPresetText;
  337. }
  338. }
  339. //!Finds immediate child widget with a corresponding userID
  340. protected Widget FindChildByID(Widget wid, int user_id)
  341. {
  342. Widget ret = wid.GetChildren();
  343. while (ret)
  344. {
  345. if (ret.GetUserID() == user_id)
  346. {
  347. return ret;
  348. }
  349. ret = ret.GetSibling();
  350. }
  351. return ret;
  352. }
  353. //!Finds all immediate children widgets with corresponding userIDs
  354. protected bool FindAllChildrenByID(Widget wid, int user_id, out array<Widget> results)
  355. {
  356. Widget child = wid.GetChildren();
  357. while (child)
  358. {
  359. if (child.GetUserID() == user_id)
  360. {
  361. results.Insert(child);
  362. }
  363. child = child.GetSibling();
  364. }
  365. return (results && results.Count() > 0);
  366. }
  367. //! returns count
  368. protected int FilterByVisible(array<Widget> input, array<Widget> filtered)
  369. {
  370. for (int i = 0; i < input.Count(); i++)
  371. {
  372. if (input[i].IsVisible())
  373. {
  374. filtered.Insert(input[i]);
  375. }
  376. }
  377. return filtered.Count();
  378. }
  379. void ColorHighlight(Widget w)
  380. {
  381. if (!w)
  382. return;
  383. int color_pnl = ARGB(255, 0, 0, 0);
  384. int color_lbl = ARGB(255, 255, 0, 0);
  385. #ifdef PLATFORM_CONSOLE
  386. color_pnl = ARGB(255, 200, 0, 0);
  387. color_lbl = ARGB(255, 255, 255, 255);
  388. #endif
  389. ButtonSetColor(w, color_pnl);
  390. ButtonSetTextColor(w, color_lbl);
  391. }
  392. void ColorNormal(Widget w)
  393. {
  394. if (!w)
  395. return;
  396. int color_pnl = ARGB(0, 0, 0, 0);
  397. int color_lbl = ARGB(255, 255, 255, 255);
  398. ButtonSetColor(w, color_pnl);
  399. ButtonSetTextColor(w, color_lbl);
  400. }
  401. void ButtonSetText(Widget w, string text)
  402. {
  403. if (!w)
  404. return;
  405. TextWidget label = TextWidget.Cast(w.FindWidget(w.GetName() + "_label"));
  406. if (label)
  407. {
  408. label.SetText(text);
  409. }
  410. }
  411. void ButtonSetColor(Widget w, int color)
  412. {
  413. if (!w)
  414. return;
  415. Widget panel = w.FindWidget(w.GetName() + "_panel");
  416. if (panel)
  417. {
  418. panel.SetColor(color);
  419. }
  420. }
  421. void ButtonSetTextColor(Widget w, int color)
  422. {
  423. if (!w)
  424. return;
  425. TextWidget label = TextWidget.Cast(w.FindAnyWidget(w.GetName() + "_label"));
  426. TextWidget text = TextWidget.Cast(w.FindAnyWidget(w.GetName() + "_text"));
  427. TextWidget text2 = TextWidget.Cast(w.FindAnyWidget(w.GetName() + "_text_1"));
  428. if (label)
  429. {
  430. label.SetColor(color);
  431. }
  432. if (text)
  433. {
  434. text.SetColor(color);
  435. }
  436. if (text2)
  437. {
  438. text2.SetColor(color);
  439. }
  440. }
  441. protected void UpdateControlsElements()
  442. {
  443. RichTextWidget toolbar_switch = RichTextWidget.Cast(layoutRoot.FindAnyWidget("ChangePresetText"));
  444. toolbar_switch.SetText(InputUtils.GetRichtextButtonIconFromInputAction("UASwitchPreset", m_NextPresetText, EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
  445. RichTextWidget toolbar_text = RichTextWidget.Cast(layoutRoot.FindAnyWidget("ContextToolbarText"));
  446. string text = string.Format(" %1",InputUtils.GetRichtextButtonIconFromInputAction("UAUIBack", "#STR_settings_menu_root_toolbar_bg_ConsoleToolbar_Back_BackText0", EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
  447. toolbar_text.SetText(text);
  448. RichTextWidget toolbar_b2 = RichTextWidget.Cast(layoutRoot.FindAnyWidget("BackIcon0"));
  449. toolbar_b2.SetText(InputUtils.GetRichtextButtonIconFromInputAction("UAUIBack", "", EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
  450. }
  451. protected void UpdateControlsElementVisibility()
  452. {
  453. bool toolbarShow = false;
  454. #ifdef PLATFORM_CONSOLE
  455. toolbarShow = !GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer() || GetGame().GetInput().GetCurrentInputDevice() == EInputDeviceType.CONTROLLER;
  456. #endif
  457. layoutRoot.FindAnyWidget("toolbar_bg").Show(toolbarShow);
  458. layoutRoot.FindAnyWidget("play_panel_root").Show(!toolbarShow);
  459. }
  460. }