this.attachment = attachment;
}
@Override
public void onDraw(Canvas canvas) {
UIStyle style = canvas.getCurrentStyle();
Vector2i attachmentSize = canvas.calculatePreferredSize(attachment);
attachmentSize.add(style.getMargin().getTotals());
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()));
}
}