use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class ScrollableArea method onDraw.
@Override
public void onDraw(Canvas canvas) {
int availableWidth = canvas.size().x;
int availableHeight = canvas.size().y;
// First, try to layout it without any scroll bars
Vector2i contentSize = canvas.calculateRestrictedSize(content, new Vector2i(availableWidth, availableHeight));
if (contentSize.x <= availableWidth && contentSize.y <= availableHeight) {
canvas.drawWidget(content, Rect2i.createFromMinAndSize(new Vector2i(0, 0), new Vector2i(availableWidth, availableHeight)));
return;
}
// Second, try to layout it just with vertical bar (if supported)
if (verticalScrollbar) {
int scrollbarWidth = canvas.calculateRestrictedSize(verticalBar, new Vector2i(availableWidth, availableHeight)).x;
int scrollbarHeight = canvas.calculateRestrictedSize(verticalBar, new Vector2i(availableWidth, availableHeight)).y;
contentSize = canvas.calculateRestrictedSize(content, new Vector2i(availableWidth - scrollbarWidth, Integer.MAX_VALUE));
if (horizontalScrollbar && contentSize.x > availableWidth - scrollbarWidth) {
if (contentSize.y > availableHeight - scrollbarHeight) {
layoutWithBothScrollbars(canvas, contentSize, availableWidth, availableHeight, scrollbarWidth, scrollbarHeight);
} else {
contentSize = canvas.calculateRestrictedSize(content, new Vector2i(availableWidth, availableHeight - scrollbarHeight));
layoutWithJustHorizontal(canvas, contentSize, availableWidth, availableHeight, scrollbarHeight);
}
} else {
layoutWithJustVertical(canvas, contentSize, availableWidth, availableHeight, scrollbarWidth);
}
} else if (horizontalScrollbar) {
// Well we know that just horizontal is allowed
int scrollbarHeight = canvas.calculateRestrictedSize(verticalBar, new Vector2i(availableWidth, availableHeight)).y;
availableHeight -= scrollbarHeight;
contentSize = canvas.calculateRestrictedSize(content, new Vector2i(Integer.MAX_VALUE, availableHeight - scrollbarHeight));
layoutWithJustHorizontal(canvas, contentSize, availableWidth, availableHeight, scrollbarHeight);
} else {
throw new IllegalStateException("ScrollableArea without any scrollbar allowed, what's the point of that?!");
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class BlockSelectionRenderSystem method renderOverlay.
@Override
public void renderOverlay() {
for (EntityRef entity : entityManager.getEntitiesWith(BlockSelectionComponent.class)) {
BlockSelectionComponent blockSelectionComponent = entity.getComponent(BlockSelectionComponent.class);
if (blockSelectionComponent.shouldRender) {
Texture texture = blockSelectionComponent.texture;
if (null == texture) {
texture = Assets.getTexture("engine:selection").get();
}
Vector2i textureDimensions = new Vector2i(texture.getWidth(), texture.getHeight());
BlockSelectionRenderer selectionRenderer = cachedBlockSelectionRendererByTextureDimensionsMap.get(textureDimensions);
if (null == selectionRenderer) {
selectionRenderer = new BlockSelectionRenderer(texture);
cachedBlockSelectionRendererByTextureDimensionsMap.put(textureDimensions, selectionRenderer);
} else {
selectionRenderer.setEffectsTexture(texture);
}
renderOverlayForOneBlockSelection(blockSelectionComponent, selectionRenderer);
}
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class ContextMenuScreen method onDraw.
@Override
public void onDraw(Canvas canvas) {
canvas.addInteractionRegion(mainListener);
Vector2i currentPosition = null;
int currentWidth = 0;
for (UIList<AbstractContextMenuItem> level : menuWidgets) {
if (level.isVisible()) {
if (currentPosition == null) {
currentPosition = new Vector2i(position);
} else {
currentPosition.addX(currentWidth);
}
Rect2i region = Rect2i.createFromMinAndSize(currentPosition, canvas.calculatePreferredSize(level));
double percentageThreshold = 0.9;
if (region.maxY() > canvas.getRegion().height() * percentageThreshold) {
region = Rect2i.createFromMinAndMax(region.minX(), region.minY() - (region.maxY() - canvas.getRegion().height()) - (int) (canvas.getRegion().height() * (1 - percentageThreshold)), region.maxX(), canvas.getRegion().height());
}
currentWidth = canvas.calculatePreferredSize(level).getX() - 8;
canvas.drawWidget(level, region);
}
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class CanvasImpl method processMouseClick.
@Override
public boolean processMouseClick(MouseInput button, Vector2i pos) {
boolean possibleDoubleClick = lastClickPosition.gridDistance(pos) < MAX_DOUBLE_CLICK_DISTANCE && lastClickButton == button && time.getGameTimeInMs() - lastClickTime < DOUBLE_CLICK_TIME;
lastClickPosition.set(pos);
lastClickButton = button;
lastClickTime = time.getGameTimeInMs();
for (InteractionRegion next : mouseOverRegions) {
if (next.region.contains(pos)) {
Vector2i relPos = new Vector2i(pos);
relPos.sub(next.offset);
if (possibleDoubleClick && nuiManager.getFocus() == next.element) {
if (next.listener.onMouseDoubleClick(createDoubleClickEvent(button, relPos))) {
clickedRegion = next;
return true;
}
} else if (next.listener.onMouseClick(createClickEvent(button, relPos))) {
clickedRegion = next;
nuiManager.setFocus(next.element);
return true;
}
}
}
return false;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class CanvasImpl method addInteractionRegion.
@Override
public void addInteractionRegion(InteractionListener listener, UIWidget tooltip, Rect2i region) {
Vector2i offset = state.drawRegion.min();
Rect2i finalRegion = state.cropRegion.intersect(relativeToAbsolute(region));
if (!finalRegion.isEmpty()) {
listener.setFocusManager(nuiManager);
if (state.drawOnTop) {
drawOnTopOperations.add(new DrawInteractionRegionOperation(finalRegion, offset, listener, state.element, tooltip));
} else {
interactionRegions.addLast(new InteractionRegion(finalRegion, offset, listener, state.element, tooltip));
}
}
}
Aggregations