mad1231999

textfield API 0.4

Feb 11th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | None | 0 0
  1. function getPos(unit)
  2.  
  3.     file = io.open("MTextFieldDir/"..unit..".txt", "r")
  4.     x = tonumber(file:read("*l"))
  5.     y = tonumber(file:read("*l"))
  6.     text = file:read("*l")
  7.     if file:read("*l") == "true" then bVisible = true else bVisible = false end
  8.     return bVisible, text, x, y
  9.    
  10. end
  11.  
  12. ------------------------------------------------------------------
  13.  
  14. function setText(unit, text)
  15.  
  16.     local tLines = {}
  17.  
  18.     if string.len(text) > 20 then
  19.  
  20.         text = string.sub(text, 1, 20)
  21.  
  22.     end
  23.  
  24.     file = io.open("MTextFieldDir/"..unit..".txt", "r")
  25.  
  26.     table.insert(tLines, 1, file:read("*l"))
  27.     table.insert(tLines, 2, file:read("*l"))
  28.     table.insert(tLines, 3, file:read("*l"))
  29.     table.insert(tLines, 4, file:read("*l"))
  30.  
  31.     tLines[3] = text
  32.     file:close()
  33.  
  34.     file = io.open("MTextFieldDir/"..unit..".txt", "w")
  35.     file:write(tLines[1].."\n"..tLines[2].."\n"..tLines[3].."\n"..tLines[4])
  36.     file:close()
  37.  
  38.     bVisible, text, x, y = getPos(unit)
  39.  
  40.     if bVisible == true then
  41.  
  42.         term.setCursorPos(x, y)
  43.         io.write("+--------------------+")
  44.         term.setCursorPos(x, y + 1)
  45.         io.write("|")
  46.         term.setCursorPos(x, y + 2)
  47.         io.write("+--------------------+")
  48.         term.setCursorPos(x + 1, y + 1)
  49.         io.write(text)
  50.         term.setCursorPos(x +  21, y + 1)
  51.         io.write("|")
  52.         term.setCursorPos(1, y + 3)
  53.  
  54.     end
  55.  
  56. end
  57.  
  58. ------------------------------------------------------------------
  59.  
  60. function newTextField(unit, x, y)
  61.  
  62.     if not fs.exists("MTextFieldDir") then
  63.  
  64.         fs.makeDir("MTextFieldDir")
  65.  
  66.     end
  67.    
  68.     file = io.open("MTextFieldDir/"..unit..".txt", "w")
  69.     file:write(x.."\n"..y.."\n\nfalse")
  70.     file:close()
  71.  
  72. end
  73.  
  74. ------------------------------------------------------------------
  75.  
  76. function setVisible(unit, bVisible)
  77.  
  78.     blah, text, x, y = getPos(unit)
  79.     local tLines = {}
  80.  
  81.     if bVisible == true then
  82.  
  83.         term.setCursorPos(x, y)
  84.         io.write("+--------------------+")
  85.         term.setCursorPos(x, y + 1)
  86.         io.write("|")
  87.         term.setCursorPos(x, y + 2)
  88.         io.write("+--------------------+")
  89.         term.setCursorPos(x + 1, y + 1)
  90.         io.write(text)
  91.         term.setCursorPos(x +  21, y + 1)
  92.         io.write("|")
  93.         term.setCursorPos(1, y + 3)
  94.         file = io.open("MTextFieldDir/"..unit..".txt", "r")
  95.  
  96.         table.insert(tLines, 1, file:read("*l"))
  97.         table.insert(tLines, 2, file:read("*l"))
  98.         table.insert(tLines, 3, file:read("*l"))
  99.         table.insert(tLines, 4, file:read("*l"))
  100.  
  101.         file:close()
  102.  
  103.         tLines[4] = "true"
  104.  
  105.         file = io.open("MTextFieldDir/"..unit..".txt", "w")
  106.         file:write(tLines[1].."\n"..tLines[2].."\n"..tLines[3].."\n"..tLines[4])
  107.         file:close()
  108.    
  109.     elseif bVisible == false then
  110.  
  111.         term.setCursorPos(x, y)
  112.         print("                      ")
  113.         print("                      ")
  114.         print("                      ")
  115.         term.setCursorPos(1, y + 3)
  116.        
  117.         file = io.open("MTextFieldDir/"..unit..".txt", "r")
  118.  
  119.         table.insert(tLines, 1, file:read("*l"))
  120.         table.insert(tLines, 2, file:read("*l"))
  121.         table.insert(tLines, 3, file:read("*l"))
  122.         table.insert(tLines, 4, file:read("*l"))
  123.  
  124.         file:close()
  125.  
  126.         tLines[4] = "false"
  127.  
  128.         file = io.open("MTextFieldDir/"..unit..".txt", "w")
  129.         file:write(tLines[1].."\n"..tLines[2].."\n"..tLines[3].."\n"..tLines[4])
  130.         file:close()
  131.  
  132.     end
  133.  
  134. end
  135.  
  136. ------------------------------------------------------------------
  137.  
  138. function getText(unit)
  139.  
  140.     file = io.open("MTextFieldDir/"..unit..".txt", "r")
  141.     text = file:read("*l")
  142.     text = file:read("*l")
  143.     text = file:read("*l")
  144.     file:close()
  145.  
  146.     return text
  147.  
  148. end
Advertisement
Add Comment
Please, Sign In to add comment