1 /**
2 Copyright: Copyright (c) 2013-2014 Andrey Penechko.
3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module anchovy.gui.layouts.absolutelayout;
8 
9 import anchovy.gui;
10 
11 public import anchovy.gui.interfaces.ilayout;
12 
13 //version = debug_absolute;
14 
15 class AbsoluteLayout : ILayout
16 {
17 	override void minimize(Widget root)
18 	{
19 	}
20 
21 	override void expand(Widget root)
22 	{
23 		Widget[] children = root.getPropertyAs!("children", Widget[]);
24 
25 		version(debug_absolute) writefln("AbsoluteLayout expand %s", root["id"]);
26 
27 		foreach(child; children)
28 		{
29 			version(debug_absolute) writeln(child["id"]);
30 
31 			ivec2 childSize = child.getPropertyAs!("prefSize", ivec2);
32 			ivec2 childMinSize = child.getPropertyAs!("minSize", ivec2);
33 			childSize = ivec2(max(childSize.x, childMinSize.x), max(childSize.y, childMinSize.y));
34 			child.setProperty!("size")(childSize);
35 		}
36 	}
37 
38 	override void onContainerResized(Widget root, ivec2 oldSize, ivec2 newSize)
39 	{
40 		Widget[] children = root.getPropertyAs!("children", Widget[]);
41 
42 		int dx = newSize.x - oldSize.x;
43 		int dy = newSize.y - oldSize.y;
44 
45 		foreach(ref widget; children)
46 		{
47 			int anchor;
48 			ivec2 pos = widget.getPropertyAs!("position", int);
49 			ivec2 size = widget.getPropertyAs!("size", ivec2);
50 			anchor = widget.getPropertyAs!("anchor", int);
51 
52 			if ((anchor & Sides.left) && (anchor & Sides.right))
53 			{
54 				int newWidth = size.x + dx;
55 				if (newWidth >= 0)
56 					size = ivec2(newWidth, size.y);
57 				else
58 					size = ivec2(0, size.y);
59 			}
60 			else if (anchor & Sides.left)
61 			{
62 				// Do nothing. X position stays unchanged, as well as width
63 			}
64 			else if (anchor & Sides.right)
65 			{
66 				pos = ivec2(pos.x + dx, pos.y);
67 			}
68 			else
69 			{
70 				assert(false); // Not yet implemented
71 			}
72 
73 			if (anchor & Sides.top && anchor & Sides.bottom)
74 			{
75 				size = ivec2(size.x, size.y + dy);
76 			}
77 			else if (anchor & Sides.top)
78 			{
79 				// Do nothing. Y position stays unchanged, as well as height
80 			}
81 			else if (anchor & Sides.bottom)
82 			{
83 				pos = ivec2(pos.x, pos.y + dy);
84 			}
85 			else
86 			{
87 				assert(false); // Not yet implemented
88 			}
89 
90 			widget.setProperty!"position"(pos);
91 			widget.setProperty!"size"(size);
92 			widget.setProperty!"anchor"(anchor);
93 		}
94 	}
95 
96 
97 }
98