2
0

editor_toaster.cpp 18 KB

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