Search in sources :

Example 11 with Drawable

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

the class TextField method draw.

@Override
public void draw(Batch batch, float parentAlpha) {
    Stage stage = getStage();
    boolean focused = stage != null && stage.getKeyboardFocus() == this;
    if (!focused)
        keyRepeatTask.cancel();
    final BitmapFont font = style.font;
    final Color fontColor = (disabled && style.disabledFontColor != null) ? style.disabledFontColor : ((focused && style.focusedFontColor != null) ? style.focusedFontColor : style.fontColor);
    final Drawable selection = style.selection;
    final Drawable cursorPatch = style.cursor;
    final Drawable background = getBackgroundDrawable();
    Color color = getColor();
    float x = getX();
    float y = getY();
    float width = getWidth();
    float height = getHeight();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    float bgLeftWidth = 0, bgRightWidth = 0;
    if (background != null) {
        background.draw(batch, x, y, width, height);
        bgLeftWidth = background.getLeftWidth();
        bgRightWidth = background.getRightWidth();
    }
    float textY = getTextY(font, background);
    calculateOffsets();
    if (focused && hasSelection && selection != null) {
        drawSelection(selection, batch, font, x + bgLeftWidth, y + textY);
    }
    float yOffset = font.isFlipped() ? -textHeight : 0;
    if (displayText.length() == 0) {
        if (!focused && messageText != null) {
            if (style.messageFontColor != null) {
                font.setColor(style.messageFontColor.r, style.messageFontColor.g, style.messageFontColor.b, style.messageFontColor.a * color.a * parentAlpha);
            } else
                font.setColor(0.7f, 0.7f, 0.7f, color.a * parentAlpha);
            BitmapFont messageFont = style.messageFont != null ? style.messageFont : font;
            messageFont.draw(batch, messageText, x + bgLeftWidth, y + textY + yOffset, 0, messageText.length(), width - bgLeftWidth - bgRightWidth, textHAlign, false, "...");
        }
    } else {
        font.setColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a * color.a * parentAlpha);
        drawText(batch, font, x + bgLeftWidth, y + textY + yOffset);
    }
    if (focused && !disabled) {
        blink();
        if (cursorOn && cursorPatch != null) {
            drawCursor(cursorPatch, batch, font, x + bgLeftWidth, y + textY);
        }
    }
}
Also used : Color(com.badlogic.gdx.graphics.Color) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) Stage(com.badlogic.gdx.scenes.scene2d.Stage) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 12 with Drawable

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

the class TextField method calculateOffsets.

protected void calculateOffsets() {
    float visibleWidth = getWidth();
    Drawable background = getBackgroundDrawable();
    if (background != null)
        visibleWidth -= background.getLeftWidth() + background.getRightWidth();
    int glyphCount = glyphPositions.size;
    float[] glyphPositions = this.glyphPositions.items;
    // Check if the cursor has gone out the left or right side of the visible area and adjust renderOffset.
    float distance = glyphPositions[Math.max(0, cursor - 1)] + renderOffset;
    if (distance <= 0)
        renderOffset -= distance;
    else {
        int index = Math.min(glyphCount - 1, cursor + 1);
        float minX = glyphPositions[index] - visibleWidth;
        if (-renderOffset < minX)
            renderOffset = -minX;
    }
    // Prevent renderOffset from starting too close to the end, eg after text was deleted.
    float maxOffset = 0;
    float width = glyphPositions[glyphCount - 1];
    for (int i = glyphCount - 2; i >= 0; i--) {
        float x = glyphPositions[i];
        if (width - x > visibleWidth)
            break;
        maxOffset = x;
    }
    if (-renderOffset > maxOffset)
        renderOffset = -maxOffset;
    // calculate first visible char based on render offset
    visibleTextStart = 0;
    float startX = 0;
    for (int i = 0; i < glyphCount; i++) {
        if (glyphPositions[i] >= -renderOffset) {
            visibleTextStart = Math.max(0, i);
            startX = glyphPositions[i];
            break;
        }
    }
    // calculate last visible char based on visible width and render offset
    int length = Math.min(displayText.length(), glyphPositions.length - 1);
    visibleTextEnd = Math.min(length, cursor + 1);
    for (; visibleTextEnd <= length; visibleTextEnd++) if (glyphPositions[visibleTextEnd] > startX + visibleWidth)
        break;
    visibleTextEnd = Math.max(0, visibleTextEnd - 1);
    if ((textHAlign & Align.left) == 0) {
        textOffset = visibleWidth - (glyphPositions[visibleTextEnd] - startX);
        if ((textHAlign & Align.center) != 0)
            textOffset = Math.round(textOffset * 0.5f);
    } else
        textOffset = startX + renderOffset;
    // calculate selection x position and width
    if (hasSelection) {
        int minIndex = Math.min(cursor, selectionStart);
        int maxIndex = Math.max(cursor, selectionStart);
        float minX = Math.max(glyphPositions[minIndex] - glyphPositions[visibleTextStart], -textOffset);
        float maxX = Math.min(glyphPositions[maxIndex] - glyphPositions[visibleTextStart], visibleWidth - textOffset);
        selectionX = minX;
        selectionWidth = maxX - minX - style.font.getData().cursorX;
    }
}
Also used : Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable)

