editor_log.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* editor_log.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 "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/dynamic_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 = String::utf8(p_errorexp);
  45. } else {
  46. err_str = String::utf8(p_file) + ":" + itos(p_line) + " - " + String::utf8(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. switch (p_what) {
  56. case NOTIFICATION_ENTER_TREE:
  57. case NOTIFICATION_THEME_CHANGED: {
  58. if (log != nullptr) {
  59. Ref<DynamicFont> df_output_code = get_font("output_source", "EditorFonts");
  60. if (df_output_code.is_valid()) {
  61. log->add_font_override("normal_font", get_font("output_source", "EditorFonts"));
  62. log->add_color_override("selection_color", get_color("accent_color", "Editor") * Color(1, 1, 1, 0.4));
  63. }
  64. }
  65. theme_cache.error_color = get_color("error_color", "Editor");
  66. theme_cache.error_icon = get_icon("Error", "EditorIcons");
  67. theme_cache.warning_color = get_color("warning_color", "Editor");
  68. theme_cache.warning_icon = get_icon("Warning", "EditorIcons");
  69. theme_cache.message_color = get_color("font_color", "Editor") * Color(1, 1, 1, 0.6);
  70. } break;
  71. }
  72. }
  73. void EditorLog::_clear_request() {
  74. log->clear();
  75. tool_button->set_icon(Ref<Texture>());
  76. }
  77. void EditorLog::_copy_request() {
  78. String text = log->get_selected_text();
  79. if (text == "") {
  80. text = log->get_text();
  81. }
  82. if (text != "") {
  83. OS::get_singleton()->set_clipboard(text);
  84. }
  85. }
  86. void EditorLog::clear() {
  87. _clear_request();
  88. }
  89. void EditorLog::copy() {
  90. _copy_request();
  91. }
  92. void EditorLog::add_message(const String &p_msg, MessageType p_type) {
  93. bool restore = p_type != MSG_TYPE_STD;
  94. switch (p_type) {
  95. case MSG_TYPE_STD: {
  96. } break;
  97. case MSG_TYPE_ERROR: {
  98. log->push_color(theme_cache.error_color);
  99. Ref<Texture> icon = theme_cache.error_icon;
  100. log->add_image(icon);
  101. log->add_text(" ");
  102. tool_button->set_icon(icon);
  103. } break;
  104. case MSG_TYPE_WARNING: {
  105. log->push_color(theme_cache.warning_color);
  106. Ref<Texture> icon = theme_cache.warning_icon;
  107. log->add_image(icon);
  108. log->add_text(" ");
  109. tool_button->set_icon(icon);
  110. } break;
  111. case MSG_TYPE_EDITOR: {
  112. // Distinguish editor messages from messages printed by the project
  113. log->push_color(theme_cache.message_color);
  114. } break;
  115. }
  116. log->add_text(p_msg);
  117. log->add_newline();
  118. if (restore) {
  119. log->pop();
  120. }
  121. }
  122. void EditorLog::set_tool_button(ToolButton *p_tool_button) {
  123. tool_button = p_tool_button;
  124. }
  125. void EditorLog::_undo_redo_cbk(void *p_self, const String &p_name) {
  126. EditorLog *self = (EditorLog *)p_self;
  127. self->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  128. }
  129. void EditorLog::_bind_methods() {
  130. ClassDB::bind_method(D_METHOD("_clear_request"), &EditorLog::_clear_request);
  131. ClassDB::bind_method(D_METHOD("_copy_request"), &EditorLog::_copy_request);
  132. ADD_SIGNAL(MethodInfo("clear_request"));
  133. ADD_SIGNAL(MethodInfo("copy_request"));
  134. }
  135. EditorLog::EditorLog() {
  136. VBoxContainer *vb = this;
  137. HBoxContainer *hb = memnew(HBoxContainer);
  138. vb->add_child(hb);
  139. title = memnew(Label);
  140. title->set_text(TTR("Output:"));
  141. title->set_h_size_flags(SIZE_EXPAND_FILL);
  142. hb->add_child(title);
  143. copybutton = memnew(Button);
  144. hb->add_child(copybutton);
  145. copybutton->set_text(TTR("Copy"));
  146. copybutton->set_shortcut(ED_SHORTCUT("editor/copy_output", TTR("Copy Selection"), KEY_MASK_CMD | KEY_C));
  147. copybutton->connect("pressed", this, "_copy_request");
  148. clearbutton = memnew(Button);
  149. hb->add_child(clearbutton);
  150. clearbutton->set_text(TTR("Clear"));
  151. clearbutton->set_shortcut(ED_SHORTCUT("editor/clear_output", TTR("Clear Output"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_K));
  152. clearbutton->connect("pressed", this, "_clear_request");
  153. log = memnew(RichTextLabel);
  154. log->set_scroll_follow(true);
  155. log->set_selection_enabled(true);
  156. log->set_focus_mode(FOCUS_CLICK);
  157. log->set_custom_minimum_size(Size2(0, 180) * EDSCALE);
  158. log->set_v_size_flags(SIZE_EXPAND_FILL);
  159. log->set_h_size_flags(SIZE_EXPAND_FILL);
  160. log->set_deselect_on_focus_loss_enabled(false);
  161. vb->add_child(log);
  162. add_message(VERSION_FULL_NAME " (c) 2007-2022 Juan Linietsky, Ariel Manzur & Godot Contributors.");
  163. eh.errfunc = _error_handler;
  164. eh.userdata = this;
  165. add_error_handler(&eh);
  166. current = Thread::get_caller_id();
  167. add_constant_override("separation", get_constant("separation", "VBoxContainer"));
  168. EditorNode::get_undo_redo()->set_commit_notify_callback(_undo_redo_cbk, this);
  169. }
  170. void EditorLog::deinit() {
  171. remove_error_handler(&eh);
  172. }
  173. EditorLog::~EditorLog() {
  174. }