Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ForceAspectRatioComponent.c
Go to the documentation of this file.
1 /*
2 This component must be attached to a size widget inside an overlay.
3 It will try to rescale the size layout to fit it inside the parent widget while keeping the specified aspect ratio.
4 */
5 
6 class SCR_ForceAspectRatioComponent : ScriptedWidgetComponent
7 {
8  [Attribute("1.777", UIWidgets.EditBox)]
9  protected float m_fRatio;
10 
11  protected SizeLayoutWidget m_wRoot;
12 
13  //------------------------------------------------------------------------------------------------
14  override protected void HandlerAttached(Widget w)
15  {
16  m_wRoot = SizeLayoutWidget.Cast(w);
17 
18  if (!m_wRoot)
19  Print("SCR_ForceAspectRatioComponent must be attached to size widget!", LogLevel.ERROR);
20  }
21 
22  //------------------------------------------------------------------------------------------------
23  override bool OnUpdate(Widget w)
24  {
25  // This will also get called on updates of children, ignore them
26  if (w != m_wRoot || !GetGame().InPlayMode())
27  return true;
28 
29  GetGame().GetCallqueue().CallLater(UpdateSize, 0); // This must not be performed during OnUpdate
30 
31  return true;
32  }
33 
34  //------------------------------------------------------------------------------------------------
35  protected void UpdateSize()
36  {
37  if (!m_wRoot)
38  return;
39 
40  Widget wParent = m_wRoot.GetParent();
41 
42  // Bail if it's called after all widgets are destroyed.
43  if (!m_wRoot)
44  return;
45 
46  // Init size layout rules
47  m_wRoot.EnableHeightOverride(true);
48  m_wRoot.EnableWidthOverride(true);
49  AlignableSlot.SetVerticalAlign(m_wRoot, LayoutVerticalAlign.Center);
50  AlignableSlot.SetHorizontalAlign(m_wRoot, LayoutHorizontalAlign.Center);
51 
52  // Get size of parent area
53  float parentWidth, parentHeight;
54  wParent.GetScreenSize(parentWidth, parentHeight);
55 
56  // Ensure width and height are not zero to prevent division by zero.
57  if (parentHeight == 0)
58  parentHeight = 1;
59  if (parentWidth == 0)
60  parentWidth = 1;
61 
62  // Check ratio of parent widget
63  float parentRatio = parentWidth / parentHeight;
64  float width, height;
65  if (parentRatio > m_fRatio)
66  {
67  width = parentHeight * m_fRatio;
68  height = parentHeight;
69  }
70  else
71  {
72  width = parentWidth;
73  height = parentWidth / m_fRatio;
74  }
75 
76  auto workspace = GetGame().GetWorkspace();
77  float widthUnscaled = workspace.DPIUnscale(width);
78  float heightUnscaled = workspace.DPIUnscale(height);
79  m_wRoot.SetWidthOverride(widthUnscaled);
80  m_wRoot.SetHeightOverride(heightUnscaled);
81  }
82 };
SCR_ForceAspectRatioComponent
Definition: SCR_ForceAspectRatioComponent.c:6
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
Attribute
SCR_ForceAspectRatioComponent Attribute
Post-process effect of scripted camera.