1 /**
2  * Component Group
3  */
4 module d2d.ComponentGroup;
5 
6 import std.algorithm;
7 import d2d;
8 
9 /**
10  * A ComponentGroup is just a way of grouping a bunch of related components into one singular group
11  * From there, a ComponentGroup functions exactly as any other normal component would
12  * Unexpected behaviour may stem if the components in the component group aren't all of the same Display
13  */
14 class ComponentGroup : Component {
15 
16     Component[] subComponents; ///The components that are in this ComponentGroup
17 
18     /**
19      * Gets the location of this group as the smallest rectangle that contains all components
20      */
21     override @property iRectangle location() {
22         iVector minValue = new iVector(int.max);
23         iVector maxValue = new iVector(int.min);
24         foreach (component; this.subComponents) {
25             if (component.location.initialPoint.x < minValue.x) {
26                 minValue.x = component.location.initialPoint.x;
27             }
28             if (component.location.initialPoint.y < minValue.y) {
29                 minValue.y = component.location.initialPoint.y;
30             }
31             if (component.location.initialPoint.x + component.location.extent.x > maxValue.x) {
32                 maxValue.x = component.location.initialPoint.x + component.location.extent.x;
33             }
34             if (component.location.initialPoint.y + component.location.extent.y > maxValue.y) {
35                 maxValue.y = component.location.initialPoint.y + component.location.initialPoint.y;
36             }
37         }
38         iVector difference = maxValue - minValue;
39         return new iRectangle(minValue.x, minValue.y, difference.x, difference.y);
40     }
41 
42     /**
43      * Creates a ComponentGroup given the component-required Display as well as what sub components are in the group
44      * Unexpected behaviour may stem if the components in the component group aren't all of the same Display
45      */
46     this(Display container, Component[] subComponents) {
47         super(container);
48         this.subComponents = subComponents;
49     }
50 
51     /**
52      * The group handles events by sending events to the sub components
53      * Components recieve events in the same order they are in the group
54      */
55     void handleEvent(SDL_Event event) {
56         this.subComponents.each!(component => component.handleEvent(event));
57     }
58 
59     /**
60      * The group handles drawing by just drawing all of the sub components
61      * Components are drawn in the order they are in the group; later components go on top of earlier components
62      */
63     override void draw() {
64         this.subComponents.each!(component => component.draw());
65     }
66 
67 }