uihintpanel.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. Ui class for hints in in-game-menu
  3. */
  4. class UiHintPanel extends ScriptedWidgetEventHandler
  5. {
  6. #ifdef DIAG_DEVELOPER
  7. static int m_ForcedIndex = -1;//only for debug purposes
  8. #endif
  9. // Const
  10. protected int m_SlideShowDelay = 25000; // The speed of the slideshow
  11. protected string m_RootPath = "Gui/layouts/new_ui/hints/in_game_hints.layout"; // Layout path
  12. protected const string m_DataPath = "scripts/data/hints.json"; // Json path
  13. // Widgets
  14. protected Widget m_RootFrame;
  15. protected Widget m_SpacerFrame;
  16. protected ButtonWidget m_UiLeftButton;
  17. protected ButtonWidget m_UiRightButton;
  18. protected RichTextWidget m_UiDescLabel;
  19. protected TextWidget m_UiHeadlineLabel;
  20. protected ImageWidget m_UiHintImage;
  21. protected TextWidget m_UiPageingLabel;
  22. // Data
  23. protected ref array<ref HintPage> m_ContentList;
  24. protected int m_PageIndex = int.MIN;
  25. protected DayZGame m_Game;
  26. protected bool m_Initialized;
  27. protected Widget m_ParentWidget;
  28. protected int m_PreviousRandomIndex = int.MIN;
  29. // ---------------------------------------------------------
  30. // Constructor
  31. void UiHintPanel(Widget parent_widget)
  32. {
  33. DayZGame game = DayZGame.Cast(GetGame());
  34. m_ParentWidget = parent_widget;
  35. Init(game);
  36. }
  37. // Destructor
  38. void ~UiHintPanel()
  39. {
  40. StopSlideShow();
  41. if(m_RootFrame)
  42. m_RootFrame.Unlink();
  43. }
  44. void Init(DayZGame game)
  45. {
  46. //as this class is now also being instantiated from within the DayZGame CTOR, where GetGame() does not work yet, we need a way to pass the game instance from DayZGame CTOR
  47. //however for modding legacy support purposes, this was done without modifying the CTOR signature with the addition of the Init method,
  48. //in order to keep compatibility with existing MODs, there is still a way to instantiate this class properly even without calling Init from the outside
  49. if (m_Initialized)
  50. return;
  51. if (!game)//is null when instantiated from DayZGame during loading before calling Init explicitly
  52. return;
  53. m_Initialized = true;
  54. m_Game = game;
  55. // Load Json File
  56. LoadContentList();
  57. // If load successful
  58. if (m_ContentList)
  59. {
  60. // Build the layout
  61. BuildLayout(m_ParentWidget);
  62. // Get random page index
  63. RandomizePageIndex();
  64. // Populate the layout with data
  65. PopulateLayout();
  66. // Start the slideshow
  67. StartSlideshow();
  68. }
  69. else
  70. ErrorEx("Could not create the hint panel. The data are missing!");
  71. }
  72. // ------------------------------------------------------
  73. // Load content data from json file
  74. protected void LoadContentList()
  75. {
  76. string errorMessage;
  77. if (!JsonFileLoader<array<ref HintPage>>.LoadFile(m_DataPath, m_ContentList, errorMessage))
  78. ErrorEx(errorMessage);
  79. }
  80. // Create and Build the layout
  81. protected void BuildLayout(Widget parent_widget)
  82. {
  83. // Create the layout
  84. m_RootFrame = m_Game.GetWorkspace().CreateWidgets(m_RootPath, parent_widget);
  85. if (m_RootFrame)
  86. {
  87. // Find Widgets
  88. m_SpacerFrame = m_RootFrame.FindAnyWidget("GridSpacerWidget1");
  89. m_UiLeftButton = ButtonWidget.Cast(m_RootFrame.FindAnyWidget("LeftButton"));
  90. m_UiRightButton = ButtonWidget.Cast(m_RootFrame.FindAnyWidget("RightButton"));
  91. m_UiHeadlineLabel = TextWidget.Cast(m_RootFrame.FindAnyWidget("HeadlineLabel"));
  92. m_UiDescLabel = RichTextWidget.Cast(m_RootFrame.FindAnyWidget("HintDescLabel"));
  93. m_UiHintImage = ImageWidget.Cast(m_RootFrame.FindAnyWidget("HintImage"));
  94. m_UiPageingLabel = TextWidget.Cast(m_RootFrame.FindAnyWidget("PageInfoLabel"));
  95. // Set handler
  96. m_RootFrame.SetHandler(this);
  97. }
  98. }
  99. // Populate the hint with content
  100. protected void PopulateLayout()
  101. {
  102. if (m_RootFrame)
  103. {
  104. SetHintHeadline();
  105. SetHintDescription();
  106. SetHintImage();
  107. SetHintPaging();
  108. }
  109. }
  110. // -------------------------------------------
  111. // Setters
  112. protected void SetHintHeadline()
  113. {
  114. m_UiHeadlineLabel.SetText(m_ContentList.Get(m_PageIndex).GetHeadlineText());
  115. }
  116. protected void SetHintDescription()
  117. {
  118. #ifdef DEVELOPER
  119. //Print("showing contents for page "+m_PageIndex);
  120. #endif
  121. m_UiDescLabel.SetText(m_ContentList.Get(m_PageIndex).GetDescriptionText());
  122. m_UiDescLabel.Update();
  123. m_SpacerFrame.Update();
  124. }
  125. protected void SetHintImage()
  126. {
  127. string image_path = m_ContentList.Get(m_PageIndex).GetImagePath();
  128. // If there is an image
  129. if (image_path)
  130. {
  131. // Show the widget
  132. m_UiHintImage.Show(true);
  133. // Set the image path
  134. m_UiHintImage.LoadImageFile(0, image_path);
  135. }
  136. else
  137. {
  138. // Hide the widget
  139. m_UiHintImage.Show(false);
  140. }
  141. }
  142. protected void SetHintPaging()
  143. {
  144. if (m_UiPageingLabel)
  145. m_UiPageingLabel.SetText(string.Format("%1 / %2", m_PageIndex + 1, m_ContentList.Count()));
  146. }
  147. void ShowRandomPage()
  148. {
  149. RandomizePageIndex();
  150. PopulateLayout();
  151. }
  152. // Set a random page index
  153. protected void RandomizePageIndex()
  154. {
  155. #ifdef DIAG_DEVELOPER
  156. if (DiagMenu.IsInitialized())
  157. {
  158. if (m_ForcedIndex != -1)
  159. {
  160. m_PageIndex = Math.Clamp(m_ForcedIndex,0,m_ContentList.Count() - 1);
  161. return;
  162. }
  163. }
  164. #endif
  165. Math.Randomize(m_Game.GetTime());
  166. Math.RandomFloat01();//throw-away value, without calling this, the next random number is always the same, calling Math.Randomize(-1) makes no difference
  167. while (m_PageIndex == m_PreviousRandomIndex)
  168. m_PageIndex = Math.RandomIntInclusive(0, m_ContentList.Count() - 1);
  169. m_PreviousRandomIndex = m_PageIndex;
  170. }
  171. // Show next hint page by incrementing the page index.
  172. protected void ShowNextPage()
  173. {
  174. // Update the page index
  175. if ( m_PageIndex < m_ContentList.Count() - 1 )
  176. {
  177. m_PageIndex++;
  178. }
  179. else
  180. {
  181. m_PageIndex = 0;
  182. }
  183. //Update the hint page
  184. PopulateLayout();
  185. }
  186. // Show previous hint page by decreasing the page index.
  187. protected void ShowPreviousPage()
  188. {
  189. // Update the page index
  190. if ( m_PageIndex == 0 )
  191. {
  192. m_PageIndex = m_ContentList.Count() - 1;
  193. }
  194. else
  195. {
  196. m_PageIndex--;
  197. }
  198. //Update the hint page
  199. PopulateLayout();
  200. }
  201. // -------------------------------------------
  202. // Slideshow
  203. // Creates new slidshow thread
  204. protected void StartSlideshow()
  205. {
  206. m_Game.GetCallQueue(CALL_CATEGORY_GUI).CallLater(SlideshowThread, m_SlideShowDelay);
  207. }
  208. // Slidshow thread - run code
  209. protected void SlideshowThread()
  210. {
  211. ShowNextPage();
  212. }
  213. // Stop the slide show
  214. protected void StopSlideShow()
  215. {
  216. m_Game.GetCallQueue(CALL_CATEGORY_GUI).Remove(SlideshowThread);
  217. }
  218. // Restart the slide show
  219. protected void RestartSlideShow()
  220. {
  221. StopSlideShow();
  222. StartSlideshow();
  223. }
  224. // ----------------------------------------
  225. // Layout manipulation
  226. override bool OnClick(Widget w, int x, int y, int button)
  227. {
  228. if (button == MouseState.LEFT)
  229. {
  230. switch (w)
  231. {
  232. case m_UiLeftButton:
  233. {
  234. ShowPreviousPage();
  235. return true;
  236. }
  237. case m_UiRightButton:
  238. {
  239. ShowNextPage();
  240. return true;
  241. }
  242. }
  243. }
  244. return false;
  245. }
  246. override bool OnMouseEnter(Widget w, int x, int y)
  247. {
  248. if (w == m_RootPath || w == m_UiLeftButton || w == m_UiRightButton)
  249. {
  250. StopSlideShow();
  251. return true;
  252. }
  253. return false;
  254. }
  255. override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  256. {
  257. if (w == m_RootPath || w == m_UiLeftButton || w == m_UiRightButton)
  258. {
  259. RestartSlideShow();
  260. return true;
  261. }
  262. return false;
  263. }
  264. }
  265. // ---------------------------------------------------------------------------------------------------------
  266. class UiHintPanelLoading extends UiHintPanel
  267. {
  268. override void Init(DayZGame game)
  269. {
  270. m_RootPath = "Gui/layouts/new_ui/hints/in_game_hints_load.layout";
  271. super.Init(game);
  272. }
  273. }