“退回到上一步”的功能,大體思路是這樣的:將每一次涂色后的相關(guān)數(shù)據(jù)壓棧,點擊回退時,從棧中取出數(shù)據(jù),根據(jù)這些數(shù)據(jù)恢復(fù)上一次的樣子。思路很簡單,可是,壓入棧中的數(shù)據(jù)應(yīng)該是什么呢?我這里壓入的是鼠標(biāo)點擊的點坐標(biāo)和這個點所在的封閉區(qū)域的顏色值,坐標(biāo)好辦,但是如何知道顏色值呢?quick-cocos-2d-x并沒有提供獲取當(dāng)前點的像素值的接口,這就需要一個table來標(biāo)記每一個區(qū)域的顏色值了。那么這樣一來,首先需要做的是將圖像分割成一個個區(qū)域,給每個區(qū)域分配一個ID。創(chuàng)建一個markRegionTable用來記錄每一個區(qū)域當(dāng)前的顏色。當(dāng)點擊某某個區(qū)域時,先獲取該區(qū)域的ID,然后在markRegionTable[ID]中獲取顏色,如果顏色跟當(dāng)前畫筆的顏色不同,則將該顏色壓棧,并將markRegionTable[ID]的值修改為當(dāng)前畫筆的顏色值?;赝藭r,取棧頂元素,得到坐標(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