package com.jonathan.survivor.hud; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.utils.Array; import com.jonathan.survivor.Assets; public class TiledImage { /** Stores the images which form the TileImage. */ private Array<Image> images; /** Stores the number of rows of images in the TimeImage. */ private int rows = 1; /** Holds the number of columns in which the images are laid out in the TileImage. */ private int cols = 1; /** Stores the total width of the TiledImage. */ private float width; /** Stores the total height of the TiledImage. */ private float height; /** Creates a TiledImage which will have its images laid out in rows and columns. * * @param regions The TextureRegions which will compose the TiledImage. */ public TiledImage(TextureRegion ...regions) { //Creates a new Array to holds the TiledImage's images. images = new Array<Image>(); //Cycles through the given list of TextureRegions for(int i = 0; i < regions.length; i++) { //Adds the TextureRegion as an image in the TiledImage. Note that all the images will be placed horizontally, right next to each other in the given order. add(regions[i]); } } /** Adds an image to the TiledImage. Note that the image is added in the next column, after the image which was last added to the TiledImage. */ public void add(TextureRegion region) { //Creates a new Image instance from the given TextureRegion. Image image = new Image(region); //Scale down the image if a @2x or @4x atlas was chosen so that the image occupies the same amount of space, no matter the screen size. image.setSize(image.getWidth() / Assets.instance.scaleFactor, image.getHeight() / Assets.instance.scaleFactor); //Add the image's width and height to the total width and height of the TiledImage. width += image.getWidth(); //Adds the image to the list of images in the TiledImage. images.add(image); //Positions the images in the images:Array<Image> next to each other. positionImages(); } /** Skip a row. */ public void row() { } /** Sets the bottom-left position of the TiledImage at the given (x,y) coordinates. */ public void setPosition(float x, float y) { //Places the bottom-left position of the first image to the given (x,y) position. The rest of the images will update according to the amount of rows and columns in the TiledImage. images.get(0).setPosition(x, y); //Re-positions the images so that the images positionImages(); } /** Re-positions the images so that they are in a grid-like fashion, with the same amount of rows and columns specified in the member variables. */ private void positionImages() { //Cycles through all the images belonging to the TiledImage. for(int i = 0; i < images.size; i++) { //Gets the image that is currently being cycled through. Image image = images.get(i); //If there are images placed before the one we are cycling through if(i > 0) { //Retrieve the image that was added to the TileImage right before the one that was just created. Image previousImage = images.get(i-1); //Place the created image right next to the previous image. image.setX(previousImage.getX() + previousImage.getWidth()); //Places the image at the same y-position as the previous image. image.setY(previousImage.getY()); } } } /** Adds the TiledImage to the stage, so that it can be rendered. */ public void addToStage(Stage stage) { //Cycles through each image in the TiledImage. for(int i = 0; i < images.size; i++) { //Adds all the images stored in the TiledImage to the stage. stage.addActor(images.get(i)); } } /** Retrieves the total width of the TiledImage. */ public float getWidth() { return width; } /** Sets the total width of the TiledImage. */ public void setWidth(float width) { //TODO } /** Retrieves the total height of the TiledImage. */ public float getHeight() { //If there is only one row in the TiledImage if(rows == 1) //The height of the image is the height of the image in the first column of the first row. return images.get(1).getHeight(); throw new RuntimeException("TiledImage.getHeight(): Must implement the getHeight() method when there is more than one row in the TiledImage"); } /** Sets the total height of the TiledImage. */ public void setHeight(float height) { //TODO } }