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
|
package ch.asynk.zproject.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import ch.asynk.zproject.ZProject;
public class LoadingScreen implements Screen
{
private final ZProject zproject;
private final SpriteBatch batch;
private final Color c;
private final OrthographicCamera camera;
private final float BLINK_AFTER = 2f;
private final float ALPHA_FACTOR = 1.5f;
private boolean paused;
private TextureAtlas atlas;
private AtlasRegion bar;
private AtlasRegion border;
private AtlasRegion loading;
private AtlasRegion loaded;
private int x, y;
private int count;
private float percent;
private float alpha;
private boolean incr;
private boolean isLoaded;
public interface LoadAction {
void call();
}
private LoadAction onLoaded;
public LoadingScreen(final ZProject zproject, LoadAction startLoading, LoadAction onLoaded)
{
this.zproject = zproject;
this.onLoaded = onLoaded;
this.batch = new SpriteBatch();
this.c = batch.getColor();
this.camera = new OrthographicCamera();
int w = Gdx.graphics.getWidth();
int h = Gdx.graphics.getHeight();
this.camera.setToOrtho(false, w, h);
this.camera.update();
this.paused = false;
atlas = zproject.assets.getAtlas(zproject.assets.LOADING);
bar = atlas.findRegion("bar");
border = atlas.findRegion("border");
loading = atlas.findRegion("loading");
loaded = atlas.findRegion("loaded");
computeCoords(w, h);
percent = 0f;
alpha = 1f;
incr = false;
isLoaded = false;
startLoading.call();
}
private void computeCoords(int width, int height)
{
x = (width - border.getRegionWidth()) / 2;
y = (height - border.getRegionHeight()) / 2;
}
@Override public void render(float delta)
{
if (paused) return;
if (!isLoaded) {
if (zproject.assets.update()) {
ZProject.debug("LoadingScreen", "assets loaded");
isLoaded = true;
percent = 1f;
} else {
percent = zproject.assets.getProgress();
}
}
if (!isLoaded && percent >= 1f) {
count = 0;
alpha = 1f;
incr = false;
}
delta *= ALPHA_FACTOR;
if (incr) {
alpha += delta;
if (alpha >= 1f ) {
alpha = 1f;
incr = false;
if (isLoaded) count += 1;
if (count >= BLINK_AFTER) {
onLoaded.call();
return;
}
}
} else {
alpha -= delta;
if (alpha <= 0f ) {
alpha = 0f;
incr = true;
}
}
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.setColor(c.r, c.g, c.b, 1f);
batch.draw(border, x, y);
batch.draw(bar, x + 4, y + 4, percent * bar.getRegionWidth(), bar.getRegionHeight());
batch.setColor(c.r, c.g, c.b, alpha);
if (!isLoaded) {
batch.draw(loading, x, y + border.getRegionHeight() + 3);
} else {
batch.draw(loaded, x + border.getRegionWidth() - loaded.getRegionWidth(), y - loaded.getRegionHeight() - 3);
}
batch.end();
}
@Override public void resize(int width, int height)
{
if (paused) return;
ZProject.debug("LoadingScreen", String.format("resize (%d,%d)",width, height));
this.camera.setToOrtho(false, width, height);
this.camera.update();
computeCoords(width, height);
}
@Override public void dispose()
{
ZProject.debug("LoadingScreen", "dispose()");
batch.dispose();
}
@Override public void show()
{
ZProject.debug("LoadingScreen", "show()");
}
@Override public void hide()
{
ZProject.debug("LoadingScreen", "hide()");
}
@Override public void pause()
{
paused = true;
ZProject.debug("pause() ");
}
@Override public void resume()
{
ZProject.debug("resume() ");
resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
paused = false;
}
}
|