editor_toaster.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*************************************************************************/
  2. /* editor_toaster.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef EDITOR_TOASTER_H
  31. #define EDITOR_TOASTER_H
  32. #include "core/string/ustring.h"
  33. #include "core/templates/local_vector.h"
  34. #include "scene/gui/box_container.h"
  35. class Button;
  36. class PanelContainer;
  37. class EditorToaster : public HBoxContainer {
  38. GDCLASS(EditorToaster, HBoxContainer);
  39. public:
  40. enum Severity {
  41. SEVERITY_INFO = 0,
  42. SEVERITY_WARNING,
  43. SEVERITY_ERROR,
  44. };
  45. private:
  46. ErrorHandlerList eh;
  47. const int stylebox_radius = 3;
  48. Ref<StyleBoxFlat> info_panel_style_background;
  49. Ref<StyleBoxFlat> warning_panel_style_background;
  50. Ref<StyleBoxFlat> error_panel_style_background;
  51. Ref<StyleBoxFlat> info_panel_style_progress;
  52. Ref<StyleBoxFlat> warning_panel_style_progress;
  53. Ref<StyleBoxFlat> error_panel_style_progress;
  54. Button *main_button = nullptr;
  55. PanelContainer *disable_notifications_panel = nullptr;
  56. Button *disable_notifications_button = nullptr;
  57. VBoxContainer *vbox_container = nullptr;
  58. const int max_temporary_count = 5;
  59. struct Toast {
  60. Severity severity = SEVERITY_INFO;
  61. // Timing.
  62. real_t duration = -1.0;
  63. real_t remaining_time = 0.0;
  64. bool popped = false;
  65. // Messages
  66. String message;
  67. String tooltip;
  68. int count = 0;
  69. };
  70. Map<Control *, Toast> toasts;
  71. bool is_processing_error = false; // Makes sure that we don't handle errors that are triggered within the EditorToaster error processing.
  72. const double default_message_duration = 5.0;
  73. static void _error_handler(void *p_self, const char *p_func, const char *p_file, int p_line, const char *p_error, const char *p_errorexp, bool p_editor_notify, ErrorHandlerType p_type);
  74. void _update_vbox_position();
  75. void _update_disable_notifications_button();
  76. void _auto_hide_or_free_toasts();
  77. void _draw_button();
  78. void _draw_progress(Control *panel);
  79. void _set_notifications_enabled(bool p_enabled);
  80. void _repop_old();
  81. void _popup_str(String p_message, Severity p_severity, String p_tooltip);
  82. void _close_button_theme_changed(Control *p_close_button);
  83. protected:
  84. static EditorToaster *singleton;
  85. static void _bind_methods();
  86. void _notification(int p_what);
  87. public:
  88. static EditorToaster *get_singleton();
  89. Control *popup(Control *p_control, Severity p_severity = SEVERITY_INFO, double p_time = 0.0, String p_tooltip = String());
  90. void popup_str(String p_message, Severity p_severity = SEVERITY_INFO, String p_tooltip = String());
  91. void close(Control *p_control);
  92. EditorToaster();
  93. ~EditorToaster();
  94. };
  95. VARIANT_ENUM_CAST(EditorToaster::Severity);
  96. #endif // EDITOR_TOASTER_H