use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class Rect2iTypeHandler method deserialize.
@Override
public Rect2i deserialize(PersistedData data, DeserializationContext context) {
if (!data.isNull() && data.isValueMap()) {
PersistedDataMap map = data.getAsValueMap();
Vector2i min = context.deserializeAs(map.get(MIN_FIELD), Vector2i.class);
Vector2i size = context.deserializeAs(map.get(SIZE_FIELD), Vector2i.class);
return Rect2i.createFromMinAndSize(min, size);
}
return null;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class MigComponent method calculatePreferredSize.
public void calculatePreferredSize(Canvas canvas, Vector2i sizeHint) {
minimumSize = canvas.calculatePreferredSize(widget);
minimumSize.x = Math.min(sizeHint.x, minimumSize.x);
minimumSize.y = Math.min(sizeHint.y, minimumSize.y);
preferredSize = new Vector2i(Math.max(sizeHint.x, minimumSize.x), Math.max(sizeHint.y, minimumSize.y));
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class UIDoubleSlider method getTickerPreferredContentSize.
private Vector2i getTickerPreferredContentSize(Canvas canvas, String part) {
Vector2i result = new Vector2i();
canvas.setPart(part);
int tickerWidth = canvas.getCurrentStyle().getFont().getWidth(formatString);
tickerWidth += canvas.getCurrentStyle().getMargin().getTotalWidth();
result.x = tickerWidth;
if (canvas.getCurrentStyle().getFixedWidth() != 0) {
result.x = Math.max(result.x, canvas.getCurrentStyle().getFixedWidth());
} else {
result.x = Math.max(result.x, canvas.getCurrentStyle().getMinWidth());
}
if (canvas.getCurrentStyle().getFixedHeight() != 0) {
result.y = canvas.getCurrentStyle().getFixedHeight();
} else {
result.y = canvas.getCurrentStyle().getMinHeight();
}
return result;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class UIDropdownScrollable method createScrollbarItems.
/**
* Located in the onDraw method, this draws the menu items with a scrollbar.
*
* @param canvas {@link Canvas} from the onDraw method.
* @param frame Menu frame.
* @param font {@link Font} used in the menu.
* @param itemMargin Margin around every menu item.
* @param height Total menu height.
* @param itemHeight Height per menu item.
*/
private void createScrollbarItems(Canvas canvas, Rect2i frame, Font font, Border itemMargin, int height, int itemHeight) {
// Scrollable Area
Rect2i scrollableArea = Rect2i.createFromMinAndSize(0, canvas.size().y, canvas.size().x, height - itemMargin.getBottom());
// Scrollbar Measurement
int scrollbarWidth = canvas.calculateRestrictedSize(verticalBar, new Vector2i(canvas.size().x, canvas.size().y)).x;
int scrollbarHeight = frame.size().y - itemMargin.getTop();
int availableWidth = frame.size().x - scrollbarWidth;
int scrollbarXPos = availableWidth - itemMargin.getRight();
int scrollbarYPos = itemMargin.getTotalHeight() * 2 + font.getLineHeight();
// Draw Scrollbar
Rect2i scrollbarRegion = Rect2i.createFromMinAndSize(scrollbarXPos, scrollbarYPos, scrollbarWidth, scrollbarHeight);
canvas.drawWidget(verticalBar, scrollbarRegion);
// Set the range of Scrollbar
float maxVertBarDesired = itemHeight * (optionListeners.size() - visibleOptionsNum - 0.5f) + itemMargin.getBottom();
verticalBar.setRange((int) maxVertBarDesired);
for (int i = 0; i < optionListeners.size(); ++i) {
readItemMouseOver(canvas, i);
Rect2i itemRegion = Rect2i.createFromMinAndSize(0, itemHeight * i - verticalBar.getValue(), availableWidth, itemHeight);
// If outside location, then hide
try (SubRegion ignored = canvas.subRegion(scrollableArea, true)) {
drawItem(canvas, itemMargin, i, itemRegion);
}
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class UIIconBar method onDraw.
@Override
public void onDraw(Canvas canvas) {
canvas.setPart(ICON_PART);
if (icon != null && getMaxValue() > 0) {
Vector2i iconSize = getIconSize(canvas);
float ratio = maxIcons * getValue() / getMaxValue();
int fullIcons = TeraMath.floorToInt(ratio);
boolean halfIcon = false;
if (ratio - fullIcons >= 0.5f) {
fullIcons++;
} else if (ratio - fullIcons > 0) {
halfIcon = true;
}
Vector2i offset = new Vector2i();
for (int i = 0; i < maxIcons; ++i) {
Rect2i iconRegion = Rect2i.createFromMinAndSize(offset, iconSize);
canvas.drawBackground(iconRegion);
if (ratio - i >= 0.5f) {
canvas.drawTexture(icon, iconRegion);
} else if (ratio - i > 0f) {
switch(halfIconMode) {
case SHRINK:
Vector2i halfSize = new Vector2i(iconSize);
halfSize.x /= 2;
halfSize.y /= 2;
canvas.drawTexture(icon, Rect2i.createFromMinAndSize(new Vector2i(offset.x + halfSize.x / 2, offset.y + halfSize.y / 2), halfSize));
break;
case SPLIT:
canvas.drawTextureRaw(icon, Rect2i.createFromMinAndSize(offset, new Vector2i(iconSize.x / 2, iconSize.y)), ScaleMode.STRETCH, 0f, 0f, (float) (iconSize.x / 2) / iconSize.x, 1.0f);
break;
default:
canvas.drawTexture(icon, iconRegion);
break;
}
}
offset.x += iconSize.x + spacing;
if (offset.x + iconSize.x > canvas.size().x) {
offset.x = 0;
offset.y += iconSize.y + spacing;
}
}
}
}
Aggregations