controlsxboxnew.c 13 KB

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