40
40
#include " scene/gui/menu_button.h"
41
41
42
42
void Camera2DEditor::edit (Camera2D *p_camera) {
43
- if (p_camera == selected_camera) {
43
+ if (p_camera == selected_camera) {
44
44
return ;
45
45
}
46
+
47
+ const Callable update_overlays = callable_mp (plugin, &EditorPlugin::update_overlays);
48
+ const StringName update_signal = SNAME (" _camera_limit_enabled_updated" );
49
+
50
+ if (selected_camera) {
51
+ selected_camera->disconnect (update_signal, update_overlays);
52
+ }
46
53
selected_camera = p_camera;
54
+
55
+ if (selected_camera) {
56
+ selected_camera->connect (update_signal, update_overlays);
57
+ }
58
+ plugin->update_overlays ();
59
+ }
60
+
61
+ bool Camera2DEditor::forward_canvas_gui_input (const Ref<InputEvent> &p_event) {
62
+ if (!selected_camera || !selected_camera->is_limit_enabled ()) {
63
+ return false ;
64
+ }
65
+
66
+ Ref<InputEventMouseButton> mb = p_event;
67
+ if (mb.is_valid ()) {
68
+ if (mb->get_button_index () == MouseButton::LEFT) {
69
+ if (mb->is_pressed ()) {
70
+ const Rect2 limit_rect = selected_camera->get_limit_rect ();
71
+ const Vector2 pos = CanvasItemEditor::get_singleton ()->get_canvas_transform ().affine_inverse ().xform (mb->get_position ());
72
+ drag_revert = limit_rect;
73
+
74
+ if (pos.y > limit_rect.position .y && pos.y < limit_rect.get_end ().y ) {
75
+ if (ABS (pos.x - limit_rect.position .x ) < 8 ) {
76
+ drag_type = Drag::LEFT;
77
+ return true ;
78
+ } else if (ABS (pos.x - limit_rect.get_end ().x ) < 8 ) {
79
+ drag_type = Drag::RIGHT;
80
+ return true ;
81
+ }
82
+ } else if (pos.x > limit_rect.position .x && pos.x < limit_rect.get_end ().x ) {
83
+ if (ABS (pos.y - limit_rect.position .y ) < 8 ) {
84
+ drag_type = Drag::TOP;
85
+ return true ;
86
+ } else if (ABS (pos.y - limit_rect.get_end ().y ) < 8 ) {
87
+ drag_type = Drag::BOTTOM;
88
+ return true ;
89
+ }
90
+ }
91
+
92
+ if (limit_rect.has_point (pos)) {
93
+ drag_type = Drag::CENTER;
94
+ center_drag_point = pos - limit_rect.position ;
95
+ plugin->update_overlays ();
96
+ return true ;
97
+ }
98
+ } else if (drag_type != Drag::NONE) {
99
+ EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton ();
100
+ ur->create_action (TTR (" Edit Camera2D Limits" ));
101
+ ur->add_do_method (selected_camera, " _set_limit_rect" , selected_camera->get_limit_rect ());
102
+ ur->add_do_method (this , " _update_overlays_if_needed" , selected_camera);
103
+ ur->add_undo_method (selected_camera, " _set_limit_rect" , drag_revert);
104
+ ur->add_undo_method (this , " _update_overlays_if_needed" , selected_camera);
105
+ ur->commit_action (false );
106
+
107
+ drag_type = Drag::NONE;
108
+ return true ;
109
+ }
110
+ } else if (drag_type != Drag::NONE && mb->get_button_index () == MouseButton::RIGHT && mb->is_pressed ()) {
111
+ selected_camera->set_limit_rect (drag_revert);
112
+ drag_type = Drag::NONE;
113
+ plugin->update_overlays ();
114
+ return true ;
115
+ }
116
+ return false ;
117
+ }
118
+
119
+ if (drag_type == Drag::NONE) {
120
+ return false ;
121
+ }
122
+
123
+ Ref<InputEventMouseMotion> mm = p_event;
124
+ if (mm.is_valid ()) {
125
+ Vector2 pos = CanvasItemEditor::get_singleton ()->get_canvas_transform ().affine_inverse ().xform (mm->get_position ());
126
+ pos = CanvasItemEditor::get_singleton ()->snap_point (pos);
127
+
128
+ switch (drag_type) {
129
+ case Drag::LEFT: {
130
+ selected_camera->set_limit (SIDE_LEFT, MIN (selected_camera->get_limit (SIDE_RIGHT), pos.x ));
131
+ plugin->update_overlays ();
132
+ } break ;
133
+
134
+ case Drag::RIGHT: {
135
+ selected_camera->set_limit (SIDE_RIGHT, MAX (selected_camera->get_limit (SIDE_LEFT), pos.x ));
136
+ plugin->update_overlays ();
137
+ } break ;
138
+
139
+ case Drag::TOP: {
140
+ selected_camera->set_limit (SIDE_TOP, MIN (selected_camera->get_limit (SIDE_BOTTOM), pos.y ));
141
+ plugin->update_overlays ();
142
+ } break ;
143
+
144
+ case Drag::BOTTOM: {
145
+ selected_camera->set_limit (SIDE_BOTTOM, MAX (selected_camera->get_limit (SIDE_TOP), pos.y ));
146
+ plugin->update_overlays ();
147
+ } break ;
148
+
149
+ case Drag::CENTER: {
150
+ Rect2 target_rect = selected_camera->get_limit_rect ();
151
+ target_rect.position = pos - center_drag_point;
152
+ selected_camera->set_limit_rect (target_rect);
153
+ plugin->update_overlays ();
154
+ } break ;
155
+ }
156
+ return true ;
157
+ }
158
+
159
+ return false ;
160
+ }
161
+
162
+ void Camera2DEditor::forward_canvas_draw_over_viewport (Control *p_overlay) {
163
+ if (!selected_camera || !selected_camera->is_limit_enabled ()) {
164
+ return ;
165
+ }
166
+ Rect2 limit_rect = selected_camera->get_limit_rect ();
167
+ limit_rect = CanvasItemEditor::get_singleton ()->get_canvas_transform ().xform (limit_rect);
168
+ p_overlay->draw_rect (limit_rect, Color (1 , 1 , 0.25 , 0.63 ), false , 3 );
47
169
}
48
170
49
171
void Camera2DEditor::_menu_option (int p_option) {
50
172
switch (p_option) {
51
173
case MENU_SNAP_LIMITS_TO_VIEWPORT: {
52
174
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton ();
53
- Rect2 prev_rect = selected_camera->get_limit_rect ();
54
- ur->create_action (TTR (" Snap the Limits to the Viewport" ), UndoRedo::MERGE_DISABLE, selected_camera);
55
- ur->add_do_method (this , " _snap_limits_to_viewport" );
56
- ur->add_do_reference (selected_camera);
57
- ur->add_undo_method (this , " _undo_snap_limits_to_viewport" , prev_rect);
175
+ ur->create_action (TTR (" Snap Camera2D Limits to the Viewport" ), UndoRedo::MERGE_DISABLE, selected_camera);
176
+ ur->add_do_method (this , " _snap_limits_to_viewport" , selected_camera);
177
+ ur->add_undo_method (selected_camera, " _set_limit_rect" , selected_camera->get_limit_rect ());
178
+ ur->add_undo_method (this , " _update_overlays_if_needed" , selected_camera);
58
179
ur->commit_action ();
59
180
} break ;
60
181
}
61
182
}
62
183
63
- void Camera2DEditor::_snap_limits_to_viewport () {
64
- selected_camera->set_limit (SIDE_LEFT, 0 );
65
- selected_camera->set_limit (SIDE_TOP, 0 );
66
- selected_camera->set_limit (SIDE_RIGHT, GLOBAL_GET (" display/window/size/viewport_width" ));
67
- selected_camera->set_limit (SIDE_BOTTOM, GLOBAL_GET (" display/window/size/viewport_height" ));
184
+ void Camera2DEditor::_snap_limits_to_viewport (Camera2D *p_camera) {
185
+ p_camera->set_limit (SIDE_LEFT, 0 );
186
+ p_camera->set_limit (SIDE_TOP, 0 );
187
+ p_camera->set_limit (SIDE_RIGHT, GLOBAL_GET (" display/window/size/viewport_width" ));
188
+ p_camera->set_limit (SIDE_BOTTOM, GLOBAL_GET (" display/window/size/viewport_height" ));
189
+ _update_overlays_if_needed (p_camera);
68
190
}
69
191
70
- void Camera2DEditor::_undo_snap_limits_to_viewport (const Rect2 &p_prev_rect) {
71
- Point2 end = p_prev_rect.get_end ();
72
- selected_camera->set_limit (SIDE_LEFT, p_prev_rect.position .x );
73
- selected_camera->set_limit (SIDE_TOP, p_prev_rect.position .y );
74
- selected_camera->set_limit (SIDE_RIGHT, end.x );
75
- selected_camera->set_limit (SIDE_BOTTOM, end.y );
192
+ void Camera2DEditor::_update_overlays_if_needed (Camera2D *p_camera) {
193
+ if (p_camera == selected_camera) {
194
+ plugin->update_overlays ();
195
+ }
76
196
}
77
197
78
198
void Camera2DEditor::_notification (int p_what) {
@@ -84,56 +204,24 @@ void Camera2DEditor::_notification(int p_what) {
84
204
}
85
205
86
206
void Camera2DEditor::_bind_methods () {
87
- ClassDB::bind_method (D_METHOD (" _snap_limits_to_viewport" ), &Camera2DEditor::_snap_limits_to_viewport);
88
- ClassDB::bind_method (D_METHOD (" _undo_snap_limits_to_viewport " , " prev_rect " ), &Camera2DEditor::_undo_snap_limits_to_viewport );
207
+ ClassDB::bind_method (D_METHOD (" _snap_limits_to_viewport" , " camera " ), &Camera2DEditor::_snap_limits_to_viewport);
208
+ ClassDB::bind_method (D_METHOD (" _update_overlays_if_needed " , " camera " ), &Camera2DEditor::_update_overlays_if_needed );
89
209
}
90
210
91
- Camera2DEditor::Camera2DEditor () {
92
- options = memnew (MenuButton);
93
-
94
- CanvasItemEditor::get_singleton ()->add_control_to_menu_panel (options);
211
+ Camera2DEditor::Camera2DEditor (EditorPlugin *p_plugin) {
212
+ plugin = p_plugin;
95
213
214
+ options = memnew (MenuButton);
96
215
options->set_text (TTRC (" Camera2D" ));
97
-
98
216
options->get_popup ()->add_item (TTRC (" Snap the Limits to the Viewport" ), MENU_SNAP_LIMITS_TO_VIEWPORT);
99
217
options->set_switch_on_hover (true );
100
-
218
+ options->hide ();
219
+ CanvasItemEditor::get_singleton ()->add_control_to_menu_panel (options);
101
220
options->get_popup ()->connect (SceneStringName (id_pressed), callable_mp (this , &Camera2DEditor::_menu_option));
102
-
103
- add_user_signal (MethodInfo (" _editor_theme_changed" ));
104
- }
105
-
106
- void Camera2DEditorPlugin::_update_approach_text_visibility () {
107
- if (camera_2d_editor->selected_camera == nullptr ) {
108
- return ;
109
- }
110
- approach_to_move_rect->set_visible (camera_2d_editor->selected_camera ->is_limit_enabled ());
111
- }
112
-
113
- void Camera2DEditorPlugin::_editor_theme_changed () {
114
- approach_to_move_rect->remove_theme_color_override (SceneStringName (font_color));
115
- approach_to_move_rect->add_theme_color_override (SceneStringName (font_color), Color (0 .6f , 0 .6f , 0 .6f , 1 ));
116
- approach_to_move_rect->add_theme_color_override (" font_shadow_color" , Color (0 .2f , 0 .2f , 0 .2f , 1 ));
117
- approach_to_move_rect->add_theme_constant_override (" shadow_outline_size" , 1 * EDSCALE);
118
- approach_to_move_rect->add_theme_constant_override (" line_spacing" , 0 );
119
221
}
120
222
121
223
void Camera2DEditorPlugin::edit (Object *p_object) {
122
- Callable update_text = callable_mp (this , &Camera2DEditorPlugin::_update_approach_text_visibility);
123
- StringName update_signal = SNAME (" _camera_limit_enabled_updated" );
124
-
125
- Camera2D *prev_cam = camera_2d_editor->selected_camera ;
126
- if (prev_cam != nullptr && prev_cam->is_connected (update_signal, update_text)) {
127
- prev_cam->disconnect (update_signal, update_text);
128
- }
129
- Camera2D *cam = Object::cast_to<Camera2D>(p_object);
130
- if (cam != nullptr ) {
131
- camera_2d_editor->edit (cam);
132
- _update_approach_text_visibility ();
133
- if (!cam->is_connected (update_signal, update_text)) {
134
- cam->connect (update_signal, update_text);
135
- }
136
- }
224
+ camera_2d_editor->edit (Object::cast_to<Camera2D>(p_object));
137
225
}
138
226
139
227
bool Camera2DEditorPlugin::handles (Object *p_object) const {
@@ -143,24 +231,12 @@ bool Camera2DEditorPlugin::handles(Object *p_object) const {
143
231
void Camera2DEditorPlugin::make_visible (bool p_visible) {
144
232
if (p_visible) {
145
233
camera_2d_editor->options ->show ();
146
- approach_to_move_rect->show ();
147
234
} else {
148
235
camera_2d_editor->options ->hide ();
149
- approach_to_move_rect->hide ();
150
236
}
151
237
}
152
238
153
239
Camera2DEditorPlugin::Camera2DEditorPlugin () {
154
- camera_2d_editor = memnew (Camera2DEditor);
240
+ camera_2d_editor = memnew (Camera2DEditor ( this ) );
155
241
EditorNode::get_singleton ()->get_gui_base ()->add_child (camera_2d_editor);
156
- camera_2d_editor->connect (SNAME (" _editor_theme_changed" ), callable_mp (this , &Camera2DEditorPlugin::_editor_theme_changed));
157
-
158
- approach_to_move_rect = memnew (Label);
159
- approach_to_move_rect->set_focus_mode (Control::FOCUS_ACCESSIBILITY);
160
- approach_to_move_rect->set_text (TTRC (" In Move Mode: \n Hold Ctrl + left mouse button to move the limit rectangle.\n Hold left mouse button to move the camera only." ));
161
- approach_to_move_rect->hide ();
162
- _editor_theme_changed ();
163
- CanvasItemEditor::get_singleton ()->get_controls_container ()->add_child (approach_to_move_rect);
164
-
165
- make_visible (false );
166
242
}
0 commit comments