popup.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 ((ac_popup || 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) || ac_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) || ac_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 (!Engine::get_singleton()->is_editor_hint()) {
  134. return;
  135. }
  136. if (
  137. p_property.name == "transient" ||
  138. p_property.name == "exclusive" ||
  139. p_property.name == "popup_window" ||
  140. p_property.name == "unfocusable") {
  141. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  142. }
  143. }
  144. Rect2i Popup::_popup_adjust_rect() const {
  145. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  146. Rect2i parent_rect = get_usable_parent_rect();
  147. if (parent_rect == Rect2i()) {
  148. return Rect2i();
  149. }
  150. Rect2i current(get_position(), get_size());
  151. if (!is_embedded() && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
  152. // We're fine as is, the Display Server will take care of that for us.
  153. return current;
  154. }
  155. if (current.position.x + current.size.x > parent_rect.position.x + parent_rect.size.x) {
  156. current.position.x = parent_rect.position.x + parent_rect.size.x - current.size.x;
  157. }
  158. if (current.position.x < parent_rect.position.x) {
  159. current.position.x = parent_rect.position.x;
  160. }
  161. if (current.position.y + current.size.y > parent_rect.position.y + parent_rect.size.y) {
  162. current.position.y = parent_rect.position.y + parent_rect.size.y - current.size.y;
  163. }
  164. if (current.position.y < parent_rect.position.y) {
  165. current.position.y = parent_rect.position.y;
  166. }
  167. if (current.size.y > parent_rect.size.y) {
  168. current.size.y = parent_rect.size.y;
  169. }
  170. if (current.size.x > parent_rect.size.x) {
  171. current.size.x = parent_rect.size.x;
  172. }
  173. // Early out if max size not set.
  174. Size2i popup_max_size = get_max_size();
  175. if (popup_max_size <= Size2()) {
  176. return current;
  177. }
  178. if (current.size.x > popup_max_size.x) {
  179. current.size.x = popup_max_size.x;
  180. }
  181. if (current.size.y > popup_max_size.y) {
  182. current.size.y = popup_max_size.y;
  183. }
  184. return current;
  185. }
  186. void Popup::_bind_methods() {
  187. ADD_SIGNAL(MethodInfo("popup_hide"));
  188. }
  189. Popup::Popup() {
  190. set_wrap_controls(true);
  191. set_visible(false);
  192. set_transient(true);
  193. set_flag(FLAG_BORDERLESS, true);
  194. set_flag(FLAG_RESIZE_DISABLED, true);
  195. set_flag(FLAG_MINIMIZE_DISABLED, true);
  196. set_flag(FLAG_MAXIMIZE_DISABLED, true);
  197. set_flag(FLAG_POPUP, true);
  198. set_flag(FLAG_POPUP_WM_HINT, true);
  199. }
  200. Popup::~Popup() {
  201. }
  202. #ifdef TOOLS_ENABLED
  203. PackedStringArray PopupPanel::get_configuration_warnings() const {
  204. PackedStringArray warnings = Popup::get_configuration_warnings();
  205. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !GLOBAL_GET_CACHED(bool, "display/window/subwindows/embed_subwindows")) {
  206. Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  207. 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)) {
  208. 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."));
  209. }
  210. }
  211. return warnings;
  212. }
  213. #endif
  214. void PopupPanel::_input_from_window(const Ref<InputEvent> &p_event) {
  215. if (p_event.is_valid()) {
  216. if (!get_flag(FLAG_POPUP)) {
  217. return;
  218. }
  219. Ref<InputEventMouseButton> b = p_event;
  220. // Hide it if the shadows have been clicked.
  221. if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
  222. Rect2 panel_area = panel->get_global_rect();
  223. float win_scale = get_content_scale_factor();
  224. panel_area.position *= win_scale;
  225. panel_area.size *= win_scale;
  226. if (!panel_area.has_point(b->get_position())) {
  227. _close_pressed();
  228. }
  229. }
  230. } else {
  231. WARN_PRINT_ONCE("PopupPanel has received an invalid InputEvent. Consider filtering out invalid events.");
  232. }
  233. Popup::_input_from_window(p_event);
  234. }
  235. Size2 PopupPanel::_get_contents_minimum_size() const {
  236. Size2 ms;
  237. for (int i = 0; i < get_child_count(); i++) {
  238. Control *c = Object::cast_to<Control>(get_child(i));
  239. if (!c || c == panel) {
  240. continue;
  241. }
  242. if (c->is_set_as_top_level()) {
  243. continue;
  244. }
  245. Size2 cms = c->get_combined_minimum_size();
  246. ms = cms.max(ms);
  247. }
  248. // Take shadows into account.
  249. ms.width += panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
  250. ms.height += panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
  251. return ms + theme_cache.panel_style->get_minimum_size();
  252. }
  253. Rect2i PopupPanel::_popup_adjust_rect() const {
  254. Rect2i current = Popup::_popup_adjust_rect();
  255. if (current == Rect2i()) {
  256. return current;
  257. }
  258. pre_popup_rect = current;
  259. _update_shadow_offsets();
  260. _update_child_rects();
  261. if (is_layout_rtl()) {
  262. current.position -= Vector2(-panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  263. } else {
  264. current.position -= Vector2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  265. }
  266. 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();
  267. return current;
  268. }
  269. void PopupPanel::_update_shadow_offsets() const {
  270. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !is_embedded()) {
  271. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  272. return;
  273. }
  274. const Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  275. if (sb.is_null()) {
  276. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  277. return;
  278. }
  279. const int shadow_size = sb->get_shadow_size();
  280. if (shadow_size == 0) {
  281. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  282. return;
  283. }
  284. // Offset the background panel so it leaves space inside the window for the shadows to be drawn.
  285. const Point2 shadow_offset = sb->get_shadow_offset();
  286. if (is_layout_rtl()) {
  287. panel->set_offset(SIDE_LEFT, MAX(0, shadow_size + shadow_offset.x));
  288. panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size + shadow_offset.x));
  289. } else {
  290. panel->set_offset(SIDE_LEFT, MAX(0, shadow_size - shadow_offset.x));
  291. panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size - shadow_offset.x));
  292. }
  293. panel->set_offset(SIDE_TOP, MAX(0, shadow_size - shadow_offset.y));
  294. panel->set_offset(SIDE_BOTTOM, MIN(0, -shadow_size - shadow_offset.y));
  295. }
  296. void PopupPanel::_update_child_rects() const {
  297. Vector2 cpos(theme_cache.panel_style->get_offset());
  298. cpos += Vector2(is_layout_rtl() ? -panel->get_offset(SIDE_RIGHT) : panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP));
  299. Vector2 csize = Vector2(get_size()) / get_content_scale_factor() - theme_cache.panel_style->get_minimum_size();
  300. // Trim shadows.
  301. csize.width -= panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
  302. csize.height -= panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
  303. for (int i = 0; i < get_child_count(); i++) {
  304. Control *c = Object::cast_to<Control>(get_child(i));
  305. if (!c || c == panel) {
  306. continue;
  307. }
  308. if (c->is_set_as_top_level()) {
  309. continue;
  310. }
  311. c->set_position(cpos);
  312. c->set_size(csize);
  313. }
  314. }
  315. void PopupPanel::_notification(int p_what) {
  316. switch (p_what) {
  317. case NOTIFICATION_THEME_CHANGED: {
  318. panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);
  319. if (is_visible()) {
  320. _update_shadow_offsets();
  321. }
  322. _update_child_rects();
  323. #ifdef TOOLS_ENABLED
  324. update_configuration_warnings();
  325. #endif
  326. } break;
  327. case Control::NOTIFICATION_TRANSLATION_CHANGED:
  328. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  329. if (is_visible()) {
  330. _update_shadow_offsets();
  331. }
  332. } break;
  333. case NOTIFICATION_VISIBILITY_CHANGED: {
  334. if (!is_visible()) {
  335. // Remove the extra space used by the shadows, so they can be ignored when the popup is hidden.
  336. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  337. _update_child_rects();
  338. if (pre_popup_rect != Rect2i()) {
  339. set_position(pre_popup_rect.position);
  340. set_size(pre_popup_rect.size);
  341. pre_popup_rect = Rect2i();
  342. }
  343. } else if (pre_popup_rect == Rect2i()) {
  344. // The popup was made visible directly (without `popup_*()`), so just update the offsets without touching the rect.
  345. _update_shadow_offsets();
  346. _update_child_rects();
  347. }
  348. } break;
  349. case NOTIFICATION_WM_SIZE_CHANGED: {
  350. _update_child_rects();
  351. if (is_visible()) {
  352. const Vector2i offsets = Vector2i(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM));
  353. // Check if the size actually changed.
  354. if (pre_popup_rect.size + offsets != get_size()) {
  355. // Play safe, and stick with the new size.
  356. pre_popup_rect = Rect2i();
  357. }
  358. }
  359. } break;
  360. }
  361. }
  362. void PopupPanel::_bind_methods() {
  363. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupPanel, panel_style, "panel");
  364. }
  365. PopupPanel::PopupPanel() {
  366. set_flag(FLAG_TRANSPARENT, true);
  367. panel = memnew(Panel);
  368. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  369. add_child(panel, false, INTERNAL_MODE_FRONT);
  370. #ifdef TOOLS_ENABLED
  371. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp((Node *)this, &Node::update_configuration_warnings));
  372. #endif
  373. }