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

com.uwsoft.editor.renderer.actor.ImageItem Maven / Gradle / Ivy

Go to download

overlap2d-runtime-libgdx provides functionality to load, manipulate and render scenes generated by Overlap2D.

There is a newer version: 0.1.1
Show newest version
package com.uwsoft.editor.renderer.actor;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.uwsoft.editor.renderer.data.Essentials;
import com.uwsoft.editor.renderer.data.SimpleImageVO;
import com.uwsoft.editor.renderer.physics.PhysicsBodyLoader;
import com.uwsoft.editor.renderer.resources.IResourceRetriever;
import com.uwsoft.editor.renderer.utils.CustomVariables;

public class ImageItem extends Image implements IBaseItem {

    public SimpleImageVO dataVO;
    public Essentials essentials;
    public float mulX = 1f;
    public float mulY = 1f;
    protected int layerIndex = 0;
    private CustomVariables customVariables = new CustomVariables();
    private boolean isLockedByLayer = false;
    private CompositeItem parentItem = null;

    private Body body;

    public ImageItem(SimpleImageVO vo, Essentials e, CompositeItem parent) {
        this(vo, e);
        setParentItem(parent);
    }

    public ImageItem(SimpleImageVO vo, Essentials e) {
        super(e.rm.getTextureRegion(vo.imageName));
        init(vo, e);
    }

    protected ImageItem(SimpleImageVO vo, Essentials e, NinePatch ninePatch) {
        super(ninePatch);
        init(vo, e);
    }

    private void init(SimpleImageVO vo, Essentials e) {
        dataVO = vo;
        this.essentials = e;
        setX(dataVO.x);
        setY(dataVO.y);
        setScaleX(dataVO.scaleX);
        setScaleY(dataVO.scaleY);
        customVariables.loadFromString(dataVO.customVars);
        this.setRotation(dataVO.rotation);
        if (dataVO.zIndex < 0) dataVO.zIndex = 0;
        if (dataVO.tint == null) {
            setTint(new Color(1, 1, 1, 1));
        } else {
            setTint(new Color(dataVO.tint[0], dataVO.tint[1], dataVO.tint[2], dataVO.tint[3]));
        }

    }

    public void setTint(Color tint) {
        float[] clr = new float[4];
        clr[0] = tint.r;
        clr[1] = tint.g;
        clr[2] = tint.b;
        clr[3] = tint.a;
        this.getDataVO().tint = clr;
        this.setColor(tint);
    }

    public SimpleImageVO getDataVO() {
        //updateDataVO();
        return dataVO;
    }


    public void renew() {
        setX(dataVO.x * this.mulX);
        setY(dataVO.y * this.mulY);
        setScaleX(dataVO.scaleX);
        setScaleY(dataVO.scaleY);
        setRotation(dataVO.rotation);
        setColor(dataVO.tint[0], dataVO.tint[1], dataVO.tint[2], dataVO.tint[3]);
        customVariables.loadFromString(dataVO.customVars);
    }

    @Override
    public boolean isLockedByLayer() {
        return isLockedByLayer;
    }

    @Override
    public void setLockByLayer(boolean isLocked) {
        isLockedByLayer = isLocked;
    }

    @Override
    public boolean isComposite() {
        return false;
    }

    public void updateDataVO() {
        dataVO.x = getX() / this.mulX;
        dataVO.y = getY() / this.mulY;
        dataVO.scaleX = getScaleX();
        dataVO.scaleY = getScaleY();
        dataVO.rotation = getRotation();

        if (getZIndex() >= 0) {
            dataVO.zIndex = getZIndex();
        }

        if (dataVO.layerName == null || dataVO.layerName.equals("")) {
            dataVO.layerName = "Default";
        }
        dataVO.customVars = customVariables.saveAsString();
    }

    public void applyResolution(float mulX, float mulY) {
        this.mulX = mulX;
        this.mulY = mulY;
        setX(dataVO.x * this.mulX);
        setY(dataVO.y * this.mulY);
        setScaleX(dataVO.scaleX );
        setScaleY(dataVO.scaleY);
        updateDataVO();
    }

    @Override
    public int getLayerIndex() {
        return layerIndex;
    }

    @Override
    public void setLayerIndex(int index) {
        layerIndex = index;
    }

    public CompositeItem getParentItem() {
        return parentItem;
    }

    public void setParentItem(CompositeItem parentItem) {
        this.parentItem = parentItem;
    }

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }

    public void dispose() {
        if(essentials.world != null && getBody() != null)essentials.world.destroyBody(getBody());
        setBody(null);
    }

    public CustomVariables getCustomVariables() {
        return customVariables;
    }


    @Override
    public void act(float delta) {
        // physics is enabled for this body and it is not static body
        if(essentials.world != null && body != null && dataVO.physicsBodyData != null && dataVO.physicsBodyData.bodyType > 0 && !essentials.physicsStopped) {
            setX(body.getPosition().x/ PhysicsBodyLoader.SCALE);
            setY(body.getPosition().y/PhysicsBodyLoader.SCALE);
            setRotation(body.getAngle() * MathUtils.radiansToDegrees);
        }

        super.act(delta);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy