Search in sources :

Example 1 with TextureRegion

use of org.terasology.rendering.assets.texture.TextureRegion in project Terasology by MovingBlocks.

the class StringTextIconRenderer method draw.

@Override
public void draw(T value, Canvas canvas) {
    // Drawing the icon
    TextureRegion texture = getTexture(value);
    if (texture != null) {
        if (marginTop + texture.getHeight() + marginBottom > canvas.size().y) {
            // Icon does not fit within the canvas - vertically shrinking it
            int iconHeight = canvas.size().y - marginTop - marginBottom;
            canvas.drawTexture(texture, Rect2i.createFromMinAndSize(marginLeft, marginTop, texture.getWidth(), iconHeight));
        } else {
            // Icon fits within the canvas - vertically centering it
            int iconVerticalPosition = (canvas.size().y - texture.getHeight()) / 2;
            canvas.drawTexture(texture, Rect2i.createFromMinAndSize(marginLeft, iconVerticalPosition, texture.getWidth(), texture.getHeight()));
        }
    }
    // Drawing the text, adjusting for icon width
    String text = getString(value);
    int iconWidth;
    if (texture != null) {
        iconWidth = marginLeft + texture.getWidth() + marginRight;
    } else {
        iconWidth = 0;
    }
    Rect2i textRegion = Rect2i.createFromMinAndSize(iconWidth, 0, canvas.getRegion().width() - iconWidth, canvas.getRegion().height());
    canvas.drawText(text, textRegion);
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) TextureRegion(org.terasology.rendering.assets.texture.TextureRegion)

Example 2 with TextureRegion

use of org.terasology.rendering.assets.texture.TextureRegion in project Terasology by MovingBlocks.

the class StringTextIconRenderer method getPreferredSize.

@Override
public Vector2i getPreferredSize(T value, Canvas canvas) {
    Font font = canvas.getCurrentStyle().getFont();
    String text = getString(value);
    TextureRegion texture = getTexture(value);
    if (texture == null) {
        List<String> lines = TextLineBuilder.getLines(font, text, canvas.size().x);
        return font.getSize(lines);
    } else {
        int iconWidth = marginLeft + texture.getWidth() + marginRight;
        List<String> lines = TextLineBuilder.getLines(font, text, canvas.size().x - iconWidth);
        return font.getSize(lines).addX(iconWidth);
    }
}
Also used : TextureRegion(org.terasology.rendering.assets.texture.TextureRegion) Font(org.terasology.rendering.assets.font.Font)

Aggregations

TextureRegion (org.terasology.rendering.assets.texture.TextureRegion)2 Rect2i (org.terasology.math.geom.Rect2i)1 Font (org.terasology.rendering.assets.font.Font)1