1/*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17package com.badlogic.gdx.scenes.scene2d.ui;
18
19import com.badlogic.gdx.scenes.scene2d.Actor;
20import com.badlogic.gdx.scenes.scene2d.utils.Layout;
21
22/** Value placeholder, allowing the value to be computed on request. Values are provided an actor for context which reduces the
23 * number of value instances that need to be created and reduces verbosity in code that specifies values.
24 * @author Nathan Sweet */
25abstract public class Value {
26	/** @param context May be null. */
27	abstract public float get (Actor context);
28
29	/** A value that is always zero. */
30	static public final Fixed zero = new Fixed(0);
31
32	/** A fixed value that is not computed each time it is used.
33	 * @author Nathan Sweet */
34	static public class Fixed extends Value {
35		private final float value;
36
37		public Fixed (float value) {
38			this.value = value;
39		}
40
41		public float get (Actor context) {
42			return value;
43		}
44	}
45
46	/** Value that is the minWidth of the actor in the cell. */
47	static public Value minWidth = new Value() {
48		public float get (Actor context) {
49			if (context instanceof Layout) return ((Layout)context).getMinWidth();
50			return context == null ? 0 : context.getWidth();
51		}
52	};
53
54	/** Value that is the minHeight of the actor in the cell. */
55	static public Value minHeight = new Value() {
56		public float get (Actor context) {
57			if (context instanceof Layout) return ((Layout)context).getMinHeight();
58			return context == null ? 0 : context.getHeight();
59		}
60	};
61
62	/** Value that is the prefWidth of the actor in the cell. */
63	static public Value prefWidth = new Value() {
64		public float get (Actor context) {
65			if (context instanceof Layout) return ((Layout)context).getPrefWidth();
66			return context == null ? 0 : context.getWidth();
67
68		}
69	};
70
71	/** Value that is the prefHeight of the actor in the cell. */
72	static public Value prefHeight = new Value() {
73		public float get (Actor context) {
74			if (context instanceof Layout) return ((Layout)context).getPrefHeight();
75			return context == null ? 0 : context.getHeight();
76		}
77	};
78
79	/** Value that is the maxWidth of the actor in the cell. */
80	static public Value maxWidth = new Value() {
81		public float get (Actor context) {
82			if (context instanceof Layout) return ((Layout)context).getMaxWidth();
83			return context == null ? 0 : context.getWidth();
84		}
85	};
86
87	/** Value that is the maxHeight of the actor in the cell. */
88	static public Value maxHeight = new Value() {
89		public float get (Actor context) {
90			if (context instanceof Layout) return ((Layout)context).getMaxHeight();
91			return context == null ? 0 : context.getHeight();
92		}
93	};
94
95	/** Returns a value that is a percentage of the actor's width. */
96	static public Value percentWidth (final float percent) {
97		return new Value() {
98			public float get (Actor actor) {
99				return actor.getWidth() * percent;
100			}
101		};
102	}
103
104	/** Returns a value that is a percentage of the actor's height. */
105	static public Value percentHeight (final float percent) {
106		return new Value() {
107			public float get (Actor actor) {
108				return actor.getHeight() * percent;
109			}
110		};
111	}
112
113	/** Returns a value that is a percentage of the specified actor's width. The context actor is ignored. */
114	static public Value percentWidth (final float percent, final Actor actor) {
115		if (actor == null) throw new IllegalArgumentException("actor cannot be null.");
116		return new Value() {
117			public float get (Actor context) {
118				return actor.getWidth() * percent;
119			}
120		};
121	}
122
123	/** Returns a value that is a percentage of the specified actor's height. The context actor is ignored. */
124	static public Value percentHeight (final float percent, final Actor actor) {
125		if (actor == null) throw new IllegalArgumentException("actor cannot be null.");
126		return new Value() {
127			public float get (Actor context) {
128				return actor.getHeight() * percent;
129			}
130		};
131	}
132}
133