Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ForceAspectRatioComponent.c
Go to the documentation of this file.
1/*
2This component must be attached to a size widget inside an overlay.
3It will try to rescale the size layout to fit it inside the parent widget while keeping the specified aspect ratio.
4*/
5
7{
8 [Attribute("1.777", UIWidgets.EditBox)]
9 protected float m_fRatio;
10
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};
ArmaReforgerScripted GetGame()
Definition game.c:1398
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
LayoutHorizontalAlign
LayoutVerticalAlign