Guest User

Untitled

a guest
Feb 7th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. local run = true
  2. local sItem = 1
  3. local tW,tH = term.getSize()
  4. local tMen = {}
  5. local run = true
  6. local cMen = 1
  7.  
  8. -- Menu functions
  9. function sMenu(menu)
  10. local h = fs.open("menus","a")
  11. h.writeLine(textutils.serialize(menu))
  12. h.close()
  13. end
  14.  
  15. function ex()
  16. for i=1, #tMen do
  17. for j=1,#(tMen[i].choice) do
  18. if tMen[i].choice[j].name == tMen[cMen].name then
  19. cMen = i
  20. sItem = 1
  21. return
  22. end
  23. end
  24. end
  25. term.clear()
  26. term.setCursorPos(1,1)
  27. run = false
  28. end
  29.  
  30. function wizard()
  31. local counter = 1
  32. local name = nil
  33. local choice = {}
  34. term.clear()
  35. term.setCursorPos(1,1)
  36. print("This wizards guides you through the Menu Creation!")
  37. print("Please insert a Name for your Menu")
  38. name = read()
  39. print("Please insert the Choices of your Menu;Exit by typing 'exit'")
  40. while true do
  41. print("Choice "..tostring(counter))
  42. local tmp = read()
  43. if tmp == "exit" then
  44. break
  45. elseif counter > 7 then
  46. print("To much entries; Please create SubMenus")
  47. break
  48. end
  49. choice[counter] = {}
  50. print("please insert the Function it should start;for a submenu type 'submenu'")
  51. local tmp2 = read()
  52. if tmp2 == "submenu" then
  53. print("What is the name of the connected Menu?")
  54. local tmp3 = read()
  55. choice[counter].name = tmp3
  56. else
  57. choice[counter].name = nil
  58. end
  59.  
  60. choice[counter].text = tmp
  61. choice[counter].handler = tmp2
  62.  
  63. counter = counter + 1
  64. end
  65. choice[counter] = {}
  66. choice[counter].text = "Exit"
  67. choice[counter].handler = "exx"
  68.  
  69.  
  70. local menu = {}
  71. menu.name = name
  72. menu.choice = choice
  73. sMenu(menu)
  74. end
  75.  
  76. function gMenuCount()
  77. if not fs.exists("menus") then
  78. return 0
  79. else
  80. local counter = 0
  81. local h = fs.open("menus","r")
  82. while h.readLine() ~= nil do
  83. counter = counter + 1
  84. end
  85. h.close()
  86. return counter
  87. end
  88. end
  89.  
  90. function gMenu(nr)
  91. if not fs.exists("menus") then
  92. return "no file"
  93. elseif nr > gMenuCount() then
  94. return "does not exist"
  95. else
  96. local h = fs.open("menus","r")
  97. for i = 1,(nr-1) do
  98. h.readLine()
  99. end
  100. local menu = textutils.unserialize(h.readLine())
  101. if menu == nil then
  102. return "menu was nil"
  103. else
  104. return menu
  105. end
  106. end
  107. end
  108.  
  109. function gtMen()
  110. if not fs.exists("menus") then
  111. return false
  112. end
  113. print("done gtMen test")
  114. local i = 1
  115. local h = fs.open("menus","r")
  116. local tmp = h.readLine()
  117. while not (tmp == nil) do
  118. print("gtMen inLoop")
  119. tmp = textutils.unserialize(tmp)
  120. print("start gtMen test 2 ")
  121. if not (tmp == nil) then
  122. tMen[i] = {}
  123. tMen[i] = tmp
  124. end
  125. print("done gtMen test 2")
  126. i = i +1
  127. tmp = h.readLine()
  128. end
  129. h.close()
  130. print("done gtMen")
  131. end
  132.  
  133. -- Print
  134. function cbstr(str,char)
  135. if char == nil then
  136. char = " "
  137. end
  138. local v = math.floor((tW-string.len(str))/2)
  139. for i=1,(v-1) do
  140. str = string.format(char..str)
  141. end
  142. for i = string.len(str),tW-1 do
  143. str = string.format(str..char)
  144. end
  145. return str
  146. end
  147.  
  148. function chprint(strtbl)
  149. if strtbl == nil then
  150. strtbl = {"Test1","Test2","Test3"}
  151. end
  152. local space = 3
  153. local k = 0
  154. local v = math.floor((tH-#strtbl)/2)
  155. if v > space then
  156. while k < (space*2 -tH + #strtbl)/(#strtbl-1) do
  157. k = k+1
  158. end
  159. else
  160. space = 0
  161. end
  162. for i=1,#strtbl do
  163. for j=1,space do
  164. print(" ")
  165. end
  166. print(strtbl[i])
  167. for j=1,k do
  168. print(" ")
  169. end
  170. end
  171. end
  172.  
  173. function printMenu()
  174. print(cbstr(tMen[cMen].name,"-"))
  175. local strtbl = {}
  176. for i=1,#(tMen[cMen].choice) do
  177. if i == sItem then
  178. strtbl[i] =cbstr("[ "..tostring(tMen[cMen].choice[i].text).." ]")
  179. else
  180. strtbl[i] =cbstr(" "..tostring(tMen[cMen].choice[i].text).." ")
  181. end
  182. end
  183. chprint(strtbl)
  184. end
  185.  
  186. -- Handler
  187. function onKeyPressed(key)
  188. if key == keys.enter then
  189. onItemS()
  190. elseif key == keys.up then
  191. if sItem > 1 then
  192. sItem = sItem - 1
  193. end
  194. elseif key == keys.down then
  195. if sItem < #(tMen[cMen].choice) then
  196. sItem = sItem + 1
  197. end
  198. end
  199. end
  200.  
  201. function onItemS()
  202. if tMen[cMen].choice[sItem].handler == "submenu" then
  203. for i = 1,#tMen do
  204. if tMen[i].name == tMen[cMen].choice[sItem].name then
  205. cMen = i
  206. return
  207. end
  208. end
  209. elseif tMen[cMen].choice[sItem].handler == "exx" then
  210. ex()
  211. else
  212. local h = loadstring(tMen[cMen].choice[sItem].handler)
  213. h()
  214. end
  215. end
  216.  
  217. -- Main
  218. function main()
  219. run = true
  220. gtMen()
  221. while run do
  222. term.clear()
  223. term.setCursorPos(1,1)
  224. printMenu()
  225. local event,key = os.pullEvent("key")
  226. onKeyPressed(key)
  227. end
  228. end
Advertisement
Add Comment
Please, Sign In to add comment