use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class UIList method onDraw.
@Override
public void onDraw(Canvas canvas) {
updateItemListeners();
canvas.setPart("item");
boolean enabled = isEnabled();
Border margin = canvas.getCurrentStyle().getMargin();
int yOffset = 0;
for (int i = 0; i < list.get().size(); ++i) {
T item = list.get().get(i);
Vector2i preferredSize = margin.grow(itemRenderer.getPreferredSize(item, canvas));
Rect2i itemRegion = Rect2i.createFromMinAndSize(0, yOffset, canvas.size().x, preferredSize.y);
ItemInteractionListener listener = itemListeners.get(i);
if (enabled) {
if (Objects.equals(item, selection.get())) {
canvas.setMode(ACTIVE_MODE);
} else if (listener.isMouseOver()) {
canvas.setMode(HOVER_MODE);
} else {
canvas.setMode(DEFAULT_MODE);
}
if (isInteractive()) {
canvas.addInteractionRegion(listener, itemRenderer.getTooltip(item), itemRegion);
}
} else {
canvas.setMode(DISABLED_MODE);
}
canvas.drawBackground(itemRegion);
itemRenderer.draw(item, canvas, margin.shrink(itemRegion));
yOffset += preferredSize.getY();
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class UIText method drawSelection.
/**
* Draws the selection indication which indicates that a certain part of the text is selected.
*
* @param canvas The canvas on which the widget resides
*/
protected void drawSelection(Canvas canvas) {
Font font = canvas.getCurrentStyle().getFont();
String currentText = getText();
int start = Math.min(getCursorPosition(), selectionStart);
int end = Math.max(getCursorPosition(), selectionStart);
Color textColor = canvas.getCurrentStyle().getTextColor();
int canvasWidth = (multiline) ? canvas.size().x : Integer.MAX_VALUE;
// TODO: Support different text alignments
List<String> rawLinesAfterCursor = TextLineBuilder.getLines(font, currentText, Integer.MAX_VALUE);
int currentChar = 0;
int lineOffset = 0;
for (int lineIndex = 0; lineIndex < rawLinesAfterCursor.size() && currentChar <= end; ++lineIndex) {
String line = rawLinesAfterCursor.get(lineIndex);
List<String> innerLines = TextLineBuilder.getLines(font, line, canvasWidth);
for (int innerLineIndex = 0; innerLineIndex < innerLines.size() && currentChar <= end; ++innerLineIndex) {
String innerLine = innerLines.get(innerLineIndex);
String selectionString;
int offsetX = 0;
if (currentChar + innerLine.length() < start) {
selectionString = "";
} else if (currentChar < start) {
offsetX = font.getWidth(innerLine.substring(0, start - currentChar));
selectionString = innerLine.substring(start - currentChar, Math.min(end - currentChar, innerLine.length()));
} else if (currentChar + innerLine.length() >= end) {
selectionString = innerLine.substring(0, end - currentChar);
} else {
selectionString = innerLine;
}
if (!selectionString.isEmpty()) {
int selectionWidth = font.getWidth(selectionString);
Vector2i selectionTopLeft = new Vector2i(offsetX, (lineOffset) * font.getLineHeight());
Rect2i region = Rect2i.createFromMinAndSize(selectionTopLeft.x, selectionTopLeft.y, selectionWidth, font.getLineHeight());
canvas.drawTexture(cursorTexture, region, textColor);
canvas.drawTextRaw(FontUnderline.strip(FontColor.stripColor(selectionString)), font, textColor.inverse(), region);
}
currentChar += innerLine.length();
lineOffset++;
}
currentChar++;
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class UITreeView method getPreferredContentSize.
@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
canvas.setPart(TREE_NODE);
if (model.get().getNodeCount() == 0) {
return new Vector2i();
}
model.get().setEnumerateExpandedOnly(false);
Vector2i result = new Vector2i();
for (int i = 0; i < model.get().getNodeCount(); i++) {
Tree<T> node = model.get().getNode(i);
Vector2i preferredSize = canvas.getCurrentStyle().getMargin().grow(itemRenderer.getPreferredSize(node.getValue(), canvas).addX(node.getDepth() * levelIndent.get()));
result.x = Math.max(result.x, preferredSize.x);
result.y += preferredSize.y;
}
model.get().setEnumerateExpandedOnly(true);
// Account for the expand/contract button!
result.addX(levelIndent.get());
return result;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class DocumentRenderer method getDocumentPreferredSize.
public static Vector2i getDocumentPreferredSize(DocumentData documentData, Font defaultFont, Color defaultColor, int availableWidth) {
DefaultDocumentRenderStyle defaultDocumentRenderStyle = new DefaultDocumentRenderStyle(defaultFont, defaultColor);
DocumentRenderStyle documentRenderStyle = getDocumentRenderStyle(defaultDocumentRenderStyle, documentData);
Collection<ParagraphData> paragraphs = documentData.getParagraphs();
int minParagraphsWidth = getParagraphsMinimumWidth(availableWidth, documentRenderStyle, paragraphs);
int documentSideMargins = documentRenderStyle.getDocumentMarginLeft().getValue(availableWidth) + documentRenderStyle.getDocumentMarginRight().getValue(availableWidth);
int documentWidth = Math.max(availableWidth, minParagraphsWidth + documentSideMargins);
ContainerFlowContainerRenderSpace containerRenderSpace = new ContainerFlowContainerRenderSpace(documentWidth);
int preferredHeight = Math.max(getParagraphsPreferredHeight(documentRenderStyle, paragraphs, containerRenderSpace, 0), containerRenderSpace.getNextClearYPosition(ParagraphRenderStyle.ClearStyle.BOTH));
int documentVerticalMargins = documentRenderStyle.getDocumentMarginTop().getValue(documentWidth) + documentRenderStyle.getDocumentMarginBottom().getValue(documentWidth);
// Bring back the document indents to sides
return new Vector2i(documentWidth, preferredHeight + documentVerticalMargins);
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class SolidRasterizer method generateChunk.
@Override
public void generateChunk(CoreChunk chunk, Region chunkRegion) {
LiquidData waterLiquid = new LiquidData(LiquidType.WATER, LiquidData.MAX_LIQUID_DEPTH);
DensityFacet solidityFacet = chunkRegion.getFacet(DensityFacet.class);
SurfaceHeightFacet surfaceFacet = chunkRegion.getFacet(SurfaceHeightFacet.class);
SurfaceDepthFacet surfaceDepthFacet = chunkRegion.getFacet(SurfaceDepthFacet.class);
BiomeFacet biomeFacet = chunkRegion.getFacet(BiomeFacet.class);
SeaLevelFacet seaLevelFacet = chunkRegion.getFacet(SeaLevelFacet.class);
int seaLevel = seaLevelFacet.getSeaLevel();
Vector2i pos2d = new Vector2i();
for (Vector3i pos : ChunkConstants.CHUNK_REGION) {
pos2d.set(pos.x, pos.z);
int posY = pos.y + chunk.getChunkWorldOffsetY();
// Check for an optional depth for this layer - if defined stop generating below that level
if (surfaceDepthFacet != null && posY < surfaceDepthFacet.get(pos2d)) {
continue;
}
Biome biome = biomeFacet.get(pos2d);
chunk.setBiome(pos.x, pos.y, pos.z, biome);
float density = solidityFacet.get(pos);
if (density >= 32) {
chunk.setBlock(pos, stone);
} else if (density >= 0) {
int depth = TeraMath.floorToInt(surfaceFacet.get(pos2d)) - posY;
Block block = getSurfaceBlock(depth, posY, biome, seaLevel);
chunk.setBlock(pos, block);
} else {
// fill up terrain up to sealevel height with water or ice
if (posY == seaLevel && CoreBiome.SNOW == biome) {
chunk.setBlock(pos, ice);
} else if (posY <= seaLevel) {
// either OCEAN or SNOW
chunk.setBlock(pos, water);
chunk.setLiquid(pos, waterLiquid);
}
}
}
}
Aggregations