Advertisement
Guest User

input

a guest
Jan 28th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. os.loadAPI("c")
  2. os.loadAPI("p")
  3. local fields = {}
  4. fields = {{15,10,23,10,nil,""},
  5.           {9,13,33,17,nil,""},
  6.           {35,13,43,14,"  save",""},
  7.           {35,16,43,17,"  clear",""}
  8.          }
  9.  
  10. -- convenience functions: because we can!
  11. local function drawFields()
  12.    local xalt,yalt = term.getCursorPos()
  13.    term.setBackgroundColor(colors.gray)
  14.    for f = 1,#fields do
  15.      for fLines = 0,fields[f][4]-fields[f][2] do
  16.        c.xwrite(string.rep(" ",fields[f][3]-fields[f][1]),fields[f][1],fields[f][2]+fLines)
  17.      end
  18.      if fields[f][6] ~= ""
  19.         then c.writefit(fields[f][6],fields[f][1],fields[f][2],fields[f][3])
  20.        end
  21.      if fields[f][5] ~= nil
  22.         then c.xwrite(fields[f][5],fields[f][1],fields[f][4])
  23.        end
  24.    end
  25.    term.setBackgroundColor(colors.black)
  26.    term.setCursorPos(xalt,yalt)
  27. end
  28.  
  29. local function clearScreen()
  30.   term.clear()
  31.   drawFields()
  32.   p.sign(34,3)
  33.   c.xwrite("NEWS-BOARD",9,5)
  34.   c.xwrite("Name:",9,10)
  35.   c.xwrite("New Message:",9,12)
  36.   term.setCursorPos(fields[1][1],fields[1][2])
  37. end
  38.  
  39. local function isField(x,y)
  40.   local returnField = -1
  41.   for i=1,#fields do
  42.     if x>=fields[i][1] and x<=fields[i][3] and y>=fields[i][2] and y<= fields[i][4]
  43.        then returnField = i end
  44.   end
  45.   return returnField  
  46. end
  47.  
  48. local function incID()
  49. local file = fs.open("archive/lastid.inf","r")
  50. local messageID = tonumber(file.readLine()) +1
  51.   file.close()
  52.   file = fs.open("archive/lastid.inf","w")
  53.   file.writeLine(messageID)
  54.   file.close()
  55.   return messageID      
  56. end
  57.  
  58. local function saveToFile()
  59. local saveFile = fs.open("archive/"..incID()..".msg","w")
  60.  
  61. local today = ""
  62. --  today = http.get("http://www.timeapi.org/cet/now?format=%25c").readAll()
  63. local firstLine = fields[1][6]..today    
  64.     saveFile.writeLine(firstLine)
  65.     saveFile.writeLine(fields[2][6])
  66.     saveFile.close()    
  67. end
  68.  
  69. local function onClick(x,y)
  70.  if isField(x,y) == 1 or isField(x,y) == 2
  71.     then term.setCursorPos(fields[isField(x,y)][1],fields[isField(x,y)][2])    
  72.  end
  73.  if isField(x,y) == 3 and string.len(fields[1][6]) >= 3 and string.len(fields[2][6]) >=5
  74.     then term.clear() --- ditte wech, wenn animiert
  75.          saveToFile()
  76.          print("saved")--- Animation hier!
  77.          sleep(2)
  78.          fields[1][6] = ""
  79.          fields[2][6] = ""
  80.          clearScreen()
  81.          shell.run("output")
  82.  elseif isField(x,y) == 4
  83.     then
  84.        --Todo clearAll Variables,counters etc.
  85.        fields[1][6] = ""
  86.        fields[2][6] = ""
  87.        clearScreen()
  88.  end                  
  89. end
  90.  
  91. local function getAction()
  92. local event,p1,p2,p3,p4,p5 = os.pullEvent()
  93. if event == "mouse_click"
  94.    then onClick(p2,p3)
  95. elseif event == "char"
  96.    then if isField(term.getCursorPos()) == 1 and string.len(fields[1][6])<8
  97.            then fields[1][6] = fields[1][6]..p1
  98.                 drawFields()
  99.                -- term.setCursorPos(fields[1][1],fields[1][2])
  100.                -- c.xwrite(fields[1][6],fields[1][1],fields[1][2])                
  101.         end
  102.         if isField(term.getCursorPos()) == 2 and string.len(fields[2][6])<115
  103.            then fields[2][6] = fields[2][6]..p1
  104.                 drawFields()
  105.                -- c.writefit(fields[2][6],fields[2][1],fields[2][2],fields[2][3])
  106.         end      
  107. elseif event == "key"
  108.    then if p1 == 14 and isField(term.getCursorPos()) == 1 and string.len(fields[1][6])>=1
  109.            then fields[1][6] = string.sub(fields[1][6],1,string.len(fields[1][6])-1)
  110.                 drawFields()  
  111.         end
  112.         if p1 == 14 and isField(term.getCursorPos()) == 2 and string.len(fields[2][6])>=1          
  113.            then fields[2][6] = string.sub(fields[2][6],1,string.len(fields[2][6])-1)
  114.                 drawFields()
  115.         end
  116.         if p1 == 15
  117.            then
  118.             if isField(term.getCursorPos()) == 1
  119.                then term.setCursorPos(fields[2][1],fields[2][2])
  120.                else term.setCursorPos(fields[1][1],fields[1][2])
  121.             end
  122.         end
  123. end
  124.  
  125. end
  126. --Program
  127. clearScreen()
  128. while true do
  129. term.setCursorBlink(true)
  130. getAction()
  131. --sleep(0.5)
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement