+++ /dev/null
-/*******************************************************************************\r
- * Copyright 2011 See AUTHORS file.\r
- * \r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- ******************************************************************************/\r
-\r
-package com.badlogic.gdx.tests;\r
-\r
-import com.badlogic.gdx.Gdx;\r
-import com.badlogic.gdx.InputAdapter;\r
-import com.badlogic.gdx.graphics.Camera;\r
-import com.badlogic.gdx.graphics.GL10;\r
-import com.badlogic.gdx.graphics.OrthographicCamera;\r
-import com.badlogic.gdx.graphics.Texture;\r
-import com.badlogic.gdx.graphics.g2d.Sprite;\r
-import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
-import com.badlogic.gdx.math.Intersector;\r
-import com.badlogic.gdx.math.Matrix4;\r
-import com.badlogic.gdx.math.Plane;\r
-import com.badlogic.gdx.math.Vector3;\r
-import com.badlogic.gdx.math.collision.Ray;\r
-import com.badlogic.gdx.tests.utils.GdxTest;\r
-\r
-public class IsoCamTest extends GdxTest {\r
- private static final int TARGET_WIDTH = 480;\r
- private static final float UNIT_TO_PIXEL = TARGET_WIDTH * 0.15f;\r
- Texture texture;\r
- OrthographicCamera cam;\r
- SpriteBatch batch;\r
- final Sprite[][] sprites = new Sprite[10][10];\r
- final Matrix4 matrix = new Matrix4();\r
-\r
- @Override\r
- public void create () {\r
- texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));\r
- float unitsOnX = (float)Math.sqrt(2) * TARGET_WIDTH / (UNIT_TO_PIXEL);\r
- float pixelsOnX = Gdx.graphics.getWidth() / unitsOnX;\r
- float unitsOnY = Gdx.graphics.getHeight() / pixelsOnX;\r
- cam = new OrthographicCamera(unitsOnX, unitsOnY, 25);\r
- cam.position.scl(30);\r
- cam.near = 1;\r
- cam.far = 1000;\r
- matrix.setToRotation(new Vector3(1, 0, 0), 90);\r
-\r
- for (int z = 0; z < 10; z++) {\r
- for (int x = 0; x < 10; x++) {\r
- sprites[x][z] = new Sprite(texture);\r
- sprites[x][z].setPosition(x, z);\r
- sprites[x][z].setSize(1, 1);\r
- }\r
- }\r
-\r
- batch = new SpriteBatch();\r
-\r
- Gdx.input.setInputProcessor(new IsoCamController(cam));\r
- }\r
-\r
- @Override\r
- public void dispose () {\r
- texture.dispose();\r
- batch.dispose();\r
- }\r
-\r
- @Override\r
- public void render () {\r
- Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
- cam.update();\r
-\r
- batch.setProjectionMatrix(cam.combined);\r
- batch.setTransformMatrix(matrix);\r
- batch.begin();\r
- for (int z = 0; z < 10; z++) {\r
- for (int x = 0; x < 10; x++) {\r
- sprites[x][z].draw(batch);\r
- }\r
- }\r
- batch.end();\r
-\r
- checkTileTouched();\r
- }\r
-\r
- final Plane xzPlane = new Plane(new Vector3(0, 1, 0), 0);\r
- final Vector3 intersection = new Vector3();\r
- Sprite lastSelectedTile = null;\r
-\r
- private void checkTileTouched () {\r
- if (Gdx.input.justTouched()) {\r
- Ray pickRay = cam.getPickRay(Gdx.input.getX(), Gdx.input.getY());\r
- Intersector.intersectRayPlane(pickRay, xzPlane, intersection);\r
- System.out.println(intersection);\r
- int x = (int)intersection.x;\r
- int z = (int)intersection.z;\r
- if (x >= 0 && x < 10 && z >= 0 && z < 10) {\r
- if (lastSelectedTile != null) lastSelectedTile.setColor(1, 1, 1, 1);\r
- Sprite sprite = sprites[x][z];\r
- sprite.setColor(1, 0, 0, 1);\r
- lastSelectedTile = sprite;\r
- }\r
- }\r
- }\r
-\r
- public class IsoCamController extends InputAdapter {\r
- final Plane xzPlane = new Plane(new Vector3(0, 1, 0), 0);\r
- final Vector3 intersection = new Vector3();\r
- final Vector3 curr = new Vector3();\r
- final Vector3 last = new Vector3(-1, -1, -1);\r
- final Vector3 delta = new Vector3();\r
- final Camera camera;\r
-\r
- public IsoCamController (Camera camera) {\r
- this.camera = camera;\r
- }\r
-\r
- @Override\r
- public boolean touchDragged (int x, int y, int pointer) {\r
- Ray pickRay = camera.getPickRay(x, y);\r
- Intersector.intersectRayPlane(pickRay, xzPlane, curr);\r
-\r
- if (!(last.x == -1 && last.y == -1 && last.z == -1)) {\r
- pickRay = camera.getPickRay(last.x, last.y);\r
- Intersector.intersectRayPlane(pickRay, xzPlane, delta);\r
- delta.sub(curr);\r
- camera.position.add(delta.x, 0, delta.z);\r
- }\r
- last.set(x, y, 0);\r
- return false;\r
- }\r
-\r
- @Override\r
- public boolean touchUp (int x, int y, int pointer, int button) {\r
- last.set(-1, -1, -1);\r
- return false;\r
- }\r
- }\r
-\r
- @Override\r
- public boolean needsGL20 () {\r
- return false;\r
- }\r
-\r
-}\r