Example 13 with Drawable

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

the class Touchpad method draw.

@Override
public void draw(Batch batch, float parentAlpha) {
    validate();
    Color c = getColor();
    batch.setColor(c.r, c.g, c.b, c.a * parentAlpha);
    float x = getX();
    float y = getY();
    float w = getWidth();
    float h = getHeight();
    final Drawable bg = style.background;
    if (bg != null)
        bg.draw(batch, x, y, w, h);
    final Drawable knob = style.knob;
    if (knob != null) {
        x += knobPosition.x - knob.getMinWidth() / 2f;
        y += knobPosition.y - knob.getMinHeight() / 2f;
        knob.draw(batch, x, y, knob.getMinWidth(), knob.getMinHeight());
    }
}
Also used : Color(com.badlogic.gdx.graphics.Color) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable)

Example 14 with Drawable

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

the class Label method layout.

public void layout() {
    BitmapFont font = cache.getFont();
    float oldScaleX = font.getScaleX();
    float oldScaleY = font.getScaleY();
    if (fontScaleChanged)
        font.getData().setScale(fontScaleX, fontScaleY);
    boolean wrap = this.wrap && ellipsis == null;
    if (wrap) {
        float prefHeight = getPrefHeight();
        if (prefHeight != lastPrefHeight) {
            lastPrefHeight = prefHeight;
            invalidateHierarchy();
        }
    }
    float width = getWidth(), height = getHeight();
    Drawable background = style.background;
    float x = 0, y = 0;
    if (background != null) {
        x = background.getLeftWidth();
        y = background.getBottomHeight();
        width -= background.getLeftWidth() + background.getRightWidth();
        height -= background.getBottomHeight() + background.getTopHeight();
    }
    GlyphLayout layout = this.layout;
    float textWidth, textHeight;
    if (wrap || text.indexOf("\n") != -1) {
        // If the text can span multiple lines, determine the text's actual size so it can be aligned within the label.
        layout.setText(font, text, 0, text.length, Color.WHITE, width, lineAlign, wrap, ellipsis);
        textWidth = layout.width;
        textHeight = layout.height;
        if ((labelAlign & Align.left) == 0) {
            if ((labelAlign & Align.right) != 0)
                x += width - textWidth;
            else
                x += (width - textWidth) / 2;
        }
    } else {
        textWidth = width;
        textHeight = font.getData().capHeight;
    }
    if ((labelAlign & Align.top) != 0) {
        y += cache.getFont().isFlipped() ? 0 : height - textHeight;
        y += style.font.getDescent();
    } else if ((labelAlign & Align.bottom) != 0) {
        y += cache.getFont().isFlipped() ? height - textHeight : 0;
        y -= style.font.getDescent();
    } else {
        y += (height - textHeight) / 2;
    }
    if (!cache.getFont().isFlipped())
        y += textHeight;
    layout.setText(font, text, 0, text.length, Color.WHITE, textWidth, lineAlign, wrap, ellipsis);
    cache.setText(layout, x, y);
    if (fontScaleChanged)
        font.getData().setScale(oldScaleX, oldScaleY);
}
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 15 with Drawable

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

the class Label method getPrefWidth.

public float getPrefWidth() {
    if (wrap)
        return 0;
    if (prefSizeInvalid)
        scaleAndComputePrefSize();
    float width = prefSize.x;
    Drawable background = style.background;
    if (background != null)
        width += background.getLeftWidth() + background.getRightWidth();
    return width;
}
Also used : 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