inspector_plugins.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. .. _doc_inspector_plugins:
  2. Inspector plugins
  3. =================
  4. The inspector dock supports custom plugins to create your own widgets for
  5. editing properties. This tutorial explains how to use the
  6. :ref:`class_EditorInspectorPlugin` and :ref:`class_EditorProperty` classes to
  7. write such plugins with the example of creating a custom value editor.
  8. Setup
  9. -----
  10. Just like :ref:`doc_making_plugins`, we start out by making a new plugin,
  11. getting a ``plugin.cfg`` file created, and start with our
  12. :ref:`class_EditorPlugin`. However, instead of using
  13. ``add_custom_node`` or ``add_control_to_dock`` we'll use
  14. ``add_inspector_plugin``.
  15. .. tabs::
  16. .. code-tab:: gdscript GDScript
  17. # MyEditorPlugin.gd
  18. tool extends EditorPlugin
  19. var plugin: EditorInspectorPlugin
  20. func _enter_tree():
  21. # EditorInspectorPlugin is a resource, so we use `new()` instead of `instance()`.
  22. plugin = preload("res://addons/MyPlugin/MyInspectorPlugin.gd").new()
  23. add_inspector_plugin(plugin)
  24. func _exit_tree():
  25. remove_inspector_plugin(plugin)
  26. EditorInspectorPlugin
  27. ---------------------
  28. To actually connect into the Inspector, we create a
  29. :ref:`class_EditorInspectorPlugin` class. This script provides the "hooks" to
  30. the inspector. Thanks to this class, the editor will call the functions within
  31. the EditorInspectorPlugin while it goes through the process of building the UI
  32. for the inspector. The script is used to check if we should enable ourselves for
  33. any :ref:`class_Object` that is currently in the inspector (including any
  34. :ref:`class_Resource` that is embedded!).
  35. Once enabled, EditorInspectorPlugin has methods that allow for adding
  36. :ref:`class_EditorProperty` nodes or just custom :ref:`class_Control` nodes to
  37. the beginning and end of the inspector for that :ref:`class_Object`, or for
  38. overriding or changing existing property editors.
  39. .. tabs::
  40. .. code-tab:: gdscript GDScript
  41. # MyInspectorPlugin.gd
  42. extends EditorInspectorPlugin
  43. func can_handle(object):
  44. # Here you can specify which object types (classes) should be handled by
  45. # this plugin. For example if the plugin is specific to your player
  46. # class defined with `class_name MyPlayer`, you can do:
  47. # `return object is MyPlayer`
  48. # In this example we'll support all objects, so:
  49. return true
  50. func parse_property(object, type, path, hint, hint_text, usage):
  51. # We will handle properties of type integer.
  52. if type == TYPE_INT:
  53. # Register *an instance* of the custom property editor that we'll define next.
  54. add_property_editor(path, MyIntEditor.new())
  55. # We return `true` to notify the inspector that we'll be handling
  56. # this integer property, so it doesn't need to parse other plugins
  57. # (including built-in ones) for an appropriate editor.
  58. return true
  59. else:
  60. return false
  61. EditorProperty
  62. --------------
  63. Next, we define the actual :ref:`class_EditorProperty` custom value editor that
  64. we want instantiated to edit integers. This is a custom :ref:`class_Control` and
  65. we can add any kinds of additional nodes to make advanced widgets to embed in
  66. the inspector.
  67. .. tabs::
  68. .. code-tab:: gdscript GDScript
  69. # MyIntEditor.gd
  70. extends EditorProperty
  71. class_name MyIntEditor
  72. var updating = false
  73. var spin = EditorSpinSlider.new()
  74. func _init():
  75. # We'll add an EditorSpinSlider control, which is the same that the
  76. # inspector already uses for integer and float edition.
  77. # If you want to put the editor below the property name, use:
  78. # `set_bottom_editor(spin)`
  79. # Otherwise to put it inline with the property name use:
  80. add_child(spin)
  81. # To remember focus when selected back:
  82. add_focusable(spin)
  83. # Setup the EditorSpinSlider
  84. spin.set_min(0)
  85. spin.set_max(1000)
  86. spin.connect("value_changed", self, "_spin_changed")
  87. func _spin_changed(value):
  88. if (updating):
  89. return
  90. emit_changed(get_edited_property(), value)
  91. func update_property():
  92. var new_value = get_edited_object()[get_edited_property()]
  93. updating = true
  94. spin.set_value(new_value)
  95. updating = false