plugin_config_dialog.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*************************************************************************/
  2. /* plugin_config_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "plugin_config_dialog.h"
  31. #include "core/io/config_file.h"
  32. #include "core/os/dir_access.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_plugin.h"
  35. #include "modules/gdscript/gdscript.h"
  36. #include "scene/gui/grid_container.h"
  37. void PluginConfigDialog::_clear_fields() {
  38. name_edit->set_text("");
  39. subfolder_edit->set_text("");
  40. desc_edit->set_text("");
  41. author_edit->set_text("");
  42. version_edit->set_text("");
  43. script_edit->set_text("");
  44. }
  45. void PluginConfigDialog::_on_confirmed() {
  46. String path = "res://addons/" + subfolder_edit->get_text();
  47. if (!_edit_mode) {
  48. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  49. if (!d || d->make_dir_recursive(path) != OK)
  50. return;
  51. }
  52. Ref<ConfigFile> cf = memnew(ConfigFile);
  53. cf->set_value("plugin", "name", name_edit->get_text());
  54. cf->set_value("plugin", "description", desc_edit->get_text());
  55. cf->set_value("plugin", "author", author_edit->get_text());
  56. cf->set_value("plugin", "version", version_edit->get_text());
  57. cf->set_value("plugin", "script", script_edit->get_text());
  58. cf->save(path.plus_file("plugin.cfg"));
  59. if (!_edit_mode) {
  60. String type = script_option_edit->get_item_text(script_option_edit->get_selected());
  61. Ref<Script> script;
  62. if (type == GDScriptLanguage::get_singleton()->get_name()) {
  63. Ref<GDScript> gdscript = memnew(GDScript);
  64. gdscript->set_source_code(
  65. "tool\n"
  66. "extends EditorPlugin\n"
  67. "\n"
  68. "func _enter_tree():\n"
  69. "\tpass\n"
  70. "\n"
  71. "func _exit_tree():\n"
  72. "\tpass\n");
  73. String script_path = path.plus_file(script_edit->get_text());
  74. gdscript->set_path(script_path);
  75. ResourceSaver::save(script_path, gdscript);
  76. script = gdscript;
  77. }
  78. //TODO: other languages
  79. emit_signal("plugin_ready", script.operator->(), active_edit->is_pressed() ? subfolder_edit->get_text() : "");
  80. } else {
  81. EditorNode::get_singleton()->get_project_settings()->update_plugins();
  82. }
  83. _clear_fields();
  84. }
  85. void PluginConfigDialog::_on_cancelled() {
  86. _clear_fields();
  87. }
  88. void PluginConfigDialog::_on_required_text_changed(const String &p_text) {
  89. String ext = script_option_edit->get_item_metadata(script_option_edit->get_selected());
  90. get_ok()->set_disabled(script_edit->get_text().get_basename().empty() || script_edit->get_text().get_extension() != ext || name_edit->get_text().empty());
  91. }
  92. void PluginConfigDialog::_notification(int p_what) {
  93. switch (p_what) {
  94. case NOTIFICATION_READY: {
  95. connect("confirmed", this, "_on_confirmed");
  96. get_cancel()->connect("pressed", this, "_on_cancelled");
  97. } break;
  98. }
  99. }
  100. void PluginConfigDialog::config(const String &p_config_path) {
  101. if (p_config_path.length()) {
  102. Ref<ConfigFile> cf = memnew(ConfigFile);
  103. cf->load(p_config_path);
  104. name_edit->set_text(cf->get_value("plugin", "name", ""));
  105. subfolder_edit->set_text(p_config_path.get_base_dir().get_basename().get_file());
  106. desc_edit->set_text(cf->get_value("plugin", "description", ""));
  107. author_edit->set_text(cf->get_value("plugin", "author", ""));
  108. version_edit->set_text(cf->get_value("plugin", "version", ""));
  109. script_edit->set_text(cf->get_value("plugin", "script", ""));
  110. _edit_mode = true;
  111. active_edit->hide();
  112. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 1))->hide();
  113. subfolder_edit->hide();
  114. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 1))->hide();
  115. set_title(TTR("Edit a Plugin"));
  116. } else {
  117. _clear_fields();
  118. _edit_mode = false;
  119. active_edit->show();
  120. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 1))->show();
  121. subfolder_edit->show();
  122. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 1))->show();
  123. set_title(TTR("Create a Plugin"));
  124. }
  125. get_ok()->set_disabled(!_edit_mode);
  126. get_ok()->set_text(_edit_mode ? TTR("Update") : TTR("Create"));
  127. }
  128. void PluginConfigDialog::_bind_methods() {
  129. ClassDB::bind_method("_on_required_text_changed", &PluginConfigDialog::_on_required_text_changed);
  130. ClassDB::bind_method("_on_confirmed", &PluginConfigDialog::_on_confirmed);
  131. ClassDB::bind_method("_on_cancelled", &PluginConfigDialog::_on_cancelled);
  132. ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
  133. }
  134. PluginConfigDialog::PluginConfigDialog() {
  135. get_ok()->set_disabled(true);
  136. set_hide_on_ok(true);
  137. GridContainer *grid = memnew(GridContainer);
  138. grid->set_columns(2);
  139. add_child(grid);
  140. Label *name_lb = memnew(Label);
  141. name_lb->set_text(TTR("Plugin Name:"));
  142. grid->add_child(name_lb);
  143. name_edit = memnew(LineEdit);
  144. name_edit->connect("text_changed", this, "_on_required_text_changed");
  145. name_edit->set_placeholder("MyPlugin");
  146. grid->add_child(name_edit);
  147. Label *subfolder_lb = memnew(Label);
  148. subfolder_lb->set_text(TTR("Subfolder:"));
  149. grid->add_child(subfolder_lb);
  150. subfolder_edit = memnew(LineEdit);
  151. subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
  152. grid->add_child(subfolder_edit);
  153. Label *desc_lb = memnew(Label);
  154. desc_lb->set_text(TTR("Description:"));
  155. grid->add_child(desc_lb);
  156. desc_edit = memnew(TextEdit);
  157. desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
  158. grid->add_child(desc_edit);
  159. Label *author_lb = memnew(Label);
  160. author_lb->set_text(TTR("Author:"));
  161. grid->add_child(author_lb);
  162. author_edit = memnew(LineEdit);
  163. author_edit->set_placeholder("Godette");
  164. grid->add_child(author_edit);
  165. Label *version_lb = memnew(Label);
  166. version_lb->set_text(TTR("Version:"));
  167. grid->add_child(version_lb);
  168. version_edit = memnew(LineEdit);
  169. version_edit->set_placeholder("1.0");
  170. grid->add_child(version_edit);
  171. Label *script_option_lb = memnew(Label);
  172. script_option_lb->set_text(TTR("Language:"));
  173. grid->add_child(script_option_lb);
  174. script_option_edit = memnew(OptionButton);
  175. script_option_edit->add_item(GDScriptLanguage::get_singleton()->get_name());
  176. script_option_edit->set_item_metadata(0, GDScriptLanguage::get_singleton()->get_extension());
  177. script_option_edit->select(0);
  178. //TODO: add other languages
  179. grid->add_child(script_option_edit);
  180. Label *script_lb = memnew(Label);
  181. script_lb->set_text(TTR("Script Name:"));
  182. grid->add_child(script_lb);
  183. script_edit = memnew(LineEdit);
  184. script_edit->connect("text_changed", this, "_on_required_text_changed");
  185. script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
  186. grid->add_child(script_edit);
  187. Label *active_lb = memnew(Label);
  188. active_lb->set_text(TTR("Activate now?"));
  189. grid->add_child(active_lb);
  190. active_edit = memnew(CheckBox);
  191. active_edit->set_pressed(true);
  192. grid->add_child(active_edit);
  193. }
  194. PluginConfigDialog::~PluginConfigDialog() {
  195. }