Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local s=""
- local owner=""
- local id=os.computerID()
- local label=os.getComputerLabel()
- local nButtons=0
- local nLabels=0
- local button={} --кнопки
- local label={} --текст
- local mx=0 --мышка х
- local my=0 --мышка у
- local mb=0 --мышка нажатие
- local isConnect=false
- local idT=0
- local com=""
- local canGPS=false
- local xPos,yPos,zPos=nil,nil,nil
- local loss=0
- function Events()
- while true do
- e, p1, mx, my = os.pullEvent()
- if e=="mouse_click" then
- if click(13) then connect() elseif
- click(14) then term.clear() term.setCursorPos(1,1) return elseif
- click(1) then sendCom("up") elseif
- click(2) then sendCom("down") elseif
- click(3) then sendCom("front") elseif
- click(4) then sendCom("back") elseif
- click(5) then sendCom("left") elseif
- click(6) then sendCom("right") elseif
- click(7) then sendPos() elseif
- click(8) then sendCom("dig") elseif
- click(9) then sendCom("exit") isConnect=false MenuMain() elseif
- click(10) then sendCom("place") elseif
- click(11) then sendCom(">")elseif
- click(12) then sendCom("<") elseif
- click(15) then if label[1].active==0 then label[1].active=1 drawscreen() else label[1].active=0 drawscreen() end
- end
- end
- os.sleep(0.1)
- end
- end
- function sendCom(com)
- rednet.send(idT,tostring(com))
- end
- function sendPos()
- if canGPS then
- local pos = {gps.locate()}
- stringPos=(textutils.serialize(pos))
- rednet.send(idT,"!"..stringPos)
- end
- end
- do -- ------------------------------------------------------------------------------------- GUI
- function newLabel(xx,yy,ddx,ddy,ccolor,ttext,aactive)
- nLabels=nLabels+1
- label[nLabels]={}
- label[nLabels].x=xx
- label[nLabels].dx=ddx
- label[nLabels].y=yy
- label[nLabels].dy=ddy
- label[nLabels].color=ccolor
- label[nLabels].text=ttext
- label[nLabels].active=aactive
- end
- function newButton(xx,yy,ddx,ddy,ccolor,ttext,aactive) --создается новая кнопка
- nButtons=nButtons+1
- button[nButtons]={}
- button[nButtons].x=xx
- button[nButtons].dx=ddx
- button[nButtons].y=yy
- button[nButtons].dy=ddy
- button[nButtons].color=ccolor
- button[nButtons].text=ttext
- button[nButtons].active=aactive
- end
- function click(id) --если нажатие по кнопке id, то true
- if button[id].active==1 then return (mx>=button[id].x)and(mx<=button[id].x+button[id].dx-1)and(my>=button[id].y)and(my<=button[id].y+button[id].dy-1) else return false end
- end
- function drawButton(i) --рисуется кнопка i
- xx=tonumber(button[i].x)
- dxx=tonumber(button[i].dx)
- yy=tonumber(button[i].y)
- dyy=tonumber(button[i].dy)
- c=button[i].color
- text=button[i].text
- l=#text
- term.setBackgroundColour(c)
- for a=xx,xx+dxx-1 do
- for b=yy,yy+dyy-1 do
- term.setCursorPos( a , b )
- term.write(" ")
- end
- end
- if l>dxx then term.setCursorPos( xx , yy ) print(string.sub(text,1,dxx)) else term.setCursorPos( xx+(dxx/2)-(l/2), yy+(dyy/2) ) term.write(text) end
- end
- function drawLabel(i) -- текст
- local xx=tonumber(label[i].x)
- local dxx=tonumber(label[i].dx)
- local yy=tonumber(label[i].y)
- local dyy=tonumber(label[i].dy)
- local c=label[i].color
- local text=label[i].text
- local l=#text -- длина текста
- local h=math.floor(l/dxx) --сколько строк нужно для записи текста
- local area=dxx*dyy
- term.setTextColor(c)
- if (l>dxx)and(l>area) then
- for p=0,dyy-1 do
- term.setCursorPos(xx,yy+p) print( string.sub(text,p*dxx+1,dxx+p*dxx) ) --если текст больше площади метки
- end
- end
- if (l>dxx)and(l<area) then
- for p=0,h do
- term.setCursorPos(xx,yy+p) print( string.sub(text,p*dxx+1,dxx+p*dxx) ) --если текст больше ширины метки
- end
- end
- if l<dxx then term.setCursorPos( xx+(dxx/2)-(l/2), yy+(dyy/2) ) print(text) end --если текст меньше ширины метки
- term.setTextColor(1)
- end
- function drawscreen() --рисуется весь экран
- term.setBackgroundColour(65535)
- term.clear()
- for i=1,#button do
- if button[i].active==1 then drawButton(i) end
- end
- term.setBackgroundColour(65535)
- for i=1,#label do
- if label[i].active==1 then drawLabel(i) end
- end
- term.setBackgroundColour(65535)
- end
- end -- -------------------------------------------------------------------------------------
- function MenuMain() --главное меню
- button[1].active=0 button[2].active=0 button[3].active=0
- button[4].active=0 button[5].active=0 button[6].active=0
- button[7].active=0 button[8].active=0 button[9].active=0
- button[10].active=0 button[11].active=0 button[12].active=0
- button[13].active=1 button[14].active=1 button[15].active=1 label[1].active=0
- drawscreen()
- end
- function MenuRadio() --меню управления
- button[1].active=1 button[2].active=1 button[3].active=1
- button[4].active=1 button[5].active=1 button[6].active=1
- button[7].active=1 button[8].active=1 button[9].active=1
- button[10].active=1 button[11].active=1 button[12].active=1
- button[13].active=0 button[14].active=0 button[15].active=0 label[1].active=0
- drawscreen()
- end
- function createButtons()
- newButton(18,3,5,2,8192,"up",0) --вверх 1
- newButton(18,7,5,2,8192,"down",0)--вниз 2
- newButton(6,1,5,3,2,"front",0) --вперед 3
- newButton(6,7,5,3,2,"back",0) --назад 4
- newButton(1,4,5,3,2,"left",0) --влево 5
- newButton(11,4,5,3,2,"right",0) --вправо 6
- newButton(7,5,3,1,32,"gps",0) --в эту точку 7
- newButton(1,11,10,3,8,"dig",0) --копнуть 8
- newButton(25,1,1,1,32766,"x",0) --выключить 9
- newButton(12,11,10,3,4,"place",0) --поставить 10
- newButton(3,15,1,1,64,">",0) --следующий слот 11
- newButton(1,15,1,1,64,"<",0) --предыдущий слот 12
- newButton(5,2,12,1,2048,"CONNECT",1) -- поиск черепахи 13
- newButton(5,6,12,1,32766,"EXIT",1) -- выход 14
- newButton(5,4,12,1,2048,"INFO",1) -- инфа 15
- newLabel(2,8,21,10,1,"Write 'priem' in yourturtle and click 'CONNECT' in your smartphone. If you have GPS, you can get position of turtle and call turtle to yourself!",0)
- end
- function connect()
- term.clear()
- term.setCursorPos(1,1)
- print("Connection...")
- rednet.broadcast("dj")
- idT,msg=rednet.receive(1)
- if msg~=nil then print("Turtle ready, id = "..idT) isConnect=true else print("Turtle not found") end
- os.sleep(1)
- if isConnect==true then MenuRadio() else MenuMain() end
- end
- function getStat()
- while true do
- if isConnect then i,s=rednet.receive(2)
- term.setCursorPos(1,16)
- if(s~=nil) then
- print(s)
- loss=0
- else
- print("- error - ")
- loss=loss+1
- end
- if loss==5 then term.clear() term.setCursorPos(1,1) print("Connection lost") os.sleep(2) isConnect=false loss=0 MenuMain() end
- end
- os.sleep(1)
- end
- end
- function start()
- rednet.open("back")
- createButtons() --создаем кнопки
- term.clear() --очистка экрана
- term.setCursorPos(1,1)
- print("searching GPS...") --
- os.sleep(1)
- xPos,yPos,zPos=gps.locate(1) --поиск gps
- if xPos==nil then print("GPS not found") else print("location: x="..tostring(xPos).." y="..tostring(yPos).." z="..tostring(zPos)) canGPS=true end
- os.sleep(1)
- MenuMain()
- end
- start()
- parallel.waitForAny(Events,getStat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement