editor_log.h 6.4 KB

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