Skip to content

Commit

Permalink
Add pivot editing to UV editor
Browse files Browse the repository at this point in the history
Add draggable pivot marker to UV editors Points mode.
Bonus: Fix UV editor using hardcoded value instead of editor setting polygon_editor/point_grab_radius.
  • Loading branch information
aXu-AP committed Sep 16, 2024
1 parent 2124995 commit c4ca6a2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
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()) {
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

0 comments on commit c4ca6a2

Please sign in to comment.