Skip to content

Providing the Cursor from the Element

IMPORTANT

Non-screen Elements must be discoverable through the ParentElement hierarchy.

The CursorProvider interface provides a more declarative alternative to the element registration system.

Elements can implement CursorProvider#getCursorType(double, double) to return the CursorType to be applied when it is detected.

Implementing CursorProvider

MyButton.java
java
public class MyButton extends ClickableWidget implements CursorProvider {
    // ... your other methods

    @Override
    public CursorType getCursorType(double mouseX, double mouseY) {
        return CursorType.POINTER;
    }
}

Implement CursorProvider to your custom Screen

Custom screens can also implement CursorProvider, since Screen is an instance of Element.

This allows you to skip the element detection and offers greater control over the cursor type at the screen level.

If you return CursorType.DEFAULT, Minecraft Cursor will still perform a second pass to detect the cursor type from individual elements.

MyScreen.java
java
public class MyScreen extends Screen implements CursorProvider {
    // ...

    private CustomTableWidget tableWidget;
    private boolean isLoading;
    private boolean isSelecting;

    @Override
    public CursorType getCursorType(double mouseX, double mouseY) {
        if (isLoading) {
            return CustomCursor.WAIT; 
        }
        if (isSelecting) {
            return CustomCursor.CROSSHAIR;
        }
        if (tableWidget.isMouseOver(mouseX, mouseY)) {
            return CustomCursor.CELL;
        }
        return CursorType.DEFAULT;
    }
}

Practical Examples

For more examples, you can take a look at the source code of Minecraft Cursor.

Released under the CC0-1.0 License.