Search in sources :

Example 1 with MouseDevice

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()));
    }
}
Also used : UIStyle(org.terasology.rendering.nui.skin.UIStyle) InputSystem(org.terasology.input.InputSystem) Vector2i(org.terasology.math.geom.Vector2i) SubRegion(org.terasology.rendering.nui.SubRegion) MouseDevice(org.terasology.input.device.MouseDevice)

Aggregations

InputSystem (org.terasology.input.InputSystem)1 MouseDevice (org.terasology.input.device.MouseDevice)1 Vector2i (org.terasology.math.geom.Vector2i)1 SubRegion (org.terasology.rendering.nui.SubRegion)1 UIStyle (org.terasology.rendering.nui.skin.UIStyle)1