window_wrapper.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. if (wrapped_control->get_parent() != parent) {
  90. // Move the control to the window.
  91. wrapped_control->reparent(parent, false);
  92. _set_window_rect(p_rect);
  93. wrapped_control->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  94. } else if (!p_visible) {
  95. // Remove control from window.
  96. wrapped_control->reparent(this, false);
  97. }
  98. window->set_visible(p_visible);
  99. if (!p_visible && !override_close_request) {
  100. emit_signal("window_close_requested");
  101. }
  102. emit_signal("window_visibility_changed", p_visible);
  103. }
  104. void WindowWrapper::_set_window_rect(const Rect2 p_rect) {
  105. // Set the window rect even when the window is maximized to have a good default size
  106. // when the user remove the maximized mode.
  107. window->set_position(p_rect.position);
  108. window->set_size(p_rect.size);
  109. if (EDITOR_GET("interface/multi_window/maximize_window")) {
  110. window->set_mode(Window::MODE_MAXIMIZED);
  111. }
  112. }
  113. void WindowWrapper::_window_size_changed() {
  114. emit_signal(SNAME("window_size_changed"));
  115. }
  116. void WindowWrapper::_window_close_request() {
  117. if (override_close_request) {
  118. emit_signal("window_close_requested");
  119. } else {
  120. set_window_enabled(false);
  121. }
  122. }
  123. void WindowWrapper::_bind_methods() {
  124. ADD_SIGNAL(MethodInfo("window_visibility_changed", PropertyInfo(Variant::BOOL, "visible")));
  125. ADD_SIGNAL(MethodInfo("window_close_requested"));
  126. ADD_SIGNAL(MethodInfo("window_size_changed"));
  127. }
  128. void WindowWrapper::_notification(int p_what) {
  129. if (!is_window_available()) {
  130. return;
  131. }
  132. switch (p_what) {
  133. case NOTIFICATION_VISIBILITY_CHANGED: {
  134. // Grab the focus when WindowWrapper.set_visible(true) is called
  135. // and the window is showing.
  136. grab_window_focus();
  137. } break;
  138. case NOTIFICATION_READY: {
  139. set_process_shortcut_input(true);
  140. } break;
  141. case NOTIFICATION_THEME_CHANGED: {
  142. window_background->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("PanelForeground", EditorStringName(EditorStyles)));
  143. } break;
  144. }
  145. }
  146. void WindowWrapper::shortcut_input(const Ref<InputEvent> &p_event) {
  147. if (enable_shortcut.is_valid() && enable_shortcut->matches_event(p_event)) {
  148. set_window_enabled(true);
  149. }
  150. }
  151. void WindowWrapper::set_wrapped_control(Control *p_control, const Ref<Shortcut> &p_enable_shortcut) {
  152. ERR_FAIL_NULL(p_control);
  153. ERR_FAIL_COND(wrapped_control);
  154. wrapped_control = p_control;
  155. enable_shortcut = p_enable_shortcut;
  156. add_child(p_control);
  157. }
  158. Control *WindowWrapper::get_wrapped_control() const {
  159. return wrapped_control;
  160. }
  161. Control *WindowWrapper::release_wrapped_control() {
  162. set_window_enabled(false);
  163. if (wrapped_control) {
  164. Control *old_wrapped = wrapped_control;
  165. wrapped_control->get_parent()->remove_child(wrapped_control);
  166. wrapped_control = nullptr;
  167. return old_wrapped;
  168. }
  169. return nullptr;
  170. }
  171. bool WindowWrapper::is_window_available() const {
  172. return window != nullptr;
  173. }
  174. bool WindowWrapper::get_window_enabled() const {
  175. return is_window_available() ? window->is_visible() : false;
  176. }
  177. void WindowWrapper::set_window_enabled(bool p_enabled) {
  178. _set_window_enabled_with_rect(p_enabled, _get_default_window_rect());
  179. }
  180. Rect2i WindowWrapper::get_window_rect() const {
  181. ERR_FAIL_COND_V(!get_window_enabled(), Rect2i());
  182. return Rect2i(window->get_position(), window->get_size());
  183. }
  184. int WindowWrapper::get_window_screen() const {
  185. ERR_FAIL_COND_V(!get_window_enabled(), -1);
  186. return window->get_current_screen();
  187. }
  188. void WindowWrapper::restore_window(const Rect2i &p_rect, int p_screen) {
  189. ERR_FAIL_COND(!is_window_available());
  190. ERR_FAIL_INDEX(p_screen, DisplayServer::get_singleton()->get_screen_count());
  191. _set_window_enabled_with_rect(true, p_rect);
  192. window->set_current_screen(p_screen);
  193. }
  194. void WindowWrapper::restore_window_from_saved_position(const Rect2 p_window_rect, int p_screen, const Rect2 p_screen_rect) {
  195. ERR_FAIL_COND(!is_window_available());
  196. Rect2 window_rect = p_window_rect;
  197. int screen = p_screen;
  198. Rect2 restored_screen_rect = p_screen_rect;
  199. if (screen < 0 || screen >= DisplayServer::get_singleton()->get_screen_count()) {
  200. // Fallback to the main window screen if the saved screen is not available.
  201. screen = get_window()->get_window_id();
  202. }
  203. Rect2i real_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  204. if (restored_screen_rect == Rect2i()) {
  205. // Fallback to the target screen rect.
  206. restored_screen_rect = real_screen_rect;
  207. }
  208. if (window_rect == Rect2i()) {
  209. // Fallback to a standard rect.
  210. window_rect = Rect2i(restored_screen_rect.position + restored_screen_rect.size / 4, restored_screen_rect.size / 2);
  211. }
  212. // Adjust the window rect size in case the resolution changes.
  213. Vector2 screen_ratio = Vector2(real_screen_rect.size) / Vector2(restored_screen_rect.size);
  214. // The screen positioning may change, so remove the original screen position.
  215. window_rect.position -= restored_screen_rect.position;
  216. window_rect = Rect2i(window_rect.position * screen_ratio, window_rect.size * screen_ratio);
  217. window_rect.position += real_screen_rect.position;
  218. // Make sure to restore the window if the user minimized it the last time it was displayed.
  219. if (window->get_mode() == Window::MODE_MINIMIZED) {
  220. window->set_mode(Window::MODE_WINDOWED);
  221. }
  222. // All good, restore the window.
  223. window->set_current_screen(p_screen);
  224. if (window->is_visible()) {
  225. _set_window_rect(window_rect);
  226. } else {
  227. _set_window_enabled_with_rect(true, window_rect);
  228. }
  229. }
  230. void WindowWrapper::enable_window_on_screen(int p_screen, bool p_auto_scale) {
  231. int current_screen = Object::cast_to<Window>(get_viewport())->get_current_screen();
  232. int screen = p_screen < 0 ? current_screen : p_screen;
  233. bool auto_scale = p_auto_scale && !EDITOR_GET("interface/multi_window/maximize_window");
  234. if (auto_scale && current_screen != screen) {
  235. Rect2 control_rect = _get_default_window_rect();
  236. Rect2i source_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(current_screen);
  237. Rect2i dest_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  238. // Adjust the window rect size in case the resolution changes.
  239. Vector2 screen_ratio = Vector2(source_screen_rect.size) / Vector2(dest_screen_rect.size);
  240. // The screen positioning may change, so remove the original screen position.
  241. control_rect.position -= source_screen_rect.position;
  242. control_rect = Rect2i(control_rect.position * screen_ratio, control_rect.size * screen_ratio);
  243. control_rect.position += dest_screen_rect.position;
  244. restore_window(control_rect, p_screen);
  245. } else {
  246. window->set_current_screen(p_screen);
  247. set_window_enabled(true);
  248. }
  249. }
  250. void WindowWrapper::set_window_title(const String &p_title) {
  251. if (!is_window_available()) {
  252. return;
  253. }
  254. window->set_title(p_title);
  255. }
  256. void WindowWrapper::set_margins_enabled(bool p_enabled) {
  257. if (!is_window_available()) {
  258. return;
  259. }
  260. if (!p_enabled && margins) {
  261. margins->queue_free();
  262. margins = nullptr;
  263. } else if (p_enabled && !margins) {
  264. Size2 borders = Size2(4, 4) * EDSCALE;
  265. margins = memnew(MarginContainer);
  266. margins->add_theme_constant_override("margin_right", borders.width);
  267. margins->add_theme_constant_override("margin_top", borders.height);
  268. margins->add_theme_constant_override("margin_left", borders.width);
  269. margins->add_theme_constant_override("margin_bottom", borders.height);
  270. window->add_child(margins);
  271. margins->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  272. }
  273. }
  274. Size2 WindowWrapper::get_margins_size() {
  275. if (!margins) {
  276. return Size2();
  277. }
  278. return Size2(margins->get_margin_size(SIDE_LEFT) + margins->get_margin_size(SIDE_RIGHT), margins->get_margin_size(SIDE_TOP) + margins->get_margin_size(SIDE_RIGHT));
  279. }
  280. Size2 WindowWrapper::get_margins_top_left() {
  281. if (!margins) {
  282. return Size2();
  283. }
  284. return Size2(margins->get_margin_size(SIDE_LEFT), margins->get_margin_size(SIDE_TOP));
  285. }
  286. void WindowWrapper::grab_window_focus() {
  287. if (get_window_enabled() && is_visible()) {
  288. window->grab_focus();
  289. }
  290. }
  291. void WindowWrapper::set_override_close_request(bool p_enabled) {
  292. override_close_request = p_enabled;
  293. }
  294. WindowWrapper::WindowWrapper() {
  295. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  296. return;
  297. }
  298. window = memnew(Window);
  299. window_id = window->get_instance_id();
  300. window->set_wrap_controls(true);
  301. add_child(window);
  302. window->hide();
  303. window->connect("close_requested", callable_mp(this, &WindowWrapper::_window_close_request));
  304. window->connect("size_changed", callable_mp(this, &WindowWrapper::_window_size_changed));
  305. ShortcutBin *capturer = memnew(ShortcutBin);
  306. window->add_child(capturer);
  307. window_background = memnew(Panel);
  308. window_background->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  309. window->add_child(window_background);
  310. ProgressDialog::get_singleton()->add_host_window(window);
  311. }
  312. WindowWrapper::~WindowWrapper() {
  313. if (ObjectDB::get_instance(window_id)) {
  314. ProgressDialog::get_singleton()->remove_host_window(window);
  315. }
  316. }
  317. // ScreenSelect
  318. void ScreenSelect::_build_advanced_menu() {
  319. // Clear old screen list.
  320. while (screen_list->get_child_count(false) > 0) {
  321. Node *child = screen_list->get_child(0);
  322. screen_list->remove_child(child);
  323. child->queue_free();
  324. }
  325. // Populate screen list.
  326. const real_t height = real_t(get_theme_font_size(SceneStringName(font_size))) * 1.5;
  327. int current_screen = get_window()->get_current_screen();
  328. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  329. Button *button = memnew(Button);
  330. Size2 screen_size = Size2(DisplayServer::get_singleton()->screen_get_size(i));
  331. Size2 button_size = Size2(height * (screen_size.x / screen_size.y), height);
  332. button->set_custom_minimum_size(button_size);
  333. screen_list->add_child(button);
  334. button->set_text(itos(i));
  335. button->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  336. button->set_tooltip_text(vformat(TTR("Make this panel floating in the screen %d."), i));
  337. if (i == current_screen) {
  338. Color accent_color = get_theme_color("accent_color", EditorStringName(Editor));
  339. button->add_theme_color_override(SceneStringName(font_color), accent_color);
  340. }
  341. button->connect(SceneStringName(pressed), callable_mp(this, &ScreenSelect::_emit_screen_signal).bind(i));
  342. button->connect(SceneStringName(pressed), callable_mp(static_cast<BaseButton *>(this), &ScreenSelect::set_pressed).bind(false));
  343. button->connect(SceneStringName(pressed), callable_mp(static_cast<Window *>(popup), &Popup::hide));
  344. }
  345. }
  346. void ScreenSelect::_emit_screen_signal(int p_screen_idx) {
  347. if (!is_disabled()) {
  348. emit_signal("request_open_in_screen", p_screen_idx);
  349. }
  350. }
  351. void ScreenSelect::_bind_methods() {
  352. ADD_SIGNAL(MethodInfo("request_open_in_screen", PropertyInfo(Variant::INT, "screen")));
  353. }
  354. void ScreenSelect::_notification(int p_what) {
  355. switch (p_what) {
  356. case NOTIFICATION_READY: {
  357. connect(SceneStringName(gui_input), callable_mp(this, &ScreenSelect::_handle_mouse_shortcut));
  358. } break;
  359. case NOTIFICATION_THEME_CHANGED: {
  360. set_button_icon(get_editor_theme_icon("MakeFloating"));
  361. const real_t popup_height = real_t(get_theme_font_size(SceneStringName(font_size))) * 2.0;
  362. popup->set_min_size(Size2(0, popup_height * 3));
  363. } break;
  364. }
  365. }
  366. void ScreenSelect::_handle_mouse_shortcut(const Ref<InputEvent> &p_event) {
  367. const Ref<InputEventMouseButton> mouse_button = p_event;
  368. if (mouse_button.is_valid()) {
  369. if (mouse_button->is_pressed() && mouse_button->get_button_index() == MouseButton::LEFT) {
  370. _emit_screen_signal(get_window()->get_current_screen());
  371. accept_event();
  372. }
  373. }
  374. }
  375. void ScreenSelect::_show_popup() {
  376. // Adapted from /scene/gui/menu_button.cpp::show_popup
  377. if (!get_viewport()) {
  378. return;
  379. }
  380. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  381. popup->set_size(Size2(size.width, 0));
  382. Point2 gp = get_screen_position();
  383. gp.y += size.y;
  384. if (is_layout_rtl()) {
  385. gp.x += size.width - popup->get_size().width;
  386. }
  387. popup->set_position(gp);
  388. popup->popup();
  389. }
  390. void ScreenSelect::pressed() {
  391. if (popup->is_visible()) {
  392. popup->hide();
  393. return;
  394. }
  395. _build_advanced_menu();
  396. _show_popup();
  397. }
  398. ScreenSelect::ScreenSelect() {
  399. set_button_mask(MouseButtonMask::RIGHT);
  400. set_flat(true);
  401. set_toggle_mode(true);
  402. set_focus_mode(FOCUS_NONE);
  403. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  404. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  405. set_disabled(true);
  406. set_tooltip_text(EditorNode::get_singleton()->get_multiwindow_support_tooltip_text());
  407. } else {
  408. set_tooltip_text(TTR("Make this panel floating.\nRight-click to open the screen selector."));
  409. }
  410. // Create the popup.
  411. const Size2 borders = Size2(4, 4) * EDSCALE;
  412. popup = memnew(PopupPanel);
  413. popup->connect("popup_hide", callable_mp(static_cast<BaseButton *>(this), &ScreenSelect::set_pressed).bind(false));
  414. add_child(popup);
  415. MarginContainer *popup_root = memnew(MarginContainer);
  416. popup_root->add_theme_constant_override("margin_right", borders.width);
  417. popup_root->add_theme_constant_override("margin_top", borders.height);
  418. popup_root->add_theme_constant_override("margin_left", borders.width);
  419. popup_root->add_theme_constant_override("margin_bottom", borders.height);
  420. popup->add_child(popup_root);
  421. VBoxContainer *vb = memnew(VBoxContainer);
  422. vb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  423. popup_root->add_child(vb);
  424. Label *description = memnew(Label(TTR("Select Screen")));
  425. description->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  426. vb->add_child(description);
  427. screen_list = memnew(HBoxContainer);
  428. screen_list->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  429. vb->add_child(screen_list);
  430. popup_root->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  431. }