Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
CanvasWidget.cs
浏览该文件的文档.
1using Engine;
2
3namespace Game {
5 public Dictionary<Widget, Vector2> m_positions = [];
6
7 public Vector2 Size { get; set; } = new(-1f);
8
9 public static void SetPosition(Widget widget, Vector2 position) {
10 (widget.ParentWidget as CanvasWidget)?.SetWidgetPosition(widget, position);
11 }
12
14 if (m_positions.TryGetValue(widget, out Vector2 value)) {
15 return value;
16 }
17 return null;
18 }
19
20 public void SetWidgetPosition(Widget widget, Vector2? position) {
21 if (position.HasValue) {
22 m_positions[widget] = position.Value;
23 }
24 else {
25 m_positions.Remove(widget);
26 }
27 }
28
29 public override void WidgetRemoved(Widget widget) {
30 m_positions.Remove(widget);
31 }
32
33 public override void MeasureOverride(Vector2 parentAvailableSize) {
34 Vector2 desiredSize = Vector2.Zero;
35 if (Size.X >= 0f) {
36 parentAvailableSize.X = MathUtils.Min(parentAvailableSize.X, Size.X);
37 }
38 if (Size.Y >= 0f) {
39 parentAvailableSize.Y = MathUtils.Min(parentAvailableSize.Y, Size.Y);
40 }
41 foreach (Widget child in Children) {
42 try {
43 if (child.IsVisible) {
44 Vector2? widgetPosition = GetWidgetPosition(child);
45 Vector2 v = widgetPosition ?? Vector2.Zero;
46 child.Measure(Vector2.Max(parentAvailableSize - v - child.MarginHorizontalSumAndVerticalSum, Vector2.Zero));
47 Vector2 vector = default;
48 vector.X = MathUtils.Max(desiredSize.X, v.X + child.ParentDesiredSize.X + child.MarginHorizontalSum);
49 vector.Y = MathUtils.Max(desiredSize.Y, v.Y + child.ParentDesiredSize.Y + child.MarginVerticalSum);
50 desiredSize = vector;
51 }
52 }
53 catch (Exception e) {
54 throw new Exception($"Exception measuring widget of type {child.GetType().FullName}.", e);
55 }
56 }
57 if (Size.X >= 0f) {
58 desiredSize.X = Size.X;
59 }
60 if (Size.Y >= 0f) {
61 desiredSize.Y = Size.Y;
62 }
63 DesiredSize = desiredSize;
64 }
65
66 public override void ArrangeOverride() {
67 foreach (Widget child in Children) {
68 try {
69 if (child.IsVisible) {
70 Vector2? widgetPosition = GetWidgetPosition(child);
71 if (widgetPosition.HasValue) {
72 Vector2 zero = Vector2.Zero;
73 zero.X = !float.IsPositiveInfinity(child.ParentDesiredSize.X)
74 ? child.ParentDesiredSize.X
75 : MathUtils.Max(ActualSize.X - widgetPosition.Value.X, 0f);
76 zero.Y = !float.IsPositiveInfinity(child.ParentDesiredSize.Y)
77 ? child.ParentDesiredSize.Y
78 : MathUtils.Max(ActualSize.Y - widgetPosition.Value.Y, 0f);
79 child.Arrange(widgetPosition.Value, zero);
80 }
81 else {
83 }
84 }
85 }
86 catch (Exception e) {
87 throw new Exception($"Exception arranging widget of type {child.GetType().FullName}.", e);
88 }
89 }
90 }
91 }
92}
static int Min(int x1, int x2)
static int Max(int x1, int x2)
static void SetPosition(Widget widget, Vector2 position)
Dictionary< Widget, Vector2 > m_positions
override void WidgetRemoved(Widget widget)
override void MeasureOverride(Vector2 parentAvailableSize)
override void ArrangeOverride()
Vector2? GetWidgetPosition(Widget widget)
void SetWidgetPosition(Widget widget, Vector2? position)
readonly WidgetsList Children
static void ArrangeChildWidgetInCell(Vector2 c1, Vector2 c2, Widget widget)
virtual float MarginVerticalSum
Vector2 ParentDesiredSize
virtual void Arrange(Vector2 position, Vector2 parentActualSize)
virtual bool IsVisible
virtual float MarginHorizontalSum
Vector2 DesiredSize
virtual Vector2 MarginHorizontalSumAndVerticalSum
Vector2 ActualSize
virtual void Measure(Vector2 parentAvailableSize)
static readonly Vector2 Zero
static Vector2 Max(Vector2 v, float f)