editor_log.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**************************************************************************/
  2. /* editor_log.h */
  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. #pragma once
  31. #include "core/os/thread.h"
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/button.h"
  34. #include "scene/gui/line_edit.h"
  35. #include "scene/gui/rich_text_label.h"
  36. class UndoRedo;
  37. class EditorLog : public HBoxContainer {
  38. GDCLASS(EditorLog, HBoxContainer);
  39. public:
  40. enum MessageType {
  41. MSG_TYPE_STD,
  42. MSG_TYPE_ERROR,
  43. MSG_TYPE_STD_RICH,
  44. MSG_TYPE_WARNING,
  45. MSG_TYPE_EDITOR,
  46. };
  47. private:
  48. struct LogMessage {
  49. String text;
  50. MessageType type;
  51. int count = 1;
  52. bool clear = true;
  53. LogMessage() {}
  54. LogMessage(const String &p_text, MessageType p_type, bool p_clear) :
  55. text(p_text),
  56. type(p_type),
  57. clear(p_clear) {
  58. }
  59. };
  60. struct {
  61. Color error_color;
  62. Ref<Texture2D> error_icon;
  63. Color warning_color;
  64. Ref<Texture2D> warning_icon;
  65. Color message_color;
  66. } theme_cache;
  67. // Encapsulates all data and functionality regarding filters.
  68. struct LogFilter {
  69. private:
  70. // Force usage of set method since it has functionality built-in.
  71. int message_count = 0;
  72. bool active = true;
  73. public:
  74. MessageType type;
  75. Button *toggle_button = nullptr;
  76. void initialize_button(const String &p_name, const String &p_tooltip, Callable p_toggled_callback) {
  77. toggle_button = memnew(Button);
  78. toggle_button->set_toggle_mode(true);
  79. toggle_button->set_pressed(true);
  80. toggle_button->set_text(itos(message_count));
  81. toggle_button->set_accessibility_name(TTRGET(p_name));
  82. toggle_button->set_tooltip_text(TTRGET(p_tooltip));
  83. toggle_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  84. // When toggled call the callback and pass the MessageType this button is for.
  85. toggle_button->connect(SceneStringName(toggled), p_toggled_callback.bind(type));
  86. }
  87. int get_message_count() {
  88. return message_count;
  89. }
  90. void set_message_count(int p_count) {
  91. message_count = p_count;
  92. toggle_button->set_text(itos(message_count));
  93. }
  94. bool is_active() {
  95. return active;
  96. }
  97. void set_active(bool p_active) {
  98. toggle_button->set_pressed(p_active);
  99. active = p_active;
  100. }
  101. LogFilter(MessageType p_type) :
  102. type(p_type) {
  103. }
  104. };
  105. int line_limit = 10000;
  106. Vector<LogMessage> messages;
  107. // Maps MessageTypes to LogFilters for convenient access and storage (don't need 1 member per filter).
  108. HashMap<MessageType, LogFilter *> type_filter_map;
  109. RichTextLabel *log = nullptr;
  110. Button *clear_button = nullptr;
  111. Button *copy_button = nullptr;
  112. Button *collapse_button = nullptr;
  113. bool collapse = false;
  114. Button *show_search_button = nullptr;
  115. LineEdit *search_box = nullptr;
  116. // Reference to the "Output" button on the toolbar so we can update its icon when warnings or errors are encountered.
  117. Button *tool_button = nullptr;
  118. bool is_loading_state = false; // Used to disable saving requests while loading (some signals from buttons will try to trigger a save, which happens during loading).
  119. Timer *save_state_timer = nullptr;
  120. static void _error_handler(void *p_self, const char *p_func, const char *p_file, int p_line, const char *p_error, const char *p_errorexp, bool p_editor_notify, ErrorHandlerType p_type);
  121. ErrorHandlerList eh;
  122. //void _dragged(const Point2& p_ofs);
  123. void _meta_clicked(const String &p_meta);
  124. void _clear_request();
  125. void _copy_request();
  126. static void _undo_redo_cbk(void *p_self, const String &p_name);
  127. void _rebuild_log();
  128. void _add_log_line(LogMessage &p_message, bool p_replace_previous = false);
  129. bool _check_display_message(LogMessage &p_message);
  130. void _set_filter_active(bool p_active, MessageType p_message_type);
  131. void _set_search_visible(bool p_visible);
  132. void _search_changed(const String &p_text);
  133. void _process_message(const String &p_msg, MessageType p_type, bool p_clear);
  134. void _reset_message_counts();
  135. void _set_collapse(bool p_collapse);
  136. void _start_state_save_timer();
  137. void _save_state();
  138. void _load_state();
  139. void _update_theme();
  140. void _editor_settings_changed();
  141. protected:
  142. void _notification(int p_what);
  143. public:
  144. void add_message(const String &p_msg, MessageType p_type = MSG_TYPE_STD);
  145. void set_tool_button(Button *p_tool_button);
  146. void register_undo_redo(UndoRedo *p_undo_redo);
  147. void deinit();
  148. void clear();
  149. EditorLog();
  150. ~EditorLog();
  151. };
  152. VARIANT_ENUM_CAST(EditorLog::MessageType);