editor_log.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // Don't tint the icon even when in "pressed" state.
  88. toggle_button->add_theme_color_override("icon_color_pressed", Color(1, 1, 1, 1));
  89. toggle_button->set_focus_mode(FOCUS_NONE);
  90. // When toggled call the callback and pass the MessageType this button is for.
  91. toggle_button->connect("toggled", p_toggled_callback.bind(type));
  92. }
  93. int get_message_count() {
  94. return message_count;
  95. }
  96. void set_message_count(int p_count) {
  97. message_count = p_count;
  98. toggle_button->set_text(itos(message_count));
  99. }
  100. bool is_active() {
  101. return active;
  102. }
  103. void set_active(bool p_active) {
  104. toggle_button->set_pressed(p_active);
  105. active = p_active;
  106. }
  107. LogFilter(MessageType p_type) :
  108. type(p_type) {
  109. }
  110. };
  111. int line_limit = 10000;
  112. Vector<LogMessage> messages;
  113. // Maps MessageTypes to LogFilters for convenient access and storage (don't need 1 member per filter).
  114. HashMap<MessageType, LogFilter *> type_filter_map;
  115. RichTextLabel *log = nullptr;
  116. Button *clear_button = nullptr;
  117. Button *copy_button = nullptr;
  118. Button *collapse_button = nullptr;
  119. bool collapse = false;
  120. Button *show_search_button = nullptr;
  121. LineEdit *search_box = nullptr;
  122. // Reference to the "Output" button on the toolbar so we can update it's icon when
  123. // Warnings or Errors are encounetered.
  124. Button *tool_button = nullptr;
  125. bool is_loading_state = false; // Used to disable saving requests while loading (some signals from buttons will try trigger a save, which happens during loading).
  126. Timer *save_state_timer = nullptr;
  127. 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);
  128. ErrorHandlerList eh;
  129. Thread::ID current;
  130. //void _dragged(const Point2& p_ofs);
  131. void _meta_clicked(const String &p_meta);
  132. void _clear_request();
  133. void _copy_request();
  134. static void _undo_redo_cbk(void *p_self, const String &p_name);
  135. void _rebuild_log();
  136. void _add_log_line(LogMessage &p_message, bool p_replace_previous = false);
  137. bool _check_display_message(LogMessage &p_message);
  138. void _set_filter_active(bool p_active, MessageType p_message_type);
  139. void _set_search_visible(bool p_visible);
  140. void _search_changed(const String &p_text);
  141. void _process_message(const String &p_msg, MessageType p_type, bool p_clear);
  142. void _reset_message_counts();
  143. void _set_collapse(bool p_collapse);
  144. void _start_state_save_timer();
  145. void _save_state();
  146. void _load_state();
  147. void _update_theme();
  148. void _editor_settings_changed();
  149. protected:
  150. void _notification(int p_what);
  151. public:
  152. void add_message(const String &p_msg, MessageType p_type = MSG_TYPE_STD);
  153. void set_tool_button(Button *p_tool_button);
  154. void register_undo_redo(UndoRedo *p_undo_redo);
  155. void deinit();
  156. void clear();
  157. EditorLog();
  158. ~EditorLog();
  159. };
  160. VARIANT_ENUM_CAST(EditorLog::MessageType);
  161. #endif // EDITOR_LOG_H