popup.cpp 8.4 KB

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