Advertisement
ravneravn

build board

May 21st, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.69 KB | None | 0 0
  1.  
  2.  
  3. side = "right"
  4. pSide = "top"
  5. mon = peripheral.wrap(side)
  6. w, h = mon.getSize()
  7. project = {}
  8. mainMenu = {"Rules", "View projects"}
  9. yourProjects = {}
  10. names = {}
  11. nameList = {}
  12. screen = 0
  13. deleteYes = false
  14. deleteNo = false
  15. goBack = false
  16. backToMainMenu = false
  17. selectedName = false
  18. player = peripheral.wrap(pSide)
  19. screen = 0
  20. rules = {[1] = "- No griefing -", [2] = "- No stealing -", [3] = "- No cheating in any way -", [4] = "- PvP only on agreement -", [5] = "- Be friendly and respectful -", [6] = "- Have fun -", [7] = "- Don't break the rules -"}
  21. for i = 1, 100 do
  22. names[i] = {}
  23. end
  24.  
  25.  
  26.  
  27. function checkFile(path) -- check if file exists
  28. if not fs.exists(path) then
  29. file = fs.open(path, "w")
  30. file.close()
  31. end
  32. end
  33.  
  34.  
  35. function writeFile(fileName, tableName) -- overwrites an old file
  36. file = fs.open(fileName, "w")
  37. file.write(textutils.serialize(tableName))
  38. file.close()
  39. end
  40.  
  41.  
  42. function loadProjects(load) -- Load filed called projects
  43. checkFile(load)
  44. file = fs.open(load, "r")
  45. readFile = file.readLine()
  46. if readFile ~= nil then
  47. project = textutils.unserialize(readFile)
  48. end
  49. file.close()
  50. end
  51.  
  52. function loadNames(load) -- load file called names
  53. checkFile(load)
  54. file = fs.open(load, "r")
  55. readFile = file.readLine()
  56. if readFile ~= nil then
  57. names = textutils.unserialize(readFile)
  58. end
  59. file.close()
  60. end
  61.  
  62. function loadRules(load) -- load file called rules
  63. checkFile(load)
  64. file = fs.open(load, "r")
  65. readFile = file.readLine()
  66. if readFile ~= nil then
  67. rules = textutils.unserialize(readFile)
  68. end
  69. file.close()
  70. end
  71.  
  72.  
  73.  
  74.  
  75.  
  76. function getInputProject() -- read user input and inserts it to the project table
  77. term.setCursorPos(w/2, h-2)
  78. term.setBackgroundColor(colors.black)
  79. term.setTextColor(colors.white)
  80. input = read()
  81. table.insert(project, input)
  82. end
  83.  
  84.  
  85. function getInputName(projectName) -- read user input and inserts it to table in names table.
  86. term.setCursorPos(w/2, h-2)
  87. term.setBackgroundColor(colors.black)
  88. term.setTextColor(colors.white)
  89. input = read()
  90. i = projectName
  91. table.insert(names[i], input)
  92. end
  93.  
  94. function drawImage(image, x, y)
  95. myImage = paintutils.loadImage(image)
  96. paintutils.drawImage(myImage, x, y)
  97. end
  98.  
  99.  
  100.  
  101. function viewProjects() -- displays stored projects
  102. term.clear()
  103. mWrite("<--", 1, h, colors.black, colors.white)
  104. x = 1
  105. y = 1
  106. mWrite("New project", (w-11)/2, h, colors.black, colors.white)
  107. if table.getn(project) > 0 then
  108. for i = 1, #project do
  109. drawImage("button", x, y)
  110. cY = y +2
  111. cX = x+1
  112. y = y + 4
  113. mWrite(project[i], cX, cY, colors.lime)
  114. mWrite("[X]", w-2, 1, colors.black, colors.red)
  115. if y >= h - 4 then y = 1 x = x+12 end
  116. end
  117. else
  118. mWrite("There are currently no projects", (w-31)/2, h/2, colors.white)
  119. end
  120. end
  121.  
  122.  
  123. function startMenu() -- displays start menu
  124. term.clear()
  125. drawImage("header", 1, 1)
  126. mWrite("Build Board", (w-11)/2, 2, colors.lightGray)
  127. x = (w/8)+2
  128. y = h/2
  129. cY = y+1
  130. cX = x +5
  131. for i = 1, #mainMenu do
  132. drawImage("main", x, y)
  133. mWrite(mainMenu[i], cX, cY, colors.white)
  134. cX = cX + 16
  135. x = x+20
  136. end
  137. end
  138.  
  139.  
  140. function rulesDisplay()
  141. term.clear()
  142. -- mWrite("Add rule", (w-8)/2, h, colors.lightBlue, colors.white)
  143. y = ((h-(#rules*2))/2)+2
  144. drawImage("rulesScreen", 1,1)
  145. mWrite("<--", 1, h, colors.lightBlue, colors.white)
  146. for i = 1, #rules do
  147. x = (w-#rules[i])/2
  148. mWrite(rules[i], x, y, colors.white)
  149. y= y+2
  150. end
  151. rulesTouch()
  152. end
  153.  
  154.  
  155.  
  156. function drawBoard() -- decides which menu to draw
  157. term.clear()
  158. startMenu()
  159. if screen == 0 then
  160. event, param1, posx, posy = os.pullEvent("monitor_touch")
  161. if posx > 27 and posx < 44 and posy > (h/2)-1 and posy < (h/2)+3 then screen = 1
  162. elseif posx > 7 and posx < 24 and posy > (h/2)-1 and posy < (h/2)+3 then screen = -1
  163. end
  164. end
  165. if screen == 0 then
  166. startMenu()
  167. --touch()
  168. elseif screen == 1 then
  169. viewProjects()
  170. inputProject()
  171. elseif screen == -1 then
  172. rulesDisplay()
  173. end
  174. end
  175.  
  176.  
  177.  
  178. function mWrite(message, cX, cY, background, text) -- writes message on cX and cY with background as background color
  179. mon.setBackgroundColor(background)
  180. if text == nil then mon.setTextColor(colors.black)
  181. else mon.setTextColor(text) end
  182. mon.setCursorPos(cX, cY)
  183. mon.write(message)
  184. mon.setBackgroundColor(colors.black)
  185. end
  186.  
  187.  
  188. function rulesTouch() -- Toggle between menues
  189. event, param1, x, y = os.pullEvent()
  190. if event == "monitor_touch" and x < 4 and y == h then screen = screen +1
  191. if screen == 2 then screen = 0 end
  192. -- elseif event == "monitor_touch" and x > (w-8)/2 and x < ((w-8)/2)+8 and y == h then
  193. -- newRule = read()
  194. -- table.insert(rules, newRule)
  195. -- writeFile("rules", rules)
  196. end
  197. end
  198.  
  199.  
  200. function inputProject() -- add a project to the list
  201. event, param1, x, y = os.pullEvent()
  202. if event == "monitor_touch" and x > (w-13)/2 and x < (w+11)/2 and y == h then
  203. getInputProject()
  204. writeFile("projects", project)
  205. elseif event == "monitor_touch" and x > w-3 and y == 1 then selectDeleteProject()
  206. elseif event == "monitor_touch" and x < 4 and y == h then screen = screen +1
  207. if screen == 2 then screen = 0 end
  208. else clickProject()
  209. end
  210. end
  211.  
  212.  
  213.  
  214. function clickProject() -- determines which project is clicked
  215. if event == "monitor_touch" and x > 1 and x < 13 and y > 1 and y < 5 and table.getn(project) > 0 then enterProject(1)
  216. elseif event == "monitor_touch" and x > 1 and x < 13 and y > 5 and y <9 and table.getn(project) > 1 then enterProject(2)
  217. elseif event == "monitor_touch" and x > 1 and x < 13 and y > 9 and y < 13 and table.getn(project) > 2 then enterProject(3)
  218. elseif event == "monitor_touch" and x > 1 and x < 13 and y > 13 and y < 17 and table.getn(project) > 3 then enterProject(4)
  219.  
  220. elseif event == "monitor_touch" and x > 13 and x < 25 and y > 1 and y < 5 and table.getn(project) > 4 then enterProject(5)
  221. elseif event == "monitor_touch" and x > 13 and x < 25 and y > 5 and y <9 and table.getn(project) > 5 then enterProject(6)
  222. elseif event == "monitor_touch" and x > 13 and x < 25 and y > 9 and y < 13 and table.getn(project) > 6 then enterProject(7)
  223. elseif event == "monitor_touch" and x > 13 and x < 25 and y > 13 and y < 17 and table.getn(project) > 7 then enterProject(8)
  224.  
  225. elseif event == "monitor_touch" and x > 25 and x < 37 and y > 1 and y < 5 and table.getn(project) > 8 then enterProject(9)
  226. elseif event == "monitor_touch" and x > 25 and x < 37 and y > 5 and y < 9 and table.getn(project) > 9 then enterProject(10)
  227. elseif event == "monitor_touch" and x > 25 and x < 37 and y > 9 and y < 13 and table.getn(project) > 10 then enterProject(11)
  228. elseif event == "monitor_touch" and x > 25 and x < 37 and y > 13 and y < 17 and table.getn(project) > 11 then enterProject(12)
  229.  
  230. elseif event == "monitor_touch" and x > 37 and x < 49 and y > 1 and y < 5 and table.getn(project) > 12 then enterProject(13)
  231. elseif event == "monitor_touch" and x > 37 and x < 49 and y > 5 and y < 9 and table.getn(project) > 13 then enterProject(14)
  232. elseif event == "monitor_touch" and x > 37 and x < 49 and y > 9 and y < 13 and table.getn(project) > 14 then enterProject(15)
  233. elseif event == "monitor_touch" and x > 37 and x < 49 and y > 13 and y < 17 and table.getn(project) > 15 then enterProject(16)
  234.  
  235. elseif event == "monitor_touch" and y == h and x < 4 then screen = screen +1
  236. if screen == 2 then screen = 1 end
  237. end
  238. end
  239.  
  240.  
  241.  
  242.  
  243. function enterProject(projectName) -- see who is working on clicked project
  244. currProject = projectName
  245. while true do
  246. term.clear()
  247. mWrite("<--", 1, h, colors.black, colors.white)
  248. drawImage("header", 1,1)
  249. mWrite(project[projectName], (w-#project[projectName])/2, 2, colors.lightGray)
  250. mWrite("Add name", (w-8)/2, h, colors.black, colors.white)
  251. mWrite("[X]", w-2, 1, colors.lightGray, colors.red)
  252. x = 1
  253. y = 5
  254. if names[projectName] ~= nil then
  255. if table.getn(names[projectName]) > 0 then
  256. for i = 1, #names[projectName] do
  257. drawImage("button", x, y)
  258. cY = y +2
  259. cX = x+1
  260. y = y + 4
  261. mWrite(names[projectName][i], cX, cY, colors.lime)
  262. if y >= h - 4 then y = 5 x = x+12 end
  263. end
  264. else
  265. mWrite("No one is working on this project", (w-33)/2, h/2, colors.white)
  266. end
  267. end
  268. inputName(projectName)
  269. if backToMain == true then
  270. backToMain = false
  271. break
  272. end
  273. end
  274. end
  275.  
  276.  
  277. function selectDeleteName(projectName) -- select a name for delete
  278. currProject = projectName
  279. while true do
  280. term.clear()
  281. mWrite("<--", 1, h, colors.black, colors.white)
  282. drawImage("header", 1,1)
  283. mWrite(project[projectName], (w-#project[projectName])/2, 2, colors.lightGray)
  284. x = 1
  285. y = 5
  286. if names[projectName] ~= nil then
  287. if table.getn(names[projectName]) > 0 then
  288. for i = 1, #names[projectName] do
  289. drawImage("buttonGray", x, y)
  290. cY = y +2
  291. cX = x+1
  292. y = y + 4
  293. mWrite(names[projectName][i], cX, cY, colors.lightGray)
  294. if y >= h - 4 then y = 5 x = x+12 end
  295. end
  296. else
  297. mWrite("No one is working on this project", (w-33)/2, h/2, colors.white)
  298. end
  299. end
  300. activeProject = projectName
  301. clickName(projectName)
  302. if selectedName ~= false then
  303. deleteName(selectedName, projectName)
  304. selectedName = false
  305. end
  306. if goBack == true then
  307. goBack = false
  308. break
  309. end
  310. end
  311. end
  312.  
  313.  
  314.  
  315. function tableCounter(projectName)
  316. number = 0
  317. for k, v in pairs(names[projectName]) do
  318. number = number +1
  319. end
  320. return tonumber(number)
  321. end
  322.  
  323. function clickName(projectName)
  324. event, param1, x, y = os.pullEvent("monitor_touch")
  325. if event == "monitor_touch" then notInUse = 1 end
  326. event, param1, x, y = os.pullEvent("monitor_touch")
  327. if x > 1 and x < 13 and y > 5 and y < 9 then selectedName = 1
  328. elseif x > 1 and x < 13 and y > 9 and y <13 then selectedName = 2
  329. elseif x > 1 and x < 13 and y > 13 and y < 17 then selectedName = 3
  330.  
  331. elseif x > 13 and x < 25 and y > 5 and y < 9 then selectedName = 4
  332. elseif x > 13 and x < 25 and y > 9 and y <13 then selectedName = 5
  333. elseif x > 13 and x < 25 and y > 13 and y < 17 then selectedName = 6
  334.  
  335. elseif x > 25 and x < 37 and y > 5 and y < 9 then selectedName = 7
  336. elseif x > 25 and x < 37 and y > 9 and y < 13 then selectedName = 8
  337. elseif x > 25 and x < 37 and y > 13 and y < 17 then selectedName = 9
  338.  
  339. elseif x > 37 and x < 49 and y > 5 and y < 9 then selectedName = 10
  340. elseif x > 37 and x < 49 and y > 9 and y < 13 then selectedName = 11
  341. elseif x > 37 and x < 49 and y > 13 and y < 17 then selectedName = 12
  342.  
  343. elseif y == h and x < 4 then goBack = true
  344. end
  345. end
  346.  
  347.  
  348. function deleteName(selectedName, projectName)
  349. currProject = projectName
  350. selectedName = selectedName
  351. term.clear()
  352. drawImage("yes", (w/4), h/2)
  353. drawImage("no", (w/4)*3, h/2)
  354. mWrite("Yes", (w/4)+1, (h/2)+1, colors.white)
  355. mWrite("No", (w/4)*3+1, (h/2)+1, colors.white)
  356.  
  357. event, param1, x, y = os.pullEvent("monitor_touch")
  358. if x > (w/4)-1 and x < (w/4)+5 and y > (h/2)-1 and y < (h/2) +3 then deleteYes = true
  359. elseif x > (w/4)*3-1 and x < (w/4)*3+5 and y > (h/2)-1 and y < (h/2) +3 then deleteNo = true
  360. end
  361.  
  362. if deleteNo == true then goBack = true deleteNo = false
  363. elseif deleteYes == true then table.remove(names[projectName], selectedName)
  364. goBack = true
  365. deleteYes = false
  366. writeFile("names", names)
  367. end
  368. end
  369.  
  370.  
  371.  
  372. function inputName(projectName) -- add a name to a project
  373. currProject = projectName
  374. event, param1, x, y = os.pullEvent("monitor_touch")
  375. if event == "monitor_touch" and x > (w-11)/2 and x < (w+11)/2 and y == h then
  376. getInputName(projectName)
  377. writeFile("names", names)
  378. elseif event == "monitor_touch" and x > w-3 and y == 1 then selectDeleteName(projectName)
  379. elseif event == "monitor_touch" and x < 4 and y > h-2 then
  380. screen = screen +1
  381. backToMain = true
  382. if screen == 2 then screen = 1 end
  383. end
  384. end
  385.  
  386.  
  387.  
  388. function selectDeleteProject() -- select a project for delete
  389. while true do
  390. term.clear()
  391. x = 1
  392. y = 1
  393. if table.getn(project) > 0 then
  394. for i = 1, #project do
  395. drawImage("buttonGray", x, y)
  396. cY = y +2
  397. cX = x+1
  398. y = y + 4
  399. mWrite(project[i], cX, cY, colors.lightGray)
  400. if y >= h - 4 then y = 1 x = x+12 end
  401. end
  402. else
  403. mWrite("There are currently no projects", (w-31)/2, h/2, colors.white)
  404. end
  405. pickProject()
  406. if goBack == true then
  407. goBack = false
  408. break
  409. end
  410. end
  411. end
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420. function pickProject() -- registers which project you want to delete
  421. event, param1, x, y = os.pullEvent("monitor_touch")
  422. if event == "monitor_touch" then notInUse = 1 end
  423. event, param1, x, y = os.pullEvent("monitor_touch")
  424. if x > 1 and x < 13 and y > 1 and y < 5 then deleteProject(1)
  425. elseif x > 1 and x < 13 and y > 5 and y <9 then deleteProject(2)
  426. elseif x > 1 and x < 13 and y > 9 and y < 13 then deleteProject(3)
  427. elseif x > 1 and x < 13 and y > 13 and y < 17 then deleteProject(4)
  428.  
  429. elseif x > 13 and x < 25 and y > 1 and y < 5 then deleteProject(5)
  430. elseif x > 13 and x < 25 and y > 5 and y <9 then deleteProject(6)
  431. elseif x > 13 and x < 25 and y > 9 and y < 13 then deleteProject(7)
  432. elseif x > 13 and x < 25 and y > 13 and y < 17 then deleteProject(8)
  433.  
  434. elseif x > 25 and x < 37 and y > 1 and y < 5 then deleteProject(9)
  435. elseif x > 25 and x < 37 and y > 5 and y < 9 then deleteProject(10)
  436. elseif x > 25 and x < 37 and y > 9 and y < 13 then deleteProject(11)
  437. elseif x > 25 and x < 37 and y > 13 and y < 17 then deleteProject(12)
  438.  
  439. elseif x > 37 and x < 49 and y > 1 and y < 5 then deleteProject(13)
  440. elseif x > 37 and x < 49 and y > 5 and y < 9 then deleteProject(14)
  441. elseif x > 37 and x < 49 and y > 9 and y < 13 then deleteProject(15)
  442. elseif x > 37 and x < 49 and y > 13 and y < 17 then deleteProject(16)
  443.  
  444. elseif event == "monitor_touch" and y == h and x < 4 then goBack = true
  445. if screen == 2 then screen = 1 end
  446. end
  447. end
  448.  
  449.  
  450. function deleteProject(selectedProject) -- deletes selected project
  451. term.clear()
  452. drawImage("yes", (w/4), h/2)
  453. drawImage("no", (w/4)*3, h/2)
  454. mWrite("Yes", (w/4)+1, (h/2)+1, colors.white)
  455. mWrite("No", (w/4)*3+1, (h/2)+1, colors.white)
  456.  
  457. event, param1, x, y = os.pullEvent("monitor_touch")
  458. if x > (w/4)-1 and x < (w/4)+5 and y > (h/2)-1 and y < (h/2) +3 then deleteYes = true
  459. elseif x > (w/4)*3-1 and x < (w/4)*3+5 and y > (h/2)-1 and y < (h/2) +3 then deleteNo = true
  460. end
  461.  
  462. if deleteNo == true then goBack = true deleteNo = false
  463. elseif deleteYes == true then table.remove(project, selectedProject)
  464. goBack = true
  465. deleteYes = false
  466. writeFile("projects", project)
  467. end
  468. end
  469.  
  470.  
  471.  
  472. ---------- LOOP -------------
  473. term.redirect(mon)
  474. while true do
  475. loadProjects("projects")
  476. loadNames("names")
  477. -- loadRules("rules")
  478. drawBoard()
  479. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement