1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.Color; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.OrthographicCamera; 23 import com.badlogic.gdx.graphics.Texture; 24 import com.badlogic.gdx.graphics.g2d.Batch; 25 import com.badlogic.gdx.graphics.g2d.BitmapFont; 26 import com.badlogic.gdx.graphics.g2d.Sprite; 27 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 28 import com.badlogic.gdx.graphics.g2d.TextureAtlas; 29 import com.badlogic.gdx.graphics.g2d.TextureRegion; 30 import com.badlogic.gdx.scenes.scene2d.Actor; 31 import com.badlogic.gdx.scenes.scene2d.InputEvent; 32 import com.badlogic.gdx.scenes.scene2d.InputListener; 33 import com.badlogic.gdx.scenes.scene2d.Stage; 34 import com.badlogic.gdx.tests.utils.GdxTest; 35 36 /** A simple example of how to use a y-down coordinate system. 37 * @author mzechner */ 38 public class YDownTest extends GdxTest { 39 SpriteBatch batch; 40 BitmapFont font; 41 TextureRegion region; 42 Sprite sprite; 43 TextureAtlas atlas; 44 Stage stage; 45 MyActor image; 46 OrthographicCamera camera; 47 48 @Override create()49 public void create () { 50 // a bitmap font to draw some text, note that we 51 // pass true to the constructor, which flips glyphs on y 52 font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), true); 53 54 // a texture region, note the flipping on y again 55 region = new TextureRegion(new Texture("data/badlogic.jpg")); 56 region.flip(false, true); 57 58 // a texture atlas, note the boolean 59 atlas = new TextureAtlas(Gdx.files.internal("data/pack"), true); 60 61 // a sprite, created from a region in the atlas 62 sprite = atlas.createSprite("badlogicsmall"); 63 sprite.setPosition(0, 0); 64 65 // a sprite batch with which we want to render 66 batch = new SpriteBatch(); 67 68 // a camera, note the setToOrtho call, which will set the y-axis 69 // to point downwards 70 camera = new OrthographicCamera(); 71 camera.setToOrtho(true); 72 73 // a stage which uses our y-down camera and a simple actor (see MyActor below), 74 // which uses the flipped region. The key here is to 75 // set our y-down camera on the stage, the rest is just for demo purposes. 76 stage = new Stage(); 77 stage.getViewport().setCamera(camera); 78 image = new MyActor(region); 79 image.setPosition(100, 100); 80 stage.addActor(image); 81 82 // finally we write up the stage as the input process and call it a day. 83 Gdx.input.setInputProcessor(stage); 84 } 85 86 @Override resize(int width, int height)87 public void resize (int width, int height) { 88 // handling resizing is simple, just set the camera to ortho again 89 camera.setToOrtho(true, width, height); 90 } 91 92 @Override render()93 public void render () { 94 // clear the screen, update the camera and make the sprite batch 95 // use its matrices. 96 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 97 camera.update(); 98 batch.setProjectionMatrix(camera.combined); 99 100 // render all the things, we render in a y-down 101 // cartesian coordinate system 102 batch.begin(); 103 // drawing a region, x and y will be the top left corner of the region, would be bottom left 104 // with y-up. 105 batch.draw(region, 20, 100); 106 // drawing text, x and y will be the top left corner for text, same as with y-up 107 font.draw(batch, "This is a test", 270, 100); 108 // drawing regions from an atlas, x and y will be the top left corner. 109 // you shouldn't call findRegion every frame, cache the result. 110 batch.draw(atlas.findRegion("badlogicsmall"), 360, 100); 111 // drawing a sprite created from an atlas, FIXME wut?! AtlasSprite#setPosition seems to be wrong 112 sprite.setColor(Color.RED); 113 sprite.draw(batch); 114 // finally we draw our current touch/mouse coordinates 115 font.draw(batch, Gdx.input.getX() + ", " + Gdx.input.getY(), 0, 0); 116 batch.end(); 117 118 // tell the stage to act and draw itself 119 stage.act(Gdx.graphics.getDeltaTime()); 120 stage.draw(); 121 } 122 123 /** A very simple actor implementation that does not obey rotation/scale/origin set on the actor. Allows dragging of the actor. 124 * @author mzechner */ 125 public class MyActor extends Actor { 126 TextureRegion region; 127 float lastX; 128 float lastY; 129 MyActor(TextureRegion region)130 public MyActor (TextureRegion region) { 131 this.region = region; 132 setWidth(region.getRegionWidth()); 133 setHeight(region.getRegionHeight()); 134 135 addListener(new InputListener() { 136 public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 137 // we only care for the first finger to make things easier 138 if (pointer != 0) return false; 139 140 // record the coordinates the finger went down on. they 141 // are given relative to the actor's upper left corner (0, 0) 142 lastX = x; 143 lastY = y; 144 return true; 145 } 146 147 public void touchDragged (InputEvent event, float x, float y, int pointer) { 148 // we only care for the first finger to make things easier 149 if (pointer != 0) return; 150 151 // adjust the actor's position by (current mouse position - last mouse position) 152 // in the actor's coordinate system. 153 moveBy(x - lastX, y - lastY); 154 155 // save the current mouse position as the basis for the next drag event. 156 // we adjust by the same delta so next time drag is called, lastX/lastY 157 // are in the actor's local coordinate system automatically. 158 lastX = x - (x - lastX); 159 lastY = y - (y - lastY); 160 } 161 }); 162 } 163 164 @Override draw(Batch batch, float parentAlpha)165 public void draw (Batch batch, float parentAlpha) { 166 batch.draw(region, getX(), getY()); 167 } 168 } 169 170 @Override dispose()171 public void dispose () { 172 batch.dispose(); 173 font.dispose(); 174 atlas.dispose(); 175 region.getTexture().dispose(); 176 stage.dispose(); 177 } 178 } 179