CrabUI
Loading...
Searching...
No Matches
CUILayoutGrid.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6using Barotrauma;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
10
11namespace CrabUI
12{
13 /// <summary>
14 /// A Grid containing children in its cells
15 /// Dividing host into rows and columns basing on GridTemplate
16 /// And then placing children basin on their GridCell
17 /// </summary>
18 public class CUILayoutGrid : CUILayout
19 {
20 public enum TrackSizeType
21 {
22 Unknown,
23 Absolute,
24 Relative,
25 Fractional,
26 }
27
28 public class GridTrack
29 {
30 public TrackSizeType Type;
31 public float? Absolute;
32 public float? Relative;
33 public float? Fractional;
34 public float Start;
35 public float End;
36 public float Size;
37
38 public float RealSize(float hostSize)
39 {
40 float size = 0;
41
42 if (Absolute.HasValue) size = Absolute.Value;
43 if (Relative.HasValue) size = Relative.Value * hostSize;
44
45 return size;
46 }
47
48 public GridTrack(string value)
49 {
50 value = value.Trim();
51
52 float f = 0;
53 if (value.EndsWith("fr"))
54 {
55 if (float.TryParse(value.Substring(0, value.Length - 2), out f))
56 {
57 Fractional = f;
58 Type = TrackSizeType.Fractional;
59 }
60 }
61
62 if (value.EndsWith("%"))
63 {
64 if (float.TryParse(value.Substring(0, value.Length - 1), out f))
65 {
66 Relative = f / 100f;
67 Type = TrackSizeType.Relative;
68 }
69 }
70
71 if (float.TryParse(value, out f))
72 {
73 Absolute = f;
74 Type = TrackSizeType.Absolute;
75 }
76 }
77
78 public override string ToString() => $"[{Absolute},{Relative},{Fractional}]";
79
80 }
81
82
83
84 List<GridTrack> Rows = new();
85 List<GridTrack> Columns = new();
86
87 public void CalculateTracks()
88 {
89 Rows.Clear();
90 Columns.Clear();
91
92 if (Host.GridTemplateRows != null)
93 {
94 foreach (string s in Host.GridTemplateRows.Split(' '))
95 {
96 Rows.Add(new GridTrack(s));
97 }
98 }
99
100 if (Host.GridTemplateColumns != null)
101 {
102 foreach (string s in Host.GridTemplateColumns.Split(' '))
103 {
104 Columns.Add(new GridTrack(s));
105 }
106 }
107
108 if (Rows.Count == 0) Rows.Add(new GridTrack("100%"));
109 if (Columns.Count == 0) Columns.Add(new GridTrack("100%"));
110
111 float x = 0;
112 foreach (GridTrack track in Columns)
113 {
114 track.Start = x;
115 track.Size = track.RealSize(Host.Real.Width);
116 x += track.Size;
117 track.End = x;
118 }
119
120 float y = 0;
121 foreach (GridTrack track in Rows)
122 {
123 track.Start = y;
124 track.Size = track.RealSize(Host.Real.Height);
125 y += track.Size;
126 track.End = y;
127 }
128
129 }
130
131
132 internal override void Update()
133 {
134 if (Changed && Host.Children.Count > 0)
135 {
136 Host.InvokeOnLayoutUpdated();
137
138 CalculateTracks();
139
140 foreach (CUIComponent c in Host.Children)
141 {
142 float x = 0;
143 float y = 0;
144 float w = 0;
145 float h = 0;
146
147 int startCellX = 0;
148 int startCellY = 0;
149 if (c.GridStartCell != null)
150 {
151 startCellX = Math.Clamp(c.GridStartCell.Value.X, 0, Rows.Count);
152 startCellY = Math.Clamp(c.GridStartCell.Value.Y, 0, Columns.Count);
153 }
154
155 int endCellX = 0;
156 int endCellY = 0;
157 if (c.GridEndCell != null)
158 {
159 endCellX = Math.Clamp(c.GridEndCell.Value.X, 0, Rows.Count);
160 endCellY = Math.Clamp(c.GridEndCell.Value.Y, 0, Columns.Count);
161 }
162
163 CUIRect real = new CUIRect(
164 Columns[startCellX].Start,
165 Rows[startCellY].Start,
166 Columns[endCellX].End - Columns[startCellX].Start,
167 Rows[endCellY].End - Rows[startCellY].Start
168 );
169
170 real = real.Shift(Host.Real.Position);
171
172 c.AmIOkWithThisSize(real.Size);
173
174 c.SetReal(real, "Grid Layout update");
175 }
176 }
177
178 base.Update();
179 }
180
181 public CUILayoutGrid() : base() { }
182 public CUILayoutGrid(CUIComponent host) : base(host) { }
183 }
184}
Base class for all components.
string GridTemplateColumns
Used in Grid, space separated Columns sizes, either in pixels (123) or in % (123%)
string GridTemplateRows
Used in Grid, space separated Row sizes, either in pixels (123) or in % (123%)
CUIRect Real
Calculated prop, position on real screen in pixels Should be fully calculated after CUIMainComponent....
Point? GridStartCell
Component will be placed in this cell in the grid component.
Point? GridEndCell
And resized to fit cells from GridStartCell to GridEndCell.
A Grid containing children in its cells Dividing host into rows and columns basing on GridTemplate ...
Base class for all layouts.
Definition CUILayout.cs:18
Rectangle with float.
Definition CUIRect.cs:17