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);
}
}
}
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;
}
}
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());
}
}
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);
}
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;
}
Aggregations