Search in sources :

Example 1 with SpriteRendererComponent

use of com.talosvfx.talos.editor.addons.scene.logic.components.SpriteRendererComponent in project talos by rockbite.

the class SceneEditorWorkspace method createSpriteObject.

public GameObject createSpriteObject(FileHandle importedAsset, Vector2 sceneCords, GameObject parent) {
    GameObject spriteObject = createObjectByTypeName("sprite", sceneCords, parent);
    SpriteRendererComponent component = spriteObject.getComponent(SpriteRendererComponent.class);
    component.path = AssetImporter.relative(importedAsset);
    component.reloadTexture();
    TextureRegion texture = component.texture;
    float aspect = (float) texture.getRegionWidth() / texture.getRegionHeight();
    TransformComponent transformComponent = spriteObject.getComponent(TransformComponent.class);
    transformComponent.scale.x *= aspect;
    return spriteObject;
}
Also used : SpriteRendererComponent(com.talosvfx.talos.editor.addons.scene.logic.components.SpriteRendererComponent) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) TransformComponent(com.talosvfx.talos.editor.addons.scene.logic.components.TransformComponent)

Example 2 with SpriteRendererComponent

use of com.talosvfx.talos.editor.addons.scene.logic.components.SpriteRendererComponent in project talos by rockbite.

the class SmartTransformGizmo method moveInLayerOrder.

protected void moveInLayerOrder(GameObject gameObject, int direction) {
    // direction -1 for down, 1 for up
    if (gameObject.hasComponent(SpriteRendererComponent.class)) {
        SpriteRendererComponent component = gameObject.getComponent(SpriteRendererComponent.class);
        String sortingLayer = component.sortingLayer;
        Array<GameObject> list = SceneEditorAddon.get().workspace.getRootGO().getChildrenByComponent(SpriteRendererComponent.class, new Array<>());
        for (int i = list.size - 1; i >= 0; i--) {
            if (!list.get(i).getComponent(SpriteRendererComponent.class).sortingLayer.equals(sortingLayer)) {
                list.removeIndex(i);
            }
        }
        // find closest game object if any that is lower to my index, and swap with it
        if (list.size > 1) {
            GameObject closest = list.first();
            if (list.first() == gameObject) {
                closest = list.get(1);
            }
            int origDst = component.orderingInLayer - closest.getComponent(SpriteRendererComponent.class).orderingInLayer;
            for (GameObject candidate : list) {
                int dst = component.orderingInLayer - candidate.getComponent(SpriteRendererComponent.class).orderingInLayer;
                boolean matchesDirection = false;
                boolean matchesDirectionEquals = false;
                if (direction == -1 && dst >= 0)
                    matchesDirectionEquals = true;
                if (direction == 1 && dst <= 0)
                    matchesDirectionEquals = true;
                if (direction == -1 && origDst > dst)
                    matchesDirection = true;
                if (direction == 1 && origDst < dst)
                    matchesDirection = true;
                if (matchesDirectionEquals && candidate != gameObject) {
                    if (matchesDirection) {
                        origDst = dst;
                        closest = candidate;
                    }
                }
            }
            SpriteRendererComponent closestComponent = closest.getComponent(SpriteRendererComponent.class);
            if (closestComponent.orderingInLayer > component.orderingInLayer && direction == -1) {
                return;
            }
            if (closestComponent.orderingInLayer < component.orderingInLayer && direction == 1) {
                return;
            }
            if (closestComponent.orderingInLayer == component.orderingInLayer) {
                if (direction == -1) {
                    closestComponent.orderingInLayer++;
                    Notifications.fireEvent(Notifications.obtainEvent(ComponentUpdated.class).set(closestComponent, false));
                } else if (direction == 1) {
                    component.orderingInLayer++;
                    Notifications.fireEvent(Notifications.obtainEvent(ComponentUpdated.class).set(component, false));
                }
            } else {
                // swap
                int tmp = component.orderingInLayer;
                component.orderingInLayer = closestComponent.orderingInLayer;
                closestComponent.orderingInLayer = tmp;
                Notifications.fireEvent(Notifications.obtainEvent(ComponentUpdated.class).set(component, false));
                Notifications.fireEvent(Notifications.obtainEvent(ComponentUpdated.class).set(closestComponent, false));
            }
        }
    }
}
Also used : SpriteRendererComponent(com.talosvfx.talos.editor.addons.scene.logic.components.SpriteRendererComponent) GameObject(com.talosvfx.talos.editor.addons.scene.logic.GameObject) ComponentUpdated(com.talosvfx.talos.editor.addons.scene.events.ComponentUpdated)

Example 3 with SpriteRendererComponent

use of com.talosvfx.talos.editor.addons.scene.logic.components.SpriteRendererComponent in project talos by rockbite.

the class SpriteImporter method makeInstance.

@Override
public void makeInstance(FileHandle asset, GameObject parent) {
    if (!AssetImporter.getMetadataHandleFor(asset).exists()) {
        createMetadataFor(asset);
    }
    SpriteMetadata metadata = AssetImporter.readMetadataFor(asset, SpriteMetadata.class);
    SceneEditorWorkspace workspace = SceneEditorAddon.get().workspace;
    Vector2 sceneCords = workspace.getMouseCordsOnScene();
    GameObject gameObject = workspace.createSpriteObject(asset, sceneCords, parent);
    if (metadata.borderData != null) {
        SpriteRendererComponent component = gameObject.getComponent(SpriteRendererComponent.class);
        component.renderMode = SpriteRendererComponent.RenderMode.sliced;
    } else {
        if (gameObject.hasComponent(TransformComponent.class)) {
            TransformComponent component = gameObject.getComponent(TransformComponent.class);
            Texture texture = new Texture(asset);
            component.scale.x = texture.getWidth() / metadata.pixelsPerUnit;
            component.scale.y = texture.getHeight() / metadata.pixelsPerUnit;
        }
    }
}
Also used : SpriteRendererComponent(com.talosvfx.talos.editor.addons.scene.logic.components.SpriteRendererComponent) SceneEditorWorkspace(com.talosvfx.talos.editor.addons.scene.SceneEditorWorkspace) Vector2(com.badlogic.gdx.math.Vector2) GameObject(com.talosvfx.talos.editor.addons.scene.logic.GameObject) SpriteMetadata(com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata) TransformComponent(com.talosvfx.talos.editor.addons.scene.logic.components.TransformComponent) Texture(com.badlogic.gdx.graphics.Texture)

Aggregations

SpriteRendererComponent (com.talosvfx.talos.editor.addons.scene.logic.components.SpriteRendererComponent)3 GameObject (com.talosvfx.talos.editor.addons.scene.logic.GameObject)2 TransformComponent (com.talosvfx.talos.editor.addons.scene.logic.components.TransformComponent)2 Texture (com.badlogic.gdx.graphics.Texture)1 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)1 Vector2 (com.badlogic.gdx.math.Vector2)1 SceneEditorWorkspace (com.talosvfx.talos.editor.addons.scene.SceneEditorWorkspace)1 ComponentUpdated (com.talosvfx.talos.editor.addons.scene.events.ComponentUpdated)1 SpriteMetadata (com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata)1