editor_toaster.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*************************************************************************/
  2. /* editor_toaster.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "editor/editor_node.h"
  31. #include "editor/editor_scale.h"
  32. #include "scene/gui/label.h"
  33. #include "scene/gui/panel_container.h"
  34. #include "editor_toaster.h"
  35. EditorToaster *EditorToaster::singleton;
  36. void EditorToaster::_notification(int p_what) {
  37. switch (p_what) {
  38. case NOTIFICATION_INTERNAL_PROCESS: {
  39. double delta = get_process_delta_time();
  40. // Check if one element is hovered, if so, don't elapse time.
  41. bool hovered = false;
  42. for (const KeyValue<Control *, Toast> &element : toasts) {
  43. if (Rect2(Vector2(), element.key->get_size()).has_point(element.key->get_local_mouse_position())) {
  44. hovered = true;
  45. break;
  46. }
  47. }
  48. // Elapses the time and remove toasts if needed.
  49. if (!hovered) {
  50. for (const KeyValue<Control *, Toast> &element : toasts) {
  51. if (!element.value.popped || element.value.duration <= 0) {
  52. continue;
  53. }
  54. toasts[element.key].remaining_time -= delta;
  55. if (toasts[element.key].remaining_time < 0) {
  56. close(element.key);
  57. }
  58. element.key->update();
  59. }
  60. } else {
  61. // Reset the timers when hovered.
  62. for (const KeyValue<Control *, Toast> &element : toasts) {
  63. if (!element.value.popped || element.value.duration <= 0) {
  64. continue;
  65. }
  66. toasts[element.key].remaining_time = element.value.duration;
  67. element.key->update();
  68. }
  69. }
  70. // Change alpha over time.
  71. bool needs_update = false;
  72. for (const KeyValue<Control *, Toast> &element : toasts) {
  73. Color modulate = element.key->get_modulate();
  74. // Change alpha over time.
  75. if (element.value.popped && modulate.a < 1.0) {
  76. modulate.a += delta * 3;
  77. element.key->set_modulate(modulate);
  78. } else if (!element.value.popped && modulate.a > 0.0) {
  79. modulate.a -= delta * 2;
  80. element.key->set_modulate(modulate);
  81. }
  82. // Hide element if it is not visible anymore.
  83. if (modulate.a <= 0) {
  84. element.key->hide();
  85. needs_update = true;
  86. }
  87. }
  88. if (needs_update) {
  89. _update_vbox_position();
  90. _update_disable_notifications_button();
  91. main_button->update();
  92. }
  93. } break;
  94. case NOTIFICATION_ENTER_TREE:
  95. case NOTIFICATION_THEME_CHANGED: {
  96. if (vbox_container->is_visible()) {
  97. main_button->set_icon(get_theme_icon(SNAME("Notification"), SNAME("EditorIcons")));
  98. } else {
  99. main_button->set_icon(get_theme_icon(SNAME("NotificationDisabled"), SNAME("EditorIcons")));
  100. }
  101. disable_notifications_button->set_icon(get_theme_icon(SNAME("NotificationDisabled"), SNAME("EditorIcons")));
  102. // Styleboxes background.
  103. info_panel_style_background->set_bg_color(get_theme_color("base_color", "Editor"));
  104. warning_panel_style_background->set_bg_color(get_theme_color("base_color", "Editor"));
  105. warning_panel_style_background->set_border_color(get_theme_color("warning_color", "Editor"));
  106. error_panel_style_background->set_bg_color(get_theme_color("base_color", "Editor"));
  107. error_panel_style_background->set_border_color(get_theme_color("error_color", "Editor"));
  108. // Styleboxes progress.
  109. info_panel_style_progress->set_bg_color(get_theme_color("base_color", "Editor").lightened(0.03));
  110. warning_panel_style_progress->set_bg_color(get_theme_color("base_color", "Editor").lightened(0.03));
  111. warning_panel_style_progress->set_border_color(get_theme_color("warning_color", "Editor"));
  112. error_panel_style_progress->set_bg_color(get_theme_color("base_color", "Editor").lightened(0.03));
  113. error_panel_style_progress->set_border_color(get_theme_color("error_color", "Editor"));
  114. main_button->update();
  115. disable_notifications_button->update();
  116. } break;
  117. case NOTIFICATION_TRANSFORM_CHANGED: {
  118. _update_vbox_position();
  119. _update_disable_notifications_button();
  120. } break;
  121. default:
  122. break;
  123. }
  124. }
  125. void EditorToaster::_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) {
  126. if (!EditorToaster::get_singleton()) {
  127. return;
  128. }
  129. #ifdef DEV_ENABLED
  130. bool in_dev = true;
  131. #else
  132. bool in_dev = false;
  133. #endif
  134. int show_all_setting = EDITOR_GET("interface/editor/show_internal_errors_in_toast_notifications");
  135. if (p_editor_notify || (show_all_setting == 0 && in_dev) || show_all_setting == 1) {
  136. String err_str;
  137. if (p_errorexp && p_errorexp[0]) {
  138. err_str = p_errorexp;
  139. } else {
  140. err_str = String(p_error);
  141. }
  142. String tooltip_str = String(p_file) + ":" + itos(p_line);
  143. if (!p_editor_notify) {
  144. if (p_type == ERR_HANDLER_WARNING) {
  145. err_str = "INTERNAL WARNING: " + err_str;
  146. } else {
  147. err_str = "INTERNAL ERROR: " + err_str;
  148. }
  149. }
  150. if (p_type == ERR_HANDLER_WARNING) {
  151. EditorToaster::get_singleton()->popup_str(err_str, SEVERITY_WARNING, tooltip_str);
  152. } else {
  153. EditorToaster::get_singleton()->popup_str(err_str, SEVERITY_ERROR, tooltip_str);
  154. }
  155. }
  156. }
  157. void EditorToaster::_update_vbox_position() {
  158. // This is kind of a workaround because it's hard to keep the VBox anchroed to the bottom.
  159. vbox_container->set_size(Vector2());
  160. vbox_container->set_position(get_global_position() - vbox_container->get_size() + Vector2(get_size().x, -5 * EDSCALE));
  161. }
  162. void EditorToaster::_update_disable_notifications_button() {
  163. bool any_visible = false;
  164. for (KeyValue<Control *, Toast> element : toasts) {
  165. if (element.key->is_visible()) {
  166. any_visible = true;
  167. break;
  168. }
  169. }
  170. if (!any_visible || !vbox_container->is_visible()) {
  171. disable_notifications_panel->hide();
  172. } else {
  173. disable_notifications_panel->show();
  174. disable_notifications_panel->set_position(get_global_position() + Vector2(5 * EDSCALE, -disable_notifications_panel->get_minimum_size().y) + Vector2(get_size().x, -5 * EDSCALE));
  175. }
  176. }
  177. void EditorToaster::_auto_hide_or_free_toasts() {
  178. // Hide or free old temporary items.
  179. int visible_temporary = 0;
  180. int temporary = 0;
  181. LocalVector<Control *> to_delete;
  182. for (int i = vbox_container->get_child_count() - 1; i >= 0; i--) {
  183. Control *control = Object::cast_to<Control>(vbox_container->get_child(i));
  184. if (toasts[control].duration <= 0) {
  185. continue; // Ignore non-temporary toasts.
  186. }
  187. temporary++;
  188. if (control->is_visible()) {
  189. visible_temporary++;
  190. }
  191. // Hide
  192. if (visible_temporary > max_temporary_count) {
  193. close(control);
  194. }
  195. // Free
  196. if (temporary > max_temporary_count * 2) {
  197. to_delete.push_back(control);
  198. }
  199. }
  200. // Delete the control right away (removed as child) as it might cause issues otherwise when iterative over the vbox_container children.
  201. for (unsigned int i = 0; i < to_delete.size(); i++) {
  202. vbox_container->remove_child(to_delete[i]);
  203. to_delete[i]->queue_delete();
  204. toasts.erase(to_delete[i]);
  205. }
  206. }
  207. void EditorToaster::_draw_button() {
  208. bool has_one = false;
  209. Severity highest_severity = SEVERITY_INFO;
  210. for (const KeyValue<Control *, Toast> &element : toasts) {
  211. if (!element.key->is_visible()) {
  212. continue;
  213. }
  214. has_one = true;
  215. if (element.value.severity > highest_severity) {
  216. highest_severity = element.value.severity;
  217. }
  218. }
  219. if (!has_one) {
  220. return;
  221. }
  222. Color color;
  223. real_t button_radius = main_button->get_size().x / 8;
  224. switch (highest_severity) {
  225. case SEVERITY_INFO:
  226. color = get_theme_color("accent_color", "Editor");
  227. break;
  228. case SEVERITY_WARNING:
  229. color = get_theme_color("warning_color", "Editor");
  230. break;
  231. case SEVERITY_ERROR:
  232. color = get_theme_color("error_color", "Editor");
  233. break;
  234. default:
  235. break;
  236. }
  237. main_button->draw_circle(Vector2(button_radius * 2, button_radius * 2), button_radius, color);
  238. }
  239. void EditorToaster::_draw_progress(Control *panel) {
  240. if (toasts.has(panel) && toasts[panel].remaining_time > 0 && toasts[panel].duration > 0) {
  241. Size2 size = panel->get_size();
  242. size.x *= MIN(1, Math::range_lerp(toasts[panel].remaining_time, 0, toasts[panel].duration, 0, 2));
  243. Ref<StyleBoxFlat> stylebox;
  244. switch (toasts[panel].severity) {
  245. case SEVERITY_INFO:
  246. stylebox = info_panel_style_progress;
  247. break;
  248. case SEVERITY_WARNING:
  249. stylebox = warning_panel_style_progress;
  250. break;
  251. case SEVERITY_ERROR:
  252. stylebox = error_panel_style_progress;
  253. break;
  254. default:
  255. break;
  256. }
  257. panel->draw_style_box(stylebox, Rect2(Vector2(), size));
  258. }
  259. }
  260. void EditorToaster::_set_notifications_enabled(bool p_enabled) {
  261. vbox_container->set_visible(p_enabled);
  262. if (p_enabled) {
  263. main_button->set_icon(get_theme_icon(SNAME("Notification"), SNAME("EditorIcons")));
  264. } else {
  265. main_button->set_icon(get_theme_icon(SNAME("NotificationDisabled"), SNAME("EditorIcons")));
  266. }
  267. _update_disable_notifications_button();
  268. }
  269. void EditorToaster::_repop_old() {
  270. // Repop olds, up to max_temporary_count
  271. bool needs_update = false;
  272. int visible = 0;
  273. for (int i = vbox_container->get_child_count() - 1; i >= 0; i--) {
  274. Control *control = Object::cast_to<Control>(vbox_container->get_child(i));
  275. if (!control->is_visible()) {
  276. control->show();
  277. toasts[control].remaining_time = toasts[control].duration;
  278. toasts[control].popped = true;
  279. needs_update = true;
  280. }
  281. visible++;
  282. if (visible >= max_temporary_count) {
  283. break;
  284. }
  285. }
  286. if (needs_update) {
  287. _update_vbox_position();
  288. _update_disable_notifications_button();
  289. main_button->update();
  290. }
  291. }
  292. Control *EditorToaster::popup(Control *p_control, Severity p_severity, double p_time, String p_tooltip) {
  293. // Create the panel according to the severity.
  294. PanelContainer *panel = memnew(PanelContainer);
  295. panel->set_tooltip(p_tooltip);
  296. switch (p_severity) {
  297. case SEVERITY_INFO:
  298. panel->add_theme_style_override("panel", info_panel_style_background);
  299. break;
  300. case SEVERITY_WARNING:
  301. panel->add_theme_style_override("panel", warning_panel_style_background);
  302. break;
  303. case SEVERITY_ERROR:
  304. panel->add_theme_style_override("panel", error_panel_style_background);
  305. break;
  306. default:
  307. break;
  308. }
  309. panel->set_modulate(Color(1, 1, 1, 0));
  310. panel->connect("draw", callable_bind(callable_mp(this, &EditorToaster::_draw_progress), panel));
  311. // Horizontal container.
  312. HBoxContainer *hbox_container = memnew(HBoxContainer);
  313. hbox_container->set_h_size_flags(SIZE_EXPAND_FILL);
  314. panel->add_child(hbox_container);
  315. // Content control.
  316. p_control->set_h_size_flags(SIZE_EXPAND_FILL);
  317. hbox_container->add_child(p_control);
  318. // Close button.
  319. if (p_time > 0.0) {
  320. Button *close_button = memnew(Button);
  321. close_button->set_flat(true);
  322. close_button->set_icon(get_theme_icon("Close", "EditorIcons"));
  323. close_button->connect("pressed", callable_bind(callable_mp(this, &EditorToaster::close), panel));
  324. hbox_container->add_child(close_button);
  325. }
  326. toasts[panel].severity = p_severity;
  327. if (p_time > 0.0) {
  328. toasts[panel].duration = p_time;
  329. toasts[panel].remaining_time = p_time;
  330. } else {
  331. toasts[panel].duration = -1.0;
  332. }
  333. toasts[panel].popped = true;
  334. vbox_container->add_child(panel);
  335. _auto_hide_or_free_toasts();
  336. _update_vbox_position();
  337. _update_disable_notifications_button();
  338. main_button->update();
  339. return panel;
  340. }
  341. void EditorToaster::popup_str(String p_message, Severity p_severity, String p_tooltip) {
  342. // Check if we already have a popup with the given message.
  343. Control *control = nullptr;
  344. for (KeyValue<Control *, Toast> element : toasts) {
  345. if (element.value.message == p_message && element.value.severity == p_severity && element.value.tooltip == p_tooltip) {
  346. control = element.key;
  347. break;
  348. }
  349. }
  350. // Create a new message if needed.
  351. if (control == nullptr) {
  352. Label *label = memnew(Label);
  353. control = popup(label, p_severity, default_message_duration, p_tooltip);
  354. toasts[control].message = p_message;
  355. toasts[control].tooltip = p_tooltip;
  356. toasts[control].count = 1;
  357. } else {
  358. if (toasts[control].popped) {
  359. toasts[control].count += 1;
  360. } else {
  361. toasts[control].count = 1;
  362. }
  363. toasts[control].remaining_time = toasts[control].duration;
  364. toasts[control].popped = true;
  365. control->show();
  366. vbox_container->move_child(control, vbox_container->get_child_count());
  367. _auto_hide_or_free_toasts();
  368. _update_vbox_position();
  369. _update_disable_notifications_button();
  370. main_button->update();
  371. }
  372. // Retrieve the label back then update the text.
  373. Label *label = Object::cast_to<Label>(control->get_child(0)->get_child(0));
  374. ERR_FAIL_COND(!label);
  375. if (toasts[control].count == 1) {
  376. label->set_text(p_message);
  377. } else {
  378. label->set_text(vformat("%s (%d)", p_message, toasts[control].count));
  379. }
  380. }
  381. void EditorToaster::close(Control *p_control) {
  382. ERR_FAIL_COND(!toasts.has(p_control));
  383. toasts[p_control].remaining_time = -1.0;
  384. toasts[p_control].popped = false;
  385. }
  386. EditorToaster *EditorToaster::get_singleton() {
  387. return singleton;
  388. }
  389. EditorToaster::EditorToaster() {
  390. set_notify_transform(true);
  391. set_process_internal(true);
  392. // VBox.
  393. vbox_container = memnew(VBoxContainer);
  394. vbox_container->set_as_top_level(true);
  395. vbox_container->connect("resized", callable_mp(this, &EditorToaster::_update_vbox_position));
  396. add_child(vbox_container);
  397. // Theming (background).
  398. info_panel_style_background.instantiate();
  399. info_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  400. warning_panel_style_background.instantiate();
  401. warning_panel_style_background->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  402. warning_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  403. error_panel_style_background.instantiate();
  404. error_panel_style_background->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  405. error_panel_style_background->set_corner_radius_all(stylebox_radius * EDSCALE);
  406. Ref<StyleBoxFlat> boxes[] = { info_panel_style_background, warning_panel_style_background, error_panel_style_background };
  407. for (int i = 0; i < 3; i++) {
  408. boxes[i]->set_default_margin(SIDE_LEFT, int(stylebox_radius * 2.5));
  409. boxes[i]->set_default_margin(SIDE_RIGHT, int(stylebox_radius * 2.5));
  410. boxes[i]->set_default_margin(SIDE_TOP, 3);
  411. boxes[i]->set_default_margin(SIDE_BOTTOM, 3);
  412. }
  413. // Theming (progress).
  414. info_panel_style_progress.instantiate();
  415. info_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  416. warning_panel_style_progress.instantiate();
  417. warning_panel_style_progress->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  418. warning_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  419. error_panel_style_progress.instantiate();
  420. error_panel_style_progress->set_border_width(SIDE_LEFT, stylebox_radius * EDSCALE);
  421. error_panel_style_progress->set_corner_radius_all(stylebox_radius * EDSCALE);
  422. // Main button.
  423. main_button = memnew(Button);
  424. main_button->set_flat(true);
  425. main_button->connect("pressed", callable_mp(this, &EditorToaster::_set_notifications_enabled), varray(true));
  426. main_button->connect("pressed", callable_mp(this, &EditorToaster::_repop_old));
  427. main_button->connect("draw", callable_mp(this, &EditorToaster::_draw_button));
  428. add_child(main_button);
  429. // Disable notification button.
  430. disable_notifications_panel = memnew(PanelContainer);
  431. disable_notifications_panel->set_as_top_level(true);
  432. disable_notifications_panel->add_theme_style_override("panel", info_panel_style_background);
  433. add_child(disable_notifications_panel);
  434. disable_notifications_button = memnew(Button);
  435. disable_notifications_button->set_flat(true);
  436. disable_notifications_button->connect("pressed", callable_mp(this, &EditorToaster::_set_notifications_enabled), varray(false));
  437. disable_notifications_panel->add_child(disable_notifications_button);
  438. // Other
  439. singleton = this;
  440. eh.errfunc = _error_handler;
  441. add_error_handler(&eh);
  442. };
  443. EditorToaster::~EditorToaster() {
  444. singleton = nullptr;
  445. remove_error_handler(&eh);
  446. }