editor_validation_panel.cpp 5.0 KB

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