File size: 7,662 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
	Ui class for hints in in-game-menu
*/

class UiHintPanel extends ScriptedWidgetEventHandler
{	
	#ifdef DIAG_DEVELOPER
	static int				m_ForcedIndex = -1;//only for debug purposes
	#endif
	
	// Const
	protected int 			m_SlideShowDelay			= 25000;											// The speed of the slideshow 
	protected string 				m_RootPath			= "Gui/layouts/new_ui/hints/in_game_hints.layout";	// Layout path 
	protected const string 			m_DataPath			= "scripts/data/hints.json";						// Json path
	// Widgets
	protected Widget 				m_RootFrame;
	protected Widget 				m_SpacerFrame;
	protected ButtonWidget 			m_UiLeftButton;
	protected ButtonWidget 			m_UiRightButton;
	protected RichTextWidget		m_UiDescLabel;
	protected TextWidget			m_UiHeadlineLabel;
	protected ImageWidget 			m_UiHintImage;
	protected TextWidget 			m_UiPageingLabel;
	// Data		
	protected ref array<ref HintPage>	m_ContentList;
	protected int 					m_PageIndex = int.MIN;
	protected DayZGame				m_Game;
	protected bool					m_Initialized;
	protected Widget 				m_ParentWidget;
	protected int					m_PreviousRandomIndex = int.MIN;
	
	// ---------------------------------------------------------
	
	// Constructor
	void UiHintPanel(Widget parent_widget)
	{		
		DayZGame game = DayZGame.Cast(GetGame());
		m_ParentWidget = parent_widget;
		Init(game);
	}
	// Destructor
	void ~UiHintPanel()
	{
		StopSlideShow();
	
		if(m_RootFrame)
			m_RootFrame.Unlink();
	}

	
	void Init(DayZGame game)
	{
		//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
		//however for modding legacy support purposes, this was done without modifying the CTOR signature with the addition of the Init method, 
		//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
		
		if (m_Initialized)
			return;
		if (!game)//is null when instantiated from DayZGame during loading before calling Init explicitly
			return;
		m_Initialized = true;

		m_Game = game;
		// Load Json File 
		LoadContentList();
		// If load successful 
		if (m_ContentList)	
		{
			// Build the layout
			BuildLayout(m_ParentWidget);
			// Get random page index 
			RandomizePageIndex();
			// Populate the layout with data
			PopulateLayout();
			// Start the slideshow
			StartSlideshow();			
		}
		else 
			ErrorEx("Could not create the hint panel. The data are missing!");
	}
	
	// ------------------------------------------------------
	
	// Load content data from json file 
	protected void LoadContentList()
	{
		string errorMessage;
		if (!JsonFileLoader<array<ref HintPage>>.LoadFile(m_DataPath, m_ContentList, errorMessage))
			ErrorEx(errorMessage);
	}	
	
	// Create and Build the layout 
	protected void BuildLayout(Widget parent_widget)
	{
		// Create the layout
		m_RootFrame = m_Game.GetWorkspace().CreateWidgets(m_RootPath, parent_widget);
		
		if (m_RootFrame)
		{
			// Find Widgets
			m_SpacerFrame		= m_RootFrame.FindAnyWidget("GridSpacerWidget1");		
			m_UiLeftButton		= ButtonWidget.Cast(m_RootFrame.FindAnyWidget("LeftButton"));		
			m_UiRightButton		= ButtonWidget.Cast(m_RootFrame.FindAnyWidget("RightButton"));
			m_UiHeadlineLabel	= TextWidget.Cast(m_RootFrame.FindAnyWidget("HeadlineLabel"));	
			m_UiDescLabel		= RichTextWidget.Cast(m_RootFrame.FindAnyWidget("HintDescLabel"));
			m_UiHintImage		= ImageWidget.Cast(m_RootFrame.FindAnyWidget("HintImage"));
			m_UiPageingLabel	= TextWidget.Cast(m_RootFrame.FindAnyWidget("PageInfoLabel"));
			// Set handler
			m_RootFrame.SetHandler(this);
		}
	}
	
	// Populate the hint with content
	protected void PopulateLayout()
	{
		if (m_RootFrame)
		{
			SetHintHeadline();
			SetHintDescription();
			SetHintImage();
			SetHintPaging();
		}
	}	
	
