优化场地绘制交互

This commit is contained in:
MrZ626
2021-03-29 17:54:27 +08:00
parent 7c2ab421e9
commit 6b0f60d859

View File

@@ -1,5 +1,5 @@
local gc,sys=love.graphics,love.system local gc,sys=love.graphics,love.system
local ms,kb=love.mouse,love.keyboard local kb=love.keyboard
local max,min,int=math.max,math.min,math.floor local max,min,int=math.max,math.min,math.floor
local ins,rem=table.insert,table.remove local ins,rem=table.insert,table.remove
@@ -9,7 +9,8 @@ local FIELD=FIELD
local scene={} local scene={}
local sure local sure
local pen--Pen type local penColor--Pen color
local penMode--Pen mode (false=unavailable, else=mouse button)
local penX,penY--Pen position local penX,penY--Pen position
local demo--If show x local demo--If show x
local page local page
@@ -54,6 +55,7 @@ local minoPosCode={
local SPmode local SPmode
local SPlist={}--Smart pen path list local SPlist={}--Smart pen path list
local function SPpath(x,y) local function SPpath(x,y)
if not penMode then return end
for i=1,#SPlist do for i=1,#SPlist do
if x==SPlist[i][1]and y==SPlist[i][2]then if x==SPlist[i][1]and y==SPlist[i][2]then
return return
@@ -102,95 +104,89 @@ end
function scene.sceneInit() function scene.sceneInit()
sure=0 sure=0
pen=1 penColor=1
penMode=false
penX,penY=1,1 penX,penY=1,1
demo=false demo=false
page=1 page=1
love.keyboard.setKeyRepeat(false)
end
function scene.sceneBack()
love.keyboard.setKeyRepeat(true)
end end
function scene.mouseMove(x,y) function scene.mouseMove(x,y)
local sx,sy=int((x-200)/30)+1,20-int((y-60)/30) local sx,sy=int((x-200)/30)+1,20-int((y-60)/30)
if sx<1 or sx>10 then sx=nil end if sx>=1 and sx<=10 and sy>=1 and sy<=20 then
if sy<1 or sy>20 then sy=nil end penX,penY=sx,sy
penX,penY=sx,sy if penMode then
if ms.isDown(1,2,3)then if penColor==-2 and penMode==1 then
if sx and sy then SPpath(sx,sy)
if pen==-2 then
if ms.isDown(1)then
SPpath(sx,sy)
else
FIELD[page][sy][sx]=-1
end
else else
FIELD[page][sy][sx]= FIELD[page][sy][sx]=
ms.isDown(1)and pen or penMode==1 and penColor or
ms.isDown(2)and -1 penMode==2 and -1 or
or 0 -- penMode==3 and 0
0
end end
end end
else
penX,penY=nil
end end
end end
function scene.mouseDown(x,y,k) function scene.mouseDown(x,y,k)
if k==2 and pen==-2 then if not penMode then
SPlist={} penMode=k
else elseif penMode~=k then
scene.mouseMove(x,y) penMode=false
if penColor==-2 then
SPlist={}
end
end end
end
function scene.mouseUp()
if pen==-2 then SPdraw()end
end
function scene.wheelMoved(_,y)
if y<0 then
pen=pen+1
if pen==25 then pen=1 end
else
pen=pen-1
if pen==0 then pen=24 end
end
end
function scene.touchDown(x,y)
scene.mouseMove(x,y) scene.mouseMove(x,y)
end end
function scene.touchMove(x,y) function scene.mouseUp(_,_,k)
local sx,sy=int((x-200)/30)+1,20-int((y-60)/30) if penMode==k then
if sx<1 or sx>10 then sx=nil end penMode=false
if sy<1 or sy>20 then sy=nil end if penColor==-2 then
penX,penY=sx,sy SPdraw()
if sx and sy then
if pen==-2 then
SPpath(sx,sy)
else
FIELD[page][sy][sx]=pen
end end
end end
end end
scene.touchUp=scene.mouseUp
function scene.wheelMoved(_,y)
if penColor>0 then
if y<0 then
penColor=penColor+1
if penColor==25 then penColor=1 end
else
penColor=penColor-1
if penColor==0 then penColor=24 end
end
end
end
function scene.touchDown(x,y)scene.mouseDown(x,y,1)end
function scene.touchMove(x,y)scene.mouseMove(x,y)end
function scene.touchUp(x,y)scene.mouseUp(x,y,1)end
function scene.keyDown(key) function scene.keyDown(key)
local sx,sy=penX,penY
if key=="up"or key=="down"or key=="left"or key=="right"then if key=="up"or key=="down"or key=="left"or key=="right"then
if not sx then sx=1 end if not penX or not penY then penX,penY=1,1 end
if not sy then sy=1 end if key=="up"then
if key=="up"and sy<20 then sy=sy+1 if penY<20 then penY=penY+1 end
elseif key=="down"and sy>1 then sy=sy-1 elseif key=="down"then
elseif key=="left"and sx>1 then sx=sx-1 if penY>1 then penY=penY-1 end
elseif key=="right"and sx<10 then sx=sx+1 elseif key=="left"then
if penX>1 then penX=penX-1 end
elseif key=="right"then
if penX<10 then penX=penX+1 end
end end
if kb.isDown("space")then if kb.isDown("space")then
scene.keyDown("space") scene.keyDown("space")
end end
elseif key=="space"then elseif key=="space"then
if sx and sy then if penX and penY then
if pen==-2 then penMode=1
SPpath(sx,sy) if penColor==-2 then
SPpath(penX,penY)
else else
FIELD[page][sy][sx]=pen FIELD[page][penY][penX]=penColor
end end
end end
elseif key=="delete"then elseif key=="delete"then
@@ -260,13 +256,15 @@ function scene.keyDown(key)
elseif key=="escape"then elseif key=="escape"then
SCN.back() SCN.back()
else else
pen=penKey[key]or pen penColor=penKey[key]or penColor
end end
penX,penY,pen=sx,sy,pen
end end
function scene.keyUp(key) function scene.keyUp(key)
if key=="space"and pen==-2 then if key=="space"then
SPdraw() if penColor==-2 then
SPdraw()
end
penMode=false
end end
end end
@@ -303,20 +301,19 @@ function scene.draw()
--Draw pen --Draw pen
if penX and penY then if penX and penY then
local x,y=30*penX,600-30*penY local x,y=30*penX,600-30*penY
if kb.isDown("space")or ms.isDown(1)then if penMode==1 or penMode==2 then
gc.setLineWidth(5) gc.setLineWidth(5)
gc.rectangle("line",x-30,y,30,30,4) gc.rectangle("line",x-30,y,30,30,4)
elseif ms.isDown(3)then elseif penMode==3 then
gc.setLineWidth(3) gc.setLineWidth(3)
gc.line(x-15,y,x-30,y+15) gc.line(x-15,y,x-30,y+15)
gc.line(x,y,x-30,y+30) gc.line(x,y,x-30,y+30)
gc.line(x,y+15,x-15,y+30) gc.line(x,y+15,x-15,y+30)
else
gc.setLineWidth(2)
gc.rectangle("line",x-30,y,30,30,3)
gc.setColor(1,1,1,.2)
gc.rectangle("fill",x-30,y,30,30,3)
end end
gc.setLineWidth(2)
gc.rectangle("line",x-30,y,30,30,3)
gc.setColor(1,1,1,.2)
gc.rectangle("fill",x-30,y,30,30,3)
end end
--Draw smart pen path --Draw smart pen path
@@ -326,14 +323,15 @@ function scene.draw()
if #SPlist<=5 then if #SPlist<=5 then
gc.setColor(COLOR.rainbow_light(TIME()*6.2)) gc.setColor(COLOR.rainbow_light(TIME()*6.2))
else else
gc.setColor(COLOR.grey) gc.setColor(.9,.9,.9,.7+.2*math.sin(TIME()*12.6))
end end
for i=1,#SPlist do for i=1,#SPlist do
gc.rectangle("line",30*SPlist[i][1]-30+2,600-30*SPlist[i][2]+2,30-4,30-4,3) gc.rectangle("line",30*SPlist[i][1]-30+2,600-30*SPlist[i][2]+2,30-4,30-4,3)
end end
elseif SPmode==1 then else
gc.setColor(1,1,1)
for i=1,#SPlist do for i=1,#SPlist do
gc.rectangle("line",30*SPlist[i][1]-30+2,600-30*SPlist[i][2]+2,30-4,30-4,3) gc.draw(cross,30*SPlist[i][1]-30,600-30*SPlist[i][2])
end end
end end
end end
@@ -347,16 +345,16 @@ function scene.draw()
gc.rectangle("fill",50,600,100,6) gc.rectangle("fill",50,600,100,6)
--Draw pen color --Draw pen color
if pen>0 then if penColor>0 then
gc.setLineWidth(13) gc.setLineWidth(13)
gc.setColor(minoColor[pen]) gc.setColor(minoColor[penColor])
gc.rectangle("line",565,495,70,70) gc.rectangle("line",565,495,70,70)
elseif pen==-1 then elseif penColor==-1 then
gc.setLineWidth(5) gc.setLineWidth(5)
gc.setColor(.9,.9,.9) gc.setColor(.9,.9,.9)
gc.line(575,505,625,555) gc.line(575,505,625,555)
gc.line(575,555,625,505) gc.line(575,555,625,505)
elseif pen==-2 then elseif penColor==-2 then
gc.setLineWidth(13) gc.setLineWidth(13)
gc.setColor(COLOR.rainbow(TIME()*6.2)) gc.setColor(COLOR.rainbow(TIME()*6.2))
gc.rectangle("line",565,495,70,70) gc.rectangle("line",565,495,70,70)
@@ -382,7 +380,7 @@ function scene.draw()
end end
end end
local function setPen(i)return function()pen=i end end local function setPen(i)return function()penColor=i end end
scene.widgetList={ scene.widgetList={
WIDGET.newText{name="title", x=1020,y=5,font=70,align="R"}, WIDGET.newText{name="title", x=1020,y=5,font=70,align="R"},
WIDGET.newText{name="subTitle", x=1030,y=50,font=35,align="L",color="grey"}, WIDGET.newText{name="subTitle", x=1030,y=50,font=35,align="L",color="grey"},