Search in sources :

Example 1 with AtlasSprite

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite in project libgdx by libgdx.

the class SpriteDrawable method tint.

/** Creates a new drawable that renders the same as this drawable tinted the specified color. */
public SpriteDrawable tint(Color tint) {
    Sprite newSprite;
    if (sprite instanceof AtlasSprite)
        newSprite = new AtlasSprite((AtlasSprite) sprite);
    else
        newSprite = new Sprite(sprite);
    newSprite.setColor(tint);
    newSprite.setSize(getMinWidth(), getMinHeight());
    SpriteDrawable drawable = new SpriteDrawable(newSprite);
    drawable.setLeftWidth(getLeftWidth());
    drawable.setRightWidth(getRightWidth());
    drawable.setTopHeight(getTopHeight());
    drawable.setBottomHeight(getBottomHeight());
    return drawable;
}
Also used : AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)

Example 2 with AtlasSprite

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite in project libgdx by libgdx.

the class TextureRegionDrawable method tint.

/** Creates a new drawable that renders the same as this drawable tinted the specified color. */
public Drawable tint(Color tint) {
    Sprite sprite;
    if (region instanceof AtlasRegion)
        sprite = new AtlasSprite((AtlasRegion) region);
    else
        sprite = new Sprite(region);
    sprite.setColor(tint);
    sprite.setSize(getMinWidth(), getMinHeight());
    SpriteDrawable drawable = new SpriteDrawable(sprite);
    drawable.setLeftWidth(getLeftWidth());
    drawable.setRightWidth(getRightWidth());
    drawable.setTopHeight(getTopHeight());
    drawable.setBottomHeight(getBottomHeight());
    return drawable;
}
Also used : AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 3 with AtlasSprite

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite in project libgdx by libgdx.

the class Skin method getSprite.

/** Returns a registered sprite. If no sprite is found but a region exists with the name, a sprite is created from the region
	 * and stored in the skin. If the region is an {@link AtlasRegion} then an {@link AtlasSprite} is used if the region has been
	 * whitespace stripped or packed rotated 90 degrees. */
public Sprite getSprite(String name) {
    Sprite sprite = optional(name, Sprite.class);
    if (sprite != null)
        return sprite;
    try {
        TextureRegion textureRegion = getRegion(name);
        if (textureRegion instanceof AtlasRegion) {
            AtlasRegion region = (AtlasRegion) textureRegion;
            if (region.rotate || region.packedWidth != region.originalWidth || region.packedHeight != region.originalHeight)
                sprite = new AtlasSprite(region);
        }
        if (sprite == null)
            sprite = new Sprite(textureRegion);
        add(name, sprite, Sprite.class);
        return sprite;
    } catch (GdxRuntimeException ex) {
        throw new GdxRuntimeException("No NinePatch, TextureRegion, or Texture registered with name: " + name);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 4 with AtlasSprite

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite in project gdx-skineditor by cobolfoo.

the class Skin method newDrawable.

/**
 * Returns a tinted copy of a drawable found in the skin via
 * {@link #getDrawable(String)}.
 */
public Drawable newDrawable(Drawable drawable, Color tint) {
    if (drawable instanceof TextureRegionDrawable) {
        TextureRegion region = ((TextureRegionDrawable) drawable).getRegion();
        Sprite sprite;
        if (region instanceof AtlasRegion)
            sprite = new AtlasSprite((AtlasRegion) region);
        else
            sprite = new Sprite(region);
        sprite.setColor(tint);
        return new SpriteDrawable(sprite);
    }
    if (drawable instanceof NinePatchDrawable) {
        NinePatchDrawable patchDrawable = new NinePatchDrawable((NinePatchDrawable) drawable);
        patchDrawable.setPatch(new NinePatch(patchDrawable.getPatch(), tint));
        return patchDrawable;
    }
    if (drawable instanceof SpriteDrawable) {
        SpriteDrawable spriteDrawable = new SpriteDrawable((SpriteDrawable) drawable);
        Sprite sprite = spriteDrawable.getSprite();
        if (sprite instanceof AtlasSprite)
            sprite = new AtlasSprite((AtlasSprite) sprite);
        else
            sprite = new Sprite(sprite);
        sprite.setColor(tint);
        spriteDrawable.setSprite(sprite);
        return spriteDrawable;
    }
    throw new GdxRuntimeException("Unable to copy, unknown drawable type: " + drawable.getClass());
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)

Example 5 with AtlasSprite

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite in project commons-gdx by gemserk.

the class AnimationFromTextureAtlasResourceBuilder method build.

@Override
public Animation build() {
    TextureAtlas textureAtlas = resourceManager.getResourceValue(textureAtlasId);
    if (sprites == null) {
        try {
            sprites = textureAtlas.createSprites(prefix);
        } catch (GdxRuntimeException e) {
            throw new RuntimeException("Failed to create animation from texture atlas " + textureAtlasId, e);
        }
    }
    if (endFrame == -1)
        endFrame = sprites.size - 1;
    if (startFrame == -1)
        startFrame = 0;
    Sprite[] frames = new Sprite[endFrame - startFrame + 1];
    int frameNumber = startFrame;
    for (int i = 0; i < frames.length; i++) {
        Sprite sprite = sprites.get(frameNumber);
        if (sprite instanceof AtlasSprite)
            frames[i] = new AtlasSprite(((AtlasSprite) sprite).getAtlasRegion());
        else
            frames[i] = new Sprite(sprite);
        frameTransformation.transform(frames[i]);
        frameNumber++;
    }
    int framesCount = frames.length;
    float[] newTimes = new float[framesCount - 1];
    int lastTime = time;
    for (int i = 0; i < framesCount - 1; i++) {
        if (i < times.length) {
            newTimes[i] = ((float) times[i]) * 0.001f;
            lastTime = times[i];
        } else
            newTimes[i] = ((float) lastTime) * 0.001f;
    }
    FrameAnimationImpl frameAnimation = new FrameAnimationImpl(0.001f * (float) time, newTimes);
    frameAnimation.setLoop(loop);
    return new Animation(frames, frameAnimation);
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FrameAnimationImpl(com.gemserk.animation4j.FrameAnimationImpl) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Animation(com.gemserk.animation4j.gdx.Animation)

Aggregations

AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)13 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)12 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)7 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)5 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)4 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)3 Test (org.junit.Test)3 FrameAnimationImpl (com.gemserk.animation4j.FrameAnimationImpl)2 Animation (com.gemserk.animation4j.gdx.Animation)2 Texture (com.badlogic.gdx.graphics.Texture)1 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)1 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)1 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)1 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)1 Array (com.badlogic.gdx.utils.Array)1 FloatValue (com.gemserk.commons.values.FloatValue)1 ArrayList (java.util.ArrayList)1