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