function multiplyMatrix(matrix,vector) local x=matrix[1][1]*vector[1]+matrix[1][2]*vector[2]+matrix[1][3]*vector[3] local y=matrix[2][1]*vector[1]+matrix[2][2]*vector[2]+matrix[2][3]*vector[3] local z=matrix[3][1]*vector[1]+matrix[3][2]*vector[2]+matrix[3][3]*vector[3] return x,y,z end
function rotate(vertices,angle,x,y,z) local sum=x+y+z x,y,z=x/sum,y/sum,z/sum local c,s=math.cos(angle),math.sin(angle) local matrix={{x*x+(1-x*x)*c,x*y*(1-c)-z*s,x*z*(1-c)+y*s},{x*y*(1-c)+z*s,y*y+(1-y*y)*c,y*z*(1-c)-x*s},{x*z*(1-c)-y*s,y*z*(1-c)+x*s,z*z+(1-z*z)*c}} for i,e in pairs(vertices) do vertices[i]={multiplyMatrix(matrix,e)} end end
function translate(vertices,x,y,z) for i,e in pairs(vertices) do vertices[i]={e[1]+x,e[2]+y,e[3]+z} end end
function scale(vertices,x,y,z) for i,e in pairs(vertices) do vertices[i]={e[1]*x,e[2]*y,e[3]*z} end end
function width() return platform.window:width() end function height() return platform.window:height() end
function reverseTable(tbl) local tbl2={} for i,e in ipairs(tbl) do tbl2[#tbl-i+1]=e end return tbl2 end
function replaceFaces(tbl1,tbl2,faces) local faces2={} for i,e in pairs(tbl1) do for j,e2 in pairs(tbl2) do if e2 then if e==e2 then table.insert(faces2,faces[j]) tbl2[j]=nil end end end end return reverseTable(faces2) end
function sortFaces(vertices,faces) local faces2,distTbl,facesTbl={},{},{} local middle={} local dist,xsum,ysum,zsum=0,0,0,0 for i,e in pairs(faces) do xsum,ysum,zsum=0,0,0,0 for j,e2 in pairs(e) do xsum,ysum,zsum=xsum+vertices[e2][1],ysum+vertices[e2][2],zsum+vertices[e2][3] end middle={xsum/#e,ysum/#e-5,zsum/#e} dist=math.sqrt(middle[1]*middle[1]+middle[2]*middle[2]+middle[3]*middle[3]) table.insert(distTbl,dist) table.insert(facesTbl,dist) end table.sort(distTbl) return replaceFaces(distTbl,facesTbl,faces) end
function renderFaces(gc,vertices,faces,pos,mode) local polygon={} local faces2=sortFaces(vertices,faces) for i,e in pairs(faces2) do polygon={} for j,f in pairs(faces2[i]) do table.insert(polygon,pos[f][1]) table.insert(polygon,pos[f][2]) end table.insert(polygon,pos[faces2[i][1]][1]) table.insert(polygon,pos[faces2[i][1]][2]) if mode==4 or mode==5 then gc:setColorRGB(200,200,200) gc:fillPolygon(polygon) end if mode==2 or mode==3 or mode==4 then gc:setColorRGB(0,0,0) gc:drawPolyLine(polygon) end end end
function renderVertices(gc,pos) gc:setColorRGB(0,0,0) for i,e in pairs(pos) do if e then gc:fillRect(e[1]-1,e[2]-1,3,3) end end end
function render(gc,vertices,faces,mode) local ray,dist,nb,pos={},0,0,{} for i,e in pairs(vertices) do if e[2]<5 then ray={e[1],e[2]-5,e[3]} dist=math.sqrt(ray[1]*ray[1]+ray[2]*ray[2]+ray[3]*ray[3]) nb=((dist*5)/(ray[2]))/dist table.insert(pos,{ray[1]*nb*25+width()/2,ray[3]*nb*25+height()/2}) else table.insert(pos,false) end end if mode==1 or mode==2 then renderVertices(gc,pos) end if mode==2 or mode==3 or mode==4 or mode==5 then renderFaces(gc,vertices,faces,pos,mode) end end
I haven't done anything for two weeks, because of my homework But I have a new project ! This is a small 3D engine. I know that there is already Make3D by Levak, which is amazing, but I have to train with 3D. Moreover, mine is supposed to be a game engine, because it is very small. (I don't know if Lua is fast enough to use it)
So if anyone's interested, I post my engine... I also add screenshots of a little 3D viewer made with that engine. For now, it only draw vertices and edges. I'll try to add triangles...
function multiplyMatrix(matrix,vector) local x=matrix[1][1]*vector[1]+matrix[1][2]*vector[2]+matrix[1][3]*vector[3] local y=matrix[2][1]*vector[1]+matrix[2][2]*vector[2]+matrix[2][3]*vector[3] local z=matrix[3][1]*vector[1]+matrix[3][2]*vector[2]+matrix[3][3]*vector[3] return x,y,z end
function width() return platform.window:width() end function height() return platform.window:height() end
function reverseTable(tbl) local tbl2={} for i,e in ipairs(tbl) do tbl2[#tbl-i+1]=e end return tbl2 end
function replaceFaces(tbl1,tbl2,faces) local faces2,count={},1 for i,e in pairs(tbl1) do for j,e2 in pairs(tbl2) do if e2 then if e==e2 then faces2[count]=faces[j] count=count+1 tbl2[j]=nil end end end end return reverseTable(faces2) end
function sortFaces(vertices,faces,offset) local faces2,distTbl,facesTbl={},{},{} local middle={} local dist,xsum,ysum,zsum=0,0,0,0 for i,e in pairs(faces) do xsum,ysum,zsum=0,0,0,0 for j,e2 in pairs(e) do xsum,ysum,zsum=xsum+vertices[e2][1],ysum+vertices[e2][2],zsum+vertices[e2][3] end middle={xsum/#e,ysum/#e+offset,zsum/#e} dist=middle[1]*middle[1]+middle[2]*middle[2]+middle[3]*middle[3] distTbl[i]=dist facesTbl[i]=dist end table.sort(distTbl) return replaceFaces(distTbl,facesTbl,faces) end
function chooseColor(gc,vertices,face,color) if #face<3 then gc:setColorRGB(color[1],color[2],color[3]) else local a,b,c=vertices[face[1]][1]-vertices[face[2]][1],vertices[face[1]][2]-vertices[face[2]][2],vertices[face[1]][3]-vertices[face[2]][3] local d,e,f=vertices[face[1]][1]-vertices[face[3]][1],vertices[face[1]][2]-vertices[face[3]][2],vertices[face[1]][3]-vertices[face[3]][3] local normale={math.abs(b*f-c*e),math.abs(c*d-a*f),math.abs(a*e-b*d)} local angle=math.atan(math.sqrt(normale[1]*normale[1]+normale[3]*normale[3])/normale[2]) local R,G,B=color[1]+angle*-60+50,color[2]+angle*-60+50,color[3]+angle*-60+50 R,G,B=R>255 and 255 or R,G>255 and 255 or G,B>255 and 255 or B R,G,B=R<0 and 0 or R,G<0 and 0 or G,B<0 and 0 or B gc:setColorRGB(R,G,B) end end
function renderFaces(gc,vertices,faces,pos,mode,color,offset) local polygon,size,faces2={},0,{} if mode==4 or mode==5 or mode==6 then faces2=sortFaces(vertices,faces,offset) else faces2=faces end for i,e in pairs(faces2) do polygon,size={},0 drawPoly=true for j,f in pairs(faces2[i]) do if not pos[f] then drawPoly=false else polygon[j*2-1]=pos[f][1] polygon[j*2]=pos[f][2] size=size+2 end end if drawPoly then polygon[size+1]=pos[faces2[i][1]][1] polygon[size+2]=pos[faces2[i][1]][2] if mode==4 then gc:setColorRGB(color[1],color[2],color[3]) gc:fillPolygon(polygon) elseif mode==5 or mode==6 then chooseColor(gc,vertices,e,color) gc:fillPolygon(polygon) end if mode==2 or mode==3 or mode==4 or mode==5 then gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawPolyLine(polygon) end end end end
function renderVertices(gc,pos) gc:setColorRGB(0,0,0) for i,e in pairs(pos) do if e then gc:fillRect(e[1]-1,e[2]-1,3,3) end end end
function rotate(vertices,angle,x,y,z) local sum=x+y+z x,y,z=x/sum,y/sum,z/sum local c,s=math.cos(angle),math.sin(angle) local matrix={{x*x+(1-x*x)*c,x*y*(1-c)-z*s,x*z*(1-c)+y*s},{x*y*(1-c)+z*s,y*y+(1-y*y)*c,y*z*(1-c)-x*s},{x*z*(1-c)-y*s,y*z*(1-c)+x*s,z*z+(1-z*z)*c}} for i,e in pairs(vertices) do vertices[i]={multiplyMatrix(matrix,e)} end end
function translate(vertices,x,y,z) for i,e in pairs(vertices) do vertices[i]={e[1]+x,e[2]+y,e[3]+z} end end
function scale(vertices,x,y,z) for i,e in pairs(vertices) do vertices[i]={e[1]*x,e[2]*y,e[3]*z} end end
function render(gc,vertices,faces,mode,color,offset) local yDist,pos=0,{} for i,e in pairs(vertices) do if e[2]>-offset then yDist=offset/(e[2]+offset) pos[i]={e[1]*yDist*25+width()/2,height()/2-e[3]*yDist*25} else pos[i]=false end end if mode==1 or mode==2 then renderVertices(gc,pos) end if #faces>0 and (mode==2 or mode==3 or mode==4 or mode==5 or mode==6) then renderFaces(gc,vertices,faces,pos,mode,color,offset) end end
Sorry for off-topic, but what happened ? I never use #omnomIRC so I don't know anything...
Getting back to the topic, I voted for Sequenced. I like both Sequenced and PrizmCity, but since prizmCity has awesome graphics, I think it will get more votes.
function menuAction(action) if string.find("NOS",action) then fileAction(action) elseif string.find("+-",action) then setZoom(action) elseif string.find("defpclr",action) then selectTool(action) elseif string.find("hs",action) then setColor(action) end refresh() end
function fileAction(action) if action=="N" then status="requesting" rqstr="" requested="Image width" elseif action=="O" then status="requesting" rqstr="" requested="File name" elseif action=="S" then if not imgTable then status="error" errtype="No opened file" else status="requesting" rqstr=imgName requested="Save as" end end end
function setColor(action) if action=="h" then status="requesting" rqstr="" requested="Hex code" elseif action=="s" then status="selectingColor" selColor=1 newColor={math.floor(color[1]/5)*5,math.floor(color[2]/5)*5,math.floor(color[3]/5)*5} end end
function setZoom(action) if action=="+" then --Zoom in if imgZoom<8 then xScroll=0 yScroll=0 imgZoom=imgZoom*2 end elseif action=="-" then --Zoom out if imgZoom>1 then xScroll=0 yScroll=0 imgZoom=imgZoom/2 end end end
function selectTool(action) toolStep=1 if action=="d" then tool="draw" elseif action=="e" then tool="erase" elseif action=="f" then tool="fill" elseif action=="p" then tool="pick" elseif action=="c" then tool="circle" elseif action=="l" then tool="line" elseif action=="r" then tool="rect" end end
toolAction={}
function toolAction.draw() imgTable[xPos][yPos]={1,math.floor(color[1]/8),math.floor(color[2]/8),math.floor(color[3]/8)} changedMark="*" end
function toolAction.erase() imgTable[xPos][yPos][1]=0 changedMark="*" end
function toolAction.pick() if imgTable[xPos][yPos][1]==0 then color={255,255,255} else color={imgTable[xPos][yPos][2]*8,imgTable[xPos][yPos][3]*8,imgTable[xPos][yPos][4]*8} end end
function toolAction.fill() fillPixel(xPos,yPos) end
function toolAction.rect() if toolStep==1 then toolStep=2 step1.x,step1.y=xPos,yPos else toolStep=1 drawRect(step1.x,step1.y,xPos,yPos) end end
function toolAction.circle() if toolStep==1 then toolStep=2 step1.x,step1.y=xPos,yPos else toolStep=1 drawCircle(step1.x,step1.y,xPos,yPos) end end
function toolAction.line() if toolStep==1 then toolStep=2 step1.x,step1.y=xPos,yPos else toolStep=1 drawLine(step1.x,step1.y,xPos,yPos) end end
function requestAction() if requested=="Hex code" then loadHexColor() elseif requested=="Image width" then if tonumber(rqstr) and tonumber(rqstr)~=0 then tempVarWidth=tonumber(rqstr) status="requesting" requested="Image height" rqstr="" else status="error" errtype="Invalid number" end elseif requested=="Image height" then if tonumber(rqstr) and tonumber(rqstr)~=0 then if tonumber(rqstr)*tempVarWidth>3000 then status="error" errtype="Overflow (3000 px max)" else file.new() end else status="error" errtype="Invalid number" end elseif requested=="File name" then rqstr=string.lower(rqstr) file.load() elseif requested=="Save as" then rqstr=string.lower(rqstr) file.save() end end
function imgTools.getPixel(byte1,byte2) local str2 str2=imgTools.addZeros(mathTools.base10to2(tonumber(byte2)),8)..imgTools.addZeros(mathTools.base10to2(tonumber(byte1)),8) return {tonumber(string.sub(str2,1,1)),mathTools.base2to10(string.sub(str2,2,6)),mathTools.base2to10(string.sub(str2,7,11)),mathTools.base2to10(string.sub(str2,12,16))} end
function imgTools.getSize(img) imgWidth=mathTools.base2to10(mathTools.base10to2(tonumber(string.sub(img,10,12)..string.sub(img,7,9)..string.sub(img,4,6)..string.sub(img,1,3)))) imgHeight=mathTools.base2to10(mathTools.base10to2(tonumber(string.sub(img,22,24)..string.sub(img,19,21)..string.sub(img,16,18)..string.sub(img,13,15)))) end
function imgTools.addZeros(str,strSize) return string.rep("0",strSize-string.len(str))..str end
function imgTools.convertChars(img) local finished,img2,index index=1 img2="" finished=nil while not finished do if string.sub(img,index,index)~=[[\]] then img2=img2..imgTools.addZeros(tostring(string.byte(string.sub(img,index,index))),3) index=index+1 else img2=img2..string.sub(img,index+1,index+3) index=index+4 end if index>string.len(img) then finished=1 end end return img2 end
function imgTools.generateHeader() local binaryHeader,header binaryHeader={} binaryHeader[1]=imgTools.addZeros(mathTools.base10to2(imgWidth),32) binaryHeader[2]=imgTools.addZeros(mathTools.base10to2(imgHeight),32) binaryHeader[3]=imgTools.addZeros(mathTools.base10to2(imgWidth*2),32) header={"","",""} for i=1,3 do for j=3,0,-1 do header[i]=header[i]..imgTools.addZeros(tostring(mathTools.base2to10(string.sub(binaryHeader[i],8*j+1,8*(j+1)))),3) end end return header[1]..header[2].."000000000000"..header[3].."016000001000" end
function imgTools.convertPixel(pixel) local str str=tostring(pixel[1])..imgTools.addZeros(mathTools.base10to2(pixel[2]),5)..imgTools.addZeros(mathTools.base10to2(pixel[3]),5)..imgTools.addZeros(mathTools.base10to2(pixel[4]),5) return imgTools.addZeros(tostring(mathTools.base2to10(string.sub(str,9,16))),3)..imgTools.addZeros(tostring(mathTools.base2to10(string.sub(str,1,8))),3) end
function imgTools.addSlashes(str) local finished,str2,index finished=nil str2=[[\]]..str index=1 while not finished do index=index+4 if index>string.len(str2) then finished=1 else str2=string.sub(str2,1,index-1)..[[\]]..string.sub(str2,index) end end return str2 end
function imgTools.img2table(str) local index str2=imgTools.convertChars(str) imgTable={} imgTools.getSize(str2) for i=1,imgWidth do imgTable[i]={} for j=1,imgHeight do index=(i-1)*6+61+(j-1)*6*imgWidth imgTable[i][j]=imgTools.getPixel(string.sub(str2,index,index+2),string.sub(str2,index+3,index+5)) end end end
function imgTools.drawTable(gc,x,y) gc:setColorRGB(0,0,0) gc:setPen("thin","dashed") gc:drawRect(x-1,y-1,imgWidth*imgZoom+1,imgHeight*imgZoom+1) gc:setPen("thin","smooth") for i=1,imgWidth do for j=1,imgHeight do if imgTable[i][j][1]==1 then gc:setColorRGB(imgTable[i][j][2]*8,imgTable[i][j][3]*8,imgTable[i][j][4]*8) gc:fillRect((i-1)*imgZoom+x,(j-1)*imgZoom+y,imgZoom,imgZoom) else gc:setColorRGB(175,175,175) for k=0,imgZoom-1 do gc:fillRect((i-1)*imgZoom+x+k,(j-1)*imgZoom+y+k,1,1) end end end end end
function imgTools.table2img() local imgStr imgStr=imgTools.generateHeader() for i=1,imgHeight do for j=1,imgWidth do imgStr=imgStr..imgTools.convertPixel(imgTable[j][i]) end end return imgTools.addSlashes(imgStr) end
function mathTools.base10to2(n) local str str="" if n~=0 then while n~=1 do str=str..tostring(n%2) n=math.floor(n/2) end str=str..tostring(n%2) return string.reverse(str) else return "0" end end
function mathTools.base2to10(n) local num = 0 local ex = string.len(n) - 1 local l = 0 l = ex + 1 for i = 1, l do b = string.sub(n, i, i) if b == "1" then num = num + 2^ex end ex = ex - 1 end return num end
function mathTools.int2Hex(int) if int<10 then return tostring(int) else if int==10 then return "A" elseif int==11 then return "B" elseif int==12 then return "C" elseif int==13 then return "D" elseif int==14 then return "E" elseif int==15 then return "F" end end end
--FILE
file={}
function file.save() if rqstr=="" then status="error" errtype="Please type a name" elseif string.find("0123456789",string.sub(rqstr,1,1)) then status="error" errtype="Invalid name" else imgString=imgTools.table2img() var.store(rqstr,imgString) imgName=rqstr changedMark="" end end
function file.load() local test test=var.recall(rqstr) if not test then status="error" errtype="File does not exist" elseif type(test)~="string" then status="error" errtype="Invalid file" else imgZoom=1 xPos=1 yPos=1 xScroll=0 yScroll=0 tool=nil changedMark="" imgString=test imgTools.img2table(imgString) imgName=rqstr end end
function file.new() imgWidth=tempVarWidth imgHeight=tonumber(rqstr) imgZoom=1 xPos=1 yPos=1 xScroll=0 yScroll=0 tool=nil changedMark="" imgName="unsaved" imgTable={} for i=1,imgWidth do imgTable[i]={} for j=1,imgHeight do imgTable[i][j]={0,0,0,0} end end end
--MISCELLANEOUS
function width() return platform.window:width() end
function height() return platform.window:height() end
function refresh() platform.window:invalidate() end
function copyTable(t) local t2 = {} for k,v in pairs(t) do t2[k] = v end return t2 end
function samePixels(px1,px2) if (px1[1]==px2[1] and px1[2]==px2[2] and px1[3]==px2[3] and px1[4]==px2[4]) or (px1[1]==0 and px2[1]==0) then return 1 else return nil end end
function fillPixel(x,y) local previousPixel,xTest,yTest previousPixel=copyTable(imgTable[x][y]) if not samePixels(previousPixel,{1,math.floor(color[1]/8),math.floor(color[2]/8),math.floor(color[3]/8)}) then changedMark="*" imgTable[x][y]={1,math.floor(color[1]/8),math.floor(color[2]/8),math.floor(color[3]/8)} for i=1,4 do xTest=math.floor(i/2)-1+x yTest=(i==3 and 1 or 0)-(i==2 and 1 or 0)+y if xTest>0 and xTest<imgWidth+1 and yTest>0 and yTest<imgHeight+1 then if samePixels(previousPixel,imgTable[xTest][yTest]) then fillPixel(xTest,yTest) end end end end end
function setPixel(x,y) if x>=1 and x<=imgWidth and y>=1 and y<=imgHeight then imgTable[x][y]={1,math.floor(color[1]/8),math.floor(color[2]/8),math.floor(color[3]/8)} end end
function drawCircle(x1,y1,x2,y2) local rayon=math.floor(math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))) local x,y,m x=0 y=rayon m=5-4*rayon while x<=y do setPixel(x+x1,y+y1) setPixel(y+x1,x+y1) setPixel(-x+x1,y+y1) setPixel(-y+x1,x+y1) setPixel(x+x1,-y+y1) setPixel(y+x1,-x+y1) setPixel(-x+x1,-y+y1) setPixel(-y+x1,-x+y1) if m>0 then y=y-1 m=m-8*y end x=x+1 m=m+8*x+4 end end
function round(nb) if nb>math.floor(nb)+0.5 then return math.floor(nb)+1 else return math.floor(nb) end end
function drawLine(x1,y1,x2,y2) local reversed=nil if x1>x2 then x1,x2=x2,x1 reversed=not reversed end if y1>y2 then y1,y2=y2,y1 reversed=not reversed end local size=x2-x1>y2-y1 and x2-x1 or y2-y1 for i=0,size do if reversed then setPixel(x1+(x2-x1)-round((i/size)*(x2-x1)),y1+round((i/size)*(y2-y1))) else setPixel(x1+round((i/size)*(x2-x1)),y1+round((i/size)*(y2-y1))) end end end
function drawRect(x1,y1,x2,y2) if x1>x2 then x1,x2=x2,x1 end if y1>y2 then y1,y2=y2,y1 end for i=x1,x2 do setPixel(i,y1) setPixel(i,y2) end for i=y1,y2 do setPixel(x1,i) setPixel(x2,i) end end
function loadHexColor() local isHex,tmptable isHex=1 tmptable={} if string.len(rqstr)==6 then for i=1,6,1 do currentch=string.sub(rqstr,i,i) tmptable[i]=tonumber(currentch,16) if not tmptable[i] then isHex=nil end end else isHex=nil end if isHex then color={tmptable[1]*16+tmptable[2],tmptable[3]*16+tmptable[4],tmptable[5]*16+tmptable[6]} else status="error" errtype="Invalid hexadecimal" end end
--DRAWING function drawColorSelect(gc) gc:setColorRGB(200,200,255) gc:fillRect(width()/2-75,height()/2-50,150,100) gc:setColorRGB(0,0,0) gc:fillRect(width()/2-75,height()/2-50,150,15) gc:setPen("thin","smooth") gc:drawRect(width()/2-75,height()/2-50,150,100) gc:setFont("sansserif","r",10) gc:setColorRGB(255,255,255) gc:drawString("Select your color",width()/2-73,height()/2-53,"top") gc:setColorRGB(0,0,0) gc:drawString("Red :",width()/2-73,height()/2-35,"top") gc:drawString("Green :",width()/2-73,height()/2-20,"top") gc:drawString("Blue :",width()/2-73,height()/2-5,"top") for i=0,63 do gc:setColorRGB(i*4,newColor[2],newColor[3]) gc:fillRect(width()/2-23+i,height()/2-30,1,10) gc:setColorRGB(newColor[1],i*4,newColor[3]) gc:fillRect(width()/2-23+i,height()/2-15,1,10) gc:setColorRGB(newColor[1],newColor[2],i*4) gc:fillRect(width()/2-23+i,height()/2,1,10) end gc:setColorRGB(0,0,0) gc:drawRect(width()/2-23,height()/2-46+selColor*15,64,11) for i=1,3 do gc:drawString(tostring(newColor[i]),width()/2+52,height()/2-50+i*15,"top") gc:fillRect(width()/2-24+newColor[i]/4,height()/2-47+i*15,3,14) end gc:setColorRGB(color[1],color[2],color[3]) gc:fillRect(width()/2-70,height()/2+20,30,20) gc:setColorRGB(newColor[1],newColor[2],newColor[3]) gc:fillRect(width()/2-28,height()/2+20,30,20) gc:setColorRGB(0,0,0) gc:drawRect(width()/2-70,height()/2+20,30,20) gc:drawRect(width()/2-28,height()/2+20,30,20) gc:drawString(mathTools.int2Hex(math.floor(newColor[1]/16))..mathTools.int2Hex(newColor[1]%16)..mathTools.int2Hex(math.floor(newColor[2]/16))..mathTools.int2Hex(newColor[2]%16)..mathTools.int2Hex(math.floor(newColor[3]/16))..mathTools.int2Hex(newColor[3]%16),width()/2+20,height()/2+20,"top") gc:drawString("Old New",width()/2-65,height()/2+21,"top") end
function drawRequest(gc,msg) gc:setColorRGB(200,200,255) gc:fillRect(width()/2-75,height()/2-25,150,50) gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawRect(width()/2-75,height()/2-25,150,50) gc:fillRect(width()/2-75,height()/2-25,150,15) gc:setFont("sansserif","r",10) gc:setColorRGB(255,255,255) gc:drawString(msg,width()/2-70,height()/2-28,"top") gc:setColorRGB(0,0,0) gc:drawRect(width()/2-70,height()/2,140,20) gc:drawString(rqstr,width()/2-65,height()/2,"top") end
function drawError(gc) gc:setColorRGB(200,200,255) gc:fillRect(width()/2-75,height()/2-20,150,40) gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawRect(width()/2-75,height()/2-20,150,40) gc:fillRect(width()/2-75,height()/2-20,150,15) gc:setFont("sansserif","r",10) gc:setColorRGB(255,255,255) gc:drawString("Error",width()/2-73,height()/2-23,"top") gc:setColorRGB(0,0,0) gc:drawString(errtype,width()/2-70,height()/2-5,"top") end
function drawCursor(gc) gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawRect((xPos-1)*imgZoom+29-xScroll*imgZoom,(yPos-1)*imgZoom+29-yScroll*imgZoom,imgZoom+1,imgZoom+1) gc:setColorRGB(255,255,255) gc:drawRect((xPos-1)*imgZoom+30-xScroll*imgZoom,(yPos-1)*imgZoom+30-yScroll*imgZoom,imgZoom-1,imgZoom-1) end
function drawTool(gc) local id=tool if not id then id="move" end gc:drawImage(toolsSprites[id],0,15) end
drawStep1={}
function drawStep1.circle(gc) local rayon=math.sqrt((xPos-step1.x)*(xPos-step1.x)+(yPos-step1.y)*(yPos-step1.y)) local startX,startY,size startX,startY=(step1.x-rayon-0.6)*imgZoom+30-xScroll*imgZoom,(step1.y-rayon-0.6)*imgZoom+30-yScroll*imgZoom size=2*rayon*imgZoom gc:setColorRGB(255,255,255) gc:drawArc(startX-1,startY-1,size,size,0,360) gc:setColorRGB(0,0,0) gc:drawArc(startX,startY,size,size,0,360) end
function drawStep1.line(gc) gc:setColorRGB(255,255,255) gc:drawLine((step1.x-0.6)*imgZoom+30-xScroll*imgZoom,(step1.y-0.6)*imgZoom+30-yScroll*imgZoom,(xPos-0.6)*imgZoom+30-xScroll*imgZoom,(yPos-0.6)*imgZoom+30-yScroll*imgZoom) gc:setColorRGB(0,0,0) gc:drawLine((step1.x-0.6)*imgZoom+29-xScroll*imgZoom,(step1.y-0.6)*imgZoom+29-yScroll*imgZoom,(xPos-0.6)*imgZoom+29-xScroll*imgZoom,(yPos-0.6)*imgZoom+29-yScroll*imgZoom) end
function drawStep1.rect(gc) local startX,startY,sizeX,sizeY,lowerY,higherY lowerX,lowerY=xPos<step1.x and xPos or step1.x,yPos<step1.y and yPos or step1.y startX,startY=(lowerX-0.6)*imgZoom+30-xScroll*imgZoom,(lowerY-0.6)*imgZoom+30-yScroll*imgZoom sizeX,sizeY=math.abs((xPos-step1.x)*imgZoom),math.abs((yPos-step1.y)*imgZoom) gc:setColorRGB(255,255,255) gc:drawRect(startX-1,startY-1,sizeX,sizeY) gc:setColorRGB(0,0,0) gc:drawRect(startX,startY,sizeX,sizeY) end
function drawStep1.start(gc) local x,y=(step1.x-1)*imgZoom+29-xScroll*imgZoom,(step1.y-1)*imgZoom+29-yScroll*imgZoom gc:setColorRGB(255,255,255) gc:drawLine(x-1,y,x+imgZoom-1,y+imgZoom) gc:drawLine(x+imgZoom-1,y,x-1,y+imgZoom) gc:setColorRGB(0,0,0) gc:drawLine(x,y,x+imgZoom,y+imgZoom) gc:drawLine(x+imgZoom,y,x,y+imgZoom) end
function on.paint(gc) if imgTable then imgTools.drawTable(gc,30-xScroll*imgZoom,30-yScroll*imgZoom) if tool then drawCursor(gc) if toolStep==2 then drawStep1[tool](gc) drawStep1.start(gc) end end end gc:setColorRGB(color[1],color[2],color[3]) gc:fillRect(0,0,20,15) gc:setColorRGB(0,0,0) gc:setPen("thin","smooth") gc:drawRect(0,0,19,15) drawTool(gc) gc:setFont("sansserif","r",10) if imgTable then gc:drawString(changedMark..imgName.." - "..tostring(imgWidth).."x"..tostring(imgHeight).." ("..tostring(imgZoom*100).."%)",25,0,"top") else gc:drawString("Open or create a file",25,0,"top") end if status=="selectingColor" then drawColorSelect(gc) elseif status=="requesting" then drawRequest(gc,requested) elseif status=="error" then drawError(gc) end gc:setColorRGB(0,0,0) gc:setFont("sansserif","r",8) gc:drawString("Lua nSpaint - Par Loic Pujet",10,height()-12,"top") end
--EVENTS function on.backspaceKey() if status=="requesting" then rqstr=string.sub(rqstr,1,string.len(rqstr)-1) platform.window:invalidate() end end
function on.charIn(ch) if status=="drawing" then if string.find("NOS+-defphsclr",ch) then menuAction(ch) end elseif status=="requesting" and string.len(rqstr)<10 and string.find("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",ch) then rqstr=rqstr..ch end refresh() end
function on.escapeKey() if status=="drawing" then tool=nil else status="drawing" end refresh() end
function on.enterKey() if status=="drawing" and tool and imgTable then toolAction[tool]() elseif status=="selectingColor" then color=newColor status="drawing" elseif status=="requesting" then status="drawing" requestAction() elseif status=="error" then status="drawing" end refresh() end
function on.arrowKey(ar) if status=="drawing" and imgTable then if tool then if ar=="up" and yPos>1 then yPos=yPos-1 end if ar=="down" and yPos<imgHeight then yPos=yPos+1 end if ar=="left" and xPos>1 then xPos=xPos-1 end if ar=="right" and xPos<imgWidth then xPos=xPos+1 end else if ar=="down" and (imgHeight-yScroll)*imgZoom>height()-45 then yScroll=yScroll+1 end if ar=="up" and yScroll>0 then yScroll=yScroll-1 end if ar=="right" and (imgWidth-xScroll)*imgZoom>width()-45 then xScroll=xScroll+1 end if ar=="left" and xScroll>0 then xScroll=xScroll-1 end end elseif status=="selectingColor" then if ar=="up" then selColor=selColor-1 elseif ar=="down" then selColor=selColor+1 elseif ar=="right" and newColor[selColor]<251 then newColor[selColor]=newColor[selColor]+5 elseif ar=="left" and newColor[selColor]>4 then newColor[selColor]=newColor[selColor]-5 end if selColor>3 then selColor=1 elseif selColor<1 then selColor=3 end end refresh() end