CrabUI
Loading...
Searching...
No Matches
CUILayoutSimple.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 /// Default layout, simple descartes coordinate plane
15 /// </summary>
17 {
18 //public CUIComponent Host;
19 internal override void Update()
20 {
21 if (Changed && Host.Children.Count > 0)
22 {
23 Host.InvokeOnLayoutUpdated();
24
25 CUI3DOffset offset = Host.ChildOffsetBounds.Check(Host.ChildrenOffset);
26
27 foreach (CUIComponent c in Host.Children)
28 {
29 float x, y, w, h;
30
31 x = 0;
32 if (c.Relative.Left.HasValue) x = c.Relative.Left.Value * Host.Real.Width;
33 if (c.CrossRelative.Left.HasValue) x = c.CrossRelative.Left.Value * Host.Real.Height;
34 if (c.Absolute.Left.HasValue) x = c.Absolute.Left.Value;
35
36 if (c.RelativeMin.Left.HasValue) x = Math.Max(x, c.RelativeMin.Left.Value * Host.Real.Width);
37 if (c.AbsoluteMin.Left.HasValue) x = Math.Max(x, c.AbsoluteMin.Left.Value);
38
39 if (c.RelativeMax.Left.HasValue) x = Math.Min(x, c.RelativeMax.Left.Value * Host.Real.Width);
40 if (c.AbsoluteMax.Left.HasValue) x = Math.Min(x, c.AbsoluteMax.Left.Value);
41
42
43 y = 0;
44 if (c.Relative.Top.HasValue) y = c.Relative.Top.Value * Host.Real.Height;
45 if (c.CrossRelative.Top.HasValue) y = c.CrossRelative.Top.Value * Host.Real.Width;
46 if (c.Absolute.Top.HasValue) y = c.Absolute.Top.Value;
47
48 if (c.RelativeMin.Top.HasValue) y = Math.Max(y, c.RelativeMin.Top.Value * Host.Real.Height);
49 if (c.AbsoluteMin.Top.HasValue) y = Math.Max(y, c.AbsoluteMin.Top.Value);
50
51 if (c.RelativeMax.Top.HasValue) y = Math.Min(y, c.RelativeMax.Top.Value * Host.Real.Height);
52 if (c.AbsoluteMax.Top.HasValue) y = Math.Min(y, c.AbsoluteMax.Top.Value);
53
54
55 w = 0;
56 if (c.Relative.Width.HasValue) w = c.Relative.Width.Value * Host.Real.Width;
57 if (c.CrossRelative.Width.HasValue) w = c.CrossRelative.Width.Value * Host.Real.Height;
58 if (c.Absolute.Width.HasValue) w = c.Absolute.Width.Value;
59
60 if (c.RelativeMin.Width.HasValue) w = Math.Max(w, c.RelativeMin.Width.Value * Host.Real.Width);
61 if (c.AbsoluteMin.Width.HasValue) w = Math.Max(w, c.AbsoluteMin.Width.Value);
62
63 if (c.ForcedMinSize.X.HasValue) w = Math.Max(w, c.ForcedMinSize.X.Value);
64
65 if (c.RelativeMax.Width.HasValue) w = Math.Min(w, c.RelativeMax.Width.Value * Host.Real.Width);
66 if (c.AbsoluteMax.Width.HasValue) w = Math.Min(w, c.AbsoluteMax.Width.Value);
67
68
69 h = 0;
70 if (c.Relative.Height.HasValue) h = c.Relative.Height.Value * Host.Real.Height;
71 if (c.CrossRelative.Height.HasValue) h = c.CrossRelative.Height.Value * Host.Real.Width;
72 if (c.Absolute.Height.HasValue) h = c.Absolute.Height.Value;
73
74 if (c.RelativeMin.Height.HasValue) h = Math.Max(h, c.RelativeMin.Height.Value * Host.Real.Height);
75 if (c.AbsoluteMin.Height.HasValue) h = Math.Max(h, c.AbsoluteMin.Height.Value);
76 if (c.ForcedMinSize.Y.HasValue) h = Math.Max(h, c.ForcedMinSize.Y.Value);
77
78
79
80 if (c.RelativeMax.Height.HasValue) h = Math.Min(h, c.RelativeMax.Height.Value * Host.Real.Height);
81 if (c.AbsoluteMax.Height.HasValue) h = Math.Min(h, c.AbsoluteMax.Height.Value);
82
83
84 (w, h) = c.AmIOkWithThisSize(new Vector2(w, h));
85 (x, y) = CUIAnchor.GetChildPos(
86 new CUIRect(Vector2.Zero, Host.Real.Size),
87 c.ParentAnchor ?? c.Anchor,
88 new Vector2(x, y),
89 new Vector2(w, h),
90 c.Anchor
91 );
92 CUIRect real;
93 if (Host.ChildrenBoundaries != null)
94 {
95 real = Host.ChildrenBoundaries(Host.Real).Check(x, y, w, h);
96 }
97 else
98 {
99 real = new CUIRect(x, y, w, h);
100 }
101
102 if (!c.Fixed)
103 {
104 real = offset.Transform(real);
105 }
106 //TODO guh...
107 real = real.Shift(Host.Real.Position);
108
109
110 c.SetReal(real, "Simple Layout update");
111 }
112 }
113
114 base.Update();
115 }
116
117 internal override void ResizeToContent()
118 {
119 if (AbsoluteChanged && Host.FitContent.X)
120 {
121 float rightmostRight = 0;
122 foreach (CUIComponent c in Host.Children)
123 {
124 if (c.Ghost.X) continue;
125
126 float x = 0;
127 float w = 0;
128
129 if (c.Absolute.Left.HasValue) x = c.Absolute.Left.Value;
130 if (c.AbsoluteMin.Left.HasValue) x = Math.Max(x, c.AbsoluteMin.Left.Value);
131 if (c.AbsoluteMax.Left.HasValue) x = Math.Min(x, c.AbsoluteMax.Left.Value);
132
133 if (c.Absolute.Width.HasValue) w = c.Absolute.Width.Value;
134 if (c.AbsoluteMin.Width.HasValue) w = Math.Max(w, c.AbsoluteMin.Width.Value);
135 else if (c.ForcedMinSize.X.HasValue) w = Math.Max(w, c.ForcedMinSize.X.Value);
136 if (c.AbsoluteMax.Width.HasValue) w = Math.Min(w, c.AbsoluteMax.Width.Value);
137
138 rightmostRight = Math.Max(rightmostRight, x + w);
139 }
140
141 Host.SetForcedMinSize(Host.ForcedMinSize with { X = rightmostRight });
142 }
143
144 if (AbsoluteChanged && Host.FitContent.Y)
145 {
146 float bottommostBottom = 0;
147 foreach (CUIComponent c in Host.Children)
148 {
149 if (c.Ghost.Y) continue;
150
151 float y = 0;
152 float h = 0;
153
154 if (c.Absolute.Top.HasValue) y = c.Absolute.Top.Value;
155 if (c.AbsoluteMin.Top.HasValue) y = Math.Max(y, c.AbsoluteMin.Top.Value);
156 if (c.AbsoluteMax.Top.HasValue) y = Math.Min(y, c.AbsoluteMax.Top.Value);
157
158 if (c.Absolute.Height.HasValue) h = c.Absolute.Height.Value;
159 if (c.AbsoluteMin.Height.HasValue) h = Math.Max(h, c.AbsoluteMin.Height.Value);
160 else if (c.ForcedMinSize.Y.HasValue) h = Math.Max(h, c.ForcedMinSize.Y.Value);
161 if (c.AbsoluteMax.Height.HasValue) h = Math.Min(h, c.AbsoluteMax.Height.Value);
162
163 bottommostBottom = Math.Max(bottommostBottom, y + h);
164 }
165
166 Host.SetForcedMinSize(Host.ForcedMinSize with { Y = bottommostBottom });
167 }
168
169 base.ResizeToContent();
170 }
171
172 public CUILayoutSimple() : base() { }
173 public CUILayoutSimple(CUIComponent host) : base(host) { }
174 }
175}
CUIAnchor is just a Vector2 This is a static class containing some helper methods.
Definition CUIAnchor.cs:18
Base class for all components.
CUINullRect Relative
Relative to parent size and position, [0..1].
CUI3DOffset ChildrenOffset
Will shift all children by this much, e.g. this is how scroll works It's also 3D.
CUINullVector2 ForcedMinSize
This is used by text to prevent resizing beyond that and works as AbsoluteMin.
Vector2 Anchor
this point of this component
CUIBool2 FitContent
Will resize itself to fit components with absolute size, e.g. text.
CUIRect Real
Calculated prop, position on real screen in pixels Should be fully calculated after CUIMainComponent....
bool Fixed
Should it ignore child offset?
CUINullRect CrossRelative
It's like Relative, but to the opposite dimension E.g. Real.Width = CrossRelative....
Func< CUIRect, CUIBoundaries > ChildrenBoundaries
Limits to children positions.
Vector2? ParentAnchor
will be attached to this point of parent
CUIBool2 Ghost
Ghost components don't affect layout.
CUINullRect Absolute
Absolute size and position in pixels.
Base class for all layouts.
Definition CUILayout.cs:18
Default layout, simple descartes coordinate plane.
Offset of child components with X, Y, Z.
Rectangle with float.
Definition CUIRect.cs:17