editor_log.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* editor_log.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 "editor_log.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/version.h"
  33. #include "editor_node.h"
  34. #include "scene/gui/center_container.h"
  35. #include "scene/resources/dynamic_font.h"
  36. 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) {
  37. EditorLog *self = (EditorLog *)p_self;
  38. if (self->current != Thread::get_caller_id())
  39. return;
  40. String err_str;
  41. if (p_errorexp && p_errorexp[0]) {
  42. err_str = p_errorexp;
  43. } else {
  44. err_str = String(p_file) + ":" + itos(p_line) + " - " + String(p_error);
  45. }
  46. if (p_type == ERR_HANDLER_WARNING) {
  47. self->add_message(err_str, MSG_TYPE_WARNING);
  48. } else {
  49. self->add_message(err_str, MSG_TYPE_ERROR);
  50. }
  51. }
  52. void EditorLog::_notification(int p_what) {
  53. if (p_what == NOTIFICATION_ENTER_TREE) {
  54. //button->set_icon(get_icon("Console","EditorIcons"));
  55. log->add_font_override("normal_font", get_font("output_source", "EditorFonts"));
  56. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  57. Ref<DynamicFont> df_output_code = get_font("output_source", "EditorFonts");
  58. if (df_output_code.is_valid()) {
  59. if (log != NULL) {
  60. log->add_font_override("normal_font", get_font("output_source", "EditorFonts"));
  61. }
  62. }
  63. }
  64. }
  65. void EditorLog::_clear_request() {
  66. log->clear();
  67. tool_button->set_icon(Ref<Texture>());
  68. }
  69. void EditorLog::_copy_request() {
  70. log->selection_copy();
  71. }
  72. void EditorLog::clear() {
  73. _clear_request();
  74. }
  75. void EditorLog::copy() {
  76. _copy_request();
  77. }
  78. void EditorLog::add_message(const String &p_msg, MessageType p_type) {
  79. log->add_newline();
  80. bool restore = p_type != MSG_TYPE_STD;
  81. switch (p_type) {
  82. case MSG_TYPE_STD: {
  83. } break;
  84. case MSG_TYPE_ERROR: {
  85. log->push_color(get_color("error_color", "Editor"));
  86. Ref<Texture> icon = get_icon("Error", "EditorIcons");
  87. log->add_image(icon);
  88. log->add_text(" ");
  89. tool_button->set_icon(icon);
  90. } break;
  91. case MSG_TYPE_WARNING: {
  92. log->push_color(get_color("warning_color", "Editor"));
  93. Ref<Texture> icon = get_icon("Warning", "EditorIcons");
  94. log->add_image(icon);
  95. log->add_text(" ");
  96. tool_button->set_icon(icon);
  97. } break;
  98. }
  99. log->add_text(p_msg);
  100. if (restore)
  101. log->pop();
  102. }
  103. void EditorLog::set_tool_button(ToolButton *p_tool_button) {
  104. tool_button = p_tool_button;
  105. }
  106. void EditorLog::_undo_redo_cbk(void *p_self, const String &p_name) {
  107. EditorLog *self = (EditorLog *)p_self;
  108. self->add_message(p_name);
  109. }
  110. void EditorLog::_bind_methods() {
  111. ClassDB::bind_method(D_METHOD("_clear_request"), &EditorLog::_clear_request);
  112. ClassDB::bind_method(D_METHOD("_copy_request"), &EditorLog::_copy_request);
  113. ADD_SIGNAL(MethodInfo("clear_request"));
  114. ADD_SIGNAL(MethodInfo("copy_request"));
  115. }
  116. EditorLog::EditorLog() {
  117. VBoxContainer *vb = this;
  118. HBoxContainer *hb = memnew(HBoxContainer);
  119. vb->add_child(hb);
  120. title = memnew(Label);
  121. title->set_text(TTR("Output:"));
  122. title->set_h_size_flags(SIZE_EXPAND_FILL);
  123. hb->add_child(title);
  124. copybutton = memnew(Button);
  125. hb->add_child(copybutton);
  126. copybutton->set_text(TTR("Copy"));
  127. copybutton->set_shortcut(ED_SHORTCUT("editor/copy_output", TTR("Copy Selection"), KEY_MASK_CMD | KEY_C));
  128. copybutton->connect("pressed", this, "_copy_request");
  129. clearbutton = memnew(Button);
  130. hb->add_child(clearbutton);
  131. clearbutton->set_text(TTR("Clear"));
  132. clearbutton->set_shortcut(ED_SHORTCUT("editor/clear_output", TTR("Clear Output"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_K));
  133. clearbutton->connect("pressed", this, "_clear_request");
  134. log = memnew(RichTextLabel);
  135. log->set_scroll_follow(true);
  136. log->set_selection_enabled(true);
  137. log->set_focus_mode(FOCUS_CLICK);
  138. log->set_custom_minimum_size(Size2(0, 180) * EDSCALE);
  139. log->set_v_size_flags(SIZE_EXPAND_FILL);
  140. log->set_h_size_flags(SIZE_EXPAND_FILL);
  141. vb->add_child(log);
  142. add_message(VERSION_FULL_NAME " (c) 2007-2019 Juan Linietsky, Ariel Manzur & Godot Contributors.");
  143. eh.errfunc = _error_handler;
  144. eh.userdata = this;
  145. add_error_handler(&eh);
  146. current = Thread::get_caller_id();
  147. add_constant_override("separation", get_constant("separation", "VBoxContainer"));
  148. EditorNode::get_undo_redo()->set_commit_notify_callback(_undo_redo_cbk, this);
  149. }
  150. void EditorLog::deinit() {
  151. remove_error_handler(&eh);
  152. }
  153. EditorLog::~EditorLog() {
  154. }