Search in sources :

Example 1 with NinePatch

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

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;
    // Use texture or texture region. If it has splits, use ninepatch. If it 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) {
    }
    // Check for explicit registration of ninepatch, sprite, or tiled 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);
        }
    }
    if (drawable instanceof BaseDrawable)
        ((BaseDrawable) drawable).setName(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) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) BaseDrawable(com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) BaseDrawable(com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable) 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 2 with NinePatch

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

the class Skin method getPatch.

/** Returns a registered ninepatch. If no ninepatch is found but a region exists with the name, a ninepatch is created from the
	 * region and stored in the skin. If the region is an {@link AtlasRegion} then the {@link AtlasRegion#splits} are used,
	 * otherwise the ninepatch will have the region as the center patch. */
public NinePatch getPatch(String name) {
    NinePatch patch = optional(name, NinePatch.class);
    if (patch != null)
        return patch;
    try {
        TextureRegion region = getRegion(name);
        if (region instanceof AtlasRegion) {
            int[] splits = ((AtlasRegion) region).splits;
            if (splits != null) {
                patch = new NinePatch(region, splits[0], splits[1], splits[2], splits[3]);
                int[] pads = ((AtlasRegion) region).pads;
                if (pads != null)
                    patch.setPadding(pads[0], pads[1], pads[2], pads[3]);
            }
        }
        if (patch == null)
            patch = new NinePatch(region);
        add(name, patch, NinePatch.class);
        return patch;
    } 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) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 3 with NinePatch

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

the class NinePatchTest method newNinePatch.

// Make a basic NinePatch with different colors in each of the nine patches
static NinePatch newNinePatch() {
    final int patchSize = 8;
    final int pixmapSize = patchSize * 3;
    TextureRegion tr = newPatchPix(patchSize, pixmapSize);
    return new NinePatch(tr, patchSize, patchSize, patchSize, patchSize);
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch)

Example 4 with NinePatch

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

the class NinePatchTest method newULQuadPatch.

// Make a upper-left "quad" patch (only 4 patches defined in the top-left corner of the ninepatch)
static NinePatch newULQuadPatch() {
    final int patchSize = 8;
    final int pixmapSize = patchSize * 2;
    TextureRegion tr = newPatchPix(patchSize, pixmapSize);
    return new NinePatch(tr, patchSize, 0, patchSize, 0);
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch)

Example 5 with NinePatch

use of com.badlogic.gdx.graphics.g2d.NinePatch 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)

Aggregations

NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)14 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)5 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)5 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)5 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)4 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)4 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)3 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)3 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)2 Texture (com.badlogic.gdx.graphics.Texture)2 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)2 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)2 TiledDrawable (com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable)2 Batch (com.badlogic.gdx.graphics.g2d.Batch)1 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)1 Actor (com.badlogic.gdx.scenes.scene2d.Actor)1 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)1