tblext.gd 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. @tool
  2. extends EditorPlugin
  3. const _tool_json_root_path := "res://.tbl"
  4. func _enter_tree() -> void:
  5. var helper := preload('tblext.gd')
  6. var base_dir: String = helper.resource_path.get_base_dir()
  7. var tools_dir := base_dir.path_join('editor/tools')
  8. var dir := DirAccess.open(tools_dir)
  9. for file in dir.get_files():
  10. if file.get_extension() == 'rb':
  11. var json_path := _tool_json_root_path.path_join(file.get_basename()) + '.json'
  12. if FileAccess.file_exists(json_path):
  13. make_panel(ProjectSettings.globalize_path(json_path), ProjectSettings.globalize_path(dir.get_current_dir().path_join(file)))
  14. func make_panel(tool_json_path:String, tool_script_path:String) -> VBoxContainer:
  15. var panel := VBoxContainer.new()
  16. var tool_json = JSON.parse_string(FileAccess.get_file_as_string(tool_json_path))
  17. var options = tool_json.options
  18. for option in options:
  19. if option.value is String:
  20. var comment := Label.new()
  21. var line_edit := LineEdit.new()
  22. comment.text = option.comment
  23. line_edit.text = option.value
  24. panel.add_child(comment)
  25. panel.add_child(line_edit)
  26. panel.tree_exiting.connect(func():
  27. option.value = line_edit.text
  28. )
  29. elif option.value is int:
  30. var comment := Label.new()
  31. var range := SpinBox.new()
  32. comment.text = option.comment
  33. range.value = option.value
  34. range.step = 1
  35. range.min_value = -65535
  36. range.max_value = 65535
  37. panel.add_child(comment)
  38. panel.add_child(range)
  39. panel.tree_exiting.connect(func():
  40. option.value = int(range.value)
  41. )
  42. elif option.value is float:
  43. var comment := Label.new()
  44. var range := SpinBox.new()
  45. comment.text = option.comment
  46. range.value = option.value
  47. panel.add_child(comment)
  48. panel.add_child(range)
  49. panel.tree_exiting.connect(func():
  50. option.value = range.value
  51. )
  52. elif option.value is bool:
  53. var check_box := CheckBox.new()
  54. check_box.text = option.comment
  55. check_box.button_pressed = option.value
  56. panel.add_child(check_box)
  57. panel.tree_exiting.connect(func():
  58. option.value = check_box.button_pressed
  59. )
  60. else:
  61. print("unsupported option value({0}): {1}".format([option.name, typeof(option.value)]))
  62. var command := Button.new()
  63. command.text = tool_json.command_name
  64. command.button_up.connect(func():
  65. OS.execute('ruby', [tool_script_path])
  66. )
  67. panel.add_child(command)
  68. panel.name = tool_json.title
  69. add_control_to_dock(DOCK_SLOT_LEFT_BR, panel)
  70. panel.tree_exited.connect(func():
  71. var json := FileAccess.open(tool_json_path, FileAccess.WRITE)
  72. json.store_string(JSON.stringify(tool_json, "\t"))
  73. )
  74. tree_exiting.connect(func():
  75. remove_control_from_docks(panel)
  76. panel.queue_free()
  77. )
  78. return panel