popup.cpp 14 KB

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