Skip to content

Commit 5bf553f

Browse files
authored
editor_main: Improved undo/redo functionality (#534)
1 parent 23f6bd9 commit 5bf553f

File tree

2 files changed

+21
-62
lines changed

2 files changed

+21
-62
lines changed

[editor]/editor_main/server/saveloadtest_server.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ addEventHandler("newResource", root,
117117
triggerEvent("onNewMap", resourceRoot)
118118
dumpSave()
119119
editor_gui.outputMessage(getPlayerName(client).." started a new map.", root, 255, 0, 0)
120+
121+
actionList = {}
122+
currentActionIndex = 0
120123
end
121124
)
122125

@@ -148,6 +151,9 @@ function handleOpenResource()
148151
setElementCollisionsEnabled(obj, true)
149152
end
150153

154+
actionList = {}
155+
currentActionIndex = 0
156+
151157
triggerEvent("onMapOpened", mapContainer, openingResource)
152158
flattenTreeRuns = 0
153159
triggerClientEvent(root, "saveLoadProgressBar", root, true)
Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
-- make sure events dont get called more than once in a row!
22

33
-- action list
4-
local actionList = {}
5-
local index = 0
6-
7-
-- action states
8-
local justUndid = false
9-
local justRedid = false
10-
local justAdded = false
4+
actionList = {}
5+
currentActionIndex = 0
116

127
-- ADD
138
function insertAction(action) -- messes shit up
149
-- insert action into list, removing any actions that might come after it
15-
if (not justUndid) then
16-
removeActionsAfterIndex(index)
17-
index = index + 1
18-
else
19-
removeActionsAfterIndex(index-1)
20-
end
21-
table.insert(actionList, index, action)
22-
-- update action state flags
23-
justUndid = false
24-
justRedid = false
25-
justAdded = true
10+
while #actionList > currentActionIndex do
11+
table.remove(actionList, #actionList)
12+
end
13+
currentActionIndex = currentActionIndex + 1
14+
table.insert(actionList, currentActionIndex, action)
2615
end
2716

2817
-- create
@@ -67,56 +56,20 @@ function addActionElementPropertiesChange(oldProperties, newProperties)
6756
end
6857
addEventHandler("onElementPropertiesChange_undoredo", root, addActionElementPropertiesChange)
6958

70-
-- removes lowIndex??
71-
function removeActionsAfterIndex(lowIndex)
72-
local curIndex = #actionList
73-
while (curIndex > lowIndex) do
74-
actionList[curIndex]:destructor()
75-
table.remove(actionList, curIndex)
76-
curIndex = curIndex - 1
77-
end
78-
end
79-
8059
function undo()
81-
if (justUndid) then
82-
if (index > 1) then
83-
index = index - 1
84-
actionList[index]:performUndo()
85-
-- update action state flags
86-
justUndid = true
87-
justRedid = false
88-
justAdded = false
89-
end
90-
elseif (justRedid or justAdded) then
91-
actionList[index]:performUndo()
92-
-- update action state flags
93-
justUndid = true
94-
justRedid = false
95-
justAdded = false
96-
end
60+
if currentActionIndex > 0 then
61+
actionList[currentActionIndex]:performUndo()
62+
currentActionIndex = currentActionIndex - 1
63+
end
9764
end
9865
addCommandHandler("undo", undo)
9966
addEventHandler("doUndo", root, undo)
10067

10168
function redo()
102-
if (not justAdded) then
103-
if (justRedid) then
104-
if (actionList[index+1]) then
105-
index = index + 1
106-
actionList[index]:performRedo()
107-
-- update action state flags
108-
justUndid = false
109-
justRedid = true
110-
justAdded = false
111-
end
112-
elseif (justUndid) then
113-
actionList[index]:performRedo()
114-
-- update action state flags
115-
justUndid = false
116-
justRedid = true
117-
justAdded = false
118-
end
119-
end
69+
if currentActionIndex < #actionList then
70+
currentActionIndex = currentActionIndex + 1
71+
actionList[currentActionIndex]:performRedo()
72+
end
12073
end
12174
addCommandHandler("redo", redo)
12275
addEventHandler("doRedo", root, redo)

0 commit comments

Comments
 (0)