Put frame duration as a member of the Frame class

Instead of having a frame_duration[] array in the Project class. This makes the code much more readable and understandable, using less lines of code and, by avoiding an extra array, we also avoid potential out-of-bound array crashes.

The functionality for the user has not changed, and by testing so far I didn't find any issues.
This commit is contained in:
Manolis Papadeas 2020-12-16 22:54:08 +02:00
parent 329f4edc5c
commit 05c9ef70d4
9 changed files with 40 additions and 66 deletions

View file

@ -9,7 +9,7 @@ func _draw() -> void:
if frame >= current_project.frames.size():
frame = current_project.current_frame
$AnimationTimer.wait_time = current_project.frame_duration[frame] * (1 / Global.animation_timeline.fps)
$AnimationTimer.wait_time = current_project.frames[frame].duration * (1 / Global.animation_timeline.fps)
if animation_timer.is_stopped():
frame = current_project.current_frame
@ -30,6 +30,6 @@ func _on_AnimationTimer_timeout() -> void:
frame = 0
$AnimationTimer.set_one_shot(true)
$AnimationTimer.wait_time = Global.current_project.frame_duration[frame] * (1 / Global.animation_timeline.fps)
$AnimationTimer.wait_time = Global.current_project.frames[frame].duration * (1 / Global.animation_timeline.fps)
$AnimationTimer.start()
update()

View file

@ -149,7 +149,7 @@ func add_animated_preview() -> void:
previews.add_child(container)
frame_timer.set_one_shot(true) #The wait_time it can't change correctly if it is playing
frame_timer.wait_time = Global.current_project.frame_duration[animated_preview_current_frame] * (1 / Global.animation_timeline.fps)
frame_timer.wait_time = Global.current_project.frames[animated_preview_current_frame].duration * (1 / Global.animation_timeline.fps)
frame_timer.start()
@ -363,14 +363,14 @@ func _on_FrameTimer_timeout() -> void:
animated_preview_current_frame = 0
else:
animated_preview_current_frame += 1
frame_timer.wait_time = Global.current_project.frame_duration[(animated_preview_current_frame - 1) % (animated_preview_frames.size())] * (1 / Global.animation_timeline.fps)
frame_timer.wait_time = Global.current_project.frames[(animated_preview_current_frame - 1) % (animated_preview_frames.size())].duration * (1 / Global.animation_timeline.fps)
frame_timer.start()
Export.AnimationDirection.BACKWARDS:
if animated_preview_current_frame == 0:
animated_preview_current_frame = Export.processed_images.size() - 1
else:
animated_preview_current_frame -= 1
frame_timer.wait_time = Global.current_project.frame_duration[(animated_preview_current_frame + 1) % (animated_preview_frames.size())] * (1 / Global.animation_timeline.fps)
frame_timer.wait_time = Global.current_project.frames[(animated_preview_current_frame + 1) % (animated_preview_frames.size())].duration * (1 / Global.animation_timeline.fps)
frame_timer.start()
Export.AnimationDirection.PING_PONG:
match pingpong_direction:
@ -382,7 +382,7 @@ func _on_FrameTimer_timeout() -> void:
animated_preview_current_frame = 0
else:
animated_preview_current_frame += 1
frame_timer.wait_time = Global.current_project.frame_duration[(animated_preview_current_frame - 1) % (animated_preview_frames.size())] * (1 / Global.animation_timeline.fps)
frame_timer.wait_time = Global.current_project.frames[(animated_preview_current_frame - 1) % (animated_preview_frames.size())].duration * (1 / Global.animation_timeline.fps)
frame_timer.start()
Export.AnimationDirection.BACKWARDS:
if animated_preview_current_frame == 0:
@ -392,7 +392,7 @@ func _on_FrameTimer_timeout() -> void:
pingpong_direction = Export.AnimationDirection.FORWARD
else:
animated_preview_current_frame -= 1
frame_timer.wait_time = Global.current_project.frame_duration[(animated_preview_current_frame + 1) % (animated_preview_frames.size())] * (1 / Global.animation_timeline.fps)
frame_timer.wait_time = Global.current_project.frames[(animated_preview_current_frame + 1) % (animated_preview_frames.size())].duration * (1 / Global.animation_timeline.fps)
frame_timer.start()

View file

