plugin_config_dialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/io/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. 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/" + _get_subfolder();
  47. if (!_edit_mode) {
  48. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  49. if (d.is_null() || d->make_dir_recursive(path) != OK) {
  50. return;
  51. }
  52. }
  53. int lang_idx = script_option_edit->get_selected();
  54. String ext = ScriptServer::get_language(lang_idx)->get_extension();
  55. String script_name = script_edit->get_text().is_empty() ? _get_subfolder() : script_edit->get_text();
  56. if (script_name.get_extension().is_empty()) {
  57. script_name += "." + ext;
  58. }
  59. String script_path = path.plus_file(script_name);
  60. Ref<ConfigFile> cf = memnew(ConfigFile);
  61. cf->set_value("plugin", "name", name_edit->get_text());
  62. cf->set_value("plugin", "description", desc_edit->get_text());
  63. cf->set_value("plugin", "author", author_edit->get_text());
  64. cf->set_value("plugin", "version", version_edit->get_text());
  65. cf->set_value("plugin", "script", script_name);
  66. cf->save(path.plus_file("plugin.cfg"));
  67. if (!_edit_mode) {
  68. String class_name = script_name.get_basename();
  69. String template_content = "";
  70. Vector<ScriptLanguage::ScriptTemplate> templates = ScriptServer::get_language(lang_idx)->get_built_in_templates("EditorPlugin");
  71. if (!templates.is_empty()) {
  72. template_content = templates[0].content;
  73. }
  74. Ref<Script> script = ScriptServer::get_language(lang_idx)->make_template(template_content, class_name, "EditorPlugin");
  75. script->set_path(script_path, true);
  76. ResourceSaver::save(script);
  77. emit_signal(SNAME("plugin_ready"), script.ptr(), active_edit->is_pressed() ? _to_absolute_plugin_path(_get_subfolder()) : "");
  78. } else {
  79. EditorNode::get_singleton()->get_project_settings()->update_plugins();
  80. }
  81. _clear_fields();
  82. }
  83. void PluginConfigDialog::_on_cancelled() {
  84. _clear_fields();
  85. }
  86. void PluginConfigDialog::_on_language_changed(const int) {
  87. _on_required_text_changed(String());
  88. }
  89. void PluginConfigDialog::_on_required_text_changed(const String &) {
  90. int lang_idx = script_option_edit->get_selected();
  91. String ext = ScriptServer::get_language(lang_idx)->get_extension();
  92. Ref<Texture2D> valid_icon = get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons"));
  93. Ref<Texture2D> invalid_icon = get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  94. // Set variables to assume all is valid
  95. bool is_valid = true;
  96. name_validation->set_texture(valid_icon);
  97. subfolder_validation->set_texture(valid_icon);
  98. script_validation->set_texture(valid_icon);
  99. name_validation->set_tooltip("");
  100. subfolder_validation->set_tooltip("");
  101. script_validation->set_tooltip("");
  102. // Change valid status to invalid depending on conditions.
  103. Vector<String> errors;
  104. if (name_edit->get_text().is_empty()) {
  105. is_valid = false;
  106. name_validation->set_texture(invalid_icon);
  107. name_validation->set_tooltip(TTR("Plugin name cannot be blank."));
  108. }
  109. if ((!script_edit->get_text().get_extension().is_empty() && script_edit->get_text().get_extension() != ext) || script_edit->get_text().ends_with(".")) {
  110. is_valid = false;
  111. script_validation->set_texture(invalid_icon);
  112. script_validation->set_tooltip(vformat(TTR("Script extension must match chosen language extension (.%s)."), ext));
  113. }
  114. if (!subfolder_edit->get_text().is_empty() && !subfolder_edit->get_text().is_valid_filename()) {
  115. is_valid = false;
  116. subfolder_validation->set_texture(invalid_icon);
  117. subfolder_validation->set_tooltip(TTR("Subfolder name is not a valid folder name."));
  118. } else {
  119. String path = "res://addons/" + _get_subfolder();
  120. if (!_edit_mode && DirAccess::exists(path)) { // Only show this error if in "create" mode.
  121. is_valid = false;
  122. subfolder_validation->set_texture(invalid_icon);
  123. subfolder_validation->set_tooltip(TTR("Subfolder cannot be one which already exists."));
  124. }
  125. }
  126. get_ok_button()->set_disabled(!is_valid);
  127. }
  128. String PluginConfigDialog::_get_subfolder() {
  129. return subfolder_edit->get_text().is_empty() ? name_edit->get_text().replace(" ", "_").to_lower() : subfolder_edit->get_text();
  130. }
  131. String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {
  132. return "res://addons/" + p_plugin_name + "/plugin.cfg";
  133. }
  134. void PluginConfigDialog::_notification(int p_what) {
  135. switch (p_what) {
  136. case NOTIFICATION_VISIBILITY_CHANGED: {
  137. if (is_visible()) {
  138. name_edit->grab_focus();
  139. }
  140. } break;
  141. case NOTIFICATION_READY: {
  142. connect("confirmed", callable_mp(this, &PluginConfigDialog::_on_confirmed));
  143. get_cancel_button()->connect("pressed", callable_mp(this, &PluginConfigDialog::_on_cancelled));
  144. } break;
  145. }
  146. }
  147. void PluginConfigDialog::config(const String &p_config_path) {
  148. if (p_config_path.length()) {
  149. Ref<ConfigFile> cf = memnew(ConfigFile);
  150. Error err = cf->load(p_config_path);
  151. ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
  152. name_edit->set_text(cf->get_value("plugin", "name", ""));
  153. subfolder_edit->set_text(p_config_path.get_base_dir().get_basename().get_file());
  154. desc_edit->set_text(cf->get_value("plugin", "description", ""));
  155. author_edit->set_text(cf->get_value("plugin", "author", ""));
  156. version_edit->set_text(cf->get_value("plugin", "version", ""));
  157. script_edit->set_text(cf->get_value("plugin", "script", ""));
  158. _edit_mode = true;
  159. active_edit->hide();
  160. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 2))->hide();
  161. subfolder_edit->hide();
  162. subfolder_validation->hide();
  163. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 2))->hide();
  164. set_title(TTR("Edit a Plugin"));
  165. } else {
  166. _clear_fields();
  167. _edit_mode = false;
  168. active_edit->show();
  169. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 2))->show();
  170. subfolder_edit->show();
  171. subfolder_validation->show();
  172. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 2))->show();
  173. set_title(TTR("Create a Plugin"));
  174. }
  175. // Simulate text changing so the errors populate.
  176. _on_required_text_changed("");
  177. get_ok_button()->set_disabled(!_edit_mode);
  178. set_ok_button_text(_edit_mode ? TTR("Update") : TTR("Create"));
  179. }
  180. void PluginConfigDialog::_bind_methods() {
  181. ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
  182. }
  183. PluginConfigDialog::PluginConfigDialog() {
  184. get_ok_button()->set_disabled(true);
  185. set_hide_on_ok(true);
  186. VBoxContainer *vbox = memnew(VBoxContainer);
  187. vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  188. vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  189. add_child(vbox);
  190. GridContainer *grid = memnew(GridContainer);
  191. grid->set_columns(3);
  192. vbox->add_child(grid);
  193. // Plugin Name
  194. Label *name_lb = memnew(Label);
  195. name_lb->set_text(TTR("Plugin Name:"));
  196. name_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  197. grid->add_child(name_lb);
  198. name_validation = memnew(TextureRect);
  199. name_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  200. grid->add_child(name_validation);
  201. name_edit = memnew(LineEdit);
  202. name_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  203. name_edit->set_placeholder("MyPlugin");
  204. grid->add_child(name_edit);
  205. // Subfolder
  206. Label *subfolder_lb = memnew(Label);
  207. subfolder_lb->set_text(TTR("Subfolder:"));
  208. subfolder_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  209. grid->add_child(subfolder_lb);
  210. subfolder_validation = memnew(TextureRect);
  211. subfolder_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  212. grid->add_child(subfolder_validation);
  213. subfolder_edit = memnew(LineEdit);
  214. subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
  215. subfolder_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  216. grid->add_child(subfolder_edit);
  217. // Description
  218. Label *desc_lb = memnew(Label);
  219. desc_lb->set_text(TTR("Description:"));
  220. desc_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  221. grid->add_child(desc_lb);
  222. Control *desc_spacer = memnew(Control);
  223. grid->add_child(desc_spacer);
  224. desc_edit = memnew(TextEdit);
  225. desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
  226. desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  227. grid->add_child(desc_edit);
  228. // Author
  229. Label *author_lb = memnew(Label);
  230. author_lb->set_text(TTR("Author:"));
  231. author_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  232. grid->add_child(author_lb);
  233. Control *author_spacer = memnew(Control);
  234. grid->add_child(author_spacer);
  235. author_edit = memnew(LineEdit);
  236. author_edit->set_placeholder("Godette");
  237. grid->add_child(author_edit);
  238. // Version
  239. Label *version_lb = memnew(Label);
  240. version_lb->set_text(TTR("Version:"));
  241. version_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  242. grid->add_child(version_lb);
  243. Control *version_spacer = memnew(Control);
  244. grid->add_child(version_spacer);
  245. version_edit = memnew(LineEdit);
  246. version_edit->set_placeholder("1.0");
  247. grid->add_child(version_edit);
  248. // Language dropdown
  249. Label *script_option_lb = memnew(Label);
  250. script_option_lb->set_text(TTR("Language:"));
  251. script_option_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  252. grid->add_child(script_option_lb);
  253. Control *script_opt_spacer = memnew(Control);
  254. grid->add_child(script_opt_spacer);
  255. script_option_edit = memnew(OptionButton);
  256. int default_lang = 0;
  257. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  258. ScriptLanguage *lang = ScriptServer::get_language(i);
  259. script_option_edit->add_item(lang->get_name());
  260. if (lang->get_name() == "GDScript") {
  261. default_lang = i;
  262. }
  263. }
  264. script_option_edit->select(default_lang);
  265. grid->add_child(script_option_edit);
  266. script_option_edit->connect("item_selected", callable_mp(this, &PluginConfigDialog::_on_language_changed));
  267. // Plugin Script Name
  268. Label *script_lb = memnew(Label);
  269. script_lb->set_text(TTR("Script Name:"));
  270. script_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  271. grid->add_child(script_lb);
  272. script_validation = memnew(TextureRect);
  273. script_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  274. grid->add_child(script_validation);
  275. script_edit = memnew(LineEdit);
  276. script_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  277. script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
  278. grid->add_child(script_edit);
  279. // Activate now checkbox
  280. // TODO Make this option work better with languages like C#. Right now, it does not work because the C# project must be compiled first.
  281. Label *active_lb = memnew(Label);
  282. active_lb->set_text(TTR("Activate now?"));
  283. active_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  284. grid->add_child(active_lb);
  285. Control *active_spacer = memnew(Control);
  286. grid->add_child(active_spacer);
  287. active_edit = memnew(CheckBox);
  288. active_edit->set_pressed(true);
  289. grid->add_child(active_edit);
  290. }
  291. PluginConfigDialog::~PluginConfigDialog() {
  292. }