diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index 1dab1f5d7..b20d7c75c 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -158,6 +158,7 @@ func new_empty_frame() -> Frame: return frame +## Returns the currently selected [BaseCel]. func get_current_cel() -> BaseCel: return frames[current_frame].cels[current_layer] @@ -644,6 +645,22 @@ func can_pixel_get_drawn( return true +## Loops through all of the cels until it finds a drawable (non-[GroupCel]) [BaseCel] +## in the specified [param frame] and returns it. If no drawable cel is found, +## meaning that all of the cels are [GroupCel]s, the method returns null. +## If no [param frame] is specified, the method will use the current frame. +func find_first_drawable_cel(frame := frames[current_frame]) -> BaseCel: + var result: BaseCel + var cel := frame.cels[0] + var i := 0 + while cel is GroupCel and i < layers.size(): + cel = frame.cels[i] + i += 1 + if not cel is GroupCel: + result = cel + return result + + # Timeline modifications # Modifying layers or frames Arrays on the current project should generally only be done # through these methods. diff --git a/src/UI/Canvas/Canvas.gd b/src/UI/Canvas/Canvas.gd index 0841d4588..7bb4b66ff 100644 --- a/src/UI/Canvas/Canvas.gd +++ b/src/UI/Canvas/Canvas.gd @@ -39,12 +39,13 @@ func _draw() -> void: if Global.mirror_view: position_tmp.x = position_tmp.x + Global.current_project.size.x scale_tmp.x = -1 + # If we just use the first cel and it happens to be a GroupCel + # nothing will get drawn + var cel_to_draw := Global.current_project.find_first_drawable_cel() draw_set_transform(position_tmp, rotation, scale_tmp) # Placeholder so we can have a material here - draw_texture( - Global.current_project.frames[Global.current_project.current_frame].cels[0].image_texture, - Vector2.ZERO - ) + if is_instance_valid(cel_to_draw): + draw_texture(cel_to_draw.image_texture, Vector2.ZERO) draw_layers() if Global.onion_skinning: refresh_onion() diff --git a/src/UI/Canvas/CanvasPreview.gd b/src/UI/Canvas/CanvasPreview.gd index ee33d5df7..fe93c74b4 100644 --- a/src/UI/Canvas/CanvasPreview.gd +++ b/src/UI/Canvas/CanvasPreview.gd @@ -40,8 +40,12 @@ func _draw() -> void: frame_index = project.current_frame var frame := project.frames[frame_index] animation_timer.wait_time = frame.duration * (1.0 / project.fps) - var texture := frame.cels[0].image_texture - draw_texture(texture, Vector2.ZERO) # Placeholder so we can have a material here + # If we just use the first cel and it happens to be a GroupCel + # nothing will get drawn + var cel_to_draw := Global.current_project.find_first_drawable_cel(frame) + # Placeholder so we can have a material here + if is_instance_valid(cel_to_draw): + draw_texture(cel_to_draw.image_texture, Vector2.ZERO) if material == animation_material: # Only use a unique material if the animation of the canvas preview is playing # Otherwise showing a different frame than the main canvas is impossible @@ -58,9 +62,12 @@ func _draw() -> void: frame_index = start_sprite_sheet_frame - 1 var src_rect := slices[frame_index] var rect := Rect2(Vector2.ZERO, src_rect.size) - var texture := project.frames[project.current_frame].cels[0].image_texture + # If we just use the first cel and it happens to be a GroupCel + # nothing will get drawn + var cel_to_draw := Global.current_project.find_first_drawable_cel() # Placeholder so we can have a material here - draw_texture_rect_region(texture, rect, src_rect) + if is_instance_valid(cel_to_draw): + draw_texture_rect_region(cel_to_draw.image_texture, rect, src_rect) transparent_checker.fit_rect(rect)