use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class NUIManagerInternal method createScreen.
@Override
public <T extends CoreScreenLayer> T createScreen(ResourceUrn screenUri, Class<T> expectedType) {
boolean existsAlready = !screenUri.isInstance() && assetManager.isLoaded(screenUri, UIElement.class);
Optional<UIElement> opt = Assets.get(screenUri, UIElement.class);
if (!opt.isPresent()) {
logger.error("Can't find screen '{}'", screenUri);
} else {
UIElement element = opt.get();
UIWidget root = element.getRootWidget();
if (expectedType.isInstance(root)) {
T screen = expectedType.cast(root);
if (!existsAlready) {
initialiseScreen(screen, screenUri);
}
return screen;
} else {
logger.error("Screen '{}' is a '{}' and not a '{}'", screenUri, root.getClass(), expectedType);
}
}
return null;
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class CardLayout method getPreferredContentSize.
/**
* Retrieves the preferred content size of this {@code CardLayout}.
* This is the minimum size this layout will take, given no space restrictions.
*
* @param canvas The {@code Canvas} on which this {@code CardLayout} is drawn
* @param sizeHint A {@link Vector2i} representing the space available for widgets to be drawn in this layout
* @return A {@code Vector2i} representing the preferred content size of this {@code CardLayout}
*/
@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
int maxX = 0;
int maxY = 0;
for (UIWidget uiWidget : widgets.values()) {
Vector2i preferredContentSize = uiWidget.getPreferredContentSize(canvas, sizeHint);
maxX = Math.max(maxX, preferredContentSize.x);
maxY = Math.max(maxY, preferredContentSize.y);
}
return new Vector2i(maxX, maxY);
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class CardLayout method getMaxContentSize.
/**
* Retrieves the maximum content size of this {@code CardLayout}.
*
* @param canvas The {@code Canvas} on which this {@code CardLayout} is drawn
* @return A {@code Vector2i} representing the maximum content size of this {@code CardLayout}
*/
@Override
public Vector2i getMaxContentSize(Canvas canvas) {
int maxX = 0;
int maxY = 0;
for (UIWidget uiWidget : widgets.values()) {
Vector2i maxContentSize = uiWidget.getMaxContentSize(canvas);
maxX = Math.max(maxX, maxContentSize.x);
maxY = Math.max(maxY, maxContentSize.y);
}
return new Vector2i(maxX, maxY);
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class ColumnLayout method getPreferredContentSize.
@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i areaHint) {
Vector2i availableSize = new Vector2i(areaHint);
int numRows = TeraMath.ceilToInt((float) widgetList.size() / columns);
if (numRows > 0) {
availableSize.y -= verticalSpacing * (numRows - 1);
}
if (columns > 0) {
availableSize.x -= horizontalSpacing * (columns - 1);
}
Iterator<List<UIWidget>> rows = getRowIterator();
Vector2i size = new Vector2i();
int[] columnSizes = new int[columns];
while (rows.hasNext()) {
List<UIWidget> row = rows.next();
RowInfo rowInfo = calculateRowSize(row, canvas, availableSize);
size.y += rowInfo.height;
if (rows.hasNext()) {
size.y += verticalSpacing;
}
for (int i = 0; i < rowInfo.widgetSizes.size(); ++i) {
columnSizes[i] = Math.max(columnSizes[i], rowInfo.widgetSizes.get(i).getX());
}
}
for (int columnSize : columnSizes) {
size.x += columnSize;
}
if (!autoSizeColumns) {
for (int i = 0; i < columns; ++i) {
size.x = Math.max(size.x, TeraMath.floorToInt(columnSizes[i] / columnWidths[i]));
}
}
size.x += horizontalSpacing * (columns - 1);
return size;
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class ColumnLayout method onDraw.
@Override
public void onDraw(Canvas canvas) {
if (!widgetList.isEmpty()) {
Vector2i availableSize = canvas.size();
int numRows = TeraMath.ceilToInt((float) widgetList.size() / columns);
if (numRows > 0) {
availableSize.y -= verticalSpacing * (numRows - 1);
}
if (columns > 0) {
availableSize.x -= horizontalSpacing * (columns - 1);
}
List<List<UIWidget>> rows = Lists.newArrayList(getRowIterator());
List<RowInfo> rowInfos = Lists.newArrayList();
rowInfos.addAll(rows.stream().map(row -> calculateRowSize(row, canvas, availableSize)).collect(Collectors.toList()));
int[] minWidths = new int[columns];
int minRowWidth = 0;
int rowOffsetX = 0;
if (autoSizeColumns) {
for (RowInfo row : rowInfos) {
for (int column = 0; column < row.widgetSizes.size(); column++) {
minWidths[column] = Math.max(minWidths[column], row.widgetSizes.get(column).getX());
}
}
for (int width : minWidths) {
minRowWidth += width;
}
minRowWidth += (columns - 1) * horizontalSpacing;
rowOffsetX = (canvas.size().x - minRowWidth) / 2;
} else {
minRowWidth = canvas.size().x;
for (int i = 0; i < columns; ++i) {
minWidths[i] = TeraMath.floorToInt((minRowWidth - (columns - 1) * horizontalSpacing) * columnWidths[i]);
}
}
int rowOffsetY = 0;
int usedHeight = 0;
for (RowInfo row : rowInfos) {
usedHeight += row.height;
}
usedHeight += (rowInfos.size() - 1) * verticalSpacing;
int excessHeight = canvas.size().y - usedHeight;
if (fillVerticalSpace) {
if (extendLast && numRows > 0) {
// give all the extra space to the last entry
rowInfos.get(numRows - 1).height += excessHeight;
} else {
// distribute extra height equally
int extraSpacePerRow = excessHeight / rowInfos.size();
for (RowInfo row : rowInfos) {
row.height += extraSpacePerRow;
}
}
} else {
rowOffsetY = excessHeight / 2;
}
for (int rowIndex = 0; rowIndex < rows.size(); ++rowIndex) {
List<UIWidget> row = rows.get(rowIndex);
RowInfo rowInfo = rowInfos.get(rowIndex);
int cellOffsetX = rowOffsetX;
for (int i = 0; i < row.size(); ++i) {
UIWidget widget = row.get(i);
int rowHeight = rowInfo.height;
if (widget != null) {
Rect2i drawRegion = Rect2i.createFromMinAndSize(cellOffsetX, rowOffsetY, minWidths[i], rowHeight);
canvas.drawWidget(widget, drawRegion);
}
cellOffsetX += minWidths[i] + horizontalSpacing;
}
rowOffsetY += rowInfo.height + verticalSpacing;
}
}
}
Aggregations