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