plugin_config_dialog.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "editor/editor_scale.h"
  36. #include "editor/project_settings_editor.h"
  37. #include "scene/gui/grid_container.h"
  38. #include "modules/modules_enabled.gen.h"
  39. #ifdef MODULE_GDSCRIPT_ENABLED
  40. #include "modules/gdscript/gdscript.h"
  41. #endif
  42. void PluginConfigDialog::_clear_fields() {
  43. name_edit->set_text("");
  44. subfolder_edit->set_text("");
  45. desc_edit->set_text("");
  46. author_edit->set_text("");
  47. version_edit->set_text("");
  48. script_edit->set_text("");
  49. }
  50. void PluginConfigDialog::_on_confirmed() {
  51. String path = "res://addons/" + subfolder_edit->get_text();
  52. if (!_edit_mode) {
  53. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  54. if (!d || d->make_dir_recursive(path) != OK) {
  55. return;
  56. }
  57. }
  58. Ref<ConfigFile> cf = memnew(ConfigFile);
  59. cf->set_value("plugin", "name", name_edit->get_text());
  60. cf->set_value("plugin", "description", desc_edit->get_text());
  61. cf->set_value("plugin", "author", author_edit->get_text());
  62. cf->set_value("plugin", "version", version_edit->get_text());
  63. cf->set_value("plugin", "script", script_edit->get_text());
  64. cf->save(path.plus_file("plugin.cfg"));
  65. if (!_edit_mode) {
  66. int lang_idx = script_option_edit->get_selected();
  67. String lang_name = ScriptServer::get_language(lang_idx)->get_name();
  68. Ref<Script> script;
  69. // TODO Use script templates. Right now, this code won't add the 'tool' annotation to other languages.
  70. // TODO Better support script languages with named classes (has_named_classes).
  71. // FIXME: It's hacky to have hardcoded access to the GDScript module here.
  72. // The editor code should not have to know what languages are enabled.
  73. #ifdef MODULE_GDSCRIPT_ENABLED
  74. if (lang_name == GDScriptLanguage::get_singleton()->get_name()) {
  75. // Hard-coded GDScript template to keep usability until we use script templates.
  76. Ref<Script> gdscript = memnew(GDScript);
  77. gdscript->set_source_code(
  78. "@tool\n"
  79. "extends EditorPlugin\n"
  80. "\n"
  81. "\n"
  82. "func _enter_tree()%VOID_RETURN%:\n"
  83. "%TS%pass\n"
  84. "\n"
  85. "\n"
  86. "func _exit_tree()%VOID_RETURN%:\n"
  87. "%TS%pass\n");
  88. GDScriptLanguage::get_singleton()->make_template("", "", gdscript);
  89. String script_path = path.plus_file(script_edit->get_text());
  90. gdscript->set_path(script_path);
  91. ResourceSaver::save(script_path, gdscript);
  92. script = gdscript;
  93. } else {
  94. #endif
  95. String script_path = path.plus_file(script_edit->get_text());
  96. String class_name = script_path.get_file().get_basename();
  97. script = ScriptServer::get_language(lang_idx)->get_template(class_name, "EditorPlugin");
  98. script->set_path(script_path);
  99. ResourceSaver::save(script_path, script);
  100. #ifdef MODULE_GDSCRIPT_ENABLED
  101. }
  102. #endif
  103. emit_signal("plugin_ready", script.operator->(), active_edit->is_pressed() ? _to_absolute_plugin_path(subfolder_edit->get_text()) : "");
  104. } else {
  105. EditorNode::get_singleton()->get_project_settings()->update_plugins();
  106. }
  107. _clear_fields();
  108. }
  109. void PluginConfigDialog::_on_cancelled() {
  110. _clear_fields();
  111. }
  112. void PluginConfigDialog::_on_required_text_changed(const String &) {
  113. int lang_idx = script_option_edit->get_selected();
  114. String ext = ScriptServer::get_language(lang_idx)->get_extension();
  115. get_ok_button()->set_disabled(script_edit->get_text().get_basename().is_empty() || script_edit->get_text().get_extension() != ext || name_edit->get_text().is_empty());
  116. }
  117. String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {
  118. return "res://addons/" + p_plugin_name + "/plugin.cfg";
  119. }
  120. void PluginConfigDialog::_notification(int p_what) {
  121. switch (p_what) {
  122. case NOTIFICATION_VISIBILITY_CHANGED: {
  123. if (is_visible()) {
  124. name_edit->grab_focus();
  125. }
  126. } break;
  127. case NOTIFICATION_READY: {
  128. connect("confirmed", callable_mp(this, &PluginConfigDialog::_on_confirmed));
  129. get_cancel_button()->connect("pressed", callable_mp(this, &PluginConfigDialog::_on_cancelled));
  130. } break;
  131. }
  132. }
  133. void PluginConfigDialog::config(const String &p_config_path) {
  134. if (p_config_path.length()) {
  135. Ref<ConfigFile> cf = memnew(ConfigFile);
  136. Error err = cf->load(p_config_path);
  137. ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
  138. name_edit->set_text(cf->get_value("plugin", "name", ""));
  139. subfolder_edit->set_text(p_config_path.get_base_dir().get_basename().get_file());
  140. desc_edit->set_text(cf->get_value("plugin", "description", ""));
  141. author_edit->set_text(cf->get_value("plugin", "author", ""));
  142. version_edit->set_text(cf->get_value("plugin", "version", ""));
  143. script_edit->set_text(cf->get_value("plugin", "script", ""));
  144. _edit_mode = true;
  145. active_edit->hide();
  146. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 1))->hide();
  147. subfolder_edit->hide();
  148. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 1))->hide();
  149. set_title(TTR("Edit a Plugin"));
  150. } else {
  151. _clear_fields();
  152. _edit_mode = false;
  153. active_edit->show();
  154. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 1))->show();
  155. subfolder_edit->show();
  156. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 1))->show();
  157. set_title(TTR("Create a Plugin"));
  158. }
  159. get_ok_button()->set_disabled(!_edit_mode);
  160. get_ok_button()->set_text(_edit_mode ? TTR("Update") : TTR("Create"));
  161. }
  162. void PluginConfigDialog::_bind_methods() {
  163. ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
  164. }
  165. PluginConfigDialog::PluginConfigDialog() {
  166. get_ok_button()->set_disabled(true);
  167. set_hide_on_ok(true);
  168. GridContainer *grid = memnew(GridContainer);
  169. grid->set_columns(2);
  170. add_child(grid);
  171. Label *name_lb = memnew(Label);
  172. name_lb->set_text(TTR("Plugin Name:"));
  173. grid->add_child(name_lb);
  174. name_edit = memnew(LineEdit);
  175. name_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  176. name_edit->set_placeholder("MyPlugin");
  177. grid->add_child(name_edit);
  178. Label *subfolder_lb = memnew(Label);
  179. subfolder_lb->set_text(TTR("Subfolder:"));
  180. grid->add_child(subfolder_lb);
  181. subfolder_edit = memnew(LineEdit);
  182. subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
  183. grid->add_child(subfolder_edit);
  184. Label *desc_lb = memnew(Label);
  185. desc_lb->set_text(TTR("Description:"));
  186. grid->add_child(desc_lb);
  187. desc_edit = memnew(TextEdit);
  188. desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
  189. desc_edit->set_wrap_enabled(true);
  190. grid->add_child(desc_edit);
  191. Label *author_lb = memnew(Label);
  192. author_lb->set_text(TTR("Author:"));
  193. grid->add_child(author_lb);
  194. author_edit = memnew(LineEdit);
  195. author_edit->set_placeholder("Godette");
  196. grid->add_child(author_edit);
  197. Label *version_lb = memnew(Label);
  198. version_lb->set_text(TTR("Version:"));
  199. grid->add_child(version_lb);
  200. version_edit = memnew(LineEdit);
  201. version_edit->set_placeholder("1.0");
  202. grid->add_child(version_edit);
  203. Label *script_option_lb = memnew(Label);
  204. script_option_lb->set_text(TTR("Language:"));
  205. grid->add_child(script_option_lb);
  206. script_option_edit = memnew(OptionButton);
  207. int default_lang = 0;
  208. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  209. ScriptLanguage *lang = ScriptServer::get_language(i);
  210. script_option_edit->add_item(lang->get_name());
  211. #ifdef MODULE_GDSCRIPT_ENABLED
  212. if (lang == GDScriptLanguage::get_singleton()) {
  213. default_lang = i;
  214. }
  215. #endif
  216. }
  217. script_option_edit->select(default_lang);
  218. grid->add_child(script_option_edit);
  219. Label *script_lb = memnew(Label);
  220. script_lb->set_text(TTR("Script Name:"));
  221. grid->add_child(script_lb);
  222. script_edit = memnew(LineEdit);
  223. script_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  224. script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
  225. grid->add_child(script_edit);
  226. // TODO Make this option work better with languages like C#. Right now, it does not work because the C# project must be compiled first.
  227. Label *active_lb = memnew(Label);
  228. active_lb->set_text(TTR("Activate now?"));
  229. grid->add_child(active_lb);
  230. active_edit = memnew(CheckBox);
  231. active_edit->set_pressed(true);
  232. grid->add_child(active_edit);
  233. }
  234. PluginConfigDialog::~PluginConfigDialog() {
  235. }