popup.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /**************************************************************************/
  2. /* popup.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 "popup.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "core/config/project_settings.h"
  33. #endif
  34. #include "scene/gui/panel.h"
  35. #include "scene/resources/style_box_flat.h"
  36. #include "scene/theme/theme_db.h"
  37. void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
  38. if (get_flag(FLAG_POPUP) && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  39. hide_reason = HIDE_REASON_CANCELED; // ESC pressed, mark as canceled unconditionally.
  40. _close_pressed();
  41. }
  42. Window::_input_from_window(p_event);
  43. }
  44. void Popup::_initialize_visible_parents() {
  45. if (is_embedded()) {
  46. visible_parents.clear();
  47. Window *parent_window = this;
  48. while (parent_window) {
  49. parent_window = parent_window->get_parent_visible_window();
  50. if (parent_window) {
  51. visible_parents.push_back(parent_window);
  52. parent_window->connect(SceneStringName(focus_entered), callable_mp(this, &Popup::_parent_focused));
  53. parent_window->connect(SceneStringName(tree_exited), callable_mp(this, &Popup::_deinitialize_visible_parents));
  54. }
  55. }
  56. }
  57. }
  58. void Popup::_deinitialize_visible_parents() {
  59. if (is_embedded()) {
  60. for (Window *parent_window : visible_parents) {
  61. parent_window->disconnect(SceneStringName(focus_entered), callable_mp(this, &Popup::_parent_focused));
  62. parent_window->disconnect(SceneStringName(tree_exited), callable_mp(this, &Popup::_deinitialize_visible_parents));
  63. }
  64. visible_parents.clear();
  65. }
  66. }
  67. void Popup::_notification(int p_what) {
  68. switch (p_what) {
  69. case NOTIFICATION_VISIBILITY_CHANGED: {
  70. if (!is_in_edited_scene_root()) {
  71. if (is_visible()) {
  72. _initialize_visible_parents();
  73. popped_up = true;
  74. hide_reason = HIDE_REASON_NONE;
  75. } else {
  76. _deinitialize_visible_parents();
  77. if (hide_reason == HIDE_REASON_NONE) {
  78. hide_reason = HIDE_REASON_CANCELED;
  79. }
  80. emit_signal(SNAME("popup_hide"));
  81. popped_up = false;
  82. }
  83. }
  84. } break;
  85. case NOTIFICATION_UNPARENTED:
  86. case NOTIFICATION_EXIT_TREE: {
  87. if (!is_in_edited_scene_root()) {
  88. _deinitialize_visible_parents();
  89. }
  90. } break;
  91. case NOTIFICATION_WM_CLOSE_REQUEST: {
  92. if (!is_in_edited_scene_root()) {
  93. if (hide_reason == HIDE_REASON_NONE) {
  94. hide_reason = HIDE_REASON_UNFOCUSED;
  95. }
  96. _close_pressed();
  97. }
  98. } break;
  99. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  100. if (!is_in_edited_scene_root() && get_flag(FLAG_POPUP)) {
  101. if (hide_reason == HIDE_REASON_NONE) {
  102. hide_reason = HIDE_REASON_UNFOCUSED;
  103. }
  104. _close_pressed();
  105. }
  106. } break;
  107. }
  108. }
  109. void Popup::_parent_focused() {
  110. if (popped_up && get_flag(FLAG_POPUP)) {
  111. if (hide_reason == HIDE_REASON_NONE) {
  112. hide_reason = HIDE_REASON_UNFOCUSED;
  113. }
  114. _close_pressed();
  115. }
  116. }
  117. void Popup::_close_pressed() {
  118. popped_up = false;
  119. _deinitialize_visible_parents();
  120. callable_mp((Window *)this, &Window::hide).call_deferred();
  121. }
  122. void Popup::_post_popup() {
  123. Window::_post_popup();
  124. popped_up = true;
  125. }
  126. void Popup::_validate_property(PropertyInfo &p_property) const {
  127. if (!Engine::get_singleton()->is_editor_hint()) {
  128. return;
  129. }
  130. if (
  131. p_property.name == "transient" ||
  132. p_property.name == "exclusive" ||
  133. p_property.name == "popup_window" ||
  134. p_property.name == "unfocusable") {
  135. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  136. }
  137. }
  138. Rect2i Popup::_popup_adjust_rect() const {
  139. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  140. Rect2i parent_rect = get_usable_parent_rect();
  141. if (parent_rect == Rect2i()) {
  142. return Rect2i();
  143. }
  144. Rect2i current(get_position(), get_size());
  145. if (!is_embedded() && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
  146. // We're fine as is, the Display Server will take care of that for us.
  147. return current;
  148. }
  149. if (current.position.x + current.size.x > parent_rect.position.x + parent_rect.size.x) {
  150. current.position.x = parent_rect.position.x + parent_rect.size.x - current.size.x;
  151. }
  152. if (current.position.x < parent_rect.position.x) {
  153. current.position.x = parent_rect.position.x;
  154. }
  155. if (current.position.y + current.size.y > parent_rect.position.y + parent_rect.size.y) {
  156. current.position.y = parent_rect.position.y + parent_rect.size.y - current.size.y;
  157. }
  158. if (current.position.y < parent_rect.position.y) {
  159. current.position.y = parent_rect.position.y;
  160. }
  161. if (current.size.y > parent_rect.size.y) {
  162. current.size.y = parent_rect.size.y;
  163. }
  164. if (current.size.x > parent_rect.size.x) {
  165. current.size.x = parent_rect.size.x;
  166. }
  167. // Early out if max size not set.
  168. Size2i popup_max_size = get_max_size();
  169. if (popup_max_size <= Size2()) {
  170. return current;
  171. }
  172. if (current.size.x > popup_max_size.x) {
  173. current.size.x = popup_max_size.x;
  174. }
  175. if (current.size.y > popup_max_size.y) {
  176. current.size.y = popup_max_size.y;
  177. }
  178. return current;
  179. }
  180. void Popup::_bind_methods() {
  181. ADD_SIGNAL(MethodInfo("popup_hide"));
  182. }
  183. Popup::Popup() {
  184. set_wrap_controls(true);
  185. set_visible(false);
  186. set_transient(true);
  187. set_flag(FLAG_BORDERLESS, true);
  188. set_flag(FLAG_RESIZE_DISABLED, true);
  189. set_flag(FLAG_MINIMIZE_DISABLED, true);
  190. set_flag(FLAG_MAXIMIZE_DISABLED, true);
  191. set_flag(FLAG_POPUP, true);
  192. set_flag(FLAG_POPUP_WM_HINT, true);
  193. }
  194. Popup::~Popup() {
  195. }
  196. #ifdef TOOLS_ENABLED
  197. PackedStringArray PopupPanel::get_configuration_warnings() const {
  198. PackedStringArray warnings = Popup::get_configuration_warnings();
  199. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !GLOBAL_GET_CACHED(bool, "display/window/subwindows/embed_subwindows")) {
  200. Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  201. if (sb.is_valid() && (sb->get_shadow_size() > 0 || sb->get_corner_radius(CORNER_TOP_LEFT) > 0 || sb->get_corner_radius(CORNER_TOP_RIGHT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_LEFT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_RIGHT) > 0)) {
  202. warnings.push_back(RTR("The current theme style has shadows and/or rounded corners for popups, but those won't display correctly if \"display/window/per_pixel_transparency/allowed\" isn't enabled in the Project Settings, nor if it isn't supported."));
  203. }
  204. }
  205. return warnings;
  206. }
  207. #endif
  208. void PopupPanel::_input_from_window(const Ref<InputEvent> &p_event) {
  209. if (p_event.is_valid()) {
  210. if (!get_flag(FLAG_POPUP)) {
  211. return;
  212. }
  213. Ref<InputEventMouseButton> b = p_event;
  214. // Hide it if the shadows have been clicked.
  215. if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
  216. Rect2 panel_area = panel->get_global_rect();
  217. float win_scale = get_content_scale_factor();
  218. panel_area.position *= win_scale;
  219. panel_area.size *= win_scale;
  220. if (!panel_area.has_point(b->get_position())) {
  221. _close_pressed();
  222. }
  223. }
  224. } else {
  225. WARN_PRINT_ONCE("PopupPanel has received an invalid InputEvent. Consider filtering out invalid events.");
  226. }
  227. Popup::_input_from_window(p_event);
  228. }
  229. Size2 PopupPanel::_get_contents_minimum_size() const {
  230. Size2 ms;
  231. for (int i = 0; i < get_child_count(); i++) {
  232. Control *c = Object::cast_to<Control>(get_child(i));
  233. if (!c || c == panel) {
  234. continue;
  235. }
  236. if (c->is_set_as_top_level()) {
  237. continue;
  238. }
  239. Size2 cms = c->get_combined_minimum_size();
  240. ms = cms.max(ms);
  241. }
  242. // Take shadows into account.
  243. ms.width += panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
  244. ms.height += panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
  245. return ms + theme_cache.panel_style->get_minimum_size();
  246. }
  247. Rect2i PopupPanel::_popup_adjust_rect() const {
  248. Rect2i current = Popup::_popup_adjust_rect();
  249. if (current == Rect2i()) {
  250. return current;
  251. }
  252. pre_popup_rect = current;
  253. _update_shadow_offsets();
  254. _update_child_rects();
  255. if (is_layout_rtl()) {
  256. current.position -= Vector2(-panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  257. } else {
  258. current.position -= Vector2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  259. }
  260. current.size += Vector2(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM)) * get_content_scale_factor();
  261. return current;
  262. }
  263. void PopupPanel::_update_shadow_offsets() const {
  264. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !is_embedded()) {
  265. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  266. return;
  267. }
  268. const Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  269. if (sb.is_null()) {
  270. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  271. return;
  272. }
  273. const int shadow_size = sb->get_shadow_size();
  274. if (shadow_size == 0) {
  275. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  276. return;
  277. }
  278. // Offset the background panel so it leaves space inside the window for the shadows to be drawn.
  279. const Point2 shadow_offset = sb->get_shadow_offset();
  280. if (is_layout_rtl()) {
  281. panel->set_offset(SIDE_LEFT, MAX(0, shadow_size + shadow_offset.x));
  282. panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size + shadow_offset.x));
  283. } else {
  284. panel->set_offset(SIDE_LEFT, MAX(0, shadow_size - shadow_offset.x));
  285. panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size - shadow_offset.x));
  286. }
  287. panel->set_offset(SIDE_TOP, MAX(0, shadow_size - shadow_offset.y));
  288. panel->set_offset(SIDE_BOTTOM, MIN(0, -shadow_size - shadow_offset.y));
  289. }
  290. void PopupPanel::_update_child_rects() const {
  291. Vector2 cpos(theme_cache.panel_style->get_offset());
  292. cpos += Vector2(is_layout_rtl() ? -panel->get_offset(SIDE_RIGHT) : panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP));
  293. Vector2 csize = Vector2(get_size()) / get_content_scale_factor() - theme_cache.panel_style->get_minimum_size();
  294. // Trim shadows.
  295. csize.width -= panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
  296. csize.height -= panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
  297. for (int i = 0; i < get_child_count(); i++) {
  298. Control *c = Object::cast_to<Control>(get_child(i));
  299. if (!c || c == panel) {
  300. continue;
  301. }
  302. if (c->is_set_as_top_level()) {
  303. continue;
  304. }
  305. c->set_position(cpos);
  306. c->set_size(csize);
  307. }
  308. }
  309. void PopupPanel::_notification(int p_what) {
  310. switch (p_what) {
  311. case NOTIFICATION_THEME_CHANGED: {
  312. panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);
  313. if (is_visible()) {
  314. _update_shadow_offsets();
  315. }
  316. _update_child_rects();
  317. #ifdef TOOLS_ENABLED
  318. update_configuration_warnings();
  319. #endif
  320. } break;
  321. case Control::NOTIFICATION_TRANSLATION_CHANGED:
  322. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  323. if (is_visible()) {
  324. _update_shadow_offsets();
  325. }
  326. } break;
  327. case NOTIFICATION_VISIBILITY_CHANGED: {
  328. if (!is_visible()) {
  329. // Remove the extra space used by the shadows, so they can be ignored when the popup is hidden.
  330. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  331. _update_child_rects();
  332. if (pre_popup_rect != Rect2i()) {
  333. set_position(pre_popup_rect.position);
  334. set_size(pre_popup_rect.size);
  335. pre_popup_rect = Rect2i();
  336. }
  337. } else if (pre_popup_rect == Rect2i()) {
  338. // The popup was made visible directly (without `popup_*()`), so just update the offsets without touching the rect.
  339. _update_shadow_offsets();
  340. _update_child_rects();
  341. }
  342. } break;
  343. case NOTIFICATION_WM_SIZE_CHANGED: {
  344. _update_child_rects();
  345. if (is_visible()) {
  346. const Vector2i offsets = Vector2i(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM));
  347. // Check if the size actually changed.
  348. if (pre_popup_rect.size + offsets != get_size()) {
  349. // Play safe, and stick with the new size.
  350. pre_popup_rect = Rect2i();
  351. }
  352. }
  353. } break;
  354. }
  355. }
  356. void PopupPanel::_bind_methods() {
  357. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupPanel, panel_style, "panel");
  358. }
  359. PopupPanel::PopupPanel() {
  360. set_flag(FLAG_TRANSPARENT, true);
  361. panel = memnew(Panel);
  362. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  363. add_child(panel, false, INTERNAL_MODE_FRONT);
  364. #ifdef TOOLS_ENABLED
  365. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp((Node *)this, &Node::update_configuration_warnings));
  366. #endif
  367. }