1 | /**************************************************************************/ |
2 | /* editor_toaster.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 | #ifndef EDITOR_TOASTER_H |
32 | #define EDITOR_TOASTER_H |
33 | |
34 | #include "core/string/ustring.h" |
35 | #include "core/templates/local_vector.h" |
36 | #include "scene/gui/box_container.h" |
37 | |
38 | class Button; |
39 | class PanelContainer; |
40 | class StyleBoxFlat; |
41 | |
42 | class EditorToaster : public HBoxContainer { |
43 | GDCLASS(EditorToaster, HBoxContainer); |
44 | |
45 | public: |
46 | enum Severity { |
47 | SEVERITY_INFO = 0, |
48 | SEVERITY_WARNING, |
49 | SEVERITY_ERROR, |
50 | }; |
51 | |
52 | private: |
53 | ErrorHandlerList eh; |
54 | |
55 | const int stylebox_radius = 3; |
56 | |
57 | Ref<StyleBoxFlat> info_panel_style_background; |
58 | Ref<StyleBoxFlat> warning_panel_style_background; |
59 | Ref<StyleBoxFlat> error_panel_style_background; |
60 | |
61 | Ref<StyleBoxFlat> info_panel_style_progress; |
62 | Ref<StyleBoxFlat> warning_panel_style_progress; |
63 | Ref<StyleBoxFlat> error_panel_style_progress; |
64 | |
65 | Button *main_button = nullptr; |
66 | PanelContainer *disable_notifications_panel = nullptr; |
67 | Button *disable_notifications_button = nullptr; |
68 | |
69 | VBoxContainer *vbox_container = nullptr; |
70 | const int max_temporary_count = 5; |
71 | struct Toast { |
72 | Severity severity = SEVERITY_INFO; |
73 | |
74 | // Timing. |
75 | real_t duration = -1.0; |
76 | real_t remaining_time = 0.0; |
77 | bool popped = false; |
78 | |
79 | // Messages |
80 | String message; |
81 | String tooltip; |
82 | int count = 0; |
83 | Label *message_label = nullptr; |
84 | Label *message_count_label = nullptr; |
85 | }; |
86 | HashMap<Control *, Toast> toasts; |
87 | |
88 | bool is_processing_error = false; // Makes sure that we don't handle errors that are triggered within the EditorToaster error processing. |
89 | |
90 | const double default_message_duration = 5.0; |
91 | |
92 | 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); |
93 | static void _error_handler_impl(const String &p_file, int p_line, const String &p_error, const String &p_errorexp, bool p_editor_notify, int p_type); |
94 | void _update_vbox_position(); |
95 | void _update_disable_notifications_button(); |
96 | void _auto_hide_or_free_toasts(); |
97 | |
98 | void _draw_button(); |
99 | void _draw_progress(Control *panel); |
100 | |
101 | void _set_notifications_enabled(bool p_enabled); |
102 | void _repop_old(); |
103 | void (String p_message, Severity p_severity, String p_tooltip); |
104 | void _close_button_theme_changed(Control *p_close_button); |
105 | |
106 | protected: |
107 | static EditorToaster *singleton; |
108 | static void _bind_methods(); |
109 | |
110 | void _notification(int p_what); |
111 | |
112 | public: |
113 | static EditorToaster *get_singleton(); |
114 | |
115 | Control *(Control *p_control, Severity p_severity = SEVERITY_INFO, double p_time = 0.0, String p_tooltip = String()); |
116 | void (String p_message, Severity p_severity = SEVERITY_INFO, String p_tooltip = String()); |
117 | void close(Control *p_control); |
118 | |
119 | EditorToaster(); |
120 | ~EditorToaster(); |
121 | }; |
122 | |
123 | VARIANT_ENUM_CAST(EditorToaster::Severity); |
124 | |
125 | #endif // EDITOR_TOASTER_H |
126 | |