OSDN Git Service

bb2fac6fcd81f5073d37303ff0397aa1333983f3
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / scenes / scene2d / ui / WidgetGroup.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * \r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  * \r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  ******************************************************************************/\r
16 \r
17 package com.badlogic.gdx.scenes.scene2d.ui;\r
18 \r
19 import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
20 import com.badlogic.gdx.scenes.scene2d.Actor;\r
21 import com.badlogic.gdx.scenes.scene2d.Group;\r
22 import com.badlogic.gdx.scenes.scene2d.Stage;\r
23 import com.badlogic.gdx.scenes.scene2d.utils.Layout;\r
24 import com.badlogic.gdx.utils.SnapshotArray;\r
25 \r
26 /** A {@link Group} that participates in layout and provides a minimum, preferred, and maximum size.\r
27  * <p>\r
28  * The default preferred size of a widget group is 0 and this is almost always overridden by a subclass. The default minimum size\r
29  * returns the preferred size, so a subclass may choose to return 0 for minimum size if it wants to allow itself to be sized\r
30  * smaller than the preferred size. The default maximum size is 0, which means no maximum size.\r
31  * <p>\r
32  * See {@link Layout} for details on how a widget group should participate in layout. A widget group's mutator methods should call\r
33  * {@link #invalidate()} or {@link #invalidateHierarchy()} as needed. By default, invalidateHierarchy is called when child widgets\r
34  * are added and removed.\r
35  * @author Nathan Sweet */\r
36 public class WidgetGroup extends Group implements Layout {\r
37         private boolean needsLayout = true;\r
38         private boolean fillParent;\r
39         private boolean layoutEnabled = true;\r
40 \r
41         public float getMinWidth () {\r
42                 return getPrefWidth();\r
43         }\r
44 \r
45         public float getMinHeight () {\r
46                 return getPrefHeight();\r
47         }\r
48 \r
49         public float getPrefWidth () {\r
50                 return 0;\r
51         }\r
52 \r
53         public float getPrefHeight () {\r
54                 return 0;\r
55         }\r
56 \r
57         public float getMaxWidth () {\r
58                 return 0;\r
59         }\r
60 \r
61         public float getMaxHeight () {\r
62                 return 0;\r
63         }\r
64 \r
65         public void setLayoutEnabled (boolean enabled) {\r
66                 if (layoutEnabled == enabled) return;\r
67                 layoutEnabled = enabled;\r
68                 setLayoutEnabled(this, enabled);\r
69         }\r
70 \r
71         private void setLayoutEnabled (Group parent, boolean enabled) {\r
72                 SnapshotArray<Actor> children = getChildren();\r
73                 for (int i = 0, n = children.size; i < n; i++) {\r
74                         Actor actor = children.get(i);\r
75                         if (actor instanceof Layout)\r
76                                 ((Layout)actor).setLayoutEnabled(enabled);\r
77                         else if (actor instanceof Group) //\r
78                                 setLayoutEnabled((Group)actor, enabled);\r
79                 }\r
80         }\r
81 \r
82         public void validate () {\r
83                 if (!layoutEnabled) return;\r
84                 Group parent = getParent();\r
85                 if (fillParent && parent != null) {\r
86                         Stage stage = getStage();\r
87 \r
88                         float parentWidth, parentHeight;\r
89                         if (stage != null && parent == stage.getRoot()) {\r
90                                 parentWidth = stage.getWidth();\r
91                                 parentHeight = stage.getHeight();\r
92                         } else {\r
93                                 parentWidth = parent.getWidth();\r
94                                 parentHeight = parent.getHeight();\r
95                         }\r
96                         if (getWidth() != parentWidth || getHeight() != parentHeight) {\r
97                                 setWidth(parentWidth);\r
98                                 setHeight(parentHeight);\r
99                                 invalidate();\r
100                         }\r
101                 }\r
102 \r
103                 if (!needsLayout) return;\r
104                 needsLayout = false;\r
105                 layout();\r
106         }\r
107 \r
108         /** Returns true if the widget's layout has been {@link #invalidate() invalidated}. */\r
109         public boolean needsLayout () {\r
110                 return needsLayout;\r
111         }\r
112 \r
113         public void invalidate () {\r
114                 needsLayout = true;\r
115         }\r
116 \r
117         public void invalidateHierarchy () {\r
118                 invalidate();\r
119                 Group parent = getParent();\r
120                 if (parent instanceof Layout) ((Layout)parent).invalidateHierarchy();\r
121         }\r
122 \r
123         protected void childrenChanged () {\r
124                 invalidateHierarchy();\r
125         }\r
126 \r
127         public void pack () {\r
128                 float newWidth = getPrefWidth();\r
129                 float newHeight = getPrefHeight();\r
130                 if (newWidth != getWidth() || newHeight != getHeight()) {\r
131                         setWidth(newWidth);\r
132                         setHeight(newHeight);\r
133                         invalidate();\r
134                 }\r
135                 validate();\r
136         }\r
137 \r
138         public void setFillParent (boolean fillParent) {\r
139                 this.fillParent = fillParent;\r
140         }\r
141 \r
142         public void layout () {\r
143         }\r
144 \r
145         /** If this method is overridden, the super method or {@link #validate()} should be called to ensure the widget group is laid\r
146          * out. */\r
147         public void draw (SpriteBatch batch, float parentAlpha) {\r
148                 validate();\r
149                 super.draw(batch, parentAlpha);\r
150         }\r
151 }\r