1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 09:09:47 +00:00

Compare commits

...

7 commits

Author SHA1 Message Date
Emmanouil Papadeas b126e95b64 Almost made selection rotation with gizmos functional
Not exposed yet
2024-01-25 02:33:41 +02:00
Emmanouil Papadeas 3d04a8d276 Selection rotation with gizmos works on selections without content now
Still not ready and thus not exposed
2024-01-25 01:35:42 +02:00
Emmanouil Papadeas 964e9fbd26 Don't set the selection_map of the project to the original_bitmap, if the latter is empty
Shouldn't happen, but best to check in case it does. Setting empty data to the selection_map breaks selections.
2024-01-25 01:06:30 +02:00
Emmanouil Papadeas de5db85345 When resizing a selection with gizmos or from the tool options, only set the original_bitmap when we're not already transforming content 2024-01-25 00:59:53 +02:00
Emmanouil Papadeas f8b32762a1 Fix canceling selection content resizing breaking the selection 2024-01-25 00:45:49 +02:00
Emmanouil Papadeas 56fe1840e0 Make selections scale properly even if they don't transform any image content
Fixes #774.
2024-01-25 00:40:53 +02:00
Emmanouil Papadeas 3a0977ce21 Some code cleanup in Selection.gd 2024-01-25 00:11:19 +02:00
3 changed files with 66 additions and 74 deletions

View file

@ -425,6 +425,8 @@ func fake_rotsprite(sprite: Image, angle: float, pivot: Vector2) -> void:
func nn_rotate(sprite: Image, angle: float, pivot: Vector2) -> void:
if is_zero_approx(angle):
return
var aux := Image.new()
aux.copy_from(sprite)
var ox: int

View file

@ -252,6 +252,8 @@ func _on_Size_value_changed(value: Vector2i) -> void:
if timer.is_stopped():
undo_data = selection_node.get_undo_data(false)
if not selection_node.is_moving_content:
selection_node.original_bitmap.copy_from(Global.current_project.selection_map)
timer.start()
selection_node.big_bounding_rectangle.size = value
selection_node.resize_selection()
@ -262,5 +264,5 @@ func _on_Size_ratio_toggled(button_pressed: bool) -> void:
func _on_Timer_timeout() -> void:
if !selection_node.is_moving_content:
if not selection_node.is_moving_content:
selection_node.commit_undo("Move Selection", undo_data)

View file

