All Downloads are FREE. Search and download functionalities are using the official Maven repository.

games.rednblack.editor.renderer.components.TextureRegionComponent Maven / Gradle / Ivy

The newest version!
package games.rednblack.editor.renderer.components;

import com.artemis.ComponentMapper;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import games.rednblack.editor.renderer.commons.RefreshableComponent;
import games.rednblack.editor.renderer.components.shape.PolygonShapeComponent;
import games.rednblack.editor.renderer.utils.ABAtlasRegion;
import games.rednblack.editor.renderer.utils.ABRepeatablePolygonSprite;
import games.rednblack.editor.renderer.utils.RepeatablePolygonSprite;

public class TextureRegionComponent extends RefreshableComponent {

    protected transient ComponentMapper dimensionsCM;
    protected transient ComponentMapper polygonCM;

    protected boolean needsRefresh = false;

    public String regionName = "";
    public transient TextureRegion region = null;
    public boolean isRepeat = false;
    public boolean isPolygon = false;

    public float ppwu = 1;

    public transient TextureRegion screenRegion = null;

    // optional
    public transient RepeatablePolygonSprite repeatablePolygonSprite = null;

    public void setPolygonSprite(PolygonShapeComponent polygonShapeComponent) {
        Array verticesArray = polygonShapeComponent.vertices;
        if (verticesArray == null) return;
        //TODO Another buddy that should be pooled
        float[] vertices = new float[verticesArray.size * 2];
        for (int i = 0; i < verticesArray.size; i++) {
            vertices[i * 2] = verticesArray.get(i).x;
            vertices[i * 2 + 1] = verticesArray.get(i).y;
        }

        if (repeatablePolygonSprite == null) {
            if (region instanceof ABAtlasRegion) {
                RepeatablePolygonSprite normalSprite = new RepeatablePolygonSprite();
                ABAtlasRegion normalRegion = (ABAtlasRegion) region;
                normalSprite.setTextureRegion(normalRegion.getRegionB());
                repeatablePolygonSprite = new ABRepeatablePolygonSprite(normalSprite, normalRegion.getUseRegionB());
            } else {
                repeatablePolygonSprite = new RepeatablePolygonSprite();
            }
        }
        repeatablePolygonSprite.clear();
        repeatablePolygonSprite.setWorldMultiplier(1f / ppwu);
        repeatablePolygonSprite.setVertices(vertices);
        repeatablePolygonSprite.setTextureRegion(region);
    }

    @Override
    public void reset() {
        regionName = "";
        region = null;
        repeatablePolygonSprite = null;
        isRepeat = false;
        isPolygon = false;
        needsRefresh = false;

        screenRegion = null;
    }

    @Override
    public void scheduleRefresh() {
        needsRefresh = true;
    }

    @Override
    public void executeRefresh(int entity) {
        if (needsRefresh) {
            refresh(entity);
            needsRefresh = false;
        }
    }

    protected void refresh(int entity) {
        PolygonShapeComponent polygonShapeComponent = polygonCM.get(entity);

        if (isPolygon && polygonShapeComponent != null && polygonShapeComponent.vertices != null) {
            DimensionsComponent dimensionsComponent = dimensionsCM.get(entity);
            dimensionsComponent.setPolygon(polygonShapeComponent);
            setPolygonSprite(polygonShapeComponent);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy