popup.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*************************************************************************/
  2. /* popup.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 (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 (uint32_t i = 0; i < visible_parents.size(); ++i) {
  57. visible_parents[i]->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
  58. visible_parents[i]->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_visible()) {
  67. _initialize_visible_parents();
  68. } else {
  69. _deinitialize_visible_parents();
  70. emit_signal(SNAME("popup_hide"));
  71. popped_up = false;
  72. }
  73. } break;
  74. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  75. if (has_focus()) {
  76. popped_up = true;
  77. }
  78. } break;
  79. case NOTIFICATION_EXIT_TREE: {
  80. _deinitialize_visible_parents();
  81. } break;
  82. case NOTIFICATION_WM_CLOSE_REQUEST:
  83. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  84. _close_pressed();
  85. } break;
  86. }
  87. }
  88. void Popup::_parent_focused() {
  89. if (popped_up && get_flag(FLAG_POPUP)) {
  90. _close_pressed();
  91. }
  92. }
  93. void Popup::_close_pressed() {
  94. popped_up = false;
  95. _deinitialize_visible_parents();
  96. call_deferred(SNAME("hide"));
  97. }
  98. void Popup::_post_popup() {
  99. Window::_post_popup();
  100. popped_up = true;
  101. }
  102. void Popup::_bind_methods() {
  103. ADD_SIGNAL(MethodInfo("popup_hide"));
  104. }
  105. Rect2i Popup::_popup_adjust_rect() const {
  106. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  107. Rect2i parent = get_usable_parent_rect();
  108. if (parent == Rect2i()) {
  109. return Rect2i();
  110. }
  111. Rect2i current(get_position(), get_size());
  112. if (current.position.x + current.size.x > parent.position.x + parent.size.x) {
  113. current.position.x = parent.position.x + parent.size.x - current.size.x;
  114. }
  115. if (current.position.x < parent.position.x) {
  116. current.position.x = parent.position.x;
  117. }
  118. if (current.position.y + current.size.y > parent.position.y + parent.size.y) {
  119. current.position.y = parent.position.y + parent.size.y - current.size.y;
  120. }
  121. if (current.position.y < parent.position.y) {
  122. current.position.y = parent.position.y;
  123. }
  124. if (current.size.y > parent.size.y) {
  125. current.size.y = parent.size.y;
  126. }
  127. if (current.size.x > parent.size.x) {
  128. current.size.x = parent.size.x;
  129. }
  130. // Early out if max size not set.
  131. Size2i max_size = get_max_size();
  132. if (max_size <= Size2()) {
  133. return current;
  134. }
  135. if (current.size.x > max_size.x) {
  136. current.size.x = max_size.x;
  137. }
  138. if (current.size.y > max_size.y) {
  139. current.size.y = max_size.y;
  140. }
  141. return current;
  142. }
  143. Popup::Popup() {
  144. set_wrap_controls(true);
  145. set_visible(false);
  146. set_transient(true);
  147. set_flag(FLAG_BORDERLESS, true);
  148. set_flag(FLAG_RESIZE_DISABLED, true);
  149. set_flag(FLAG_POPUP, true);
  150. connect("window_input", callable_mp(this, &Popup::_input_from_window));
  151. }
  152. Popup::~Popup() {
  153. }
  154. Size2 PopupPanel::_get_contents_minimum_size() const {
  155. Ref<StyleBox> p = get_theme_stylebox(SNAME("panel"), get_class_name());
  156. Size2 ms;
  157. for (int i = 0; i < get_child_count(); i++) {
  158. Control *c = Object::cast_to<Control>(get_child(i));
  159. if (!c || c == panel) {
  160. continue;
  161. }
  162. if (c->is_set_as_top_level()) {
  163. continue;
  164. }
  165. Size2 cms = c->get_combined_minimum_size();
  166. ms.x = MAX(cms.x, ms.x);
  167. ms.y = MAX(cms.y, ms.y);
  168. }
  169. return ms + p->get_minimum_size();
  170. }
  171. void PopupPanel::_update_child_rects() {
  172. Ref<StyleBox> p = get_theme_stylebox(SNAME("panel"), get_class_name());
  173. Vector2 cpos(p->get_offset());
  174. Vector2 csize(get_size() - p->get_minimum_size());
  175. for (int i = 0; i < get_child_count(); i++) {
  176. Control *c = Object::cast_to<Control>(get_child(i));
  177. if (!c) {
  178. continue;
  179. }
  180. if (c->is_set_as_top_level()) {
  181. continue;
  182. }
  183. if (c == panel) {
  184. c->set_position(Vector2());
  185. c->set_size(get_size());
  186. } else {
  187. c->set_position(cpos);
  188. c->set_size(csize);
  189. }
  190. }
  191. }
  192. void PopupPanel::_notification(int p_what) {
  193. switch (p_what) {
  194. case NOTIFICATION_THEME_CHANGED: {
  195. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), get_class_name()));
  196. } break;
  197. case NOTIFICATION_ENTER_TREE:
  198. case NOTIFICATION_READY: {
  199. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), get_class_name()));
  200. _update_child_rects();
  201. } break;
  202. case NOTIFICATION_WM_SIZE_CHANGED: {
  203. _update_child_rects();
  204. } break;
  205. }
  206. }
  207. PopupPanel::PopupPanel() {
  208. panel = memnew(Panel);
  209. add_child(panel, false, INTERNAL_MODE_FRONT);
  210. }