Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_BackendImageComponent.c
Go to the documentation of this file.
1 // When enabled, the component will visualize debug information
2 //#define BACKEND_IMAGE_DEBUG
3 
6 {
7  // Attributes
8  [Attribute("", UIWidgets.ResourceNamePicker, desc: "Image used as fallback for addon image", params: "edds")]
9  protected ResourceName m_sFallbackImage;
10 
11  // Other
12  protected ref BackendImage m_BackendImage;
13  protected ref SCR_WorkshopItemCallback_DownloadImage m_DownloadImageCallback;
14 
15  protected bool m_bIsWaitingForWidgetInit;
16  protected int m_iPreferredWidth;
17 
18  // Debugging
19  protected int m_iScreenWidth;
20 
21  protected ref ScriptInvokerVoid Event_OnImageSelected;
22 
23  //------------------------------------------------------------------------------------------------
24  protected void InvokeEventOnImageSelected()
25  {
26  if (Event_OnImageSelected)
27  Event_OnImageSelected.Invoke();
28  }
29 
30  //------------------------------------------------------------------------------------------------
31  ScriptInvokerVoid GetEventOnImageSelected()
32  {
35 
37  }
38 
39  //----------------------------------------------------------------------------------
41  void SetImage(BackendImage image)
42  {
43  bool showDefaultImage = true;
44  if (image)
45  {
46  if (image.GetScale(0))
47  showDefaultImage = false;
48  }
49 
50  if (showDefaultImage)
51  {
52  ShowDefaultImage();
54  #ifdef BACKEND_IMAGE_DEBUG
55  ShowDebugText("Image unavailable\nShowing default image");
56  #endif
57  return;
58  }
59 
60  m_BackendImage = image;
61 
62  CheckWidgetInitialized();
63  }
64 
65  //----------------------------------------------------------------------------------
66  protected void CheckWidgetInitialized()
67  {
68  Widget wImageSize = GetImageSizeWidget();
69  if (!wImageSize) // Makes no sense
70  {
71  #ifdef BACKEND_IMAGE_DEBUG
72  ShowDebugText("SCR_BackendImageComponent - IsWidgetReady() - no image size widget!");
73  #endif
74  return;
75  }
76 
77  // Get widget size instantly, related variables might be needed for debug text
78  float fWidth, fHeight;
79  wImageSize.GetScreenSize(fWidth, fHeight);
80 
81  m_iScreenWidth = Math.Round(fWidth); // Store for debugging
82 
83  // Wait untill the parent widget has been initialized
84  int preferredWidth = Math.Round(fWidth);
85  if (preferredWidth == 0)
86  {
87  #ifdef BACKEND_IMAGE_WIDGET_DEBUG
88  Debug.Error(string.Format("SCR_BackendImageComponent - IsWidgetReady() - widget has no size: %1 | %2", m_wRoot, m_wRoot.GetName()));
89  #endif
90 
91  if (!m_bIsWaitingForWidgetInit)
92  {
93  ShowLoadingImage(string.Empty);
94  GetGame().GetCallqueue().CallLater(CheckWidgetInitialized, 0, true);
95 
97  }
98 
99  return;
100  }
101 
102  GetGame().GetCallqueue().Remove(CheckWidgetInitialized);
103 
104  m_iPreferredWidth = preferredWidth;
105 
107 
108  SetImage_Internal();
109  }
110 
111  //----------------------------------------------------------------------------------
112  protected void SetImage_Internal()
113  {
114  BackendApi backend = GetGame().GetBackendApi();
115  bool connected = backend.IsActive() && backend.IsAuthenticated();
116  if (connected)
117  {
118  // We are connected, download image
119  ImageScale imageScale = m_BackendImage.GetScale(m_iPreferredWidth);
120 
121  // Discard the old callback
122  // We must unsubscribe from events of previous callback, because it still might get called
123  // When previous image is downloaded
124  if (m_DownloadImageCallback)
125  {
126  m_DownloadImageCallback.m_OnError.Remove(Callback_DownloadImage_OnTimeoutError);
127  m_DownloadImageCallback.m_OnTimeout.Remove(Callback_DownloadImage_OnTimeoutError);
128  m_DownloadImageCallback.m_OnSuccess.Remove(Callback_DownloadImage_OnSuccess);
129  }
130 
132  m_DownloadImageCallback.m_OnError.Insert(Callback_DownloadImage_OnTimeoutError);
133  m_DownloadImageCallback.m_OnTimeout.Insert(Callback_DownloadImage_OnTimeoutError);
134  m_DownloadImageCallback.m_OnSuccess.Insert(Callback_DownloadImage_OnSuccess);
135 
136  m_DownloadImageCallback.m_Scale = imageScale;
137  bool downloadStarted = imageScale.Download(m_DownloadImageCallback);
138  if (downloadStarted)
139  {
140  // While we are downloading, try to get a smaller local scale
141  ImageScale localImageScale = m_BackendImage.GetLocalScale(m_iPreferredWidth);
142  if (localImageScale)
143  {
144  if (!localImageScale.Path().IsEmpty())
145  ShowLoadingImage(localImageScale.Path());
146  else
147  ShowLoadingImage(string.Empty);
148  }
149  else
150  ShowLoadingImage(string.Empty);
151 
152  #ifdef BACKEND_IMAGE_DEBUG
153  ShowDebugText("Waiting");
154  #endif
155  }
156  else
157  {
158  // Most likely we have already downloaded this
159  string imagePath = imageScale.Path();
160  if (!imagePath.IsEmpty())
161  {
162  #ifdef BACKEND_IMAGE_DEBUG
163  string dbgtxt = string.Format("Local Image: %1x%2", imageScale.Width(), imageScale.Height());
164  ShowDebugText(dbgtxt);
165  #endif
166 
167  ShowImage(imagePath);
168  }
169  else
170  ShowDefaultImage();
171  }
172  }
173  else
174  {
175  // No connection, try to use local image
176  ImageScale localImageScale = m_BackendImage.GetLocalScale(m_iPreferredWidth);
177  if (localImageScale)
178  {
179  string imagePath = localImageScale.Path();
180  if (!imagePath.IsEmpty())
181  ShowImage(localImageScale.Path());
182  else
183  ShowDefaultImage();
184  }
185  else
186  ShowDefaultImage();
187  }
188  }
189 
190  //------------------------------------------------------------------------------------------------
191  protected void Callback_DownloadImage_OnTimeoutError(SCR_WorkshopItemCallback_DownloadImage callback)
192  {
193  ShowDefaultImage();
194  }
195 
196  //------------------------------------------------------------------------------------------------
197  protected void Callback_DownloadImage_OnSuccess(SCR_WorkshopItemCallback_DownloadImage callback)
198  {
199  if (callback.m_Scale)
200  {
201  string imgPath = callback.m_Scale.Path();
202 
203  if (!imgPath.IsEmpty())
204  {
205  ShowImage(imgPath);
206 
207  #ifdef BACKEND_IMAGE_DEBUG
208  string dbgtxt = string.Format("Received %1x%2", callback.m_Scale.Width(), callback.m_Scale.Height());
209  ShowDebugText(dbgtxt);
210  #endif
211  }
212  else
213  ShowDefaultImage();
214  }
215  else
216  ShowDefaultImage();
217  }
218 
219  // Implement handling of widgets here
220  protected void ShowLoadingImage(string fallbackImage); // Must visualize loading of image. When !fallbackImage.IsEmpty(), there is a local different size of this image.
221  protected void ShowDefaultImage(); // Must display something when image is not available
222  protected void ShowImage(string imagePath); // Must display provided image
223  protected Widget GetImageSizeWidget(); // Must return widget which wraps image widget. It will be used for size measurement.
224  protected void ShowDebugText(string txt); // Should implement visualization of debug text
225 }
226 
230 class SCR_BackendImageComponent : SCR_BackendImageComponentBase
231 {
232  [Attribute()]
233  protected string m_sImageWidgetName;
234 
235  [Attribute("", UIWidgets.EditBox, "Widget from which we will be reading size of image widget. It can be detached from image widget, since its reported size can be different when wrapped into a scale widget.")]
236  protected string m_sImageSizeWidgetName;
237 
238  [Attribute("1")]
239  protected bool m_bShowLoadingImage;
240 
241  [Attribute("")]
242  protected string m_sLoadingOverlayName;
243 
244  [Attribute("")]
245  protected string m_sDebugTextName;
246 
247  // Widgets
248  protected ImageWidget m_wImage;
249  protected Widget m_wImageSize;
251  protected TextWidget m_wDebugText;
252 
253  //----------------------------------------------------------------------------------
254  override void ShowLoadingImage(string fallbackImage)
255  {
256  if (!m_wImage)
257  return;
258 
259  if (fallbackImage.IsEmpty())
260  m_wImage.SetVisible(false);
261  else
262  ShowImage(fallbackImage);
263 
264  if (m_LoadingOverlay)
265  m_LoadingOverlay.SetShown(true);
266  }
267 
268  //----------------------------------------------------------------------------------
270  override void ShowDefaultImage()
271  {
272  if (!m_wImage)
273  return;
274 
275  m_wImage.SetVisible(false);
276 
277  if (m_LoadingOverlay)
278  m_LoadingOverlay.SetShown(false);
279 
281  }
282 
283  //----------------------------------------------------------------------------------
284  override void ShowImage(string imagePath)
285  {
286  if (!m_wImage)
287  return;
288 
289  if (imagePath.IsEmpty())
290  {
291  m_wImage.LoadImageTexture(0, m_sFallbackImage, false, true);
292  return;
293  }
294 
295  m_wImage.LoadImageTexture(0, imagePath, false, true);
296 
297  int sx, sy;
298  m_wImage.GetImageSize(0, sx, sy);
299  m_wImage.SetSize(sx, sy);
300 
301  m_wImage.SetVisible(true);
302 
303  if (m_LoadingOverlay)
304  m_LoadingOverlay.SetShown(false);
305 
307  }
308 
309  //----------------------------------------------------------------------------------
310  override Widget GetImageSizeWidget()
311  {
312  return m_wImageSize;
313  }
314 
315  //----------------------------------------------------------------------------------
316  override void HandlerAttached(Widget w)
317  {
318  super.HandlerAttached(w);
319 
320  if (!m_sImageWidgetName.IsEmpty())
321  m_wImage = ImageWidget.Cast(w.FindAnyWidget(m_sImageWidgetName));
322 
323  if (!m_sImageSizeWidgetName.IsEmpty())
324  m_wImageSize = w.FindAnyWidget(m_sImageSizeWidgetName);
325 
326  if (!m_sLoadingOverlayName.IsEmpty())
327  {
328  Widget wLoading = w.FindAnyWidget(m_sLoadingOverlayName);
329  if (wLoading)
330  {
331  m_LoadingOverlay = SCR_LoadingOverlay.Cast(wLoading.FindHandler(SCR_LoadingOverlay));
332  }
333  }
334 
335  if (!m_sDebugTextName.IsEmpty())
336  {
337  m_wDebugText = TextWidget.Cast(w.FindAnyWidget(m_sDebugTextName));
338 
339 
340  if (m_wDebugText)
341  {
342  #ifdef BACKEND_IMAGE_DEBUG
343  m_wDebugText.SetVisible(true);
344  ShowDebugText("SCR_BackendImageComponent\nInitialized");
345  #else
346  m_wDebugText.SetVisible(false);
347  #endif
348  }
349  }
350  }
351 
352  //----------------------------------------------------------------------------------
353  override void ShowDebugText(string txt)
354  {
355  if (!m_wDebugText)
356  return;
357 
358  string commotDebugData;
359 
360  if (m_iPreferredWidth != 0)
361  commotDebugData = string.Format("Widget Screen Width: %1\nPrefered Width: %2\nScales in Backend:\n%3",
363 
364  m_wDebugText.SetText(commotDebugData + "\n" + txt);
365  }
366 
367  //----------------------------------------------------------------------------------
368  protected string FormatAvailableScales()
369  {
370  if (!m_BackendImage)
371  return string.Empty;
372 
373  array<ImageScale> scales = {};
374  m_BackendImage.GetScales(scales);
375 
376  string str;
377  int j = 0;
378  for (int i = 0; i < scales.Count(); i++)
379  {
380  string s;
381 
382  if (scales[i])
383  s = string.Format("%1x%2 ", scales[i].Width(), scales[i].Height());
384  else
385  s = "null ";
386 
387  j = j+1;
388  if (j == 3)
389  {
390  s = s + "\n";
391  j = 0;
392  }
393 
394  str = str + s;
395  }
396 
397  return str;
398  }
399 
400  //----------------------------------------------------------------------------------
401  void SetImageSaturation(float saturation)
402  {
403  if (m_wImage)
404  m_wImage.SetSaturation(saturation);
405  }
406 }
m_iPreferredWidth
protected int m_iPreferredWidth
Definition: SCR_BackendImageComponent.c:11
ShowImage
protected void ShowImage(string imagePath)
Definition: SCR_BackendImageComponent.c:284
GetImageSizeWidget
protected Widget GetImageSizeWidget()
Definition: SCR_BackendImageComponent.c:310
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
FormatAvailableScales
protected string FormatAvailableScales()
Definition: SCR_BackendImageComponent.c:368
HandlerAttached
override void HandlerAttached(Widget w)
Definition: SCR_BackendImageComponent.c:316
SetImageSaturation
void SetImageSaturation(float saturation)
Definition: SCR_BackendImageComponent.c:401
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_BackendImage
protected ref BackendImage m_BackendImage
Definition: SCR_BackendImageComponent.c:7
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
m_sDebugTextName
protected string m_sDebugTextName
Definition: SCR_BackendImageComponent.c:245
m_bIsWaitingForWidgetInit
protected bool m_bIsWaitingForWidgetInit
Definition: SCR_BackendImageComponent.c:10
SCR_LoadingOverlay
Definition: SCR_LoadingOverlay.c:49
ShowDefaultImage
protected void ShowDefaultImage()
By default it hides image widget.
Definition: SCR_BackendImageComponent.c:270
ShowLoadingImage
protected void ShowLoadingImage(string fallbackImage)
Definition: SCR_BackendImageComponent.c:254
Event_OnImageSelected
protected ref ScriptInvokerVoid Event_OnImageSelected
Definition: SCR_BackendImageComponent.c:16
m_bShowLoadingImage
protected bool m_bShowLoadingImage
Definition: SCR_BackendImageComponent.c:239
m_LoadingOverlay
protected SCR_LoadingOverlay m_LoadingOverlay
Definition: SCR_BackendImageComponent.c:250
Attribute
SCR_BackendImageComponentBase SCR_ScriptedWidgetComponent Attribute()] protected string m_sImageWidgetName
Post-process effect of scripted camera.
SCR_BackendImageComponentBase
This abstract component handles loading of backend images and also manages the loading overlay while ...
Definition: SCR_BackendImageComponent.c:5
m_iScreenWidth
protected int m_iScreenWidth
Definition: SCR_BackendImageComponent.c:14
InvokeEventOnImageSelected
protected void InvokeEventOnImageSelected()
Definition: SCR_BackendImageComponent.c:19
m_sLoadingOverlayName
protected string m_sLoadingOverlayName
Definition: SCR_BackendImageComponent.c:242
m_DownloadImageCallback
protected ref SCR_WorkshopItemCallback_DownloadImage m_DownloadImageCallback
Definition: SCR_BackendImageComponent.c:8
m_sImageWidgetName
protected string m_sImageWidgetName
Definition: SCR_EntityTooltipEditorUIComponent.c:3
m_sFallbackImage
protected ResourceName m_sFallbackImage
Definition: SCR_BackendImageComponent.c:4
ScriptInvokerVoid
ScriptInvokerBase< ScriptInvokerVoidMethod > ScriptInvokerVoid
Definition: SCR_ScriptInvokerHelper.c:9
m_wImage
protected ImageWidget m_wImage
Definition: SCR_BackendImageComponent.c:248
m_wDebugText
protected TextWidget m_wDebugText
Definition: SCR_BackendImageComponent.c:251
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
m_sImageSizeWidgetName
protected string m_sImageSizeWidgetName
Definition: SCR_BackendImageComponent.c:236
SCR_ScriptedWidgetComponent
Definition: SCR_ScriptedWidgetComponent.c:7
callback
DownloadConfigCallback callback
m_wImageSize
protected Widget m_wImageSize
Definition: SCR_BackendImageComponent.c:249
ShowDebugText
protected void ShowDebugText(string txt)
Definition: SCR_BackendImageComponent.c:353
SCR_WorkshopItemCallback_DownloadImage
Definition: SCR_OnlineServiceWorkshop.c:267