fixed layer redo/undo, added redo, fixed duplicate layer

This commit is contained in:
cobrapitz 2020-11-02 03:59:25 +01:00
parent fee893e07d
commit fd5907a8b4
13 changed files with 160 additions and 84 deletions

View File

@ -191,15 +191,11 @@ func duplicate_layer(layer_name: String, new_layer_name: String):
if layer.name == new_layer_name:
return
var dup_layer
for layer in layers:
if layer.name == layer_name:
dup_layer = layer
break
var dup_layer :GELayer = find_layer_by_name(layer_name)
var layer :GELayer = add_new_layer(new_layer_name)
layer.pixels = dup_layer.pixels.duplicate()
layer.name = new_layer_name
for idx in range(dup_layer.pixels.size()):
var pos = GEUtils.to_2D(idx, layer.layer_width)
layer.set_pixel(pos.x, pos.y, dup_layer.pixels[idx])
return layer
@ -281,6 +277,10 @@ func set_pixel(x: int, y: int, color: Color):
_set_pixel(active_layer, x, y, color)
func _set_pixel_v(layer: GELayer, v: Vector2, color: Color):
_set_pixel(layer, v.x, v.y, color)
func _set_pixel(layer: GELayer, x: int, y: int, color: Color):
if not is_inside_canvas(x, y):
return

View File

@ -12,6 +12,7 @@ enum Tools {
BRIGHTEN
COLORPICKER,
CUT,
PASTECUT,
}
var layer_buttons: Control
@ -115,7 +116,7 @@ func _input(event):
if event.scancode == KEY_Z and event.is_pressed() and not event.is_echo():
undo_action()
elif event.scancode == KEY_Y and event.is_pressed() and not event.is_echo():
pass
redo_action()
_handle_zoom(event)
@ -295,6 +296,10 @@ func brush_process():
find_node("ColorPicker").color = selected_color
Tools.CUT:
do_action([cell_mouse_position, last_cell_mouse_position, selected_color])
Tools.PASTECUT:
do_action([cell_mouse_position, last_cell_mouse_position,
_selection_cells, _selection_colors,
_cut_pos, _cut_size])
Tools.RAINBOW:
do_action([cell_mouse_position, last_cell_mouse_position])
update()
@ -354,6 +359,7 @@ func _on_Save_pressed():
func do_action(data: Array):
if _current_action == null:
print("clear redo")
_redo_history.clear()
_current_action.do_action(paint_canvas, data)
@ -366,8 +372,8 @@ func commit_action():
var commit_data = _current_action.commit_action(paint_canvas)
var action = get_action()
action.action_data = _current_action.action_data.duplicate(true)
_actions_history.push_back(action)
_redo_history.clear()
match brush_mode:
Tools.CUT:
@ -375,8 +381,8 @@ func commit_action():
continue
_cut_pos = _current_action.mouse_start_pos
_cut_size = _current_action.mouse_end_pos - _current_action.mouse_start_pos
_selection_cells = _current_action.action_data.do.cells.duplicate()
_selection_colors = _current_action.action_data.do.colors.duplicate()
_selection_cells = _current_action.action_data.redo.cells.duplicate()
_selection_colors = _current_action.action_data.redo.colors.duplicate()
_just_cut = true
_current_action = null
@ -389,13 +395,23 @@ func commit_action():
func redo_action():
pass
if _redo_history.empty():
print("nothing to redo")
return
var action = _redo_history.pop_back()
if not action:
return
_actions_history.append(action)
action.redo_action(paint_canvas)
paint_canvas.update()
print("redo action")
func undo_action():
var action = _actions_history.pop_back()
if not action:
return
return
_redo_history.append(action)
action.undo_action(paint_canvas)
update()
paint_canvas.update()
@ -554,12 +570,9 @@ func remove_active_layer():
layer_buttons.remove_child(_layer_button_ref[layer_name])
_layer_button_ref[layer_name].queue_free()
_layer_button_ref.erase(layer_name)
func duplicate_active_layer():
# copy the last layer button (or the initial one)
var new_layer_button = layer_buttons.get_child(0).duplicate()
layer_buttons.add_child_below_node(
layer_buttons.get_child(layer_buttons.get_child_count() - 1), new_layer_button, true)
@ -568,15 +581,9 @@ func duplicate_active_layer():
new_layer_button.find_node("Select").text = "Layer " + str(_total_added_layers)
var new_layer = paint_canvas.duplicate_layer(paint_canvas.active_layer.name, new_layer_button.name)
new_layer.update_texture()
_layer_button_ref[new_layer.name] = new_layer_button
new_layer_button.find_node("Select").disconnect("pressed", self, "select_layer")
new_layer_button.find_node("Visible").disconnect("pressed", self, "toggle_layer_visibility")
new_layer_button.find_node("Up").disconnect("pressed", self, "move_down")
new_layer_button.find_node("Down").disconnect("pressed", self, "move_up")
new_layer_button.find_node("Lock").disconnect("pressed", self, "lock_layer")
new_layer_button.find_node("Select").connect("pressed", self, "select_layer", [new_layer_button.name])
new_layer_button.find_node("Visible").connect("pressed", self, "toggle_layer_visibility",
[new_layer_button.find_node("Visible"), new_layer_button.name])
@ -584,7 +591,7 @@ func duplicate_active_layer():
new_layer_button.find_node("Down").connect("pressed", self, "move_up", [new_layer_button])
new_layer_button.find_node("Lock").connect("pressed", self, "lock_layer", [new_layer_button, new_layer_button.name])
print("added layer: ", new_layer.name, " (total:", layer_buttons.size(), ")")
print("added layer: ", new_layer.name, " (total:", layer_buttons.get_child_count(), ")")
func move_up(layer_btn):

