custom_drawing_in_2d.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Custom Drawing in 2D
  2. ====================
  3. Why?
  4. ----
  5. Godot has nodes to draw sprites, polygons, particles, and all sort of
  6. stuff. For far most cases this is enough, but not always. If something
  7. desired is not supported, and before crying in fear, angst and range
  8. because a node to draw that-specific-something does not exist.. it would
  9. be good to know that it is possible to easily make any 2D node (be it
  10. `Control <https://github.com/okamstudio/godot/wiki/class_control>`__ or
  11. `Node2D <https://github.com/okamstudio/godot/wiki/class_node2d>`__
  12. based) draw custom commands. It is *really* easy to do it too.
  13. But..
  14. -----
  15. Custom drawing manually in a node is *really* useful. Here are some
  16. examples why:
  17. - Drawing shapes or logic that is not handled by nodes (example: making
  18. a node that draws a circle, an image with trails, a special kind of
  19. animated polygon, etc).
  20. - Visualizations that are not that compatible with nodes: (example: a
  21. tetris board). The tetris example uses a custom draw function to draw
  22. the blocks.
  23. - Managing drawing logic of a large amount of simple objects (in the
  24. hundreds of thousands). Using a thousand nodes is probably not nearly
  25. as efficient as drawing, but a thousand of draw calls are cheap.
  26. Check the "Shower of Bullets" demo as example.
  27. - Making a custom UI control. There are plenty of controls available,
  28. but it's easy to run into the need to make a new, custom one.
  29. OK, How?
  30. --------
  31. Add a script to any
  32. `CanvasItem <https://github.com/okamstudio/godot/wiki/class_canvasitem>`__
  33. derived node, like
  34. `Control <https://github.com/okamstudio/godot/wiki/class_control>`__ or
  35. `Node2D <https://github.com/okamstudio/godot/wiki/class_node2d>`__.
  36. Override the \_draw() function.
  37. ::
  38. extends Node2D
  39. func _draw():
  40. #your draw commands here
  41. pass
  42. Draw commands are described in the
  43. `CanvasItem <https://github.com/okamstudio/godot/wiki/class_canvasitem>`__
  44. class reference. There are plenty of them.
  45. Updating
  46. --------
  47. The \_draw() function is only called once, and then the draw commands
  48. are cached and remembered, so further calls are unnecessary.
  49. If re-drawing is required because a state or something else changed,
  50. simply call
  51. `CanvasItem.update() <https://github.com/okamstudio/godot/wiki/class_canvasitem#update>`__
  52. in that same node and a new \_draw() call will happen.
  53. Here is a little more complex example. A texture variable that will be
  54. redrawn if modified:
  55. ::
  56. extends Node2D
  57. var texture setget _set_texture
  58. func _set_texture(value):
  59. #if the texture variable is modified externally,
  60. #this callback is called.
  61. texture=value #texture was changed
  62. update() #update the node
  63. func _draw():
  64. draw_texture(texture,Vector2())
  65. In some cases, it may be desired to draw every frame. For this, just
  66. call update() from the \_process() callback, like this:
  67. ::
  68. extends Node2D
  69. func _draw():
  70. #your draw commands here
  71. pass
  72. func _process(delta):
  73. update()
  74. func _ready():
  75. set_process(true)
  76. OK! This is basically it! Enjoy drawing your own nodes!
  77. Tools
  78. -----
  79. | Drawing your own nodes might also be desired while running them in the
  80. editor, to use as preview or visualization of some feature or
  81. behavior.
  82. | Remember to just use the "tool" keyword at the top of the script
  83. (check the [[GDScript]] reference if you forgot what this does).
  84. *Juan Linietsky, Ariel Manzur, Distributed under the terms of the `CC
  85. By <https://creativecommons.org/licenses/by/3.0/legalcode>`__ license.*