tutorialsmenu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. class TutorialsMenu extends UIScriptedMenu
  2. {
  3. protected const string PATH_MOUSEKEY = "scripts/data/pagedatatutorials.json";
  4. protected const string PATH_X1_OLD = "xbox/pagedatatutorials.json";
  5. protected const string PATH_X1_NEW = "xbox/pagedatatutorialsalternate.json";
  6. protected const string PATH_PS_OLD = "ps4/pagedatatutorials.json";
  7. protected const string PATH_PS_NEW = "ps4/pagedatatutorialsalternate.json";
  8. protected string m_BackButtonTextID;
  9. protected Widget m_InfoTextLeft;
  10. protected Widget m_InfoTextRight;
  11. protected ButtonWidget m_Back;
  12. protected ImageWidget m_ControlsLayoutImage;
  13. protected const int TABS_COUNT = 4;
  14. protected ImageWidget m_tab_images[TABS_COUNT];
  15. protected TabberUI m_TabScript;
  16. protected ref TutorialKeybinds m_KeybindsTab;
  17. //============================================
  18. // Init
  19. //============================================
  20. override Widget Init()
  21. {
  22. #ifdef PLATFORM_CONSOLE
  23. layoutRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/tutorials/xbox/tutorials.layout");
  24. #else
  25. layoutRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/tutorials/pc/tutorials.layout");
  26. #endif
  27. m_InfoTextLeft = layoutRoot.FindAnyWidget("InfoTextLeft");
  28. m_InfoTextRight = layoutRoot.FindAnyWidget("InfoTextRight");
  29. m_Back = ButtonWidget.Cast(layoutRoot.FindAnyWidget("back"));
  30. layoutRoot.FindAnyWidget("Tabber").GetScript(m_TabScript);
  31. m_TabScript.m_OnTabSwitch.Insert(DrawConnectingLines);
  32. #ifdef PLATFORM_CONSOLE
  33. if (GetGame().GetInput().IsEnabledMouseAndKeyboard())
  34. {
  35. m_KeybindsTab = new TutorialKeybinds(layoutRoot.FindAnyWidget("Tab_6"), this);
  36. m_TabScript.EnableTabControl(6, true);
  37. }
  38. #endif
  39. m_tab_images[0] = ImageWidget.Cast(layoutRoot.FindAnyWidget("MovementTabBackdropImageWidget"));
  40. m_tab_images[1] = ImageWidget.Cast(layoutRoot.FindAnyWidget("WeaponsAndActionsBackdropImageWidget"));
  41. m_tab_images[2] = ImageWidget.Cast(layoutRoot.FindAnyWidget("InventoryTabBackdropImageWidget"));
  42. m_tab_images[3] = ImageWidget.Cast(layoutRoot.FindAnyWidget("MenusTabBackdropImageWidget"));
  43. UpdateControlsElements();
  44. UpdateControlsElementVisibility();
  45. PPERequesterBank.GetRequester(PPERequester_TutorialMenu).Start(new Param1<float>(0.6));
  46. DrawConnectingLines(0);
  47. GetGame().GetMission().GetOnInputPresetChanged().Insert(OnInputPresetChanged);
  48. GetGame().GetMission().GetOnInputDeviceChanged().Insert(OnInputDeviceChanged);
  49. return layoutRoot;
  50. }
  51. void ~TutorialsMenu()
  52. {
  53. m_TabScript.m_OnTabSwitch.Remove(DrawConnectingLines);
  54. PPERequesterBank.GetRequester(PPERequester_TutorialMenu).Stop();
  55. }
  56. protected void OnInputPresetChanged()
  57. {
  58. #ifdef PLATFORM_CONSOLE
  59. UpdateControlsElements();
  60. UpdateControlsElementVisibility();
  61. #endif
  62. }
  63. protected void OnInputDeviceChanged(EInputDeviceType pInputDeviceType)
  64. {
  65. bool mk = GetGame().GetInput().IsEnabledMouseAndKeyboard();
  66. bool mkServer = GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer();
  67. switch (pInputDeviceType)
  68. {
  69. case EInputDeviceType.CONTROLLER:
  70. if (mk && mkServer)
  71. {
  72. GetGame().GetUIManager().ShowUICursor(false);
  73. }
  74. break;
  75. default:
  76. if (mk && mkServer)
  77. {
  78. GetGame().GetUIManager().ShowUICursor(true);
  79. }
  80. break;
  81. }
  82. UpdateControlsElementVisibility();
  83. }
  84. override void OnShow()
  85. {
  86. super.OnShow();
  87. SetFocus(null);
  88. OnInputDeviceChanged(GetGame().GetInput().GetCurrentInputDevice());
  89. }
  90. void Back()
  91. {
  92. GetGame().GetUIManager().Back();
  93. }
  94. void DrawConnectingLines(int index)
  95. {
  96. if (index == 6)
  97. {
  98. m_InfoTextLeft.Show(false);
  99. m_InfoTextRight.Show(false);
  100. }
  101. else
  102. {
  103. m_InfoTextLeft.Show(true);
  104. m_InfoTextRight.Show(true);
  105. ref array<ref JsonControlMappingInfo> control_mapping_info = new array<ref JsonControlMappingInfo>;
  106. ref array<ref array <ref JsonControlMappingInfo>> tab_array = new array<ref array <ref JsonControlMappingInfo>>;
  107. map<string, ref array<int>> button_marker_groups_unflitred = new map<string, ref array<int>>;
  108. map<string, ref array<int>> button_marker_groups = new map<string, ref array<int>>;
  109. float text_widget_pos_x, text_widget_pos_y;
  110. float text_widget_width, text_widget_height;
  111. float dot_pos_x, dot_pos_y;
  112. float dot_width, dot_height;
  113. float draw_pos_x, draw_pos_y;
  114. CanvasWidget canvas_widget = CanvasWidget.Cast(layoutRoot.FindAnyWidget("CanvasWidget_" + index));
  115. canvas_widget.Clear();
  116. control_mapping_info = GetControlMappingInfo();
  117. for (int i = 0; i < m_TabScript.GetTabCount(); i++)
  118. {
  119. tab_array.Insert(new array<ref JsonControlMappingInfo>);
  120. for (int j = 0; j < 30; j++)
  121. {
  122. tab_array[i].Insert(NULL);
  123. }
  124. }
  125. // insert json info to array by index, so it is sorted
  126. for (i = 0; i < control_mapping_info.Count(); i++)
  127. {
  128. JsonControlMappingInfo info = control_mapping_info.Get(i);
  129. tab_array[info.m_TabID][info.m_TextWidgetID] = info;
  130. }
  131. // create group of buttons which are connected together with line
  132. for (int l = 0; l < control_mapping_info.Count(); l++)
  133. {
  134. JsonControlMappingInfo info1 = control_mapping_info[l];
  135. string button_name = info1.m_ButtonName;
  136. int text_widget_id = info1.m_TextWidgetID;
  137. if (info1.m_TabID != index)
  138. {
  139. continue;
  140. }
  141. if (!button_marker_groups_unflitred.Contains(button_name))
  142. {
  143. button_marker_groups_unflitred.Insert(button_name, new array<int>);
  144. button_marker_groups_unflitred.Get(button_name).Insert(text_widget_id);
  145. }
  146. else
  147. {
  148. button_marker_groups_unflitred.Get(button_name).Insert(text_widget_id);
  149. }
  150. }
  151. // we want groups which are bigger than 1
  152. for (l = 0; l < button_marker_groups_unflitred.Count(); l++)
  153. {
  154. if (button_marker_groups_unflitred.GetElement(l).Count() > 1)
  155. {
  156. string key = button_marker_groups_unflitred.GetKey(l);
  157. button_marker_groups.Insert(button_marker_groups_unflitred.GetKey(l), button_marker_groups_unflitred.Get(key));
  158. }
  159. }
  160. // hide all button markers
  161. Widget xbox_controls_image = layoutRoot.FindAnyWidget("Markers_" + index);
  162. Widget panel_widget;
  163. Widget button_marker_widget;
  164. for (l = 0; l < tab_array[index].Count(); l++)
  165. {
  166. panel_widget = layoutRoot.FindAnyWidget("PanelWidget" + l);
  167. if (tab_array[index][l] != NULL)
  168. {
  169. TextWidget text_widget = TextWidget.Cast(panel_widget.FindAnyWidget("TextWidget" + l));
  170. button_marker_widget = layoutRoot.FindAnyWidget("button_marker_" + tab_array[index][l].m_ButtonName);
  171. text_widget.SetText(tab_array[index][l].m_InfoText);
  172. panel_widget.Show(true);
  173. panel_widget.Update();
  174. if (!button_marker_groups.Contains(tab_array[index][l].m_ButtonName))
  175. {
  176. panel_widget.GetScreenPos(text_widget_pos_x, text_widget_pos_y);
  177. panel_widget.GetScreenSize(text_widget_width,text_widget_height);
  178. button_marker_widget.GetScreenPos(dot_pos_x, dot_pos_y);
  179. button_marker_widget.GetScreenSize(dot_width, dot_height);
  180. draw_pos_y = text_widget_pos_y + text_widget_height / 2;
  181. if (l < 15)
  182. {
  183. draw_pos_x = text_widget_pos_x + text_widget_width - 1;
  184. }
  185. else
  186. {
  187. draw_pos_x = text_widget_pos_x;
  188. }
  189. canvas_widget.DrawLine(draw_pos_x, draw_pos_y, dot_pos_x+dot_width/2, draw_pos_y, 2, ARGBF(0.6, 1, 1, 1));
  190. canvas_widget.DrawLine(dot_pos_x+dot_width/2, draw_pos_y, dot_pos_x+dot_width/2, dot_pos_y+dot_height/2, 2, ARGBF(0.6, 1, 1, 1));
  191. }
  192. }
  193. else
  194. {
  195. panel_widget.Show(false);
  196. }
  197. panel_widget.Update();
  198. }
  199. // draw connecting lines
  200. for (l = 0; l < button_marker_groups.Count(); l++)
  201. {
  202. text_widget_pos_x = 0;
  203. text_widget_pos_y = 0;
  204. text_widget_width = 0;
  205. text_widget_height = 0;
  206. float group_point_x = 0, group_point_y = 0;
  207. float first_x = 0, first_y = 0;
  208. ref array<int> element = button_marker_groups.GetElement(l);
  209. string key_name = button_marker_groups.GetKey(l);
  210. button_marker_widget = layoutRoot.FindAnyWidget("button_marker_" + key_name);
  211. for (int g = 0; g < element.Count(); g++)
  212. {
  213. panel_widget = layoutRoot.FindAnyWidget("PanelWidget" + element[g]);
  214. panel_widget.GetScreenPos(text_widget_pos_x, text_widget_pos_y);
  215. panel_widget.GetScreenSize(text_widget_width, text_widget_height);
  216. if (g == 0)
  217. {
  218. if (element[0] < 15)
  219. {
  220. first_x = text_widget_pos_x + text_widget_width +50;
  221. }
  222. else
  223. {
  224. first_x = text_widget_pos_x - 50;
  225. }
  226. first_y = text_widget_pos_y + text_widget_height/2;
  227. }
  228. group_point_x += text_widget_pos_x;
  229. group_point_y += text_widget_pos_y;
  230. if (element[0] < 15)
  231. {
  232. canvas_widget.DrawLine(text_widget_pos_x + text_widget_width - 1, text_widget_pos_y + text_widget_height/2, text_widget_pos_x + text_widget_width +50, text_widget_pos_y + text_widget_height/2, 2, ARGBF(0.6, 1, 1, 1));
  233. }
  234. else
  235. {
  236. canvas_widget.DrawLine(text_widget_pos_x, text_widget_pos_y + text_widget_height/2, text_widget_pos_x - 50, text_widget_pos_y + text_widget_height/2, 2, ARGBF(0.6, 1, 1, 1));
  237. }
  238. }
  239. if (element[0] < 15)
  240. {
  241. group_point_x = group_point_x/element.Count() + text_widget_width + 50;
  242. }
  243. else
  244. {
  245. group_point_x = group_point_x/element.Count() - 50;
  246. }
  247. group_point_y = group_point_y/element.Count() + text_widget_height/2;
  248. button_marker_widget.GetScreenPos(dot_pos_x, dot_pos_y);
  249. button_marker_widget.GetScreenSize(dot_width, dot_height);
  250. canvas_widget.DrawLine(group_point_x, group_point_y, dot_pos_x+dot_width/2, group_point_y, 2, ARGBF(0.6, 1, 1, 1));
  251. canvas_widget.DrawLine(dot_pos_x+dot_width/2, group_point_y, dot_pos_x+dot_width/2, dot_pos_y, 2, ARGBF(0.6, 1, 1, 1));
  252. if (element[0] < 15)
  253. {
  254. canvas_widget.DrawLine(first_x, first_y, text_widget_pos_x + text_widget_width +50, text_widget_pos_y + text_widget_height/2, 2, ARGBF(0.6, 1, 1, 1));
  255. }
  256. else
  257. {
  258. canvas_widget.DrawLine(first_x, first_y, text_widget_pos_x - 50, text_widget_pos_y + text_widget_height/2, 2, ARGBF(0.6, 1, 1, 1));
  259. }
  260. }
  261. }
  262. }
  263. protected array<ref JsonControlMappingInfo> GetControlMappingInfo()
  264. {
  265. array<ref JsonControlMappingInfo> control_mapping_info = new array<ref JsonControlMappingInfo>;
  266. string file_path = PATH_MOUSEKEY; //remains set for PC vatiant
  267. string profile_name = "";
  268. GetGame().GetInput().GetProfileName(GetGame().GetInput().GetCurrentProfile(),profile_name);
  269. #ifdef PLATFORM_CONSOLE
  270. if (!GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer())
  271. {
  272. if (profile_name == "#STR_UAPRESET_0")
  273. {
  274. #ifdef PLATFORM_XBOX
  275. file_path = PATH_X1_OLD;
  276. #else
  277. file_path = PATH_PS_OLD;
  278. #endif
  279. }
  280. else if (profile_name == "#STR_UAPRESET_1")
  281. {
  282. #ifdef PLATFORM_XBOX
  283. file_path = PATH_X1_NEW;
  284. #else
  285. file_path = PATH_PS_NEW;
  286. #endif
  287. }
  288. else
  289. {
  290. ErrorEx("Invalid file path!");
  291. file_path = "";
  292. }
  293. }
  294. #endif
  295. FileHandle file_handle = OpenFile(file_path, FileMode.READ);
  296. JsonSerializer js = new JsonSerializer();
  297. string js_error = "";
  298. string line_content = "";
  299. string content = "";
  300. if (file_handle)
  301. {
  302. while (FGets(file_handle, line_content) >= 0)
  303. {
  304. content += line_content;
  305. }
  306. CloseFile(file_handle);
  307. if (js.ReadFromString(control_mapping_info, content, js_error))
  308. {
  309. return control_mapping_info;
  310. }
  311. else
  312. {
  313. ErrorEx("JSON ERROR => [TutorialsMenu]: " + js_error);
  314. }
  315. }
  316. else
  317. {
  318. ErrorEx("FILEHANDLE ERROR => [TutorialsMenu]: " + js_error);
  319. }
  320. return control_mapping_info;
  321. }
  322. override void Update(float timeslice)
  323. {
  324. if (GetUApi().GetInputByID(UAUITabLeft).LocalPress())
  325. {
  326. m_TabScript.PreviousTab();
  327. }
  328. //RIGHT BUMPER - TAB RIGHT
  329. if (GetUApi().GetInputByID(UAUITabRight).LocalPress())
  330. {
  331. m_TabScript.NextTab();
  332. }
  333. if (GetUApi().GetInputByID(UAUIBack).LocalPress())
  334. {
  335. Back();
  336. }
  337. }
  338. /// Initial texts load for the footer buttons
  339. protected void LoadFooterButtonTexts()
  340. {
  341. TextWidget uiBackText = TextWidget.Cast(layoutRoot.FindAnyWidget("BackText"));
  342. if (uiBackText)
  343. {
  344. uiBackText.SetText(m_BackButtonTextID);
  345. }
  346. }
  347. /// Set correct bottom button texts based on platform (ps4 vs xbox texts)
  348. protected void LoadTextStrings()
  349. {
  350. #ifdef PLATFORM_PS4
  351. m_BackButtonTextID = "ps4_ingame_menu_back";
  352. #else
  353. m_BackButtonTextID = "STR_rootFrame_toolbar_bg_ConsoleToolbar_Back_BackText0";
  354. #endif
  355. }
  356. override bool OnClick(Widget w, int x, int y, int button)
  357. {
  358. if (button == MouseState.LEFT)
  359. {
  360. if (w == m_Back)
  361. {
  362. Back();
  363. return true;
  364. }
  365. }
  366. return false;
  367. }
  368. override bool OnMouseEnter(Widget w, int x, int y)
  369. {
  370. if (IsFocusable(w))
  371. {
  372. ColorHighlight(w);
  373. return true;
  374. }
  375. return false;
  376. }
  377. override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  378. {
  379. if (IsFocusable(w))
  380. {
  381. ColorNormal(w);
  382. return true;
  383. }
  384. return false;
  385. }
  386. override bool OnFocus(Widget w, int x, int y)
  387. {
  388. if (IsFocusable(w))
  389. {
  390. ColorHighlight(w);
  391. return true;
  392. }
  393. return false;
  394. }
  395. override bool OnFocusLost(Widget w, int x, int y)
  396. {
  397. if (IsFocusable(w))
  398. {
  399. ColorNormal(w);
  400. return true;
  401. }
  402. return false;
  403. }
  404. bool IsFocusable(Widget w)
  405. {
  406. return (w && w == m_Back);
  407. }
  408. //Coloring functions (Until WidgetStyles are useful)
  409. void ColorHighlight(Widget w)
  410. {
  411. if (!w)
  412. return;
  413. int color_pnl = ARGB(255, 0, 0, 0);
  414. int color_lbl = ARGB(255, 255, 0, 0);
  415. int color_img = ARGB(255, 200, 0, 0);
  416. #ifdef PLATFORM_CONSOLE
  417. color_pnl = ARGB(255, 200, 0, 0);
  418. color_lbl = ARGB(255, 255, 255, 255);
  419. #endif
  420. ButtonSetColor(w, color_pnl);
  421. ButtonSetTextColor(w, color_lbl);
  422. ImagenSetColor(w, color_img);
  423. }
  424. void ColorNormal(Widget w)
  425. {
  426. if (!w)
  427. return;
  428. int color_pnl = ARGB(0, 0, 0, 0);
  429. int color_lbl = ARGB(255, 255, 255, 255);
  430. int color_img = ARGB(255, 255, 255, 255);
  431. ButtonSetColor(w, color_pnl);
  432. ButtonSetTextColor(w, color_lbl);
  433. ImagenSetColor(w, color_img);
  434. }
  435. void ButtonSetText(Widget w, string text)
  436. {
  437. if (!w)
  438. return;
  439. TextWidget label = TextWidget.Cast(w.FindWidget(w.GetName() + "_label"));
  440. if (label)
  441. {
  442. label.SetText(text);
  443. }
  444. }
  445. void ButtonSetColor(Widget w, int color)
  446. {
  447. if (!w)
  448. return;
  449. Widget panel = w.FindWidget(w.GetName() + "_panel");
  450. if (panel)
  451. {
  452. panel.SetColor(color);
  453. }
  454. }
  455. void ImagenSetColor(Widget w, int color)
  456. {
  457. if (!w)
  458. return;
  459. Widget panel = w.FindWidget(w.GetName() + "_image");
  460. if (panel)
  461. {
  462. panel.SetColor(color);
  463. }
  464. }
  465. void ButtonSetTextColor(Widget w, int color)
  466. {
  467. if (!w)
  468. return;
  469. TextWidget label = TextWidget.Cast(w.FindAnyWidget(w.GetName() + "_label"));
  470. TextWidget text = TextWidget.Cast(w.FindAnyWidget(w.GetName() + "_text"));
  471. TextWidget text2 = TextWidget.Cast(w.FindAnyWidget(w.GetName() + "_text_1"));
  472. if (label)
  473. {
  474. label.SetColor(color);
  475. }
  476. if (text)
  477. {
  478. text.SetColor(color);
  479. }
  480. if (text2)
  481. {
  482. text2.SetColor(color);
  483. }
  484. }
  485. protected void UpdateControlsElements()
  486. {
  487. #ifdef PLATFORM_CONSOLE
  488. RichTextWidget toolbar_text = RichTextWidget.Cast(layoutRoot.FindAnyWidget("ContextToolbarText"));
  489. if (toolbar_text)
  490. {
  491. string text = string.Format(" %1",InputUtils.GetRichtextButtonIconFromInputAction("UAUIBack", "#STR_settings_menu_root_toolbar_bg_ConsoleToolbar_Back_BackText0", EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
  492. toolbar_text.SetText(text);
  493. }
  494. RichTextWidget toolbar_b2 = RichTextWidget.Cast(layoutRoot.FindAnyWidget("BackIcon0"));
  495. toolbar_b2.SetText(InputUtils.GetRichtextButtonIconFromInputAction("UAUIBack", "", EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
  496. #endif
  497. }
  498. protected void UpdateControlsElementVisibility()
  499. {
  500. bool toolbarShow = false;
  501. #ifdef PLATFORM_CONSOLE
  502. toolbarShow = !GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer() || GetGame().GetInput().GetCurrentInputDevice() == EInputDeviceType.CONTROLLER;
  503. #endif
  504. #ifdef PLATFORM_CONSOLE
  505. layoutRoot.FindAnyWidget("toolbar_bg").Show(toolbarShow);
  506. #endif
  507. layoutRoot.FindAnyWidget("play_panel_root").Show(!toolbarShow);
  508. }
  509. }