popup.cpp 7.6 KB

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