custom_mouse_cursor.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 and Custom Image Hotspot.
  22. .. image:: img/cursor_project_settings.png
  23. Custom Image is the desired image that you would like to set as the mouse cursor.
  24. Custom Hotspot is the point in the image that you would like to use as the cursor's detection point.
  25. .. warning::
  26. The custom image **must** be 256×256 pixels at most. To avoid rendering
  27. issues, sizes lower than or equal to 128×128 are recommended.
  28. On the web platform, the maximum allowed cursor image size is 128×128.
  29. Using a script
  30. --------------
  31. Create a Node and attach the following script.
  32. .. tabs::
  33. .. code-tab:: gdscript GDScript
  34. extends Node
  35. # Load the custom images for the mouse cursor.
  36. var arrow = load("res://arrow.png")
  37. var beam = load("res://beam.png")
  38. func _ready():
  39. # Changes only the arrow shape of the cursor.
  40. # This is similar to changing it in the project settings.
  41. Input.set_custom_mouse_cursor(arrow)
  42. # Changes a specific shape of the cursor (here, the I-beam shape).
  43. Input.set_custom_mouse_cursor(beam, Input.CURSOR_IBEAM)
  44. .. code-tab:: csharp
  45. public override void _Ready()
  46. {
  47. // Load the custom images for the mouse cursor.
  48. var arrow = ResourceLoader.Load("res://arrow.png");
  49. var beam = ResourceLoader.Load("res://beam.png");
  50. // Changes only the arrow shape of the cursor.
  51. // This is similar to changing it in the project settings.
  52. Input.SetCustomMouseCursor(arrow);
  53. // Changes a specific shape of the cursor (here, the I-beam shape).
  54. Input.SetCustomMouseCursor(beam, Input.CursorShape.Ibeam);
  55. }
  56. .. seealso::
  57. Check :ref:`Input.set_custom_mouse_cursor() <class_Input_method_set_custom_mouse_cursor>`'s
  58. documentation for more information on usage and platform-specific caveats.
  59. Demo project
  60. ------------
  61. Find out more by studying this demo project:
  62. https://github.com/guilhermefelipecgs/custom_hardware_cursor
  63. Cursor list
  64. -----------
  65. As documented in the :ref:`Input <class_Input>` class (see the **CursorShape**
  66. enum), there are multiple mouse cursors you can define. Which ones you want to
  67. use depends on your use case.