public void setItems(Object[] objects) { if (objects == null) throw new IllegalArgumentException("items cannot be null."); if (!(objects instanceof String[])) { String[] strings = new String[objects.length]; for (int i = 0, n = objects.length; i < n; i++) strings[i] = String.valueOf(objects[i]); objects = strings; } this.items = (String[]) objects; selectedIndex = 0; Drawable bg = style.background; BitmapFont font = style.font; prefHeight = Math.max( bg.getTopHeight() + bg.getBottomHeight() + font.getCapHeight() - font.getDescent() * 2, bg.getMinHeight()); float max = 0; for (int i = 0; i < items.length; i++) max = Math.max(font.getBounds(items[i]).width, max); prefWidth = bg.getLeftWidth() + bg.getRightWidth() + max; prefWidth = Math.max( prefWidth, max + style.listBackground.getLeftWidth() + style.listBackground.getRightWidth() + 2 * style.itemSpacing); invalidateHierarchy(); }
/** Draws selection rectangle * */ protected void drawSelection(Drawable selection, Batch batch, BitmapFont font, float x, float y) { selection.draw( batch, x + textOffset + selectionX + fontOffset, y - textHeight - font.getDescent(), selectionWidth, textHeight); }
private void layout() { final BitmapFont font = style.font; final Drawable listSelection = style.listSelection; final Drawable listBackground = style.listBackground; itemHeight = font.getCapHeight() + -font.getDescent() * 2 + style.itemSpacing; itemHeight += listSelection.getTopHeight() + listSelection.getBottomHeight(); textOffsetX = listSelection.getLeftWidth() + style.itemSpacing; textOffsetY = listSelection.getTopHeight() + -font.getDescent() + style.itemSpacing / 2; setWidth(SelectBox.this.getWidth()); setHeight( items.length * itemHeight + listBackground.getTopHeight() + listBackground.getBottomHeight()); }
public void layout(CustomListStyle style) { this.style = style; BitmapFont font = style.font; Drawable selectedDrawable = style.selection; textOffsetX = selectedDrawable.getLeftWidth(); textOffsetY = selectedDrawable.getTopHeight() - font.getDescent(); itemHeight = font.getCapHeight() - font.getDescent() * 2; if (hasSubtitle()) { itemHeight += style.subtitleFont.getCapHeight() - style.subtitleFont.getDescent() * 2; ; } itemHeight += selectedDrawable.getTopHeight() + selectedDrawable.getBottomHeight(); }
protected float getTextY(BitmapFont font, Drawable background) { float height = getHeight(); float textY = textHeight / 2 + font.getDescent(); if (background != null) { float bottom = background.getBottomHeight(); textY = textY + (height - background.getTopHeight() - bottom) / 2 + bottom; } else { textY = textY + height / 2; } if (font.usesIntegerPositions()) textY = (int) textY; return textY; }
protected void drawCursor(Drawable cursorPatch, Batch batch, BitmapFont font, float x, float y) { cursorPatch.draw( batch, x + textOffset + glyphPositions.get(cursor) - glyphPositions.get(visibleTextStart) + fontOffset + font.getData().cursorX, y - textHeight - font.getDescent(), cursorPatch.getMinWidth(), textHeight); }
@Override public void renderContent(@NtN Batch batch, @NtN Vector2 pos, @NtN Vector2 size) { batch.flush(); ScissorStack.pushScissors(new Rectangle(pos.x, pos.y, size.x, size.y)); checkGlyphLayout(size); float yOffset = (font.getLineHeight() - font.getAscent() - font.getCapHeight() - font.getDescent()) / 2; font.draw( batch, glyphLayout, pos.x, pos.y + size.y - yOffset + currentLine * font.getLineHeight()); batch.flush(); ScissorStack.popScissors(); }
public void setText(String text) { if (text == null) throw new IllegalArgumentException("text cannot be null."); BitmapFont font = style.font; StringBuffer buffer = new StringBuffer(); for (int i = 0; i < text.length(); i++) { if (maxLength > 0 && buffer.length() + 1 > maxLength) { break; } char c = text.charAt(i); if (font.containsCharacter(c) && (filter == null || filter.acceptChar(this, c))) buffer.append(c); } this.text = buffer.toString(); updateDisplayText(); cursor = 0; clearSelection(); textBounds.set(font.getBounds(displayText)); textBounds.height -= font.getDescent() * 2; font.computeGlyphAdvancesAndPositions(displayText, glyphAdvances, glyphPositions); }
@Override public void draw(SpriteBatch batch, float parentAlpha) { final BitmapFont font = style.font; final Color fontColor = disabled ? style.disabledFontColor : style.fontColor; final Drawable selection = style.selection; final Drawable cursorPatch = style.cursor; final Drawable background = style.background; Color color = getColor(); float x = getX(); float y = getY(); float width = getWidth(); float height = getHeight(); float textY = textBounds.height / 2 + font.getDescent(); batch.setColor(color.r, color.g, color.b, color.a * parentAlpha); float bgLeftWidth = 0; if (background != null) { background.draw(batch, x, y, width, height); bgLeftWidth = background.getLeftWidth(); float bottom = background.getBottomHeight(); textY = (int) (textY + (height - background.getTopHeight() - bottom) / 2 + bottom); } else textY = (int) (textY + height / 2); calculateOffsets(); Stage stage = getStage(); boolean focused = stage != null && stage.getKeyboardFocus() == this; if (focused && hasSelection && selection != null) { selection.draw( batch, x + selectionX + bgLeftWidth + renderOffset, y + textY - textBounds.height - font.getDescent(), selectionWidth, textBounds.height + font.getDescent() / 2); } float yOffset = font.isFlipped() ? -textBounds.height : 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 * parentAlpha); } else font.setColor(0.7f, 0.7f, 0.7f, parentAlpha); BitmapFont messageFont = style.messageFont != null ? style.messageFont : font; messageFont.draw(batch, messageText, x + bgLeftWidth, y + textY + yOffset); } } else { font.setColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a * parentAlpha); font.draw( batch, displayText, x + bgLeftWidth + textOffset, y + textY + yOffset, visibleTextStart, visibleTextEnd); } if (focused && !disabled) { blink(); if (cursorOn && cursorPatch != null) { cursorPatch.draw( batch, x + bgLeftWidth + textOffset + glyphPositions.get(cursor) - glyphPositions.items[visibleTextStart] - 1, y + textY - textBounds.height - font.getDescent(), cursorPatch.getMinWidth(), textBounds.height + font.getDescent() / 2); } } }
public void draw( Batch batch, float parentAlpha, T item, boolean selected, float x, float y, float width, float height) { BitmapFont font = style.font; Drawable selectedDrawable = style.selection; Color fontColorSelected = style.fontColorSelected; Color fontColorUnselected = style.fontColorUnselected; if (selected) { selectedDrawable.draw(batch, x, y - height, width, height); font.setColor( fontColorSelected.r, fontColorSelected.g, fontColorSelected.b, fontColorSelected.a * parentAlpha); } else { font.setColor( fontColorUnselected.r, fontColorUnselected.g, fontColorUnselected.b, fontColorUnselected.a * parentAlpha); } if (hasImage()) { TextureRegion r = getCellImage(item); float ih = r.getRegionHeight(); float iw = r.getRegionWidth(); if (ih > getItemHeight() - MARGIN) { ih = getItemHeight() - MARGIN; iw *= ih / r.getRegionHeight(); } batch.draw(r, x, y - ih - MARGIN / 2, iw, ih); x += iw; } font.draw(batch, getCellTitle(item), x + textOffsetX, y - textOffsetY); if (hasSubtitle()) { if (selected) { style.subtitleFont.setColor( fontColorSelected.r, fontColorSelected.g, fontColorSelected.b, fontColorSelected.a * parentAlpha * 0.5f); } else { style.subtitleFont.setColor( fontColorUnselected.r, fontColorUnselected.g, fontColorUnselected.b, fontColorUnselected.a * parentAlpha * 0.5f); } style.subtitleFont.draw( batch, getCellSubTitle(item), x + textOffsetX, y - textOffsetY - (font.getCapHeight() - font.getDescent() * 2)); } }