Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/editor_validation_panel.cpp
9898 views
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_string_names.h"
34
#include "editor/themes/editor_scale.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
label->set_focus_mode(FOCUS_ACCESSIBILITY);
68
message_container->add_child(label);
69
label->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
70
label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
71
label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
72
73
valid_messages[p_id] = p_valid_message;
74
labels[p_id] = label;
75
}
76
77
void EditorValidationPanel::set_accept_button(Button *p_button) {
78
accept_button = p_button;
79
}
80
81
void EditorValidationPanel::set_update_callback(const Callable &p_callback) {
82
update_callback = p_callback;
83
}
84
85
void EditorValidationPanel::update() {
86
ERR_FAIL_COND(!update_callback.is_valid());
87
88
if (pending_update) {
89
return;
90
}
91
pending_update = true;
92
callable_mp(this, &EditorValidationPanel::_update).call_deferred();
93
}
94
95
void EditorValidationPanel::set_message(int p_id, const String &p_text, MessageType p_type, bool p_auto_prefix) {
96
ERR_FAIL_COND(!valid_messages.has(p_id));
97
98
Label *label = labels[p_id];
99
if (p_text.is_empty()) {
100
label->hide();
101
return;
102
}
103
label->show();
104
105
if (p_auto_prefix) {
106
label->set_text(String(U"• ") + p_text);
107
} else {
108
label->set_text(p_text);
109
}
110
111
switch (p_type) {
112
case MSG_OK:
113
label->add_theme_color_override(SceneStringName(font_color), theme_cache.valid_color);
114
break;
115
case MSG_WARNING:
116
label->add_theme_color_override(SceneStringName(font_color), theme_cache.warning_color);
117
break;
118
case MSG_ERROR:
119
label->add_theme_color_override(SceneStringName(font_color), theme_cache.error_color);
120
valid = false;
121
break;
122
case MSG_INFO:
123
label->remove_theme_color_override(SceneStringName(font_color));
124
break;
125
}
126
}
127
128
bool EditorValidationPanel::is_valid() const {
129
return valid;
130
}
131
132
EditorValidationPanel::EditorValidationPanel() {
133
set_v_size_flags(SIZE_EXPAND_FILL);
134
135
message_container = memnew(VBoxContainer);
136
add_child(message_container);
137
}
138
139