From 19975b154814f5c07d7892ffd938a0c50f97f00c Mon Sep 17 00:00:00 2001 From: kobewi Date: Thu, 5 Sep 2024 20:07:16 +0200 Subject: [PATCH] Add Create Curve button to Path2D editor --- editor/plugins/path_2d_editor_plugin.cpp | 61 +++++++++++++++++++----- editor/plugins/path_2d_editor_plugin.h | 4 ++ editor/plugins/path_3d_editor_plugin.cpp | 55 +++++++++++++++++---- editor/plugins/path_3d_editor_plugin.h | 9 +++- 4 files changed, 105 insertions(+), 24 deletions(-) diff --git a/editor/plugins/path_2d_editor_plugin.cpp b/editor/plugins/path_2d_editor_plugin.cpp index 24cc15d6566..0455b266b43 100644 --- a/editor/plugins/path_2d_editor_plugin.cpp +++ b/editor/plugins/path_2d_editor_plugin.cpp @@ -42,7 +42,6 @@ void Path2DEditor::_notification(int p_what) { switch (p_what) { - case NOTIFICATION_ENTER_TREE: case NOTIFICATION_THEME_CHANGED: { curve_edit->set_icon(get_editor_theme_icon(SNAME("CurveEdit"))); curve_edit_curve->set_icon(get_editor_theme_icon(SNAME("CurveCurve"))); @@ -50,6 +49,8 @@ void Path2DEditor::_notification(int p_what) { curve_del->set_icon(get_editor_theme_icon(SNAME("CurveDelete"))); curve_close->set_icon(get_editor_theme_icon(SNAME("CurveClose"))); curve_clear_points->set_icon(get_editor_theme_icon(SNAME("Clear"))); + + create_curve_button->set_icon(get_editor_theme_icon(SNAME("Curve2D"))); } break; } } @@ -450,6 +451,16 @@ void Path2DEditor::_node_visibility_changed() { } canvas_item_editor->update_viewport(); + _update_toolbar(); +} + +void Path2DEditor::_update_toolbar() { + if (!node) { + return; + } + bool has_curve = node->get_curve().is_valid(); + toolbar->set_visible(has_curve); + create_curve_button->set_visible(!has_curve); } void Path2DEditor::edit(Node *p_path2d) { @@ -463,6 +474,7 @@ void Path2DEditor::edit(Node *p_path2d) { if (p_path2d) { node = Object::cast_to(p_path2d); + _update_toolbar(); if (!node->is_connected(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed))) { node->connect(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed)); @@ -477,7 +489,7 @@ void Path2DEditor::edit(Node *p_path2d) { } void Path2DEditor::_bind_methods() { - //ClassDB::bind_method(D_METHOD("_menu_option"),&Path2DEditor::_menu_option); + ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path2DEditor::_update_toolbar); ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path2DEditor::_clear_curve_points); ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path2DEditor::_restore_curve_points); } @@ -601,6 +613,21 @@ void Path2DEditor::_cancel_current_action() { action = ACTION_NONE; } +void Path2DEditor::_create_curve() { + ERR_FAIL_NULL(node); + + Ref new_curve; + new_curve.instantiate(); + + EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); + undo_redo->create_action(TTR("Create Curve in Path2D")); + undo_redo->add_do_property(node, "curve", new_curve); + undo_redo->add_undo_property(node, "curve", Ref()); + undo_redo->add_do_method(this, "_update_toolbar"); + undo_redo->add_undo_method(this, "_update_toolbar"); + undo_redo->commit_action(); +} + void Path2DEditor::_confirm_clear_points() { if (!node || node->get_curve().is_null()) { return; @@ -648,6 +675,8 @@ void Path2DEditor::_restore_curve_points(Path2D *p_path2d, const PackedVector2Ar } Path2DEditor::Path2DEditor() { + toolbar = memnew(HBoxContainer); + curve_edit = memnew(Button); curve_edit->set_theme_type_variation("FlatButton"); curve_edit->set_toggle_mode(true); @@ -655,7 +684,7 @@ Path2DEditor::Path2DEditor() { curve_edit->set_focus_mode(Control::FOCUS_NONE); 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")); curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT)); - add_child(curve_edit); + toolbar->add_child(curve_edit); curve_edit_curve = memnew(Button); curve_edit_curve->set_theme_type_variation("FlatButton"); @@ -663,7 +692,7 @@ Path2DEditor::Path2DEditor() { curve_edit_curve->set_focus_mode(Control::FOCUS_NONE); curve_edit_curve->set_tooltip_text(TTR("Select Control Points (Shift+Drag)")); curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT_CURVE)); - add_child(curve_edit_curve); + toolbar->add_child(curve_edit_curve); curve_create = memnew(Button); curve_create->set_theme_type_variation("FlatButton"); @@ -671,7 +700,7 @@ Path2DEditor::Path2DEditor() { curve_create->set_focus_mode(Control::FOCUS_NONE); curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Right Click: Delete Point")); curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CREATE)); - add_child(curve_create); + toolbar->add_child(curve_create); curve_del = memnew(Button); curve_del->set_theme_type_variation("FlatButton"); @@ -679,42 +708,48 @@ Path2DEditor::Path2DEditor() { curve_del->set_focus_mode(Control::FOCUS_NONE); curve_del->set_tooltip_text(TTR("Delete Point")); curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_DELETE)); - add_child(curve_del); + toolbar->add_child(curve_del); curve_close = memnew(Button); curve_close->set_theme_type_variation("FlatButton"); curve_close->set_focus_mode(Control::FOCUS_NONE); curve_close->set_tooltip_text(TTR("Close Curve")); curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CLOSE)); - add_child(curve_close); + toolbar->add_child(curve_close); curve_clear_points = memnew(Button); curve_clear_points->set_theme_type_variation("FlatButton"); curve_clear_points->set_focus_mode(Control::FOCUS_NONE); curve_clear_points->set_tooltip_text(TTR("Clear Points")); curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_confirm_clear_points)); - add_child(curve_clear_points); + toolbar->add_child(curve_clear_points); clear_points_dialog = memnew(ConfirmationDialog); clear_points_dialog->set_title(TTR("Please Confirm...")); clear_points_dialog->set_text(TTR("Remove all curve points?")); clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CLEAR_POINTS)); - add_child(clear_points_dialog); - - PopupMenu *menu; + toolbar->add_child(clear_points_dialog); handle_menu = memnew(MenuButton); handle_menu->set_flat(false); handle_menu->set_theme_type_variation("FlatMenuButton"); handle_menu->set_text(TTR("Options")); - add_child(handle_menu); + toolbar->add_child(handle_menu); - menu = handle_menu->get_popup(); + PopupMenu *menu = handle_menu->get_popup(); menu->add_check_item(TTR("Mirror Handle Angles")); menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle); menu->add_check_item(TTR("Mirror Handle Lengths")); menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length); menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path2DEditor::_handle_option_pressed)); + + add_child(toolbar); + + create_curve_button = memnew(Button); + create_curve_button->set_text(TTR("Create Curve")); + create_curve_button->hide(); + add_child(create_curve_button); + create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_create_curve)); } void Path2DEditorPlugin::edit(Object *p_object) { diff --git a/editor/plugins/path_2d_editor_plugin.h b/editor/plugins/path_2d_editor_plugin.h index f45b75a968c..f2f03f12f1f 100644 --- a/editor/plugins/path_2d_editor_plugin.h +++ b/editor/plugins/path_2d_editor_plugin.h @@ -58,6 +58,7 @@ class Path2DEditor : public HBoxContainer { }; Mode mode = MODE_EDIT; + HBoxContainer *toolbar = nullptr; Button *curve_clear_points = nullptr; Button *curve_close = nullptr; Button *curve_create = nullptr; @@ -66,6 +67,7 @@ class Path2DEditor : public HBoxContainer { Button *curve_edit_curve = nullptr; MenuButton *handle_menu = nullptr; + Button *create_curve_button = nullptr; ConfirmationDialog *clear_points_dialog = nullptr; bool mirror_handle_angle = true; @@ -100,7 +102,9 @@ class Path2DEditor : public HBoxContainer { void _cancel_current_action(); void _node_visibility_changed(); + void _update_toolbar(); + void _create_curve(); void _confirm_clear_points(); void _clear_curve_points(Path2D *p_path2d); void _restore_curve_points(Path2D *p_path2d, const PackedVector2Array &p_points); diff --git a/editor/plugins/path_3d_editor_plugin.cpp b/editor/plugins/path_3d_editor_plugin.cpp index 240206e1245..7ba464f4e50 100644 --- a/editor/plugins/path_3d_editor_plugin.cpp +++ b/editor/plugins/path_3d_editor_plugin.cpp @@ -652,6 +652,7 @@ void Path3DEditorPlugin::edit(Object *p_object) { if (path->get_curve().is_valid()) { path->get_curve()->emit_signal(CoreStringName(changed)); } + _update_toolbar(); } } else { Path3D *pre = path; @@ -732,6 +733,21 @@ void Path3DEditorPlugin::_handle_option_pressed(int p_option) { } } +void Path3DEditorPlugin::_create_curve() { + ERR_FAIL_NULL(path); + + Ref new_curve; + new_curve.instantiate(); + + EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); + undo_redo->create_action(TTR("Create Curve in Path3D")); + undo_redo->add_do_property(path, "curve", new_curve); + undo_redo->add_undo_property(path, "curve", Ref()); + undo_redo->add_do_method(this, "_update_toolbar"); + undo_redo->add_undo_method(this, "_update_toolbar"); + undo_redo->commit_action(); +} + void Path3DEditorPlugin::_confirm_clear_points() { if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) { return; @@ -783,6 +799,17 @@ void Path3DEditorPlugin::_update_theme() { curve_del->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveDelete"), EditorStringName(EditorIcons))); curve_close->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveClose"), EditorStringName(EditorIcons))); curve_clear_points->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Clear"), EditorStringName(EditorIcons))); + + create_curve_button->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Curve3D"), EditorStringName(EditorIcons))); +} + +void Path3DEditorPlugin::_update_toolbar() { + if (!path) { + return; + } + bool has_curve = path->get_curve().is_valid(); + toolbar->set_visible(has_curve); + create_curve_button->set_visible(!has_curve); } void Path3DEditorPlugin::_notification(int p_what) { @@ -808,6 +835,7 @@ void Path3DEditorPlugin::_notification(int p_what) { } void Path3DEditorPlugin::_bind_methods() { + ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path3DEditorPlugin::_update_toolbar); ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path3DEditorPlugin::_clear_curve_points); ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path3DEditorPlugin::_restore_curve_points); } @@ -830,65 +858,74 @@ Path3DEditorPlugin::Path3DEditorPlugin() { topmenu_bar->hide(); Node3DEditor::get_singleton()->add_control_to_menu_panel(topmenu_bar); + toolbar = memnew(HBoxContainer); + topmenu_bar->add_child(toolbar); + curve_edit = memnew(Button); curve_edit->set_theme_type_variation("FlatButton"); curve_edit->set_toggle_mode(true); curve_edit->set_focus_mode(Control::FOCUS_NONE); curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Click: Select multiple Points") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point")); - topmenu_bar->add_child(curve_edit); + toolbar->add_child(curve_edit); curve_edit_curve = memnew(Button); curve_edit_curve->set_theme_type_variation("FlatButton"); curve_edit_curve->set_toggle_mode(true); curve_edit_curve->set_focus_mode(Control::FOCUS_NONE); curve_edit_curve->set_tooltip_text(TTR("Select Control Points") + "\n" + TTR("Shift+Click: Drag out Control Points")); - topmenu_bar->add_child(curve_edit_curve); + toolbar->add_child(curve_edit_curve); curve_edit_tilt = memnew(Button); curve_edit_tilt->set_theme_type_variation("FlatButton"); curve_edit_tilt->set_toggle_mode(true); curve_edit_tilt->set_focus_mode(Control::FOCUS_NONE); curve_edit_tilt->set_tooltip_text(TTR("Select Tilt Handles")); - topmenu_bar->add_child(curve_edit_tilt); + toolbar->add_child(curve_edit_tilt); curve_create = memnew(Button); curve_create->set_theme_type_variation("FlatButton"); curve_create->set_toggle_mode(true); curve_create->set_focus_mode(Control::FOCUS_NONE); curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)")); - topmenu_bar->add_child(curve_create); + toolbar->add_child(curve_create); curve_del = memnew(Button); curve_del->set_theme_type_variation("FlatButton"); curve_del->set_toggle_mode(true); curve_del->set_focus_mode(Control::FOCUS_NONE); curve_del->set_tooltip_text(TTR("Delete Point")); - topmenu_bar->add_child(curve_del); + toolbar->add_child(curve_del); curve_close = memnew(Button); curve_close->set_theme_type_variation("FlatButton"); curve_close->set_focus_mode(Control::FOCUS_NONE); curve_close->set_tooltip_text(TTR("Close Curve")); - topmenu_bar->add_child(curve_close); + toolbar->add_child(curve_close); curve_clear_points = memnew(Button); curve_clear_points->set_theme_type_variation("FlatButton"); curve_clear_points->set_focus_mode(Control::FOCUS_NONE); curve_clear_points->set_tooltip_text(TTR("Clear Points")); curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_confirm_clear_points)); - topmenu_bar->add_child(curve_clear_points); + toolbar->add_child(curve_clear_points); clear_points_dialog = memnew(ConfirmationDialog); clear_points_dialog->set_title(TTR("Please Confirm...")); clear_points_dialog->set_text(TTR("Remove all curve points?")); clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path3DEditorPlugin::_clear_points)); - topmenu_bar->add_child(clear_points_dialog); + toolbar->add_child(clear_points_dialog); handle_menu = memnew(MenuButton); handle_menu->set_flat(false); handle_menu->set_theme_type_variation("FlatMenuButton"); handle_menu->set_text(TTR("Options")); - topmenu_bar->add_child(handle_menu); + toolbar->add_child(handle_menu); + + create_curve_button = memnew(Button); + create_curve_button->set_text(TTR("Create Curve")); + create_curve_button->hide(); + topmenu_bar->add_child(create_curve_button); + create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_create_curve)); PopupMenu *menu; menu = handle_menu->get_popup(); diff --git a/editor/plugins/path_3d_editor_plugin.h b/editor/plugins/path_3d_editor_plugin.h index ee73df1617a..09a7a46803c 100644 --- a/editor/plugins/path_3d_editor_plugin.h +++ b/editor/plugins/path_3d_editor_plugin.h @@ -113,6 +113,8 @@ class Path3DEditorPlugin : public EditorPlugin { Ref path_3d_gizmo_plugin; HBoxContainer *topmenu_bar = nullptr; + + HBoxContainer *toolbar = nullptr; Button *curve_create = nullptr; Button *curve_edit = nullptr; Button *curve_edit_curve = nullptr; @@ -122,6 +124,7 @@ class Path3DEditorPlugin : public EditorPlugin { Button *curve_clear_points = nullptr; MenuButton *handle_menu = nullptr; + Button *create_curve_button = nullptr; ConfirmationDialog *clear_points_dialog = nullptr; float disk_size = 0.8; @@ -138,14 +141,16 @@ class Path3DEditorPlugin : public EditorPlugin { Path3D *path = nullptr; void _update_theme(); + void _update_toolbar(); void _mode_changed(int p_mode); void _close_curve(); void _handle_option_pressed(int p_option); bool handle_clicked = false; - bool mirror_handle_angle; - bool mirror_handle_length; + bool mirror_handle_angle = true; + bool mirror_handle_length = true; + void _create_curve(); void _confirm_clear_points(); void _clear_points(); void _clear_curve_points();