1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-31 07:29:49 +00:00

UndoRedo vol 8 - Draw while animating should now work with UndoRedo

This commit is contained in:
OverloadedOrama 2019-11-05 18:19:41 +02:00
parent 8bc0879814
commit 40e0978b3f
4 changed files with 51 additions and 32 deletions

View file

@ -117,7 +117,6 @@ func _process(delta) -> void:
if (can_handle || Global.undos == Global.undo_redo.get_version()) && Global.current_frame == frame: 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":
@ -242,28 +241,46 @@ func _process(delta) -> void:
update_texture(current_layer_index) update_texture(current_layer_index)
func handle_undo(action : String) -> void: func handle_undo(action : String) -> void:
var canvases := []
var animation_timer := $"../../../../../../AnimationTimer"
var layer_index := -1
if animation_timer.is_stopped(): #if we're not animating, store only the current canvas
canvases = [self]
layer_index = current_layer_index
else: #If we're animating, store all canvases
canvases = Global.canvases
Global.undos += 1 Global.undos += 1
Global.undo_redo.create_action(action)
for c in canvases:
#I'm not sure why I have to unlock it, but... #I'm not sure why I have to unlock it, but...
#...if I don't, it doesn't work properly #...if I don't, it doesn't work properly
layers[current_layer_index][0].unlock() c.layers[c.current_layer_index][0].unlock()
var data = layers[current_layer_index][0].data var data = c.layers[c.current_layer_index][0].data
layers[current_layer_index][0].lock() c.layers[c.current_layer_index][0].lock()
Global.undo_redo.create_action(action) Global.undo_redo.add_undo_property(c.layers[c.current_layer_index][0], "data", data)
Global.undo_redo.add_undo_property(layers[current_layer_index][0], "data", data)
if action == "Rectangle Select": if action == "Rectangle Select":
var selected_pixels = Global.selected_pixels.duplicate() var selected_pixels = Global.selected_pixels.duplicate()
Global.undo_redo.add_undo_property(Global.selection_rectangle, "polygon", Global.selection_rectangle.polygon) Global.undo_redo.add_undo_property(Global.selection_rectangle, "polygon", Global.selection_rectangle.polygon)
Global.undo_redo.add_undo_property(Global, "selected_pixels", selected_pixels) Global.undo_redo.add_undo_property(Global, "selected_pixels", selected_pixels)
Global.undo_redo.add_undo_method(Global, "undo", self, current_layer_index) Global.undo_redo.add_undo_method(Global, "undo", canvases, layer_index)
func handle_redo(action : String) -> void: func handle_redo(action : String) -> void:
if Global.undos < Global.undo_redo.get_version(): if Global.undos < Global.undo_redo.get_version():
return return
Global.undo_redo.add_do_property(layers[current_layer_index][0], "data", layers[current_layer_index][0].data) var canvases := []
var animation_timer := $"../../../../../../AnimationTimer"
var layer_index := -1
if animation_timer.is_stopped():
canvases = [self]
layer_index = current_layer_index
else:
canvases = Global.canvases
for c in canvases:
Global.undo_redo.add_do_property(c.layers[c.current_layer_index][0], "data", c.layers[c.current_layer_index][0].data)
if action == "Rectangle Select": if action == "Rectangle Select":
Global.undo_redo.add_do_property(Global.selection_rectangle, "polygon", Global.selection_rectangle.polygon) Global.undo_redo.add_do_property(Global.selection_rectangle, "polygon", Global.selection_rectangle.polygon)
Global.undo_redo.add_do_property(Global, "selected_pixels", Global.selected_pixels) Global.undo_redo.add_do_property(Global, "selected_pixels", Global.selected_pixels)
Global.undo_redo.add_do_method(Global, "redo", self, current_layer_index) Global.undo_redo.add_do_method(Global, "redo", canvases, layer_index)
Global.undo_redo.commit_action() Global.undo_redo.commit_action()
print("Do: ", Global.undo_redo.get_current_action_name()) print("Do: ", Global.undo_redo.get_current_action_name())

View file

@ -162,33 +162,35 @@ func find_node_by_name(root, node_name) -> Node:
return found return found
return null return null
func undo(canvas : Canvas, layer_index : int = -1) -> void: func undo(canvases : Array, layer_index : int = -1) -> void:
undos -= 1 undos -= 1
var action_name : String = undo_redo.get_current_action_name() var action_name : String = undo_redo.get_current_action_name()
if action_name == "Draw" || action_name == "Rectangle Select" || action_name == "Scale": if action_name == "Draw" || action_name == "Rectangle Select" || action_name == "Scale":
for c in canvases:
if layer_index > -1: if layer_index > -1:
canvas.update_texture(layer_index) c.update_texture(layer_index)
else: else:
for i in canvas.layers.size(): for i in c.layers.size():
canvas.update_texture(i) c.update_texture(i)
if action_name == "Scale": if action_name == "Scale":
canvas.camera_zoom() c.camera_zoom()
print("Undo: ", action_name) print("Undo: ", action_name)
func redo(canvas : Canvas, layer_index : int = -1) -> void: func redo(canvases : Array, layer_index : int = -1) -> void:
if undos < undo_redo.get_version(): #If we did undo and then redo if undos < undo_redo.get_version(): #If we did undo and then redo
undos = undo_redo.get_version() undos = undo_redo.get_version()
var action_name : String = undo_redo.get_current_action_name() var action_name : String = undo_redo.get_current_action_name()
if action_name == "Draw" || action_name == "Rectangle Select" || action_name == "Scale": if action_name == "Draw" || action_name == "Rectangle Select" || action_name == "Scale":
for c in canvases:
if layer_index > -1: if layer_index > -1:
canvas.update_texture(layer_index) c.update_texture(layer_index)
else: else:
for i in canvas.layers.size(): for i in c.layers.size():
canvas.update_texture(i) c.update_texture(i)
if action_name == "Scale": if action_name == "Scale":
canvas.camera_zoom() c.camera_zoom()
print("Redo: ", action_name) print("Redo: ", action_name)
func change_frame() -> void: func change_frame() -> void:

View file

@ -6,7 +6,7 @@ var currently_selected := false
var visibility_toggled := false var visibility_toggled := false
func _ready() -> void: func _ready() -> void:
var stylebox = StyleBoxFlat.new() var stylebox := StyleBoxFlat.new()
stylebox.bg_color = Color("3d3b45") stylebox.bg_color = Color("3d3b45")
add_stylebox_override("panel", stylebox) add_stylebox_override("panel", stylebox)
changed_selection() changed_selection()

View file

@ -182,8 +182,8 @@ func edit_menu_id_pressed(id : int) -> void:
Global.undo_redo.add_undo_property(Global.canvas.layers[j][0], "data", Global.canvas.layers[j][0].data) Global.undo_redo.add_undo_property(Global.canvas.layers[j][0], "data", Global.canvas.layers[j][0].data)
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()
4: #Clear selection 4: #Clear selection
Global.canvas.handle_undo("Rectangle Select") Global.canvas.handle_undo("Rectangle Select")
@ -516,8 +516,8 @@ func _on_ScaleImage_confirmed() -> void:
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.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()
func add_layer(is_new := true) -> void: func add_layer(is_new := true) -> void: