plugin_config_dialog.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #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. Ref<ConfigFile> cf = memnew(ConfigFile);
  58. cf->set_value("plugin", "name", name_edit->get_text());
  59. cf->set_value("plugin", "description", desc_edit->get_text());
  60. cf->set_value("plugin", "author", author_edit->get_text());
  61. cf->set_value("plugin", "version", version_edit->get_text());
  62. cf->set_value("plugin", "script", script_edit->get_text());
  63. cf->save(path.plus_file("plugin.cfg"));
  64. if (!_edit_mode) {
  65. int lang_idx = script_option_edit->get_selected();
  66. String lang_name = ScriptServer::get_language(lang_idx)->get_name();
  67. Ref<Script> script;
  68. // TODO Use script templates. Right now, this code won't add the 'tool' annotation to other languages.
  69. // TODO Better support script languages with named classes (has_named_classes).
  70. // FIXME: It's hacky to have hardcoded access to the GDScript module here.
  71. // The editor code should not have to know what languages are enabled.
  72. #ifdef MODULE_GDSCRIPT_ENABLED
  73. if (lang_name == GDScriptLanguage::get_singleton()->get_name()) {
  74. // Hard-coded GDScript template to keep usability until we use script templates.
  75. Ref<Script> gdscript = memnew(GDScript);
  76. gdscript->set_source_code(
  77. "tool\n"
  78. "extends EditorPlugin\n"
  79. "\n"
  80. "\n"
  81. "func _enter_tree()%VOID_RETURN%:\n"
  82. "%TS%pass\n"
  83. "\n"
  84. "\n"
  85. "func _exit_tree()%VOID_RETURN%:\n"
  86. "%TS%pass\n");
  87. GDScriptLanguage::get_singleton()->make_template("", "", gdscript);
  88. String script_path = path.plus_file(script_edit->get_text());
  89. gdscript->set_path(script_path);
  90. ResourceSaver::save(script_path, gdscript);
  91. script = gdscript;
  92. } else {
  93. #endif
  94. String script_path = path.plus_file(script_edit->get_text());
  95. String class_name = script_path.get_file().get_basename();
  96. script = ScriptServer::get_language(lang_idx)->get_template(class_name, "EditorPlugin");
  97. script->set_path(script_path);
  98. ResourceSaver::save(script_path, script);
  99. #ifdef MODULE_GDSCRIPT_ENABLED
  100. }
  101. #endif
  102. emit_signal("plugin_ready", script.operator->(), active_edit->is_pressed() ? subfolder_edit->get_text() : "");
  103. } else {
  104. EditorNode::get_singleton()->get_project_settings()->update_plugins();
  105. }
  106. _clear_fields();
  107. }
  108. void PluginConfigDialog::_on_cancelled() {
  109. _clear_fields();
  110. }
  111. void PluginConfigDialog::_on_required_text_changed(const String &) {
  112. int lang_idx = script_option_edit->get_selected();
  113. String ext = ScriptServer::get_language(lang_idx)->get_extension();
  114. get_ok()->set_disabled(script_edit->get_text().get_basename().empty() || script_edit->get_text().get_extension() != ext || name_edit->get_text().empty());
  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()->connect("pressed", callable_mp(this, &PluginConfigDialog::_on_cancelled));
  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. get_ok()->set_disabled(!_edit_mode);
  156. get_ok()->set_text(_edit_mode ? TTR("Update") : TTR("Create"));
  157. }
  158. void PluginConfigDialog::_bind_methods() {
  159. ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
  160. }
  161. PluginConfigDialog::PluginConfigDialog() {
  162. get_ok()->set_disabled(true);
  163. set_hide_on_ok(true);
  164. GridContainer *grid = memnew(GridContainer);
  165. grid->set_columns(2);
  166. add_child(grid);
  167. Label *name_lb = memnew(Label);
  168. name_lb->set_text(TTR("Plugin Name:"));
  169. grid->add_child(name_lb);
  170. name_edit = memnew(LineEdit);
  171. name_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  172. name_edit->set_placeholder("MyPlugin");
  173. grid->add_child(name_edit);
  174. Label *subfolder_lb = memnew(Label);
  175. subfolder_lb->set_text(TTR("Subfolder:"));
  176. grid->add_child(subfolder_lb);
  177. subfolder_edit = memnew(LineEdit);
  178. subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
  179. grid->add_child(subfolder_edit);
  180. Label *desc_lb = memnew(Label);
  181. desc_lb->set_text(TTR("Description:"));
  182. grid->add_child(desc_lb);
  183. desc_edit = memnew(TextEdit);
  184. desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
  185. grid->add_child(desc_edit);
  186. Label *author_lb = memnew(Label);
  187. author_lb->set_text(TTR("Author:"));
  188. grid->add_child(author_lb);
  189. author_edit = memnew(LineEdit);
  190. author_edit->set_placeholder("Godette");
  191. grid->add_child(author_edit);
  192. Label *version_lb = memnew(Label);
  193. version_lb->set_text(TTR("Version:"));
  194. grid->add_child(version_lb);
  195. version_edit = memnew(LineEdit);
  196. version_edit->set_placeholder("1.0");
  197. grid->add_child(version_edit);
  198. Label *script_option_lb = memnew(Label);
  199. script_option_lb->set_text(TTR("Language:"));
  200. grid->add_child(script_option_lb);
  201. script_option_edit = memnew(OptionButton);
  202. int default_lang = 0;
  203. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  204. ScriptLanguage *lang = ScriptServer::get_language(i);
  205. script_option_edit->add_item(lang->get_name());
  206. #ifdef MODULE_GDSCRIPT_ENABLED
  207. if (lang == GDScriptLanguage::get_singleton()) {
  208. default_lang = i;
  209. }
  210. #endif
  211. }
  212. script_option_edit->select(default_lang);
  213. grid->add_child(script_option_edit);
  214. Label *script_lb = memnew(Label);
  215. script_lb->set_text(TTR("Script Name:"));
  216. grid->add_child(script_lb);
  217. script_edit = memnew(LineEdit);
  218. script_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  219. script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
  220. grid->add_child(script_edit);
  221. // TODO Make this option work better with languages like C#. Right now, it does not work because the C# project must be compiled first.
  222. Label *active_lb = memnew(Label);
  223. active_lb->set_text(TTR("Activate now?"));
  224. grid->add_child(active_lb);
  225. active_edit = memnew(CheckBox);
  226. active_edit->set_pressed(true);
  227. grid->add_child(active_edit);
  228. }
  229. PluginConfigDialog::~PluginConfigDialog() {
  230. }