diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2015-07-19 13:20:33 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2015-07-19 13:20:33 +0200 |
commit | de0463bcf0f76ef8b07f2719679c9e0d72745c5d (patch) | |
tree | 9a33df947ceeea16a3e20b400585b1d3c304e77e /core/src/ch/asynk/rustanddust/screens/MenuCamera.java | |
parent | e66f9f2a61d3dab4545e996046486de0d44e2901 (diff) | |
download | RustAndDust-de0463bcf0f76ef8b07f2719679c9e0d72745c5d.zip RustAndDust-de0463bcf0f76ef8b07f2719679c9e0d72745c5d.tar.gz |
welcome RustAndDust
Diffstat (limited to 'core/src/ch/asynk/rustanddust/screens/MenuCamera.java')
-rw-r--r-- | core/src/ch/asynk/rustanddust/screens/MenuCamera.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/core/src/ch/asynk/rustanddust/screens/MenuCamera.java b/core/src/ch/asynk/rustanddust/screens/MenuCamera.java new file mode 100644 index 0000000..be8341a --- /dev/null +++ b/core/src/ch/asynk/rustanddust/screens/MenuCamera.java @@ -0,0 +1,112 @@ +package ch.asynk.rustanddust.screens; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.math.Matrix4; +import com.badlogic.gdx.math.Rectangle; + +public class MenuCamera extends OrthographicCamera +{ + private static final float ZEROF = 0.01f; + + private float virtualAspect; + private final Rectangle virtual; + private final Rectangle window; + private int hudLeft; + private int hudBottom; + private int hudCorrection; + + private Matrix4 uiMatrix; + private Matrix4 uiInvProjMatrix; + + public MenuCamera(int cx, int cy, int width, int height, int hudCorrection) + { + super(width, height); + this.virtual = new Rectangle(); + this.virtual.set(cx, cy, width, height); + this.virtualAspect = (virtual.width / virtual.height); + this.window = new Rectangle(); + this.window.set(0, 0, 0, 0); + this.position.set(virtual.x, virtual.y, 0f); + this.hudLeft = 0; + this.hudBottom = 0; + this.hudCorrection = hudCorrection; + + this.uiMatrix = new Matrix4(); + this.uiInvProjMatrix = new Matrix4(); + } + + public void updateViewport(int screenWidth, int screenHeight) + { + float aspect = (screenWidth / (float) screenHeight); + float diff = (virtualAspect - aspect); + + if (diff < -ZEROF) { + viewportWidth = (virtual.height * aspect); + viewportHeight = virtual.height; + } else if (diff > ZEROF) { + viewportWidth = virtual.width; + viewportHeight = (virtual.width / aspect); + } + + window.width = screenWidth; + window.height = screenHeight; + hudLeft = hudCorrection; + hudBottom = (int) (hudLeft / aspect); + + Gdx.gl.glViewport((int)window.x, (int)window.y, (int)window.width, (int)window.height); + + update(true); + + uiMatrix.setToOrtho2D(getHudLeft(), getHudBottom(), getHudWidth(), getHudHeight()); + uiInvProjMatrix.set(uiMatrix); + Matrix4.inv(uiInvProjMatrix.val); + } + + public float getScreenWidth() + { + return window.width; + } + + public float getScreenHeight() + { + return window.height; + } + + public int getHudLeft() + { + return hudLeft; + } + + public int getHudBottom() + { + return hudBottom; + } + + public int getHudWidth() + { + return (int) window.width - (2 * getHudLeft()); + } + + public int getHudHeight() + { + return (int) window.height - (2 * getHudBottom()); + } + + public void uiUnproject(float x, float y, Vector3 v) + { + x = x - window.x; + y = Gdx.graphics.getHeight() - y - 1; + y = y - window.y; + v.x = (2 * x) / window.width - 1; + v.y = (2 * y) / window.height - 1; + v.z = 2 * v.z - 1; + v.prj(uiInvProjMatrix); + } + + public Matrix4 uiCombined() + { + return uiMatrix; + } +} |