Skip to content

Commit

Permalink
Keep the aspect ratio correctly in the image effect dialog previews
Browse files Browse the repository at this point in the history
  • Loading branch information
OverloadedOrama committed Feb 11, 2024
1 parent 2b4d85a commit 85b2550
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 32 deletions.
20 changes: 3 additions & 17 deletions src/Classes/ImageEffect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var affect: int = SELECTED_CELS
var selected_cels := Image.create(1, 1, false, Image.FORMAT_RGBA8)
var current_frame := Image.create(1, 1, false, Image.FORMAT_RGBA8)
var preview_image := Image.new()
var aspect_ratio_container: AspectRatioContainer
var preview: TextureRect
var selection_checkbox: CheckBox
var affect_option_button: OptionButton
Expand Down Expand Up @@ -38,7 +39,7 @@ func _about_to_popup() -> void:
Global.canvas.selection.transform_content_confirm()
prepare_animator(Global.current_project)
set_and_update_preview_image(Global.current_project.current_frame)
update_transparent_background_size()
aspect_ratio_container.ratio = float(preview_image.get_width()) / preview_image.get_height()


# prepares "animate_panel.frames" according to affect
Expand Down Expand Up @@ -131,6 +132,7 @@ func commit_action(_cel: Image, _project := Global.current_project) -> void:


func set_nodes() -> void:
aspect_ratio_container = $VBoxContainer/AspectRatioContainer
preview = $VBoxContainer/AspectRatioContainer/Preview
selection_checkbox = $VBoxContainer/OptionsContainer/SelectionCheckBox
affect_option_button = $VBoxContainer/OptionsContainer/AffectOptionButton
Expand Down Expand Up @@ -214,22 +216,6 @@ func update_preview() -> void:
preview.texture = ImageTexture.create_from_image(preview_image)


func update_transparent_background_size() -> void:
if !preview:
return
var image_size_y := preview.size.y
var image_size_x := preview.size.x
if preview_image.get_size().x > preview_image.get_size().y:
var scale_ratio := preview_image.get_size().x / image_size_x
image_size_y = preview_image.get_size().y / scale_ratio
else:
var scale_ratio := preview_image.get_size().y / image_size_y
image_size_x = preview_image.get_size().x / scale_ratio

preview.get_node("TransparentChecker").size.x = image_size_x
preview.get_node("TransparentChecker").size.y = image_size_y


func _visibility_changed() -> void:
if visible:
return
Expand Down
17 changes: 2 additions & 15 deletions src/UI/Dialogs/ImageEffects/ResizeCanvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var image := Image.create(1, 1, false, Image.FORMAT_RGBA8)
@onready var height_spinbox: SpinBox = $VBoxContainer/OptionsContainer/HeightValue
@onready var x_spinbox: SpinBox = $VBoxContainer/OptionsContainer/XSpinBox
@onready var y_spinbox: SpinBox = $VBoxContainer/OptionsContainer/YSpinBox
@onready var aspect_ratio_container: AspectRatioContainer = $VBoxContainer/AspectRatioContainer
@onready var preview_rect: TextureRect = $VBoxContainer/AspectRatioContainer/Preview


Expand Down Expand Up @@ -82,21 +83,7 @@ func update_preview() -> void:
image, Rect2i(Vector2i.ZERO, Global.current_project.size), Vector2i(offset_x, offset_y)
)
preview_rect.texture = ImageTexture.create_from_image(preview_image)
update_transparent_background_size(preview_image)


func update_transparent_background_size(preview_image: Image) -> void:
var image_size_y := preview_rect.size.y
var image_size_x := preview_rect.size.x
if preview_image.get_size().x > preview_image.get_size().y:
var scale_ratio := preview_image.get_size().x / image_size_x
image_size_y = preview_image.get_size().y / scale_ratio
else:
var scale_ratio := preview_image.get_size().y / image_size_y
image_size_x = preview_image.get_size().x / scale_ratio

preview_rect.get_node("TransparentChecker").size.x = image_size_x
preview_rect.get_node("TransparentChecker").size.y = image_size_y
aspect_ratio_container.ratio = float(preview_image.get_width()) / preview_image.get_height()


func _on_visibility_changed() -> void:
Expand Down

0 comments on commit 85b2550

Please sign in to comment.