Make animation editor change tracks positions instead of swapping them

This commit is contained in:
Michael Alexsander Silva Dias 2019-05-25 13:01:44 -03:00
parent 25e36f3540
commit fad0338beb
5 changed files with 99 additions and 23 deletions

View file

@ -450,6 +450,17 @@
Move a track down.
</description>
</method>
<method name="track_move_to">
<return type="void">
</return>
<argument index="0" name="idx" type="int">
</argument>
<argument index="1" name="to_idx" type="int">
</argument>
<description>
Changes the index position of track [code]idx[/code] to the one defined in [code]to_idx[/code].
</description>
</method>
<method name="track_move_up">
<return type="void">
</return>
@ -571,6 +582,7 @@
<argument index="1" name="with_idx" type="int">
</argument>
<description>
Swaps the track [code]idx[/code]'s index position with the track [code]with_idx[/code].
</description>
</method>
<method name="transform_track_insert_key">

View file

@ -1213,7 +1213,8 @@ void AnimationTrackEdit::_notification(int p_what) {
if (has_focus()) {
Color accent = get_color("accent_color", "Editor");
accent.a *= 0.7;
draw_rect(Rect2(Point2(), get_size()), accent, false);
// Offside so the horizontal sides aren't cutoff.
draw_rect(Rect2(Point2(1, 0), get_size() - Size2(1, 0)), accent, false);
}
Ref<Font> font = get_font("font", "Label");
@ -2185,6 +2186,9 @@ Variant AnimationTrackEdit::get_drag_data(const Point2 &p_point) {
Dictionary drag_data;
drag_data["type"] = "animation_track";
String base_path = animation->track_get_path(track);
base_path = base_path.get_slice(":", 0); // Remove sub-path.
drag_data["group"] = base_path;
drag_data["index"] = track;
ToolButton *tb = memnew(ToolButton);
@ -2205,8 +2209,18 @@ bool AnimationTrackEdit::can_drop_data(const Point2 &p_point, const Variant &p_d
}
String type = d["type"];
if (type != "animation_track")
if (type != "animation_track") {
return false;
}
// Don't allow moving tracks outside their groups.
if (get_editor()->is_grouping_tracks()) {
String base_path = animation->track_get_path(track);
base_path = base_path.get_slice(":", 0); // Remove sub-path.
if (d["group"] != base_path) {
return false;
}
}
if (p_point.y < get_size().height / 2) {
dropping_at = -1;
@ -2227,8 +2241,18 @@ void AnimationTrackEdit::drop_data(const Point2 &p_point, const Variant &p_data)
}
String type = d["type"];
if (type != "animation_track")
if (type != "animation_track") {
return;
}
// Don't allow moving tracks outside their groups.
if (get_editor()->is_grouping_tracks()) {
String base_path = animation->track_get_path(track);
base_path = base_path.get_slice(":", 0); // Remove sub-path.
if (d["group"] != base_path) {
return;
}
}
int from_track = d["index"];
@ -2683,6 +2707,13 @@ void AnimationTrackEditor::_track_remove_request(int p_track) {
}
}
void AnimationTrackEditor::_track_grab_focus(int p_track) {
// Don't steal focus if not working with the track editor.
if (Object::cast_to<AnimationTrackEdit>(get_focus_owner()))
track_edits[p_track]->grab_focus();
}
void AnimationTrackEditor::set_anim_pos(float p_pos) {
timeline->set_play_position(p_pos);
@ -3447,7 +3478,7 @@ void AnimationTrackEditor::_update_tracks() {
if (use_grouping) {
String base_path = animation->track_get_path(i);
base_path = base_path.get_slice(":", 0); // remove subpath
base_path = base_path.get_slice(":", 0); // Remove sub-path.
if (!group_sort.has(base_path)) {
AnimationTrackEditGroup *g = memnew(AnimationTrackEditGroup);
@ -3664,17 +3695,18 @@ void AnimationTrackEditor::_update_length(double p_new_len) {
}
void AnimationTrackEditor::_dropped_track(int p_from_track, int p_to_track) {
if (p_to_track >= track_edits.size()) {
p_to_track = track_edits.size() - 1;
}
if (p_from_track == p_to_track)
if (p_from_track == p_to_track || p_from_track == p_to_track - 1) {
return;
}
_clear_selection();
undo_redo->create_action(TTR("Rearrange Tracks"));
undo_redo->add_do_method(animation.ptr(), "track_swap", p_from_track, p_to_track);
undo_redo->add_undo_method(animation.ptr(), "track_swap", p_to_track, p_from_track);
undo_redo->add_do_method(animation.ptr(), "track_move_to", p_from_track, p_to_track);
// Take into account that the position of the tracks that come after the one removed will change.
int to_track_real = p_to_track > p_from_track ? p_to_track - 1 : p_to_track;
undo_redo->add_undo_method(animation.ptr(), "track_move_to", to_track_real, p_to_track > p_from_track ? p_from_track : p_from_track + 1);
undo_redo->add_do_method(this, "_track_grab_focus", to_track_real);
undo_redo->add_undo_method(this, "_track_grab_focus", p_from_track);
undo_redo->commit_action();
}
@ -4876,10 +4908,20 @@ void AnimationTrackEditor::_cleanup_animation(Ref<Animation> p_animation) {
}
void AnimationTrackEditor::_view_group_toggle() {
_update_tracks();
view_group->set_icon(get_icon(view_group->is_pressed() ? "AnimationTrackList" : "AnimationTrackGroup", "EditorIcons"));
}
bool AnimationTrackEditor::is_grouping_tracks() {
if (!view_group) {
return false;
}
return !view_group->is_pressed();
}
void AnimationTrackEditor::_selection_changed() {
if (selected_filter->is_pressed()) {
@ -4926,6 +4968,7 @@ void AnimationTrackEditor::_bind_methods() {
ClassDB::bind_method("_animation_update", &AnimationTrackEditor::_animation_update);
ClassDB::bind_method("_timeline_changed", &AnimationTrackEditor::_timeline_changed);
ClassDB::bind_method("_track_remove_request", &AnimationTrackEditor::_track_remove_request);
ClassDB::bind_method("_track_grab_focus", &AnimationTrackEditor::_track_grab_focus);
ClassDB::bind_method("_name_limit_changed", &AnimationTrackEditor::_name_limit_changed);
ClassDB::bind_method("_update_scroll", &AnimationTrackEditor::_update_scroll);
ClassDB::bind_method("_update_tracks", &AnimationTrackEditor::_update_tracks);
@ -5024,7 +5067,6 @@ AnimationTrackEditor::AnimationTrackEditor() {
scroll->set_enable_v_scroll(true);
track_vbox->add_constant_override("separation", 0);
//timeline_vbox->add_child(memnew(HSeparator));
HBoxContainer *bottom_hb = memnew(HBoxContainer);
add_child(bottom_hb);

View file

@ -325,6 +325,7 @@ class AnimationTrackEditor : public VBoxContainer {
void _name_limit_changed();
void _timeline_changed(float p_new_pos, bool p_drag);
void _track_remove_request(int p_track);
void _track_grab_focus(int p_track);
UndoRedo *undo_redo;
@ -513,6 +514,7 @@ public:
float get_moving_selection_offset() const;
bool is_snap_enabled();
float snap_time(float p_value);
bool is_grouping_tracks();
MenuButton *get_edit_menu();
AnimationTrackEditor();

View file

@ -2559,17 +2559,6 @@ bool Animation::has_loop() const {
return loop;
}
void Animation::track_move_up(int p_track) {
if (p_track >= 0 && p_track < (tracks.size() - 1)) {
SWAP(tracks.write[p_track], tracks.write[p_track + 1]);
}
emit_changed();
emit_signal(SceneStringNames::get_singleton()->tracks_changed);
}
void Animation::track_set_imported(int p_track, bool p_imported) {
ERR_FAIL_INDEX(p_track, tracks.size());
@ -2595,12 +2584,40 @@ bool Animation::track_is_enabled(int p_track) const {
return tracks[p_track]->enabled;
}
void Animation::track_move_up(int p_track) {
if (p_track >= 0 && p_track < (tracks.size() - 1)) {
SWAP(tracks.write[p_track], tracks.write[p_track + 1]);
}
emit_changed();
emit_signal(SceneStringNames::get_singleton()->tracks_changed);
}
void Animation::track_move_down(int p_track) {
if (p_track > 0 && p_track < tracks.size()) {
SWAP(tracks.write[p_track], tracks.write[p_track - 1]);
}
emit_changed();
emit_signal(SceneStringNames::get_singleton()->tracks_changed);
}
void Animation::track_move_to(int p_track, int p_to_index) {
ERR_FAIL_INDEX(p_track, tracks.size());
ERR_FAIL_INDEX(p_to_index, tracks.size() + 1);
if (p_track == p_to_index || p_track == p_to_index - 1)
return;
Track *track = tracks.get(p_track);
tracks.remove(p_track);
// Take into account that the position of the tracks that come after the one removed will change.
tracks.insert(p_to_index > p_track ? p_to_index - 1 : p_to_index, track);
emit_changed();
emit_signal(SceneStringNames::get_singleton()->tracks_changed);
}
@ -2612,6 +2629,7 @@ void Animation::track_swap(int p_track, int p_with_track) {
if (p_track == p_with_track)
return;
SWAP(tracks.write[p_track], tracks.write[p_with_track]);
emit_changed();
emit_signal(SceneStringNames::get_singleton()->tracks_changed);
}
@ -2656,6 +2674,7 @@ void Animation::_bind_methods() {
ClassDB::bind_method(D_METHOD("track_move_up", "idx"), &Animation::track_move_up);
ClassDB::bind_method(D_METHOD("track_move_down", "idx"), &Animation::track_move_down);
ClassDB::bind_method(D_METHOD("track_move_to", "idx", "to_idx"), &Animation::track_move_to);
ClassDB::bind_method(D_METHOD("track_swap", "idx", "with_idx"), &Animation::track_swap);
ClassDB::bind_method(D_METHOD("track_set_imported", "idx", "imported"), &Animation::track_set_imported);

View file

@ -293,6 +293,7 @@ public:
void track_move_up(int p_track);
void track_move_down(int p_track);
void track_move_to(int p_track, int p_to_index);
void track_swap(int p_track, int p_with_track);
void track_set_imported(int p_track, bool p_imported);