plugin_config_dialog.cpp 14 KB

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