Simplify right-click track insertion code

This commit is contained in:
Pedro J. Estébanez 2023-08-21 19:51:54 +02:00
parent 5708a3a02e
commit 9ba984e2c1
2 changed files with 48 additions and 74 deletions

View File

@ -4422,7 +4422,7 @@ AnimationTrackEditor::TrackIndices AnimationTrackEditor::_confirm_insert(InsertD
}
}
float time = timeline->get_play_position();
float time = p_id.time == FLT_MAX ? timeline->get_play_position() : p_id.time;
Variant value;
switch (p_id.type) {
@ -4430,7 +4430,9 @@ AnimationTrackEditor::TrackIndices AnimationTrackEditor::_confirm_insert(InsertD
case Animation::TYPE_ROTATION_3D:
case Animation::TYPE_SCALE_3D:
case Animation::TYPE_BLEND_SHAPE:
case Animation::TYPE_VALUE: {
case Animation::TYPE_VALUE:
case Animation::TYPE_AUDIO:
case Animation::TYPE_ANIMATION: {
value = p_id.value;
} break;
@ -4446,10 +4448,9 @@ AnimationTrackEditor::TrackIndices AnimationTrackEditor::_confirm_insert(InsertD
bezier_edit_icon->set_disabled(false);
} break;
case Animation::TYPE_ANIMATION: {
value = p_id.value;
} break;
default: {
// Other track types shouldn't use this code path.
DEV_ASSERT(false);
}
}
@ -5165,87 +5166,68 @@ void AnimationTrackEditor::_insert_key_from_track(float p_ofs, int p_track) {
p_ofs += 0.0001;
}
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
Node *node = root->get_node_or_null(animation->track_get_path(p_track));
if (!node) {
EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a key."));
return;
}
// Special handling for this one.
if (animation->track_get_type(p_track) == Animation::TYPE_METHOD) {
method_selector->select_method_from_instance(node);
insert_key_from_track_call_ofs = p_ofs;
insert_key_from_track_call_track = p_track;
return;
}
InsertData id;
id.path = animation->track_get_path(p_track);
id.advance = false;
id.track_idx = p_track;
id.type = animation->track_get_type(p_track);
// TRANSLATORS: This describes the target of new animation track, will be inserted into another string.
id.query = vformat(TTR("node '%s'"), node->get_name());
id.time = p_ofs;
// id.value is filled in each case handled below.
switch (animation->track_get_type(p_track)) {
case Animation::TYPE_POSITION_3D: {
if (!root->has_node(animation->track_get_path(p_track))) {
EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a key."));
return;
}
Node3D *base = Object::cast_to<Node3D>(root->get_node(animation->track_get_path(p_track)));
Node3D *base = Object::cast_to<Node3D>(node);
if (!base) {
EditorNode::get_singleton()->show_warning(TTR("Track is not of type Node3D, can't insert key"));
return;
}
Vector3 pos = base->get_position();
undo_redo->create_action(TTR("Add Position Key"));
undo_redo->add_do_method(animation.ptr(), "position_track_insert_key", p_track, p_ofs, pos);
undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", p_track, p_ofs);
undo_redo->commit_action();
id.value = base->get_position();
} break;
case Animation::TYPE_ROTATION_3D: {
if (!root->has_node(animation->track_get_path(p_track))) {
EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a key."));
return;
}
Node3D *base = Object::cast_to<Node3D>(root->get_node(animation->track_get_path(p_track)));
Node3D *base = Object::cast_to<Node3D>(node);
if (!base) {
EditorNode::get_singleton()->show_warning(TTR("Track is not of type Node3D, can't insert key"));
return;
}
Quaternion rot = base->get_transform().basis.operator Quaternion();
undo_redo->create_action(TTR("Add Rotation Key"));
undo_redo->add_do_method(animation.ptr(), "rotation_track_insert_key", p_track, p_ofs, rot);
undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", p_track, p_ofs);
undo_redo->commit_action();
id.value = base->get_transform().basis.operator Quaternion();
} break;
case Animation::TYPE_SCALE_3D: {
if (!root->has_node(animation->track_get_path(p_track))) {
EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a key."));
return;
}
Node3D *base = Object::cast_to<Node3D>(root->get_node(animation->track_get_path(p_track)));
Node3D *base = Object::cast_to<Node3D>(node);
if (!base) {
EditorNode::get_singleton()->show_warning(TTR("Track is not of type Node3D, can't insert key"));
return;
}
undo_redo->create_action(TTR("Add Scale Key"));
undo_redo->add_do_method(animation.ptr(), "scale_track_insert_key", p_track, p_ofs, base->get_scale());
undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", p_track, p_ofs);
undo_redo->commit_action();
id.value = base->get_scale();
} break;
case Animation::TYPE_BLEND_SHAPE:
case Animation::TYPE_VALUE: {
NodePath bp;
Variant value;
_find_hint_for_track(p_track, bp, &value);
undo_redo->create_action(TTR("Add Track Key"));
undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_track, p_ofs, value);
undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", p_track, p_ofs);
undo_redo->commit_action();
_find_hint_for_track(p_track, bp, &id.value);
} break;
case Animation::TYPE_METHOD: {
if (!root->has_node(animation->track_get_path(p_track))) {
EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a method key."));
return;
}
Node *base = root->get_node_or_null(animation->track_get_path(p_track));
ERR_FAIL_NULL(base);
@ -5267,12 +5249,7 @@ void AnimationTrackEditor::_insert_key_from_track(float p_ofs, int p_track) {
arr[3] = 0.25;
arr[4] = 0;
undo_redo->create_action(TTR("Add Track Key"));
undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_track, p_ofs, arr);
undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", p_track, p_ofs);
undo_redo->commit_action();
id.value = arr;
} break;
case Animation::TYPE_AUDIO: {
Dictionary ak;
@ -5280,22 +5257,18 @@ void AnimationTrackEditor::_insert_key_from_track(float p_ofs, int p_track) {
ak["start_offset"] = 0;
ak["end_offset"] = 0;
undo_redo->create_action(TTR("Add Track Key"));
undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_track, p_ofs, ak);
undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", p_track, p_ofs);
undo_redo->commit_action();
id.value = ak;
} break;
case Animation::TYPE_ANIMATION: {
StringName anim = "[stop]";
undo_redo->create_action(TTR("Add Track Key"));
undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_track, p_ofs, anim);
undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", p_track, p_ofs);
undo_redo->commit_action();
id.value = StringName("[stop]");
} break;
default: {
// All track types should be handled by now.
DEV_ASSERT(false);
}
}
_query_insert(id);
}
void AnimationTrackEditor::_add_method_key(const String &p_method) {

View File

@ -464,6 +464,7 @@ class AnimationTrackEditor : public VBoxContainer {
Animation::TrackType type;
NodePath path;
int track_idx = 0;
float time = FLT_MAX; // Defaults to current timeline position.
Variant value;
String query;
bool advance = false;