From 8b4c4d9b2fbfa39e71bc1e4f4fcdd5377a9396e4 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sun, 3 Mar 2019 17:53:59 -0300 Subject: [PATCH] Implement a more coherent (and way less hack) way to block animation updates, fixes #24618 --- core/undo_redo.cpp | 12 ++++++++- core/undo_redo.h | 3 +++ editor/animation_track_editor.cpp | 30 +---------------------- editor/animation_track_editor.h | 9 ------- editor/animation_track_editor_plugins.cpp | 6 ----- platform/windows/os_windows.cpp | 5 ++-- 6 files changed, 17 insertions(+), 48 deletions(-) diff --git a/core/undo_redo.cpp b/core/undo_redo.cpp index 00894b41d8f..9a10e0585d0 100644 --- a/core/undo_redo.cpp +++ b/core/undo_redo.cpp @@ -239,6 +239,10 @@ void UndoRedo::_pop_history_tail() { } } +bool UndoRedo::is_commiting_action() const { + return commiting > 0; +} + void UndoRedo::commit_action() { ERR_FAIL_COND(action_level <= 0); @@ -321,10 +325,13 @@ bool UndoRedo::redo() { if ((current_action + 1) >= actions.size()) return false; //nothing to redo + + commiting++; current_action++; _process_operation_list(actions.write[current_action].do_ops.front()); version++; + commiting--; return true; } @@ -334,10 +341,11 @@ bool UndoRedo::undo() { ERR_FAIL_COND_V(action_level > 0, false); if (current_action < 0) return false; //nothing to redo + commiting++; _process_operation_list(actions.write[current_action].undo_ops.front()); current_action--; version--; - + commiting--; return true; } @@ -386,6 +394,7 @@ void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_ca UndoRedo::UndoRedo() { + commiting = 0; version = 1; action_level = 0; current_action = -1; @@ -484,6 +493,7 @@ void UndoRedo::_bind_methods() { ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE)); ClassDB::bind_method(D_METHOD("commit_action"), &UndoRedo::commit_action); + ClassDB::bind_method(D_METHOD("is_commiting_action"), &UndoRedo::is_commiting_action); //ClassDB::bind_method(D_METHOD("add_do_method","p_object", "p_method", "VARIANT_ARG_LIST"),&UndoRedo::add_do_method); //ClassDB::bind_method(D_METHOD("add_undo_method","p_object", "p_method", "VARIANT_ARG_LIST"),&UndoRedo::add_undo_method); diff --git a/core/undo_redo.h b/core/undo_redo.h index 4ee64dbfcfb..b626149ce62 100644 --- a/core/undo_redo.h +++ b/core/undo_redo.h @@ -94,6 +94,8 @@ private: MethodNotifyCallback method_callback; PropertyNotifyCallback property_callback; + int commiting; + protected: static void _bind_methods(); @@ -107,6 +109,7 @@ public: void add_do_reference(Object *p_object); void add_undo_reference(Object *p_object); + bool is_commiting_action() const; void commit_action(); bool redo(); diff --git a/editor/animation_track_editor.cpp b/editor/animation_track_editor.cpp index 6b45ca2cae5..6b863a162c0 100644 --- a/editor/animation_track_editor.cpp +++ b/editor/animation_track_editor.cpp @@ -692,12 +692,10 @@ void AnimationTimelineEdit::_anim_length_changed(double p_new_len) { p_new_len = MAX(0.001, p_new_len); editing = true; - *block_animation_update_ptr = true; undo_redo->create_action(TTR("Change Animation Length")); undo_redo->add_do_method(animation.ptr(), "set_length", p_new_len); undo_redo->add_undo_method(animation.ptr(), "set_length", animation->get_length()); undo_redo->commit_action(); - *block_animation_update_ptr = false; editing = false; update(); @@ -706,12 +704,10 @@ void AnimationTimelineEdit::_anim_length_changed(double p_new_len) { void AnimationTimelineEdit::_anim_loop_pressed() { - *block_animation_update_ptr = true; undo_redo->create_action(TTR("Change Animation Loop")); undo_redo->add_do_method(animation.ptr(), "set_loop", loop->is_pressed()); undo_redo->add_undo_method(animation.ptr(), "set_loop", animation->has_loop()); undo_redo->commit_action(); - *block_animation_update_ptr = false; } int AnimationTimelineEdit::get_buttons_width() const { @@ -936,10 +932,6 @@ Size2 AnimationTimelineEdit::get_minimum_size() const { return ms; } -void AnimationTimelineEdit::set_block_animation_update_ptr(bool *p_block_ptr) { - block_animation_update_ptr = p_block_ptr; -} - void AnimationTimelineEdit::set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; } @@ -1080,7 +1072,6 @@ void AnimationTimelineEdit::_bind_methods() { AnimationTimelineEdit::AnimationTimelineEdit() { - block_animation_update_ptr = NULL; editing = false; name_limit = 150; zoom = NULL; @@ -1884,12 +1875,10 @@ void AnimationTrackEdit::_gui_input(const Ref &p_event) { Point2 pos = mb->get_position(); if (check_rect.has_point(pos)) { - *block_animation_update_ptr = true; undo_redo->create_action(TTR("Toggle Track Enabled")); undo_redo->add_do_method(animation.ptr(), "track_set_enabled", track, !animation->track_is_enabled(track)); undo_redo->add_undo_method(animation.ptr(), "track_set_enabled", track, animation->track_is_enabled(track)); undo_redo->commit_action(); - *block_animation_update_ptr = false; update(); accept_event(); } @@ -2186,12 +2175,10 @@ void AnimationTrackEdit::_menu_selected(int p_index) { case MENU_CALL_MODE_CAPTURE: { Animation::UpdateMode update_mode = Animation::UpdateMode(p_index); - *block_animation_update_ptr = true; undo_redo->create_action(TTR("Change Animation Update Mode")); undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", track, update_mode); undo_redo->add_undo_method(animation.ptr(), "value_track_set_update_mode", track, animation->value_track_get_update_mode(track)); undo_redo->commit_action(); - *block_animation_update_ptr = false; update(); } break; @@ -2200,24 +2187,20 @@ void AnimationTrackEdit::_menu_selected(int p_index) { case MENU_INTERPOLATION_CUBIC: { Animation::InterpolationType interp_mode = Animation::InterpolationType(p_index - MENU_INTERPOLATION_NEAREST); - *block_animation_update_ptr = true; undo_redo->create_action(TTR("Change Animation Interpolation Mode")); undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", track, interp_mode); undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_type", track, animation->track_get_interpolation_type(track)); undo_redo->commit_action(); - *block_animation_update_ptr = false; update(); } break; case MENU_LOOP_WRAP: case MENU_LOOP_CLAMP: { bool loop_wrap = p_index == MENU_LOOP_WRAP; - *block_animation_update_ptr = true; undo_redo->create_action(TTR("Change Animation Loop Mode")); undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", track, loop_wrap); undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_loop_wrap", track, animation->track_get_interpolation_loop_wrap(track)); undo_redo->commit_action(); - *block_animation_update_ptr = false; update(); } break; @@ -2235,10 +2218,6 @@ void AnimationTrackEdit::_menu_selected(int p_index) { } } -void AnimationTrackEdit::set_block_animation_update_ptr(bool *p_block_ptr) { - block_animation_update_ptr = p_block_ptr; -} - void AnimationTrackEdit::cancel_drop() { if (dropping_at != 0) { dropping_at = 0; @@ -2301,7 +2280,6 @@ AnimationTrackEdit::AnimationTrackEdit() { root = NULL; path = NULL; menu = NULL; - block_animation_update_ptr = NULL; clicking_on_name = false; dropping_at = 0; @@ -3384,7 +3362,6 @@ void AnimationTrackEditor::_update_tracks() { track_edit->set_undo_redo(undo_redo); track_edit->set_timeline(timeline); - track_edit->set_block_animation_update_ptr(&block_animation_update); track_edit->set_root(root); track_edit->set_animation_and_track(animation, i); track_edit->set_play_position(timeline->get_play_position()); @@ -3425,7 +3402,7 @@ void AnimationTrackEditor::_animation_changed() { timeline->update(); timeline->update_values(); - if (block_animation_update) { + if (undo_redo->is_commiting_action()) { for (int i = 0; i < track_edits.size(); i++) { track_edits[i]->update(); } @@ -4075,9 +4052,7 @@ void AnimationTrackEditor::_move_selection_commit() { undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos); } - block_animation_update = true; //animation will change and this is triggered from a signal, so block updates undo_redo->commit_action(); - block_animation_update = false; moving_selection = false; for (int i = 0; i < track_edits.size(); i++) { @@ -4778,7 +4753,6 @@ void AnimationTrackEditor::_bind_methods() { AnimationTrackEditor::AnimationTrackEditor() { root = NULL; - block_animation_update = false; undo_redo = EditorNode::get_singleton()->get_undo_redo(); @@ -4796,7 +4770,6 @@ AnimationTrackEditor::AnimationTrackEditor() { timeline_vbox->add_constant_override("separation", 0); timeline = memnew(AnimationTimelineEdit); - timeline->set_block_animation_update_ptr(&block_animation_update); timeline->set_undo_redo(undo_redo); timeline_vbox->add_child(timeline); timeline->connect("timeline_changed", this, "_timeline_changed"); @@ -4815,7 +4788,6 @@ AnimationTrackEditor::AnimationTrackEditor() { bezier_edit = memnew(AnimationBezierTrackEdit); timeline_vbox->add_child(bezier_edit); - bezier_edit->set_block_animation_update_ptr(&block_animation_update); bezier_edit->set_undo_redo(undo_redo); bezier_edit->set_editor(this); bezier_edit->set_timeline(timeline); diff --git a/editor/animation_track_editor.h b/editor/animation_track_editor.h index 47058839ba8..4ad8b6fd682 100644 --- a/editor/animation_track_editor.h +++ b/editor/animation_track_editor.h @@ -76,7 +76,6 @@ class AnimationTimelineEdit : public Range { Rect2 hsize_rect; bool editing; - bool *block_animation_update_ptr; //used to block all tracks re-gen (speed up) bool panning_timeline; float panning_timeline_from; @@ -104,7 +103,6 @@ public: void set_zoom(Range *p_zoom); Range *get_zoom() const { return zoom; } void set_undo_redo(UndoRedo *p_undo_redo); - void set_block_animation_update_ptr(bool *p_block_ptr); void set_play_position(float p_pos); float get_play_position() const; @@ -170,8 +168,6 @@ class AnimationTrackEdit : public Control { void _menu_selected(int p_index); - bool *block_animation_update_ptr; //used to block all tracks re-gen (speed up) - void _path_entered(const String &p_text); void _play_position_draw(); mutable int dropping_at; @@ -216,7 +212,6 @@ public: AnimationTimelineEdit *get_timeline() const { return timeline; } AnimationTrackEditor *get_editor() const { return editor; } UndoRedo *get_undo_redo() const { return undo_redo; } - bool *get_block_animation_update_ptr() { return block_animation_update_ptr; } void set_animation_and_track(const Ref &p_animation, int p_track); virtual Size2 get_minimum_size() const; @@ -226,8 +221,6 @@ public: void set_editor(AnimationTrackEditor *p_editor); void set_root(Node *p_root); - void set_block_animation_update_ptr(bool *p_block_ptr); - void set_play_position(float p_pos); void update_play_position(); void cancel_drop(); @@ -313,8 +306,6 @@ class AnimationTrackEditor : public VBoxContainer { Vector track_edits; Vector groups; - bool block_animation_update; - int _get_track_selected(); void _animation_changed(); void _update_tracks(); diff --git a/editor/animation_track_editor_plugins.cpp b/editor/animation_track_editor_plugins.cpp index 399d561e28c..baf417fed7a 100644 --- a/editor/animation_track_editor_plugins.cpp +++ b/editor/animation_track_editor_plugins.cpp @@ -1004,12 +1004,10 @@ void AnimationTrackEditTypeAudio::drop_data(const Point2 &p_point, const Variant ofs += 0.001; } - *get_block_animation_update_ptr() = true; get_undo_redo()->create_action(TTR("Add Audio Track Clip")); get_undo_redo()->add_do_method(get_animation().ptr(), "audio_track_insert_key", get_track(), ofs, stream); get_undo_redo()->add_undo_method(get_animation().ptr(), "track_remove_key_at_position", get_track(), ofs); get_undo_redo()->commit_action(); - *get_block_animation_update_ptr() = false; update(); return; @@ -1098,21 +1096,17 @@ void AnimationTrackEditTypeAudio::_gui_input(const Ref &p_event) { float ofs_local = -len_resizing_rel / get_timeline()->get_zoom_scale(); if (len_resizing_start) { float prev_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), len_resizing_index); - *get_block_animation_update_ptr() = true; get_undo_redo()->create_action(TTR("Change Audio Track Clip Start Offset")); get_undo_redo()->add_do_method(get_animation().ptr(), "audio_track_set_key_start_offset", get_track(), len_resizing_index, prev_ofs + ofs_local); get_undo_redo()->add_undo_method(get_animation().ptr(), "audio_track_set_key_start_offset", get_track(), len_resizing_index, prev_ofs); get_undo_redo()->commit_action(); - *get_block_animation_update_ptr() = false; } else { float prev_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), len_resizing_index); - *get_block_animation_update_ptr() = true; get_undo_redo()->create_action(TTR("Change Audio Track Clip End Offset")); get_undo_redo()->add_do_method(get_animation().ptr(), "audio_track_set_key_end_offset", get_track(), len_resizing_index, prev_ofs + ofs_local); get_undo_redo()->add_undo_method(get_animation().ptr(), "audio_track_set_key_end_offset", get_track(), len_resizing_index, prev_ofs); get_undo_redo()->commit_action(); - *get_block_animation_update_ptr() = false; } len_resizing = false; diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index a213e495b49..bace65e7721 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -308,7 +308,6 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) } }; - switch (uMsg) // Check For Windows Messages { case WM_SETFOCUS: { @@ -3004,9 +3003,9 @@ bool OS_Windows::is_disable_crash_handler() const { void OS_Windows::process_and_drop_events() { - drop_events=true; + drop_events = true; process_events(); - drop_events=false; + drop_events = false; } Error OS_Windows::move_to_trash(const String &p_path) {