popup.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #include "core/config/engine.h"
  32. #include "core/os/keyboard.h"
  33. #include "scene/gui/panel.h"
  34. #include "scene/theme/theme_db.h"
  35. void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
  36. if (get_flag(FLAG_POPUP) && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  37. _close_pressed();
  38. }
  39. }
  40. void Popup::_initialize_visible_parents() {
  41. if (is_embedded()) {
  42. visible_parents.clear();
  43. Window *parent_window = this;
  44. while (parent_window) {
  45. parent_window = parent_window->get_parent_visible_window();
  46. if (parent_window) {
  47. visible_parents.push_back(parent_window);
  48. parent_window->connect("focus_entered", callable_mp(this, &Popup::_parent_focused));
  49. parent_window->connect("tree_exited", callable_mp(this, &Popup::_deinitialize_visible_parents));
  50. }
  51. }
  52. }
  53. }
  54. void Popup::_deinitialize_visible_parents() {
  55. if (is_embedded()) {
  56. for (Window *parent_window : visible_parents) {
  57. parent_window->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
  58. parent_window->disconnect("tree_exited", callable_mp(this, &Popup::_deinitialize_visible_parents));
  59. }
  60. visible_parents.clear();
  61. }
  62. }
  63. void Popup::_notification(int p_what) {
  64. switch (p_what) {
  65. case NOTIFICATION_VISIBILITY_CHANGED: {
  66. if (!is_in_edited_scene_root()) {
  67. if (is_visible()) {
  68. _initialize_visible_parents();
  69. } else {
  70. _deinitialize_visible_parents();
  71. emit_signal(SNAME("popup_hide"));
  72. popped_up = false;
  73. }
  74. }
  75. } break;
  76. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  77. if (!is_in_edited_scene_root()) {
  78. if (has_focus()) {
  79. popped_up = true;
  80. }
  81. }
  82. } break;
  83. case NOTIFICATION_UNPARENTED:
  84. case NOTIFICATION_EXIT_TREE: {
  85. if (!is_in_edited_scene_root()) {
  86. _deinitialize_visible_parents();
  87. }
  88. } break;
  89. case NOTIFICATION_WM_CLOSE_REQUEST: {
  90. if (!is_in_edited_scene_root()) {
  91. _close_pressed();
  92. }
  93. } break;
  94. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  95. if (!is_in_edited_scene_root() && get_flag(FLAG_POPUP)) {
  96. _close_pressed();
  97. }
  98. } break;
  99. }
  100. }
  101. void Popup::_parent_focused() {
  102. if (popped_up && get_flag(FLAG_POPUP)) {
  103. _close_pressed();
  104. }
  105. }
  106. void Popup::_close_pressed() {
  107. popped_up = false;
  108. _deinitialize_visible_parents();
  109. call_deferred(SNAME("hide"));
  110. }
  111. void Popup::_post_popup() {
  112. Window::_post_popup();
  113. popped_up = true;
  114. }
  115. void Popup::_validate_property(PropertyInfo &p_property) const {
  116. if (
  117. p_property.name == "transient" ||
  118. p_property.name == "exclusive" ||
  119. p_property.name == "popup_window" ||
  120. p_property.name == "unfocusable") {
  121. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  122. }
  123. }
  124. Rect2i Popup::_popup_adjust_rect() const {
  125. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  126. Rect2i parent_rect = get_usable_parent_rect();
  127. if (parent_rect == Rect2i()) {
  128. return Rect2i();
  129. }
  130. Rect2i current(get_position(), get_size());
  131. if (current.position.x + current.size.x > parent_rect.position.x + parent_rect.size.x) {
  132. current.position.x = parent_rect.position.x + parent_rect.size.x - current.size.x;
  133. }
  134. if (current.position.x < parent_rect.position.x) {
  135. current.position.x = parent_rect.position.x;
  136. }
  137. if (current.position.y + current.size.y > parent_rect.position.y + parent_rect.size.y) {
  138. current.position.y = parent_rect.position.y + parent_rect.size.y - current.size.y;
  139. }
  140. if (current.position.y < parent_rect.position.y) {
  141. current.position.y = parent_rect.position.y;
  142. }
  143. if (current.size.y > parent_rect.size.y) {
  144. current.size.y = parent_rect.size.y;
  145. }
  146. if (current.size.x > parent_rect.size.x) {
  147. current.size.x = parent_rect.size.x;
  148. }
  149. // Early out if max size not set.
  150. Size2i popup_max_size = get_max_size();
  151. if (popup_max_size <= Size2()) {
  152. return current;
  153. }
  154. if (current.size.x > popup_max_size.x) {
  155. current.size.x = popup_max_size.x;
  156. }
  157. if (current.size.y > popup_max_size.y) {
  158. current.size.y = popup_max_size.y;
  159. }
  160. return current;
  161. }
  162. void Popup::_bind_methods() {
  163. ADD_SIGNAL(MethodInfo("popup_hide"));
  164. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Popup, panel_style, "panel");
  165. }
  166. Popup::Popup() {
  167. set_wrap_controls(true);
  168. set_visible(false);
  169. set_transient(true);
  170. set_flag(FLAG_BORDERLESS, true);
  171. set_flag(FLAG_RESIZE_DISABLED, true);
  172. set_flag(FLAG_POPUP, true);
  173. connect("window_input", callable_mp(this, &Popup::_input_from_window));
  174. }
  175. Popup::~Popup() {
  176. }
  177. Size2 PopupPanel::_get_contents_minimum_size() const {
  178. Size2 ms;
  179. for (int i = 0; i < get_child_count(); i++) {
  180. Control *c = Object::cast_to<Control>(get_child(i));
  181. if (!c || c == panel) {
  182. continue;
  183. }
  184. if (c->is_set_as_top_level()) {
  185. continue;
  186. }
  187. Size2 cms = c->get_combined_minimum_size();
  188. ms.x = MAX(cms.x, ms.x);
  189. ms.y = MAX(cms.y, ms.y);
  190. }
  191. return ms + theme_cache.panel_style->get_minimum_size();
  192. }
  193. void PopupPanel::_update_child_rects() {
  194. Vector2 cpos(theme_cache.panel_style->get_offset());
  195. Vector2 csize(get_size() - theme_cache.panel_style->get_minimum_size());
  196. for (int i = 0; i < get_child_count(); i++) {
  197. Control *c = Object::cast_to<Control>(get_child(i));
  198. if (!c) {
  199. continue;
  200. }
  201. if (c->is_set_as_top_level()) {
  202. continue;
  203. }
  204. if (c == panel) {
  205. c->set_position(Vector2());
  206. c->set_size(get_size());
  207. } else {
  208. c->set_position(cpos);
  209. c->set_size(csize);
  210. }
  211. }
  212. }
  213. void PopupPanel::_notification(int p_what) {
  214. switch (p_what) {
  215. case NOTIFICATION_READY:
  216. case NOTIFICATION_THEME_CHANGED: {
  217. panel->add_theme_style_override("panel", theme_cache.panel_style);
  218. _update_child_rects();
  219. } break;
  220. case NOTIFICATION_WM_SIZE_CHANGED: {
  221. _update_child_rects();
  222. } break;
  223. }
  224. }
  225. void PopupPanel::_bind_methods() {
  226. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupPanel, panel_style, "panel");
  227. }
  228. PopupPanel::PopupPanel() {
  229. panel = memnew(Panel);
  230. add_child(panel, false, INTERNAL_MODE_FRONT);
  231. }