@ -24,11 +24,9 @@ func _h_scroll_changed(value : float) -> void:
func add_frame() -> void:
var frame_duration : Array = Global.current_project.frame_duration.duplicate()
var frame : Frame = Global.canvas.new_empty_frame()
var new_frames : Array = Global.current_project.frames.duplicate()
var new_layers : Array = Global.current_project.layers.duplicate()
frame_duration.insert(Global.current_project.current_frame + 1, 1)
new_frames.insert(Global.current_project.current_frame + 1, frame)
# Loop through the array to create new classes for each element, so that they
# won't be the same as the original array's classes. Needed for undo/redo to work properly.
@ -50,12 +48,10 @@ func add_frame() -> void:
Global.current_project.undo_redo.add_do_property(Global.current_project, "frames", new_frames)
Global.current_project.undo_redo.add_do_property(Global.current_project, "current_frame", Global.current_project.current_frame + 1)
Global.current_project.undo_redo.add_do_property(Global.current_project, "layers", new_layers)
Global.current_project.undo_redo.add_do_property(Global.current_project, "frame_duration", frame_duration) # Add an 1 in the list of frame_duration
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frames", Global.current_project.frames)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "current_frame", Global.current_project.current_frame )
Global.current_project.undo_redo.add_undo_property(Global.current_project, "layers", Global.current_project.layers)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frame_duration", Global.current_project.frame_duration)
Global.current_project.undo_redo.commit_action()
@ -65,8 +61,6 @@ func _on_DeleteFrame_pressed(frame := -1) -> void:
if frame == -1:
frame = Global.current_project.current_frame
var frame_duration : Array = Global.current_project.frame_duration.duplicate()
frame_duration.remove(frame)
var frame_to_delete : Frame = Global.current_project.frames[frame]
var new_frames : Array = Global.current_project.frames.duplicate()
new_frames.erase(frame_to_delete)
@ -113,13 +107,11 @@ func _on_DeleteFrame_pressed(frame := -1) -> void:
Global.current_project.undo_redo.add_do_property(Global.current_project, "current_frame", current_frame)
Global.current_project.undo_redo.add_do_property(Global.current_project, "animation_tags", new_animation_tags)
Global.current_project.undo_redo.add_do_property(Global.current_project, "layers", new_layers)
Global.current_project.undo_redo.add_do_property(Global.current_project, "frame_duration", frame_duration) #Remove the element of the list of the frame selected
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frames", Global.current_project.frames)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "current_frame", Global.current_project.current_frame)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "animation_tags", Global.current_project.animation_tags)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "layers", Global.current_project.layers)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frame_duration", Global.current_project.frame_duration)
Global.current_project.undo_redo.add_do_method(Global, "redo")
Global.current_project.undo_redo.add_undo_method(Global, "undo")
@ -131,8 +123,6 @@ func _on_CopyFrame_pressed(frame := -1) -> void:
frame = Global.current_project.current_frame
var new_frame := Frame.new()
var frame_duration : Array = Global.current_project.frame_duration.duplicate()
frame_duration.insert(frame + 1, 1)
var new_frames := Global.current_project.frames.duplicate()
new_frames.insert(frame + 1, new_frame)
@ -161,7 +151,6 @@ func _on_CopyFrame_pressed(frame := -1) -> void:
Global.current_project.undo_redo.add_do_property(Global.current_project, "frames", new_frames)
Global.current_project.undo_redo.add_do_property(Global.current_project, "current_frame", frame + 1)
Global.current_project.undo_redo.add_do_property(Global.current_project, "animation_tags", new_animation_tags)
Global.current_project.undo_redo.add_do_property(Global.current_project, "frame_duration", frame_duration)
for i in range(Global.current_project.layers.size()):
for child in Global.current_project.layers[i].frame_container.get_children():
Global.current_project.undo_redo.add_do_property(child, "pressed", false)
@ -170,7 +159,6 @@ func _on_CopyFrame_pressed(frame := -1) -> void:
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frames", Global.current_project.frames)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "current_frame", frame)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "animation_tags", Global.current_project.animation_tags)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frame_duration", Global.current_project.frame_duration)
Global.current_project.undo_redo.commit_action()
@ -245,8 +233,8 @@ func _on_AnimationTimer_timeout() -> void:
if animation_forward:
if Global.current_project.current_frame < last_frame:
Global.current_project.current_frame += 1
Global.animation_timer.wait_time = Global.current_project.frame_duration[Global.current_project.current_frame] * (1/fps)
Global.animation_timer.start() #Change the frame, change the wait time and start a cycle, this is the best way to do it
Global.animation_timer.wait_time = Global.current_project.frames[Global.current_project.current_frame].duration * (1/fps)
Global.animation_timer.start() # Change the frame, change the wait time and start a cycle, this is the best way to do it
else:
match animation_loop:
0: # No loop
@ -255,7 +243,7 @@ func _on_AnimationTimer_timeout() -> void:
Global.animation_timer.stop()
1: # Cycle loop
Global.current_project.current_frame = first_frame
Global.animation_timer.wait_time = Global.current_project.frame_duration[Global.current_project.current_frame] * (1/fps)
Global.animation_timer.wait_time = Global.current_project.frames[Global.current_project.current_frame].duration * (1/fps)
Global.animation_timer.start()
2: # Ping pong loop
animation_forward = false
@ -264,7 +252,7 @@ func _on_AnimationTimer_timeout() -> void:
else:
if Global.current_project.current_frame > first_frame:
Global.current_project.current_frame -= 1
Global.animation_timer.wait_time = Global.current_project.frame_duration[Global.current_project.current_frame] * (1/fps)
Global.animation_timer.wait_time = Global.current_project.frames[Global.current_project.current_frame].duration * (1/fps)
Global.animation_timer.start()
else:
match animation_loop:
@ -274,7 +262,7 @@ func _on_AnimationTimer_timeout() -> void:
Global.animation_timer.stop()
1: # Cycle loop
Global.current_project.current_frame = last_frame
Global.animation_timer.wait_time = Global.current_project.frame_duration[Global.current_project.current_frame] * (1/fps)
Global.animation_timer.wait_time = Global.current_project.frames[Global.current_project.current_frame].duration * (1/fps)
Global.animation_timer.start()
2: # Ping pong loop
animation_forward = true
@ -309,8 +297,9 @@ func play_animation(play : bool, forward_dir : bool) -> void:
Global.play_forward.connect("toggled", self, "_on_PlayForward_toggled")
if play:
Global.animation_timer.set_one_shot(true) #The wait_time it can't change correctly if it is playing
Global.animation_timer.wait_time = Global.current_project.frame_duration[Global.current_project.current_frame] * (1 / fps)
Global.animation_timer.set_one_shot(true) # The wait_time can't change correctly if it is playing
var duration : float = Global.current_project.frames[Global.current_project.current_frame].duration
Global.animation_timer.wait_time = duration * (1 / fps)
Global.animation_timer.start()
animation_forward = forward_dir
else:

