inspector_plugins.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. .. _doc_inspector_plugins:
  2. Inspector plugins
  3. =================
  4. The inspector dock allows you to create custom widgets to edit properties
  5. through plugins. This can be beneficial when working with custom datatypes and
  6. resources, although you can use the feature to change the inspector widgets for
  7. built-in types. You can design custom controls for specific properties, entire
  8. objects, and even separate controls associated with particular datatypes.
  9. This guide explains how to use the :ref:`class_EditorInspectorPlugin` and
  10. :ref:`class_EditorProperty` classes to create a custom interface for integers,
  11. replacing the default behavior with a button that generates random values
  12. between 0 and 99.
  13. .. figure:: img/inspector_plugin_example.png
  14. :align: center
  15. The default behavior on the left and the end result on the right.
  16. Setting up your plugin
  17. ----------------------
  18. Create a new empty plugin to get started.
  19. .. seealso:: See :ref:`doc_making_plugins` guide to set up your new plugin.
  20. Let's assume you've called your plugin folder ``my_inspector_plugin``. If so,
  21. you should end up with a new ``addons/my_inspector_plugin`` folder that contains
  22. two files: ``plugin.cfg`` and ``plugin.gd``.
  23. As before, ``plugin.gd`` is a script extending :ref:`class_EditorPlugin` and you
  24. need to introduce new code for its ``_enter_tree`` and ``_exit_tree`` methods.
  25. To set up your inspector plugin, you must load its script, then create and add
  26. the instance by calling ``add_inspector_plugin()``. If the plugin is disabled,
  27. you should remove the instance you have added by calling
  28. ``remove_inspector_plugin()``.
  29. .. note:: Here, you are loading a script and not a packed scene. Therefore you
  30. should use ``new()`` instead of ``instance()``.
  31. .. tabs::
  32. .. code-tab:: gdscript GDScript
  33. # plugin.gd
  34. tool
  35. extends EditorPlugin
  36. var plugin
  37. func _enter_tree():
  38. plugin = preload("res://addons/my_inspector_plugin/MyInspectorPlugin.gd").new()
  39. add_inspector_plugin(plugin)
  40. func _exit_tree():
  41. remove_inspector_plugin(plugin)
  42. Interacting with the inspector
  43. ------------------------------
  44. To interact with the inspector dock, your ``MyInspectorPlugin.gd`` script must
  45. extend the :ref:`class_EditorInspectorPlugin` class. This class provides several
  46. virtual methods that affect how the inspector handles properties.
  47. To have any effect at all, the script must implement the ``can_handle()``
  48. method. This function is called for each edited :ref:`class_Object` and must
  49. return ``true`` if this plugin should handle the object or its properties.
  50. .. note:: This includes any :ref:`class_Resource` attached to the object.
  51. You can implement four other methods to add controls to the inspector at
  52. specific positions. The ``parse_begin()`` and ``parse_end()`` methods are called
  53. only once at the beginning and the end of parsing for each object, respectively.
  54. They can add controls at the top or bottom of the inspector layout by calling
  55. ``add_custom_control()``.
  56. As the editor parses the object, it calls the ``parse_category()`` and
  57. ``parse_property()`` methods. There, in addition to ``add_custom_control()``,
  58. you can call both ``add_property_editor()`` and
  59. ``add_property_editor_for_multiple_properties()``. Use these last two methods to
  60. specifically add :ref:`class_EditorProperty`-based controls.
  61. .. tabs::
  62. .. code-tab:: gdscript GDScript
  63. # MyInspectorPlugin.gd
  64. extends EditorInspectorPlugin
  65. var RandomIntEditor = preload("res://addons/my_inspector_plugin/RandomIntEditor.gd")
  66. func can_handle(object):
  67. # We support all objects in this example.
  68. return true
  69. func parse_property(object, type, path, hint, hint_text, usage):
  70. # We handle properties of type integer.
  71. if type == TYPE_INT:
  72. # Create an instance of the custom property editor and register
  73. # it to a specific property path.
  74. add_property_editor(path, RandomIntEditor.new())
  75. # Inform the editor to remove the default property editor for
  76. # this property type.
  77. return true
  78. else:
  79. return false
  80. Adding an interface to edit properties
  81. --------------------------------------
  82. The :ref:`class_EditorProperty` class is a special type of :ref:`class_Control`
  83. that can interact with the inspector dock's edited objects. It doesn't display
  84. anything but can house any other control nodes, including complex scenes.
  85. There are three essential parts to the script extending
  86. :ref:`class_EditorProperty`:
  87. 1. You must define the ``_init()`` method to set up the control nodes'
  88. structure.
  89. 2. You should implement the ``update_property()`` to handle changes to the data
  90. from the outside.
  91. 3. A signal must be emitted at some point to inform the inspector that the
  92. control has changed the property using ``emit_changed``.
  93. You can display your custom widget in two ways. Use just the default ``add_child()``
  94. method to display it to the right of the property name, and use ``add_child()``
  95. followed by ``set_bottom_editor()`` to position it below the name.
  96. .. tabs::
  97. .. code-tab:: gdscript GDScript
  98. # RandomIntEditor.gd
  99. extends EditorProperty
  100. # The main control for editing the property.
  101. var property_control = Button.new()
  102. # An internal value of the property.
  103. var current_value = 0
  104. # A guard against internal changes when the property is updated.
  105. var updating = false
  106. func _init():
  107. # Add the control as a direct child of EditorProperty node.
  108. add_child(property_control)
  109. # Make sure the control is able to retain the focus.
  110. add_focusable(property_control)
  111. # Setup the initial state and connect to the signal to track changes.
  112. property_control.text = "Value: " + str(current_value)
  113. property_control.connect("pressed", self, "_on_button_pressed")
  114. func _on_button_pressed():
  115. # Ignore the signal if the property is currently being updated.
  116. if (updating):
  117. return
  118. # Generate a new random integer between 0 and 99.
  119. current_value = randi() % 100
  120. property_control.text = "Value: " + str(current_value)
  121. emit_changed(get_edited_property(), current_value)
  122. func update_property():
  123. # Read the current value from the property.
  124. var new_value = get_edited_object()[get_edited_property()]
  125. if (new_value == current_value):
  126. return
  127. # Update the control with the new value.
  128. updating = true
  129. current_value = new_value
  130. property_control.text = "Value: " + str(current_value)
  131. updating = false
  132. Using the example code above you should be able to make a custom widget that
  133. replaces the default :ref:`class_SpinBox` control for integers with a
  134. :ref:`class_Button` that generates random values.