inspector_plugins.rst 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. .. note::
  9. To register these scripts as a new editor plugin, you have to create a
  10. ``plugin.cfg`` file as described in :ref:`doc_making_plugins`.
  11. EditorInspectorPlugin
  12. ---------------------
  13. We start by creating a script extending the :ref:`class_EditorInspectorPlugin`
  14. class. This is needed to initialize the plugin and add the custom property
  15. editor that we'll later define.
  16. .. tabs::
  17. .. code-tab:: gdscript GDScript
  18. # MyInspectorPlugin.gd
  19. extends EditorInspectorPlugin
  20. func can_handle(object):
  21. # Here you can specify which object types (classes) should be handled by
  22. # this plugin. For example if the plugin is specific to your player
  23. # class defined with `class_name MyPlayer`, you can do:
  24. # `return object is MyPlayer`
  25. # In this example we'll support all objects, so:
  26. return true
  27. func parse_property(object, type, path, hint, hint_text, usage):
  28. # We will handle properties of type integer.
  29. if type == TYPE_INT:
  30. # Register *an instance* of the custom property editor that we'll define next.
  31. add_custom_property_editor(path, MyIntEditor.new())
  32. # We return `true` to notify the inspector that we'll be handling
  33. # this integer property, so it doesn't need to parse other plugins
  34. # (including built-in ones) for an appropriate editor.
  35. return true
  36. else:
  37. return false
  38. EditorProperty
  39. --------------
  40. Next, we define the actual :ref:`class_EditorProperty` custom value editor that
  41. we want instantiated to edit integers. This is a custom :ref:`class_Control` and
  42. we can add any kinds of additional nodes to make advanced widgets to embed in
  43. the inspector.
  44. .. tabs::
  45. .. code-tab:: gdscript GDScript
  46. # MyIntEditor.gd
  47. extends EditorProperty
  48. class_name MyIntEditor
  49. var updating = false
  50. var spin = EditorSpinSlider.new()
  51. func _init():
  52. # We'll add an EditorSpinSlider control, which is the same that the
  53. # inspector already uses for integer and float edition.
  54. # If you want to put the editor below the property name, use:
  55. # `set_bottom_editor(spin)`
  56. # Otherwise to put it inline with the property name use:
  57. add_child(spin)
  58. # To remember focus when selected back:
  59. add_focusable(spin)
  60. # Setup the EditorSpinSlider
  61. spin.set_min(0)
  62. spin.set_max(1000)
  63. spin.connect("value_changed", self, "_spin_changed")
  64. func _spin_changed(value):
  65. if (updating):
  66. return
  67. emit_changed(get_edited_property(), value)
  68. func update_property():
  69. var new_value = get_edited_object()[get_edited_property()]
  70. updating = true
  71. spin.set_value(new_value)
  72. updating = false