Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/editor_log.h
20952 views
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
31
#pragma once
32
33
#include "core/os/thread.h"
34
#include "editor/docks/editor_dock.h"
35
#include "scene/gui/button.h"
36
#include "scene/gui/line_edit.h"
37
#include "scene/gui/rich_text_label.h"
38
39
class Timer;
40
class UndoRedo;
41
42
class EditorLog : public EditorDock {
43
GDCLASS(EditorLog, EditorDock);
44
45
public:
46
enum MessageType {
47
MSG_TYPE_STD,
48
MSG_TYPE_ERROR,
49
MSG_TYPE_STD_RICH,
50
MSG_TYPE_WARNING,
51
MSG_TYPE_EDITOR,
52
};
53
54
private:
55
struct LogMessage {
56
String text;
57
MessageType type;
58
int count = 1;
59
bool clear = true;
60
61
LogMessage() {}
62
63
LogMessage(const String &p_text, MessageType p_type, bool p_clear) :
64
text(p_text),
65
type(p_type),
66
clear(p_clear) {
67
}
68
};
69
70
struct {
71
Color error_color;
72
Ref<Texture2D> error_icon;
73
74
Color warning_color;
75
Ref<Texture2D> warning_icon;
76
77
Color message_color;
78
} theme_cache;
79
80
// Encapsulates all data and functionality regarding filters.
81
struct LogFilter {
82
private:
83
// Force usage of set method since it has functionality built-in.
84
int message_count = 0;
85
bool active = true;
86
87
public:
88
MessageType type;
89
Button *toggle_button = nullptr;
90
91
void initialize_button(const String &p_name, const String &p_tooltip, Callable p_toggled_callback) {
92
toggle_button = memnew(Button);
93
toggle_button->set_toggle_mode(true);
94
toggle_button->set_pressed(true);
95
toggle_button->set_text(itos(message_count));
96
toggle_button->set_accessibility_name(TTRGET(p_name));
97
toggle_button->set_tooltip_text(TTRGET(p_tooltip));
98
toggle_button->set_focus_mode(FOCUS_ACCESSIBILITY);
99
// When toggled call the callback and pass the MessageType this button is for.
100
toggle_button->connect(SceneStringName(toggled), p_toggled_callback.bind(type));
101
}
102
103
int get_message_count() {
104
return message_count;
105
}
106
107
void set_message_count(int p_count) {
108
message_count = p_count;
109
toggle_button->set_text(itos(message_count));
110
}
111
112
bool is_active() {
113
return active;
114
}
115
116
void set_active(bool p_active) {
117
toggle_button->set_pressed(p_active);
118
active = p_active;
119
}
120
121
LogFilter(MessageType p_type) :
122
type(p_type) {
123
}
124
};
125
126
int line_limit = 10000;
127
128
Vector<LogMessage> messages;
129
// Maps MessageTypes to LogFilters for convenient access and storage (don't need 1 member per filter).
130
HashMap<MessageType, LogFilter *> type_filter_map;
131
132
RichTextLabel *log = nullptr;
133
134
Button *clear_button = nullptr;
135
Button *copy_button = nullptr;
136
137
Button *collapse_button = nullptr;
138
bool collapse = false;
139
140
Button *show_search_button = nullptr;
141
LineEdit *search_box = nullptr;
142
143
// Reusable RichTextLabel for BBCode parsing during search
144
RichTextLabel *bbcode_parser = nullptr;
145
146
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).
147
Timer *save_state_timer = nullptr;
148
149
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);
150
151
ErrorHandlerList eh;
152
153
//void _dragged(const Point2& p_ofs);
154
void _meta_clicked(const String &p_meta);
155
void _clear_request();
156
void _copy_request();
157
static void _undo_redo_cbk(void *p_self, const String &p_name);
158
159
void _rebuild_log();
160
void _add_log_line(LogMessage &p_message, bool p_replace_previous = false);
161
bool _check_display_message(LogMessage &p_message);
162
163
void _set_filter_active(bool p_active, MessageType p_message_type);
164
void _set_search_visible(bool p_visible);
165
void _search_changed(const String &p_text);
166
167
void _process_message(const String &p_msg, MessageType p_type, bool p_clear);
168
void _reset_message_counts();
169
void _set_dock_tab_icon(Ref<Texture2D> p_icon);
170
171
void _set_collapse(bool p_collapse);
172
173
void _start_state_save_timer();
174
void _save_state();
175
void _load_state();
176
177
void _update_theme();
178
void _editor_settings_changed();
179
180
protected:
181
void _notification(int p_what);
182
183
public:
184
void add_message(const String &p_msg, MessageType p_type = MSG_TYPE_STD);
185
void register_undo_redo(UndoRedo *p_undo_redo);
186
void deinit();
187
188
void clear();
189
190
EditorLog();
191
~EditorLog();
192
};
193
194
VARIANT_ENUM_CAST(EditorLog::MessageType);
195
196