Search in sources :

Example 21 with Drawable

use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project gdx-skineditor by cobolfoo.

the class Skin method getTiledDrawable.

/**
	 * Returns a registered tiled drawable. If no tiled drawable is found but a
	 * region exists with the name, a tiled drawable is created from the region
	 * and stored in the skin.
	 */
public TiledDrawable getTiledDrawable(String name) {
    TiledDrawable tiled = optional(name, TiledDrawable.class);
    if (tiled != null)
        return tiled;
    Drawable drawable = optional(name, Drawable.class);
    if (drawable != null) {
        if (!(drawable instanceof TiledDrawable)) {
            throw new GdxRuntimeException("Drawable found but is not a TiledDrawable: " + name + ", " + drawable.getClass().getName());
        }
        return (TiledDrawable) drawable;
    }
    tiled = new TiledDrawable(getRegion(name));
    add(name, tiled, TiledDrawable.class);
    return tiled;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) 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)

Example 22 with Drawable

use of com.badlogic.gdx.scenes.scene2d.utils.Drawable 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 Drawable

use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project libgdx by libgdx.

the class Label method getPrefHeight.

public float getPrefHeight() {
    if (prefSizeInvalid)
        scaleAndComputePrefSize();
    float descentScaleCorrection = 1;
    if (fontScaleChanged)
        descentScaleCorrection = fontScaleY / style.font.getScaleY();
    float height = prefSize.y - style.font.getDescent() * descentScaleCorrection * 2;
    Drawable background = style.background;
    if (background != null)
        height += background.getTopHeight() + background.getBottomHeight();
    return height;
}
Also used : Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable)

Example 24 with Drawable

use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project libgdx by libgdx.

the class List method layout.

public void layout() {
    final BitmapFont font = style.font;
    final Drawable selectedDrawable = style.selection;
    itemHeight = font.getCapHeight() - font.getDescent() * 2;
    itemHeight += selectedDrawable.getTopHeight() + selectedDrawable.getBottomHeight();
    textOffsetX = selectedDrawable.getLeftWidth();
    textOffsetY = selectedDrawable.getTopHeight() - font.getDescent();
    prefWidth = 0;
    Pool<GlyphLayout> layoutPool = Pools.get(GlyphLayout.class);
    GlyphLayout layout = layoutPool.obtain();
    for (int i = 0; i < items.size; i++) {
        layout.setText(font, toString(items.get(i)));
        prefWidth = Math.max(layout.width, prefWidth);
    }
    layoutPool.free(layout);
    prefWidth += selectedDrawable.getLeftWidth() + selectedDrawable.getRightWidth();
    prefHeight = items.size * itemHeight;
    Drawable background = style.background;
    if (background != null) {
        prefWidth += background.getLeftWidth() + background.getRightWidth();
        prefHeight += background.getTopHeight() + background.getBottomHeight();
    }
}
Also used : Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) GlyphLayout(com.badlogic.gdx.graphics.g2d.GlyphLayout) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 25 with Drawable

use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project libgdx by libgdx.

the class ProgressBar method draw.

