Moved image effect code to DrawingAlgos.gd

More specifically, moved scale, crop, invert, desaturate and generate outline code to DrawingAlgos.gd. I am a little worried of DrawingAlgos.gd getting too big though.
This commit is contained in:
OverloadedOrama 2020-06-13 18:57:28 +03:00
parent c6b977d48e
commit 53b91a4bf4
4 changed files with 226 additions and 218 deletions

View file

@ -267,12 +267,13 @@ func toggle_zen_mode() -> void:
func image_menu_id_pressed(id : int) -> void:
if Global.current_project.layers[Global.current_project.current_layer].locked: # No changes if the layer is locked
return
var image : Image = Global.current_project.frames[0].cels[0].image
match id:
0: # Scale Image
show_scale_image_popup()
1: # Crop Image
crop_image()
DrawingAlgos.crop_image(image)
2: # Flip Horizontal
flip_image(true)
@ -284,10 +285,10 @@ func image_menu_id_pressed(id : int) -> void:
show_rotate_image_popup()
5: # Invert Colors
invert_image_colors()
DrawingAlgos.invert_image_colors(image)
6: # Desaturation
desaturate_image()
DrawingAlgos.desaturate_image(image)
7: # Outline
show_add_outline_popup()
@ -301,46 +302,6 @@ func show_scale_image_popup() -> void:
Global.dialog_open(true)
func crop_image() -> void:
# Use first cel as a starting rectangle
var used_rect : Rect2 = Global.current_project.frames[0].cels[0].image.get_used_rect()
for f in Global.current_project.frames:
# However, if first cel is empty, loop through all cels until we find one that isn't
for cel in f.cels:
if used_rect != Rect2(0, 0, 0, 0):
break
else:
if cel.image.get_used_rect() != Rect2(0, 0, 0, 0):
used_rect = cel.image.get_used_rect()
# Merge all layers with content
for cel in f.cels:
if cel.image.get_used_rect() != Rect2(0, 0, 0, 0):
used_rect = used_rect.merge(cel.image.get_used_rect())
# If no layer has any content, just return
if used_rect == Rect2(0, 0, 0, 0):
return
var width := used_rect.size.x
var height := used_rect.size.y
Global.current_project.undos += 1
Global.current_project.undo_redo.create_action("Scale")
Global.current_project.undo_redo.add_do_property(Global.current_project, "size", Vector2(width, height).floor())
for f in Global.current_project.frames:
# Loop through all the layers to crop them
for j in range(Global.current_project.layers.size() - 1, -1, -1):
var sprite : Image = f.cels[j].image.get_rect(used_rect)
Global.current_project.undo_redo.add_do_property(f.cels[j].image, "data", sprite.data)
Global.current_project.undo_redo.add_undo_property(f.cels[j].image, "data", f.cels[j].image.data)
Global.current_project.undo_redo.add_undo_property(Global.current_project, "size", Global.current_project.size)
Global.current_project.undo_redo.add_undo_method(Global, "undo")
Global.current_project.undo_redo.add_do_method(Global, "redo")
Global.current_project.undo_redo.commit_action()
func flip_image(horizontal : bool) -> void:
var image : Image = Global.current_project.frames[Global.current_project.current_frame].cels[Global.current_project.current_layer].image
Global.canvas.handle_undo("Draw")
@ -360,32 +321,6 @@ func show_rotate_image_popup() -> void:
Global.dialog_open(true)
func invert_image_colors() -> void:
var image : Image = Global.current_project.frames[Global.current_project.current_frame].cels[Global.current_project.current_layer].image
Global.canvas.handle_undo("Draw")
for xx in image.get_size().x:
for yy in image.get_size().y:
var px_color = image.get_pixel(xx, yy).inverted()
if px_color.a == 0:
continue
image.set_pixel(xx, yy, px_color)
Global.canvas.handle_redo("Draw")
func desaturate_image() -> void:
var image : Image = Global.current_project.frames[Global.current_project.current_frame].cels[Global.current_project.current_layer].image
Global.canvas.handle_undo("Draw")
for xx in image.get_size().x:
for yy in image.get_size().y:
var px_color = image.get_pixel(xx, yy)
if px_color.a == 0:
continue
var gray = image.get_pixel(xx, yy).v
px_color = Color(gray, gray, gray, px_color.a)
image.set_pixel(xx, yy, px_color)
Global.canvas.handle_redo("Draw")
func show_add_outline_popup() -> void:
Global.control.get_node("OutlineDialog").popup_centered()
Global.dialog_open(true)