-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBackgroundColor.java
More file actions
255 lines (203 loc) · 4.91 KB
/
BackgroundColor.java
File metadata and controls
255 lines (203 loc) · 4.91 KB
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
247
248
249
250
251
252
253
254
255
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
public class BackgroundColor implements Drawable {
private Float x;
private Float y;
private Float width;
private Float height;
private Boolean fillParent;
private String filename;
private Texture texture;
private TextureRegion textureRegion;
private Sprite sprite;
private Color color;
public BackgroundColor(String filename) {
this(filename, 0.0f, 0.0f, 0.0f, 0.0f);
}
public BackgroundColor(String filename, float x, float y) {
this(filename, x, y, 0.0f, 0.0f);
}
public BackgroundColor(String filename, float x, float y, float width, float height) {
this.setPosition(x, y);
this.setSize(width, height);
initialize(filename);
}
private void initialize(String filename) {
setFilename(filename);
if (x == null || y == null)
setPosition(); // x = 0.0f; y = 0.0f;
if (width == null || height == null || width < 0.0f || height < 0.0f)
setSize(); // width = 0.0f; height = 0.0f;
if (color == null)
setColor(255, 255, 255, 255);
if (sprite == null) {
try {
setSprite();
} catch (Exception e) {
System.err.println(e);
}
}
if (fillParent == null)
fillParent = true;
}
private void setTexture() {
if (texture != null)
texture.dispose();
texture = new Texture(Gdx.files.internal(getFilename()));
}
private void setTextureRegion() {
textureRegion = new TextureRegion(texture, (int) getWidth(), (int) getHeight());
}
private void setSprite() {
if (texture == null)
setTexture();
setTextureRegion();
sprite = new Sprite(textureRegion);
setSpriteColor();
}
private void setSpriteColor() {
sprite.setColor(color.r, color.g, color.b, color.a);
}
private void setPosition() {
this.x = 0.0f;
this.y = 0.0f;
}
private void setPosition(float x, float y) {
this.x = x;
this.y = y;
}
private void setSize() {
this.width = sprite.getWidth();
this.height = sprite.getHeight();
}
private void setSize(float width, float height) {
this.width = width;
this.height = height;
}
public void setColor(int r, int g, int b, int a) {
color = new Color(r/255f, g/255f, b/255f, a/255f);
if (sprite != null) {
setSpriteColor();
}
}
public void setColor(float r, float g, float b, float a) {
color = new Color(r/255f, g/255f, b/255f, a/255f);
if (sprite != null) {
setSpriteColor();
}
}
private void setSpritePosition(float x, float y) {
sprite.setX(x);
sprite.setY(y);
}
private void updateSprite(float x, float y, float width, float height) {
if (fillParent) {
setSpritePosition(x, y);
if (width != textureRegion.getRegionWidth() ||
height != textureRegion.getRegionHeight()) {
setSize(width, height);
setSprite();
}
}
}
@Override
public void draw(Batch batch, float x, float y, float width, float height) {
updateSprite(x, y, width, height);
sprite.draw(batch);
}
@Override
public float getLeftWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setLeftWidth(float leftWidth) {
// TODO Auto-generated method stub
}
@Override
public float getRightWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setRightWidth(float rightWidth) {
// TODO Auto-generated method stub
}
@Override
public float getTopHeight() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setTopHeight(float topHeight) {
// TODO Auto-generated method stub
}
@Override
public float getBottomHeight() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setBottomHeight(float bottomHeight) {
// TODO Auto-generated method stub
}
@Override
public float getMinWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setMinWidth(float minWidth) {
// TODO Auto-generated method stub
}
@Override
public float getMinHeight() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setMinHeight(float minHeight) {
// TODO Auto-generated method stub
}
private void setFilename(String filename) {
this.filename = filename;
}
private String getFilename() {
return filename;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public Boolean getFillParent() {
return fillParent;
}
public void setFillParent(Boolean fillParent) {
this.fillParent = fillParent;
}
}