Skip to content

Commit

Permalink
editor_main: Improved undo/redo functionality (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
q8X committed Aug 14, 2024
1 parent 23f6bd9 commit 5bf553f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 62 deletions.
6 changes: 6 additions & 0 deletions [editor]/editor_main/server/saveloadtest_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ addEventHandler("newResource", root,
triggerEvent("onNewMap", resourceRoot)
dumpSave()
editor_gui.outputMessage(getPlayerName(client).." started a new map.", root, 255, 0, 0)

actionList = {}
currentActionIndex = 0
end
)

Expand Down Expand Up @@ -148,6 +151,9 @@ function handleOpenResource()
setElementCollisionsEnabled(obj, true)
end

actionList = {}
currentActionIndex = 0

triggerEvent("onMapOpened", mapContainer, openingResource)
flattenTreeRuns = 0
triggerClientEvent(root, "saveLoadProgressBar", root, true)
Expand Down
77 changes: 15 additions & 62 deletions [editor]/editor_main/server/undoredo_main.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
-- make sure events dont get called more than once in a row!

-- action list
local actionList = {}
local index = 0

-- action states
local justUndid = false
local justRedid = false
local justAdded = false
actionList = {}
currentActionIndex = 0

-- ADD
function insertAction(action) -- messes shit up
-- insert action into list, removing any actions that might come after it
if (not justUndid) then
removeActionsAfterIndex(index)
index = index + 1
else
removeActionsAfterIndex(index-1)
end
table.insert(actionList, index, action)
-- update action state flags
justUndid = false
justRedid = false
justAdded = true
while #actionList > currentActionIndex do
table.remove(actionList, #actionList)
end
currentActionIndex = currentActionIndex + 1
table.insert(actionList, currentActionIndex, action)
end

-- create
Expand Down Expand Up @@ -67,56 +56,20 @@ function addActionElementPropertiesChange(oldProperties, newProperties)
end
addEventHandler("onElementPropertiesChange_undoredo", root, addActionElementPropertiesChange)

-- removes lowIndex??
function removeActionsAfterIndex(lowIndex)
local curIndex = #actionList
while (curIndex > lowIndex) do
actionList[curIndex]:destructor()
table.remove(actionList, curIndex)
curIndex = curIndex - 1
end
end

function undo()
if (justUndid) then
if (index > 1) then
index = index - 1
actionList[index]:performUndo()
-- update action state flags
justUndid = true
justRedid = false
justAdded = false
end
elseif (justRedid or justAdded) then
actionList[index]:performUndo()
-- update action state flags
justUndid = true
justRedid = false
justAdded = false
end
if currentActionIndex > 0 then
actionList[currentActionIndex]:performUndo()
currentActionIndex = currentActionIndex - 1
end
end
addCommandHandler("undo", undo)
addEventHandler("doUndo", root, undo)

function redo()
if (not justAdded) then
if (justRedid) then
if (actionList[index+1]) then
index = index + 1
actionList[index]:performRedo()
-- update action state flags
justUndid = false
justRedid = true
justAdded = false
end
elseif (justUndid) then
actionList[index]:performRedo()
-- update action state flags
justUndid = false
justRedid = true
justAdded = false
end
end
if currentActionIndex < #actionList then
currentActionIndex = currentActionIndex + 1
actionList[currentActionIndex]:performRedo()
end
end
addCommandHandler("redo", redo)
addEventHandler("doRedo", root, redo)

0 comments on commit 5bf553f

Please sign in to comment.