Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pivot editing to UV editor #83612

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions editor/plugins/polygon_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ void Polygon2DEditor::_cancel_editing() {
} else if (uv_edit_mode[1]->is_pressed()) { // Edit polygon.
node->set_polygon(points_prev);
}
if (uv_move_current == UV_MODE_MOVE_PIVOT) {
uv_move_current = UV_MODE_EDIT_POINT;
node->set_offset(offset_prev);
}
}

polygon_create.clear();
Expand Down Expand Up @@ -623,6 +627,8 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
undo_redo->commit_action();
}

const real_t grab_radius = EDITOR_GET("editors/polygon_editor/point_grab_radius");

if (uv_move_current == UV_MODE_REMOVE_INTERNAL) {
uv_create_uv_prev = node->get_uv();
uv_create_poly_prev = node->get_polygon();
Expand All @@ -640,7 +646,7 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
for (int i = points_prev.size() - internal_vertices; i < points_prev.size(); i++) {
Vector2 tuv = mtx.xform(uv_create_poly_prev[i]);
real_t dist = tuv.distance_to(mb->get_position());
if (dist < 8 && dist < closest_dist) {
if (dist < grab_radius && dist < closest_dist) {
closest = i;
closest_dist = dist;
}
Expand Down Expand Up @@ -692,14 +698,21 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
point_drag_index = -1;
for (int i = 0; i < points_prev.size(); i++) {
Vector2 tuv = mtx.xform(points_prev[i]);
if (tuv.distance_to(mb->get_position()) < 8) {
if (tuv.distance_to(mb->get_position()) < grab_radius) {
uv_drag_from = tuv;
point_drag_index = i;
}
}

if (point_drag_index == -1) {
uv_drag = false;
Vector2 offset_transformed = mtx.xform(-node->get_offset());
if (uv_edit_mode[1]->is_pressed() && offset_transformed.distance_to(mb->get_position()) < grab_radius) {
uv_drag_from = offset_transformed;
offset_prev = node->get_offset();
uv_move_current = UV_MODE_MOVE_PIVOT;
} else {
uv_drag = false;
}
}
}

Expand All @@ -710,7 +723,7 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
for (int i = 0; i < points_prev.size(); i++) {
Vector2 tuv = mtx.xform(points_prev[i]);
real_t dist = tuv.distance_to(mb->get_position());
if (dist < 8 && dist < closest_dist) {
if (dist < grab_radius && dist < closest_dist) {
closest = i;
closest_dist = dist;
}
Expand Down Expand Up @@ -816,6 +829,15 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
undo_redo->commit_action();
} break;
case UV_MODE_MOVE_PIVOT: {
undo_redo->create_action(TTR("Set Offset"));
undo_redo->add_do_method(node, "set_offset", node->get_offset());
undo_redo->add_undo_method(node, "set_offset", offset_prev);
undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
undo_redo->commit_action();
uv_move_current = UV_MODE_EDIT_POINT;
} break;
default: {
} break;
}
Expand Down Expand Up @@ -869,6 +891,9 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
node->set_polygon(uv_new);
}
} break;
case UV_MODE_MOVE_PIVOT: {
node->set_offset(offset_prev - drag);
} break;
case UV_MODE_MOVE: {
Vector<Vector2> uv_new = points_prev;
for (int i = 0; i < uv_new.size(); i++) {
Expand Down Expand Up @@ -1239,6 +1264,11 @@ void Polygon2DEditor::_uv_draw() {
}
}

if (uv_edit_mode[1]->is_pressed()) {
Copy link
Member

@KoBeWi KoBeWi Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but the modes should get an enum.

EDIT:
I see there are some enums already, but for something different. uv_edit_mode values are still hard-coded, which is bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this code is a bit all over the place and there's some confusing terminology for modes, actions and menu items. I've outlined some refactoring work which I want to do in godotengine/godot-proposals#8209 (input on those points would be appreciated).

Ref<Texture2D> pivot_icon = get_editor_theme_icon(SNAME("EditorPivot"));
uv_edit_draw->draw_texture(pivot_icon, mtx.xform(-node->get_offset()) - pivot_icon->get_size() * 0.5);
}

if (uv_mode == UV_MODE_PAINT_WEIGHT || uv_mode == UV_MODE_CLEAR_WEIGHT) {
NodePath bone_path;
for (int i = 0; i < bone_scroll_vb->get_child_count(); i++) {
Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/polygon_2d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Polygon2DEditor : public AbstractPolygon2DEditor {
UV_MODE_REMOVE_POLYGON,
UV_MODE_PAINT_WEIGHT,
UV_MODE_CLEAR_WEIGHT,
UV_MODE_MOVE_PIVOT,
UV_MODE_MAX
};

Expand Down Expand Up @@ -120,6 +121,7 @@ class Polygon2DEditor : public AbstractPolygon2DEditor {
int uv_create_prev_internal_vertices = 0;
Array uv_create_bones_prev;
Array polygons_prev;
Vector2 offset_prev;

Vector2 uv_create_to;
int point_drag_index;
Expand Down
Loading