Search in sources :

Example 1 with SpriteMetadata

use of com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata in project talos by rockbite.

the class MainRenderer method renderSprite.

private void renderSprite(Batch batch, GameObject gameObject) {
    SpriteRendererComponent spriteRenderer = gameObject.getComponent(SpriteRendererComponent.class);
    SpriteMetadata metadata = SceneEditorAddon.get().workspace.getMetadata(spriteRenderer.path, SpriteMetadata.class);
    vec.set(0, 0);
    transformComponent.localToWorld(gameObject, vec);
    Vector2 renderPosition = vec;
    if (spriteRenderer.getTexture() != null) {
        batch.setColor(spriteRenderer.color);
        if (spriteRenderer.renderMode == SpriteRendererComponent.RenderMode.sliced) {
            // todo: pelase fix me, i am such a shit
            Texture texture = spriteRenderer.getTexture().getTexture();
            // todo: this has to be done better
            NinePatch patch = obtainNinePatch(texture, metadata);
            // todo: and this renders wrong so this needs fixing too
            float xSign = transformComponent.scale.x < 0 ? -1 : 1;
            float ySign = transformComponent.scale.y < 0 ? -1 : 1;
            patch.draw(batch, renderPosition.x - 0.5f * transformComponent.scale.x * xSign, renderPosition.y - 0.5f * transformComponent.scale.y * ySign, 0.5f * transformComponent.scale.x * xSign, 0.5f * transformComponent.scale.y * ySign, Math.abs(transformComponent.scale.x), Math.abs(transformComponent.scale.y), xSign, ySign, transformComponent.rotation);
        } else if (spriteRenderer.renderMode == SpriteRendererComponent.RenderMode.tiled) {
            spriteRenderer.getTexture().getTexture().setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
            float repeatX = transformComponent.scale.x / (spriteRenderer.getTexture().getTexture().getWidth() / metadata.pixelsPerUnit);
            float repeatY = transformComponent.scale.y / (spriteRenderer.getTexture().getTexture().getHeight() / metadata.pixelsPerUnit);
            spriteRenderer.getTexture().setRegion(0, 0, repeatX, repeatY);
            batch.draw(spriteRenderer.getTexture(), renderPosition.x - 0.5f, renderPosition.y - 0.5f, 0.5f, 0.5f, 1f, 1f, transformComponent.scale.x, transformComponent.scale.y, transformComponent.rotation);
        } else if (spriteRenderer.renderMode == SpriteRendererComponent.RenderMode.simple) {
            spriteRenderer.getTexture().getTexture().setWrap(Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge);
            spriteRenderer.getTexture().setRegion(0, 0, spriteRenderer.getTexture().getTexture().getWidth(), spriteRenderer.getTexture().getTexture().getHeight());
            batch.draw(spriteRenderer.getTexture(), renderPosition.x - 0.5f, renderPosition.y - 0.5f, 0.5f, 0.5f, 1f, 1f, transformComponent.scale.x, transformComponent.scale.y, transformComponent.rotation);
        }
        batch.setColor(Color.WHITE);
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) SpriteMetadata(com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) Texture(com.badlogic.gdx.graphics.Texture)

Example 2 with SpriteMetadata

use of com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata in project talos by rockbite.

the class SpriteImporter method importAsset.

@Override
public FileHandle importAsset(FileHandle fileHandle, FileHandle destinationDir) {
    FileHandle importedAsset = importAssetFile(fileHandle, destinationDir);
    if (fileHandle.nameWithoutExtension().endsWith(".9")) {
        // it's a nine slice, and needs metadata created accordingly
        FileHandle metadataHandle = AssetImporter.getMetadataHandleFor(importedAsset);
        metadataHandle = renameAsset(metadataHandle, metadataHandle.nameWithoutExtension().replace(".9", "") + ".meta");
        importedAsset = renameAsset(importedAsset, importedAsset.nameWithoutExtension().replace(".9", "") + ".png");
        SpriteMetadata metadata = AssetImporter.readMetadata(metadataHandle, SpriteMetadata.class);
        Pixmap pixmap = new Pixmap(importedAsset);
        int[] splits = ImportUtils.getSplits(pixmap);
        metadata.borderData = splits;
        AssetImporter.saveMetadata(metadataHandle, metadata);
        Pixmap newPixmap = ImportUtils.cropImage(pixmap, 1, 1, pixmap.getWidth() - 1, pixmap.getHeight() - 1);
        PixmapIO.writePNG(importedAsset, newPixmap);
        pixmap.dispose();
        newPixmap.dispose();
    }
    return importedAsset;
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) SpriteMetadata(com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 3 with SpriteMetadata

use of com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata in project talos by rockbite.

the class SpriteImporter method createMetadataFor.

@Override
public FileHandle createMetadataFor(FileHandle handle) {
    FileHandle metadataHandle = AssetImporter.getMetadataHandleFor(handle);
    SpriteMetadata metadata = new SpriteMetadata();
    AssetImporter.saveMetadata(metadataHandle, metadata);
    return metadataHandle;
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) SpriteMetadata(com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata)

Example 4 with SpriteMetadata

use of com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata 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

SpriteMetadata (com.talosvfx.talos.editor.addons.scene.utils.metadata.SpriteMetadata)4 FileHandle (com.badlogic.gdx.files.FileHandle)2 Texture (com.badlogic.gdx.graphics.Texture)2 Vector2 (com.badlogic.gdx.math.Vector2)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)1 SceneEditorWorkspace (com.talosvfx.talos.editor.addons.scene.SceneEditorWorkspace)1 GameObject (com.talosvfx.talos.editor.addons.scene.logic.GameObject)1 SpriteRendererComponent (com.talosvfx.talos.editor.addons.scene.logic.components.SpriteRendererComponent)1 TransformComponent (com.talosvfx.talos.editor.addons.scene.logic.components.TransformComponent)1