Advertisement
MaximumFrank

func

Mar 25th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. function sides()
  2. return "top", "bottom", "left", "right", "front", "back"
  3. end
  4.  
  5. function drawCenteredX(str, y, bgCol, fgCol)
  6. if fgCol ~= nil then
  7. term.setTextColor(fgCol)
  8. end
  9.  
  10. term.setBackgroundColor(bgCol)
  11.  
  12. local temp = term.getBackgroundColor()
  13.  
  14. local length = (w - string.len(str)) / 2
  15. term.setCursorPos(length, y)
  16. print(str)
  17.  
  18. term.setBackgroundColor(temp)
  19. end
  20.  
  21. function validChar(key)
  22. if key >= 2 and key <= 13 then
  23.  
  24. elseif key >= 16 and key <= 27 then
  25. return 1
  26. elseif key >= 30 and key <= 41 then
  27. return 1
  28. elseif key >= 43 and key <=53 then
  29. return 1
  30. elseif key >= 71 and key <= 83 then
  31. return 1
  32. elseif key == 181 or key == 55 then
  33. return 1
  34. else
  35. return 0
  36. end
  37. end
  38.  
  39. function readCentered(pref, rep)
  40. term.setBackgroundColor(bgCol)
  41. term.setTextColor(tbxCol)
  42.  
  43. local cin = ""
  44. local strLen = nil
  45. local kTemp = nil
  46.  
  47. term.setCursorPos((w - 1)/2, h/2)
  48.  
  49. --draw prefix if not nil
  50. if pref ~= nil then
  51. write(pref)
  52. end
  53.  
  54. while true do
  55. --super stylish cursor blink!
  56. term.setCursorBlink(true)
  57.  
  58. --ask for keypress
  59. local key = {os.pullEvent()}
  60.  
  61. if key[1] == "char" then
  62. if kTemp == true then
  63. --do nothing
  64. else
  65. --combine the keystrokes
  66. cin = cin .. key[2]
  67.  
  68. --get length of the string for offset
  69. strLen = string.len(cin) + string.len(pref)
  70.  
  71. --set new cursor position
  72. term.setCursorPos((w - strLen)/2, h/2)
  73.  
  74. --actually draw the string
  75. if pref ~= nil then
  76. if rep == nil then
  77. write(pref .. cin)
  78. else
  79. local ciw = string.rep(rep, string.len(cin))
  80. write(pref .. ciw)
  81. end
  82. else
  83. if rep == nil then
  84. write(cin)
  85. else
  86. local ciw = string.rep(rep, string.len(cin))
  87. write(ciw)
  88. end
  89. end
  90. end
  91. elseif key[1] == 'key' then
  92. if validChar(key[2]) == 1 then
  93.  
  94. elseif key[2] == 14 then
  95. --get length of string for offset
  96. strLen = string.len(cin)
  97.  
  98. --get rid of last letter
  99. cin = string.sub(cin, 1, strLen - 1)
  100.  
  101. if pref ~= nil then
  102. --get length of string for offset
  103. strLen = string.len(cin) + string.len(pref)
  104. else
  105. strLen = string.len(cin)
  106. end
  107.  
  108. --set new cursor position
  109. term.setCursorPos((w - strLen)/2, h/2)
  110.  
  111. term.clearLine()
  112.  
  113. if pref ~= nil then
  114. if rep == nil then
  115. write(pref .. cin)
  116. else
  117. local ciw = string.rep(rep, string.len(cin))
  118. write(pref..ciw)
  119. end
  120. else
  121. if rep == nil then
  122. write(cin)
  123. else
  124. local ciw = string.rep(rep, string.len(cin))
  125. write(ciw)
  126. end
  127. end
  128. elseif key[2] == 28 then
  129. term.setCursorBlink(false)
  130. return cin
  131. end
  132. end
  133. end
  134. term.setCursorBlink(false)
  135. end
  136.  
  137. function getY()
  138. local coords = {term.getCursorPos()}
  139. return coords[2]
  140. end
  141.  
  142. function drawRect(x1, y1, x2, y2, bgCol)
  143. local i = 0
  144. local j = 0
  145.  
  146. term.setCursorPos(x1, y1)
  147.  
  148. local temp = term.getBackgroundColor()
  149. term.setBackgroundColor(bgCol)
  150.  
  151. for i = y1, y2 do
  152. for j = x1, x2 do
  153. term.setCursorPos(j, i)
  154. write(' ')
  155. end
  156. end
  157.  
  158. term.setBackgroundColor(temp)
  159. end
  160.  
  161. function getCol(uname)
  162. os.loadAPI(usrPath .. uname .. "/settings")
  163. tb = settings.tbCol
  164. tbx = settings.tbxCol
  165. tx = settings.txCol
  166. bg = settings.bgCol
  167. stx = settings.stxCol
  168. os.unloadAPI(usrPath .. uname .. "/settings")
  169.  
  170. return tb, tbx, tx, bg, stx
  171. end
  172.  
  173. function drawTableCentered(t, bgCol, fgCol, lSel, uname)
  174. local i = 0
  175. local height = (h - #t) / 2
  176. local longest = 0
  177. tw = w / 2
  178.  
  179. for i = 1, #t do
  180. len = string.len(t[i])
  181.  
  182. if len > longest then
  183. longest = len
  184. end
  185. end
  186.  
  187. drawRect(tw - (longest), height, tw + (longest), height + #t + 1, bgCol)
  188.  
  189. for i = 1, #t do
  190. if i == lSel then
  191. local cols = {}
  192. if uname == nil then
  193. cols = {0, 0, 0, 0, stxCol}
  194. else
  195. cols = {getCol(uname)}
  196. end
  197.  
  198. drawCenteredX(t[i], height + i, bgCol, cols[5])
  199. else
  200. drawCenteredX(t[i], height + i, bgCol, fgCol)
  201. end
  202. end
  203. end
  204.  
  205. function getMenuKey()
  206. local event, param = os.pullEvent()
  207. if event == "key" then
  208. if param == 200 then
  209. return 1
  210. elseif param == 208 then
  211. return -1
  212. elseif param == 28 then
  213. return 2
  214. else
  215. return 0
  216. end
  217. elseif lockout ~= nil then
  218. if event == "timer" then
  219. return 3
  220. end
  221. else
  222. return 0
  223. end
  224. end
  225.  
  226. function shutdown()
  227. os.unloadAPI(rootPath .. "params")
  228. local file = fs.open(rootPath .. "params", "r")
  229. local data = file.readAll()
  230. file.close()
  231.  
  232. data = string.gsub(data, 'crash = 1', 'crash = 0')
  233.  
  234. file = fs.open(rootPath .. "params", "w")
  235. file.write(data)
  236. file.close()
  237.  
  238. os.shutdown()
  239. end
  240.  
  241. function restart()
  242. os.unloadAPI(rootPath .. "params")
  243. local file = fs.open(rootPath .. "params", "r")
  244. local data = file.readAll()
  245. file.close()
  246.  
  247. data = string.gsub(data, 'crash = 1', 'crash = 0')
  248.  
  249. file = fs.open(rootPath .. "params", "w")
  250. file.write(data)
  251. file.close()
  252.  
  253. os.reboot()
  254. end
  255.  
  256. function resetCol()
  257. if term.isColor() then
  258. tbCol = params.tbColMain
  259. tbxCol = params.tbxColMain
  260. txCol = params.txColMain
  261. bgCol = params.bgCol
  262. stxCol = params.stxColMain
  263. else
  264. tbCol = params.tbMain
  265. tbxCol = params.tbMain
  266. txCol = params.txMain
  267. bgCol = params.bgCol
  268. stxCol = params.stxMain
  269. end
  270.  
  271. return tbCol, tbxCol, txCol, bgCol, stxCol
  272. end
  273.  
  274. function clear(uname)
  275. if uname ~= nil then
  276. cols = {getCol(uname)}
  277. term.setBackgroundColor(cols[4])
  278. else
  279. term.setBackgroundColor(bgCol)
  280. end
  281. term.clear()
  282. term.setCursorPos(1, 1)
  283. end
  284.  
  285. function prgSort()
  286.  
  287. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement