use of org.terasology.input.device.MouseDevice in project Terasology by MovingBlocks.
the class CursorAttachment method onDraw.
@Override
public void onDraw(Canvas canvas) {
UIStyle style = canvas.getCurrentStyle();
Vector2i attachmentSize = canvas.calculatePreferredSize(attachment);
attachmentSize.add(style.getMargin().getTotals());
// TODO get rid of CoreRegistry. e.g. by updatin a mousePos field with an InteractionListener
MouseDevice mouse = CoreRegistry.get(InputSystem.class).getMouseDevice();
int top;
switch(style.getVerticalAlignment()) {
case TOP:
top = mouse.getPosition().y - attachmentSize.y;
break;
case MIDDLE:
top = mouse.getPosition().y - attachmentSize.y / 2;
break;
default:
top = mouse.getPosition().y + MOUSE_CURSOR_HEIGHT;
break;
}
top = TeraMath.clamp(top, 0, canvas.size().y - attachmentSize.y);
int left;
switch(style.getHorizontalAlignment()) {
case RIGHT:
left = mouse.getPosition().x - attachmentSize.x;
break;
case CENTER:
left = mouse.getPosition().x - attachmentSize.x / 2;
break;
default:
left = mouse.getPosition().x;
break;
}
left = TeraMath.clamp(left, 0, canvas.size().x - attachmentSize.x);
try (SubRegion ignored = canvas.subRegion(Rect2i.createFromMinAndSize(left, top, attachmentSize.x, attachmentSize.y), false)) {
canvas.drawBackground();
canvas.drawWidget(attachment, style.getBackgroundBorder().shrink(canvas.getRegion()));
}
}
Aggregations