summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/tabletop/Camera.java
blob: bcee9e1a4284df02879ba5659e134761a8ded6e4 (plain)
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
package ch.asynk.gdx.tabletop;

import com.badlogic.gdx.graphics.glutils.HdpiUtils;
import com.badlogic.gdx.graphics.OrthographicCamera;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Matrix4;

public class Camera extends OrthographicCamera
{
    private static final float ZEROF = 0.01f;

    private int padding;
    private float boardWidth;
    private float boardHeight;
    private float boardAspectRatio;
    private float zoomMax;
    private float zoomMin;
    private int screenWidth;
    private int screenHeight;
    private float widthFactor;
    private float heightFactor;
    private Rectangle viewport;

    private Rectangle hud;
    private Matrix4 hudMatrix;
    private Matrix4 hudInvProjMatrix;
    private int hudLeft;
    private int hudBottom;
    private boolean hudInBoard;

    public Camera(int padding, float boardWidth, float boardHeight, float zoomMax, float zoomMin, boolean hudInBoard)
    {
        super(boardWidth, boardHeight);
        this.boardWidth = boardWidth;
        this.boardHeight = boardHeight;
        this.padding = padding;
        this.boardAspectRatio = (boardWidth / boardHeight);
        this.zoomMax = zoomMax;
        this.zoomMin = zoomMin;
        this.viewport = new Rectangle();

        this.hudInBoard = hudInBoard;
        this.hud = new Rectangle();
        this.hudMatrix = new Matrix4();
        this.hudInvProjMatrix = new Matrix4();
    }

    public void setDimension(float boardWidth, float boardHeight)
    {
        setToOrtho(false, boardWidth, boardHeight);
        this.boardWidth = boardWidth;
        this.boardHeight = boardHeight;
        this.boardAspectRatio = (boardWidth / boardHeight);
    }

    public void updateViewport(int screenWidth, int screenHeight)
    {
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;

        float screenAvailableWidth = screenWidth - (2 * padding);
        float screenAvailableHeight = screenHeight - (2 * padding);
        float screenAspectRatio = (screenWidth / (float) screenHeight);
        float diff = (boardAspectRatio - screenAspectRatio);

        if (diff <= -ZEROF) {
            // screen wider than board : use max height, width grows up until max available on zooming
            viewport.height = screenAvailableHeight;
            viewport.width = java.lang.Math.min((screenAvailableHeight * boardAspectRatio /  zoom), screenAvailableWidth);
            viewport.x = padding + ((screenAvailableWidth - viewport.width) / 2f);
            viewport.y = padding;
            // camera aspect ratio must follow viewport aspect ration
            viewportHeight = boardHeight;
            viewportWidth = (viewportHeight * (viewport.width / viewport.height));
            // hud
            hud.y = 0;
            hud.x = (hud.y * viewportWidth / viewportHeight);
        } else if (diff > ZEROF) {
            // word is wider than screen : use max width, height grows up until max available on zooming
            viewport.width = screenAvailableWidth;
            viewport.height = java.lang.Math.min((screenAvailableWidth / boardAspectRatio / zoom), screenAvailableHeight);
            viewport.y = padding + ((screenAvailableHeight - viewport.height) / 2f);
            viewport.x = padding;
            // camera aspect ratio must follow viewport aspect ration
            viewportWidth = boardWidth;
            viewportHeight = (viewportWidth * (viewport.height / viewport.width));
            // hud
            hud.x = 0;
            hud.y = (hud.x * viewportHeight / viewportWidth);
        }

        if (hudInBoard) {
            hud.x = 0;
            hud.y = 0;
            hud.width = screenWidth;
            hud.height = screenHeight;
        } else {
            hud.width = (viewport.width - (2 * hud.x));
            hud.height = (viewport.height - (2 * hud.y));
        }

        // ratio viewport -> camera
        widthFactor = (viewportWidth / viewport.width);
        heightFactor = (viewportHeight / viewport.height);

        clampPosition();
        update(true);

        hudMatrix.setToOrtho2D(hud.x, hud.y, hud.width, hud.height);
        hudInvProjMatrix.set(hudMatrix);
        Matrix4.inv(hudInvProjMatrix.val);
    }

    public void applyBoardViewport()
    {
        HdpiUtils.glViewport((int)viewport.x, (int)viewport.y, (int)viewport.width, (int)viewport.height);
    }

    public void applyHudViewport()
    {
        if (hudInBoard)
            HdpiUtils.glViewport(0, 0, screenWidth, screenHeight);
        else
            applyBoardViewport();
    }

    public void centerOnWorld()
    {
        position.set((boardWidth / 2f), (boardHeight / 2f), 0f);
    }

    public void zoom(float dz)
    {
        zoom += dz;
        clampZoom();
    }

    public void clampZoom()
    {
        zoom = MathUtils.clamp(zoom, zoomMin, zoomMax);
    }

    public void translate(float dx, float dy)
    {
        float deltaX = (dx * zoom * widthFactor);
        float deltaY = (dy * zoom * heightFactor);
        translate(deltaX, -deltaY, 0);
        clampPosition();
        update(true);
    }

    public void clampPosition()
    {
        float cameraWidth = (viewportWidth * zoom);
        float cameraHeight = (viewportHeight * zoom);

        // on each axis, clamp on [ cameraDim/2 ; boardDim - cameraDim/2 ]

        if ((boardWidth - cameraWidth) > ZEROF) {
            cameraWidth /= 2f;
            position.x = MathUtils.clamp(position.x, cameraWidth, (boardWidth - cameraWidth));
        } else {
            position.x = (boardWidth / 2f);
        }

        if ((boardHeight - cameraHeight) > ZEROF) {
            cameraHeight /= 2f;
            position.y = MathUtils.clamp(position.y, cameraHeight, (boardHeight - cameraHeight));
        } else {
            position.y = (boardHeight / 2f);
        }
    }

    public void unproject(int x, int y, Vector3 v)
    {
        unproject(v.set(x, y, 0), viewport.x, viewport.y, viewport.width, viewport.height);
    }

    public void unprojectHud(float x, float y, Vector3 v)
    {
        Rectangle r = (hudInBoard ? hud : viewport);
        x = x - r.x;
        y = screenHeight - y - 1;
        y = y - r.y;
        v.x = (2 * x) / r.width - 1;
        v.y = (2 * y) / r.height - 1;
        v.z = 2 * v.z - 1;
        v.prj(hudInvProjMatrix);
    }

    public int getScreenWidth()
    {
        return screenWidth;
    }

    public int getScreenHeight()
    {
        return screenHeight;
    }

    public Matrix4 getHudMatrix()
    {
        return hudMatrix;
    }

    public Rectangle getViewport()
    {
        return viewport;
    }

    public Rectangle getHud()
    {
        return hud;
    }
}