plugin_config_dialog.cpp 13 KB

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