plugin_config_dialog.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/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. #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(SNAME("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_language_changed(const int) {
  113. _on_required_text_changed(String());
  114. }
  115. void PluginConfigDialog::_on_required_text_changed(const String &) {
  116. int lang_idx = script_option_edit->get_selected();
  117. String ext = ScriptServer::get_language(lang_idx)->get_extension();
  118. Ref<Texture2D> valid_icon = get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons"));
  119. Ref<Texture2D> invalid_icon = get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  120. // Set variables to assume all is valid
  121. bool is_valid = true;
  122. name_validation->set_texture(valid_icon);
  123. subfolder_validation->set_texture(valid_icon);
  124. script_validation->set_texture(valid_icon);
  125. name_validation->set_tooltip("");
  126. subfolder_validation->set_tooltip("");
  127. script_validation->set_tooltip("");
  128. // Change valid status to invalid depending on conditions.
  129. Vector<String> errors;
  130. if (name_edit->get_text().is_empty()) {
  131. is_valid = false;
  132. name_validation->set_texture(invalid_icon);
  133. name_validation->set_tooltip(TTR("Plugin name cannot not be blank."));
  134. }
  135. if (script_edit->get_text().get_extension() != ext) {
  136. is_valid = false;
  137. script_validation->set_texture(invalid_icon);
  138. script_validation->set_tooltip(vformat(TTR("Script extension must match chosen language extension (.%s)."), ext));
  139. }
  140. if (script_edit->get_text().get_basename().is_empty()) {
  141. is_valid = false;
  142. script_validation->set_texture(invalid_icon);
  143. script_validation->set_tooltip(TTR("Script name cannot not be blank."));
  144. }
  145. if (subfolder_edit->get_text().is_empty()) {
  146. is_valid = false;
  147. subfolder_validation->set_texture(invalid_icon);
  148. subfolder_validation->set_tooltip(TTR("Subfolder cannot be blank."));
  149. } else if (!subfolder_edit->get_text().is_valid_filename()) {
  150. subfolder_validation->set_texture(invalid_icon);
  151. subfolder_validation->set_tooltip(TTR("Subfolder name is not a valid folder name."));
  152. } else {
  153. DirAccessRef dir = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  154. String path = "res://addons/" + subfolder_edit->get_text();
  155. if (dir->dir_exists(path) && !_edit_mode) { // Only show this error if in "create" mode.
  156. is_valid = false;
  157. subfolder_validation->set_texture(invalid_icon);
  158. subfolder_validation->set_tooltip(TTR("Subfolder cannot be one which already exists."));
  159. }
  160. }
  161. get_ok_button()->set_disabled(!is_valid);
  162. }
  163. String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {
  164. return "res://addons/" + p_plugin_name + "/plugin.cfg";
  165. }
  166. void PluginConfigDialog::_notification(int p_what) {
  167. switch (p_what) {
  168. case NOTIFICATION_VISIBILITY_CHANGED: {
  169. if (is_visible()) {
  170. name_edit->grab_focus();
  171. }
  172. } break;
  173. case NOTIFICATION_READY: {
  174. connect("confirmed", callable_mp(this, &PluginConfigDialog::_on_confirmed));
  175. get_cancel_button()->connect("pressed", callable_mp(this, &PluginConfigDialog::_on_cancelled));
  176. } break;
  177. }
  178. }
  179. void PluginConfigDialog::config(const String &p_config_path) {
  180. if (p_config_path.length()) {
  181. Ref<ConfigFile> cf = memnew(ConfigFile);
  182. Error err = cf->load(p_config_path);
  183. ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
  184. name_edit->set_text(cf->get_value("plugin", "name", ""));
  185. subfolder_edit->set_text(p_config_path.get_base_dir().get_basename().get_file());
  186. desc_edit->set_text(cf->get_value("plugin", "description", ""));
  187. author_edit->set_text(cf->get_value("plugin", "author", ""));
  188. version_edit->set_text(cf->get_value("plugin", "version", ""));
  189. script_edit->set_text(cf->get_value("plugin", "script", ""));
  190. _edit_mode = true;
  191. active_edit->hide();
  192. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 2))->hide();
  193. subfolder_edit->hide();
  194. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 2))->hide();
  195. set_title(TTR("Edit a Plugin"));
  196. } else {
  197. _clear_fields();
  198. _edit_mode = false;
  199. active_edit->show();
  200. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 2))->show();
  201. subfolder_edit->show();
  202. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 2))->show();
  203. set_title(TTR("Create a Plugin"));
  204. }
  205. // Simulate text changing so the errors populate.
  206. _on_required_text_changed("");
  207. get_ok_button()->set_disabled(!_edit_mode);
  208. get_ok_button()->set_text(_edit_mode ? TTR("Update") : TTR("Create"));
  209. }
  210. void PluginConfigDialog::_bind_methods() {
  211. ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
  212. }
  213. PluginConfigDialog::PluginConfigDialog() {
  214. get_ok_button()->set_disabled(true);
  215. set_hide_on_ok(true);
  216. VBoxContainer *vbox = memnew(VBoxContainer);
  217. vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  218. vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  219. add_child(vbox);
  220. GridContainer *grid = memnew(GridContainer);
  221. grid->set_columns(3);
  222. vbox->add_child(grid);
  223. // Plugin Name
  224. Label *name_lb = memnew(Label);
  225. name_lb->set_text(TTR("Plugin Name:"));
  226. grid->add_child(name_lb);
  227. name_validation = memnew(TextureRect);
  228. name_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  229. grid->add_child(name_validation);
  230. name_edit = memnew(LineEdit);
  231. name_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  232. name_edit->set_placeholder("MyPlugin");
  233. grid->add_child(name_edit);
  234. // Subfolder
  235. Label *subfolder_lb = memnew(Label);
  236. subfolder_lb->set_text(TTR("Subfolder:"));
  237. grid->add_child(subfolder_lb);
  238. subfolder_validation = memnew(TextureRect);
  239. subfolder_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  240. grid->add_child(subfolder_validation);
  241. subfolder_edit = memnew(LineEdit);
  242. subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
  243. subfolder_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  244. grid->add_child(subfolder_edit);
  245. // Description
  246. Label *desc_lb = memnew(Label);
  247. desc_lb->set_text(TTR("Description:"));
  248. grid->add_child(desc_lb);
  249. Control *desc_spacer = memnew(Control);
  250. grid->add_child(desc_spacer);
  251. desc_edit = memnew(TextEdit);
  252. desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
  253. desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  254. grid->add_child(desc_edit);
  255. // Author
  256. Label *author_lb = memnew(Label);
  257. author_lb->set_text(TTR("Author:"));
  258. grid->add_child(author_lb);
  259. Control *author_spacer = memnew(Control);
  260. grid->add_child(author_spacer);
  261. author_edit = memnew(LineEdit);
  262. author_edit->set_placeholder("Godette");
  263. grid->add_child(author_edit);
  264. // Version
  265. Label *version_lb = memnew(Label);
  266. version_lb->set_text(TTR("Version:"));
  267. grid->add_child(version_lb);
  268. Control *version_spacer = memnew(Control);
  269. grid->add_child(version_spacer);
  270. version_edit = memnew(LineEdit);
  271. version_edit->set_placeholder("1.0");
  272. grid->add_child(version_edit);
  273. // Language dropdown
  274. Label *script_option_lb = memnew(Label);
  275. script_option_lb->set_text(TTR("Language:"));
  276. grid->add_child(script_option_lb);
  277. Control *script_opt_spacer = memnew(Control);
  278. grid->add_child(script_opt_spacer);
  279. script_option_edit = memnew(OptionButton);
  280. int default_lang = 0;
  281. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  282. ScriptLanguage *lang = ScriptServer::get_language(i);
  283. script_option_edit->add_item(lang->get_name());
  284. #ifdef MODULE_GDSCRIPT_ENABLED
  285. if (lang == GDScriptLanguage::get_singleton()) {
  286. default_lang = i;
  287. }
  288. #endif
  289. }
  290. script_option_edit->select(default_lang);
  291. grid->add_child(script_option_edit);
  292. script_option_edit->connect("item_selected", callable_mp(this, &PluginConfigDialog::_on_language_changed));
  293. // Plugin Script Name
  294. Label *script_lb = memnew(Label);
  295. script_lb->set_text(TTR("Script Name:"));
  296. grid->add_child(script_lb);
  297. script_validation = memnew(TextureRect);
  298. script_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  299. grid->add_child(script_validation);
  300. script_edit = memnew(LineEdit);
  301. script_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  302. script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
  303. grid->add_child(script_edit);
  304. // Activate now checkbox
  305. // TODO Make this option work better with languages like C#. Right now, it does not work because the C# project must be compiled first.
  306. Label *active_lb = memnew(Label);
  307. active_lb->set_text(TTR("Activate now?"));
  308. grid->add_child(active_lb);
  309. Control *active_spacer = memnew(Control);
  310. grid->add_child(active_spacer);
  311. active_edit = memnew(CheckBox);
  312. active_edit->set_pressed(true);
  313. grid->add_child(active_edit);
  314. }
  315. PluginConfigDialog::~PluginConfigDialog() {
  316. }