summaryrefslogtreecommitdiff
path: root/editor
diff options
context:
space:
mode:
Diffstat (limited to 'editor')
-rw-r--r--editor/plugins/curve_editor_plugin.cpp82
-rw-r--r--editor/plugins/curve_editor_plugin.h2
2 files changed, 53 insertions, 31 deletions
diff --git a/editor/plugins/curve_editor_plugin.cpp b/editor/plugins/curve_editor_plugin.cpp
index 6b0da50927..d729bb7b76 100644
--- a/editor/plugins/curve_editor_plugin.cpp
+++ b/editor/plugins/curve_editor_plugin.cpp
@@ -133,7 +133,16 @@ void CurveEditor::on_gui_input(const Ref<InputEvent> &p_event) {
if (!mb.is_pressed() && _dragging && mb.get_button_index() == BUTTON_LEFT) {
_dragging = false;
if (_has_undo_data) {
- push_undo(_undo_data);
+
+ UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
+
+ ur.create_action(_selected_tangent == TANGENT_NONE ? TTR("Modify Curve Point") : TTR("Modify Curve Tangent"));
+ ur.add_do_method(*_curve_ref, "_set_data", _curve_ref->get_data());
+ ur.add_undo_method(*_curve_ref, "_set_data", _undo_data);
+ // Note: this will trigger one more "changed" signal even if nothing changes,
+ // but it's ok since it would have fired every frame during the drag anyways
+ ur.commit_action();
+
_has_undo_data = false;
}
}
@@ -152,7 +161,8 @@ void CurveEditor::on_gui_input(const Ref<InputEvent> &p_event) {
if (_selected_point != -1) {
if (!_has_undo_data) {
- // Save curve state before dragging points
+ // Save full curve state before dragging points,
+ // because this operation can modify their order
_undo_data = curve.get_data();
_has_undo_data = true;
}
@@ -175,8 +185,6 @@ void CurveEditor::on_gui_input(const Ref<InputEvent> &p_event) {
curve.set_point_value(_selected_point, point_pos.y);
- //auto_calculate_tangents(i);
-
} else {
// Drag tangent
@@ -275,7 +283,13 @@ void CurveEditor::on_preset_item_selected(int preset_id) {
break;
}
- push_undo(previous_data);
+ UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
+ ur.create_action(TTR("Load Curve Preset"));
+
+ ur.add_do_method(&curve, "_set_data", curve.get_data());
+ ur.add_undo_method(&curve, "_set_data", previous_data);
+
+ ur.commit_action();
}
void CurveEditor::_curve_changed() {
@@ -397,7 +411,8 @@ CurveEditor::TangentIndex CurveEditor::get_tangent_at(Vector2 pos) const {
void CurveEditor::add_point(Vector2 pos) {
ERR_FAIL_COND(_curve_ref.is_null());
- Array prev_data = _curve_ref->get_data();
+ UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
+ ur.create_action(TTR("Remove Curve Point"));
Vector2 point_pos = get_world_pos(pos);
if (point_pos.y < 0.0)
@@ -405,41 +420,64 @@ void CurveEditor::add_point(Vector2 pos) {
else if (point_pos.y > 1.0)
point_pos.y = 1.0;
- _curve_ref->add_point(point_pos);
+ // Small trick to get the point index to feed the undo method
+ int i = _curve_ref->add_point(point_pos);
+ _curve_ref->remove_point(i);
+
+ ur.add_do_method(*_curve_ref, "add_point", point_pos);
+ ur.add_undo_method(*_curve_ref, "remove_point", i);
- push_undo(prev_data);
+ ur.commit_action();
}
void CurveEditor::remove_point(int index) {
ERR_FAIL_COND(_curve_ref.is_null());
- Array prev_data = _curve_ref->get_data();
+ UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
+ ur.create_action(TTR("Remove Curve Point"));
+
+ Curve::Point p = _curve_ref->get_point(index);
- _curve_ref->remove_point(index);
+ ur.add_do_method(*_curve_ref, "remove_point", index);
+ ur.add_undo_method(*_curve_ref, "add_point", p.pos, p.left_tangent, p.right_tangent, p.left_mode, p.right_mode);
if (index == _selected_point)
set_selected_point(-1);
- push_undo(prev_data);
+ ur.commit_action();
}
void CurveEditor::toggle_linear(TangentIndex tangent) {
ERR_FAIL_COND(_curve_ref.is_null());
- Array prev_data = _curve_ref->get_data();
+ UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
+ ur.create_action(TTR("Toggle Curve Linear Tangent"));
if (tangent == TANGENT_NONE)
tangent = _selected_tangent;
if (tangent == TANGENT_LEFT) {
+
bool is_linear = _curve_ref->get_point_left_mode(_selected_point) == Curve::TANGENT_LINEAR;
- _curve_ref->set_point_left_mode(_selected_point, is_linear ? Curve::TANGENT_FREE : Curve::TANGENT_LINEAR);
+
+ Curve::TangentMode prev_mode = _curve_ref->get_point_left_mode(_selected_point);
+ Curve::TangentMode mode = is_linear ? Curve::TANGENT_FREE : Curve::TANGENT_LINEAR;
+
+ ur.add_do_method(*_curve_ref, "set_point_left_mode", _selected_point, mode);
+ ur.add_undo_method(*_curve_ref, "set_point_left_mode", _selected_point, prev_mode);
+
} else {
+
bool is_linear = _curve_ref->get_point_right_mode(_selected_point) == Curve::TANGENT_LINEAR;
- _curve_ref->set_point_right_mode(_selected_point, is_linear ? Curve::TANGENT_FREE : Curve::TANGENT_LINEAR);
+
+ Curve::TangentMode prev_mode = _curve_ref->get_point_right_mode(_selected_point);
+ Curve::TangentMode mode = is_linear ? Curve::TANGENT_FREE : Curve::TANGENT_LINEAR;
+
+ ur.add_do_method(*_curve_ref, "set_point_right_mode", _selected_point, mode);
+ ur.add_undo_method(*_curve_ref, "set_point_right_mode", _selected_point, prev_mode);
}
- push_undo(prev_data);
+ ur.commit_action();
}
void CurveEditor::set_selected_point(int index) {
@@ -456,20 +494,6 @@ void CurveEditor::set_hover_point_index(int index) {
}
}
-void CurveEditor::push_undo(Array previous_curve_data) {
- UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
-
- ur->create_action(TTR("Modify Curve"));
- ur->add_do_method(*_curve_ref, "_set_data", _curve_ref->get_data());
- ur->add_undo_method(*_curve_ref, "_set_data", previous_curve_data);
-
- // This boolean is to prevent commit_action from executing the do method,
- // because at this point it's already done, there is no point in doing it twice
- _curve_ref->_disable_set_data = true;
- ur->commit_action();
- _curve_ref->_disable_set_data = false;
-}
-
void CurveEditor::update_view_transform() {
Vector2 control_size = get_size();
const real_t margin = 24;
diff --git a/editor/plugins/curve_editor_plugin.h b/editor/plugins/curve_editor_plugin.h
index 39d9b84d88..a4952dfb28 100644
--- a/editor/plugins/curve_editor_plugin.h
+++ b/editor/plugins/curve_editor_plugin.h
@@ -88,7 +88,6 @@ private:
void toggle_linear(TangentIndex tangent = TANGENT_NONE);
void set_selected_point(int index);
void set_hover_point_index(int index);
- void push_undo(Array previous_curve_data);
void update_view_transform();
Vector2 get_tangent_view_pos(int i, TangentIndex tangent) const;
@@ -108,7 +107,6 @@ private:
Array _undo_data;
bool _has_undo_data;
- bool _undo_no_commit;
Vector2 _context_click_pos;
int _selected_point;