Allow for easier editing of rect CollisionShape2D
This commit is contained in:
parent
6d58ea6ce7
commit
10652cd342
2 changed files with 56 additions and 18 deletions
|
@ -96,7 +96,7 @@ Variant CollisionShape2DEditor::get_handle_value(int idx) const {
|
||||||
case RECTANGLE_SHAPE: {
|
case RECTANGLE_SHAPE: {
|
||||||
Ref<RectangleShape2D> rect = node->get_shape();
|
Ref<RectangleShape2D> rect = node->get_shape();
|
||||||
|
|
||||||
if (idx < 3) {
|
if (idx < 8) {
|
||||||
return rect->get_extents().abs();
|
return rect->get_extents().abs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,16 +175,26 @@ void CollisionShape2DEditor::set_handle(int idx, Point2 &p_point) {
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case RECTANGLE_SHAPE: {
|
case RECTANGLE_SHAPE: {
|
||||||
if (idx < 3) {
|
if (idx < 8) {
|
||||||
Ref<RectangleShape2D> rect = node->get_shape();
|
Ref<RectangleShape2D> rect = node->get_shape();
|
||||||
|
Vector2 extents = (Point2)original;
|
||||||
|
|
||||||
Vector2 extents = rect->get_extents();
|
if (RECT_HANDLES[idx].x != 0) {
|
||||||
if (idx == 2) {
|
extents.x = p_point.x * RECT_HANDLES[idx].x;
|
||||||
extents = p_point;
|
}
|
||||||
} else {
|
if (RECT_HANDLES[idx].y != 0) {
|
||||||
extents[idx] = p_point[idx];
|
extents.y = p_point.y * RECT_HANDLES[idx].y;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input::get_singleton()->is_key_pressed(KEY_ALT)) {
|
||||||
|
rect->set_extents(extents.abs());
|
||||||
|
node->set_global_position(original_transform.get_origin());
|
||||||
|
} else {
|
||||||
|
rect->set_extents(((Point2)original + (extents - (Point2)original) * 0.5).abs());
|
||||||
|
Point2 pos = original_transform.affine_inverse().xform(original_transform.get_origin());
|
||||||
|
pos += (extents - (Point2)original) * RECT_HANDLES[idx] * 0.5;
|
||||||
|
node->set_global_position(original_transform.xform(pos));
|
||||||
}
|
}
|
||||||
rect->set_extents(extents.abs());
|
|
||||||
|
|
||||||
canvas_item_editor->update_viewport();
|
canvas_item_editor->update_viewport();
|
||||||
}
|
}
|
||||||
|
@ -279,8 +289,10 @@ void CollisionShape2DEditor::commit_handle(int idx, Variant &p_org) {
|
||||||
Ref<RectangleShape2D> rect = node->get_shape();
|
Ref<RectangleShape2D> rect = node->get_shape();
|
||||||
|
|
||||||
undo_redo->add_do_method(rect.ptr(), "set_extents", rect->get_extents());
|
undo_redo->add_do_method(rect.ptr(), "set_extents", rect->get_extents());
|
||||||
|
undo_redo->add_do_method(node, "set_global_transform", node->get_global_transform());
|
||||||
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
|
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
|
||||||
undo_redo->add_undo_method(rect.ptr(), "set_extents", p_org);
|
undo_redo->add_undo_method(rect.ptr(), "set_extents", p_org);
|
||||||
|
undo_redo->add_undo_method(node, "set_global_transform", original_transform);
|
||||||
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
|
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
|
@ -341,6 +353,8 @@ bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_e
|
||||||
}
|
}
|
||||||
|
|
||||||
original = get_handle_value(edit_handle);
|
original = get_handle_value(edit_handle);
|
||||||
|
original_transform = node->get_global_transform();
|
||||||
|
last_point = original;
|
||||||
pressed = true;
|
pressed = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -368,13 +382,26 @@ bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_e
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
|
Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
|
||||||
cpoint = node->get_global_transform().affine_inverse().xform(cpoint);
|
cpoint = original_transform.affine_inverse().xform(cpoint);
|
||||||
|
last_point = cpoint;
|
||||||
|
|
||||||
set_handle(edit_handle, cpoint);
|
set_handle(edit_handle, cpoint);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ref<InputEventKey> k = p_event;
|
||||||
|
|
||||||
|
if (k.is_valid()) {
|
||||||
|
if (edit_handle == -1 || !pressed || k->is_echo()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shape_type == RECTANGLE_SHAPE && k->get_scancode() == KEY_ALT) {
|
||||||
|
set_handle(edit_handle, last_point); // Update handle when Alt key is toggled.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -491,16 +518,12 @@ void CollisionShape2DEditor::forward_canvas_draw_over_viewport(Control *p_overla
|
||||||
case RECTANGLE_SHAPE: {
|
case RECTANGLE_SHAPE: {
|
||||||
Ref<RectangleShape2D> shape = node->get_shape();
|
Ref<RectangleShape2D> shape = node->get_shape();
|
||||||
|
|
||||||
handles.resize(3);
|
handles.resize(8);
|
||||||
Vector2 ext = shape->get_extents();
|
Vector2 ext = shape->get_extents();
|
||||||
handles.write[0] = Point2(ext.x, 0);
|
for (int i = 0; i < handles.size(); i++) {
|
||||||
handles.write[1] = Point2(0, -ext.y);
|
handles.write[i] = RECT_HANDLES[i] * ext;
|
||||||
handles.write[2] = Point2(ext.x, -ext.y);
|
p_overlay->draw_texture(h, gt.xform(handles[i]) - size);
|
||||||
|
}
|
||||||
p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
|
|
||||||
p_overlay->draw_texture(h, gt.xform(handles[1]) - size);
|
|
||||||
p_overlay->draw_texture(h, gt.xform(handles[2]) - size);
|
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case SEGMENT_SHAPE: {
|
case SEGMENT_SHAPE: {
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
#ifndef COLLISION_SHAPE_2D_EDITOR_PLUGIN_H
|
#ifndef COLLISION_SHAPE_2D_EDITOR_PLUGIN_H
|
||||||
#define COLLISION_SHAPE_2D_EDITOR_PLUGIN_H
|
#define COLLISION_SHAPE_2D_EDITOR_PLUGIN_H
|
||||||
|
|
||||||
|
#include "core/os/input.h"
|
||||||
|
#include "core/os/keyboard.h"
|
||||||
#include "editor/editor_node.h"
|
#include "editor/editor_node.h"
|
||||||
#include "editor/editor_plugin.h"
|
#include "editor/editor_plugin.h"
|
||||||
|
|
||||||
|
@ -52,6 +54,17 @@ class CollisionShape2DEditor : public Control {
|
||||||
SEGMENT_SHAPE
|
SEGMENT_SHAPE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const Point2 RECT_HANDLES[8] = {
|
||||||
|
Point2(1, 0),
|
||||||
|
Point2(1, 1),
|
||||||
|
Point2(0, 1),
|
||||||
|
Point2(-1, 1),
|
||||||
|
Point2(-1, 0),
|
||||||
|
Point2(-1, -1),
|
||||||
|
Point2(0, -1),
|
||||||
|
Point2(1, -1),
|
||||||
|
};
|
||||||
|
|
||||||
EditorNode *editor;
|
EditorNode *editor;
|
||||||
UndoRedo *undo_redo;
|
UndoRedo *undo_redo;
|
||||||
CanvasItemEditor *canvas_item_editor;
|
CanvasItemEditor *canvas_item_editor;
|
||||||
|
@ -63,6 +76,8 @@ class CollisionShape2DEditor : public Control {
|
||||||
int edit_handle;
|
int edit_handle;
|
||||||
bool pressed;
|
bool pressed;
|
||||||
Variant original;
|
Variant original;
|
||||||
|
Transform2D original_transform;
|
||||||
|
Point2 last_point;
|
||||||
|
|
||||||
Variant get_handle_value(int idx) const;
|
Variant get_handle_value(int idx) const;
|
||||||
void set_handle(int idx, Point2 &p_point);
|
void set_handle(int idx, Point2 &p_point);
|
||||||
|
|
Loading…
Reference in a new issue