Debian Codegraph @

editor/gui/editor_title_bar.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_title_bar.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 "editor_title_bar.h"
32
33 void EditorTitleBar::gui_input(const Ref<InputEvent> &p_event) {
34 if (!can_move) {
35 return;
36 }
37
38 Ref<InputEventMouseMotion> mm = p_event;
39 if (mm.is_valid() && moving) {
40 if (mm->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
41 Window *w = Object::cast_to<Window>(get_viewport());
42 if (w) {
43 Point2 mouse = DisplayServer::get_singleton()->mouse_get_position();
44 w->set_position(mouse - click_pos);
45 }
46 } else {
47 moving = false;
48 }
49 }
50
51 Ref<InputEventMouseButton> mb = p_event;
52 if (mb.is_valid() && has_point(mb->get_position())) {
53 Window *w = Object::cast_to<Window>(get_viewport());
54 if (w) {
55 if (mb->get_button_index() == MouseButton::LEFT) {
56 if (mb->is_pressed()) {
57 if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_DRAG)) {
58 DisplayServer::get_singleton()->window_start_drag(w->get_window_id());
59 } else {
60 click_pos = DisplayServer::get_singleton()->mouse_get_position() - w->get_position();
61 moving = true;
62 }
63 } else {
64 moving = false;
65 }
66 }
67 if (mb->get_button_index() == MouseButton::LEFT && mb->is_double_click() && mb->is_pressed()) {
68 if (DisplayServer::get_singleton()->window_maximize_on_title_dbl_click()) {
69 if (w->get_mode() == Window::MODE_WINDOWED) {
70 w->set_mode(Window::MODE_MAXIMIZED);
71 } else if (w->get_mode() == Window::MODE_MAXIMIZED) {
72 w->set_mode(Window::MODE_WINDOWED);
73 }
74 } else if (DisplayServer::get_singleton()->window_minimize_on_title_dbl_click()) {
75 w->set_mode(Window::MODE_MINIMIZED);
76 }
77 moving = false;
78 }
79 }
80 }
81 }
82
83 void EditorTitleBar::set_center_control(Control *p_center_control) {
84 center_control = p_center_control;
85 }
86
87 Control *EditorTitleBar::get_center_control() const {
88 return center_control;
89 }
90
91 void EditorTitleBar::_notification(int p_what) {
92 switch (p_what) {
93 case NOTIFICATION_EXIT_TREE: {
94 SceneTree::get_singleton()->get_root()->disconnect(SceneStringName(nonclient_window_input), callable_mp(this, &EditorTitleBar::gui_input));
95 get_window()->set_nonclient_area(Rect2i());
96 } break;
97 case NOTIFICATION_ENTER_TREE: {
98 SceneTree::get_singleton()->get_root()->connect(SceneStringName(nonclient_window_input), callable_mp(this, &EditorTitleBar::gui_input));
99 [[fallthrough]];
100 }
101 case NOTIFICATION_RESIZED: {
102 get_window()->set_nonclient_area(get_global_transform().xform(Rect2i(get_position(), get_size())));
103 } break;
104 case NOTIFICATION_SORT_CHILDREN: {
105 if (!center_control) {
106 break;
107 }
108 Control *prev = nullptr;
109 Control *base = nullptr;
110 Control *next = nullptr;
111
112 bool rtl = is_layout_rtl();
113
114 int start;
115 int end;
116 int delta;
117 if (rtl) {
118 start = get_child_count() - 1;
119 end = -1;
120 delta = -1;
121 } else {
122 start = 0;
123 end = get_child_count();
124 delta = +1;
125 }
126
127 for (int i = start; i != end; i += delta) {
128 Control *c = as_sortable_control(get_child(i));
129 if (!c) {
130 continue;
131 }
132 if (base) {
133 next = c;
134 break;
135 }
136 if (c != center_control) {
137 prev = c;
138 continue;
139 }
140 base = c;
141 }
142 if (base && prev && next) {
143 Size2i title_size = get_size();
144 Size2i c_size = base->get_combined_minimum_size();
145
146 int min_offset = prev->get_position().x + prev->get_combined_minimum_size().x;
147 int max_offset = next->get_position().x + next->get_size().x - next->get_combined_minimum_size().x - c_size.x;
148
149 int offset = (title_size.width - c_size.width) / 2;
150 offset = CLAMP(offset, min_offset, max_offset);
151
152 fit_child_in_rect(prev, Rect2i(prev->get_position().x, 0, offset - prev->get_position().x, title_size.height));
153 fit_child_in_rect(base, Rect2i(offset, 0, c_size.width, title_size.height));
154 fit_child_in_rect(next, Rect2i(offset + c_size.width, 0, next->get_position().x + next->get_size().x - (offset + c_size.width), title_size.height));
155 }
156 } break;
157 }
158 }
159
160 void EditorTitleBar::set_can_move_window(bool p_enabled) {
161 can_move = p_enabled;
162 set_process_input(can_move);
163 }
164
165 bool EditorTitleBar::get_can_move_window() const {
166 return can_move;
167 }