1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package com.badlogic.gdx.scenes.scene2d.ui;
\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
26 /** A {@link Group} that participates in layout and provides a minimum, preferred, and maximum size.
\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
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
41 public float getMinWidth () {
\r
42 return getPrefWidth();
\r
45 public float getMinHeight () {
\r
46 return getPrefHeight();
\r
49 public float getPrefWidth () {
\r
53 public float getPrefHeight () {
\r
57 public float getMaxWidth () {
\r
61 public float getMaxHeight () {
\r
65 public void setLayoutEnabled (boolean enabled) {
\r
66 if (layoutEnabled == enabled) return;
\r
67 layoutEnabled = enabled;
\r
68 setLayoutEnabled(this, enabled);
\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
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
88 float parentWidth, parentHeight;
\r
89 if (stage != null && parent == stage.getRoot()) {
\r
90 parentWidth = stage.getWidth();
\r
91 parentHeight = stage.getHeight();
\r
93 parentWidth = parent.getWidth();
\r
94 parentHeight = parent.getHeight();
\r
96 if (getWidth() != parentWidth || getHeight() != parentHeight) {
\r
97 setWidth(parentWidth);
\r
98 setHeight(parentHeight);
\r
103 if (!needsLayout) return;
\r
104 needsLayout = false;
\r
108 /** Returns true if the widget's layout has been {@link #invalidate() invalidated}. */
\r
109 public boolean needsLayout () {
\r
110 return needsLayout;
\r
113 public void invalidate () {
\r
114 needsLayout = true;
\r
117 public void invalidateHierarchy () {
\r
119 Group parent = getParent();
\r
120 if (parent instanceof Layout) ((Layout)parent).invalidateHierarchy();
\r
123 protected void childrenChanged () {
\r
124 invalidateHierarchy();
\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
138 public void setFillParent (boolean fillParent) {
\r
139 this.fillParent = fillParent;
\r
142 public void layout () {
\r
145 /** If this method is overridden, the super method or {@link #validate()} should be called to ensure the widget group is laid
\r
147 public void draw (SpriteBatch batch, float parentAlpha) {
\r
149 super.draw(batch, parentAlpha);
\r