-
Notifications
You must be signed in to change notification settings - Fork 4
CursorProvider
Important
The Minecraft Cursor Wiki has moved to:
https://fishstiz.github.io/minecraft-cursor-wiki/.
This page will no longer be updated.
The CursorProvider interface provides a more declarative alternative to the element registration system. Elements that implement CursorProvider can directly declare which cursor type to be applied when it is being hovered by the mouse using
CursorProvider#getCursorType(mouseX, mouseY).
Note: The element must be an instance of
Element. It must either be the current screen itself or be accessible from the current screen or from its parent element using theParentElement#children()method.Any container or nested container must be an instance of
ParentElementand accessible via theParentElement#children()method. This accessibility must be maintained through the entire element hierarchy, starting from the current screen down to the deepest nested parent element.For Mojang Mappings:
Element=GuiEventListener, andParentElement=ContainerEventListener
public class MyButton extends ClickableWidget implements CursorProvider {
// ... your other methods
@Override
public CursorType getCursorType(double mouseX, double mouseY) {
return CursorType.POINTER;
}
}If Minecraft Cursor is an optional dependency: Make sure that Minecraft Cursor is loaded before directly implementing CursorProvider. You can make a wrapper or subclass of your element that implements CursorProvider. Then, through a factory method, return either the normal element or the implementation depending if Minecraft Cursor is loaded.
CursorProvider can also be implemented in custom screens because Screen is an Element. The method
CursorProvider#getCursorType(mouseX, mouseY) will be invoked after each render call—assuming your custom Screen
behaves like a typical Screen (it is still only invoked when Element#isMouseOver(mouseX, mouseY) returns true).
This approach offers greater control over the cursor type at the screen level, allowing you to manage cursor behavior based on the state of your elements or other custom conditions.
It also helps you avoid the complexity of dealing with the element/parent element hierarchy, making it easier to handle the cursor type of nested elements.
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;
}
}For more examples, you can take a look at the source code of Minecraft Cursor:
Disclaimer: The actual screens and widgets may not be representative of practical examples, but you can still refer to the
CursorProviderimplementation.
|
Previous: Initialization
|
Next: CursorController Instance
|