Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ContainerWidget.cs
浏览该文件的文档.
1using Engine;
2
3namespace Game {
4 public abstract class ContainerWidget : Widget {
5 public readonly WidgetsList Children;
6
7 public IEnumerable<Widget> AllChildren {
8 get {
9 foreach (Widget childWidget in Children) {
10 yield return childWidget;
11 if (childWidget is ContainerWidget containerWidget) {
12 foreach (Widget allChild in containerWidget.AllChildren) {
13 yield return allChild;
14 }
15 }
16 }
17 }
18 }
19
20 public ContainerWidget() => Children = new WidgetsList(this);
21
22 public override void UpdateCeases() {
23 foreach (Widget child in Children) {
24 child.UpdateCeases();
25 }
26 }
27
28 public void AddChildren(Widget widget) {
29 if (Children.IndexOf(widget) < 0) {
30 Children.Add(widget);
31 }
32 }
33
34 public void RemoveChildren(Widget widget) {
35 Children.Remove(widget);
36 }
37
38 public void ClearChildren() {
39 Children.Clear();
40 }
41
42 public virtual void WidgetAdded(Widget widget) { }
43
44 public virtual void WidgetRemoved(Widget widget) { }
45
46 public override void MeasureOverride(Vector2 parentAvailableSize) {
47 foreach (Widget child in Children) {
48 try {
49 child.Measure(Vector2.Max(parentAvailableSize - child.MarginHorizontalSumAndVerticalSum, Vector2.Zero));
50 }
51 catch (Exception e) {
52 throw new Exception($"Exception measuring widget of type {child.GetType().FullName}.", e);
53 }
54 }
55 }
56
57 public override void ArrangeOverride() {
58 foreach (Widget child in Children) {
59 try {
61 }
62 catch(Exception e) {
63 throw new Exception($"Exception arranging widget of type {child.GetType().FullName}.", e);
64 }
65 }
66 }
67
68 public static void ArrangeChildWidgetInCell(Vector2 c1, Vector2 c2, Widget widget) {
69 Vector2 zero = Vector2.Zero;
70 Vector2 zero2 = Vector2.Zero;
71 Vector2 vector = c2 - c1;
72 Vector2 parentDesiredSize = widget.ParentDesiredSize;
73 if (float.IsPositiveInfinity(parentDesiredSize.X)
74 || parentDesiredSize.X > vector.X - widget.MarginHorizontalSum) {
75 parentDesiredSize.X = MathUtils.Max(vector.X - widget.MarginHorizontalSum, 0f);
76 }
77 if (float.IsPositiveInfinity(parentDesiredSize.Y)
78 || parentDesiredSize.Y > vector.Y - widget.MarginVerticalSum) {
79 parentDesiredSize.Y = MathUtils.Max(vector.Y - widget.MarginVerticalSum, 0f);
80 }
81 if (widget.HorizontalAlignment == WidgetAlignment.Near) {
82 zero.X = c1.X + widget.MarginLeft;
83 zero2.X = parentDesiredSize.X;
84 }
85 else if (widget.HorizontalAlignment == WidgetAlignment.Center) {
86 zero.X = c1.X + (vector.X - parentDesiredSize.X) / 2f;
87 zero2.X = parentDesiredSize.X;
88 }
89 else if (widget.HorizontalAlignment == WidgetAlignment.Far) {
90 zero.X = c2.X - parentDesiredSize.X - widget.MarginRight;
91 zero2.X = parentDesiredSize.X;
92 }
93 else if (widget.HorizontalAlignment == WidgetAlignment.Stretch) {
94 zero.X = c1.X + widget.MarginLeft;
95 zero2.X = MathUtils.Max(vector.X - widget.MarginHorizontalSum, 0f);
96 }
97 if (widget.VerticalAlignment == WidgetAlignment.Near) {
98 zero.Y = c1.Y + widget.MarginTop;
99 zero2.Y = parentDesiredSize.Y;
100 }
101 else if (widget.VerticalAlignment == WidgetAlignment.Center) {
102 zero.Y = c1.Y + (vector.Y - parentDesiredSize.Y) / 2f;
103 zero2.Y = parentDesiredSize.Y;
104 }
105 else if (widget.VerticalAlignment == WidgetAlignment.Far) {
106 zero.Y = c2.Y - parentDesiredSize.Y - widget.MarginBottom;
107 zero2.Y = parentDesiredSize.Y;
108 }
109 else if (widget.VerticalAlignment == WidgetAlignment.Stretch) {
110 zero.Y = c1.Y + widget.MarginTop;
111 zero2.Y = MathUtils.Max(vector.Y - widget.MarginVerticalSum, 0f);
112 }
113 widget.Arrange(zero, zero2);
114 }
115
116 public override void Dispose() {
117 foreach (Widget child in Children) {
118 child.Dispose();
119 }
120 }
121 }
122}
static int Max(int x1, int x2)
IEnumerable< Widget > AllChildren
override void ArrangeOverride()
readonly WidgetsList Children
virtual void WidgetAdded(Widget widget)
static void ArrangeChildWidgetInCell(Vector2 c1, Vector2 c2, Widget widget)
void RemoveChildren(Widget widget)
virtual void WidgetRemoved(Widget widget)
void AddChildren(Widget widget)
override void MeasureOverride(Vector2 parentAvailableSize)
virtual float MarginVerticalSum
Vector2 ParentDesiredSize
virtual void Arrange(Vector2 position, Vector2 parentActualSize)
virtual float MarginHorizontalSum
virtual WidgetAlignment VerticalAlignment
virtual Vector2 MarginHorizontalSumAndVerticalSum
virtual WidgetAlignment HorizontalAlignment
Vector2 ActualSize
virtual float MarginBottom
virtual float MarginRight
virtual void UpdateCeases()
virtual float MarginTop
virtual void Measure(Vector2 parentAvailableSize)
virtual float MarginLeft
virtual void Dispose()
static readonly Vector2 Zero
static Vector2 Max(Vector2 v, float f)