1    type Grid @mut =
2        inherit Panel
3    
4        var count : u32 = 0
5        var distance : u32 = 0
6    
7        def Control.measure self w h =
8            let count = self.count
9            let distance = self.distance
10           let items_width = if w == 0 then 0 else w - (count - 1) * distance
11           let item_width = items_width as f32 / count as f32 |> round as u32
12           let first_item_width = item_width + items_width - item_width * count
13           let mut is_first = true
14           let mut count_in_row = 0
15           let mut max_item_width = 0
16           let mut row_height = 0
17           let mut y = 0
18           for item in self.items do
19               let column_w = if is_first
20                              then first_item_width
21                              else item_width
22   
23               if is_first then
24                   is_first = false
25                   if y <> 0 then
26                       y += distance
27   
28               measure_clamped item column_w h
29               let measure_w = item.measure_width
30               let measure_h = item.measure_height
31               max_item_width = max_item_width.max measure_w
32               row_height = row_height.max measure_h
33               count_in_row += 1
34               if count_in_row == count then
35                   y += row_height
36                   row_height = 0
37                   is_first = true
38                   count_in_row = 0
39   
40           if row_height <> 0 then
41               y += row_height
42   
43           self.measure_width = max_item_width * count + distance * (count - 1)
44                measure_height = y
45   
46       def Control.arrange self =
47           let count = self.count
48           let distance = self.distance
49           let items_width = self.width - (count - 1) * distance
50           let item_width = items_width as f32 / count as f32 |> round as u32
51           let first_item_width = item_width + items_width - item_width * count
52           let mut is_first = true
53           let mut count_in_row = 0
54           let mut row_height = 0
55           let mut x = 0
56           let mut y = 0
57           for item in self.items do
58               let w = if is_first
59                       then first_item_width
60                       else item_width
61   
62               if is_first then
63                   is_first = false
64                   if y <> 0 then
65                       y += distance
66   
67               item.width = w
68                    height = item.measure_height
69                    arrange
70   
71               let v = Vector3 x.as<f32> y.as<f32> Control.offset_z
72               item.position = v
73               x += w + distance
74               row_height = row_height.max item.height
75               count_in_row += 1
76               if count_in_row == count then
77                   x = 0
78                   y += row_height
79                   row_height = 0
80                   is_first = true
81                   count_in_row = 0
82