View File

@ -6,15 +6,15 @@ var action_data = {}
func _init():
action_data["do"] = {}
action_data["redo"] = {}
action_data["undo"] = {}
action_data["preview"] = {}
func do_action(canvas, data: Array):
if not "cells" in action_data.do:
action_data.do["cells"] = []
action_data.do["colors"] = []
if not "cells" in action_data.redo:
action_data.redo["cells"] = []
action_data.redo["colors"] = []
if not "cells" in action_data.undo:
action_data.undo["cells"] = []
@ -24,9 +24,8 @@ func do_action(canvas, data: Array):
action_data.preview["cells"] = []
action_data.preview["colors"] = []
if "layer" in action_data.do:
action_data.do["layer"] = canvas.active_layer
action_data.undo["layer"] = canvas.active_layer
if not "layer" in action_data:
action_data["layer"] = canvas.active_layer
func commit_action(canvas):
@ -38,7 +37,11 @@ func undo_action(canvas):
print("NO IMPL undo_action ")
func redo_action(canvas):
print("NO IMPL redo_action ")
func can_commit() -> bool:
return not action_data.do.empty()
return not action_data.redo.empty()

View File

@ -14,8 +14,8 @@ func do_action(canvas, data: Array):
var brightened_color = canvas.get_pixel_v(pixel).lightened(0.1)
canvas.set_pixel_v(pixel, brightened_color)
action_data.do.cells.append(pixel)
action_data.do.colors.append(brightened_color)
action_data.redo.cells.append(pixel)
action_data.redo.colors.append(brightened_color)
continue
action_data.undo.colors.append(canvas.get_pixel_v(pixel))
@ -23,21 +23,26 @@ func do_action(canvas, data: Array):
var brightened_color = canvas.get_pixel_v(pixel).lightened(0.1)
canvas.set_pixel_v(pixel, brightened_color)
action_data.do.cells.append(pixel)
action_data.do.colors.append(brightened_color)
action_data.redo.cells.append(pixel)
action_data.redo.colors.append(brightened_color)
func commit_action(canvas):
var cells = action_data.do.cells
var colors = action_data.do.colors
var cells = action_data.redo.cells
var colors = action_data.redo.colors
return []
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -17,21 +17,29 @@ func do_action(canvas: GECanvas, data: Array):
canvas.set_pixel_v(p, data[2])
action_data.do.cells.append(p)
action_data.do.colors.append(data[2])
action_data.redo.cells.append(p)
action_data.redo.colors.append(data[2])
func commit_action(canvas):
var cells = action_data.do.cells
var colors = action_data.do.colors
var cells = action_data.redo.cells
var colors = action_data.redo.colors
return []
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -19,8 +19,8 @@ func do_action(canvas, data: Array):
canvas.set_pixel_v(pixel, data[2])
action_data.do.cells.append(pixel)
action_data.do.colors.append(data[2])
action_data.redo.cells.append(pixel)
action_data.redo.colors.append(data[2])
func commit_action(canvas):
@ -32,8 +32,16 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -50,8 +50,8 @@ func commit_action(canvas):
if color == Color.transparent:
continue
action_data.do.cells.append(pos)
action_data.do.colors.append(canvas.get_pixel_v(pos))
action_data.redo.cells.append(pos)
action_data.redo.colors.append(canvas.get_pixel_v(pos))
canvas.set_pixel_v(pos, Color.transparent)
@ -63,8 +63,16 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -13,8 +13,8 @@ func do_action(canvas, data: Array):
var darkened_color = canvas.get_pixel_v(pixel).darkened(dark_factor)
canvas.set_pixel_v(pixel, darkened_color)
action_data.do.cells.append(pixel)
action_data.do.colors.append(darkened_color)
action_data.redo.cells.append(pixel)
action_data.redo.colors.append(darkened_color)
continue
action_data.undo.colors.append(canvas.get_pixel_v(pixel))
@ -22,21 +22,29 @@ func do_action(canvas, data: Array):
var darkened_color = canvas.get_pixel_v(pixel).darkened(dark_factor)
canvas.set_pixel_v(pixel, darkened_color)
action_data.do.cells.append(pixel)
action_data.do.colors.append(darkened_color)
action_data.redo.cells.append(pixel)
action_data.redo.colors.append(darkened_color)
func commit_action(canvas):
var cells = action_data.do.cells
var colors = action_data.do.colors
var cells = action_data.redo.cells
var colors = action_data.redo.colors
return []
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -31,8 +31,8 @@ func commit_action(canvas):
canvas.set_pixel_v(cells[idx], colors[idx])
action_data.do.cells.append(cells[idx])
action_data.do.colors.append(colors[idx])
action_data.redo.cells.append(cells[idx])
action_data.redo.colors.append(colors[idx])
mouse_start_pos = null
return []
@ -40,8 +40,16 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -9,27 +9,33 @@ func do_action(canvas, data: Array):
for pixel in pixels:
if pixel in action_data.undo.cells or canvas.get_pixel_v(pixel) == null:
continue
action_data.undo.colors.append(canvas.get_pixel_v(pixel))
action_data.undo.cells.append(pixel)
canvas.set_pixel_v(pixel, data[2])
action_data.do.cells.append(pixel)
action_data.do.colors.append(data[2])
action_data.redo.cells.append(pixel)
action_data.redo.colors.append(data[2])
func commit_action(canvas):
var cells = action_data.do.cells
var colors = action_data.do.colors
var cells = action_data.redo.cells
var colors = action_data.redo.colors
return []
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -11,12 +11,12 @@ func do_action(canvas, data: Array):
var color = GEUtils.random_color()
canvas.set_pixel_v(pixel, color)
var idx = action_data.do.cells.find(pixel)
action_data.do.cells.remove(idx)
action_data.do.colors.remove(idx)
var idx = action_data.redo.cells.find(pixel)
action_data.redo.cells.remove(idx)
action_data.redo.colors.remove(idx)
action_data.do.cells.append(pixel)
action_data.do.colors.append(color)
action_data.redo.cells.append(pixel)
action_data.redo.colors.append(color)
continue
action_data.undo.colors.append(canvas.get_pixel_v(pixel))
@ -25,21 +25,29 @@ func do_action(canvas, data: Array):
var color = GEUtils.random_color()
canvas.set_pixel_v(pixel, color)
action_data.do.cells.append(pixel)
action_data.do.colors.append(color)
action_data.redo.cells.append(pixel)
action_data.redo.colors.append(color)
func commit_action(canvas):
var cells = action_data.do.cells
var colors = action_data.do.colors
var cells = action_data.redo.cells
var colors = action_data.redo.colors
return []
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -38,11 +38,10 @@ func commit_action(canvas):
var cells = action_data.preview.cells
var colors = action_data.preview.colors
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
action_data.do.cells.append(cells[idx])
action_data.do.colors.append(colors[idx])
action_data.redo.cells.append(cells[idx])
action_data.redo.colors.append(colors[idx])
mouse_start_pos = null
return []
@ -50,8 +49,16 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])
func redo_action(canvas):
var cells = action_data.redo.cells
var colors = action_data.redo.colors
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -42,7 +42,7 @@ _global_script_classes=[ {
"base": "GEAction",
"class": "GECut",
"language": "GDScript",
"path": "res://addons/graphics_editor/actions/Cut.gd"
"path": "res://addons/graphics_editor/actions/PasteCut.gd"
}, {
"base": "GEAction",
"class": "GEDarken",