editor_validation_panel.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_scale.h"
  32. #include "editor/editor_string_names.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_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  62. valid_messages[p_id] = p_valid_message;
  63. labels[p_id] = label;
  64. }
  65. void EditorValidationPanel::set_accept_button(Button *p_button) {
  66. accept_button = p_button;
  67. }
  68. void EditorValidationPanel::set_update_callback(const Callable &p_callback) {
  69. update_callback = p_callback;
  70. }
  71. void EditorValidationPanel::update() {
  72. ERR_FAIL_COND(update_callback.is_null());
  73. if (pending_update) {
  74. return;
  75. }
  76. pending_update = true;
  77. callable_mp(this, &EditorValidationPanel::_update).call_deferred();
  78. }
  79. void EditorValidationPanel::set_message(int p_id, const String &p_text, MessageType p_type, bool p_auto_prefix) {
  80. ERR_FAIL_COND(!valid_messages.has(p_id));
  81. Label *label = labels[p_id];
  82. if (p_text.is_empty()) {
  83. label->hide();
  84. return;
  85. }
  86. label->show();
  87. if (p_auto_prefix) {
  88. label->set_text(String(U"• ") + p_text);
  89. } else {
  90. label->set_text(p_text);
  91. }
  92. switch (p_type) {
  93. case MSG_OK:
  94. label->add_theme_color_override(SNAME("font_color"), theme_cache.valid_color);
  95. break;
  96. case MSG_WARNING:
  97. label->add_theme_color_override(SNAME("font_color"), theme_cache.warning_color);
  98. break;
  99. case MSG_ERROR:
  100. label->add_theme_color_override(SNAME("font_color"), theme_cache.error_color);
  101. valid = false;
  102. break;
  103. case MSG_INFO:
  104. label->remove_theme_color_override(SNAME("font_color"));
  105. break;
  106. }
  107. }
  108. bool EditorValidationPanel::is_valid() const {
  109. return valid;
  110. }
  111. EditorValidationPanel::EditorValidationPanel() {
  112. set_v_size_flags(SIZE_EXPAND_FILL);
  113. message_container = memnew(VBoxContainer);
  114. add_child(message_container);
  115. }