File size: 1,787 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
// -----------------------------------------------------------
class SpacerBase : ScriptedWidgetEventHandler
{
	protected Widget m_root;
	protected int m_count;

	// -----------------------------------------------------------
	void OnWidgetScriptInit(Widget w)
	{
		m_root = w;
		m_count = 0;
	
		Widget child = m_root.GetChildren();
		while (child)
		{
			m_count++;
			child.SetFlags(WidgetFlags.EXACTPOS | WidgetFlags.EXACTSIZE, false);
			child = child.GetSibling();
		}
	
		m_root.SetHandler(this);
	}
	
	// -----------------------------------------------------------
	override bool OnUpdate(Widget w)
	{
		if (w == m_root) UpdateLayout();
		return false;
	}
	
	// -----------------------------------------------------------
	override bool OnChildAdd( Widget  w, Widget  child)
	{
		m_count++;
		child.SetFlags(WidgetFlags.EXACTPOS | WidgetFlags.EXACTSIZE, false);
		return false;
	}
	
	// -----------------------------------------------------------
	override bool OnChildRemove( Widget  w, Widget  child)
	{
		m_count--;
		return false;
	}
	
	// -----------------------------------------------------------
	protected int GetChildIndex(Widget w)
	{
		Widget child = m_root.GetChildren();
	
		int index = 0;
		while (child)
		{
			if (child == w) return index;
	
			index++;
			child = child.GetSibling();
		}
	
		return INDEX_NOT_FOUND;
	}
	
	// -----------------------------------------------------------
	void UpdateLayout()
	{
		if (m_count == 0) return;
	
		float width;
		float height;
		m_root.GetScreenSize(width, height);
	
		Widget child = m_root.GetChildren();
		
		int index = 0;
		while (child)
		{
			UpdateChild(child, width, height, index);
			index++;
			child = child.GetSibling();
		}
	}
	protected void UpdateChild(Widget child, float w, float h, int index) {}
};