Skip to content

Commit

Permalink
Fix issue where shader-based effects were not respecting the selectio…
Browse files Browse the repository at this point in the history
…n bounds, when the selection was out of the canvas
  • Loading branch information
OverloadedOrama committed Apr 13, 2024
1 parent e4afcc2 commit 436406a
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 17 deletions.
19 changes: 19 additions & 0 deletions src/Classes/SelectionMap.gd
Expand Up @@ -92,6 +92,25 @@ func invert() -> void:
self.convert(Image.FORMAT_LA8)


## Return Image because Godot 3.x doesn't like when the class name is referenced inside the class
func return_cropped_copy(size: Vector2) -> Image:
var selection_map_copy: Image = get_script().new()
selection_map_copy.copy_from(self)
var diff := Vector2.ZERO
var selection_position: Vector2 = Global.canvas.selection.big_bounding_rectangle.position
if selection_position.x < 0:
diff.x += selection_position.x
if selection_position.y < 0:
diff.y += selection_position.y
if diff != Vector2.ZERO:
# If there are pixels out of bounds on the negative side (left & up),
# move them before resizing
selection_map_copy.fill(Color(0))
selection_map_copy.blit_rect(self, Rect2(Vector2.ZERO, get_size()), diff)
selection_map_copy.crop(size.x, size.y)
return selection_map_copy


func move_bitmap_values(project, move_offset := true) -> void:
var size: Vector2 = project.size
var selection_node = Global.canvas.selection
Expand Down
9 changes: 3 additions & 6 deletions src/Tools/Bucket.gd
Expand Up @@ -189,7 +189,7 @@ func fill_in_color(position: Vector2) -> void:
var selection: Image
var selection_tex := ImageTexture.new()
if project.has_selection:
selection = project.selection_map
selection = project.selection_map.return_cropped_copy(project.size)
else:
selection = Image.new()
selection.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
Expand Down Expand Up @@ -248,10 +248,7 @@ func fill_in_selection() -> void:
filler.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
filler.fill(tool_slot.color)
var rect: Rect2 = Global.canvas.selection.big_bounding_rectangle
var selection_map_copy := SelectionMap.new()
selection_map_copy.copy_from(project.selection_map)
# In case the selection map is bigger than the canvas
selection_map_copy.crop(project.size.x, project.size.y)
var selection_map_copy := project.selection_map.return_cropped_copy(project.size)
for image in images:
image.blit_rect_mask(filler, selection_map_copy, rect, rect.position)
else:
Expand All @@ -267,7 +264,7 @@ func fill_in_selection() -> void:
var selection: Image
var selection_tex := ImageTexture.new()
if project.has_selection:
selection = project.selection_map
selection = project.selection_map.return_cropped_copy(project.size)
else:
selection = Image.new()
selection.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/DesaturateDialog.gd
Expand Up @@ -17,7 +17,7 @@ func _ready() -> void:
func commit_action(cel: Image, project: Project = Global.current_project) -> void:
var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection_tex.create_from_image(project.selection_map, 0)
selection_tex.create_from_image(project.selection_map.return_cropped_copy(project.size), 0)

var params := {
"red": red, "blue": blue, "green": green, "alpha": alpha, "selection": selection_tex
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/DropShadowDialog.gd
Expand Up @@ -28,7 +28,7 @@ func commit_action(cel: Image, project: Project = Global.current_project) -> voi
var offset_y := animate_panel.get_animated_value(commit_idx, Animate.OFFSET_Y)
var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection_tex.create_from_image(project.selection_map, 0)
selection_tex.create_from_image(project.selection_map.return_cropped_copy(project.size), 0)

var params := {
"shadow_offset": Vector2(offset_x, offset_y),
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/GradientDialog.gd
Expand Up @@ -58,7 +58,7 @@ func commit_action(cel: Image, project: Project = Global.current_project) -> voi
var selection: Image
var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection = project.selection_map
selection = project.selection_map.return_cropped_copy(project.size)
else: # This is needed to prevent a weird bug with the dithering shaders and GLES2
selection = Image.new()
selection.create(project.size.x, project.size.y, false, Image.FORMAT_L8)
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/GradientMapDialog.gd
Expand Up @@ -12,7 +12,7 @@ func _ready() -> void:
func commit_action(cel: Image, project: Project = Global.current_project) -> void:
var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection_tex.create_from_image(project.selection_map, 0)
selection_tex.create_from_image(project.selection_map.return_cropped_copy(project.size), 0)

var params := {"selection": selection_tex, "map": $VBoxContainer/GradientEdit.texture}

Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/HSVDialog.gd
Expand Up @@ -29,7 +29,7 @@ func commit_action(cel: Image, project: Project = Global.current_project) -> voi
var val = animate_panel.get_animated_value(commit_idx, Animate.VALUE) / 100
var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection_tex.create_from_image(project.selection_map, 0)
selection_tex.create_from_image(project.selection_map.return_cropped_copy(project.size), 0)

var params := {"hue_shift": hue, "sat_shift": sat, "val_shift": val, "selection": selection_tex}
if !confirmed:
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/InvertColorsDialog.gd
Expand Up @@ -17,7 +17,7 @@ func _ready() -> void:
func commit_action(cel: Image, project: Project = Global.current_project) -> void:
var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection_tex.create_from_image(project.selection_map, 0)
selection_tex.create_from_image(project.selection_map.return_cropped_copy(project.size), 0)

var params := {
"red": red, "blue": blue, "green": green, "alpha": alpha, "selection": selection_tex
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/OffsetImage.gd
Expand Up @@ -33,7 +33,7 @@ func commit_action(cel: Image, project: Project = Global.current_project) -> voi
var offset := Vector2(offset_x, offset_y)
var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection_tex.create_from_image(project.selection_map, 0)
selection_tex.create_from_image(project.selection_map.return_cropped_copy(project.size), 0)

var params := {"offset": offset, "wrap_around": wrap_around, "selection": selection_tex}
if !confirmed:
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/OutlineDialog.gd
Expand Up @@ -35,7 +35,7 @@ func commit_action(cel: Image, project: Project = Global.current_project) -> voi

var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection_tex.create_from_image(project.selection_map, 0)
selection_tex.create_from_image(project.selection_map.return_cropped_copy(project.size), 0)

var params := {
"color": color,
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Dialogs/ImageEffects/Posterize.gd
Expand Up @@ -14,7 +14,7 @@ func _ready() -> void:
func commit_action(cel: Image, project: Project = Global.current_project) -> void:
var selection_tex := ImageTexture.new()
if selection_checkbox.pressed and project.has_selection:
selection_tex.create_from_image(project.selection_map, 0)
selection_tex.create_from_image(project.selection_map.return_cropped_copy(project.size), 0)

var params := {"colors": levels, "dither": dither, "selection": selection_tex}

Expand Down
4 changes: 2 additions & 2 deletions src/UI/Dialogs/ImageEffects/RotateImage.gd
Expand Up @@ -97,8 +97,8 @@ func commit_action(cel: Image, _project: Project = Global.current_project) -> vo
var selection_rectangle: Rect2 = _project.selection_map.get_used_rect()
selection_size = selection_rectangle.size

var selection: Image = _project.selection_map
selection_tex.create_from_image(selection, 0)
var selection := _project.selection_map
selection_tex.create_from_image(selection.return_cropped_copy(_project.size), 0)

if !_type_is_shader():
image.lock()
Expand Down

0 comments on commit 436406a

Please sign in to comment.