| 1 | /**************************************************************************/ |
| 2 | /* path_2d_editor_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "path_2d_editor_plugin.h" |
| 32 | |
| 33 | #include "canvas_item_editor_plugin.h" |
| 34 | #include "core/io/file_access.h" |
| 35 | #include "core/os/keyboard.h" |
| 36 | #include "editor/editor_node.h" |
| 37 | #include "editor/editor_scale.h" |
| 38 | #include "editor/editor_settings.h" |
| 39 | #include "editor/editor_undo_redo_manager.h" |
| 40 | #include "scene/gui/menu_button.h" |
| 41 | |
| 42 | void Path2DEditor::_notification(int p_what) { |
| 43 | switch (p_what) { |
| 44 | case NOTIFICATION_ENTER_TREE: |
| 45 | case NOTIFICATION_THEME_CHANGED: { |
| 46 | curve_edit->set_icon(get_editor_theme_icon(SNAME("CurveEdit" ))); |
| 47 | curve_edit_curve->set_icon(get_editor_theme_icon(SNAME("CurveCurve" ))); |
| 48 | curve_create->set_icon(get_editor_theme_icon(SNAME("CurveCreate" ))); |
| 49 | curve_del->set_icon(get_editor_theme_icon(SNAME("CurveDelete" ))); |
| 50 | curve_close->set_icon(get_editor_theme_icon(SNAME("CurveClose" ))); |
| 51 | } break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void Path2DEditor::_node_removed(Node *p_node) { |
| 56 | if (p_node == node) { |
| 57 | node = nullptr; |
| 58 | hide(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { |
| 63 | if (!node) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (!node->is_visible_in_tree()) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if (!node->get_curve().is_valid()) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius" ); |
| 76 | |
| 77 | Ref<InputEventMouseButton> mb = p_event; |
| 78 | if (mb.is_valid()) { |
| 79 | Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); |
| 80 | |
| 81 | Vector2 gpoint = mb->get_position(); |
| 82 | Vector2 cpoint = node->to_local(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mb->get_position()))); |
| 83 | |
| 84 | if (mb->is_pressed() && action == ACTION_NONE) { |
| 85 | Ref<Curve2D> curve = node->get_curve(); |
| 86 | |
| 87 | for (int i = 0; i < curve->get_point_count(); i++) { |
| 88 | real_t dist_to_p = gpoint.distance_to(xform.xform(curve->get_point_position(i))); |
| 89 | real_t dist_to_p_out = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_out(i))); |
| 90 | real_t dist_to_p_in = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_in(i))); |
| 91 | |
| 92 | // Check for point movement start (for point + in/out controls). |
| 93 | if (mb->get_button_index() == MouseButton::LEFT) { |
| 94 | if (mode == MODE_EDIT && !mb->is_shift_pressed() && dist_to_p < grab_threshold) { |
| 95 | // Points can only be moved in edit mode. |
| 96 | |
| 97 | action = ACTION_MOVING_POINT; |
| 98 | action_point = i; |
| 99 | moving_from = curve->get_point_position(i); |
| 100 | moving_screen_from = gpoint; |
| 101 | return true; |
| 102 | } else if (mode == MODE_EDIT || mode == MODE_EDIT_CURVE) { |
| 103 | // In/out controls can be moved in multiple modes. |
| 104 | if (dist_to_p_out < grab_threshold && i < (curve->get_point_count() - 1)) { |
| 105 | action = ACTION_MOVING_OUT; |
| 106 | action_point = i; |
| 107 | moving_from = curve->get_point_out(i); |
| 108 | moving_screen_from = gpoint; |
| 109 | orig_in_length = curve->get_point_in(action_point).length(); |
| 110 | return true; |
| 111 | } else if (dist_to_p_in < grab_threshold && i > 0) { |
| 112 | action = ACTION_MOVING_IN; |
| 113 | action_point = i; |
| 114 | moving_from = curve->get_point_in(i); |
| 115 | moving_screen_from = gpoint; |
| 116 | orig_out_length = curve->get_point_out(action_point).length(); |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Check for point deletion. |
| 123 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 124 | if ((mb->get_button_index() == MouseButton::RIGHT && mode == MODE_EDIT) || (mb->get_button_index() == MouseButton::LEFT && mode == MODE_DELETE)) { |
| 125 | if (dist_to_p < grab_threshold) { |
| 126 | undo_redo->create_action(TTR("Remove Point from Curve" )); |
| 127 | undo_redo->add_do_method(curve.ptr(), "remove_point" , i); |
| 128 | undo_redo->add_undo_method(curve.ptr(), "add_point" , curve->get_point_position(i), curve->get_point_in(i), curve->get_point_out(i), i); |
| 129 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 130 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 131 | undo_redo->commit_action(); |
| 132 | return true; |
| 133 | } else if (dist_to_p_out < grab_threshold) { |
| 134 | undo_redo->create_action(TTR("Remove Out-Control from Curve" )); |
| 135 | undo_redo->add_do_method(curve.ptr(), "set_point_out" , i, Vector2()); |
| 136 | undo_redo->add_undo_method(curve.ptr(), "set_point_out" , i, curve->get_point_out(i)); |
| 137 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 138 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 139 | undo_redo->commit_action(); |
| 140 | return true; |
| 141 | } else if (dist_to_p_in < grab_threshold) { |
| 142 | undo_redo->create_action(TTR("Remove In-Control from Curve" )); |
| 143 | undo_redo->add_do_method(curve.ptr(), "set_point_in" , i, Vector2()); |
| 144 | undo_redo->add_undo_method(curve.ptr(), "set_point_in" , i, curve->get_point_in(i)); |
| 145 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 146 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 147 | undo_redo->commit_action(); |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Check for point creation. |
| 155 | if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && ((mb->is_command_or_control_pressed() && mode == MODE_EDIT) || mode == MODE_CREATE)) { |
| 156 | Ref<Curve2D> curve = node->get_curve(); |
| 157 | |
| 158 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 159 | undo_redo->create_action(TTR("Add Point to Curve" )); |
| 160 | undo_redo->add_do_method(curve.ptr(), "add_point" , cpoint); |
| 161 | undo_redo->add_undo_method(curve.ptr(), "remove_point" , curve->get_point_count()); |
| 162 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 163 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 164 | undo_redo->commit_action(); |
| 165 | |
| 166 | action = ACTION_MOVING_POINT; |
| 167 | action_point = curve->get_point_count() - 1; |
| 168 | moving_from = curve->get_point_position(action_point); |
| 169 | moving_screen_from = gpoint; |
| 170 | |
| 171 | canvas_item_editor->update_viewport(); |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | // Check for segment split. |
| 177 | if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && mode == MODE_EDIT && on_edge) { |
| 178 | Vector2 gpoint2 = mb->get_position(); |
| 179 | Ref<Curve2D> curve = node->get_curve(); |
| 180 | |
| 181 | int insertion_point = -1; |
| 182 | float mbLength = curve->get_closest_offset(xform.affine_inverse().xform(gpoint2)); |
| 183 | int len = curve->get_point_count(); |
| 184 | for (int i = 0; i < len - 1; i++) { |
| 185 | float compareLength = curve->get_closest_offset(curve->get_point_position(i + 1)); |
| 186 | if (mbLength >= curve->get_closest_offset(curve->get_point_position(i)) && mbLength <= compareLength) { |
| 187 | insertion_point = i; |
| 188 | } |
| 189 | } |
| 190 | if (insertion_point == -1) { |
| 191 | insertion_point = curve->get_point_count() - 2; |
| 192 | } |
| 193 | |
| 194 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 195 | undo_redo->create_action(TTR("Split Curve" )); |
| 196 | undo_redo->add_do_method(curve.ptr(), "add_point" , xform.affine_inverse().xform(gpoint2), Vector2(0, 0), Vector2(0, 0), insertion_point + 1); |
| 197 | undo_redo->add_undo_method(curve.ptr(), "remove_point" , insertion_point + 1); |
| 198 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 199 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 200 | undo_redo->commit_action(); |
| 201 | |
| 202 | action = ACTION_MOVING_POINT; |
| 203 | action_point = insertion_point + 1; |
| 204 | moving_from = curve->get_point_position(action_point); |
| 205 | moving_screen_from = gpoint2; |
| 206 | |
| 207 | canvas_item_editor->update_viewport(); |
| 208 | |
| 209 | on_edge = false; |
| 210 | |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | // Check for point movement completion. |
| 215 | if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && action != ACTION_NONE) { |
| 216 | Ref<Curve2D> curve = node->get_curve(); |
| 217 | |
| 218 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 219 | Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from); |
| 220 | switch (action) { |
| 221 | case ACTION_NONE: |
| 222 | // N/A, handled in above condition. |
| 223 | break; |
| 224 | |
| 225 | case ACTION_MOVING_POINT: { |
| 226 | undo_redo->create_action(TTR("Move Point in Curve" )); |
| 227 | undo_redo->add_do_method(curve.ptr(), "set_point_position" , action_point, cpoint); |
| 228 | undo_redo->add_undo_method(curve.ptr(), "set_point_position" , action_point, moving_from); |
| 229 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 230 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 231 | undo_redo->commit_action(); |
| 232 | |
| 233 | } break; |
| 234 | |
| 235 | case ACTION_MOVING_IN: { |
| 236 | undo_redo->create_action(TTR("Move In-Control in Curve" )); |
| 237 | undo_redo->add_do_method(curve.ptr(), "set_point_in" , action_point, new_pos); |
| 238 | undo_redo->add_undo_method(curve.ptr(), "set_point_in" , action_point, moving_from); |
| 239 | |
| 240 | if (mirror_handle_angle) { |
| 241 | undo_redo->add_do_method(curve.ptr(), "set_point_out" , action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length)); |
| 242 | undo_redo->add_undo_method(curve.ptr(), "set_point_out" , action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_out_length)); |
| 243 | } |
| 244 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 245 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 246 | undo_redo->commit_action(); |
| 247 | |
| 248 | } break; |
| 249 | |
| 250 | case ACTION_MOVING_OUT: { |
| 251 | undo_redo->create_action(TTR("Move Out-Control in Curve" )); |
| 252 | undo_redo->add_do_method(curve.ptr(), "set_point_out" , action_point, new_pos); |
| 253 | undo_redo->add_undo_method(curve.ptr(), "set_point_out" , action_point, moving_from); |
| 254 | |
| 255 | if (mirror_handle_angle) { |
| 256 | undo_redo->add_do_method(curve.ptr(), "set_point_in" , action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length)); |
| 257 | undo_redo->add_undo_method(curve.ptr(), "set_point_in" , action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_in_length)); |
| 258 | } |
| 259 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 260 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 261 | undo_redo->commit_action(); |
| 262 | |
| 263 | } break; |
| 264 | } |
| 265 | |
| 266 | action = ACTION_NONE; |
| 267 | |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | Ref<InputEventMouseMotion> mm = p_event; |
| 273 | |
| 274 | if (mm.is_valid()) { |
| 275 | if (action == ACTION_NONE && mode == MODE_EDIT) { |
| 276 | // Handle Edge Follow |
| 277 | bool old_edge = on_edge; |
| 278 | |
| 279 | Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); |
| 280 | Vector2 gpoint = mm->get_position(); |
| 281 | |
| 282 | Ref<Curve2D> curve = node->get_curve(); |
| 283 | if (curve == nullptr) { |
| 284 | return true; |
| 285 | } |
| 286 | if (curve->get_point_count() < 2) { |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | // Find edge |
| 291 | edge_point = xform.xform(curve->get_closest_point(xform.affine_inverse().xform(mm->get_position()))); |
| 292 | on_edge = false; |
| 293 | if (edge_point.distance_to(gpoint) <= grab_threshold) { |
| 294 | on_edge = true; |
| 295 | } |
| 296 | // However, if near a control point or its in-out handles then not on edge |
| 297 | int len = curve->get_point_count(); |
| 298 | for (int i = 0; i < len; i++) { |
| 299 | Vector2 pp = curve->get_point_position(i); |
| 300 | Vector2 p = xform.xform(pp); |
| 301 | if (p.distance_to(gpoint) <= grab_threshold) { |
| 302 | on_edge = false; |
| 303 | break; |
| 304 | } |
| 305 | p = xform.xform(pp + curve->get_point_in(i)); |
| 306 | if (p.distance_to(gpoint) <= grab_threshold) { |
| 307 | on_edge = false; |
| 308 | break; |
| 309 | } |
| 310 | p = xform.xform(pp + curve->get_point_out(i)); |
| 311 | if (p.distance_to(gpoint) <= grab_threshold) { |
| 312 | on_edge = false; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | if (on_edge || old_edge != on_edge) { |
| 317 | canvas_item_editor->update_viewport(); |
| 318 | return true; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (action != ACTION_NONE) { |
| 323 | // Handle point/control movement. |
| 324 | Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); |
| 325 | Vector2 gpoint = mm->get_position(); |
| 326 | Vector2 cpoint = node->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()))); |
| 327 | |
| 328 | Ref<Curve2D> curve = node->get_curve(); |
| 329 | |
| 330 | Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from); |
| 331 | |
| 332 | switch (action) { |
| 333 | case ACTION_NONE: |
| 334 | // N/A, handled in above condition. |
| 335 | break; |
| 336 | |
| 337 | case ACTION_MOVING_POINT: { |
| 338 | curve->set_point_position(action_point, cpoint); |
| 339 | } break; |
| 340 | |
| 341 | case ACTION_MOVING_IN: { |
| 342 | curve->set_point_in(action_point, new_pos); |
| 343 | |
| 344 | if (mirror_handle_angle) { |
| 345 | curve->set_point_out(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length)); |
| 346 | } |
| 347 | } break; |
| 348 | |
| 349 | case ACTION_MOVING_OUT: { |
| 350 | curve->set_point_out(action_point, new_pos); |
| 351 | |
| 352 | if (mirror_handle_angle) { |
| 353 | curve->set_point_in(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length)); |
| 354 | } |
| 355 | } break; |
| 356 | } |
| 357 | |
| 358 | canvas_item_editor->update_viewport(); |
| 359 | return true; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | void Path2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) { |
| 367 | if (!node || !node->is_visible_in_tree() || !node->get_curve().is_valid()) { |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); |
| 372 | |
| 373 | const Ref<Texture2D> path_sharp_handle = get_editor_theme_icon(SNAME("EditorPathSharpHandle" )); |
| 374 | const Ref<Texture2D> path_smooth_handle = get_editor_theme_icon(SNAME("EditorPathSmoothHandle" )); |
| 375 | // Both handle icons must be of the same size |
| 376 | const Size2 handle_size = path_sharp_handle->get_size(); |
| 377 | |
| 378 | const Ref<Texture2D> curve_handle = get_editor_theme_icon(SNAME("EditorCurveHandle" )); |
| 379 | const Size2 curve_handle_size = curve_handle->get_size(); |
| 380 | |
| 381 | Ref<Curve2D> curve = node->get_curve(); |
| 382 | |
| 383 | int len = curve->get_point_count(); |
| 384 | Control *vpc = canvas_item_editor->get_viewport_control(); |
| 385 | |
| 386 | for (int i = 0; i < len; i++) { |
| 387 | Vector2 point = xform.xform(curve->get_point_position(i)); |
| 388 | // Determines the point icon to be used |
| 389 | bool smooth = false; |
| 390 | |
| 391 | if (i < len - 1) { |
| 392 | Vector2 pointout = xform.xform(curve->get_point_position(i) + curve->get_point_out(i)); |
| 393 | if (point != pointout) { |
| 394 | smooth = true; |
| 395 | // Draw the line with a dark and light color to be visible on all backgrounds |
| 396 | vpc->draw_line(point, pointout, Color(0, 0, 0, 0.5), Math::round(EDSCALE)); |
| 397 | vpc->draw_line(point, pointout, Color(1, 1, 1, 0.5), Math::round(EDSCALE)); |
| 398 | vpc->draw_texture_rect(curve_handle, Rect2(pointout - curve_handle_size * 0.5, curve_handle_size), false, Color(1, 1, 1, 0.75)); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (i > 0) { |
| 403 | Vector2 pointin = xform.xform(curve->get_point_position(i) + curve->get_point_in(i)); |
| 404 | if (point != pointin) { |
| 405 | smooth = true; |
| 406 | // Draw the line with a dark and light color to be visible on all backgrounds |
| 407 | vpc->draw_line(point, pointin, Color(0, 0, 0, 0.5), Math::round(EDSCALE)); |
| 408 | vpc->draw_line(point, pointin, Color(1, 1, 1, 0.5), Math::round(EDSCALE)); |
| 409 | vpc->draw_texture_rect(curve_handle, Rect2(pointin - curve_handle_size * 0.5, curve_handle_size), false, Color(1, 1, 1, 0.75)); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | vpc->draw_texture_rect( |
| 414 | smooth ? path_smooth_handle : path_sharp_handle, |
| 415 | Rect2(point - handle_size * 0.5, handle_size), |
| 416 | false); |
| 417 | } |
| 418 | |
| 419 | if (on_edge) { |
| 420 | Ref<Texture2D> add_handle = get_editor_theme_icon(SNAME("EditorHandleAdd" )); |
| 421 | p_overlay->draw_texture(add_handle, edge_point - add_handle->get_size() * 0.5); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void Path2DEditor::_node_visibility_changed() { |
| 426 | if (!node) { |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | canvas_item_editor->update_viewport(); |
| 431 | } |
| 432 | |
| 433 | void Path2DEditor::edit(Node *p_path2d) { |
| 434 | if (!canvas_item_editor) { |
| 435 | canvas_item_editor = CanvasItemEditor::get_singleton(); |
| 436 | } |
| 437 | |
| 438 | if (p_path2d) { |
| 439 | node = Object::cast_to<Path2D>(p_path2d); |
| 440 | if (!node->is_connected("visibility_changed" , callable_mp(this, &Path2DEditor::_node_visibility_changed))) { |
| 441 | node->connect("visibility_changed" , callable_mp(this, &Path2DEditor::_node_visibility_changed)); |
| 442 | } |
| 443 | |
| 444 | } else { |
| 445 | // node may have been deleted at this point |
| 446 | if (node && node->is_connected("visibility_changed" , callable_mp(this, &Path2DEditor::_node_visibility_changed))) { |
| 447 | node->disconnect("visibility_changed" , callable_mp(this, &Path2DEditor::_node_visibility_changed)); |
| 448 | } |
| 449 | node = nullptr; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void Path2DEditor::_bind_methods() { |
| 454 | //ClassDB::bind_method(D_METHOD("_menu_option"),&Path2DEditor::_menu_option); |
| 455 | } |
| 456 | |
| 457 | void Path2DEditor::_mode_selected(int p_mode) { |
| 458 | if (p_mode == MODE_CREATE) { |
| 459 | curve_create->set_pressed(true); |
| 460 | curve_edit->set_pressed(false); |
| 461 | curve_edit_curve->set_pressed(false); |
| 462 | curve_del->set_pressed(false); |
| 463 | } else if (p_mode == MODE_EDIT) { |
| 464 | curve_create->set_pressed(false); |
| 465 | curve_edit->set_pressed(true); |
| 466 | curve_edit_curve->set_pressed(false); |
| 467 | curve_del->set_pressed(false); |
| 468 | } else if (p_mode == MODE_EDIT_CURVE) { |
| 469 | curve_create->set_pressed(false); |
| 470 | curve_edit->set_pressed(false); |
| 471 | curve_edit_curve->set_pressed(true); |
| 472 | curve_del->set_pressed(false); |
| 473 | } else if (p_mode == MODE_DELETE) { |
| 474 | curve_create->set_pressed(false); |
| 475 | curve_edit->set_pressed(false); |
| 476 | curve_edit_curve->set_pressed(false); |
| 477 | curve_del->set_pressed(true); |
| 478 | } else if (p_mode == ACTION_CLOSE) { |
| 479 | //? |
| 480 | |
| 481 | if (!node->get_curve().is_valid()) { |
| 482 | return; |
| 483 | } |
| 484 | if (node->get_curve()->get_point_count() < 3) { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | Vector2 begin = node->get_curve()->get_point_position(0); |
| 489 | Vector2 end = node->get_curve()->get_point_position(node->get_curve()->get_point_count() - 1); |
| 490 | if (begin.is_equal_approx(end)) { |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 495 | undo_redo->create_action(TTR("Remove Point from Curve" )); |
| 496 | undo_redo->add_do_method(node->get_curve().ptr(), "add_point" , begin); |
| 497 | undo_redo->add_undo_method(node->get_curve().ptr(), "remove_point" , node->get_curve()->get_point_count()); |
| 498 | undo_redo->add_do_method(canvas_item_editor, "update_viewport" ); |
| 499 | undo_redo->add_undo_method(canvas_item_editor, "update_viewport" ); |
| 500 | undo_redo->commit_action(); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | mode = Mode(p_mode); |
| 505 | } |
| 506 | |
| 507 | void Path2DEditor::_handle_option_pressed(int p_option) { |
| 508 | PopupMenu *pm; |
| 509 | pm = handle_menu->get_popup(); |
| 510 | |
| 511 | switch (p_option) { |
| 512 | case HANDLE_OPTION_ANGLE: { |
| 513 | bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE); |
| 514 | mirror_handle_angle = !is_checked; |
| 515 | pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle); |
| 516 | pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle); |
| 517 | } break; |
| 518 | case HANDLE_OPTION_LENGTH: { |
| 519 | bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH); |
| 520 | mirror_handle_length = !is_checked; |
| 521 | pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length); |
| 522 | } break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | Path2DEditor::Path2DEditor() { |
| 527 | canvas_item_editor = nullptr; |
| 528 | mirror_handle_angle = true; |
| 529 | mirror_handle_length = true; |
| 530 | on_edge = false; |
| 531 | |
| 532 | mode = MODE_EDIT; |
| 533 | action = ACTION_NONE; |
| 534 | |
| 535 | curve_edit = memnew(Button); |
| 536 | curve_edit->set_flat(true); |
| 537 | curve_edit->set_toggle_mode(true); |
| 538 | curve_edit->set_pressed(true); |
| 539 | curve_edit->set_focus_mode(Control::FOCUS_NONE); |
| 540 | curve_edit->set_tooltip_text(TTR("Select Points" ) + "\n" + TTR("Shift+Drag: Select Control Points" ) + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Click: Add Point" ) + "\n" + TTR("Left Click: Split Segment (in curve)" ) + "\n" + TTR("Right Click: Delete Point" )); |
| 541 | curve_edit->connect("pressed" , callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT)); |
| 542 | add_child(curve_edit); |
| 543 | |
| 544 | curve_edit_curve = memnew(Button); |
| 545 | curve_edit_curve->set_flat(true); |
| 546 | curve_edit_curve->set_toggle_mode(true); |
| 547 | curve_edit_curve->set_focus_mode(Control::FOCUS_NONE); |
| 548 | curve_edit_curve->set_tooltip_text(TTR("Select Control Points (Shift+Drag)" )); |
| 549 | curve_edit_curve->connect("pressed" , callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT_CURVE)); |
| 550 | add_child(curve_edit_curve); |
| 551 | |
| 552 | curve_create = memnew(Button); |
| 553 | curve_create->set_flat(true); |
| 554 | curve_create->set_toggle_mode(true); |
| 555 | curve_create->set_focus_mode(Control::FOCUS_NONE); |
| 556 | curve_create->set_tooltip_text(TTR("Add Point (in empty space)" )); |
| 557 | curve_create->connect("pressed" , callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CREATE)); |
| 558 | add_child(curve_create); |
| 559 | |
| 560 | curve_del = memnew(Button); |
| 561 | curve_del->set_flat(true); |
| 562 | curve_del->set_toggle_mode(true); |
| 563 | curve_del->set_focus_mode(Control::FOCUS_NONE); |
| 564 | curve_del->set_tooltip_text(TTR("Delete Point" )); |
| 565 | curve_del->connect("pressed" , callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_DELETE)); |
| 566 | add_child(curve_del); |
| 567 | |
| 568 | curve_close = memnew(Button); |
| 569 | curve_close->set_flat(true); |
| 570 | curve_close->set_focus_mode(Control::FOCUS_NONE); |
| 571 | curve_close->set_tooltip_text(TTR("Close Curve" )); |
| 572 | curve_close->connect("pressed" , callable_mp(this, &Path2DEditor::_mode_selected).bind(ACTION_CLOSE)); |
| 573 | add_child(curve_close); |
| 574 | |
| 575 | PopupMenu *; |
| 576 | |
| 577 | handle_menu = memnew(MenuButton); |
| 578 | handle_menu->set_text(TTR("Options" )); |
| 579 | add_child(handle_menu); |
| 580 | |
| 581 | menu = handle_menu->get_popup(); |
| 582 | menu->add_check_item(TTR("Mirror Handle Angles" )); |
| 583 | menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle); |
| 584 | menu->add_check_item(TTR("Mirror Handle Lengths" )); |
| 585 | menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length); |
| 586 | menu->connect("id_pressed" , callable_mp(this, &Path2DEditor::_handle_option_pressed)); |
| 587 | } |
| 588 | |
| 589 | void Path2DEditorPlugin::edit(Object *p_object) { |
| 590 | path2d_editor->edit(Object::cast_to<Node>(p_object)); |
| 591 | } |
| 592 | |
| 593 | bool Path2DEditorPlugin::handles(Object *p_object) const { |
| 594 | return p_object->is_class("Path2D" ); |
| 595 | } |
| 596 | |
| 597 | void Path2DEditorPlugin::make_visible(bool p_visible) { |
| 598 | if (p_visible) { |
| 599 | path2d_editor->show(); |
| 600 | |
| 601 | } else { |
| 602 | path2d_editor->hide(); |
| 603 | path2d_editor->edit(nullptr); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | Path2DEditorPlugin::Path2DEditorPlugin() { |
| 608 | path2d_editor = memnew(Path2DEditor); |
| 609 | CanvasItemEditor::get_singleton()->add_control_to_menu_panel(path2d_editor); |
| 610 | path2d_editor->hide(); |
| 611 | } |
| 612 | |
| 613 | Path2DEditorPlugin::~Path2DEditorPlugin() { |
| 614 | } |
| 615 | |