-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenMap.java
262 lines (225 loc) · 7.38 KB
/
ScreenMap.java
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
256
257
258
259
260
261
262
package biemann.android.miner.screens.map;
import biemann.android.miner.MainGame;
import biemann.android.miner.screens.ActorText;
import biemann.android.miner.screens.DefaultScreen;
import biemann.android.miner.screens.game.LevelData;
import biemann.android.miner.screens.map.ActorBackground.BackLayerHorizontalScrollOffsetListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class ScreenMap extends DefaultScreen implements InputProcessor, BackLayerHorizontalScrollOffsetListener
{
// Constants
final private String FPS = "fps: ";
final private int FPS_FONT_SIZE = 20; //at 1.0 logical density
// Member Variables
private Stage mStage;
private InputMultiplexer mInputMultiplexer;
private int mTouchDownXCoord;
private int mTouchLastKnownXcoord;
private long mTouchDownTime;
private ActorBackground mActorBackground;
private BitmapFont mSpaceFontFps;
private ActorText mActorFPS;
private int mOldFPS;
private int mNewFPS;
private GlyphLayout glyphLayout;// used to calculate text width and height
// Constructor
public ScreenMap(MainGame game)
{
super(game);
mStage = new Stage(new FitViewport(mGame.getIdealWidth(), mGame.getIdealHeight()));
// create background Actor
mActorBackground = new ActorBackground(game.getGFXLoader(), game.getIdealWidth(), game.getIdealHeight());
mActorBackground.setPosition(0,0);
mStage.addActor(mActorBackground);
mActorBackground.setBackLayerHorizontalScrollOffsetListener(this);// to receive callbacks when the background changes
// Space Font (small: fps)
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/spacefont.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
final float fScaledFontSize = FPS_FONT_SIZE * Gdx.graphics.getDensity();
parameter.size = (int) fScaledFontSize;//NOTE: If you use too big of a size, things might explode
mSpaceFontFps = generator.generateFont(parameter);
generator.dispose();
// create FPS Actor
glyphLayout = new GlyphLayout();
final Color colorFps = new Color(Color.WHITE);
glyphLayout.setText(mSpaceFontFps, FPS+"000");
final float fFpsX = ((float)mGame.getIdealWidth()) - glyphLayout.width;
final float fFPSY = ((float)mGame.getIdealHeight()) - glyphLayout.height;
mActorFPS = new ActorText(mSpaceFontFps, FPS+"000", colorFps, fFpsX, fFPSY);
mStage.addActor(mActorFPS);
// catch the BACK button
Gdx.input.setCatchBackKey(true);
// mStage.addListener(new InputListener()
// {
//
// @Override
// public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
// super.touchDown(event, x, y, pointer, button);
// Gdx.app.log("my app", "Pressed");
// return true;
// }
//
// @Override
// public void touchUp(InputEvent event, float x, float y,int pointer, int button) {
// super.touchUp(event, x, y, pointer, button);
// Gdx.app.log("my app", "Released");
// }
//
// });
}
@Override
public void render(float delta)
{
//Gdx.gl.glClearColor(0, 0, 0, 0);//clear the screen black - not needed since the background image exists
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// update fps
mNewFPS = Gdx.graphics.getFramesPerSecond();
if (mNewFPS != mOldFPS)
{
mActorFPS.setText(FPS + mNewFPS);
mOldFPS = mNewFPS;
}
// update the actors
mStage.act(delta);
// draw the actors
mStage.draw();
}
// called when this screen is set as the screen with game.setScreen()
@Override
public void show()
{
// set the stage as the input processor
//Gdx.input.setInputProcessor(mStage);
// tutorial: https://github.com/libgdx/libgdx/wiki/Event-handling
mInputMultiplexer = new InputMultiplexer();
//multiplexer.addProcessor(new MyUiInputProcessor());//first, handle the actors
mInputMultiplexer.addProcessor(this);// if actors return false, handle general game UI
Gdx.input.setInputProcessor(mInputMultiplexer);
// add a fade-in effect to the whole stage
mStage.getRoot().getColor().a = 0f;
mStage.addAction( Actions.fadeIn( 1.0f ) );
}
// called when current screen changes from this to a different screen
@Override
public void hide()
{
// dispose the screen when leaving the screen;
// note that the dispose() method is not called automatically by the
// framework, so we must figure out when it's appropriate to call it
dispose();
}
// never called automatically
@Override
public void dispose()
{
mStage.dispose();
}
@Override
public boolean keyDown(int keycode)
{
if (keycode == Keys.BACK)
{
Gdx.app.exit();
}
//return super.keyDown(keycode);
return true;
}
@Override
public boolean keyUp(int keycode)
{
return false;
}
@Override
public boolean keyTyped(char character)
{
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
if (pointer == 0)
{
mTouchDownXCoord = screenX;
mTouchLastKnownXcoord = screenX;
mTouchDownTime = System.currentTimeMillis();
return true;
}
else
{
return false;
}
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button)
{
if (pointer == 0)
{
//mActorBackground.setScrollSpeed(0.0f);// don't use this otherwise the scrolling stops right away
final int iXdelta = mTouchDownXCoord - screenX;
final int iTimeDelta = (int) (System.currentTimeMillis() - mTouchDownTime);
if (iTimeDelta != 0)// avoid division by zero
{
float speedPixPerMS = iXdelta / iTimeDelta;
// maximum speed
if (speedPixPerMS > 2.0f)
speedPixPerMS = 2.0f;
else if (speedPixPerMS < -2.0f)
speedPixPerMS = -2.0f;
mActorBackground.setScrollSpeed(speedPixPerMS);
}
return true;
}
else
{
return false;
}
}
// Called when a finger or the mouse was dragged
@Override
public boolean touchDragged(int screenX, int screenY, int pointer)
{
if (pointer == 0)
{
final int iDeltaX = mTouchLastKnownXcoord - screenX;
//Gdx.app.log("miner", "touchDragged delta X="+screenX);
mActorBackground.setScrollSpeed((float)iDeltaX * 0.01f);
mTouchLastKnownXcoord = screenX;
return true;
}
else
{
return false;
}
}
@Override
public boolean mouseMoved(int screenX, int screenY)
{
return false;
}
// Called when the mouse wheel was scrolled
@Override
public boolean scrolled(int amount)
{
return false;
}
/**
* Implementation for BackLayerHorizontalScrollOffsetListener
*/
@Override
public void onBackLayerHorizontalScrollOffsetChanged(float x)
{
// called when user drags screen - used to move other actors
}
}