custom_mouse_cursor.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. .. _doc_custom_mouse_cursor:
  2. Customizing the mouse cursor
  3. ============================
  4. You might want to change the appearance of the mouse cursor in your game in
  5. order to suit the overall design. There are two ways to customize the mouse
  6. cursor:
  7. 1. Using project settings
  8. 2. Using a script
  9. Using project settings is a simpler (but more limited) way to customize the mouse cursor.
  10. The second way is more customizable, but involves scripting:
  11. .. note::
  12. You could display a "software" mouse cursor by hiding the mouse cursor and
  13. moving a Sprite2D to the cursor position in a ``_process()`` method, but
  14. this will add at least one frame of latency compared to an "hardware" mouse
  15. cursor. Therefore, it's recommended to use the approach described here
  16. whenever possible.
  17. If you have to use the "software" approach, consider adding an extrapolation step
  18. to better display the actual mouse input.
  19. Using project settings
  20. ----------------------
  21. Open project settings, go to Display>Mouse Cursor. You will see Custom Image, Custom Image Hotspot
  22. and Tooltip Position Offset.
  23. .. image:: img/cursor_project_settings.webp
  24. Custom Image is the desired image that you would like to set as the mouse cursor.
  25. Custom Hotspot is the point in the image that you would like to use as the cursor's detection point.
  26. .. warning::
  27. The custom image **must** be 256×256 pixels at most. To avoid rendering
  28. issues, sizes lower than or equal to 128×128 are recommended.
  29. On the web platform, the maximum allowed cursor image size is 128×128.
  30. Using a script
  31. --------------
  32. Create a Node and attach the following script.
  33. .. tabs::
  34. .. code-tab:: gdscript GDScript
  35. extends Node
  36. # Load the custom images for the mouse cursor.
  37. var arrow = load("res://arrow.png")
  38. var beam = load("res://beam.png")
  39. func _ready():
  40. # Changes only the arrow shape of the cursor.
  41. # This is similar to changing it in the project settings.
  42. Input.set_custom_mouse_cursor(arrow)
  43. # Changes a specific shape of the cursor (here, the I-beam shape).
  44. Input.set_custom_mouse_cursor(beam, Input.CURSOR_IBEAM)
  45. .. code-tab:: csharp
  46. public override void _Ready()
  47. {
  48. // Load the custom images for the mouse cursor.
  49. var arrow = ResourceLoader.Load("res://arrow.png");
  50. var beam = ResourceLoader.Load("res://beam.png");
  51. // Changes only the arrow shape of the cursor.
  52. // This is similar to changing it in the project settings.
  53. Input.SetCustomMouseCursor(arrow);
  54. // Changes a specific shape of the cursor (here, the I-beam shape).
  55. Input.SetCustomMouseCursor(beam, Input.CursorShape.Ibeam);
  56. }
  57. .. seealso::
  58. Check :ref:`Input.set_custom_mouse_cursor() <class_Input_method_set_custom_mouse_cursor>`'s
  59. documentation for more information on usage and platform-specific caveats.
  60. Demo project
  61. ------------
  62. Find out more by studying this demo project:
  63. https://github.com/guilhermefelipecgs/custom_hardware_cursor
  64. Cursor list
  65. -----------
  66. As documented in the :ref:`Input <class_Input>` class (see the **CursorShape**
  67. enum), there are multiple mouse cursors you can define. Which ones you want to
  68. use depends on your use case.