plugin_config_dialog.cpp 9.7 KB

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