window_wrapper.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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_string_names.h"
  33. #include "editor/gui/progress_dialog.h"
  34. #include "editor/settings/editor_settings.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 (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
  200. window_rect = Rect2i();
  201. restored_screen_rect = Rect2i();
  202. }
  203. if (screen < 0 || screen >= DisplayServer::get_singleton()->get_screen_count()) {
  204. // Fallback to the main window screen if the saved screen is not available.
  205. screen = get_window()->get_window_id();
  206. }
  207. Rect2i real_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  208. if (restored_screen_rect == Rect2i()) {
  209. // Fallback to the target screen rect.
  210. restored_screen_rect = real_screen_rect;
  211. }
  212. if (window_rect == Rect2i()) {
  213. // Fallback to a standard rect.
  214. window_rect = Rect2i(restored_screen_rect.position + restored_screen_rect.size / 4, restored_screen_rect.size / 2);
  215. }
  216. // Adjust the window rect size in case the resolution changes.
  217. Vector2 screen_ratio = Vector2(real_screen_rect.size) / Vector2(restored_screen_rect.size);
  218. // The screen positioning may change, so remove the original screen position.
  219. window_rect.position -= restored_screen_rect.position;
  220. window_rect = Rect2i(window_rect.position * screen_ratio, window_rect.size * screen_ratio);
  221. window_rect.position += real_screen_rect.position;
  222. // Make sure to restore the window if the user minimized it the last time it was displayed.
  223. if (window->get_mode() == Window::MODE_MINIMIZED) {
  224. window->set_mode(Window::MODE_WINDOWED);
  225. }
  226. // All good, restore the window.
  227. window->set_current_screen(p_screen);
  228. if (window->is_visible()) {
  229. _set_window_rect(window_rect);
  230. } else {
  231. _set_window_enabled_with_rect(true, window_rect);
  232. }
  233. }
  234. void WindowWrapper::enable_window_on_screen(int p_screen, bool p_auto_scale) {
  235. int current_screen = Object::cast_to<Window>(get_viewport())->get_current_screen();
  236. int screen = p_screen < 0 ? current_screen : p_screen;
  237. bool auto_scale = p_auto_scale && !EDITOR_GET("interface/multi_window/maximize_window");
  238. if (auto_scale && current_screen != screen) {
  239. Rect2 control_rect = _get_default_window_rect();
  240. Rect2i source_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(current_screen);
  241. Rect2i dest_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  242. // Adjust the window rect size in case the resolution changes.
  243. Vector2 screen_ratio = Vector2(source_screen_rect.size) / Vector2(dest_screen_rect.size);
  244. // The screen positioning may change, so remove the original screen position.
  245. control_rect.position -= source_screen_rect.position;
  246. control_rect = Rect2i(control_rect.position * screen_ratio, control_rect.size * screen_ratio);
  247. control_rect.position += dest_screen_rect.position;
  248. restore_window(control_rect, p_screen);
  249. } else {
  250. window->set_current_screen(p_screen);
  251. set_window_enabled(true);
  252. }
  253. }
  254. void WindowWrapper::set_window_title(const String &p_title) {
  255. if (!is_window_available()) {
  256. return;
  257. }
  258. window->set_title(p_title);
  259. }
  260. void WindowWrapper::set_margins_enabled(bool p_enabled) {
  261. if (!is_window_available()) {
  262. return;
  263. }
  264. if (!p_enabled && margins) {
  265. margins->queue_free();
  266. margins = nullptr;
  267. } else if (p_enabled && !margins) {
  268. Size2 borders = Size2(4, 4) * EDSCALE;
  269. margins = memnew(MarginContainer);
  270. margins->add_theme_constant_override("margin_right", borders.width);
  271. margins->add_theme_constant_override("margin_top", borders.height);
  272. margins->add_theme_constant_override("margin_left", borders.width);
  273. margins->add_theme_constant_override("margin_bottom", borders.height);
  274. window->add_child(margins);
  275. margins->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  276. }
  277. }
  278. Size2 WindowWrapper::get_margins_size() {
  279. if (!margins) {
  280. return Size2();
  281. }
  282. 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));
  283. }
  284. Size2 WindowWrapper::get_margins_top_left() {
  285. if (!margins) {
  286. return Size2();
  287. }
  288. return Size2(margins->get_margin_size(SIDE_LEFT), margins->get_margin_size(SIDE_TOP));
  289. }
  290. void WindowWrapper::grab_window_focus() {
  291. if (get_window_enabled() && is_visible()) {
  292. window->grab_focus();
  293. }
  294. }
  295. void WindowWrapper::set_override_close_request(bool p_enabled) {
  296. override_close_request = p_enabled;
  297. }
  298. WindowWrapper::WindowWrapper() {
  299. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  300. return;
  301. }
  302. window = memnew(Window);
  303. window_id = window->get_instance_id();
  304. window->set_wrap_controls(true);
  305. add_child(window);
  306. window->hide();
  307. window->connect("close_requested", callable_mp(this, &WindowWrapper::_window_close_request));
  308. window->connect("size_changed", callable_mp(this, &WindowWrapper::_window_size_changed));
  309. ShortcutBin *capturer = memnew(ShortcutBin);
  310. window->add_child(capturer);
  311. window_background = memnew(Panel);
  312. window_background->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  313. window->add_child(window_background);
  314. ProgressDialog::get_singleton()->add_host_window(window_id);
  315. }
  316. WindowWrapper::~WindowWrapper() {
  317. if (ProgressDialog::get_singleton()) {
  318. ProgressDialog::get_singleton()->remove_host_window(window_id);
  319. }
  320. }
  321. // ScreenSelect
  322. void ScreenSelect::_build_advanced_menu() {
  323. // Clear old screen list.
  324. while (screen_list->get_child_count(false) > 0) {
  325. Node *child = screen_list->get_child(0);
  326. screen_list->remove_child(child);
  327. child->queue_free();
  328. }
  329. // Populate screen list.
  330. const real_t height = real_t(get_theme_font_size(SceneStringName(font_size))) * 1.5;
  331. int current_screen = get_window()->get_current_screen();
  332. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  333. Button *button = memnew(Button);
  334. Size2 screen_size = Size2(DisplayServer::get_singleton()->screen_get_size(i));
  335. Size2 button_size = Size2(height * (screen_size.x / screen_size.y), height);
  336. button->set_custom_minimum_size(button_size);
  337. screen_list->add_child(button);
  338. button->set_text(itos(i));
  339. button->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  340. button->set_tooltip_text(vformat(TTR("Make this panel floating in the screen %d."), i));
  341. if (i == current_screen) {
  342. Color accent_color = get_theme_color("accent_color", EditorStringName(Editor));
  343. button->add_theme_color_override(SceneStringName(font_color), accent_color);
  344. }
  345. button->connect(SceneStringName(pressed), callable_mp(this, &ScreenSelect::_emit_screen_signal).bind(i));
  346. button->connect(SceneStringName(pressed), callable_mp(static_cast<BaseButton *>(this), &ScreenSelect::set_pressed).bind(false));
  347. button->connect(SceneStringName(pressed), callable_mp(static_cast<Window *>(popup), &Popup::hide));
  348. }
  349. }
  350. void ScreenSelect::_emit_screen_signal(int p_screen_idx) {
  351. if (!is_disabled()) {
  352. emit_signal("request_open_in_screen", p_screen_idx);
  353. }
  354. }
  355. void ScreenSelect::_bind_methods() {
  356. ADD_SIGNAL(MethodInfo("request_open_in_screen", PropertyInfo(Variant::INT, "screen")));
  357. }
  358. void ScreenSelect::_notification(int p_what) {
  359. switch (p_what) {
  360. case NOTIFICATION_READY: {
  361. connect(SceneStringName(gui_input), callable_mp(this, &ScreenSelect::_handle_mouse_shortcut));
  362. } break;
  363. case NOTIFICATION_THEME_CHANGED: {
  364. set_button_icon(get_editor_theme_icon("MakeFloating"));
  365. const real_t popup_height = real_t(get_theme_font_size(SceneStringName(font_size))) * 2.0;
  366. popup->set_min_size(Size2(0, popup_height * 3));
  367. } break;
  368. }
  369. }
  370. void ScreenSelect::_handle_mouse_shortcut(const Ref<InputEvent> &p_event) {
  371. const Ref<InputEventMouseButton> mouse_button = p_event;
  372. if (mouse_button.is_valid()) {
  373. if (mouse_button->is_pressed() && mouse_button->get_button_index() == MouseButton::LEFT) {
  374. _emit_screen_signal(get_window()->get_current_screen());
  375. accept_event();
  376. }
  377. }
  378. }
  379. void ScreenSelect::_show_popup() {
  380. // Adapted from /scene/gui/menu_button.cpp::show_popup
  381. if (!get_viewport()) {
  382. return;
  383. }
  384. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  385. popup->set_size(Size2(size.width, 0));
  386. Point2 gp = get_screen_position();
  387. gp.y += size.y;
  388. if (is_layout_rtl()) {
  389. gp.x += size.width - popup->get_size().width;
  390. }
  391. popup->set_position(gp);
  392. popup->popup();
  393. }
  394. void ScreenSelect::pressed() {
  395. if (popup->is_visible()) {
  396. popup->hide();
  397. return;
  398. }
  399. _build_advanced_menu();
  400. _show_popup();
  401. }
  402. ScreenSelect::ScreenSelect() {
  403. set_button_mask(MouseButtonMask::RIGHT);
  404. set_theme_type_variation(SceneStringName(FlatButton));
  405. set_toggle_mode(true);
  406. set_focus_mode(FOCUS_NONE);
  407. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  408. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  409. set_disabled(true);
  410. set_tooltip_text(EditorNode::get_singleton()->get_multiwindow_support_tooltip_text());
  411. } else {
  412. set_tooltip_text(TTR("Make this panel floating.") + "\n" + TTR("Right-click to open the screen selector."));
  413. }
  414. // Create the popup.
  415. const Size2 borders = Size2(4, 4) * EDSCALE;
  416. popup = memnew(PopupPanel);
  417. popup->connect("popup_hide", callable_mp(static_cast<BaseButton *>(this), &ScreenSelect::set_pressed).bind(false));
  418. add_child(popup);
  419. MarginContainer *popup_root = memnew(MarginContainer);
  420. popup_root->add_theme_constant_override("margin_right", borders.width);
  421. popup_root->add_theme_constant_override("margin_top", borders.height);
  422. popup_root->add_theme_constant_override("margin_left", borders.width);
  423. popup_root->add_theme_constant_override("margin_bottom", borders.height);
  424. popup->add_child(popup_root);
  425. VBoxContainer *vb = memnew(VBoxContainer);
  426. vb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  427. popup_root->add_child(vb);
  428. Label *description = memnew(Label(TTR("Select Screen")));
  429. description->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  430. vb->add_child(description);
  431. screen_list = memnew(HBoxContainer);
  432. screen_list->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  433. vb->add_child(screen_list);
  434. popup_root->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  435. }