1 | /**************************************************************************/ |
2 | /* editor_validation_panel.cpp */ |
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 | #include "editor_validation_panel.h" |
32 | |
33 | #include "editor/editor_scale.h" |
34 | #include "editor/editor_string_names.h" |
35 | #include "scene/gui/box_container.h" |
36 | #include "scene/gui/button.h" |
37 | #include "scene/gui/label.h" |
38 | |
39 | void EditorValidationPanel::_update() { |
40 | for (const KeyValue<int, String> &E : valid_messages) { |
41 | set_message(E.key, E.value, MSG_OK); |
42 | } |
43 | |
44 | valid = true; |
45 | update_callback.callv(Array()); |
46 | |
47 | if (accept_button) { |
48 | accept_button->set_disabled(!valid); |
49 | } |
50 | pending_update = false; |
51 | } |
52 | |
53 | void EditorValidationPanel::_notification(int p_what) { |
54 | switch (p_what) { |
55 | case NOTIFICATION_THEME_CHANGED: { |
56 | theme_cache.valid_color = get_theme_color(SNAME("success_color" ), EditorStringName(Editor)); |
57 | theme_cache.warning_color = get_theme_color(SNAME("warning_color" ), EditorStringName(Editor)); |
58 | theme_cache.error_color = get_theme_color(SNAME("error_color" ), EditorStringName(Editor)); |
59 | } break; |
60 | } |
61 | } |
62 | |
63 | void EditorValidationPanel::add_line(int p_id, const String &p_valid_message) { |
64 | ERR_FAIL_COND(valid_messages.has(p_id)); |
65 | |
66 | Label *label = memnew(Label); |
67 | message_container->add_child(label); |
68 | label->set_custom_minimum_size(Size2(200 * EDSCALE, 0)); |
69 | label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART); |
70 | |
71 | valid_messages[p_id] = p_valid_message; |
72 | labels[p_id] = label; |
73 | } |
74 | |
75 | void EditorValidationPanel::set_accept_button(Button *p_button) { |
76 | accept_button = p_button; |
77 | } |
78 | |
79 | void EditorValidationPanel::set_update_callback(const Callable &p_callback) { |
80 | update_callback = p_callback; |
81 | } |
82 | |
83 | void EditorValidationPanel::update() { |
84 | ERR_FAIL_COND(update_callback.is_null()); |
85 | |
86 | if (pending_update) { |
87 | return; |
88 | } |
89 | pending_update = true; |
90 | callable_mp(this, &EditorValidationPanel::_update).call_deferred(); |
91 | } |
92 | |
93 | void EditorValidationPanel::set_message(int p_id, const String &p_text, MessageType p_type, bool p_auto_prefix) { |
94 | ERR_FAIL_COND(!valid_messages.has(p_id)); |
95 | |
96 | Label *label = labels[p_id]; |
97 | if (p_text.is_empty()) { |
98 | label->hide(); |
99 | return; |
100 | } |
101 | label->show(); |
102 | |
103 | if (p_auto_prefix) { |
104 | label->set_text(String(U"• " ) + p_text); |
105 | } else { |
106 | label->set_text(p_text); |
107 | } |
108 | |
109 | switch (p_type) { |
110 | case MSG_OK: |
111 | label->add_theme_color_override(SNAME("font_color" ), theme_cache.valid_color); |
112 | break; |
113 | case MSG_WARNING: |
114 | label->add_theme_color_override(SNAME("font_color" ), theme_cache.warning_color); |
115 | break; |
116 | case MSG_ERROR: |
117 | label->add_theme_color_override(SNAME("font_color" ), theme_cache.error_color); |
118 | valid = false; |
119 | break; |
120 | case MSG_INFO: |
121 | label->remove_theme_color_override(SNAME("font_color" )); |
122 | break; |
123 | } |
124 | } |
125 | |
126 | bool EditorValidationPanel::is_valid() const { |
127 | return valid; |
128 | } |
129 | |
130 | EditorValidationPanel::EditorValidationPanel() { |
131 | set_v_size_flags(SIZE_EXPAND_FILL); |
132 | |
133 | message_container = memnew(VBoxContainer); |
134 | add_child(message_container); |
135 | } |
136 | |