	// -------------------------------------------
	// Setters
	protected void SetHintHeadline()
	{
		m_UiHeadlineLabel.SetText(m_ContentList.Get(m_PageIndex).GetHeadlineText());
	}
	protected void SetHintDescription()
	{
		#ifdef DEVELOPER
		//Print("showing contents for page "+m_PageIndex);
		#endif
		m_UiDescLabel.SetText(m_ContentList.Get(m_PageIndex).GetDescriptionText());
		m_UiDescLabel.Update();
		m_SpacerFrame.Update();
	}
	protected void SetHintImage()
	{
		string image_path = m_ContentList.Get(m_PageIndex).GetImagePath();
		
		// If there is an image 
		if (image_path)
		{	
			// Show the widget
			m_UiHintImage.Show(true);
			// Set the image path
			m_UiHintImage.LoadImageFile(0, image_path);
		}
		else 
		{
			// Hide the widget
			m_UiHintImage.Show(false);
		}
	}
	protected void SetHintPaging()
	{
		if (m_UiPageingLabel)
			m_UiPageingLabel.SetText(string.Format("%1 / %2", m_PageIndex + 1, m_ContentList.Count()));	
	}
	
	void ShowRandomPage()
	{
		RandomizePageIndex();
		PopulateLayout();
	}
	
	// Set a random page index 
	protected void RandomizePageIndex()
	{
		#ifdef DIAG_DEVELOPER
		if (DiagMenu.IsInitialized())
		{
			if (m_ForcedIndex != -1)
			{
				m_PageIndex = Math.Clamp(m_ForcedIndex,0,m_ContentList.Count() - 1);
				return;	
			}
		}	
		#endif
		
		Math.Randomize(m_Game.GetTime());
		Math.RandomFloat01();//throw-away value, without calling this, the next random number is always the same, calling Math.Randomize(-1) makes no difference
		while (m_PageIndex == m_PreviousRandomIndex)
			m_PageIndex = Math.RandomIntInclusive(0, m_ContentList.Count() - 1);
		m_PreviousRandomIndex = m_PageIndex;
		
	}
	// Show next hint page by incrementing the page index. 
	protected void ShowNextPage()
	{
		// Update the page index
		if ( m_PageIndex < m_ContentList.Count() - 1 )
		{
			m_PageIndex++;
		}
		else 											
		{
			m_PageIndex = 0;
		}
		
		//Update the hint page
		PopulateLayout();
	}	
	// Show previous hint page by decreasing the page index. 
	protected void ShowPreviousPage()
	{
		// Update the page index
		if ( m_PageIndex == 0 )
		{
			m_PageIndex = m_ContentList.Count() - 1;
		}
		else 
		{
			m_PageIndex--;
		
		}
		//Update the hint page
		PopulateLayout();
	}

	// -------------------------------------------
	// Slideshow
	
	// Creates new slidshow thread
	protected void StartSlideshow()
	{
		m_Game.GetCallQueue(CALL_CATEGORY_GUI).CallLater(SlideshowThread, m_SlideShowDelay);
	}
	// Slidshow thread - run code
	protected void SlideshowThread()
	{
		ShowNextPage();
	}
	// Stop the slide show
	protected void StopSlideShow()
	{
		m_Game.GetCallQueue(CALL_CATEGORY_GUI).Remove(SlideshowThread);
	}
	// Restart the slide show 
	protected void RestartSlideShow()
	{
		StopSlideShow();
		StartSlideshow();
	}
	
	// ----------------------------------------
	// Layout manipulation
	
	override bool OnClick(Widget w, int x, int y, int button)
	{
		if (button == MouseState.LEFT)
		{
			switch (w)
			{
				case m_UiLeftButton:
				{
					ShowPreviousPage();
					return true;
				}
				case m_UiRightButton:
				{	
					ShowNextPage();
					return true;
				}	
			}
		}
		return false;
	}
	override bool OnMouseEnter(Widget w, int x, int y)
	{
		if (w == m_RootPath || w == m_UiLeftButton || w == m_UiRightButton)
		{
			StopSlideShow();
			return true;
		}
		return false;
	}
	override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
	{
		if (w == m_RootPath || w == m_UiLeftButton || w == m_UiRightButton)
		{
			RestartSlideShow();
			return true;
		}
		return false;
	}
}

// ---------------------------------------------------------------------------------------------------------
class UiHintPanelLoading extends UiHintPanel
{
	override void Init(DayZGame game)
	{
		m_RootPath = "Gui/layouts/new_ui/hints/in_game_hints_load.layout";
		super.Init(game);
	}
}