1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
package ch.asynk.gdx.boardgame.ui;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import ch.asynk.gdx.boardgame.Drawable;
import ch.asynk.gdx.boardgame.Paddable;
import ch.asynk.gdx.boardgame.Positionable;
import ch.asynk.gdx.boardgame.Touchable;
public abstract class Element implements Drawable, Paddable, Positionable, Touchable
{
public static boolean DEBUG_GEOMETRY = false;
public boolean blocked;
public boolean visible;
protected float padding;
protected Element parent;
protected int sizing; // sizing policy
protected Alignment alignment; // where to position itself
protected Rectangle rect; // outer drawing coordinates
protected Rectangle innerRect; // inner drawing coordinates
protected float x, y; // given position
protected boolean dirty; // geometry must be computed
protected boolean damaged; // child Element is dirty
protected Element()
{
this.blocked = false;
this.visible = true;
this.padding = 0;
this.parent = null;
this.sizing = Sizing.NONE;
this.alignment = alignment.RELATIVE;
this.rect = new Rectangle(0, 0, 0, 0);
this.innerRect = new Rectangle(0, 0, 0, 0);
this.x = this.y = 0;
this.dirty = true;
this.damaged = false;
}
@Override public final float getX() { return rect.x; }
@Override public final float getY() { return rect.y; }
@Override public final float getWidth() { return rect.width; }
@Override public final float getHeight() { return rect.height; }
@Override public final float getInnerX() { return rect.x + padding; }
@Override public final float getInnerY() { return rect.y + padding; }
@Override public final float getInnerWidth() { return rect.width - 2 * padding; }
@Override public final float getInnerHeight() { return rect.height - 2 * padding; }
@Override public final float getInnerTop() { return rect.y + rect.height - padding; }
@Override public final float getInnerRight() { return rect.x + rect.width - padding; }
@Override public String toString()
{
return print(0);
}
protected String print(int level)
{
String suffix = "";
for (int i = 0; i < level; i++)
suffix += " ";
String r = suffix;
r += " : " + (int)x + " " + (int)y +
" [" + (int)rect.x + " " + (int)rect.y + " " + (int)rect.width + " " + (int)rect.height + "] +" +
(int)padding + " " + alignment + " " + Sizing.print(sizing) + " "+ getClass().getName() + " " + Integer.toHexString(hashCode());
if (level < 0)
return r;
if (parent != null)
r += "\n" + parent.print(level + 1);
else
r += "\n" + suffix + " *";
return r;
}
protected void print(String msg)
{
System.err.println(msg + " : " + getClass().getName() + " " + Integer.toHexString(hashCode()));
}
public void clear()
{
if (DEBUG_GEOMETRY) print(" clear");
this.dirty = false;
this.damaged = false;
}
public void taint()
{
if (this.dirty) {
if (DEBUG_GEOMETRY) print(" already dirty");
return;
}
if (DEBUG_GEOMETRY) print(" taint");
this.dirty = true;
if (parent != null) parent.damage();
}
public void damage()
{
if (this.damaged) {
if (DEBUG_GEOMETRY) print(" already damaged");
return;
}
if (DEBUG_GEOMETRY) print(" damage");
this.damaged = true;
if (parent != null) parent.damage();
}
@Override public void setPosition(float x, float y, float w, float h)
{
this.x = x;
this.y = y;
this.rect.width = w;
this.rect.height = h;
taint();
}
public final void setPositionClear(float x, float y, Rectangle area)
{
this.x = x;
this.y = y;
computePosition(area);
clear();
}
public void setParent(Element parent)
{
setParent(parent, null);
}
public void setParent(Element parent, Alignment alignment)
{
this.parent = parent;
if (alignment != null) this.alignment = alignment;
taint();
}
@Override public void setPadding(float padding)
{
this.padding = padding;
taint();
}
public void setSizing(int sizing)
{
this.sizing = sizing;
taint();
}
public void setAlignment(Alignment alignment)
{
this.alignment = alignment;
taint();
}
@Override public final void centerOn(float cx, float cy)
{
setPosition((cx - (rect.width / 2f)), (cy - (rect.height / 2f)));
}
@Override public void translate(float dx, float dy)
{
setPosition(x + dx, y + dy);
}
public final void setPosition(Rectangle r)
{
setPosition(r.x, r.y, r.width, r.height);
}
@Override public final void setPosition(float x, float y)
{
setPosition(x, y, rect.width, rect.height);
}
protected void computeDimensions() { }
protected void computeDimensions(Rectangle area)
{
if (Sizing.contains(sizing, Sizing.FILL_X)) {
this.rect.width = area.width;
}
if (Sizing.contains(sizing, Sizing.FILL_Y)) {
this.rect.height = area.height;
}
}
private void computePosition(Rectangle area)
{
if (alignment == Alignment.ABSOLUTE) {
rect.x = x;
rect.y = y;
} else if (alignment == Alignment.RELATIVE) {
rect.x = x + area.x;
rect.y = y + area.y;
} else {
rect.x = x + alignment.getX(area, rect.width);
rect.y = y + alignment.getY(area, rect.height);
}
innerRect.set(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight());
if (DEBUG_GEOMETRY) System.err.println(" pos " + print(-1));
}
public void computeGeometry(Rectangle area, boolean resized)
{
if (dirty || resized) {
computeDimensions();
computeDimensions(area);
computePosition(area);
clear();
} else if (damaged) {
damaged = false;
}
}
public abstract void drawReal(Batch batch);
@Override public void draw(Batch batch)
{
if (!visible) return;
drawReal(batch);
}
@Override public void drawDebug(ShapeRenderer shapeRenderer)
{
shapeRenderer.rect(getX(), getY(), getWidth(), getHeight());
}
@Override public Element touch(float x, float y)
{
if (!blocked && visible && rect.contains(x, y)) {
return this;
}
return null;
}
@Override public boolean drag(float x, float y, int dx, int dy)
{
if (blocked || !visible) return false;
return rect.contains(x, y);
}
}
|