Path: blob/master/editor/gui/editor_validation_panel.cpp
9898 views
/**************************************************************************/1/* editor_validation_panel.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_validation_panel.h"3132#include "editor/editor_string_names.h"33#include "editor/themes/editor_scale.h"34#include "scene/gui/box_container.h"35#include "scene/gui/button.h"36#include "scene/gui/label.h"3738void EditorValidationPanel::_update() {39for (const KeyValue<int, String> &E : valid_messages) {40set_message(E.key, E.value, MSG_OK);41}4243valid = true;44update_callback.callv(Array());4546if (accept_button) {47accept_button->set_disabled(!valid);48}49pending_update = false;50}5152void EditorValidationPanel::_notification(int p_what) {53switch (p_what) {54case NOTIFICATION_THEME_CHANGED: {55theme_cache.valid_color = get_theme_color(SNAME("success_color"), EditorStringName(Editor));56theme_cache.warning_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));57theme_cache.error_color = get_theme_color(SNAME("error_color"), EditorStringName(Editor));58} break;59}60}6162void EditorValidationPanel::add_line(int p_id, const String &p_valid_message) {63ERR_FAIL_COND(valid_messages.has(p_id));6465Label *label = memnew(Label);66label->set_focus_mode(FOCUS_ACCESSIBILITY);67message_container->add_child(label);68label->set_custom_minimum_size(Size2(200 * EDSCALE, 0));69label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);70label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);7172valid_messages[p_id] = p_valid_message;73labels[p_id] = label;74}7576void EditorValidationPanel::set_accept_button(Button *p_button) {77accept_button = p_button;78}7980void EditorValidationPanel::set_update_callback(const Callable &p_callback) {81update_callback = p_callback;82}8384void EditorValidationPanel::update() {85ERR_FAIL_COND(!update_callback.is_valid());8687if (pending_update) {88return;89}90pending_update = true;91callable_mp(this, &EditorValidationPanel::_update).call_deferred();92}9394void EditorValidationPanel::set_message(int p_id, const String &p_text, MessageType p_type, bool p_auto_prefix) {95ERR_FAIL_COND(!valid_messages.has(p_id));9697Label *label = labels[p_id];98if (p_text.is_empty()) {99label->hide();100return;101}102label->show();103104if (p_auto_prefix) {105label->set_text(String(U"• ") + p_text);106} else {107label->set_text(p_text);108}109110switch (p_type) {111case MSG_OK:112label->add_theme_color_override(SceneStringName(font_color), theme_cache.valid_color);113break;114case MSG_WARNING:115label->add_theme_color_override(SceneStringName(font_color), theme_cache.warning_color);116break;117case MSG_ERROR:118label->add_theme_color_override(SceneStringName(font_color), theme_cache.error_color);119valid = false;120break;121case MSG_INFO:122label->remove_theme_color_override(SceneStringName(font_color));123break;124}125}126127bool EditorValidationPanel::is_valid() const {128return valid;129}130131EditorValidationPanel::EditorValidationPanel() {132set_v_size_flags(SIZE_EXPAND_FILL);133134message_container = memnew(VBoxContainer);135add_child(message_container);136}137138139