@Override public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) { if (icon != null) { TextureRegion texture = icon.get(); if (texture != null) { return texture.size(); } } return new Vector2i(); }
@Override public void drawTexture(TextureRegion texture, Color color, ScaleMode mode, Rect2i absoluteRegion, float ux, float uy, float uw, float uh, float alpha) { if (!texture.getTexture().isLoaded()) { return; Vector2f scale = mode.scaleForRegion(absoluteRegion, texture.getWidth(), texture.getHeight()); Rect2f textureArea = texture.getRegion(); Mesh mesh = billboard; switch (mode) { case TILED: { TextureCacheKey key = new TextureCacheKey(texture.size(), absoluteRegion.size()); usedTextures.add(key); mesh = cachedTextures.get(key); if (mesh == null || mesh.isDisposed()) { MeshBuilder builder = new MeshBuilder(); addTiles(builder, absoluteRegion, FULL_REGION, texture.size(), FULL_REGION); mesh = builder.build(); cachedTextures.put(key, mesh); textureMat.setTexture("texture", texture.getTexture()); textureMat.setFloat4("color", color.rf(), color.gf(), color.bf(), color.af() * alpha); textureMat.bindTextures();
public static BufferedImage convertToImage(TextureRegion textureRegion) { final int width = textureRegion.getWidth(); final int height = textureRegion.getHeight(); final Rect2i pixelRegion = textureRegion.getPixelRegion(); final Texture texture = textureRegion.getTexture(); ByteBuffer textureBytes = texture.getData().getBuffers()[0]; int stride = texture.getWidth() * 4; int posX = pixelRegion.minX(); int posY = pixelRegion.minY(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int r = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4)); int g = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 1)); int b = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 2)); int a = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 3)); int argb = (a << 24) + (r << 16) + (g << 8) + b; image.setRGB(x, y, argb); } } return image; }
@Override public void onDraw(Canvas canvas) { if (fillTexture != null) { int size = TeraMath.floorToInt(canvas.size().x * getValue()); int barWidth = fillTexture.getWidth(); int offset = 0; if (time != null && animate) { offset = (int) ((time.getRealTimeInMs() / 10) % barWidth); } int drawnWidth = 0; // Draw Offset if (offset != 0) { int drawWidth = Math.min(size, offset); canvas.drawTextureRaw(fillTexture, Rect2i.createFromMinAndSize(0, 0, drawWidth, canvas.size().y), ScaleMode.STRETCH, barWidth - offset, 0, drawWidth, canvas.size().y); drawnWidth += drawWidth; } // Draw Remainder while (drawnWidth < size) { int drawWidth = Math.min(size - drawnWidth, barWidth); canvas.drawTextureRaw(fillTexture, Rect2i.createFromMinAndSize(drawnWidth, 0, drawWidth, canvas.size().y), ScaleMode.STRETCH, 0, 0, drawWidth, canvas.size().y); drawnWidth += drawWidth; } } }
@Override public void drawTextureBordered(TextureRegion texture, Rect2i region, Border border, boolean tile, float ux, float uy, float uw, float uh, float alpha) { if (!texture.getTexture().isLoaded()) { return; Vector2i textureSize = new Vector2i(TeraMath.ceilToInt(texture.getWidth() * uw), TeraMath.ceilToInt(texture.getHeight() * uh)); textureMat.setFloat2("offset", region.minX(), region.minY()); Rect2f textureArea = texture.getRegion(); textureMat.setFloat2("texOffset", textureArea.minX() + ux * textureArea.width(), textureArea.minY() + uy * textureArea.height()); textureMat.setFloat2("texSize", uw * textureArea.width(), uh * textureArea.height()); textureMat.setTexture("texture", texture.getTexture()); textureMat.setFloat4("color", 1, 1, 1, alpha); textureMat.bindTextures();
public static MeshData generateIconMeshData(TextureRegion tex, int alphaLimit, boolean withContour, Vector4f colorContour) { ByteBuffer buffer = tex.getTexture().getData().getBuffers()[0]; Rect2i pixelRegion = tex.getPixelRegion(); int posX = pixelRegion.minX(); int posY = pixelRegion.minY(); int stride = tex.getTexture().getWidth() * 4; float textureSize = Math.max(tex.getWidth(), tex.getHeight()); for (int y = 0; y < tex.getHeight(); y++) { for (int x = 0; x < tex.getWidth(); x++) { int r = buffer.get((posY + y) * stride + (posX + x) * 4) & 255; int g = buffer.get((posY + y) * stride + (posX + x) * 4 + 1) & 255; Vector4f color = new Vector4f(r / 255f, g / 255f, b / 255f, a / 255f); TessellatorHelper.addBlockMesh(tessellator, color, 2f / textureSize, 1.0f, 0.5f, 2f / textureSize * x - 1f, 2f / textureSize * (tex.getHeight() - y - 1) - 1f, 0f);
@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); } }
@Override public void drawTextureRawBordered(TextureRegion texture, Rect2i region, Border border, boolean tile, int ux, int uy, int uw, int uh) { drawTextureRawBordered(texture, region, border, tile, (float) ux / texture.getWidth(), (float) uy / texture.getHeight(), (float) uw / texture.getWidth(), (float) uh / texture.getHeight()); }
@Override public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) { if (image.get() != null) { return image.get().size(); } return Vector2i.zero(); }
public ImageFlowRenderable(TextureRegion textureRegion, Integer width, Integer height, String hyperlink) { this.textureRegion = textureRegion; if (width != null && height != null) { this.imageWidth = width; this.imageHeight = height; } else if (width != null) { this.imageWidth = width; this.imageHeight = Math.round((1f * width / textureRegion.getWidth()) * textureRegion.getHeight()); } else if (height != null) { this.imageHeight = height; this.imageWidth = Math.round((1f * height / textureRegion.getHeight()) * textureRegion.getWidth()); } else { this.imageWidth = textureRegion.getWidth(); this.imageHeight = textureRegion.getHeight(); } this.hyperlink = hyperlink; }
private Vector2i getIconSize(Canvas canvas) { UIStyle iconStyle = canvas.getCurrentStyle(); int width = iconStyle.getFixedWidth(); int height = iconStyle.getFixedHeight(); if (width == 0) { width = iconStyle.getMinWidth(); } if (height == 0) { height = iconStyle.getMinHeight(); } if (width == 0) { width = icon.getWidth(); } if (height == 0) { height = icon.getHeight(); } return new Vector2i(width, height); }
@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); }