Search in sources :

Example 21 with Sprite

use of com.badlogic.gdx.graphics.g2d.Sprite 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 22 with Sprite

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

the class Skin method getDrawable.

/**
	 * Returns a registered drawable. If no drawable is found but a region,
	 * ninepatch, or sprite exists with the name, then the appropriate drawable
	 * is created and stored in the skin.
	 */
public Drawable getDrawable(String name) {
    Drawable drawable = optional(name, Drawable.class);
    if (drawable != null)
        return drawable;
    drawable = optional(name, TiledDrawable.class);
    if (drawable != null)
        return drawable;
    // has rotation or whitespace stripping, use sprite.
    try {
        TextureRegion textureRegion = getRegion(name);
        if (textureRegion instanceof AtlasRegion) {
            AtlasRegion region = (AtlasRegion) textureRegion;
            if (region.splits != null)
                drawable = new NinePatchDrawable(getPatch(name));
            else if (region.rotate || region.packedWidth != region.originalWidth || region.packedHeight != region.originalHeight)
                drawable = new SpriteDrawable(getSprite(name));
        }
        if (drawable == null)
            drawable = new TextureRegionDrawable(textureRegion);
    } catch (GdxRuntimeException ignored) {
    }
    // drawable.
    if (drawable == null) {
        NinePatch patch = optional(name, NinePatch.class);
        if (patch != null)
            drawable = new NinePatchDrawable(patch);
        else {
            Sprite sprite = optional(name, Sprite.class);
            if (sprite != null)
                drawable = new SpriteDrawable(sprite);
            else
                throw new GdxRuntimeException("No Drawable, NinePatch, TextureRegion, Texture, or Sprite registered with name: " + name);
        }
    }
    add(name, drawable, Drawable.class);
    return drawable;
}
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) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) 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 23 with Sprite

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

the class RenderLayerSpriteBatchImpl method render.

@Override
public void render() {
    camera.getFrustum(frustum);
    if (optimizationParameters.updateCamera)
        camera.apply(spriteBatch);
    RandomAccessMap<Entity, EntityComponents> entityComponents = factory.entityComponents;
    if (blending && !spriteBatch.isBlendingEnabled())
        spriteBatch.enableBlending();
    else if (!blending && spriteBatch.isBlendingEnabled())
        spriteBatch.disableBlending();
    if (optimizationParameters.beginBatch)
        spriteBatch.begin();
    for (int i = 0; i < orderedByLayerRenderables.size(); i++) {
        Renderable renderable = orderedByLayerRenderables.get(i);
        if (!renderable.isVisible())
            continue;
        EntityComponents components = entityComponents.get(renderable.getEntity());
        FrustumCullingComponent frustumCullingComponent = components.frustumCullingComponent;
        if (frustumCullingComponent != null) {
            Spatial spatial = components.spatialComponent.getSpatial();
            entityBounds.set(frustumCullingComponent.bounds);
            entityBounds.setX(entityBounds.getX() + spatial.getX());
            entityBounds.setY(entityBounds.getY() + spatial.getY());
            if (!frustum.overlaps(entityBounds))
                continue;
        }
        SpriteComponent spriteComponent = components.spriteComponent;
        if (spriteComponent != null) {
            Sprite sprite = spriteComponent.getSprite();
            sprite.setColor(spriteComponent.getColor());
            sprite.draw(spriteBatch);
        }
        // don't like it will be asking for components all the time.
        TextComponent textComponent = components.textComponent;
        if (textComponent != null) {
            BitmapFont font = textComponent.font;
            if (font.getScaleX() != textComponent.scale) {
                font.setScale(textComponent.scale);
            }
            font.setColor(textComponent.color);
            //
            SpriteBatchUtils.drawMultilineText(//
            spriteBatch, //
            font, textComponent.text, textComponent.x, textComponent.y, textComponent.cx, textComponent.cy);
        }
        ParticleEmitterComponent particleEmitterComponent = components.particleEmitterComponent;
        if (particleEmitterComponent != null) {
            particleEmitterComponent.particleEmitter.draw(spriteBatch);
        }
    }
    if (optimizationParameters.endBatch)
        spriteBatch.end();
}
Also used : TextComponent(com.gemserk.commons.artemis.components.TextComponent) Entity(com.artemis.Entity) Renderable(com.gemserk.commons.artemis.render.Renderable) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) Spatial(com.gemserk.commons.gdx.games.Spatial) SpriteComponent(com.gemserk.commons.artemis.components.SpriteComponent) FrustumCullingComponent(com.gemserk.commons.artemis.components.FrustumCullingComponent) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) ParticleEmitterComponent(com.gemserk.commons.artemis.components.ParticleEmitterComponent)

Example 24 with Sprite

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

the class AnimationUtils method scaleAnimation.

public static void scaleAnimation(Animation animation, float scale) {
    for (int i = 0; i < animation.getFramesCount(); i++) {
        Sprite frame = animation.getFrame(i);
        float width = frame.getWidth();
        float height = frame.getHeight();
        frame.setSize(width * scale, height * scale);
    }
}
Also used : Sprite(com.badlogic.gdx.graphics.g2d.Sprite)

Example 25 with Sprite

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

the class SpriteUtils method cloneSprite.

/**
	 * Returns a clone of a Sprite based on the type of the sprite.
	 */
public static Sprite cloneSprite(Sprite sprite) {
    if (sprite instanceof AtlasSprite) {
        AtlasSprite atlasSprite = new AtlasSprite(((AtlasSprite) sprite).getAtlasRegion());
        atlasSprite.set(sprite);
        return atlasSprite;
    } else {
        return new Sprite(sprite);
    }
}
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)

Aggregations

Sprite (com.badlogic.gdx.graphics.g2d.Sprite)40 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)19 Texture (com.badlogic.gdx.graphics.Texture)17 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)13 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)10 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)8 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)8 Test (org.junit.Test)8 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)7 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)6 Pixmap (com.badlogic.gdx.graphics.Pixmap)5 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)4 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)4 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)4 InputAdapter (com.badlogic.gdx.InputAdapter)3 Matrix4 (com.badlogic.gdx.math.Matrix4)3 Vector2 (com.badlogic.gdx.math.Vector2)3 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)3 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)3