/
Viewport.java
245 lines (206 loc) · 8.65 KB
/
Viewport.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
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.utils.viewport;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.glutils.HdpiUtils;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.Ray;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
/** Manages a {@link Camera} and determines how world coordinates are mapped to and from the screen.
* @author Daniel Holderbaum
* @author Nathan Sweet */
public abstract class Viewport {
private Camera camera;
private float worldWidth, worldHeight;
private int screenX, screenY, screenWidth, screenHeight;
private final Vector3 tmp = new Vector3();
/** Calls {@link #apply(boolean)} with false. */
public void apply () {
apply(false);
}
/** Applies the viewport to the camera and sets the glViewport.
* @param centerCamera If true, the camera position is set to the center of the world. */
public void apply (boolean centerCamera) {
HdpiUtils.glViewport(screenX, screenY, screenWidth, screenHeight);
camera.viewportWidth = worldWidth;
camera.viewportHeight = worldHeight;
if (centerCamera) camera.position.set(worldWidth / 2, worldHeight / 2, 0);
camera.update();
}
/** Calls {@link #update(int, int, boolean)} with false. */
public final void update (int screenWidth, int screenHeight) {
update(screenWidth, screenHeight, false);
}
/** Configures this viewport's screen bounds using the specified screen size and calls {@link #apply(boolean)}. Typically
* called from {@link ApplicationListener#resize(int, int)} or {@link Screen#resize(int, int)}.
* <p>
* The default implementation only calls {@link #apply(boolean)}. */
public void update (int screenWidth, int screenHeight, boolean centerCamera) {
apply(centerCamera);
}
/** Transforms the specified screen coordinate to world coordinates.
* @return The vector that was passed in, transformed to world coordinates.
* @see Camera#unproject(Vector3) */
public Vector2 unproject (Vector2 screenCoords) {
tmp.set(screenCoords.x, screenCoords.y, 1);
camera.unproject(tmp, screenX, screenY, screenWidth, screenHeight);
screenCoords.set(tmp.x, tmp.y);
return screenCoords;
}
/** Transforms the specified world coordinate to screen coordinates.
* @return The vector that was passed in, transformed to screen coordinates.
* @see Camera#project(Vector3) */
public Vector2 project (Vector2 worldCoords) {
tmp.set(worldCoords.x, worldCoords.y, 1);
camera.project(tmp, screenX, screenY, screenWidth, screenHeight);
worldCoords.set(tmp.x, tmp.y);
return worldCoords;
}
/** Transforms the specified screen coordinate to world coordinates.
* @return The vector that was passed in, transformed to world coordinates.
* @see Camera#unproject(Vector3) */
public Vector3 unproject (Vector3 screenCoords) {
camera.unproject(screenCoords, screenX, screenY, screenWidth, screenHeight);
return screenCoords;
}
/** Transforms the specified world coordinate to screen coordinates.
* @return The vector that was passed in, transformed to screen coordinates.
* @see Camera#project(Vector3) */
public Vector3 project (Vector3 worldCoords) {
camera.project(worldCoords, screenX, screenY, screenWidth, screenHeight);
return worldCoords;
}
/** @see Camera#getPickRay(float, float, float, float, float, float) */
public Ray getPickRay (float screenX, float screenY) {
return camera.getPickRay(screenX, screenY, this.screenX, this.screenY, screenWidth, screenHeight);
}
/** @see ScissorStack#calculateScissors(Camera, float, float, float, float, Matrix4, Rectangle, Rectangle) */
public void calculateScissors (Matrix4 batchTransform, Rectangle area, Rectangle scissor) {
ScissorStack.calculateScissors(camera, screenX, screenY, screenWidth, screenHeight, batchTransform, area, scissor);
}
/** Transforms a point to real screen coordinates (as opposed to OpenGL ES window coordinates), where the origin is in the top
* left and the the y-axis is pointing downwards. */
public Vector2 toScreenCoordinates (Vector2 worldCoords, Matrix4 transformMatrix) {
tmp.set(worldCoords.x, worldCoords.y, 0);
tmp.mul(transformMatrix);
camera.project(tmp, screenX, screenY, screenWidth, screenHeight);
tmp.y = Gdx.graphics.getHeight() - tmp.y;
worldCoords.x = tmp.x;
worldCoords.y = tmp.y;
return worldCoords;
}
public Camera getCamera () {
return camera;
}
public void setCamera (Camera camera) {
this.camera = camera;
}
public float getWorldWidth () {
return worldWidth;
}
/** The virtual width of this viewport in world coordinates. This width is scaled to the viewport's screen width. */
public void setWorldWidth (float worldWidth) {
this.worldWidth = worldWidth;
}
public float getWorldHeight () {
return worldHeight;
}
/** The virtual height of this viewport in world coordinates. This height is scaled to the viewport's screen height. */
public void setWorldHeight (float worldHeight) {
this.worldHeight = worldHeight;
}
public void setWorldSize (float worldWidth, float worldHeight) {
this.worldWidth = worldWidth;
this.worldHeight = worldHeight;
}
public int getScreenX () {
return screenX;
}
/** Sets the viewport's offset from the left edge of the screen. This is typically set by
* {@link #update(int, int, boolean)}. */
public void setScreenX (int screenX) {
this.screenX = screenX;
}
public int getScreenY () {
return screenY;
}
/** Sets the viewport's offset from the bottom edge of the screen. This is typically set by
* {@link #update(int, int, boolean)}. */
public void setScreenY (int screenY) {
this.screenY = screenY;
}
public int getScreenWidth () {
return screenWidth;
}
/** Sets the viewport's width in screen coordinates. This is typically set by {@link #update(int, int, boolean)}. */
public void setScreenWidth (int screenWidth) {
this.screenWidth = screenWidth;
}
public int getScreenHeight () {
return screenHeight;
}
/** Sets the viewport's height in screen coordinates. This is typically set by {@link #update(int, int, boolean)}. */
public void setScreenHeight (int screenHeight) {
this.screenHeight = screenHeight;
}
/** Sets the viewport's position in screen coordinates. This is typically set by {@link #update(int, int, boolean)}. */
public void setScreenPosition (int screenX, int screenY) {
this.screenX = screenX;
this.screenY = screenY;
}
/** Sets the viewport's size in screen coordinates. This is typically set by {@link #update(int, int, boolean)}. */
public void setScreenSize (int screenWidth, int screenHeight) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
/** Sets the viewport's bounds in screen coordinates. This is typically set by {@link #update(int, int, boolean)}. */
public void setScreenBounds (int screenX, int screenY, int screenWidth, int screenHeight) {
this.screenX = screenX;
this.screenY = screenY;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
/** Returns the left gutter (black bar) width in screen coordinates. */
public int getLeftGutterWidth () {
return screenX;
}
/** Returns the right gutter (black bar) x in screen coordinates. */
public int getRightGutterX () {
return screenX + screenWidth;
}
/** Returns the right gutter (black bar) width in screen coordinates. */
public int getRightGutterWidth () {
return Gdx.graphics.getWidth() - (screenX + screenWidth);
}
/** Returns the bottom gutter (black bar) height in screen coordinates. */
public int getBottomGutterHeight () {
return screenY;
}
/** Returns the top gutter (black bar) y in screen coordinates. */
public int getTopGutterY () {
return screenY + screenHeight;
}
/** Returns the top gutter (black bar) height in screen coordinates. */
public int getTopGutterHeight () {
return Gdx.graphics.getHeight() - (screenY + screenHeight);
}
}