Path: blob/master/editor/scene/2d/path_2d_editor_plugin.cpp
20911 views
/**************************************************************************/1/* path_2d_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "path_2d_editor_plugin.h"3132#include "core/os/keyboard.h"33#include "editor/editor_node.h"34#include "editor/editor_undo_redo_manager.h"35#include "editor/scene/canvas_item_editor_plugin.h"36#include "editor/settings/editor_settings.h"37#include "editor/themes/editor_scale.h"38#include "scene/gui/dialogs.h"39#include "scene/gui/menu_button.h"40#include "scene/resources/mesh.h"4142void Path2DEditor::_notification(int p_what) {43switch (p_what) {44case NOTIFICATION_THEME_CHANGED: {45curve_edit->set_button_icon(get_editor_theme_icon(SNAME("CurveEdit")));46curve_edit_curve->set_button_icon(get_editor_theme_icon(SNAME("CurveCurve")));47curve_create->set_button_icon(get_editor_theme_icon(SNAME("CurveCreate")));48curve_del->set_button_icon(get_editor_theme_icon(SNAME("CurveDelete")));49curve_close->set_button_icon(get_editor_theme_icon(SNAME("CurveClose")));50curve_clear_points->set_button_icon(get_editor_theme_icon(SNAME("Clear")));5152create_curve_button->set_button_icon(get_editor_theme_icon(SNAME("Curve2D")));53} break;54}55}5657void Path2DEditor::_node_removed(Node *p_node) {58if (p_node == node) {59node = nullptr;60hide();61}62}6364bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {65if (!node) {66return false;67}6869if (!node->is_visible_in_tree()) {70return false;71}7273Viewport *vp = node->get_viewport();74if (vp && !vp->is_visible_subviewport()) {75return false;76}7778if (node->get_curve().is_null()) {79return false;80}8182real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");8384Ref<InputEventMouseButton> mb = p_event;85if (mb.is_valid()) {86Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();8788Vector2 gpoint = mb->get_position();89Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));90cpoint = node->to_local(node->get_viewport()->get_popup_base_transform().affine_inverse().xform(cpoint));9192if (mb->is_pressed() && action == ACTION_NONE) {93Ref<Curve2D> curve = node->get_curve();9495original_mouse_pos = gpoint;9697for (int i = 0; i < curve->get_point_count(); i++) {98real_t dist_to_p = gpoint.distance_to(xform.xform(curve->get_point_position(i)));99real_t dist_to_p_out = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_out(i)));100real_t dist_to_p_in = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_in(i)));101102// Check for point movement start (for point + in/out controls).103if (mb->get_button_index() == MouseButton::LEFT) {104if (mode == MODE_EDIT && !mb->is_shift_pressed() && dist_to_p < grab_threshold) {105// Points can only be moved in edit mode.106107action = ACTION_MOVING_POINT;108action_point = i;109moving_from = curve->get_point_position(i);110moving_screen_from = gpoint;111return true;112} else if (mode == MODE_EDIT || mode == MODE_EDIT_CURVE) {113control_points_in_range = 0;114// In/out controls can be moved in multiple modes.115if (dist_to_p_out < grab_threshold && i < (curve->get_point_count() - 1)) {116action = ACTION_MOVING_OUT;117action_point = i;118moving_from = curve->get_point_out(i);119moving_screen_from = gpoint;120orig_in_length = curve->get_point_in(action_point).length();121control_points_in_range += 1;122}123if (dist_to_p_in < grab_threshold && i > 0) {124action = ACTION_MOVING_IN;125action_point = i;126moving_from = curve->get_point_in(i);127moving_screen_from = gpoint;128orig_out_length = curve->get_point_out(action_point).length();129control_points_in_range += 1;130}131if (control_points_in_range > 0) {132return true;133}134}135}136137// Check for point deletion.138EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();139if ((mb->get_button_index() == MouseButton::RIGHT && (mode == MODE_EDIT || mode == MODE_CREATE)) || (mb->get_button_index() == MouseButton::LEFT && mode == MODE_DELETE)) {140if (dist_to_p < grab_threshold) {141undo_redo->create_action(TTR("Remove Point from Curve"));142undo_redo->add_do_method(curve.ptr(), "remove_point", i);143undo_redo->add_undo_method(curve.ptr(), "add_point", curve->get_point_position(i), curve->get_point_in(i), curve->get_point_out(i), i);144undo_redo->add_do_method(canvas_item_editor, "update_viewport");145undo_redo->add_undo_method(canvas_item_editor, "update_viewport");146undo_redo->commit_action();147return true;148} else if (dist_to_p_out < grab_threshold) {149undo_redo->create_action(TTR("Remove Out-Control from Curve"));150undo_redo->add_do_method(curve.ptr(), "set_point_out", i, Vector2());151undo_redo->add_undo_method(curve.ptr(), "set_point_out", i, curve->get_point_out(i));152undo_redo->add_do_method(canvas_item_editor, "update_viewport");153undo_redo->add_undo_method(canvas_item_editor, "update_viewport");154undo_redo->commit_action();155return true;156} else if (dist_to_p_in < grab_threshold) {157undo_redo->create_action(TTR("Remove In-Control from Curve"));158undo_redo->add_do_method(curve.ptr(), "set_point_in", i, Vector2());159undo_redo->add_undo_method(curve.ptr(), "set_point_in", i, curve->get_point_in(i));160undo_redo->add_do_method(canvas_item_editor, "update_viewport");161undo_redo->add_undo_method(canvas_item_editor, "update_viewport");162undo_redo->commit_action();163return true;164}165}166}167}168169if (action != ACTION_NONE && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {170_cancel_current_action();171return true;172}173174// Check for point creation.175if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && ((mb->is_command_or_control_pressed() && mode == MODE_EDIT) || mode == MODE_CREATE)) {176Ref<Curve2D> curve = node->get_curve();177curve->add_point(cpoint);178moving_from = cpoint;179180action = ACTION_MOVING_NEW_POINT;181action_point = curve->get_point_count() - 1;182moving_from = curve->get_point_position(action_point);183moving_screen_from = gpoint;184185canvas_item_editor->update_viewport();186187return true;188}189190// Check for segment split.191if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && mode == MODE_EDIT && on_edge) {192Vector2 gpoint2 = mb->get_position();193Ref<Curve2D> curve = node->get_curve();194195int insertion_point = -1;196float mbLength = curve->get_closest_offset(xform.affine_inverse().xform(gpoint2));197int len = curve->get_point_count();198for (int i = 0; i < len - 1; i++) {199float compareLength = curve->get_closest_offset(curve->get_point_position(i + 1));200if (mbLength >= curve->get_closest_offset(curve->get_point_position(i)) && mbLength <= compareLength) {201insertion_point = i;202}203}204if (insertion_point == -1) {205insertion_point = curve->get_point_count() - 2;206}207208const Vector2 new_point = xform.affine_inverse().xform(gpoint2);209curve->add_point(new_point, Vector2(0, 0), Vector2(0, 0), insertion_point + 1);210211action = ACTION_MOVING_NEW_POINT_FROM_SPLIT;212action_point = insertion_point + 1;213moving_from = curve->get_point_position(action_point);214moving_screen_from = gpoint2;215216canvas_item_editor->update_viewport();217218on_edge = false;219220return true;221}222223// Check for point movement completion.224if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && action != ACTION_NONE) {225Ref<Curve2D> curve = node->get_curve();226227EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();228Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);229switch (action) {230case ACTION_NONE:231// N/A, handled in above condition.232break;233234case ACTION_MOVING_POINT:235if (original_mouse_pos != gpoint) {236undo_redo->create_action(TTR("Move Point in Curve"));237undo_redo->add_undo_method(curve.ptr(), "set_point_position", action_point, moving_from);238undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);239undo_redo->add_do_method(canvas_item_editor, "update_viewport");240undo_redo->add_undo_method(canvas_item_editor, "update_viewport");241undo_redo->commit_action(false);242}243break;244case ACTION_MOVING_NEW_POINT: {245undo_redo->create_action(TTR("Add Point to Curve"));246undo_redo->add_do_method(curve.ptr(), "add_point", cpoint);247undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);248undo_redo->add_do_method(canvas_item_editor, "update_viewport");249undo_redo->add_undo_method(curve.ptr(), "remove_point", curve->get_point_count() - 1);250undo_redo->add_undo_method(canvas_item_editor, "update_viewport");251undo_redo->commit_action(false);252} break;253254case ACTION_MOVING_NEW_POINT_FROM_SPLIT: {255undo_redo->create_action(TTR("Split Curve"));256undo_redo->add_do_method(curve.ptr(), "add_point", Vector2(), Vector2(), Vector2(), action_point);257undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);258undo_redo->add_undo_method(curve.ptr(), "remove_point", action_point);259undo_redo->add_do_method(canvas_item_editor, "update_viewport");260undo_redo->add_undo_method(canvas_item_editor, "update_viewport");261undo_redo->commit_action(false);262} break;263264case ACTION_MOVING_IN: {265if (original_mouse_pos != gpoint) {266undo_redo->create_action(TTR("Move In-Control in Curve"));267undo_redo->add_do_method(curve.ptr(), "set_point_in", action_point, new_pos);268undo_redo->add_undo_method(curve.ptr(), "set_point_in", action_point, moving_from);269270if (mirror_handle_angle) {271undo_redo->add_do_method(curve.ptr(), "set_point_out", action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length));272undo_redo->add_undo_method(curve.ptr(), "set_point_out", action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_out_length));273}274undo_redo->add_do_method(canvas_item_editor, "update_viewport");275undo_redo->add_undo_method(canvas_item_editor, "update_viewport");276undo_redo->commit_action();277}278} break;279280case ACTION_MOVING_OUT: {281if (original_mouse_pos != gpoint) {282undo_redo->create_action(TTR("Move Out-Control in Curve"));283undo_redo->add_do_method(curve.ptr(), "set_point_out", action_point, new_pos);284undo_redo->add_undo_method(curve.ptr(), "set_point_out", action_point, moving_from);285286if (mirror_handle_angle) {287undo_redo->add_do_method(curve.ptr(), "set_point_in", action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length));288undo_redo->add_undo_method(curve.ptr(), "set_point_in", action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_in_length));289}290undo_redo->add_do_method(canvas_item_editor, "update_viewport");291undo_redo->add_undo_method(canvas_item_editor, "update_viewport");292undo_redo->commit_action();293}294} break;295}296297action = ACTION_NONE;298299return true;300}301}302303Ref<InputEventMouseMotion> mm = p_event;304305if (mm.is_valid()) {306// When both control points were in range of click,307// pick the point that drags the curve outwards.308if (control_points_in_range == 2) {309control_points_in_range = 0;310Ref<Curve2D> curve = node->get_curve();311Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();312Point2 relative = xform.affine_inverse().basis_xform(mm->get_relative());313real_t angle_in = relative.angle_to(curve->get_point_position(action_point - 1) - curve->get_point_position(action_point));314real_t angle_out = relative.angle_to(curve->get_point_position(action_point + 1) - curve->get_point_position(action_point));315316if (Math::abs(angle_in) < Math::abs(angle_out)) {317action = ACTION_MOVING_IN;318moving_from = curve->get_point_in(action_point);319orig_out_length = curve->get_point_out(action_point).length();320} else {321action = ACTION_MOVING_OUT;322moving_from = curve->get_point_out(action_point);323orig_in_length = curve->get_point_in(action_point).length();324}325}326327if (action == ACTION_NONE && mode == MODE_EDIT) {328// Handle Edge Follow329bool old_edge = on_edge;330331Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();332Vector2 gpoint = mm->get_position();333334Ref<Curve2D> curve = node->get_curve();335if (curve.is_null()) {336return true;337}338if (curve->get_point_count() < 2) {339return true;340}341342// Find edge343edge_point = xform.xform(curve->get_closest_point(xform.affine_inverse().xform(mm->get_position())));344on_edge = false;345if (edge_point.distance_to(gpoint) <= grab_threshold) {346on_edge = true;347}348// However, if near a control point or its in-out handles then not on edge349int len = curve->get_point_count();350for (int i = 0; i < len; i++) {351Vector2 pp = curve->get_point_position(i);352Vector2 p = xform.xform(pp);353if (p.distance_to(gpoint) <= grab_threshold) {354on_edge = false;355break;356}357p = xform.xform(pp + curve->get_point_in(i));358if (p.distance_to(gpoint) <= grab_threshold) {359on_edge = false;360break;361}362p = xform.xform(pp + curve->get_point_out(i));363if (p.distance_to(gpoint) <= grab_threshold) {364on_edge = false;365break;366}367}368if (on_edge || old_edge != on_edge) {369canvas_item_editor->update_viewport();370return true;371}372}373374if (action != ACTION_NONE) {375// Handle point/control movement.376Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();377Vector2 gpoint = mm->get_position();378Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));379cpoint = node->to_local(node->get_viewport()->get_popup_base_transform().affine_inverse().xform(cpoint));380381Ref<Curve2D> curve = node->get_curve();382383Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);384385switch (action) {386case ACTION_NONE:387// N/A, handled in above condition.388break;389390case ACTION_MOVING_POINT:391case ACTION_MOVING_NEW_POINT:392case ACTION_MOVING_NEW_POINT_FROM_SPLIT: {393curve->set_point_position(action_point, cpoint);394} break;395396case ACTION_MOVING_IN: {397curve->set_point_in(action_point, new_pos);398399if (mirror_handle_angle) {400curve->set_point_out(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length));401}402} break;403404case ACTION_MOVING_OUT: {405curve->set_point_out(action_point, new_pos);406407if (mirror_handle_angle) {408curve->set_point_in(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length));409}410} break;411}412413canvas_item_editor->update_viewport();414return true;415}416}417418return false;419}420421void Path2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {422if (!node || !node->is_visible_in_tree() || node->get_curve().is_null()) {423return;424}425426Viewport *vp = node->get_viewport();427if (vp && !vp->is_visible_subviewport()) {428return;429}430431Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();432433const Ref<Texture2D> path_sharp_handle = get_editor_theme_icon(SNAME("EditorPathSharpHandle"));434const Ref<Texture2D> path_smooth_handle = get_editor_theme_icon(SNAME("EditorPathSmoothHandle"));435// Both handle icons must be of the same size436const Size2 handle_size = path_sharp_handle->get_size();437438const Ref<Texture2D> curve_handle = get_editor_theme_icon(SNAME("EditorCurveHandle"));439const Size2 curve_handle_size = curve_handle->get_size();440441Ref<Curve2D> curve = node->get_curve();442443int len = curve->get_point_count();444Control *vpc = canvas_item_editor->get_viewport_control();445446debug_handle_lines.clear();447debug_handle_curve_transforms.clear();448debug_handle_sharp_transforms.clear();449debug_handle_smooth_transforms.clear();450451Transform2D handle_curve_transform = Transform2D().scaled(curve_handle_size * 0.5);452Transform2D handle_point_transform = Transform2D().scaled(handle_size * 0.5);453454for (int i = 0; i < len; i++) {455Vector2 point = xform.xform(curve->get_point_position(i));456// Determines the point icon to be used457bool smooth = false;458459if (i < len - 1) {460Vector2 point_out = xform.xform(curve->get_point_position(i) + curve->get_point_out(i));461if (point != point_out) {462smooth = true;463debug_handle_lines.push_back(point);464debug_handle_lines.push_back(point_out);465handle_curve_transform.set_origin(point_out);466debug_handle_curve_transforms.push_back(handle_curve_transform);467}468}469470if (i > 0) {471Vector2 point_in = xform.xform(curve->get_point_position(i) + curve->get_point_in(i));472if (point != point_in) {473smooth = true;474debug_handle_lines.push_back(point);475debug_handle_lines.push_back(point_in);476handle_curve_transform.set_origin(point_in);477debug_handle_curve_transforms.push_back(handle_curve_transform);478}479}480481handle_point_transform.set_origin(point);482if (smooth) {483debug_handle_smooth_transforms.push_back(handle_point_transform);484} else {485debug_handle_sharp_transforms.push_back(handle_point_transform);486}487}488489if (on_edge) {490Ref<Texture2D> add_handle = get_editor_theme_icon(SNAME("EditorHandleAdd"));491p_overlay->draw_texture(add_handle, edge_point - add_handle->get_size() * 0.5);492}493494RenderingServer *rs = RS::get_singleton();495rs->mesh_clear(debug_mesh_rid);496497if (!debug_handle_lines.is_empty()) {498Array handles_array;499handles_array.resize(Mesh::ARRAY_MAX);500501handles_array[Mesh::ARRAY_VERTEX] = Vector<Vector2>(debug_handle_lines);502503rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, handles_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);504rs->canvas_item_add_mesh(vpc->get_canvas_item(), debug_mesh_rid, Transform2D(), Color(0.5, 0.5, 0.5, 1.0));505}506507// Add texture rects multimeshes for handle vertices.508509uint32_t handle_curve_count = debug_handle_curve_transforms.size();510uint32_t handle_sharp_count = debug_handle_sharp_transforms.size();511uint32_t handle_smooth_count = debug_handle_smooth_transforms.size();512513// Add texture rects for curve handle vertices.514515rs->multimesh_set_visible_instances(debug_handle_curve_multimesh_rid, 0);516if (handle_curve_count > 0) {517if (rs->multimesh_get_instance_count(debug_handle_curve_multimesh_rid) != int(handle_curve_count)) {518rs->multimesh_allocate_data(debug_handle_curve_multimesh_rid, handle_curve_count, RS::MULTIMESH_TRANSFORM_2D);519}520521Vector<float> multimesh_buffer;522multimesh_buffer.resize(8 * handle_curve_count);523float *multimesh_buffer_ptrw = multimesh_buffer.ptrw();524525const Transform2D *debug_handle_transforms_ptr = debug_handle_curve_transforms.ptr();526527for (uint32_t i = 0; i < handle_curve_count; i++) {528const Transform2D &handle_transform = debug_handle_transforms_ptr[i];529530multimesh_buffer_ptrw[i * 8 + 0] = handle_transform[0][0];531multimesh_buffer_ptrw[i * 8 + 1] = handle_transform[1][0];532multimesh_buffer_ptrw[i * 8 + 2] = 0;533multimesh_buffer_ptrw[i * 8 + 3] = handle_transform[2][0];534multimesh_buffer_ptrw[i * 8 + 4] = handle_transform[0][1];535multimesh_buffer_ptrw[i * 8 + 5] = handle_transform[1][1];536multimesh_buffer_ptrw[i * 8 + 6] = 0;537multimesh_buffer_ptrw[i * 8 + 7] = handle_transform[2][1];538}539540rs->multimesh_set_buffer(debug_handle_curve_multimesh_rid, multimesh_buffer);541rs->multimesh_set_visible_instances(debug_handle_curve_multimesh_rid, handle_curve_count);542543rs->canvas_item_add_multimesh(vpc->get_canvas_item(), debug_handle_curve_multimesh_rid, curve_handle->get_rid());544}545546// Add texture rects for sharp handle vertices.547548rs->multimesh_set_visible_instances(debug_handle_sharp_multimesh_rid, 0);549if (handle_sharp_count > 0) {550if (rs->multimesh_get_instance_count(debug_handle_sharp_multimesh_rid) != int(handle_sharp_count)) {551rs->multimesh_allocate_data(debug_handle_sharp_multimesh_rid, handle_sharp_count, RS::MULTIMESH_TRANSFORM_2D);552}553554Vector<float> multimesh_buffer;555multimesh_buffer.resize(8 * handle_sharp_count);556float *multimesh_buffer_ptrw = multimesh_buffer.ptrw();557558const Transform2D *debug_handle_transforms_ptr = debug_handle_sharp_transforms.ptr();559560for (uint32_t i = 0; i < handle_sharp_count; i++) {561const Transform2D &handle_transform = debug_handle_transforms_ptr[i];562563multimesh_buffer_ptrw[i * 8 + 0] = handle_transform[0][0];564multimesh_buffer_ptrw[i * 8 + 1] = handle_transform[1][0];565multimesh_buffer_ptrw[i * 8 + 2] = 0;566multimesh_buffer_ptrw[i * 8 + 3] = handle_transform[2][0];567multimesh_buffer_ptrw[i * 8 + 4] = handle_transform[0][1];568multimesh_buffer_ptrw[i * 8 + 5] = handle_transform[1][1];569multimesh_buffer_ptrw[i * 8 + 6] = 0;570multimesh_buffer_ptrw[i * 8 + 7] = handle_transform[2][1];571}572573rs->multimesh_set_buffer(debug_handle_sharp_multimesh_rid, multimesh_buffer);574rs->multimesh_set_visible_instances(debug_handle_sharp_multimesh_rid, handle_sharp_count);575576rs->canvas_item_add_multimesh(vpc->get_canvas_item(), debug_handle_sharp_multimesh_rid, curve_handle->get_rid());577}578579// Add texture rects for smooth handle vertices.580581rs->multimesh_set_visible_instances(debug_handle_smooth_multimesh_rid, 0);582if (handle_smooth_count > 0) {583if (rs->multimesh_get_instance_count(debug_handle_smooth_multimesh_rid) != int(handle_smooth_count)) {584rs->multimesh_allocate_data(debug_handle_smooth_multimesh_rid, handle_smooth_count, RS::MULTIMESH_TRANSFORM_2D);585}586587Vector<float> multimesh_buffer;588multimesh_buffer.resize(8 * handle_smooth_count);589float *multimesh_buffer_ptrw = multimesh_buffer.ptrw();590591const Transform2D *debug_handle_transforms_ptr = debug_handle_smooth_transforms.ptr();592593for (uint32_t i = 0; i < handle_smooth_count; i++) {594const Transform2D &handle_transform = debug_handle_transforms_ptr[i];595596multimesh_buffer_ptrw[i * 8 + 0] = handle_transform[0][0];597multimesh_buffer_ptrw[i * 8 + 1] = handle_transform[1][0];598multimesh_buffer_ptrw[i * 8 + 2] = 0;599multimesh_buffer_ptrw[i * 8 + 3] = handle_transform[2][0];600multimesh_buffer_ptrw[i * 8 + 4] = handle_transform[0][1];601multimesh_buffer_ptrw[i * 8 + 5] = handle_transform[1][1];602multimesh_buffer_ptrw[i * 8 + 6] = 0;603multimesh_buffer_ptrw[i * 8 + 7] = handle_transform[2][1];604}605606rs->multimesh_set_buffer(debug_handle_smooth_multimesh_rid, multimesh_buffer);607rs->multimesh_set_visible_instances(debug_handle_smooth_multimesh_rid, handle_smooth_count);608609rs->canvas_item_add_multimesh(vpc->get_canvas_item(), debug_handle_smooth_multimesh_rid, curve_handle->get_rid());610}611}612613void Path2DEditor::_node_visibility_changed() {614if (!node) {615return;616}617618canvas_item_editor->update_viewport();619_update_toolbar();620}621622void Path2DEditor::_update_toolbar() {623if (!node) {624return;625}626bool has_curve = node->get_curve().is_valid();627toolbar->set_visible(has_curve);628create_curve_button->set_visible(!has_curve);629}630631void Path2DEditor::edit(Node *p_path2d) {632if (!canvas_item_editor) {633canvas_item_editor = CanvasItemEditor::get_singleton();634}635636if (action != ACTION_NONE) {637_cancel_current_action();638}639640if (p_path2d) {641node = Object::cast_to<Path2D>(p_path2d);642_update_toolbar();643644if (!node->is_connected(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed))) {645node->connect(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed));646}647} else {648// The node may have been deleted at this point.649if (node && node->is_connected(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed))) {650node->disconnect(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed));651}652node = nullptr;653}654655canvas_item_editor->update_viewport();656}657658void Path2DEditor::_bind_methods() {659ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path2DEditor::_update_toolbar);660ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path2DEditor::_clear_curve_points);661ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path2DEditor::_restore_curve_points);662}663664void Path2DEditor::_mode_selected(int p_mode) {665if (p_mode == MODE_CREATE) {666curve_create->set_pressed(true);667curve_edit->set_pressed(false);668curve_edit_curve->set_pressed(false);669curve_del->set_pressed(false);670} else if (p_mode == MODE_EDIT) {671curve_create->set_pressed(false);672curve_edit->set_pressed(true);673curve_edit_curve->set_pressed(false);674curve_del->set_pressed(false);675} else if (p_mode == MODE_EDIT_CURVE) {676curve_create->set_pressed(false);677curve_edit->set_pressed(false);678curve_edit_curve->set_pressed(true);679curve_del->set_pressed(false);680} else if (p_mode == MODE_DELETE) {681curve_create->set_pressed(false);682curve_edit->set_pressed(false);683curve_edit_curve->set_pressed(false);684curve_del->set_pressed(true);685} else if (p_mode == MODE_CLOSE) {686if (node->get_curve().is_null()) {687return;688}689if (node->get_curve()->get_point_count() < 3) {690return;691}692Vector2 begin = node->get_curve()->get_point_position(0);693Vector2 end = node->get_curve()->get_point_position(node->get_curve()->get_point_count() - 1);694695if (begin.is_equal_approx(end)) {696return;697}698EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();699700undo_redo->create_action(TTR("Close the Curve"));701undo_redo->add_do_method(node->get_curve().ptr(), "add_point", begin);702undo_redo->add_undo_method(node->get_curve().ptr(), "remove_point", node->get_curve()->get_point_count());703undo_redo->add_do_method(canvas_item_editor, "update_viewport");704undo_redo->add_undo_method(canvas_item_editor, "update_viewport");705undo_redo->commit_action();706return;707} else if (p_mode == MODE_CLEAR_POINTS) {708if (node->get_curve().is_null()) {709return;710}711if (node->get_curve()->get_point_count() == 0) {712return;713}714EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();715PackedVector2Array points = node->get_curve()->get_points().duplicate();716717undo_redo->create_action(TTR("Clear Curve Points"), UndoRedo::MERGE_DISABLE, node);718undo_redo->add_do_method(this, "_clear_curve_points", node);719undo_redo->add_undo_method(this, "_restore_curve_points", node, points);720undo_redo->add_do_method(canvas_item_editor, "update_viewport");721undo_redo->add_undo_method(canvas_item_editor, "update_viewport");722undo_redo->commit_action();723return;724}725mode = Mode(p_mode);726}727728void Path2DEditor::_handle_option_pressed(int p_option) {729PopupMenu *pm;730pm = handle_menu->get_popup();731732switch (p_option) {733case HANDLE_OPTION_ANGLE: {734bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);735mirror_handle_angle = !is_checked;736pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);737pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);738} break;739case HANDLE_OPTION_LENGTH: {740bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);741mirror_handle_length = !is_checked;742pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);743} break;744}745}746747void Path2DEditor::_cancel_current_action() {748ERR_FAIL_NULL(node);749Ref<Curve2D> curve = node->get_curve();750ERR_FAIL_COND(curve.is_null());751752switch (action) {753case ACTION_MOVING_POINT: {754curve->set_point_position(action_point, moving_from);755} break;756757case ACTION_MOVING_NEW_POINT: {758curve->remove_point(curve->get_point_count() - 1);759} break;760761case ACTION_MOVING_NEW_POINT_FROM_SPLIT: {762curve->remove_point(action_point);763} break;764765case ACTION_MOVING_IN: {766curve->set_point_in(action_point, moving_from);767curve->set_point_out(action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_out_length));768} break;769770case ACTION_MOVING_OUT: {771curve->set_point_out(action_point, moving_from);772curve->set_point_in(action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_in_length));773} break;774775default: {776}777}778779canvas_item_editor->update_viewport();780action = ACTION_NONE;781}782783void Path2DEditor::_create_curve() {784ERR_FAIL_NULL(node);785786Ref<Curve2D> new_curve;787new_curve.instantiate();788789EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();790undo_redo->create_action(TTR("Create Curve in Path2D"));791undo_redo->add_do_property(node, "curve", new_curve);792undo_redo->add_undo_property(node, "curve", Ref<Curve2D>());793undo_redo->add_do_method(this, "_update_toolbar");794undo_redo->add_undo_method(this, "_update_toolbar");795undo_redo->commit_action();796}797798void Path2DEditor::_confirm_clear_points() {799if (!node || node->get_curve().is_null()) {800return;801}802if (node->get_curve()->get_point_count() == 0) {803return;804}805clear_points_dialog->reset_size();806clear_points_dialog->popup_centered();807}808809void Path2DEditor::_clear_curve_points(Path2D *p_path2d) {810if (!p_path2d || p_path2d->get_curve().is_null()) {811return;812}813Ref<Curve2D> curve = p_path2d->get_curve();814815if (curve->get_point_count() == 0) {816return;817}818curve->clear_points();819820if (node == p_path2d) {821_mode_selected(MODE_CREATE);822}823}824825void Path2DEditor::_restore_curve_points(Path2D *p_path2d, const PackedVector2Array &p_points) {826if (!p_path2d || p_path2d->get_curve().is_null()) {827return;828}829Ref<Curve2D> curve = p_path2d->get_curve();830831if (curve->get_point_count() > 0) {832curve->clear_points();833}834835for (int i = 0; i < p_points.size(); i += 3) {836curve->add_point(p_points[i + 2], p_points[i], p_points[i + 1]); // The Curve2D::points pattern is [point_in, point_out, point_position].837}838839if (node == p_path2d) {840_mode_selected(MODE_EDIT);841}842}843844Path2DEditor::Path2DEditor() {845toolbar = memnew(HBoxContainer);846847curve_edit = memnew(Button);848curve_edit->set_theme_type_variation(SceneStringName(FlatButton));849curve_edit->set_toggle_mode(true);850curve_edit->set_pressed(true);851curve_edit->set_focus_mode(Control::FOCUS_ACCESSIBILITY);852curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Drag: Select Control Points") + "\n" + vformat(TTR("%s+Click: Add Point"), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL)) + "\n" + TTR("Left Click: Split Segment (in curve)") + "\n" + TTR("Right Click: Delete Point"));853curve_edit->set_accessibility_name(TTRC("Select Points"));854curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT));855toolbar->add_child(curve_edit);856857curve_edit_curve = memnew(Button);858curve_edit_curve->set_theme_type_variation(SceneStringName(FlatButton));859curve_edit_curve->set_toggle_mode(true);860curve_edit_curve->set_focus_mode(Control::FOCUS_ACCESSIBILITY);861curve_edit_curve->set_tooltip_text(TTR("Select Control Points (Shift+Drag)"));862curve_edit_curve->set_accessibility_name(TTRC("Select Control Points"));863curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT_CURVE));864toolbar->add_child(curve_edit_curve);865866curve_create = memnew(Button);867curve_create->set_theme_type_variation(SceneStringName(FlatButton));868curve_create->set_toggle_mode(true);869curve_create->set_focus_mode(Control::FOCUS_ACCESSIBILITY);870curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Right Click: Delete Point"));871curve_create->set_accessibility_name(TTRC("Add Point (in empty space)"));872curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CREATE));873toolbar->add_child(curve_create);874875curve_del = memnew(Button);876curve_del->set_theme_type_variation(SceneStringName(FlatButton));877curve_del->set_toggle_mode(true);878curve_del->set_focus_mode(Control::FOCUS_ACCESSIBILITY);879curve_del->set_tooltip_text(TTR("Delete Point"));880curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_DELETE));881toolbar->add_child(curve_del);882883curve_close = memnew(Button);884curve_close->set_theme_type_variation(SceneStringName(FlatButton));885curve_close->set_focus_mode(Control::FOCUS_ACCESSIBILITY);886curve_close->set_tooltip_text(TTR("Close Curve"));887curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CLOSE));888toolbar->add_child(curve_close);889890curve_clear_points = memnew(Button);891curve_clear_points->set_theme_type_variation(SceneStringName(FlatButton));892curve_clear_points->set_focus_mode(Control::FOCUS_ACCESSIBILITY);893curve_clear_points->set_tooltip_text(TTR("Clear Points"));894curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_confirm_clear_points));895toolbar->add_child(curve_clear_points);896897clear_points_dialog = memnew(ConfirmationDialog);898clear_points_dialog->set_title(TTR("Please Confirm..."));899clear_points_dialog->set_text(TTR("Remove all curve points?"));900clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CLEAR_POINTS));901toolbar->add_child(clear_points_dialog);902903handle_menu = memnew(MenuButton);904handle_menu->set_flat(false);905handle_menu->set_theme_type_variation("FlatMenuButton");906handle_menu->set_text(TTR("Options"));907toolbar->add_child(handle_menu);908909PopupMenu *menu = handle_menu->get_popup();910menu->add_check_item(TTR("Mirror Handle Angles"));911menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);912menu->add_check_item(TTR("Mirror Handle Lengths"));913menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);914menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path2DEditor::_handle_option_pressed));915916add_child(toolbar);917918create_curve_button = memnew(Button);919create_curve_button->set_text(TTR("Create Curve"));920create_curve_button->hide();921add_child(create_curve_button);922create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_create_curve));923924ERR_FAIL_NULL(RS::get_singleton());925RenderingServer *rs = RS::get_singleton();926927debug_mesh_rid = rs->mesh_create();928929{930debug_handle_mesh_rid = rs->mesh_create();931932Vector<Vector2> vertex_array;933vertex_array.resize(4);934Vector2 *vertex_array_ptrw = vertex_array.ptrw();935vertex_array_ptrw[0] = Vector2(-1.0, -1.0);936vertex_array_ptrw[1] = Vector2(1.0, -1.0);937vertex_array_ptrw[2] = Vector2(1.0, 1.0);938vertex_array_ptrw[3] = Vector2(-1.0, 1.0);939940Vector<Vector2> uv_array;941uv_array.resize(4);942Vector2 *uv_array_ptrw = uv_array.ptrw();943uv_array_ptrw[0] = Vector2(0.0, 0.0);944uv_array_ptrw[1] = Vector2(1.0, 0.0);945uv_array_ptrw[2] = Vector2(1.0, 1.0);946uv_array_ptrw[3] = Vector2(0.0, 1.0);947948Vector<int> index_array;949index_array.resize(6);950int *index_array_ptrw = index_array.ptrw();951index_array_ptrw[0] = 0;952index_array_ptrw[1] = 1;953index_array_ptrw[2] = 3;954index_array_ptrw[3] = 1;955index_array_ptrw[4] = 2;956index_array_ptrw[5] = 3;957958Array mesh_arrays;959mesh_arrays.resize(RS::ARRAY_MAX);960mesh_arrays[RS::ARRAY_VERTEX] = vertex_array;961mesh_arrays[RS::ARRAY_TEX_UV] = uv_array;962mesh_arrays[RS::ARRAY_INDEX] = index_array;963964rs->mesh_add_surface_from_arrays(debug_handle_mesh_rid, RS::PRIMITIVE_TRIANGLES, mesh_arrays, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);965966debug_handle_curve_multimesh_rid = rs->multimesh_create();967debug_handle_sharp_multimesh_rid = rs->multimesh_create();968debug_handle_smooth_multimesh_rid = rs->multimesh_create();969970rs->multimesh_set_mesh(debug_handle_curve_multimesh_rid, debug_handle_mesh_rid);971rs->multimesh_set_mesh(debug_handle_sharp_multimesh_rid, debug_handle_mesh_rid);972rs->multimesh_set_mesh(debug_handle_smooth_multimesh_rid, debug_handle_mesh_rid);973}974}975976Path2DEditor::~Path2DEditor() {977ERR_FAIL_NULL(RS::get_singleton());978RS::get_singleton()->free_rid(debug_mesh_rid);979RS::get_singleton()->free_rid(debug_handle_curve_multimesh_rid);980RS::get_singleton()->free_rid(debug_handle_sharp_multimesh_rid);981RS::get_singleton()->free_rid(debug_handle_smooth_multimesh_rid);982RS::get_singleton()->free_rid(debug_handle_mesh_rid);983}984985void Path2DEditorPlugin::edit(Object *p_object) {986path2d_editor->edit(Object::cast_to<Node>(p_object));987}988989bool Path2DEditorPlugin::handles(Object *p_object) const {990return p_object->is_class("Path2D");991}992993void Path2DEditorPlugin::make_visible(bool p_visible) {994if (p_visible) {995path2d_editor->show();996} else {997path2d_editor->hide();998path2d_editor->edit(nullptr);999}1000}10011002Path2DEditorPlugin::Path2DEditorPlugin() {1003path2d_editor = memnew(Path2DEditor);1004CanvasItemEditor::get_singleton()->add_control_to_menu_panel(path2d_editor);1005path2d_editor->hide();1006}100710081009