|
| 1 | +extends Sprite2D |
| 2 | + |
| 3 | +var current_image: ProcessedImage |
| 4 | +@onready |
| 5 | +var cutout_area : Control = %CutOutArea |
| 6 | +var minimum_zoom : float |
| 7 | +var space_available_from_center : Vector2 |
| 8 | +var additional_offset : Vector2 |
| 9 | +var size: Vector2 |
| 10 | +var old_relative_position: Vector2 |
| 11 | +var pinch_zoom_start: Vector2 |
| 12 | + |
| 13 | +# Called when the node enters the scene tree for the first time. |
| 14 | +func _ready() -> void: |
| 15 | + Application.active_item_changed.connect(update_image) |
| 16 | + cutout_area.resized.connect(update_positions) |
| 17 | + |
| 18 | +func update_image(image: ProcessedImage) -> void: |
| 19 | + |
| 20 | + if current_image != null: |
| 21 | + current_image.was_loaded.disconnect(update_image.bind(image)) |
| 22 | + |
| 23 | + current_image = image |
| 24 | + |
| 25 | + if image.original_texture == null: |
| 26 | + image.load_image() |
| 27 | + image.was_loaded.connect(update_image.bind(image)) |
| 28 | + |
| 29 | + texture = image.original_texture |
| 30 | + |
| 31 | +func update_positions() -> void: |
| 32 | + if texture == null: |
| 33 | + return |
| 34 | + |
| 35 | + # Set size to original texture size |
| 36 | + size = texture.get_size() |
| 37 | + |
| 38 | + # Adjust the scale so that it fits our aspectratio container |
| 39 | + var container_ratio := float(Application.crop_to_size.x)/float(Application.crop_to_size.y) |
| 40 | + var image_ratio := float(size.x)/float(size.y) |
| 41 | + |
| 42 | + var scale_tmp : float |
| 43 | + |
| 44 | + if image_ratio < container_ratio: |
| 45 | + scale_tmp = cutout_area.size.x / size.x |
| 46 | + else: |
| 47 | + scale_tmp = cutout_area.size.y / size.y |
| 48 | + |
| 49 | + minimum_zoom = scale_tmp |
| 50 | + scale = Vector2(scale_tmp, scale_tmp) |
| 51 | + |
| 52 | +func _input(event: InputEvent) -> void: |
| 53 | + |
| 54 | + |
| 55 | + # Check for mouse events to update the position |
| 56 | + |
| 57 | + if event is InputEventMouseMotion and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT): |
| 58 | + |
| 59 | + additional_offset -= event.relative |
| 60 | + clamp_position() |
| 61 | + |
| 62 | + |
| 63 | + if event is InputEventMouseMotion and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT): |
| 64 | + |
| 65 | + if event.relative.x > 0: |
| 66 | + scale *= 1.0 + event.relative.x / 200 |
| 67 | + |
| 68 | + |
| 69 | + elif event.relative.x < 0: |
| 70 | + scale *= 1.0 + event.relative.x / 200 |
| 71 | + |
| 72 | + |
| 73 | + if event.relative.y > 0: |
| 74 | + scale *= 1.0 + event.relative.y / 1000 |
| 75 | + |
| 76 | + |
| 77 | + elif event.relative.y < 0: |
| 78 | + scale *= 1.0 + event.relative.y / 1000 |
| 79 | + |
| 80 | + if scale.x < minimum_zoom: |
| 81 | + scale = Vector2(minimum_zoom, minimum_zoom) |
| 82 | + |
| 83 | + |
| 84 | + additional_offset += (pinch_zoom_start - get_local_mouse_position())*scale |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + if event is InputEventMouseButton: |
| 89 | + |
| 90 | + if event.button_index == MOUSE_BUTTON_RIGHT: |
| 91 | + if event.pressed: |
| 92 | + pinch_zoom_start = get_local_mouse_position() |
| 93 | + |
| 94 | + if event.button_index == MOUSE_BUTTON_WHEEL_UP: |
| 95 | + scale *= 1.05 |
| 96 | + additional_offset += (old_relative_position - get_local_mouse_position())*scale |
| 97 | + |
| 98 | + |
| 99 | + elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN: |
| 100 | + scale *= 0.9 |
| 101 | + |
| 102 | + if scale.x < minimum_zoom: |
| 103 | + scale = Vector2(minimum_zoom, minimum_zoom) |
| 104 | + additional_offset += (old_relative_position - get_local_mouse_position())*scale |
| 105 | + |
| 106 | + clamp_position() |
| 107 | + |
| 108 | + |
| 109 | + old_relative_position = get_local_mouse_position() |
| 110 | + |
| 111 | +func clamp_position() -> void: |
| 112 | + space_available_from_center = ((size - (cutout_area.size / scale))/2)*scale |
| 113 | + |
| 114 | + if abs(additional_offset.x) > space_available_from_center.x: |
| 115 | + additional_offset.x = space_available_from_center.x * sign(additional_offset.x) |
| 116 | + if abs(additional_offset.y) > space_available_from_center.y: |
| 117 | + additional_offset.y = space_available_from_center.y * sign(additional_offset.y) |
| 118 | + |
| 119 | + position = -additional_offset |
0 commit comments