@ -13,6 +13,11 @@ var is_pasting := false
var big_bounding_rectangle := Rect2i():
set(value):
big_bounding_rectangle = value
if value.size == Vector2i(0, 0):
set_process_input(false)
Global.can_draw = true
else:
set_process_input(true)
for slot in Tools._slots.values():
if slot.tool_node is BaseSelectionTool:
slot.tool_node.set_spinbox_values()
@ -34,7 +39,8 @@ var preview_image_texture := ImageTexture.new()
var undo_data: Dictionary
var gizmos: Array[Gizmo] = []
var dragged_gizmo: Gizmo = null
var prev_angle := 0
var angle := 0.0
var content_pivot := Vector2.ZERO
var mouse_pos_on_gizmo_drag := Vector2.ZERO
var resize_keep_ratio := false
@ -53,24 +59,24 @@ class Gizmo:
type = _type
direction = _direction
func get_cursor() -> Control.CursorShape:
var cursor := Control.CURSOR_MOVE
func get_cursor() -> DisplayServer.CursorShape:
var cursor := DisplayServer.CURSOR_MOVE
if direction == Vector2i.ZERO:
return Control.CURSOR_POINTING_HAND
return DisplayServer.CURSOR_POINTING_HAND
elif direction == Vector2i(-1, -1) or direction == Vector2i(1, 1): # Top left or bottom right
if Global.mirror_view:
cursor = Control.CURSOR_BDIAGSIZE
cursor = DisplayServer.CURSOR_BDIAGSIZE
else:
cursor = Control.CURSOR_FDIAGSIZE
cursor = DisplayServer.CURSOR_FDIAGSIZE
elif direction == Vector2i(1, -1) or direction == Vector2i(-1, 1): # Top right or bottom left
if Global.mirror_view:
cursor = Control.CURSOR_FDIAGSIZE
cursor = DisplayServer.CURSOR_FDIAGSIZE
else:
cursor = Control.CURSOR_BDIAGSIZE
cursor = DisplayServer.CURSOR_BDIAGSIZE
elif direction == Vector2i(0, -1) or direction == Vector2i(0, 1): # Center top or center bottom
cursor = Control.CURSOR_VSIZE
cursor = DisplayServer.CURSOR_VSIZE
elif direction == Vector2i(-1, 0) or direction == Vector2i(1, 0): # Center left or center right
cursor = Control.CURSOR_HSIZE
cursor = DisplayServer.CURSOR_HSIZE
return cursor
@ -84,12 +90,11 @@ func _ready() -> void:
gizmos.append(Gizmo.new(Gizmo.Type.SCALE, Vector2i(0, 1))) # Center bottom
gizmos.append(Gizmo.new(Gizmo.Type.SCALE, Vector2i(-1, 1))) # Bottom left
gizmos.append(Gizmo.new(Gizmo.Type.SCALE, Vector2i(-1, 0))) # Center left
#gizmos.append(Gizmo.new(Gizmo.Type.ROTATE)) # Rotation gizmo (temp)
func _input(event: InputEvent) -> void:
var project := Global.current_project
image_current_pixel = canvas.current_pixel
if Global.mirror_view:
image_current_pixel.x = Global.current_project.size.x - image_current_pixel.x
@ -99,7 +104,6 @@ func _input(event: InputEvent) -> void:
elif Input.is_action_just_pressed("transformation_cancel"):
transform_content_cancel()
var project := Global.current_project
if not project.layers[project.current_layer].can_layer_get_drawn():
return
if event is InputEventKey:
@ -123,17 +127,14 @@ func _input(event: InputEvent) -> void:
if Input.is_action_pressed("transform_move_selection_only"):
transform_content_confirm()
if not is_moving_content:
original_bitmap.copy_from(Global.current_project.selection_map)
original_big_bounding_rectangle = big_bounding_rectangle
if Input.is_action_pressed("transform_move_selection_only"):
undo_data = get_undo_data(false)
temp_rect = big_bounding_rectangle
else:
transform_content_start()
project.selection_offset = Vector2.ZERO
if dragged_gizmo.type == Gizmo.Type.ROTATE:
var img_size := maxi(
original_preview_image.get_width(), original_preview_image.get_height()
)
original_preview_image.crop(img_size, img_size)
else:
var prev_temp_rect := temp_rect
dragged_gizmo.direction.x *= signi(temp_rect.size.x)
@ -157,7 +158,9 @@ func _input(event: InputEvent) -> void:
Global.can_draw = true
dragged_gizmo = null
if not is_moving_content:
original_bitmap = SelectionMap.new()
commit_undo("Select", undo_data)
angle = 0.0
if dragged_gizmo:
if dragged_gizmo.type == Gizmo.Type.SCALE:
@ -166,17 +169,17 @@ func _input(event: InputEvent) -> void:
_gizmo_rotate()
else: # Set the appropriate cursor
if gizmo_hover:
Global.main_viewport.mouse_default_cursor_shape = gizmo_hover.get_cursor()
DisplayServer.cursor_set_shape(gizmo_hover.get_cursor())
else:
var cursor := Control.CURSOR_ARROW
var cursor := DisplayServer.CURSOR_ARROW
if Global.cross_cursor:
cursor = Control.CURSOR_CROSS
cursor = DisplayServer.CURSOR_CROSS
var layer: BaseLayer = project.layers[project.current_layer]
if not layer.can_layer_get_drawn():
cursor = Control.CURSOR_FORBIDDEN
cursor = DisplayServer.CURSOR_FORBIDDEN
if Global.main_viewport.mouse_default_cursor_shape != cursor:
Global.main_viewport.mouse_default_cursor_shape = cursor
if DisplayServer.cursor_get_shape() != cursor:
DisplayServer.cursor_set_shape(cursor)
func _move_with_arrow_keys(event: InputEvent) -> void:
@ -210,14 +213,14 @@ func _move_with_arrow_keys(event: InputEvent) -> void:
var move := input.rotated(snappedf(Global.camera.rotation, PI / 2))
# These checks are needed to fix a bug where the selection got stuck
# to the canvas boundaries when they were 1px away from them
if is_equal_approx(absf(move.x), 0.0):
if is_zero_approx(absf(move.x)):
move.x = 0
if is_equal_approx(absf(move.y), 0.0):
if is_zero_approx(absf(move.y)):
move.y = 0
move_content(move * step)
# Check if an event is a ui_up/down/left/right event-press
## Check if an event is a ui_up/down/left/right event pressed
func _is_action_direction_pressed(event: InputEvent) -> bool:
for action in KEY_MOVE_ACTION_NAMES:
if event.is_action_pressed(action, false, true):
@ -225,7 +228,7 @@ func _is_action_direction_pressed(event: InputEvent) -> bool:
return false
# Check if an event is a ui_up/down/left/right event release
## Check if an event is a ui_up/down/left/right event
func _is_action_direction(event: InputEvent) -> bool:
for action in KEY_MOVE_ACTION_NAMES:
if event.is_action(action, true):
@ -233,7 +236,7 @@ func _is_action_direction(event: InputEvent) -> bool:
return false
# Check if an event is a ui_up/down/left/right event release
## Check if an event is a ui_up/down/left/right event release
func _is_action_direction_released(event: InputEvent) -> bool:
for action in KEY_MOVE_ACTION_NAMES:
if event.is_action_released(action, true):
@ -337,8 +340,6 @@ func _gizmo_resize() -> void:
temp_rect.position.y = end_y - temp_rect.size.y
big_bounding_rectangle = temp_rect.abs()
# big_bounding_rectangle.position = Vector2(big_bounding_rectangle.position).ceil()
# big_bounding_rectangle.size = big_bounding_rectangle.size.floor()
if big_bounding_rectangle.size.x == 0:
big_bounding_rectangle.size.x = 1
if big_bounding_rectangle.size.y == 0:
@ -370,9 +371,14 @@ func _resize_rect(pos: Vector2, dir: Vector2) -> void:
func resize_selection() -> void:
var size := big_bounding_rectangle.size.abs()
if is_moving_content:
if original_bitmap.is_empty():
print("original_bitmap is empty, this shouldn't happen.")
else:
Global.current_project.selection_map.copy_from(original_bitmap)
if is_moving_content:
content_pivot = original_big_bounding_rectangle.size / 2.0
preview_image.copy_from(original_preview_image)
DrawingAlgos.nn_rotate(preview_image, angle, content_pivot)
preview_image.resize(size.x, size.y, Image.INTERPOLATE_NEAREST)
if temp_rect.size.x < 0:
preview_image.flip_x()
@ -380,6 +386,12 @@ func resize_selection() -> void:
preview_image.flip_y()
preview_image_texture = ImageTexture.create_from_image(preview_image)
Global.current_project.selection_map.copy_from(original_bitmap)
var bitmap_pivot := (
original_big_bounding_rectangle.position
+ ((original_big_bounding_rectangle.end - original_big_bounding_rectangle.position) / 2)
)
DrawingAlgos.nn_rotate(Global.current_project.selection_map, angle, bitmap_pivot)
Global.current_project.selection_map.resize_bitmap_values(
Global.current_project, size, temp_rect.size.x < 0, temp_rect.size.y < 0
)
@ -388,40 +400,12 @@ func resize_selection() -> void:
Global.canvas.queue_redraw()
func _gizmo_rotate() -> void: ## Currently unused, as it does not work properly yet
var angle := image_current_pixel.angle_to_point(mouse_pos_on_gizmo_drag)
angle = deg_to_rad(floorf(rad_to_deg(angle)))
if angle == prev_angle:
return
prev_angle = angle
var pivot := Vector2(big_bounding_rectangle.size.x / 2.0, big_bounding_rectangle.size.y / 2.0)
preview_image.copy_from(original_preview_image)
if original_big_bounding_rectangle.position != big_bounding_rectangle.position:
preview_image.fill(Color(0, 0, 0, 0))
var pos_diff := (
(original_big_bounding_rectangle.position - big_bounding_rectangle.position).abs()
)
preview_image.blit_rect(
original_preview_image, Rect2(Vector2.ZERO, preview_image.get_size()), pos_diff
)
DrawingAlgos.nn_rotate(preview_image, angle, pivot)
preview_image_texture = ImageTexture.create_from_image(preview_image)
var bitmap_image := SelectionMap.new()
bitmap_image.copy_from(original_bitmap)
var bitmap_pivot := (
original_big_bounding_rectangle.position
+ ((original_big_bounding_rectangle.end - original_big_bounding_rectangle.position) / 2)
)
DrawingAlgos.nn_rotate(bitmap_image, angle, bitmap_pivot)
Global.current_project.selection_map.copy_from(bitmap_image)
Global.current_project.selection_map_changed()
big_bounding_rectangle = Global.current_project.selection_map.get_used_rect()
queue_redraw()
Global.canvas.queue_redraw()
func _gizmo_rotate() -> void:
angle = image_current_pixel.angle_to_point(mouse_pos_on_gizmo_drag)
resize_selection()
func select_rect(rect: Rect2i, operation: int = SelectionOperation.ADD) -> void:
func select_rect(rect: Rect2i, operation := SelectionOperation.ADD) -> void:
var project := Global.current_project
# Used only if the selection is outside of the canvas boundaries,
# on the left and/or above (negative coords)
@ -508,15 +492,15 @@ func transform_content_confirm() -> void:
return
var project := Global.current_project
for cel in _get_selected_draw_cels():
var cel_image: Image = cel.get_image()
var src: Image = preview_image
var cel_image := cel.get_image()
var src := Image.new()
src.copy_from(preview_image)
if not is_pasting:
src.copy_from(cel.transformed_content)
cel.transformed_content = null
DrawingAlgos.nn_rotate(src, angle, content_pivot)
src.resize(
big_bounding_rectangle.size.x,
big_bounding_rectangle.size.y,
Image.INTERPOLATE_NEAREST
preview_image.get_width(), preview_image.get_height(), Image.INTERPOLATE_NEAREST
)
if temp_rect.size.x < 0:
src.flip_x()
@ -537,6 +521,8 @@ func transform_content_confirm() -> void:
original_bitmap = SelectionMap.new()
is_moving_content = false
is_pasting = false
angle = 0.0
content_pivot = Vector2.ZERO
queue_redraw()
Global.canvas.queue_redraw()
@ -568,6 +554,8 @@ func transform_content_cancel() -> void:
preview_image = Image.new()
original_bitmap = SelectionMap.new()
is_pasting = false
angle = 0.0
content_pivot = Vector2.ZERO
queue_redraw()
Global.canvas.queue_redraw()