@Override
public void draw(Batch batch, float parentAlpha) {
    ProgressBarStyle style = this.style;
    boolean disabled = this.disabled;
    final Drawable knob = getKnobDrawable();
    final Drawable bg = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
    final Drawable knobBefore = (disabled && style.disabledKnobBefore != null) ? style.disabledKnobBefore : style.knobBefore;
    final Drawable knobAfter = (disabled && style.disabledKnobAfter != null) ? style.disabledKnobAfter : style.knobAfter;
    Color color = getColor();
    float x = getX();
    float y = getY();
    float width = getWidth();
    float height = getHeight();
    float knobHeight = knob == null ? 0 : knob.getMinHeight();
    float knobWidth = knob == null ? 0 : knob.getMinWidth();
    float percent = getVisualPercent();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    if (vertical) {
        float positionHeight = height;
        float bgTopHeight = 0;
        if (bg != null) {
            if (round)
                bg.draw(batch, Math.round(x + (width - bg.getMinWidth()) * 0.5f), y, Math.round(bg.getMinWidth()), height);
            else
                bg.draw(batch, x + width - bg.getMinWidth() * 0.5f, y, bg.getMinWidth(), height);
            bgTopHeight = bg.getTopHeight();
            positionHeight -= bgTopHeight + bg.getBottomHeight();
        }
        float knobHeightHalf = 0;
        if (min != max) {
            if (knob == null) {
                knobHeightHalf = knobBefore == null ? 0 : knobBefore.getMinHeight() * 0.5f;
                position = (positionHeight - knobHeightHalf) * percent;
                position = Math.min(positionHeight - knobHeightHalf, position);
            } else {
                knobHeightHalf = knobHeight * 0.5f;
                position = (positionHeight - knobHeight) * percent;
                position = Math.min(positionHeight - knobHeight, position) + bg.getBottomHeight();
            }
            position = Math.max(0, position);
        }
        if (knobBefore != null) {
            float offset = 0;
            if (bg != null)
                offset = bgTopHeight;
            if (round)
                knobBefore.draw(batch, Math.round(x + (width - knobBefore.getMinWidth()) * 0.5f), Math.round(y + offset), Math.round(knobBefore.getMinWidth()), Math.round(position + knobHeightHalf));
            else
                knobBefore.draw(batch, x + (width - knobBefore.getMinWidth()) * 0.5f, y + offset, knobBefore.getMinWidth(), position + knobHeightHalf);
        }
        if (knobAfter != null) {
            if (round)
                knobAfter.draw(batch, Math.round(x + (width - knobAfter.getMinWidth()) * 0.5f), Math.round(y + position + knobHeightHalf), Math.round(knobAfter.getMinWidth()), Math.round(height - position - knobHeightHalf));
            else
                knobAfter.draw(batch, x + (width - knobAfter.getMinWidth()) * 0.5f, y + position + knobHeightHalf, knobAfter.getMinWidth(), height - position - knobHeightHalf);
        }
        if (knob != null) {
            if (round)
                knob.draw(batch, Math.round(x + (width - knobWidth) * 0.5f), Math.round(y + position), Math.round(knobWidth), Math.round(knobHeight));
            else
                knob.draw(batch, x + (width - knobWidth) * 0.5f, y + position, knobWidth, knobHeight);
        }
    } else {
        float positionWidth = width;
        float bgLeftWidth = 0;
        if (bg != null) {
            if (round)
                bg.draw(batch, x, Math.round(y + (height - bg.getMinHeight()) * 0.5f), width, Math.round(bg.getMinHeight()));
            else
                bg.draw(batch, x, y + (height - bg.getMinHeight()) * 0.5f, width, bg.getMinHeight());
            bgLeftWidth = bg.getLeftWidth();
            positionWidth -= bgLeftWidth + bg.getRightWidth();
        }
        float knobWidthHalf = 0;
        if (min != max) {
            if (knob == null) {
                knobWidthHalf = knobBefore == null ? 0 : knobBefore.getMinWidth() * 0.5f;
                position = (positionWidth - knobWidthHalf) * percent;
                position = Math.min(positionWidth - knobWidthHalf, position);
            } else {
                knobWidthHalf = knobWidth * 0.5f;
                position = (positionWidth - knobWidth) * percent;
                position = Math.min(positionWidth - knobWidth, position) + bgLeftWidth;
            }
            position = Math.max(0, position);
        }
        if (knobBefore != null) {
            float offset = 0;
            if (bg != null)
                offset = bgLeftWidth;
            if (round)
                knobBefore.draw(batch, Math.round(x + offset), Math.round(y + (height - knobBefore.getMinHeight()) * 0.5f), Math.round(position + knobWidthHalf), Math.round(knobBefore.getMinHeight()));
            else
                knobBefore.draw(batch, x + offset, y + (height - knobBefore.getMinHeight()) * 0.5f, position + knobWidthHalf, knobBefore.getMinHeight());
        }
        if (knobAfter != null) {
            if (round)
                knobAfter.draw(batch, Math.round(x + position + knobWidthHalf), Math.round(y + (height - knobAfter.getMinHeight()) * 0.5f), Math.round(width - position - knobWidthHalf), Math.round(knobAfter.getMinHeight()));
            else
                knobAfter.draw(batch, x + position + knobWidthHalf, y + (height - knobAfter.getMinHeight()) * 0.5f, width - position - knobWidthHalf, knobAfter.getMinHeight());
        }
        if (knob != null) {
            if (round)
                knob.draw(batch, Math.round(x + position), Math.round(y + (height - knobHeight) * 0.5f), Math.round(knobWidth), Math.round(knobHeight));
            else
                knob.draw(batch, x + position, y + (height - knobHeight) * 0.5f, knobWidth, knobHeight);
        }
    }
}
Also used : Color(com.badlogic.gdx.graphics.Color) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable)

Aggregations

Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)34 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)10 Color (com.badlogic.gdx.graphics.Color)9 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)7 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)6 Actor (com.badlogic.gdx.scenes.scene2d.Actor)5 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)5 TiledDrawable (com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable)5 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)5 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)4 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)4 GlyphLayout (com.badlogic.gdx.graphics.g2d.GlyphLayout)3 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)3 ListStyle (com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle)3 ScrollPaneStyle (com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle)3 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)3 Array (com.badlogic.gdx.utils.Array)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)2 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)2