“退回到上一步”的功能恢准,大體思路是這樣的:將每一次涂色后的相關(guān)數(shù)據(jù)壓棧,點(diǎn)擊回退時(shí)甫题,從棧中取出數(shù)據(jù)馁筐,根據(jù)這些數(shù)據(jù)恢復(fù)上一次的樣子。思路很簡(jiǎn)單坠非,可是敏沉,壓入棧中的數(shù)據(jù)應(yīng)該是什么呢?我這里壓入的是鼠標(biāo)點(diǎn)擊的點(diǎn)坐標(biāo)和這個(gè)點(diǎn)所在的封閉區(qū)域的顏色值,坐標(biāo)好辦盟迟,但是如何知道顏色值呢秋泳?quick-cocos-2d-x并沒有提供獲取當(dāng)前點(diǎn)的像素值的接口,這就需要一個(gè)table來(lái)標(biāo)記每一個(gè)區(qū)域的顏色值了攒菠。那么這樣一來(lái)迫皱,首先需要做的是將圖像分割成一個(gè)個(gè)區(qū)域,給每個(gè)區(qū)域分配一個(gè)ID辖众。創(chuàng)建一個(gè)markRegionTable用來(lái)記錄每一個(gè)區(qū)域當(dāng)前的顏色卓起。當(dāng)點(diǎn)擊某某個(gè)區(qū)域時(shí),先獲取該區(qū)域的ID凹炸,然后在markRegionTable[ID]中獲取顏色戏阅,如果顏色跟當(dāng)前畫筆的顏色不同,則將該顏色壓棧啤它,并將markRegionTable[ID]的值修改為當(dāng)前畫筆的顏色值奕筐。回退時(shí)蚕键,取棧頂元素救欧,得到坐標(biāo)和顏色值衰粹,根據(jù)坐標(biāo)給所在區(qū)域涂上該顏色就行了锣光。
入棧邏輯大致如下:
local regionId = region_data[x][y] --求出區(qū)域ID
markRegionTable = markRegionTable or {}
if not regionId then return end
if markRegionTable[regionId] == nil then
???? Stack.push(colorMsgStack, {{x = x, y = y}, 1})
? ? markRegionTable[regionId] = drawLayer._selectedColor?? --drawLayer._selectedColor為畫筆顏色
elseif markRegionTable[regionId] ~= drawLayer._selectedColor then
???? Stack.push(colorMsgStack, {{x = x, y = y}, markRegionTable[regionId]})
????? markRegionTable[regionId] = drawLayer._selectedColor
end
出棧邏輯大致如下:
if event.colorMsg == nil then
????? return
end
local x = event.colorMsg[1].x
local y = event.colorMsg[1].y
local regionId = region_data[x][imgTop - y]
local color = event.colorMsg[2]
if not regionId then return end
drawLayer:setColor(color, drawLayer:getRowAndCol(color))
drawLayer:fillColor(x, y)
markRegionTable[regionId] = color