tutorialsmenu.c 16 KB

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