version_control_editor_plugin.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*************************************************************************/
  2. /* version_control_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "version_control_editor_plugin.h"
  31. #include "core/object/script_language.h"
  32. #include "core/os/keyboard.h"
  33. #include "editor/editor_file_system.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "scene/gui/separator.h"
  38. VersionControlEditorPlugin *VersionControlEditorPlugin::singleton = nullptr;
  39. void VersionControlEditorPlugin::_bind_methods() {
  40. ClassDB::bind_method(D_METHOD("popup_vcs_set_up_dialog"), &VersionControlEditorPlugin::popup_vcs_set_up_dialog);
  41. // Used to track the status of files in the staging area
  42. BIND_ENUM_CONSTANT(CHANGE_TYPE_NEW);
  43. BIND_ENUM_CONSTANT(CHANGE_TYPE_MODIFIED);
  44. BIND_ENUM_CONSTANT(CHANGE_TYPE_RENAMED);
  45. BIND_ENUM_CONSTANT(CHANGE_TYPE_DELETED);
  46. BIND_ENUM_CONSTANT(CHANGE_TYPE_TYPECHANGE);
  47. }
  48. void VersionControlEditorPlugin::_create_vcs_metadata_files() {
  49. String dir = "res://";
  50. EditorVCSInterface::create_vcs_metadata_files(EditorVCSInterface::VCSMetadata(metadata_selection->get_selected()), dir);
  51. }
  52. void VersionControlEditorPlugin::_selected_a_vcs(int p_id) {
  53. List<StringName> available_addons = get_available_vcs_names();
  54. const StringName selected_vcs = set_up_choice->get_item_text(p_id);
  55. }
  56. void VersionControlEditorPlugin::_populate_available_vcs_names() {
  57. static bool called = false;
  58. if (!called) {
  59. List<StringName> available_addons = get_available_vcs_names();
  60. for (int i = 0; i < available_addons.size(); i++) {
  61. set_up_choice->add_item(available_addons[i]);
  62. }
  63. called = true;
  64. }
  65. }
  66. VersionControlEditorPlugin *VersionControlEditorPlugin::get_singleton() {
  67. return singleton ? singleton : memnew(VersionControlEditorPlugin);
  68. }
  69. void VersionControlEditorPlugin::popup_vcs_metadata_dialog() {
  70. metadata_dialog->popup_centered();
  71. }
  72. void VersionControlEditorPlugin::popup_vcs_set_up_dialog(const Control *p_gui_base) {
  73. fetch_available_vcs_addon_names();
  74. List<StringName> available_addons = get_available_vcs_names();
  75. if (available_addons.size() >= 1) {
  76. Size2 popup_size = Size2(400, 100);
  77. Size2 window_size = p_gui_base->get_viewport_rect().size;
  78. popup_size.x = MIN(window_size.x * 0.5, popup_size.x);
  79. popup_size.y = MIN(window_size.y * 0.5, popup_size.y);
  80. _populate_available_vcs_names();
  81. set_up_dialog->popup_centered_clamped(popup_size * EDSCALE);
  82. } else {
  83. EditorNode::get_singleton()->show_warning(TTR("No VCS addons are available."), TTR("Error"));
  84. }
  85. }
  86. void VersionControlEditorPlugin::_initialize_vcs() {
  87. register_editor();
  88. ERR_FAIL_COND_MSG(EditorVCSInterface::get_singleton(), EditorVCSInterface::get_singleton()->get_vcs_name() + " is already active");
  89. const int id = set_up_choice->get_selected_id();
  90. String selected_addon = set_up_choice->get_item_text(id);
  91. String path = ScriptServer::get_global_class_path(selected_addon);
  92. Ref<Script> script = ResourceLoader::load(path);
  93. ERR_FAIL_COND_MSG(!script.is_valid(), "VCS Addon path is invalid");
  94. EditorVCSInterface *vcs_interface = memnew(EditorVCSInterface);
  95. ScriptInstance *addon_script_instance = script->instance_create(vcs_interface);
  96. ERR_FAIL_COND_MSG(!addon_script_instance, "Failed to create addon script instance.");
  97. // The addon is attached as a script to the VCS interface as a proxy end-point
  98. vcs_interface->set_script_and_instance(script, addon_script_instance);
  99. EditorVCSInterface::set_singleton(vcs_interface);
  100. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area));
  101. String res_dir = OS::get_singleton()->get_resource_dir();
  102. ERR_FAIL_COND_MSG(!EditorVCSInterface::get_singleton()->initialize(res_dir), "VCS was not initialized");
  103. _refresh_stage_area();
  104. }
  105. void VersionControlEditorPlugin::_send_commit_msg() {
  106. if (EditorVCSInterface::get_singleton()) {
  107. if (staged_files_count == 0) {
  108. commit_status->set_text(TTR("No files added to stage"));
  109. return;
  110. }
  111. EditorVCSInterface::get_singleton()->commit(commit_message->get_text());
  112. commit_message->set_text("");
  113. version_control_dock_button->set_pressed(false);
  114. } else {
  115. WARN_PRINT("No VCS addon is initialized. Select a Version Control Addon from Project menu");
  116. }
  117. _update_commit_status();
  118. _refresh_stage_area();
  119. _clear_file_diff();
  120. }
  121. void VersionControlEditorPlugin::_refresh_stage_area() {
  122. if (EditorVCSInterface::get_singleton()) {
  123. staged_files_count = 0;
  124. clear_stage_area();
  125. Dictionary modified_file_paths = EditorVCSInterface::get_singleton()->get_modified_files_data();
  126. String file_path;
  127. for (int i = 0; i < modified_file_paths.size(); i++) {
  128. file_path = modified_file_paths.get_key_at_index(i);
  129. TreeItem *found = stage_files->search_item_text(file_path, nullptr, true);
  130. if (!found) {
  131. ChangeType change_index = (ChangeType)(int)modified_file_paths.get_value_at_index(i);
  132. String change_text = file_path + " (" + change_type_to_strings[change_index] + ")";
  133. Color &change_color = change_type_to_color[change_index];
  134. TreeItem *new_item = stage_files->create_item(stage_files->get_root());
  135. new_item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  136. new_item->set_text(0, change_text);
  137. new_item->set_metadata(0, file_path);
  138. new_item->set_custom_color(0, change_color);
  139. new_item->set_checked(0, true);
  140. new_item->set_editable(0, true);
  141. } else {
  142. if (found->get_metadata(0) == diff_file_name->get_text()) {
  143. _refresh_file_diff();
  144. }
  145. }
  146. commit_status->set_text(TTR("New changes detected"));
  147. }
  148. } else {
  149. WARN_PRINT("No VCS addon is initialized. Select a Version Control Addon from Project menu.");
  150. }
  151. }
  152. void VersionControlEditorPlugin::_stage_selected() {
  153. if (!EditorVCSInterface::get_singleton()) {
  154. WARN_PRINT("No VCS addon is initialized. Select a Version Control Addon from Project menu");
  155. return;
  156. }
  157. staged_files_count = 0;
  158. TreeItem *root = stage_files->get_root();
  159. if (root) {
  160. TreeItem *file_entry = root->get_first_child();
  161. while (file_entry) {
  162. if (file_entry->is_checked(0)) {
  163. EditorVCSInterface::get_singleton()->stage_file(file_entry->get_metadata(0));
  164. file_entry->set_icon_modulate(0, EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("success_color"), SNAME("Editor")));
  165. staged_files_count++;
  166. } else {
  167. EditorVCSInterface::get_singleton()->unstage_file(file_entry->get_metadata(0));
  168. file_entry->set_icon_modulate(0, EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  169. }
  170. file_entry = file_entry->get_next();
  171. }
  172. }
  173. _update_stage_status();
  174. }
  175. void VersionControlEditorPlugin::_stage_all() {
  176. if (!EditorVCSInterface::get_singleton()) {
  177. WARN_PRINT("No VCS addon is initialized. Select a Version Control Addon from Project menu");
  178. return;
  179. }
  180. staged_files_count = 0;
  181. TreeItem *root = stage_files->get_root();
  182. if (root) {
  183. TreeItem *file_entry = root->get_first_child();
  184. while (file_entry) {
  185. EditorVCSInterface::get_singleton()->stage_file(file_entry->get_metadata(0));
  186. file_entry->set_icon_modulate(0, EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("success_color"), SNAME("Editor")));
  187. file_entry->set_checked(0, true);
  188. staged_files_count++;
  189. file_entry = file_entry->get_next();
  190. }
  191. }
  192. _update_stage_status();
  193. }
  194. void VersionControlEditorPlugin::_view_file_diff() {
  195. version_control_dock_button->set_pressed(true);
  196. String file_path = stage_files->get_selected()->get_metadata(0);
  197. _display_file_diff(file_path);
  198. }
  199. void VersionControlEditorPlugin::_display_file_diff(String p_file_path) {
  200. Array diff_content = EditorVCSInterface::get_singleton()->get_file_diff(p_file_path);
  201. diff_file_name->set_text(p_file_path);
  202. diff->clear();
  203. diff->push_font(EditorNode::get_singleton()->get_gui_base()->get_theme_font(SNAME("source"), SNAME("EditorFonts")));
  204. for (int i = 0; i < diff_content.size(); i++) {
  205. Dictionary line_result = diff_content[i];
  206. if (line_result["status"] == "+") {
  207. diff->push_color(EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("success_color"), SNAME("Editor")));
  208. } else if (line_result["status"] == "-") {
  209. diff->push_color(EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  210. } else {
  211. diff->push_color(EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("font_color"), SNAME("Label")));
  212. }
  213. diff->add_text((String)line_result["content"]);
  214. diff->pop();
  215. }
  216. diff->pop();
  217. }
  218. void VersionControlEditorPlugin::_refresh_file_diff() {
  219. String open_file = diff_file_name->get_text();
  220. if (!open_file.is_empty()) {
  221. _display_file_diff(diff_file_name->get_text());
  222. }
  223. }
  224. void VersionControlEditorPlugin::_clear_file_diff() {
  225. diff->clear();
  226. diff_file_name->set_text("");
  227. version_control_dock_button->set_pressed(false);
  228. }
  229. void VersionControlEditorPlugin::_update_stage_status() {
  230. String status;
  231. if (staged_files_count == 1) {
  232. status = TTR("Stage contains 1 file");
  233. } else {
  234. status = vformat(TTR("Stage contains %d files"), staged_files_count);
  235. }
  236. commit_status->set_text(status);
  237. }
  238. void VersionControlEditorPlugin::_update_commit_status() {
  239. String status;
  240. if (staged_files_count == 1) {
  241. status = TTR("Committed 1 file");
  242. } else {
  243. status = vformat(TTR("Committed %d files"), staged_files_count);
  244. }
  245. commit_status->set_text(status);
  246. staged_files_count = 0;
  247. }
  248. void VersionControlEditorPlugin::_update_commit_button() {
  249. commit_button->set_disabled(commit_message->get_text().strip_edges().is_empty());
  250. }
  251. void VersionControlEditorPlugin::_commit_message_gui_input(const Ref<InputEvent> &p_event) {
  252. if (!commit_message->has_focus()) {
  253. return;
  254. }
  255. if (commit_message->get_text().strip_edges().is_empty()) {
  256. // Do not allow empty commit messages.
  257. return;
  258. }
  259. const Ref<InputEventKey> k = p_event;
  260. if (k.is_valid() && k->is_pressed()) {
  261. if (ED_IS_SHORTCUT("version_control/commit", p_event)) {
  262. if (staged_files_count == 0) {
  263. // Stage all files only when no files were previously staged.
  264. _stage_all();
  265. }
  266. _send_commit_msg();
  267. commit_message->accept_event();
  268. return;
  269. }
  270. }
  271. }
  272. void VersionControlEditorPlugin::register_editor() {
  273. if (!EditorVCSInterface::get_singleton()) {
  274. EditorNode::get_singleton()->add_control_to_dock(EditorNode::DOCK_SLOT_RIGHT_UL, version_commit_dock);
  275. TabContainer *dock_vbc = (TabContainer *)version_commit_dock->get_parent_control();
  276. dock_vbc->set_tab_title(dock_vbc->get_tab_idx_from_control(version_commit_dock), TTR("Commit"));
  277. Button *vc = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Version Control"), version_control_dock);
  278. set_version_control_tool_button(vc);
  279. }
  280. }
  281. void VersionControlEditorPlugin::fetch_available_vcs_addon_names() {
  282. List<StringName> global_classes;
  283. ScriptServer::get_global_class_list(&global_classes);
  284. for (int i = 0; i != global_classes.size(); i++) {
  285. String path = ScriptServer::get_global_class_path(global_classes[i]);
  286. Ref<Script> script = ResourceLoader::load(path);
  287. ERR_FAIL_COND(script.is_null());
  288. if (script->get_instance_base_type() == "EditorVCSInterface") {
  289. available_addons.push_back(global_classes[i]);
  290. }
  291. }
  292. }
  293. void VersionControlEditorPlugin::clear_stage_area() {
  294. stage_files->get_root()->clear_children();
  295. }
  296. void VersionControlEditorPlugin::shut_down() {
  297. if (EditorVCSInterface::get_singleton()) {
  298. if (EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area))) {
  299. EditorFileSystem::get_singleton()->disconnect("filesystem_changed", callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area));
  300. }
  301. EditorVCSInterface::get_singleton()->shut_down();
  302. memdelete(EditorVCSInterface::get_singleton());
  303. EditorVCSInterface::set_singleton(nullptr);
  304. EditorNode::get_singleton()->remove_control_from_dock(version_commit_dock);
  305. EditorNode::get_singleton()->remove_bottom_panel_item(version_control_dock);
  306. }
  307. }
  308. bool VersionControlEditorPlugin::is_vcs_initialized() const {
  309. return EditorVCSInterface::get_singleton() ? EditorVCSInterface::get_singleton()->is_vcs_initialized() : false;
  310. }
  311. const String VersionControlEditorPlugin::get_vcs_name() const {
  312. return EditorVCSInterface::get_singleton() ? EditorVCSInterface::get_singleton()->get_vcs_name() : "";
  313. }
  314. VersionControlEditorPlugin::VersionControlEditorPlugin() {
  315. singleton = this;
  316. staged_files_count = 0;
  317. version_control_actions = memnew(PopupMenu);
  318. metadata_dialog = memnew(ConfirmationDialog);
  319. metadata_dialog->set_title(TTR("Create Version Control Metadata"));
  320. metadata_dialog->set_min_size(Size2(200, 40));
  321. version_control_actions->add_child(metadata_dialog);
  322. VBoxContainer *metadata_vb = memnew(VBoxContainer);
  323. HBoxContainer *metadata_hb = memnew(HBoxContainer);
  324. metadata_hb->set_custom_minimum_size(Size2(200, 20));
  325. Label *l = memnew(Label);
  326. l->set_text(TTR("Create VCS metadata files for:"));
  327. metadata_hb->add_child(l);
  328. metadata_selection = memnew(OptionButton);
  329. metadata_selection->set_custom_minimum_size(Size2(100, 20));
  330. metadata_selection->add_item("None", (int)EditorVCSInterface::VCSMetadata::NONE);
  331. metadata_selection->add_item("Git", (int)EditorVCSInterface::VCSMetadata::GIT);
  332. metadata_selection->select((int)EditorVCSInterface::VCSMetadata::GIT);
  333. metadata_dialog->get_ok_button()->connect("pressed", callable_mp(this, &VersionControlEditorPlugin::_create_vcs_metadata_files));
  334. metadata_hb->add_child(metadata_selection);
  335. metadata_vb->add_child(metadata_hb);
  336. l = memnew(Label);
  337. l->set_text(TTR("Existing VCS metadata files will be overwritten."));
  338. metadata_vb->add_child(l);
  339. metadata_dialog->add_child(metadata_vb);
  340. set_up_dialog = memnew(AcceptDialog);
  341. set_up_dialog->set_title(TTR("Set Up Version Control"));
  342. set_up_dialog->set_min_size(Size2(400, 100));
  343. version_control_actions->add_child(set_up_dialog);
  344. set_up_ok_button = set_up_dialog->get_ok_button();
  345. set_up_ok_button->set_text(TTR("Close"));
  346. set_up_vbc = memnew(VBoxContainer);
  347. set_up_vbc->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  348. set_up_dialog->add_child(set_up_vbc);
  349. set_up_hbc = memnew(HBoxContainer);
  350. set_up_hbc->set_h_size_flags(BoxContainer::SIZE_EXPAND_FILL);
  351. set_up_vbc->add_child(set_up_hbc);
  352. set_up_vcs_status = memnew(RichTextLabel);
  353. set_up_vcs_status->set_text(TTR("VCS Addon is not initialized"));
  354. set_up_vbc->add_child(set_up_vcs_status);
  355. set_up_vcs_label = memnew(Label);
  356. set_up_vcs_label->set_text(TTR("Version Control System"));
  357. set_up_hbc->add_child(set_up_vcs_label);
  358. set_up_choice = memnew(OptionButton);
  359. set_up_choice->set_h_size_flags(HBoxContainer::SIZE_EXPAND_FILL);
  360. set_up_choice->connect("item_selected", callable_mp(this, &VersionControlEditorPlugin::_selected_a_vcs));
  361. set_up_hbc->add_child(set_up_choice);
  362. set_up_init_settings = nullptr;
  363. set_up_init_button = memnew(Button);
  364. set_up_init_button->set_text(TTR("Initialize"));
  365. set_up_init_button->connect("pressed", callable_mp(this, &VersionControlEditorPlugin::_initialize_vcs));
  366. set_up_vbc->add_child(set_up_init_button);
  367. version_commit_dock = memnew(VBoxContainer);
  368. version_commit_dock->set_visible(false);
  369. commit_box_vbc = memnew(VBoxContainer);
  370. commit_box_vbc->set_alignment(VBoxContainer::ALIGNMENT_BEGIN);
  371. commit_box_vbc->set_h_size_flags(VBoxContainer::SIZE_EXPAND_FILL);
  372. commit_box_vbc->set_v_size_flags(VBoxContainer::SIZE_EXPAND_FILL);
  373. version_commit_dock->add_child(commit_box_vbc);
  374. stage_tools = memnew(HSplitContainer);
  375. stage_tools->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN_COLLAPSED);
  376. commit_box_vbc->add_child(stage_tools);
  377. staging_area_label = memnew(Label);
  378. staging_area_label->set_h_size_flags(Label::SIZE_EXPAND_FILL);
  379. staging_area_label->set_text(TTR("Staging area"));
  380. stage_tools->add_child(staging_area_label);
  381. refresh_button = memnew(Button);
  382. refresh_button->set_tooltip(TTR("Detect new changes"));
  383. refresh_button->set_text(TTR("Refresh"));
  384. refresh_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  385. refresh_button->connect("pressed", callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area));
  386. stage_tools->add_child(refresh_button);
  387. stage_files = memnew(Tree);
  388. stage_files->set_h_size_flags(Tree::SIZE_EXPAND_FILL);
  389. stage_files->set_v_size_flags(Tree::SIZE_EXPAND_FILL);
  390. stage_files->set_columns(1);
  391. stage_files->set_column_title(0, TTR("Changes"));
  392. stage_files->set_column_titles_visible(true);
  393. stage_files->set_allow_reselect(true);
  394. stage_files->set_allow_rmb_select(true);
  395. stage_files->set_select_mode(Tree::SelectMode::SELECT_MULTI);
  396. stage_files->set_edit_checkbox_cell_only_when_checkbox_is_pressed(true);
  397. stage_files->connect("cell_selected", callable_mp(this, &VersionControlEditorPlugin::_view_file_diff));
  398. stage_files->create_item();
  399. stage_files->set_hide_root(true);
  400. commit_box_vbc->add_child(stage_files);
  401. change_type_to_strings[CHANGE_TYPE_NEW] = TTR("New");
  402. change_type_to_strings[CHANGE_TYPE_MODIFIED] = TTR("Modified");
  403. change_type_to_strings[CHANGE_TYPE_RENAMED] = TTR("Renamed");
  404. change_type_to_strings[CHANGE_TYPE_DELETED] = TTR("Deleted");
  405. change_type_to_strings[CHANGE_TYPE_TYPECHANGE] = TTR("Typechange");
  406. change_type_to_color[CHANGE_TYPE_NEW] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("success_color"), SNAME("Editor"));
  407. change_type_to_color[CHANGE_TYPE_MODIFIED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  408. change_type_to_color[CHANGE_TYPE_RENAMED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"));
  409. change_type_to_color[CHANGE_TYPE_DELETED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), SNAME("Editor"));
  410. change_type_to_color[CHANGE_TYPE_TYPECHANGE] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("font_color"), SNAME("Editor"));
  411. stage_buttons = memnew(HSplitContainer);
  412. stage_buttons->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN_COLLAPSED);
  413. commit_box_vbc->add_child(stage_buttons);
  414. stage_selected_button = memnew(Button);
  415. stage_selected_button->set_h_size_flags(Button::SIZE_EXPAND_FILL);
  416. stage_selected_button->set_text(TTR("Stage Selected"));
  417. stage_selected_button->connect("pressed", callable_mp(this, &VersionControlEditorPlugin::_stage_selected));
  418. stage_buttons->add_child(stage_selected_button);
  419. stage_all_button = memnew(Button);
  420. stage_all_button->set_text(TTR("Stage All"));
  421. stage_all_button->connect("pressed", callable_mp(this, &VersionControlEditorPlugin::_stage_all));
  422. stage_buttons->add_child(stage_all_button);
  423. commit_box_vbc->add_child(memnew(HSeparator));
  424. commit_message = memnew(TextEdit);
  425. commit_message->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  426. commit_message->set_h_grow_direction(Control::GrowDirection::GROW_DIRECTION_BEGIN);
  427. commit_message->set_v_grow_direction(Control::GrowDirection::GROW_DIRECTION_END);
  428. commit_message->set_custom_minimum_size(Size2(200, 100));
  429. commit_message->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  430. commit_message->connect("text_changed", callable_mp(this, &VersionControlEditorPlugin::_update_commit_button));
  431. commit_message->connect("gui_input", callable_mp(this, &VersionControlEditorPlugin::_commit_message_gui_input));
  432. commit_box_vbc->add_child(commit_message);
  433. ED_SHORTCUT("version_control/commit", TTR("Commit"), KeyModifierMask::CMD | Key::ENTER);
  434. commit_button = memnew(Button);
  435. commit_button->set_text(TTR("Commit Changes"));
  436. commit_button->set_disabled(true);
  437. commit_button->connect("pressed", callable_mp(this, &VersionControlEditorPlugin::_send_commit_msg));
  438. commit_box_vbc->add_child(commit_button);
  439. commit_status = memnew(Label);
  440. commit_status->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  441. commit_box_vbc->add_child(commit_status);
  442. version_control_dock = memnew(PanelContainer);
  443. version_control_dock->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  444. version_control_dock->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  445. version_control_dock->hide();
  446. diff_vbc = memnew(VBoxContainer);
  447. diff_vbc->set_h_size_flags(HBoxContainer::SIZE_FILL);
  448. diff_vbc->set_v_size_flags(HBoxContainer::SIZE_FILL);
  449. version_control_dock->add_child(diff_vbc);
  450. diff_hbc = memnew(HBoxContainer);
  451. diff_hbc->set_h_size_flags(HBoxContainer::SIZE_FILL);
  452. diff_vbc->add_child(diff_hbc);
  453. diff_heading = memnew(Label);
  454. diff_heading->set_text(TTR("Status"));
  455. diff_heading->set_tooltip(TTR("View file diffs before committing them to the latest version"));
  456. diff_hbc->add_child(diff_heading);
  457. diff_file_name = memnew(Label);
  458. diff_file_name->set_text(TTR("No file diff is active"));
  459. diff_file_name->set_h_size_flags(Label::SIZE_EXPAND_FILL);
  460. diff_file_name->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  461. diff_hbc->add_child(diff_file_name);
  462. diff_refresh_button = memnew(Button);
  463. diff_refresh_button->set_tooltip(TTR("Detect changes in file diff"));
  464. diff_refresh_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  465. diff_refresh_button->connect("pressed", callable_mp(this, &VersionControlEditorPlugin::_refresh_file_diff));
  466. diff_hbc->add_child(diff_refresh_button);
  467. diff = memnew(RichTextLabel);
  468. diff->set_h_size_flags(TextEdit::SIZE_EXPAND_FILL);
  469. diff->set_v_size_flags(TextEdit::SIZE_EXPAND_FILL);
  470. diff->set_selection_enabled(true);
  471. diff_vbc->add_child(diff);
  472. }
  473. VersionControlEditorPlugin::~VersionControlEditorPlugin() {
  474. shut_down();
  475. memdelete(version_control_dock);
  476. memdelete(version_commit_dock);
  477. memdelete(version_control_actions);
  478. }