Fix off-by-one errors in undo stack

This commit is contained in:
Luke Hubmayer-Werner 2020-05-22 23:12:11 +09:30
parent 117e8eab8e
commit c5232db66e
1 changed files with 13 additions and 4 deletions

View File

@ -280,14 +280,23 @@ func _input(event: InputEvent) -> void:
var undo_stack = [] var undo_stack = []
var undo_preview_pos := 0 setget set_undo_preview_pos var undo_preview_pos := 0 setget set_undo_preview_pos
func apply_undo_preview():
undo_stack.resize(len(undo_stack)-undo_preview_pos)
undo_preview_pos = 0
func do_action(action): func do_action(action):
if undo_preview_pos > 0:
apply_undo_preview()
action.activate() action.activate()
undo_stack.append(action) undo_stack.append(action)
func undo(): func undo():
if !undo_stack.empty(): if !undo_stack.empty():
var action = undo_stack.pop_back() if undo_preview_pos > 0:
action.undo() apply_undo_preview()
else:
var action = undo_stack.pop_back()
action.undo()
func set_undo_preview_pos(value): func set_undo_preview_pos(value):
if value < 0: # Negative is past the end of the stack if value < 0: # Negative is past the end of the stack
@ -300,10 +309,10 @@ func set_undo_preview_pos(value):
var delta = value - old_pos var delta = value - old_pos
if delta > 0: # We are undoing if delta > 0: # We are undoing
for i in delta: for i in delta:
undo_stack[-old_pos-i].undo() undo_stack[-old_pos-1-i].undo()
elif delta < 0: # We are redoing elif delta < 0: # We are redoing
for i in -delta: for i in -delta:
undo_stack[-old_pos+1+i].activate() undo_stack[-old_pos+i].activate()
class UndoAction: class UndoAction:
var action_type var action_type