plugin_config_dialog.cpp 10 KB

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