Undo stack walking

This commit is contained in:
Luke Hubmayer-Werner 2020-05-22 22:44:49 +09:30
parent 3251e2f03b
commit 117e8eab8e
1 changed files with 31 additions and 10 deletions

View File

@ -122,7 +122,7 @@ func _draw() -> void:
# Undo Stack
var u = len(undo_stack)
for i in u:
draw_string(font, grid_corner(i, cols+1), str(undo_stack[u-i-1]), Color.black)
draw_string(font, grid_corner(i, cols+1), str(undo_stack[u-i-1]), Color.black if i>=undo_preview_pos else Color.gray)
# print(action)
func _process(delta: float) -> void:
@ -272,8 +272,38 @@ func _input(event: InputEvent) -> void:
match event.scancode:
KEY_Z:
undo()
KEY_DOWN:
self.undo_preview_pos += 1
KEY_UP:
self.undo_preview_pos -= 1
var undo_stack = []
var undo_preview_pos := 0 setget set_undo_preview_pos
func do_action(action):
action.activate()
undo_stack.append(action)
func undo():
if !undo_stack.empty():
var action = undo_stack.pop_back()
action.undo()
func set_undo_preview_pos(value):
if value < 0: # Negative is past the end of the stack
value = 0
elif value > len(undo_stack):
value = len(undo_stack)
var old_pos = undo_preview_pos
undo_preview_pos = value
var delta = value - old_pos
if delta > 0: # We are undoing
for i in delta:
undo_stack[-old_pos-i].undo()
elif delta < 0: # We are redoing
for i in -delta:
undo_stack[-old_pos+1+i].activate()
class UndoAction:
var action_type
@ -354,15 +384,6 @@ class UndoActionCell extends UndoAction:
UndoActions.SET_COLOR:
cell_colors.set_cell(cell_row, cell_col, old_state)
func do_action(action):
action.activate()
undo_stack.append(action)
func undo():
if !undo_stack.empty():
var action = undo_stack.pop_back()
action.undo()
func set_cell_color(row, col, new_color):
if new_color != cell_colors.get_cell(row, col):
do_action(UndoActionCell.new(UndoActions.SET_COLOR, row, col, new_color, cell_colors))