Advertisement
kylergs

GUI 1.1.3

Nov 26th, 2012
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.63 KB | None | 0 0
  1. -- Basic Functions --
  2.  
  3. local user = "guest"
  4. local select = 1
  5. local rsel = 1
  6. local fitem = 1
  7. local mSel = 1
  8. local l = 1
  9. local menustate = "admin"
  10. w,h = term.getSize()
  11.  
  12. function isInteger(nIn)
  13.     if math.floor(nIn) == nIn then return true
  14.     else return false end
  15. end
  16. function printCentred(str , h)
  17.   term.setCursorPos(w/2 - #str/2, h)
  18.   term.write(str)
  19. end
  20. function printRight(str , h)
  21.   term.setCursorPos(w - #str, h)
  22.   term.write(str)
  23. end
  24. function printLeft(str , h)
  25.   term.setCursorPos(1, h)
  26.   term.write(str)
  27. end
  28.  
  29. function returnFileAsTable(aFile) --This function and the two bellow it are credit of Challenge.Accepted on the ComputerCraft Forums
  30.     f= fs.open(aFile,"r")
  31.     aTable = {}
  32.     aLine = f.readLine()
  33.     while aLine ~= nil do
  34.         table.insert(aTable, aLine)
  35.         aLine = f.readLine()
  36.     end
  37.     return aTable
  38. end
  39. function seperateString(aString, sep)
  40.     p = string.find(aString, sep)
  41.     return string.sub(aString, 1, p-1), string.sub(aString, p+1)
  42. end
  43. function iterateTable(aTable)
  44.     local keyTable = {}
  45.     local definitionTable = {}
  46.     for i=1, #aTable do
  47.         key, def = seperateString(aTable[i], ":")
  48.         table.insert(keyTable, key)
  49.         table.insert(definitionTable, def)
  50.     end
  51.     return keyTable, definitionTable
  52. end
  53.  
  54. -- Menus --
  55.  
  56. function drawHeader()
  57.   term.setTextColour(colours.white)
  58.   printCentred("KYLEOS - TEST 1.0", 1)
  59.   printLeft(string.rep("-",w),2)
  60.   printLeft(string.rep("-",w),h-1)
  61.   printLeft(menustate.." : "..user,h)
  62.   printRight("made by kylergs",h)
  63. end
  64.  
  65. -- Menu state --
  66.  
  67. local path = "GUI/GUImopt"
  68. fs.makeDir(path)
  69.  
  70. local mopt = {}
  71. local tAllValues = {}
  72. local i3
  73.  
  74. local menus = fs.list(path)
  75. if #menus ~= 0 then
  76.     for i = 1,#menus do
  77.         mopt[menus[i]] = {}
  78.         tAllValues = returnFileAsTable(path.."/"..menus[i])
  79.         mopt[menus[i]].options = {}
  80.         i3 = 1
  81.         for i2 = 1,#tAllValues,5 do
  82.             table.insert(mopt[menus[i]].options, {})
  83.             mopt[menus[i]].options[i3].display = tAllValues[i2]
  84.             mopt[menus[i]].options[i3].link = tAllValues[i2+1]
  85.             mopt[menus[i]].options[i3].ftype = tAllValues[i2+2]
  86.             mopt[menus[i]].options[i3].colour = colours[tAllValues[i2+3]]
  87.             i3 = i3+1
  88.         end
  89.     end
  90. else
  91.     term.clear()
  92.     drawHeader()
  93.     printCentred("No Menu's Configued", 8)
  94.     printCentred("Closing Interface", 9)
  95.     printCentred("Press Any Key to Continue", 10)
  96.     os.pullEvent("key")
  97.     sleep(0.1)
  98.     term.clear()
  99.     term.setCursorPos(1,1)
  100.     error()
  101. end
  102. if not mopt[menustate] then
  103. menustate = menus[1]
  104. end
  105.  
  106. local hist = {}
  107.  
  108. -- Scrolling List --
  109.  
  110. fitem = 1
  111. rsel = 1
  112.  
  113. function drawList(items)
  114.     a = h-5  a = a/2
  115.     if #items > a then l = a
  116.     else l = #items end
  117.     j = 4
  118.     for i = fitem, l+fitem-1 do
  119.         term.setTextColour(items[i].colour)
  120.         printCentred(items[i].display, j)
  121.         j = j +2
  122.     end
  123.  
  124.   term.setTextColour(items[select].colour)
  125.   local selpos = rsel*2 + 2
  126.   printCentred("[  "..items[select].display.."  ]", selpos)
  127.    
  128. end
  129. function checkSel(items)
  130.     a = h -5  a= a/2
  131.     if rsel > a -1 and select<#items then
  132.         rsel = rsel -1
  133.         fitem = fitem+1
  134.     elseif rsel == 1 and select > 1 then
  135.         rsel = rsel +1
  136.         fitem = fitem -1
  137.     end
  138.  
  139. end
  140.  
  141. -- User Area for added functions (Suggested) --
  142.  
  143.  
  144. -- Special Programs --
  145.  
  146. function logon()
  147.         local users = {
  148.             ["guest"]={default = "main", pass=""},
  149.             ["member"]={default = "member", pass="password"},
  150.             ["admin"]={default = "admin", pass="admin"},
  151.             ["op"]={default = "main", pass=""},
  152.             ["kylergs"] = {default = "main", pass = "kyle"}
  153.         }
  154.         os.loadAPI("GUI/logonapi")
  155.         user = logonapi.logon(users,user)
  156.         os.unloadAPI("GUI/logonapi")
  157. end
  158.  
  159. tsPrograms = {
  160.     ["logon"] = logon
  161. }
  162. -- Type Specific Functions --
  163.  
  164. function changeMenu()
  165. menustate = mopt[menustate].options[select].link
  166. select = 1; rsel = 1; fitem = 1
  167. table.insert(hist, menustate)
  168. end
  169. function plainText()
  170. end
  171. function special()
  172.     local link = mopt[menustate].options[select].link
  173.     if link == "quit" then term.clear(); term.setCursorPos(1,1); error();
  174.     elseif link == "back" and #hist > 1 then table.remove(hist); menustate = hist[#hist];
  175.         select = 1; rsel = 1; fitem = 1;
  176.     end
  177. end
  178. function runExternal()
  179. shell.run(mopt[menustate].options[select].link)
  180. end
  181. function specialFunction()
  182.     x = mopt[menustate].options[select].link
  183.     tsPrograms[x]()
  184. end
  185.  
  186. local typelist = {
  187. ["menu"] = {func = changeMenu};
  188. ["text"] = {func = plainText};
  189. ["special"] = {func = special};
  190. ["program"] = {func = runExternal};
  191. ["sfunc"] = {func = specialFunction};
  192. }
  193.  
  194. -- Run --
  195.  
  196. function runMain()
  197.     while true do
  198.         term.clear()
  199.         drawHeader()
  200.         mms = mopt[menustate]
  201.         drawList(mms.options)
  202.         do id, key, param2, param3 = os.pullEvent(); repeat until id == "key" or id =="mouse_click" or id == "mouse_scroll" end
  203.         if id == "key" then
  204.             if key == 200 and select > 1 then select = select-1 ; rsel=rsel-1 ; checkSel(mms.options)
  205.             elseif key == 208 and select < #mms.options then select = select + 1 ; rsel = rsel +1 ; checkSel(mms.options)  
  206.             elseif key == 28 then  
  207.                 typelist[mms.options[select].ftype].func()
  208.             end
  209.         elseif id =="mouse_click" then
  210.             mSel = param3/2 -1
  211.             if isInteger(mSel) and key == 1 and mSel <= l then  select = mSel+fitem -1; rsel=mSel;
  212.                 typelist[mms.options[select].ftype].func()
  213.             end
  214.         elseif id =="mouse_scroll" then
  215.             if key == -1 and fitem > 1 then fitem = fitem-1 ; select=select-1 ; checkSel(mms.options)
  216.             elseif key == -1 and fitem == 1 and select > 1 then select = select - 1 ; rsel = rsel -1 ;
  217.             elseif key == 1 and fitem +l -1< #mms.options then select = select + 1 ; fitem = fitem+1 ; checkSel(mms.options) ;
  218.             elseif key == 1 and fitem +l-1 >= #mms.options and select < #mms.options then select = select + 1 ; rsel = rsel +1
  219.             end
  220.         end
  221.     end
  222. end
  223.  
  224. runMain()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement