Skip to content

Commit 0a43177

Browse files
committed
Add a dedicated editor for Camera2D limits
1 parent 19bb187 commit 0a43177

File tree

4 files changed

+176
-167
lines changed

4 files changed

+176
-167
lines changed

editor/plugins/camera_2d_editor_plugin.cpp

Lines changed: 146 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,159 @@
4040
#include "scene/gui/menu_button.h"
4141

4242
void Camera2DEditor::edit(Camera2D *p_camera) {
43-
if (p_camera == selected_camera) {
43+
if (p_camera == selected_camera) {
4444
return;
4545
}
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+
}
4653
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);
47169
}
48170

49171
void Camera2DEditor::_menu_option(int p_option) {
50172
switch (p_option) {
51173
case MENU_SNAP_LIMITS_TO_VIEWPORT: {
52174
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);
58179
ur->commit_action();
59180
} break;
60181
}
61182
}
62183

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);
68190
}
69191

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+
}
76196
}
77197

78198
void Camera2DEditor::_notification(int p_what) {
@@ -84,56 +204,24 @@ void Camera2DEditor::_notification(int p_what) {
84204
}
85205

86206
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);
89209
}
90210

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;
95213

214+
options = memnew(MenuButton);
96215
options->set_text(TTRC("Camera2D"));
97-
98216
options->get_popup()->add_item(TTRC("Snap the Limits to the Viewport"), MENU_SNAP_LIMITS_TO_VIEWPORT);
99217
options->set_switch_on_hover(true);
100-
218+
options->hide();
219+
CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
101220
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);
119221
}
120222

121223
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));
137225
}
138226

139227
bool Camera2DEditorPlugin::handles(Object *p_object) const {
@@ -143,24 +231,12 @@ bool Camera2DEditorPlugin::handles(Object *p_object) const {
143231
void Camera2DEditorPlugin::make_visible(bool p_visible) {
144232
if (p_visible) {
145233
camera_2d_editor->options->show();
146-
approach_to_move_rect->show();
147234
} else {
148235
camera_2d_editor->options->hide();
149-
approach_to_move_rect->hide();
150236
}
151237
}
152238

153239
Camera2DEditorPlugin::Camera2DEditorPlugin() {
154-
camera_2d_editor = memnew(Camera2DEditor);
240+
camera_2d_editor = memnew(Camera2DEditor(this));
155241
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: \nHold Ctrl + left mouse button to move the limit rectangle.\nHold 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);
166242
}

editor/plugins/camera_2d_editor_plugin.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,57 @@ class MenuButton;
3939
class Camera2DEditor : public Control {
4040
GDCLASS(Camera2DEditor, Control);
4141

42+
EditorPlugin *plugin = nullptr;
43+
4244
enum Menu {
4345
MENU_SNAP_LIMITS_TO_VIEWPORT,
4446
};
4547

48+
enum class Drag {
49+
NONE,
50+
LEFT,
51+
TOP,
52+
RIGHT,
53+
BOTTOM,
54+
CENTER,
55+
} drag_type;
56+
Rect2 drag_revert;
57+
Vector2 center_drag_point;
58+
4659
Camera2D *selected_camera = nullptr;
4760

4861
friend class Camera2DEditorPlugin;
4962
MenuButton *options = nullptr;
5063

5164
void _menu_option(int p_option);
52-
void _snap_limits_to_viewport();
53-
void _undo_snap_limits_to_viewport(const Rect2 &p_prev_rect);
65+
void _snap_limits_to_viewport(Camera2D *p_camera);
66+
void _update_overlays_if_needed(Camera2D *p_camera);
5467

5568
protected:
5669
static void _bind_methods();
5770
void _notification(int p_what);
5871

5972
public:
6073
void edit(Camera2D *p_camera);
61-
Camera2DEditor();
74+
75+
bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
76+
void forward_canvas_draw_over_viewport(Control *p_overlay);
77+
78+
Camera2DEditor(EditorPlugin *p_plugin);
6279
};
6380

6481
class Camera2DEditorPlugin : public EditorPlugin {
6582
GDCLASS(Camera2DEditorPlugin, EditorPlugin);
6683

6784
Camera2DEditor *camera_2d_editor = nullptr;
6885

69-
Label *approach_to_move_rect = nullptr;
70-
71-
void _editor_theme_changed();
72-
void _update_approach_text_visibility();
73-
7486
public:
7587
virtual void edit(Object *p_object) override;
7688
virtual bool handles(Object *p_object) const override;
7789
virtual void make_visible(bool p_visible) override;
7890

91+
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return camera_2d_editor->forward_canvas_gui_input(p_event); }
92+
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { camera_2d_editor->forward_canvas_draw_over_viewport(p_overlay); }
93+
7994
Camera2DEditorPlugin();
8095
};

0 commit comments

Comments
 (0)