Advertisement
DjSapsan

radio

Dec 9th, 2014
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.45 KB | None | 0 0
  1. local s=""
  2. local owner=""
  3. local id=os.computerID()
  4. local label=os.getComputerLabel()
  5. local nButtons=0
  6. local nLabels=0
  7. local button={} --кнопки
  8. local label={}  --текст
  9. local mx=0  --мышка х
  10. local my=0  --мышка у
  11. local mb=0  --мышка нажатие
  12. local isConnect=false
  13. local idT=0
  14. local com=""
  15. local canGPS=false
  16. local xPos,yPos,zPos=nil,nil,nil
  17. local loss=0
  18.  
  19. function Events()
  20.     while true do
  21.         e, p1, mx, my = os.pullEvent()
  22.         if e=="mouse_click" then
  23.             if  click(13) then connect() elseif
  24.                 click(14) then term.clear() term.setCursorPos(1,1) return elseif
  25.                 click(1) then sendCom("up") elseif
  26.                 click(2) then sendCom("down") elseif
  27.                 click(3) then sendCom("front") elseif
  28.                 click(4) then sendCom("back") elseif
  29.                 click(5) then sendCom("left") elseif
  30.                 click(6) then sendCom("right") elseif
  31.                 click(7) then sendPos() elseif
  32.                 click(8) then sendCom("dig") elseif
  33.                 click(9) then sendCom("exit") isConnect=false MenuMain() elseif
  34.                 click(10) then sendCom("place") elseif
  35.                 click(11) then sendCom(">")elseif
  36.                 click(12) then sendCom("<") elseif
  37.                 click(15) then if label[1].active==0 then label[1].active=1 drawscreen() else label[1].active=0 drawscreen() end
  38.             end
  39.         end
  40.         os.sleep(0.1)
  41.     end
  42. end
  43.  
  44. function sendCom(com)
  45.     rednet.send(idT,tostring(com))
  46. end
  47.  
  48. function sendPos()
  49.     if canGPS then
  50.         local pos = {gps.locate()}
  51.         stringPos=(textutils.serialize(pos))
  52.         rednet.send(idT,"!"..stringPos)
  53.     end
  54. end
  55.  
  56. do -- ------------------------------------------------------------------------------------- GUI
  57.  
  58. function newLabel(xx,yy,ddx,ddy,ccolor,ttext,aactive)
  59.     nLabels=nLabels+1
  60.     label[nLabels]={}
  61.     label[nLabels].x=xx
  62.     label[nLabels].dx=ddx
  63.     label[nLabels].y=yy
  64.     label[nLabels].dy=ddy
  65.     label[nLabels].color=ccolor
  66.     label[nLabels].text=ttext
  67.     label[nLabels].active=aactive
  68. end
  69.  
  70. function newButton(xx,yy,ddx,ddy,ccolor,ttext,aactive)  --создается новая кнопка
  71.     nButtons=nButtons+1
  72.     button[nButtons]={}
  73.     button[nButtons].x=xx
  74.     button[nButtons].dx=ddx
  75.     button[nButtons].y=yy
  76.     button[nButtons].dy=ddy
  77.     button[nButtons].color=ccolor
  78.     button[nButtons].text=ttext
  79.     button[nButtons].active=aactive
  80. end
  81.  
  82. function click(id)                          --если нажатие по кнопке id, то true
  83.     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
  84. end
  85.  
  86. function drawButton(i)                      --рисуется кнопка i
  87.      xx=tonumber(button[i].x)
  88.      dxx=tonumber(button[i].dx)
  89.      yy=tonumber(button[i].y)
  90.      dyy=tonumber(button[i].dy)
  91.      c=button[i].color
  92.      text=button[i].text
  93.      l=#text
  94.     term.setBackgroundColour(c)
  95.     for a=xx,xx+dxx-1 do
  96.         for b=yy,yy+dyy-1 do
  97.             term.setCursorPos( a , b )
  98.             term.write(" ")
  99.         end
  100.     end
  101.    
  102.     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
  103.      
  104. end
  105.  
  106. function drawLabel(i)                       -- текст
  107.     local xx=tonumber(label[i].x)
  108.     local dxx=tonumber(label[i].dx)
  109.     local yy=tonumber(label[i].y)
  110.     local dyy=tonumber(label[i].dy)
  111.     local c=label[i].color
  112.     local text=label[i].text
  113.     local l=#text               -- длина текста
  114.     local h=math.floor(l/dxx)   --сколько строк нужно для записи текста
  115.     local area=dxx*dyy
  116.     term.setTextColor(c)
  117.  
  118.     if (l>dxx)and(l>area) then
  119.         for p=0,dyy-1 do
  120.             term.setCursorPos(xx,yy+p) print( string.sub(text,p*dxx+1,dxx+p*dxx) )      --если текст больше площади метки
  121.         end
  122.     end
  123.    
  124.     if (l>dxx)and(l<area) then
  125.         for p=0,h do
  126.             term.setCursorPos(xx,yy+p) print( string.sub(text,p*dxx+1,dxx+p*dxx) )      --если текст больше ширины метки
  127.         end
  128.     end
  129.    
  130.     if l<dxx then term.setCursorPos( xx+(dxx/2)-(l/2), yy+(dyy/2) ) print(text)  end    --если текст меньше ширины метки
  131.    
  132.     term.setTextColor(1)
  133. end
  134.  
  135. function drawscreen()                       --рисуется весь экран
  136.     term.setBackgroundColour(65535)
  137.     term.clear()
  138.    
  139.         for i=1,#button do
  140.             if button[i].active==1 then drawButton(i) end
  141.         end
  142.        
  143.     term.setBackgroundColour(65535)
  144.    
  145.         for i=1,#label do
  146.             if label[i].active==1 then drawLabel(i) end
  147.         end
  148.        
  149.     term.setBackgroundColour(65535)
  150. end
  151.  
  152. end -- -------------------------------------------------------------------------------------
  153.  
  154.  
  155. function MenuMain() --главное меню
  156.     button[1].active=0  button[2].active=0  button[3].active=0
  157.     button[4].active=0  button[5].active=0  button[6].active=0
  158.     button[7].active=0  button[8].active=0  button[9].active=0
  159.     button[10].active=0 button[11].active=0 button[12].active=0
  160.     button[13].active=1 button[14].active=1 button[15].active=1 label[1].active=0
  161.     drawscreen()
  162. end
  163.  
  164. function MenuRadio()    --меню управления
  165.     button[1].active=1  button[2].active=1  button[3].active=1
  166.     button[4].active=1  button[5].active=1  button[6].active=1
  167.     button[7].active=1  button[8].active=1  button[9].active=1
  168.     button[10].active=1 button[11].active=1 button[12].active=1
  169.     button[13].active=0 button[14].active=0 button[15].active=0 label[1].active=0
  170.     drawscreen()
  171. end
  172.  
  173. function createButtons()
  174.     newButton(18,3,5,2,8192,"up",0) --вверх 1
  175.     newButton(18,7,5,2,8192,"down",0)--вниз 2
  176.     newButton(6,1,5,3,2,"front",0)  --вперед 3
  177.     newButton(6,7,5,3,2,"back",0)   --назад 4
  178.     newButton(1,4,5,3,2,"left",0)   --влево 5
  179.     newButton(11,4,5,3,2,"right",0) --вправо 6
  180.     newButton(7,5,3,1,32,"gps",0)   --в эту точку 7
  181.     newButton(1,11,10,3,8,"dig",0)  --копнуть 8
  182.     newButton(25,1,1,1,32766,"x",0) --выключить 9
  183.     newButton(12,11,10,3,4,"place",0)   --поставить 10
  184.     newButton(3,15,1,1,64,">",0)    --следующий слот 11
  185.     newButton(1,15,1,1,64,"<",0)    --предыдущий слот 12
  186.    
  187.     newButton(5,2,12,1,2048,"CONNECT",1)    -- поиск черепахи 13
  188.     newButton(5,6,12,1,32766,"EXIT",1)  -- выход 14
  189.     newButton(5,4,12,1,2048,"INFO",1)   -- инфа 15
  190.    
  191.     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)
  192. end
  193.  
  194. function connect()
  195.     term.clear()
  196.     term.setCursorPos(1,1)
  197.     print("Connection...")
  198.     rednet.broadcast("dj")
  199.     idT,msg=rednet.receive(1)
  200.     if msg~=nil then print("Turtle ready, id = "..idT) isConnect=true else print("Turtle not found") end
  201.     os.sleep(1)
  202.     if isConnect==true then MenuRadio() else MenuMain() end
  203. end
  204.  
  205. function getStat()
  206.     while true do
  207.         if isConnect then i,s=rednet.receive(2)
  208.         term.setCursorPos(1,16)
  209.        
  210.             if(s~=nil) then
  211.                 print(s)
  212.                 loss=0
  213.             else
  214.                 print("- error -                                                                                                              ")
  215.                 loss=loss+1
  216.             end
  217.            
  218.             if loss==5 then term.clear() term.setCursorPos(1,1) print("Connection lost") os.sleep(2) isConnect=false loss=0 MenuMain() end
  219.         end
  220.         os.sleep(1)
  221.     end
  222. end
  223.  
  224. function start()
  225.     rednet.open("back")
  226.     createButtons()             --создаем кнопки
  227.     term.clear()                --очистка экрана
  228.     term.setCursorPos(1,1)
  229.     print("searching GPS...")   --
  230.     os.sleep(1)
  231.     xPos,yPos,zPos=gps.locate(1)            --поиск gps
  232.     if xPos==nil then print("GPS not found") else print("location: x="..tostring(xPos).." y="..tostring(yPos).." z="..tostring(zPos)) canGPS=true end  
  233.     os.sleep(1)
  234.     MenuMain()
  235. end
  236.  
  237. start()
  238. parallel.waitForAny(Events,getStat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement