window_wrapper.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**************************************************************************/
  2. /* window_wrapper.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 "window_wrapper.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/progress_dialog.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/panel.h"
  39. #include "scene/gui/popup.h"
  40. #include "scene/main/window.h"
  41. // WindowWrapper
  42. // Capture all shortcut events not handled by other nodes.
  43. class ShortcutBin : public Node {
  44. GDCLASS(ShortcutBin, Node);
  45. virtual void _notification(int what) {
  46. switch (what) {
  47. case NOTIFICATION_READY:
  48. set_process_shortcut_input(true);
  49. break;
  50. }
  51. }
  52. virtual void shortcut_input(const Ref<InputEvent> &p_event) override {
  53. if (!get_window()->is_visible()) {
  54. return;
  55. }
  56. Window *grandparent_window = get_window()->get_parent_visible_window();
  57. ERR_FAIL_NULL(grandparent_window);
  58. if (Object::cast_to<InputEventKey>(p_event.ptr()) || Object::cast_to<InputEventShortcut>(p_event.ptr())) {
  59. // HACK: Propagate the window input to the editor main window to handle global shortcuts.
  60. grandparent_window->push_input(p_event);
  61. if (grandparent_window->is_input_handled()) {
  62. get_viewport()->set_input_as_handled();
  63. }
  64. }
  65. }
  66. };
  67. Rect2 WindowWrapper::_get_default_window_rect() const {
  68. // Assume that the control rect is the desired one for the window.
  69. return wrapped_control->get_screen_rect();
  70. }
  71. Node *WindowWrapper::_get_wrapped_control_parent() const {
  72. if (margins) {
  73. return margins;
  74. }
  75. return window;
  76. }
  77. void WindowWrapper::_set_window_enabled_with_rect(bool p_visible, const Rect2 p_rect) {
  78. ERR_FAIL_NULL(wrapped_control);
  79. if (!is_window_available()) {
  80. return;
  81. }
  82. if (window->is_visible() == p_visible) {
  83. if (p_visible) {
  84. window->grab_focus();
  85. }
  86. return;
  87. }
  88. Node *parent = _get_wrapped_control_parent();
  89. // In the GameView plugin, we need to the the signal before the window is actually closed
  90. // to prevent the embedded game to be seen the parent window for a fraction of a second.
  91. if (!p_visible) {
  92. emit_signal("window_before_closing");
  93. }
  94. if (wrapped_control->get_parent() != parent) {
  95. // Move the control to the window.
  96. wrapped_control->reparent(parent, false);
  97. _set_window_rect(p_rect);
  98. wrapped_control->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  99. } else if (!p_visible) {
  100. // Remove control from window.
  101. wrapped_control->reparent(this, false);
  102. }
  103. window->set_visible(p_visible);
  104. if (!p_visible) {
  105. emit_signal("window_close_requested");
  106. }
  107. emit_signal("window_visibility_changed", p_visible);
  108. }
  109. void WindowWrapper::_set_window_rect(const Rect2 p_rect) {
  110. // Set the window rect even when the window is maximized to have a good default size
  111. // when the user remove the maximized mode.
  112. window->set_position(p_rect.position);
  113. window->set_size(p_rect.size);
  114. if (EDITOR_GET("interface/multi_window/maximize_window")) {
  115. window->set_mode(Window::MODE_MAXIMIZED);
  116. }
  117. }
  118. void WindowWrapper::_window_size_changed() {
  119. emit_signal(SNAME("window_size_changed"));
  120. }
  121. void WindowWrapper::_bind_methods() {
  122. ADD_SIGNAL(MethodInfo("window_visibility_changed", PropertyInfo(Variant::BOOL, "visible")));
  123. ADD_SIGNAL(MethodInfo("window_close_requested"));
  124. ADD_SIGNAL(MethodInfo("window_before_closing"));
  125. ADD_SIGNAL(MethodInfo("window_size_changed"));
  126. }
  127. void WindowWrapper::_notification(int p_what) {
  128. if (!is_window_available()) {
  129. return;
  130. }
  131. switch (p_what) {
  132. case NOTIFICATION_VISIBILITY_CHANGED: {
  133. // Grab the focus when WindowWrapper.set_visible(true) is called
  134. // and the window is showing.
  135. grab_window_focus();
  136. } break;
  137. case NOTIFICATION_READY: {
  138. set_process_shortcut_input(true);
  139. } break;
  140. case NOTIFICATION_THEME_CHANGED: {
  141. window_background->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("PanelForeground", EditorStringName(EditorStyles)));
  142. } break;
  143. }
  144. }
  145. void WindowWrapper::shortcut_input(const Ref<InputEvent> &p_event) {
  146. if (enable_shortcut.is_valid() && enable_shortcut->matches_event(p_event)) {
  147. set_window_enabled(true);
  148. }
  149. }
  150. void WindowWrapper::set_wrapped_control(Control *p_control, const Ref<Shortcut> &p_enable_shortcut) {
  151. ERR_FAIL_NULL(p_control);
  152. ERR_FAIL_COND(wrapped_control);
  153. wrapped_control = p_control;
  154. enable_shortcut = p_enable_shortcut;
  155. add_child(p_control);
  156. }
  157. Control *WindowWrapper::get_wrapped_control() const {
  158. return wrapped_control;
  159. }
  160. Control *WindowWrapper::release_wrapped_control() {
  161. set_window_enabled(false);
  162. if (wrapped_control) {
  163. Control *old_wrapped = wrapped_control;
  164. wrapped_control->get_parent()->remove_child(wrapped_control);
  165. wrapped_control = nullptr;
  166. return old_wrapped;
  167. }
  168. return nullptr;
  169. }
  170. bool WindowWrapper::is_window_available() const {
  171. return window != nullptr;
  172. }
  173. bool WindowWrapper::get_window_enabled() const {
  174. return is_window_available() ? window->is_visible() : false;
  175. }
  176. void WindowWrapper::set_window_enabled(bool p_enabled) {
  177. _set_window_enabled_with_rect(p_enabled, _get_default_window_rect());
  178. }
  179. Rect2i WindowWrapper::get_window_rect() const {
  180. ERR_FAIL_COND_V(!get_window_enabled(), Rect2i());
  181. return Rect2i(window->get_position(), window->get_size());
  182. }
  183. int WindowWrapper::get_window_screen() const {
  184. ERR_FAIL_COND_V(!get_window_enabled(), -1);
  185. return window->get_current_screen();
  186. }
  187. void WindowWrapper::restore_window(const Rect2i &p_rect, int p_screen) {
  188. ERR_FAIL_COND(!is_window_available());
  189. ERR_FAIL_INDEX(p_screen, DisplayServer::get_singleton()->get_screen_count());
  190. _set_window_enabled_with_rect(true, p_rect);
  191. window->set_current_screen(p_screen);
  192. }
  193. void WindowWrapper::restore_window_from_saved_position(const Rect2 p_window_rect, int p_screen, const Rect2 p_screen_rect) {
  194. ERR_FAIL_COND(!is_window_available());
  195. Rect2 window_rect = p_window_rect;
  196. int screen = p_screen;
  197. Rect2 restored_screen_rect = p_screen_rect;
  198. if (screen < 0 || screen >= DisplayServer::get_singleton()->get_screen_count()) {
  199. // Fallback to the main window screen if the saved screen is not available.
  200. screen = get_window()->get_window_id();
  201. }
  202. Rect2i real_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  203. if (restored_screen_rect == Rect2i()) {
  204. // Fallback to the target screen rect.
  205. restored_screen_rect = real_screen_rect;
  206. }
  207. if (window_rect == Rect2i()) {
  208. // Fallback to a standard rect.
  209. window_rect = Rect2i(restored_screen_rect.position + restored_screen_rect.size / 4, restored_screen_rect.size / 2);
  210. }
  211. // Adjust the window rect size in case the resolution changes.
  212. Vector2 screen_ratio = Vector2(real_screen_rect.size) / Vector2(restored_screen_rect.size);
  213. // The screen positioning may change, so remove the original screen position.
  214. window_rect.position -= restored_screen_rect.position;
  215. window_rect = Rect2i(window_rect.position * screen_ratio, window_rect.size * screen_ratio);
  216. window_rect.position += real_screen_rect.position;
  217. // All good, restore the window.
  218. window->set_current_screen(p_screen);
  219. if (window->is_visible()) {
  220. _set_window_rect(window_rect);
  221. } else {
  222. _set_window_enabled_with_rect(true, window_rect);
  223. }
  224. }
  225. void WindowWrapper::enable_window_on_screen(int p_screen, bool p_auto_scale) {
  226. int current_screen = Object::cast_to<Window>(get_viewport())->get_current_screen();
  227. int screen = p_screen < 0 ? current_screen : p_screen;
  228. bool auto_scale = p_auto_scale && !EDITOR_GET("interface/multi_window/maximize_window");
  229. if (auto_scale && current_screen != screen) {
  230. Rect2 control_rect = _get_default_window_rect();
  231. Rect2i source_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(current_screen);
  232. Rect2i dest_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  233. // Adjust the window rect size in case the resolution changes.
  234. Vector2 screen_ratio = Vector2(source_screen_rect.size) / Vector2(dest_screen_rect.size);
  235. // The screen positioning may change, so remove the original screen position.
  236. control_rect.position -= source_screen_rect.position;
  237. control_rect = Rect2i(control_rect.position * screen_ratio, control_rect.size * screen_ratio);
  238. control_rect.position += dest_screen_rect.position;
  239. restore_window(control_rect, p_screen);
  240. } else {
  241. window->set_current_screen(p_screen);
  242. set_window_enabled(true);
  243. }
  244. }
  245. void WindowWrapper::set_window_title(const String &p_title) {
  246. if (!is_window_available()) {
  247. return;
  248. }
  249. window->set_title(p_title);
  250. }
  251. void WindowWrapper::set_margins_enabled(bool p_enabled) {
  252. if (!is_window_available()) {
  253. return;
  254. }
  255. if (!p_enabled && margins) {
  256. margins->queue_free();
  257. margins = nullptr;
  258. } else if (p_enabled && !margins) {
  259. Size2 borders = Size2(4, 4) * EDSCALE;
  260. margins = memnew(MarginContainer);
  261. margins->add_theme_constant_override("margin_right", borders.width);
  262. margins->add_theme_constant_override("margin_top", borders.height);
  263. margins->add_theme_constant_override("margin_left", borders.width);
  264. margins->add_theme_constant_override("margin_bottom", borders.height);
  265. window->add_child(margins);
  266. margins->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  267. }
  268. }
  269. void WindowWrapper::grab_window_focus() {
  270. if (get_window_enabled() && is_visible()) {
  271. window->grab_focus();
  272. }
  273. }
  274. WindowWrapper::WindowWrapper() {
  275. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  276. return;
  277. }
  278. window = memnew(Window);
  279. window->set_wrap_controls(true);
  280. add_child(window);
  281. window->hide();
  282. window->connect("close_requested", callable_mp(this, &WindowWrapper::set_window_enabled).bind(false));
  283. window->connect("size_changed", callable_mp(this, &WindowWrapper::_window_size_changed));
  284. ShortcutBin *capturer = memnew(ShortcutBin);
  285. window->add_child(capturer);
  286. window_background = memnew(Panel);
  287. window_background->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  288. window->add_child(window_background);
  289. ProgressDialog::get_singleton()->add_host_window(window);
  290. }
  291. // ScreenSelect
  292. void ScreenSelect::_build_advanced_menu() {
  293. // Clear old screen list.
  294. while (screen_list->get_child_count(false) > 0) {
  295. Node *child = screen_list->get_child(0);
  296. screen_list->remove_child(child);
  297. child->queue_free();
  298. }
  299. // Populate screen list.
  300. const real_t height = real_t(get_theme_font_size(SceneStringName(font_size))) * 1.5;
  301. int current_screen = get_window()->get_current_screen();
  302. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  303. Button *button = memnew(Button);
  304. Size2 screen_size = Size2(DisplayServer::get_singleton()->screen_get_size(i));
  305. Size2 button_size = Size2(height * (screen_size.x / screen_size.y), height);
  306. button->set_custom_minimum_size(button_size);
  307. screen_list->add_child(button);
  308. button->set_text(itos(i));
  309. button->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  310. button->set_tooltip_text(vformat(TTR("Make this panel floating in the screen %d."), i));
  311. if (i == current_screen) {
  312. Color accent_color = get_theme_color("accent_color", EditorStringName(Editor));
  313. button->add_theme_color_override(SceneStringName(font_color), accent_color);
  314. }
  315. button->connect(SceneStringName(pressed), callable_mp(this, &ScreenSelect::_emit_screen_signal).bind(i));
  316. button->connect(SceneStringName(pressed), callable_mp(static_cast<BaseButton *>(this), &ScreenSelect::set_pressed).bind(false));
  317. button->connect(SceneStringName(pressed), callable_mp(static_cast<Window *>(popup), &Popup::hide));
  318. }
  319. }
  320. void ScreenSelect::_emit_screen_signal(int p_screen_idx) {
  321. if (!is_disabled()) {
  322. emit_signal("request_open_in_screen", p_screen_idx);
  323. }
  324. }
  325. void ScreenSelect::_bind_methods() {
  326. ADD_SIGNAL(MethodInfo("request_open_in_screen", PropertyInfo(Variant::INT, "screen")));
  327. }
  328. void ScreenSelect::_notification(int p_what) {
  329. switch (p_what) {
  330. case NOTIFICATION_READY: {
  331. connect(SceneStringName(gui_input), callable_mp(this, &ScreenSelect::_handle_mouse_shortcut));
  332. } break;
  333. case NOTIFICATION_THEME_CHANGED: {
  334. set_button_icon(get_editor_theme_icon("MakeFloating"));
  335. const real_t popup_height = real_t(get_theme_font_size(SceneStringName(font_size))) * 2.0;
  336. popup->set_min_size(Size2(0, popup_height * 3));
  337. } break;
  338. }
  339. }
  340. void ScreenSelect::_handle_mouse_shortcut(const Ref<InputEvent> &p_event) {
  341. const Ref<InputEventMouseButton> mouse_button = p_event;
  342. if (mouse_button.is_valid()) {
  343. if (mouse_button->is_pressed() && mouse_button->get_button_index() == MouseButton::LEFT) {
  344. _emit_screen_signal(get_window()->get_current_screen());
  345. accept_event();
  346. }
  347. }
  348. }
  349. void ScreenSelect::_show_popup() {
  350. // Adapted from /scene/gui/menu_button.cpp::show_popup
  351. if (!get_viewport()) {
  352. return;
  353. }
  354. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  355. popup->set_size(Size2(size.width, 0));
  356. Point2 gp = get_screen_position();
  357. gp.y += size.y;
  358. if (is_layout_rtl()) {
  359. gp.x += size.width - popup->get_size().width;
  360. }
  361. popup->set_position(gp);
  362. popup->popup();
  363. }
  364. void ScreenSelect::pressed() {
  365. if (popup->is_visible()) {
  366. popup->hide();
  367. return;
  368. }
  369. _build_advanced_menu();
  370. _show_popup();
  371. }
  372. ScreenSelect::ScreenSelect() {
  373. set_button_mask(MouseButtonMask::RIGHT);
  374. set_flat(true);
  375. set_toggle_mode(true);
  376. set_focus_mode(FOCUS_NONE);
  377. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  378. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  379. set_disabled(true);
  380. set_tooltip_text(EditorNode::get_singleton()->get_multiwindow_support_tooltip_text());
  381. } else {
  382. set_tooltip_text(TTR("Make this panel floating.\nRight-click to open the screen selector."));
  383. }
  384. // Create the popup.
  385. const Size2 borders = Size2(4, 4) * EDSCALE;
  386. popup = memnew(PopupPanel);
  387. popup->connect("popup_hide", callable_mp(static_cast<BaseButton *>(this), &ScreenSelect::set_pressed).bind(false));
  388. add_child(popup);
  389. MarginContainer *popup_root = memnew(MarginContainer);
  390. popup_root->add_theme_constant_override("margin_right", borders.width);
  391. popup_root->add_theme_constant_override("margin_top", borders.height);
  392. popup_root->add_theme_constant_override("margin_left", borders.width);
  393. popup_root->add_theme_constant_override("margin_bottom", borders.height);
  394. popup->add_child(popup_root);
  395. VBoxContainer *vb = memnew(VBoxContainer);
  396. vb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  397. popup_root->add_child(vb);
  398. Label *description = memnew(Label(TTR("Select Screen")));
  399. description->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  400. vb->add_child(description);
  401. screen_list = memnew(HBoxContainer);
  402. screen_list->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  403. vb->add_child(screen_list);
  404. popup_root->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  405. }