use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class CanvasImpl method addInteractionRegion.
@Override
public void addInteractionRegion(InteractionListener listener, String tooltip, Rect2i region) {
UIWidget tooltipLabelWidget = (tooltip == null || tooltip.isEmpty()) ? null : new UILabel(tooltip);
addInteractionRegion(listener, tooltipLabelWidget, region);
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class NUIManagerInternal method addOverlay.
@Override
public <T extends ControlWidget> T addOverlay(ResourceUrn overlayUri, Class<T> expectedType) {
boolean existsAlready = assetManager.isLoaded(overlayUri, UIElement.class);
Optional<UIElement> opt = Assets.get(overlayUri, UIElement.class);
if (!opt.isPresent()) {
logger.error("Can't find overlay '{}'", overlayUri);
} else {
UIElement element = opt.get();
UIWidget root = element.getRootWidget();
if (expectedType.isInstance(root)) {
T overlay = expectedType.cast(root);
if (!existsAlready) {
initialiseOverlay(overlay, overlayUri);
}
addOverlay(overlay, overlayUri);
return overlay;
} else {
logger.error("Screen '{}' is a '{}' and not a '{}'", overlayUri, root.getClass(), expectedType);
}
}
return null;
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class ColumnLayout method calculateRowSize.
private RowInfo calculateRowSize(List<UIWidget> row, Canvas canvas, Vector2i areaHint) {
int availableWidth = areaHint.x - horizontalSpacing * (columns - 1);
RowInfo rowInfo = new RowInfo();
for (int i = 0; i < columns && i < row.size(); ++i) {
UIWidget widget = row.get(i);
Vector2i cellSize = new Vector2i(availableWidth, areaHint.y);
if (!autoSizeColumns) {
cellSize.x *= columnWidths[i];
}
if (widget != null) {
Vector2i contentSize = canvas.calculateRestrictedSize(widget, cellSize);
rowInfo.widgetSizes.add(contentSize);
rowInfo.height = Math.max(rowInfo.height, contentSize.y);
} else {
rowInfo.widgetSizes.add(new Vector2i(0, 0));
}
}
return rowInfo;
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class ColumnLayout method getMaxContentSize.
@Override
public Vector2i getMaxContentSize(Canvas canvas) {
Iterator<List<UIWidget>> rows = getRowIterator();
Vector2i size = new Vector2i();
int[] columnSizes = new int[columns];
while (rows.hasNext()) {
List<UIWidget> row = rows.next();
int rowHeight = 0;
for (int i = 0; i < row.size(); ++i) {
Vector2i maxSize = canvas.calculateMaximumSize(row.get(i));
columnSizes[i] = Math.max(columnSizes[i], maxSize.x);
rowHeight = Math.max(rowHeight, maxSize.y);
}
size.y = TeraMath.addClampAtMax(size.y, rowHeight);
if (rows.hasNext()) {
size.y = TeraMath.addClampAtMax(size.y, verticalSpacing);
}
}
long width = 0;
for (int columnSize : columnSizes) {
width += columnSize;
}
if (!autoSizeColumns) {
for (int i = 0; i < columns; ++i) {
width = Math.min(width, TeraMath.floorToInt(columnSizes[i] / columnWidths[i]));
}
}
width += horizontalSpacing * (columns - 1);
size.x = (int) Math.min(Integer.MAX_VALUE, width);
return size;
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class MultiRowLayout method onDraw.
@Override
public void onDraw(Canvas canvas) {
if (!widgetList.isEmpty()) {
Vector2i availableSize = canvas.size();
int numColumns = TeraMath.ceilToInt((float) widgetList.size() / rows);
if (numColumns > 0) {
availableSize.x -= horizontalSpacing * (numColumns - 1);
}
if (rows > 0) {
availableSize.y -= verticalSpacing * (rows - 1);
}
List<List<UIWidget>> columns = Lists.newArrayList(getColumnIterator());
List<ColumnInfo> columnInfos = Lists.newArrayList();
columnInfos.addAll(columns.stream().map(column -> calculateColumnSize(column, canvas, availableSize)).collect(Collectors.toList()));
int[] minHeights = new int[rows];
int minColumnHeight = 0;
int columnOffsetY = 0;
if (autoSizeRows) {
for (ColumnInfo column : columnInfos) {
for (int row = 0; row < column.widgetSizes.size(); row++) {
minHeights[row] = Math.max(minHeights[row], column.widgetSizes.get(row).getY());
}
}
for (int height : minHeights) {
minColumnHeight += height;
}
minColumnHeight += (rows - 1) * verticalSpacing;
columnOffsetY = (canvas.size().y - minColumnHeight) / 2;
} else {
minColumnHeight = canvas.size().y;
for (int i = 0; i < rows; ++i) {
minHeights[i] = TeraMath.floorToInt((minColumnHeight - (rows - 1) * verticalSpacing) * rowHeights[i]);
}
}
int columnOffsetX = 0;
int usedWidth = 0;
for (ColumnInfo column : columnInfos) {
usedWidth += column.width;
}
usedWidth += (columnInfos.size() - 1) * horizontalSpacing;
columnOffsetX = (canvas.size().x - usedWidth) / 2;
for (int columnIndex = 0; columnIndex < columns.size(); ++columnIndex) {
List<UIWidget> column = columns.get(columnIndex);
ColumnInfo columnInfo = columnInfos.get(columnIndex);
int cellOffsetY = columnOffsetY;
for (int i = 0; i < column.size(); ++i) {
UIWidget widget = column.get(i);
int columnWidth = columnInfo.width;
if (widget != null) {
Rect2i drawRegion = Rect2i.createFromMinAndSize(columnOffsetX, cellOffsetY, columnWidth, minHeights[i]);
canvas.drawWidget(widget, drawRegion);
}
cellOffsetY += minHeights[i] + verticalSpacing;
}
columnOffsetX += columnInfo.width + horizontalSpacing;
}
}
}
Aggregations