Skip to content

Commit ec5ca6e

Browse files
committed
Highlight points on hover in the Polygon2D editor
1 parent d7bdc0b commit ec5ca6e

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

editor/plugins/polygon_2d_editor_plugin.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,9 @@ void Polygon2DEditor::_canvas_input(const Ref<InputEvent> &p_input) {
589589
if (closest == -1) {
590590
return;
591591
}
592+
if (closest == highlighted_point) {
593+
highlighted_point = -1;
594+
}
592595

593596
previous_polygon.remove_at(closest);
594597
previous_uv.remove_at(closest);
@@ -776,6 +779,41 @@ void Polygon2DEditor::_canvas_input(const Ref<InputEvent> &p_input) {
776779
Ref<InputEventMouseMotion> mm = p_input;
777780

778781
if (mm.is_valid()) {
782+
// Highlight a point near the cursor
783+
if (current_mode == MODE_POINTS || current_mode == MODE_POLYGONS || current_mode == MODE_UV) {
784+
if (is_creating) {
785+
if (editing_points.size() > 2 && mtx.affine_inverse().xform(mm->get_position()).distance_to(node->get_polygon()[0]) < (8 / draw_zoom)) {
786+
if (highlighted_point != 0) {
787+
highlighted_point = 0;
788+
canvas->queue_redraw();
789+
}
790+
} else if (highlighted_point == 0) {
791+
highlighted_point = -1;
792+
canvas->queue_redraw();
793+
}
794+
} else {
795+
Vector<Vector2> points;
796+
if (current_mode == MODE_POINTS || current_mode == MODE_POLYGONS) {
797+
points = node->get_polygon();
798+
} else {
799+
points = node->get_uv();
800+
}
801+
int i = points.size() - 1;
802+
for (; i >= 0; i--) {
803+
if (mtx.affine_inverse().xform(mm->get_position()).distance_to(points[i]) < (8 / draw_zoom)) {
804+
if (highlighted_point != i) {
805+
highlighted_point = i;
806+
canvas->queue_redraw();
807+
}
808+
break;
809+
}
810+
}
811+
if (i == -1 && highlighted_point >= 0) {
812+
highlighted_point = -1;
813+
canvas->queue_redraw();
814+
}
815+
}
816+
}
779817
if (is_dragging) {
780818
Vector2 uv_drag_to = mm->get_position();
781819
uv_drag_to = snap_point(uv_drag_to);
@@ -1179,12 +1217,17 @@ void Polygon2DEditor::_canvas_draw() {
11791217
float weight = weight_r[i];
11801218
canvas->draw_rect(Rect2(draw_pos - Vector2(2, 2) * EDSCALE, Vector2(5, 5) * EDSCALE), Color(weight, weight, weight, 1.0), Math::round(EDSCALE));
11811219
} else {
1182-
if (i < uv_draw_max) {
1183-
canvas->draw_texture(handle, mtx.xform(uvs[i]) - handle->get_size() * 0.5);
1184-
} else {
1220+
Color mod(1, 1, 1);
1221+
Color highlight = Color(0.4, 0.4, 0.4, 0);
1222+
if (i >= uv_draw_max) {
11851223
// Internal vertex
1186-
canvas->draw_texture(handle, mtx.xform(uvs[i]) - handle->get_size() * 0.5, Color(0.6, 0.8, 1));
1224+
mod = Color(0.6, 0.8, 1);
1225+
highlight = Color(0.25, 0.25, 0.25, 0);
1226+
}
1227+
if (i == highlighted_point) {
1228+
mod -= Color(0.25, 0.25, 0.25, 0);
11871229
}
1230+
canvas->draw_texture(handle, mtx.xform(uvs[i]) - handle->get_size() * 0.5, mod);
11881231
}
11891232
}
11901233

editor/plugins/polygon_2d_editor_plugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class Polygon2DEditor : public AbstractPolygon2DEditor {
135135
int point_drag_index = -1;
136136
bool is_dragging = false;
137137
bool is_creating = false;
138+
int highlighted_point = -1;
138139
Vector<int> polygon_create;
139140
Action current_action = ACTION_CREATE;
140141
Vector2 drag_from;

0 commit comments

Comments
 (0)