Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify right-click track insertion code #80860

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 47 additions & 74 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4422,15 +4422,17 @@ 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) {
case Animation::TYPE_POSITION_3D:
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;
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);

Expand All @@ -5267,35 +5249,26 @@ 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;
ak["stream"] = Ref<Resource>();
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) {
Expand Down
1 change: 1 addition & 0 deletions editor/animation_track_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading