Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_HUDSizeCalculator.c
Go to the documentation of this file.
6
8{
9 //------------------------------------------------------------------------------------------------
16 void ResizeGroup(notnull array<SCR_HUDSlotUIComponent> slots, int groupHeight, int groupWidth)
17 {
18 array<SCR_HUDSlotUIComponent> remainingSlots = {};
19 foreach (SCR_HUDSlotUIComponent slot : slots)
20 {
21 // Resetting the Height and Width of the Slot before attempting to resize.
22 slot.SetHeight(0, false);
23 slot.SetWidth(0, false);
24
25 // If the Slot isn't visible or doesn't have any content, the Resizing algorithm shouldn't take it into calculation.
26 if (slot.GetRootWidget().IsVisible() && slot.GetContentWidget() && slot.GetContentWidget().IsVisible())
27 remainingSlots.Insert(slot);
28 }
29
30 // Sorting the remainingSlots array by priority using a bubble sort algorithm.
31 SortSlotsByPriority(remainingSlots);
32
33 // Looping until all slots have been assigned a height and a width.
34 while (remainingSlots.Count() > 0)
35 {
36 // Getting the first Slot from the sorted array (highest priority)
37 SCR_HUDSlotUIComponent currentSlot = remainingSlots[0];
38
39 if (!currentSlot.m_aHeightSteps.IsEmpty())
40 Resize(slots, remainingSlots, groupHeight, SCR_EHUDManagerResizeType.HEIGHT);
41 if (!currentSlot.m_aWidthSteps.IsEmpty())
42 Resize(slots, remainingSlots, groupWidth, SCR_EHUDManagerResizeType.WIDTH);
43
44 // Removing the first Slot from the array after being done with the calculation.
45 remainingSlots.RemoveOrdered(0);
46 }
47 }
48
49 //------------------------------------------------------------------------------------------------
50 private void Resize(notnull array<SCR_HUDSlotUIComponent> slots, notnull array<SCR_HUDSlotUIComponent> remainingSlots, int totalSize, SCR_EHUDManagerResizeType resizeType)
51 {
52 int remainingGroupSize;
53 int maxAvailableSizeSum;
54 array<int> sizeSteps = null;
55
56
57 // Get the first Slot from the sorted array (highest priority)
58 SCR_HUDSlotUIComponent currentSlot = remainingSlots[0];
59
60 if (resizeType == SCR_EHUDManagerResizeType.HEIGHT)
61 {
62 // Calculating the remaining Group Height by subtracting the Total Assigned Height from the Group Height
63 remainingGroupSize = totalSize - GetTotalAssignedHeight(slots);
64 // Calculating the sum of the Maximum Available Heights of the remaining Slots.
65 maxAvailableSizeSum = GetMaxAvailableHeightSizeSum(remainingSlots);
66 sizeSteps = currentSlot.m_aHeightSteps;
67 }
68 else
69 {
70 // Calculating the remaining Group Width by subtracting the Total Assigned Width from the Group Width
71 remainingGroupSize = totalSize - GetTotalAssignedWidth(slots);
72 // Calculating the sum of the Maximum Available Widths of the remaining Slots.
73 maxAvailableSizeSum = GetMaxAvailableWidthSizeSum(remainingSlots);
74 sizeSteps = currentSlot.m_aWidthSteps;
75 }
76
77 // If the sum of all Maximum Available Sizes of remaining Slots is greater than the Remaining Group Size
78 // This means that, if all Slots uses their Max Size than we will go over the Group Size Limit.
79 if (maxAvailableSizeSum > remainingGroupSize)
80 {
81 // Iterating through each Size Step of a slot, from high to small. (This is possible becasue Size Step Arrays are sorted.)
82 for (int i = sizeSteps.Count() - 1; i >= 0; i--)
83 {
84 int stepToUse = sizeSteps[i];
85
86 // Calculating the difference between the current step (stepToUse) and the Highest Step.
87 int differenceBetweenHighest = sizeSteps[sizeSteps.Count() - 1] - sizeSteps[i];
88 // Subtracting the previously calculated difference from the sum of all the Highest Size Step of all the remaining Slots.
89 int modifiedMaxAvailableSizeSum = maxAvailableSizeSum - differenceBetweenHighest;
90
91 // Checking if all the slots can fit into the Remaining Size in the case of using the currently iterated Height Step instead of the Highest.
92 if (modifiedMaxAvailableSizeSum <= remainingGroupSize)
93 {
94 if (resizeType == SCR_EHUDManagerResizeType.HEIGHT)
95 currentSlot.SetHeight(stepToUse);
96 else
97 currentSlot.SetWidth(stepToUse);
98
99 return;
100 }
101 }
102 // If we failed to find a suitable Size Step so far, algorithm falls back to the smallest Size Step possible.
103 if (resizeType == SCR_EHUDManagerResizeType.HEIGHT)
104 currentSlot.SetHeight(sizeSteps[0]);
105 else
106 currentSlot.SetWidth(sizeSteps[0]);
107 }
108 else
109 {
110 /*
111 Assigning the new Size to the minimum between Remaining Group Size or Max Size Step of the Slot.
112 We don't want the Slot to be larger than it's Maximum Size Step and we also don't want it to occupy more space than the Remaining Size.
113 */
114 if (resizeType == SCR_EHUDManagerResizeType.HEIGHT)
115 {
116 int distributedSize = Math.Min(remainingGroupSize, GetMaxAvailableHeight(currentSlot));
117 currentSlot.SetHeight(distributedSize);
118 }
119 else
120 {
121 int distributedSize = Math.Min(remainingGroupSize, GetMaxAvailableWidth(currentSlot));
122 currentSlot.SetWidth(distributedSize);
123 }
124 }
125 }
126
127 //------------------------------------------------------------------------------------------------
131 private int GetTotalAssignedHeight(notnull array<SCR_HUDSlotUIComponent> slots)
132 {
133 int totalHeight;
134 foreach (SCR_HUDSlotUIComponent slot : slots)
135 {
136 if (slot.GetRootWidget().IsVisible())
137 {
138 totalHeight += slot.GetHeight();
139 }
140 }
141
142 return totalHeight;
143 }
144
145 //------------------------------------------------------------------------------------------------
149 private int GetTotalAssignedWidth(notnull array<SCR_HUDSlotUIComponent> slots)
150 {
151 int totalWidth;
152 foreach (SCR_HUDSlotUIComponent slot : slots)
153 {
154 if (slot.GetRootWidget().IsVisible())
155 {
156 totalWidth += slot.GetWidth();
157 }
158 }
159
160 return totalWidth;
161 }
162
163 //------------------------------------------------------------------------------------------------
167 private void SortSlotsByPriority(notnull array<SCR_HUDSlotUIComponent> slots)
168 {
169 for (int i = 0; i < slots.Count() - 1; i++)
170 {
171 for (int j = 0; j < slots.Count() - 1 - i; j++)
172 {
173 if (slots[j].GetPriority() > slots[j + 1].GetPriority())
174 {
175 SCR_HUDSlotUIComponent temp = slots[j];
176 slots[j] = slots[j + 1];
177 slots[j + 1] = temp;
178 }
179 }
180 }
181 }
182
183 //------------------------------------------------------------------------------------------------
187 private int GetMaxAvailableHeightSizeSum(notnull array<SCR_HUDSlotUIComponent> remainingSlots)
188 {
189 int sum;
190 foreach (SCR_HUDSlotUIComponent slot : remainingSlots)
191 {
192 if (slot.GetRootWidget().IsVisible())
193 sum += GetMaxAvailableHeight(slot);
194 }
195
196 return sum;
197 }
198
199 //------------------------------------------------------------------------------------------------
203 private int GetMaxAvailableWidthSizeSum(notnull array<SCR_HUDSlotUIComponent> remainingSlots)
204 {
205 int sum;
206 foreach (SCR_HUDSlotUIComponent slot : remainingSlots)
207 {
208 if (slot.GetRootWidget().IsVisible())
209 sum += GetMaxAvailableWidth(slot);
210 }
211
212 return sum;
213 }
214
215 //------------------------------------------------------------------------------------------------
219 private int GetMaxAvailableHeight(notnull SCR_HUDSlotUIComponent slot)
220 {
221 // If the Slot isn't visible, it shouldn't be part of the calculation.
222 if (!slot.GetRootWidget().IsVisible())
223 return 0;
224 // If the Slot is marked as Size To Content, it's size will always be the same as it's Content.
225 else if (slot.m_bSizeToContent)
226 return GetContentHeight(slot);
227
228 array<int> heightSteps = slot.m_aHeightSteps;
229 // As the Size Steps are sorted on ascending order, the Last element is the Heighest one.
230 return heightSteps[heightSteps.Count() - 1];
231 }
232
233 //------------------------------------------------------------------------------------------------
237 private int GetMaxAvailableWidth(notnull SCR_HUDSlotUIComponent slot)
238 {
239 // If the Slot isn't visible, it shouldn't be part of the calculation.
240 if (!slot.GetRootWidget().IsVisible())
241 return 0;
242 // If the Slot is marked as Size To Content, it's size will always be the same as it's Content.
243 else if (slot.m_bSizeToContent)
244 return GetContentWidth(slot);
245
246 array<int> widthSteps = slot.m_aWidthSteps;
247 // As the Size Steps are sorted on ascending order, the Last element is the Heighest one.
248 return widthSteps[widthSteps.Count() - 1];
249 }
250
251 //------------------------------------------------------------------------------------------------
255 private int GetMinAvailableHeight(notnull SCR_HUDSlotUIComponent slot)
256 {
257 // If the Slot isn't visible, it shouldn't be part of the calculation.
258 if (!slot.GetRootWidget().IsVisible())
259 return 0;
260 // If the Slot is marked as Size To Content, it's size will always be the same as it's Content.
261 else if (slot.m_bSizeToContent)
262 return GetContentHeight(slot);
263
264 // As the Size Steps are sorted on ascending order, the first element is the smallest one.
265 return slot.m_aHeightSteps[0];
266 }
267
268 //------------------------------------------------------------------------------------------------
272 private int GetMinAvailableWidth(notnull SCR_HUDSlotUIComponent slot)
273 {
274 // If the Slot isn't visible, it shouldn't be part of the calculation.
275 if (!slot.GetRootWidget().IsVisible())
276 return 0;
277 // If the Slot is marked as Size To Content, it's size will always be the same as it's Content.
278 else if (slot.m_bSizeToContent)
279 return GetContentWidth(slot);
280
281 // As the Size Steps are sorted on ascending order, the first element is the smallest one.
282 return slot.m_aWidthSteps[0];
283 }
284
285 //------------------------------------------------------------------------------------------------
289 private int GetContentHeight(notnull SCR_HUDSlotUIComponent slot)
290 {
291#ifdef WORKBENCH
293 {
294 array<int> heightSteps = slot.m_aHeightSteps;
295 if (!heightSteps || heightSteps.IsEmpty())
296 return 0;
297
298 return heightSteps[heightSteps.Count() - 1];
299 }
300#endif
301 Widget child = slot.GetContentWidget();
302 if (!child || !slot.GetRootWidget().IsVisible())
303 return 0;
304
305 float childWidth, childHeight;
306 child.GetScreenSize(childWidth, childHeight);
307 childHeight = GetGame().GetWorkspace().DPIUnscale(childHeight);
308
309 return childHeight;
310 }
311
312 //------------------------------------------------------------------------------------------------
316 private int GetContentWidth(notnull SCR_HUDSlotUIComponent slot)
317 {
318#ifdef WORKBENCH
319 if (SCR_Global.IsEditMode() && slot.m_bSizeToContent)
320 {
321 array<int> widthSteps = slot.m_aWidthSteps;
322 if (!widthSteps || widthSteps.IsEmpty())
323 return 0;
324
325 return widthSteps[widthSteps.Count() - 1];
326 }
327#endif
328 Widget child = slot.GetRootWidget().GetChildren();
329 if (!child || !slot.GetRootWidget().IsVisible())
330 return 0;
331
332 float childWidth, childHeight;
333 child.GetScreenSize(childWidth, childHeight);
334 childWidth = GetGame().GetWorkspace().DPIUnscale(childWidth);
335
336 return childWidth;
337 }
338};
ArmaReforgerScripted GetGame()
Definition game.c:1398
SCR_EHUDManagerResizeType
Definition Math.c:13
static bool IsEditMode()
Definition Functions.c:1566
void SetHeight(int height, bool notifyResizing=true)
void SetWidth(int width, bool notifyResizing=true)