View file

@ -105,18 +105,10 @@ func _on_PopupMenu_id_pressed(ID : int) -> void:
Global.frame_properties.popup_centered()
Global.dialog_open(true)
Global.frame_properties.set_frame_label(frame)
Global.frame_properties.set_frame_dur(Global.current_project.frame_duration[frame])
Global.frame_properties.set_frame_dur(Global.current_project.frames[frame].duration)
func change_frame_order(rate : int) -> void:
var change = frame + rate
var frame_duration : Array = Global.current_project.frame_duration.duplicate()
if rate > 0: #There is no function in the class Array in godot to make this quickly and this is the fastest way to swap positions I think of
frame_duration.insert(change + 1, frame_duration[frame])
frame_duration.remove(frame)
else:
frame_duration.insert(change, frame_duration[frame])
frame_duration.remove(frame + 1)
var new_frames : Array = Global.current_project.frames.duplicate()
var temp = new_frames[frame]
new_frames[frame] = new_frames[change]
@ -124,14 +116,12 @@ func change_frame_order(rate : int) -> void:
Global.current_project.undo_redo.create_action("Change Frame Order")
Global.current_project.undo_redo.add_do_property(Global.current_project, "frames", new_frames)
Global.current_project.undo_redo.add_do_property(Global.current_project, "frame_duration", frame_duration)
if Global.current_project.current_frame == frame:
Global.current_project.undo_redo.add_do_property(Global.current_project, "current_frame", change)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "current_frame", Global.current_project.current_frame)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frames", Global.current_project.frames)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frame_duration", Global.current_project.frame_duration)
Global.current_project.undo_redo.add_undo_method(Global, "undo")
Global.current_project.undo_redo.add_do_method(Global, "redo")

View file

@ -13,16 +13,16 @@ func _on_FrameProperties_popup_hide() -> void:
Global.dialog_open(false)
func _on_FrameProperties_confirmed():
var frame : int = int(frame_num.get_text())
var frame : int = int(frame_num.get_text()) - 1
var duration : float = frame_dur.get_value()
var frame_duration = Global.current_project.frame_duration.duplicate()
frame_duration[frame - 1] = duration
var new_duration = Global.current_project.frames[frame].duration
new_duration = duration
Global.current_project.undos += 1
Global.current_project.undo_redo.create_action("Change frame duration")
Global.current_project.undo_redo.add_do_property(Global.current_project, "frame_duration", frame_duration)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "frame_duration", Global.current_project.frame_duration)
Global.current_project.undo_redo.add_do_property(Global.current_project.frames[frame], "duration", new_duration)
Global.current_project.undo_redo.add_undo_property(Global.current_project.frames[frame], "duration", Global.current_project.frames[frame].duration)
Global.current_project.undo_redo.add_do_method(Global, "redo")
Global.current_project.undo_redo.add_undo_method(Global, "undo")