File size: 2,295 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// -----------------------------------------------------------
class AutoHeightSpacer : ScriptedWidgetEventHandler
{
	reference bool AlignChilds;
	reference int MinHeight;
	reference int MaxHeight;
	reference int Gap;
	reference float Top;
	protected Widget m_root;

	void Update()
	{
		float x = 0;
		float y = 0;
		float width = 0;
		float height = 0;
		float heightOld = 0;
		float top = Top;
		float rowRight;
		float rowHeight;
		float rowWidth;
		Widget child = m_root.GetChildren();

		//PrintString(m_root.GetName() + ": AutoHeightSpacer::Update()");

		if ( !AlignChilds )	top = -100000;

		if (child != NULL)
		{
		// first row init
		m_root.GetScreenSize(rowWidth, height);
		rowHeight = 0;
		rowRight = rowWidth;

		while (child)
		{
			if (child.IsVisible() == false || child.GetName() == "SelectedContainer" || child.GetName() == "Icon")
			{
				// skip invisible widgets
				child = child.GetSibling();
				continue;
			}

			child.GetScreenSize(width, height);

			if (AlignChilds)
			{
				child.SetFlags(WidgetFlags.EXACTPOS, false);

				// no space left in this row, move to next one
				if (rowRight < width)
				{
					top += rowHeight;
					if ( rowHeight > 0 ) top += Gap;
					rowRight = rowWidth;
					rowHeight = 0;
				}

				// increase row height if necessary
				if (height > rowHeight) rowHeight = height;

				child.SetPos(rowWidth - rowRight, top, false);
				rowRight -= width + Gap;
			}
			else
			{
				child.GetScreenPos(x, y);
				y += height;
				if (top < y) top = y;
			}

			child = child.GetSibling();
		}

		// add last row height;
		top += rowHeight;

		if (AlignChilds)
		{
			height = top;
		}
		else
		{
			m_root.GetScreenPos(x, y);
			height = top - y;
		}
		}

		m_root.GetSize(width, heightOld);

		if (MaxHeight > 0 && height > MaxHeight)
		{
			height = MaxHeight;
		}

		if (MinHeight > height)
		{
			height = MinHeight;
		}

		if (Math.AbsInt(heightOld - height) > 1)
		{
			m_root.SetSize(width, height);
		}
		else if (AlignChilds)
		{
			m_root.Update();
		}

		return;
	}

	void OnWidgetScriptInit(Widget w)
	{
		m_root = w;
		m_root.SetHandler(this);
		m_root.SetFlags(WidgetFlags.VEXACTPOS);
		Update();
	}

	override bool OnChildRemove( Widget  w, Widget  child) {
		if (w == m_root)
		{
			Update();
		}

		return false;
	}
};