Fixed undo with fill tool

This commit is contained in:
LDDestroier 2018-10-26 13:39:21 -04:00 committed by GitHub
parent d02ee82e20
commit 9d390e753a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 108 additions and 107 deletions

215
pain.lua
View File

@ -1457,8 +1457,8 @@ end
local fillTool = function(_frame,cx,cy,dot) -- "_frame" is the frame NUMBER local fillTool = function(_frame,cx,cy,dot) -- "_frame" is the frame NUMBER
local maxX, maxY = 0, 0 local maxX, maxY = 0, 0
local minX, minY = 0, 0 local minX, minY = 0, 0
paintEncoded = clearAllRedundant(paintEncoded) paintEncoded = clearAllRedundant(paintEncoded)
local frame = paintEncoded[_frame] local frame = paintEncoded[_frame]
local scx, scy = cx+paint.scrollX, cy+paint.scrollY local scx, scy = cx+paint.scrollX, cy+paint.scrollY
local output = {} local output = {}
for a = 1, #frame do for a = 1, #frame do
@ -1467,134 +1467,135 @@ local fillTool = function(_frame,cx,cy,dot) -- "_frame" is the frame NUMBER
minX = math.min(minX, frame[a].x) minX = math.min(minX, frame[a].x)
minY = math.min(minY, frame[a].y) minY = math.min(minY, frame[a].y)
end end
maxX = math.max(maxX, scx) maxX = math.max(maxX, scx)
maxY = math.max(maxY, scy) maxY = math.max(maxY, scy)
minX = math.min(minX, scx) minX = math.min(minX, scx)
minY = math.min(minY, scy) minY = math.min(minY, scy)
maxX = math.max(maxX, screenEdges[1]) maxX = math.max(maxX, screenEdges[1])
maxY = math.max(maxY, screenEdges[2]) maxY = math.max(maxY, screenEdges[2])
local doop = {} local doop = {}
local touched = {} local touched = {}
local check = {[scy] = {[scx] = true}} local check = {[scy] = {[scx] = true}}
for y = minY, maxY do for y = minY, maxY do
doop[y] = {} doop[y] = {}
touched[y] = {} touched[y] = {}
for x = minX, maxX do for x = minX, maxX do
doop[y][x] = { doop[y][x] = {
c = " ", c = " ",
b = 0, b = 0,
t = 0 t = 0
} }
touched[y][x] = false touched[y][x] = false
end end
end end
for a = 1, #frame do for a = 1, #frame do
doop[frame[a].y][frame[a].x] = { doop[frame[a].y][frame[a].x] = {
c = frame[a].c, c = frame[a].c,
t = frame[a].t, t = frame[a].t,
b = frame[a].b b = frame[a].b
} }
end end
local initDot = { local initDot = {
c = doop[scy][scx].c, c = doop[scy][scx].c,
t = doop[scy][scx].t, t = doop[scy][scx].t,
b = doop[scy][scx].b b = doop[scy][scx].b
} }
local chkpos = function(x, y, checkList) local chkpos = function(x, y, checkList)
if (x < minX or x > maxX) or (y < minY or y > maxY) then if (x < minX or x > maxX) or (y < minY or y > maxY) then
return false return false
else else
if (doop[y][x].b ~= initDot.b) or (doop[y][x].t ~= initDot.t) or (doop[y][x].c ~= initDot.c) then if (doop[y][x].b ~= initDot.b) or (doop[y][x].t ~= initDot.t) or (doop[y][x].c ~= initDot.c) then
return false return false
end end
if check[y] then if check[y] then
if check[y][x] then if check[y][x] then
return false return false
end end
end end
if touched[y][x] then if touched[y][x] then
return false return false
end end
return true return true
end end
end end
local doBreak local doBreak
local step = 0 local step = 0
while true do while true do
doBreak = true doBreak = true
for chY, v in pairs(check) do for chY, v in pairs(check) do
for chX, isTrue in pairs(v) do for chX, isTrue in pairs(v) do
if isTrue and (not touched[chY][chX]) then if isTrue and (not touched[chY][chX]) then
step = step + 1 step = step + 1
if doFillAnimation then if doFillAnimation then
if (chX-paint.scrollX >= 1 and chX-paint.scrollX <= scr_x and chY-paint.scrollY >= 1 and chY-paint.scrollY <= scr_y) then if (chX-paint.scrollX >= 1 and chX-paint.scrollX <= scr_x and chY-paint.scrollY >= 1 and chY-paint.scrollY <= scr_y) then
reRenderPAIN() reRenderPAIN()
end end
end end
frame[#frame+1] = { frame[#frame+1] = {
x = chX, x = chX,
y = chY, y = chY,
c = dot.c, c = dot.c,
t = dot.t, t = dot.t,
b = dot.b b = dot.b
} }
touched[chY][chX] = true touched[chY][chX] = true
-- check adjacent -- check adjacent
if chkpos(chX+1, chY) then if chkpos(chX+1, chY) then
check[chY][chX+1] = true check[chY][chX+1] = true
doBreak = false doBreak = false
end end
if chkpos(chX-1, chY) then if chkpos(chX-1, chY) then
check[chY][chX-1] = true check[chY][chX-1] = true
doBreak = false doBreak = false
end end
if chkpos(chX, chY+1) then if chkpos(chX, chY+1) then
check[chY+1] = check[chY+1] or {} check[chY+1] = check[chY+1] or {}
check[chY+1][chX] = true check[chY+1][chX] = true
doBreak = false doBreak = false
end end
if chkpos(chX, chY-1) then if chkpos(chX, chY-1) then
check[chY-1] = check[chY-1] or {} check[chY-1] = check[chY-1] or {}
check[chY-1][chX] = true check[chY-1][chX] = true
doBreak = false doBreak = false
end end
-- check diagonal -- check diagonal
if doFillDiagonal then if doFillDiagonal then
if chkpos(chX-1, chY-1) then if chkpos(chX-1, chY-1) then
check[chY-1] = check[chY-1] or {} check[chY-1] = check[chY-1] or {}
check[chY-1][chX-1] = true check[chY-1][chX-1] = true
doBreak = false doBreak = false
end end
if chkpos(chX+1, chY-1) then if chkpos(chX+1, chY-1) then
check[chY-1] = check[chY-1] or {} check[chY-1] = check[chY-1] or {}
check[chY-1][chX+1] = true check[chY-1][chX+1] = true
doBreak = false doBreak = false
end end
if chkpos(chX-1, chY+1) then if chkpos(chX-1, chY+1) then
check[chY+1] = check[chY+1] or {} check[chY+1] = check[chY+1] or {}
check[chY+1][chX-1] = true check[chY+1][chX-1] = true
doBreak = false doBreak = false
end end
if chkpos(chX+1, chY+1) then if chkpos(chX+1, chY+1) then
check[chY+1] = check[chY+1] or {} check[chY+1] = check[chY+1] or {}
check[chY+1][chX+1] = true check[chY+1][chX+1] = true
doBreak = false doBreak = false
end end
end end
if step % 1024 == 0 then -- tries to prevent crash if step % 1024 == 0 then -- tries to prevent crash
sleep(0) sleep(0)
end end
end end
end end
end end
if doBreak then if doBreak then
break break
end end
end end
paintEncoded = clearAllRedundant(paintEncoded) paintEncoded = clearAllRedundant(paintEncoded)
saveToUndoBuffer()
end end
local boxCharSelector = function() local boxCharSelector = function()