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_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):
if undo_preview_pos > 0:
apply_undo_preview()
action.activate()
undo_stack.append(action)
func undo():
if !undo_stack.empty():
var action = undo_stack.pop_back()
action.undo()
if undo_preview_pos > 0:
apply_undo_preview()
else:
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
@ -300,10 +309,10 @@ func set_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()
undo_stack[-old_pos-1-i].undo()
elif delta < 0: # We are redoing
for i in -delta:
undo_stack[-old_pos+1+i].activate()
undo_stack[-old_pos+i].activate()
class UndoAction:
var action_type