1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-30 23:19:49 +00:00

UndoRedo vol 7 - New/Open/Import clear history, Crop has undo/redo

- New, Open and Import now clear undo/redo history
- Crop Image now has undo/redo
- Fixed bug where redo wasn't working properly in multiple frames

Found bug when drawing while animating - undo/redo isn't being registered properly.
This commit is contained in:
OverloadedOrama 2019-11-05 03:14:03 +02:00
parent 18d109f959
commit 8bc0879814
3 changed files with 27 additions and 14 deletions

View file

@ -96,7 +96,7 @@ func _process(delta) -> void:
#Handle Undo/Redo #Handle Undo/Redo
var can_handle : bool = mouse_in_canvas && Global.can_draw && Global.has_focus && Global.current_frame == frame var can_handle : bool = mouse_in_canvas && Global.can_draw && Global.has_focus
var mouse_pressed : bool = (Input.is_action_just_pressed("left_mouse") && !Input.is_action_pressed("right_mouse")) || (Input.is_action_just_pressed("right_mouse") && !Input.is_action_pressed("left_mouse")) var mouse_pressed : bool = (Input.is_action_just_pressed("left_mouse") && !Input.is_action_pressed("right_mouse")) || (Input.is_action_just_pressed("right_mouse") && !Input.is_action_pressed("left_mouse"))
#If we're already pressing a mouse button and we haven't handled undo yet,... #If we're already pressing a mouse button and we haven't handled undo yet,...
@ -107,16 +107,17 @@ func _process(delta) -> void:
mouse_pressed = true mouse_pressed = true
if mouse_pressed: if mouse_pressed:
if can_handle: if can_handle && Global.current_frame == frame:
if current_action != "None": if current_action != "None":
if current_action == "RectSelect": if current_action == "RectSelect":
handle_undo("Rectangle Select") handle_undo("Rectangle Select")
else: else:
handle_undo("Draw") handle_undo("Draw")
elif (Input.is_action_just_released("left_mouse") && !Input.is_action_pressed("right_mouse")) || (Input.is_action_just_released("right_mouse") && !Input.is_action_pressed("left_mouse")): elif (Input.is_action_just_released("left_mouse") && !Input.is_action_pressed("right_mouse")) || (Input.is_action_just_released("right_mouse") && !Input.is_action_pressed("left_mouse")):
if can_handle || Global.undos == Global.undo_redo.get_version(): if (can_handle || Global.undos == Global.undo_redo.get_version()) && Global.current_frame == frame:
if previous_action != "None" && previous_action != "RectSelect": if previous_action != "None" && previous_action != "RectSelect":
handle_redo("Draw") handle_redo("Draw")
print(layers[0][0].data["data"][0])
match current_action: #Handle current tool match current_action: #Handle current tool
"Pencil": "Pencil":

View file

@ -171,6 +171,9 @@ func undo(canvas : Canvas, layer_index : int = -1) -> void:
else: else:
for i in canvas.layers.size(): for i in canvas.layers.size():
canvas.update_texture(i) canvas.update_texture(i)
if action_name == "Scale":
canvas.camera_zoom()
print("Undo: ", action_name) print("Undo: ", action_name)
func redo(canvas : Canvas, layer_index : int = -1) -> void: func redo(canvas : Canvas, layer_index : int = -1) -> void:
@ -183,6 +186,9 @@ func redo(canvas : Canvas, layer_index : int = -1) -> void:
else: else:
for i in canvas.layers.size(): for i in canvas.layers.size():
canvas.update_texture(i) canvas.update_texture(i)
if action_name == "Scale":
canvas.camera_zoom()
print("Redo: ", action_name) print("Redo: ", action_name)
func change_frame() -> void: func change_frame() -> void:

View file

