plugin_config_dialog.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/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. #include "modules/modules_enabled.gen.h" // For gdscript.
  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("plugin_ready", script.operator->(), active_edit->is_pressed() ? 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_required_text_changed(const String &) {
  113. int lang_idx = script_option_edit->get_selected();
  114. String ext = ScriptServer::get_language(lang_idx)->get_extension();
  115. get_ok()->set_disabled(script_edit->get_text().get_basename().empty() || script_edit->get_text().get_extension() != ext || name_edit->get_text().empty());
  116. }
  117. void PluginConfigDialog::_notification(int p_what) {
  118. switch (p_what) {
  119. case NOTIFICATION_READY: {
  120. connect("confirmed", this, "_on_confirmed");
  121. get_cancel()->connect("pressed", this, "_on_cancelled");
  122. } break;
  123. case NOTIFICATION_POST_POPUP: {
  124. name_edit->grab_focus();
  125. } break;
  126. }
  127. }
  128. void PluginConfigDialog::config(const String &p_config_path) {
  129. if (p_config_path.length()) {
  130. Ref<ConfigFile> cf = memnew(ConfigFile);
  131. Error err = cf->load(p_config_path);
  132. ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
  133. name_edit->set_text(cf->get_value("plugin", "name", ""));
  134. subfolder_edit->set_text(p_config_path.get_base_dir().get_basename().get_file());
  135. desc_edit->set_text(cf->get_value("plugin", "description", ""));
  136. author_edit->set_text(cf->get_value("plugin", "author", ""));
  137. version_edit->set_text(cf->get_value("plugin", "version", ""));
  138. script_edit->set_text(cf->get_value("plugin", "script", ""));
  139. _edit_mode = true;
  140. active_edit->hide();
  141. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 1))->hide();
  142. subfolder_edit->hide();
  143. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 1))->hide();
  144. set_title(TTR("Edit a Plugin"));
  145. } else {
  146. _clear_fields();
  147. _edit_mode = false;
  148. active_edit->show();
  149. Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 1))->show();
  150. subfolder_edit->show();
  151. Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 1))->show();
  152. set_title(TTR("Create a Plugin"));
  153. }
  154. get_ok()->set_disabled(!_edit_mode);
  155. get_ok()->set_text(_edit_mode ? TTR("Update") : TTR("Create"));
  156. }
  157. void PluginConfigDialog::_bind_methods() {
  158. ClassDB::bind_method("_on_required_text_changed", &PluginConfigDialog::_on_required_text_changed);
  159. ClassDB::bind_method("_on_confirmed", &PluginConfigDialog::_on_confirmed);
  160. ClassDB::bind_method("_on_cancelled", &PluginConfigDialog::_on_cancelled);
  161. ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
  162. }
  163. PluginConfigDialog::PluginConfigDialog() {
  164. get_ok()->set_disabled(true);
  165. set_hide_on_ok(true);
  166. GridContainer *grid = memnew(GridContainer);
  167. grid->set_columns(2);
  168. add_child(grid);
  169. Label *name_lb = memnew(Label);
  170. name_lb->set_text(TTR("Plugin Name:"));
  171. grid->add_child(name_lb);
  172. name_edit = memnew(LineEdit);
  173. name_edit->connect("text_changed", this, "_on_required_text_changed");
  174. name_edit->set_placeholder("MyPlugin");
  175. grid->add_child(name_edit);
  176. Label *subfolder_lb = memnew(Label);
  177. subfolder_lb->set_text(TTR("Subfolder:"));
  178. grid->add_child(subfolder_lb);
  179. subfolder_edit = memnew(LineEdit);
  180. subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
  181. grid->add_child(subfolder_edit);
  182. Label *desc_lb = memnew(Label);
  183. desc_lb->set_text(TTR("Description:"));
  184. grid->add_child(desc_lb);
  185. desc_edit = memnew(TextEdit);
  186. desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
  187. desc_edit->set_wrap_enabled(true);
  188. grid->add_child(desc_edit);
  189. Label *author_lb = memnew(Label);
  190. author_lb->set_text(TTR("Author:"));
  191. grid->add_child(author_lb);
  192. author_edit = memnew(LineEdit);
  193. author_edit->set_placeholder("Godette");
  194. grid->add_child(author_edit);
  195. Label *version_lb = memnew(Label);
  196. version_lb->set_text(TTR("Version:"));
  197. grid->add_child(version_lb);
  198. version_edit = memnew(LineEdit);
  199. version_edit->set_placeholder("1.0");
  200. grid->add_child(version_edit);
  201. Label *script_option_lb = memnew(Label);
  202. script_option_lb->set_text(TTR("Language:"));
  203. grid->add_child(script_option_lb);
  204. script_option_edit = memnew(OptionButton);
  205. int default_lang = 0;
  206. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  207. ScriptLanguage *lang = ScriptServer::get_language(i);
  208. script_option_edit->add_item(lang->get_name());
  209. #ifdef MODULE_GDSCRIPT_ENABLED
  210. if (lang == GDScriptLanguage::get_singleton()) {
  211. default_lang = i;
  212. }
  213. #endif
  214. }
  215. script_option_edit->select(default_lang);
  216. grid->add_child(script_option_edit);
  217. Label *script_lb = memnew(Label);
  218. script_lb->set_text(TTR("Script Name:"));
  219. grid->add_child(script_lb);
  220. script_edit = memnew(LineEdit);
  221. script_edit->connect("text_changed", this, "_on_required_text_changed");
  222. script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
  223. grid->add_child(script_edit);
  224. // TODO Make this option work better with languages like C#. Right now, it does not work because the C# project must be compiled first.
  225. Label *active_lb = memnew(Label);
  226. active_lb->set_text(TTR("Activate now?"));
  227. grid->add_child(active_lb);
  228. active_edit = memnew(CheckBox);
  229. active_edit->set_pressed(true);
  230. grid->add_child(active_edit);
  231. }
  232. PluginConfigDialog::~PluginConfigDialog() {
  233. }