popup.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
  35. Ref<InputEventKey> key = p_event;
  36. if (get_flag(FLAG_POPUP) && key.is_valid() && key->is_pressed() && key->get_keycode() == Key::ESCAPE) {
  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::_update_theme_item_cache() {
  64. Window::_update_theme_item_cache();
  65. theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
  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. emit_signal(SNAME("popup_hide"));
  76. popped_up = false;
  77. }
  78. }
  79. } break;
  80. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  81. if (!is_in_edited_scene_root()) {
  82. if (has_focus()) {
  83. popped_up = true;
  84. }
  85. }
  86. } break;
  87. case NOTIFICATION_EXIT_TREE: {
  88. if (!is_in_edited_scene_root()) {
  89. _deinitialize_visible_parents();
  90. }
  91. } break;
  92. case NOTIFICATION_WM_CLOSE_REQUEST: {
  93. if (!is_in_edited_scene_root()) {
  94. _close_pressed();
  95. }
  96. } break;
  97. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  98. if (!is_in_edited_scene_root() && get_flag(FLAG_POPUP)) {
  99. _close_pressed();
  100. }
  101. } break;
  102. }
  103. }
  104. void Popup::_parent_focused() {
  105. if (popped_up && get_flag(FLAG_POPUP)) {
  106. _close_pressed();
  107. }
  108. }
  109. void Popup::_close_pressed() {
  110. popped_up = false;
  111. _deinitialize_visible_parents();
  112. call_deferred(SNAME("hide"));
  113. }
  114. void Popup::_post_popup() {
  115. Window::_post_popup();
  116. popped_up = true;
  117. }
  118. void Popup::_bind_methods() {
  119. ADD_SIGNAL(MethodInfo("popup_hide"));
  120. }
  121. void Popup::_validate_property(PropertyInfo &p_property) const {
  122. if (
  123. p_property.name == "transient" ||
  124. p_property.name == "exclusive" ||
  125. p_property.name == "popup_window" ||
  126. p_property.name == "unfocusable") {
  127. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  128. }
  129. }
  130. Rect2i Popup::_popup_adjust_rect() const {
  131. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  132. Rect2i parent_rect = get_usable_parent_rect();
  133. if (parent_rect == Rect2i()) {
  134. return Rect2i();
  135. }
  136. Rect2i current(get_position(), get_size());
  137. if (current.position.x + current.size.x > parent_rect.position.x + parent_rect.size.x) {
  138. current.position.x = parent_rect.position.x + parent_rect.size.x - current.size.x;
  139. }
  140. if (current.position.x < parent_rect.position.x) {
  141. current.position.x = parent_rect.position.x;
  142. }
  143. if (current.position.y + current.size.y > parent_rect.position.y + parent_rect.size.y) {
  144. current.position.y = parent_rect.position.y + parent_rect.size.y - current.size.y;
  145. }
  146. if (current.position.y < parent_rect.position.y) {
  147. current.position.y = parent_rect.position.y;
  148. }
  149. if (current.size.y > parent_rect.size.y) {
  150. current.size.y = parent_rect.size.y;
  151. }
  152. if (current.size.x > parent_rect.size.x) {
  153. current.size.x = parent_rect.size.x;
  154. }
  155. // Early out if max size not set.
  156. Size2i popup_max_size = get_max_size();
  157. if (popup_max_size <= Size2()) {
  158. return current;
  159. }
  160. if (current.size.x > popup_max_size.x) {
  161. current.size.x = popup_max_size.x;
  162. }
  163. if (current.size.y > popup_max_size.y) {
  164. current.size.y = popup_max_size.y;
  165. }
  166. return current;
  167. }
  168. Popup::Popup() {
  169. set_wrap_controls(true);
  170. set_visible(false);
  171. set_transient(true);
  172. set_flag(FLAG_BORDERLESS, true);
  173. set_flag(FLAG_RESIZE_DISABLED, true);
  174. set_flag(FLAG_POPUP, true);
  175. connect("window_input", callable_mp(this, &Popup::_input_from_window));
  176. }
  177. Popup::~Popup() {
  178. }
  179. Size2 PopupPanel::_get_contents_minimum_size() const {
  180. Size2 ms;
  181. for (int i = 0; i < get_child_count(); i++) {
  182. Control *c = Object::cast_to<Control>(get_child(i));
  183. if (!c || c == panel) {
  184. continue;
  185. }
  186. if (c->is_set_as_top_level()) {
  187. continue;
  188. }
  189. Size2 cms = c->get_combined_minimum_size();
  190. ms.x = MAX(cms.x, ms.x);
  191. ms.y = MAX(cms.y, ms.y);
  192. }
  193. return ms + theme_cache.panel_style->get_minimum_size();
  194. }
  195. void PopupPanel::_update_child_rects() {
  196. Vector2 cpos(theme_cache.panel_style->get_offset());
  197. Vector2 csize(get_size() - theme_cache.panel_style->get_minimum_size());
  198. for (int i = 0; i < get_child_count(); i++) {
  199. Control *c = Object::cast_to<Control>(get_child(i));
  200. if (!c) {
  201. continue;
  202. }
  203. if (c->is_set_as_top_level()) {
  204. continue;
  205. }
  206. if (c == panel) {
  207. c->set_position(Vector2());
  208. c->set_size(get_size());
  209. } else {
  210. c->set_position(cpos);
  211. c->set_size(csize);
  212. }
  213. }
  214. }
  215. void PopupPanel::_update_theme_item_cache() {
  216. Popup::_update_theme_item_cache();
  217. theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
  218. }
  219. void PopupPanel::_notification(int p_what) {
  220. switch (p_what) {
  221. case NOTIFICATION_READY:
  222. case NOTIFICATION_THEME_CHANGED: {
  223. panel->add_theme_style_override("panel", theme_cache.panel_style);
  224. _update_child_rects();
  225. } break;
  226. case NOTIFICATION_WM_SIZE_CHANGED: {
  227. _update_child_rects();
  228. } break;
  229. }
  230. }
  231. PopupPanel::PopupPanel() {
  232. panel = memnew(Panel);
  233. add_child(panel, false, INTERNAL_MODE_FRONT);
  234. }