@ -170,18 +170,21 @@ func edit_menu_id_pressed(id : int) -> void:
if used_rect == Rect2(0, 0, 0, 0): if used_rect == Rect2(0, 0, 0, 0):
return return
var width := used_rect.size.x
var height := used_rect.size.y
Global.undos += 1
Global.undo_redo.create_action("Scale")
Global.undo_redo.add_do_property(Global.canvas, "size", Vector2(width, height).floor())
#Loop through all the layers to crop them #Loop through all the layers to crop them
for j in range(Global.canvas.layers.size() - 1, -1, -1): for j in range(Global.canvas.layers.size() - 1, -1, -1):
var sprite := Image.new() var sprite : Image = Global.canvas.layers[j][0].get_rect(used_rect)
sprite = Global.canvas.layers[j][0].get_rect(used_rect) Global.undo_redo.add_do_property(Global.canvas.layers[j][0], "data", sprite.data)
Global.canvas.layers[j][0] = sprite Global.undo_redo.add_undo_property(Global.canvas.layers[j][0], "data", Global.canvas.layers[j][0].data)
Global.canvas.layers[j][0].lock()
Global.canvas.update_texture(j)
var width = Global.canvas.layers[0][0].get_width() Global.undo_redo.add_undo_property(Global.canvas, "size", Global.canvas.size)
var height = Global.canvas.layers[0][0].get_height() Global.undo_redo.add_undo_method(Global, "undo", Global.canvas)
Global.canvas.size = Vector2(width, height).floor() Global.undo_redo.add_do_method(Global, "redo", Global.canvas)
Global.canvas.camera_zoom() Global.undo_redo.commit_action()
4: #Clear selection 4: #Clear selection
Global.canvas.handle_undo("Rectangle Select") Global.canvas.handle_undo("Rectangle Select")
Global.selection_rectangle.polygon[0] = Vector2.ZERO Global.selection_rectangle.polygon[0] = Vector2.ZERO
@ -237,6 +240,7 @@ func _on_CreateNewImage_confirmed() -> void:
Global.canvas.update_texture(0) Global.canvas.update_texture(0)
Global.remove_frame_button.disabled = true Global.remove_frame_button.disabled = true
Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN
Global.undo_redo.clear_history(false)
func _on_OpenSprite_file_selected(path) -> void: func _on_OpenSprite_file_selected(path) -> void:
var file := File.new() var file := File.new()
@ -306,6 +310,8 @@ func _on_OpenSprite_file_selected(path) -> void:
Global.create_brush_button(image) Global.create_brush_button(image)
brush_line = file.get_line() brush_line = file.get_line()
Global.undo_redo.clear_history(false)
file.close() file.close()
func _on_SaveSprite_file_selected(path) -> void: func _on_SaveSprite_file_selected(path) -> void:
@ -394,6 +400,8 @@ func _on_ImportSprites_files_selected(paths) -> void:
Global.remove_frame_button.disabled = true Global.remove_frame_button.disabled = true
Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN
Global.undo_redo.clear_history(false)
func clear_canvases() -> void: func clear_canvases() -> void:
for child in Global.vbox_layer_container.get_children(): for child in Global.vbox_layer_container.get_children():
if child is PanelContainer: if child is PanelContainer:
@ -506,13 +514,11 @@ func _on_ScaleImage_confirmed() -> void:
sprite.resize(width, height, interpolation) sprite.resize(width, height, interpolation)
Global.undo_redo.add_do_property(Global.canvas.layers[i][0], "data", sprite.data) Global.undo_redo.add_do_property(Global.canvas.layers[i][0], "data", sprite.data)
Global.undo_redo.add_undo_property(Global.canvas.layers[i][0], "data", Global.canvas.layers[i][0].data) Global.undo_redo.add_undo_property(Global.canvas.layers[i][0], "data", Global.canvas.layers[i][0].data)
Global.canvas.layers[i][0].lock()
Global.undo_redo.add_undo_property(Global.canvas, "size", Global.canvas.size) Global.undo_redo.add_undo_property(Global.canvas, "size", Global.canvas.size)
Global.undo_redo.add_undo_method(Global, "undo", Global.canvas) Global.undo_redo.add_undo_method(Global, "undo", Global.canvas)
Global.undo_redo.add_do_method(Global, "redo", Global.canvas) Global.undo_redo.add_do_method(Global, "redo", Global.canvas)
Global.undo_redo.commit_action() Global.undo_redo.commit_action()
Global.canvas.camera_zoom()
func add_layer(is_new := true) -> void: func add_layer(is_new := true) -> void:
var new_layer := Image.new() var new_layer := Image.new()