Package com.aqpproject.visualisation.gdx

Source Code of com.aqpproject.visualisation.gdx.SpriteGDX

/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.visualisation.gdx;

import com.aqpproject.tools.Vector2D;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.awt.Rectangle;
import java.io.File;

/**
* SpriteGDX class
*
* @author Alexandre, Clement, Fabrice
*/
public class SpriteGDX {

    /**
     * Constructor
     *
     * @param path path to the sprite
     * @param frame frame of the sprite
     */
    public SpriteGDX(String path, Rectangle rectangle, int frame) {
        m_path = new File(path).getAbsolutePath();
        FileHandle f = Gdx.files.absolute(m_path);
        m_texture = new Texture(f);
        m_region = new TextureRegion(m_texture, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
        m_rectangle = rectangle;
        m_frame = frame;
    }

    public void setFrame(int frame) {
        int current = Math.max(1, Math.min(frame, m_frame)) - 1;
        int dx = current * m_rectangle.width;
        m_region = new TextureRegion(m_texture, m_rectangle.x + dx, m_rectangle.y, m_rectangle.width, m_rectangle.height);
    }

    /**
     * @return the region of the sprite
     */
    public TextureRegion getRegion() {
        return m_region;
    }

    public float getDefaultOriginX() {
        return m_region.getRegionX();
    }

    public float getDefaultOriginY() {
        return m_region.getRegionY();
    }

    public float getRealOriginX() {
        return (m_region.getRegionWidth() - m_region.getRegionX()) / 2.f;
    }

    public float getRealOriginY() {
        return (m_region.getRegionHeight() - m_region.getRegionY()) / 2.f;
    }

    /**
     * @return the center of the sprite
     */
    public Vector2D getDefaultCenter() {
        return new Vector2D(getDefaultOriginX(), getDefaultOriginY());
    }

    public Vector2D getRealCenter() {
        return new Vector2D(getRealOriginX(), getRealOriginY());
    }

    public int getWidth() {
        return m_rectangle.width;
    }

    public int getHeight() {
        return m_rectangle.height;
    }
    /////////////////////////////////////////
    // Attributes
    /////////////////////////////////////////
    private String m_path;
    private Texture m_texture;
    private TextureRegion m_region;
    private Rectangle m_rectangle;
    private int m_frame;
}
TOP

Related Classes of com.aqpproject.visualisation.gdx.SpriteGDX

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.