editor_log.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************/
  2. /* editor_log.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 "editor_log.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/version.h"
  33. #include "editor_node.h"
  34. #include "editor_scale.h"
  35. #include "scene/gui/center_container.h"
  36. #include "scene/resources/font.h"
  37. void EditorLog::_error_handler(void *p_self, const char *p_func, const char *p_file, int p_line, const char *p_error, const char *p_errorexp, ErrorHandlerType p_type) {
  38. EditorLog *self = (EditorLog *)p_self;
  39. if (self->current != Thread::get_caller_id()) {
  40. return;
  41. }
  42. String err_str;
  43. if (p_errorexp && p_errorexp[0]) {
  44. err_str = p_errorexp;
  45. } else {
  46. err_str = String(p_file) + ":" + itos(p_line) + " - " + String(p_error);
  47. }
  48. if (p_type == ERR_HANDLER_WARNING) {
  49. self->add_message(err_str, MSG_TYPE_WARNING);
  50. } else {
  51. self->add_message(err_str, MSG_TYPE_ERROR);
  52. }
  53. }
  54. void EditorLog::_notification(int p_what) {
  55. if (p_what == NOTIFICATION_ENTER_TREE) {
  56. //button->set_icon(get_icon("Console","EditorIcons"));
  57. log->add_theme_font_override("normal_font", get_theme_font("output_source", "EditorFonts"));
  58. log->add_theme_font_size_override("normal_font_size", get_theme_font_size("output_source_size", "EditorFonts"));
  59. log->add_theme_color_override("selection_color", get_theme_color("accent_color", "Editor") * Color(1, 1, 1, 0.4));
  60. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  61. Ref<Font> df_output_code = get_theme_font("output_source", "EditorFonts");
  62. if (df_output_code.is_valid()) {
  63. if (log != nullptr) {
  64. log->add_theme_font_override("normal_font", get_theme_font("output_source", "EditorFonts"));
  65. log->add_theme_font_size_override("normal_font_size", get_theme_font_size("output_source_size", "EditorFonts"));
  66. log->add_theme_color_override("selection_color", get_theme_color("accent_color", "Editor") * Color(1, 1, 1, 0.4));
  67. }
  68. }
  69. }
  70. }
  71. void EditorLog::_clear_request() {
  72. log->clear();
  73. tool_button->set_icon(Ref<Texture2D>());
  74. }
  75. void EditorLog::_copy_request() {
  76. String text = log->get_selected_text();
  77. if (text == "") {
  78. text = log->get_text();
  79. }
  80. if (text != "") {
  81. DisplayServer::get_singleton()->clipboard_set(text);
  82. }
  83. }
  84. void EditorLog::clear() {
  85. _clear_request();
  86. }
  87. void EditorLog::copy() {
  88. _copy_request();
  89. }
  90. void EditorLog::add_message(const String &p_msg, MessageType p_type) {
  91. bool restore = p_type != MSG_TYPE_STD;
  92. switch (p_type) {
  93. case MSG_TYPE_STD: {
  94. } break;
  95. case MSG_TYPE_ERROR: {
  96. log->push_color(get_theme_color("error_color", "Editor"));
  97. Ref<Texture2D> icon = get_theme_icon("Error", "EditorIcons");
  98. log->add_image(icon);
  99. log->add_text(" ");
  100. tool_button->set_icon(icon);
  101. } break;
  102. case MSG_TYPE_WARNING: {
  103. log->push_color(get_theme_color("warning_color", "Editor"));
  104. Ref<Texture2D> icon = get_theme_icon("Warning", "EditorIcons");
  105. log->add_image(icon);
  106. log->add_text(" ");
  107. tool_button->set_icon(icon);
  108. } break;
  109. case MSG_TYPE_EDITOR: {
  110. // Distinguish editor messages from messages printed by the project
  111. log->push_color(get_theme_color("font_color", "Editor") * Color(1, 1, 1, 0.6));
  112. } break;
  113. }
  114. log->add_text(p_msg);
  115. log->add_newline();
  116. if (restore) {
  117. log->pop();
  118. }
  119. }
  120. void EditorLog::set_tool_button(Button *p_tool_button) {
  121. tool_button = p_tool_button;
  122. }
  123. void EditorLog::_undo_redo_cbk(void *p_self, const String &p_name) {
  124. EditorLog *self = (EditorLog *)p_self;
  125. self->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  126. }
  127. void EditorLog::_bind_methods() {
  128. ADD_SIGNAL(MethodInfo("clear_request"));
  129. ADD_SIGNAL(MethodInfo("copy_request"));
  130. }
  131. EditorLog::EditorLog() {
  132. VBoxContainer *vb = this;
  133. HBoxContainer *hb = memnew(HBoxContainer);
  134. vb->add_child(hb);
  135. title = memnew(Label);
  136. title->set_text(TTR("Output:"));
  137. title->set_h_size_flags(SIZE_EXPAND_FILL);
  138. hb->add_child(title);
  139. copybutton = memnew(Button);
  140. hb->add_child(copybutton);
  141. copybutton->set_text(TTR("Copy"));
  142. copybutton->set_shortcut(ED_SHORTCUT("editor/copy_output", TTR("Copy Selection"), KEY_MASK_CMD | KEY_C));
  143. copybutton->set_shortcut_context(this);
  144. copybutton->connect("pressed", callable_mp(this, &EditorLog::_copy_request));
  145. clearbutton = memnew(Button);
  146. hb->add_child(clearbutton);
  147. clearbutton->set_text(TTR("Clear"));
  148. clearbutton->set_shortcut(ED_SHORTCUT("editor/clear_output", TTR("Clear Output"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_K));
  149. clearbutton->set_shortcut_context(this);
  150. clearbutton->connect("pressed", callable_mp(this, &EditorLog::_clear_request));
  151. log = memnew(RichTextLabel);
  152. log->set_scroll_follow(true);
  153. log->set_selection_enabled(true);
  154. log->set_focus_mode(FOCUS_CLICK);
  155. log->set_custom_minimum_size(Size2(0, 180) * EDSCALE);
  156. log->set_v_size_flags(SIZE_EXPAND_FILL);
  157. log->set_h_size_flags(SIZE_EXPAND_FILL);
  158. vb->add_child(log);
  159. add_message(VERSION_FULL_NAME " (c) 2007-2021 Juan Linietsky, Ariel Manzur & Godot Contributors.");
  160. eh.errfunc = _error_handler;
  161. eh.userdata = this;
  162. add_error_handler(&eh);
  163. current = Thread::get_caller_id();
  164. add_theme_constant_override("separation", get_theme_constant("separation", "VBoxContainer"));
  165. EditorNode::get_undo_redo()->set_commit_notify_callback(_undo_redo_cbk, this);
  166. }
  167. void EditorLog::deinit() {
  168. remove_error_handler(&eh);
  169. }
  170. EditorLog::~EditorLog() {
  171. }