MudkipTheEpic

Button API V1.1

Jan 5th, 2013
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. fs.makeDir("buttons")
  2. function buttons()
  3. local event, button, X, Y = os.pullEvent("mouse_click")
  4. if button == 1 then
  5. X = tostring(X)
  6. Y = tostring(Y)
  7. XY = X.."_"..Y
  8.  
  9. if fs.isDir("buttons") then
  10. sDir = shell.resolve("buttons")
  11. end
  12. local tAll = fs.list( sDir )
  13. local tFiles = {}
  14. local tDirs = {}
  15.  
  16. for n, sItem in pairs( tAll ) do
  17. local sPath = fs.combine( sDir, sItem )
  18. local check = string.find(sPath, XY)
  19. if check ~= nil then
  20. dash = string.find(sPath, "-")
  21. ampersand = string.find(sPath, "=")
  22. iconlabel = string.sub(sPath, ampersand+1, dash-1)
  23. term.clear()
  24. term.setCursorPos(1,1)
  25. term.setTextColor(2^14)
  26. term.setBackgroundColor(2^5)
  27. local file = fs.open(sPath, "r")
  28. local fileline = file.readLine()
  29. file.close()
  30. local pos = string.find(fileline, " ")
  31. local name = string.sub(fileline, pos+3, string.len(fileline) - 2)
  32. local w, l = term.getSize()
  33. local namelength = string.len(name)
  34. w = tonumber(w)
  35. namelength = tonumber(namelength)
  36. local empty = string.rep(" ", w - namelength - 5)
  37. local nAl = name.." ("..iconlabel..")"
  38. local nAlL = string.len(nAl)
  39. nAlL = w - nAlL
  40. local Npos = math.ceil(nAlL / 2)
  41. print(name..empty.."("..iconlabel..")")
  42. term.setTextColor(1)
  43. term.setBackgroundColor(2^15)
  44. term.setCursorPos(1,2)
  45. pcall(shell.run(sPath))
  46. term.setCursorBlink(false)
  47. sleep(2)
  48. term.clear()
  49. term.setCursorPos(19,9)
  50. term.setTextColor(2^math.random(1,14))
  51. textutils.slowPrint("Program:")
  52. term.setCursorPos(Npos, 10)
  53. term.setTextColor(2^math.random(1,14))
  54. textutils.slowPrint(nAl)
  55. term.setCursorPos(15, 11)
  56. term.setTextColor(2^math.random(1,14))
  57. textutils.slowPrint("has finished its run.")
  58. sleep(0.5)
  59. term.setCursorPos(18,12)
  60. term.setTextColor(2^math.random(1,14))
  61. print("Please wait....")
  62. sleep(1)
  63. term.setTextColor(1)
  64. term.clear()
  65. term.setCursorPos(1,1)
  66. end
  67. end
  68. elseif button == 2 then
  69. while true do
  70. term.clear()
  71. term.setCursorPos(1,1)
  72. print("What program would you like this button to run?")
  73. input = read()
  74. if fs.exists(input) == true and fs.isDir(input) == false then
  75. break
  76. end
  77. end
  78. while true do
  79. term.clear()
  80. term.setCursorPos(1,1)
  81. print("Please enter a 3 character abbreviation for your button. Please do not use =, -, or _ in your label.")
  82. local L = read()
  83. if string.len(L) == 3 then
  84. if string.find(L, "=") == nil then
  85. if string.find(L, "-") == nil then
  86. if string.find(L, "_") == nil then
  87. if string.find(L, "/") == nil then
  88. if string.find(L, "\\") == nil then
  89. label = L
  90. break
  91. end
  92. end
  93. end
  94. end
  95. end
  96. end
  97. end
  98. fs.makeDir("buttons")
  99. local XY = X.."_"..Y
  100. XY = tostring(XY)
  101. local nb = fs.open("buttons/button="..label.."-"..XY , "w")
  102. nb.writeLine("shell.run ('"..input.."')")
  103. nb.close()
  104. end
  105. listbuttons()
  106. end
  107.  
  108. function listbuttons()
  109. term.clear()
  110. term.setCursorPos(1,1)
  111. if fs.isDir("buttons") then
  112. sDir = shell.resolve("buttons")
  113.  
  114. local tAll = fs.list( sDir )
  115. local tFiles = {}
  116. local tDirs = {}
  117.  
  118. for n, sItem in pairs( tAll ) do
  119. local sPath = fs.combine( sDir, sItem )
  120. dash = string.find(sPath, "-")
  121. colon = string.find(sPath, "_")
  122. ampersand = string.find(sPath, "=")
  123. length = string.len(sPath)
  124. iconlabel = string.sub(sPath, ampersand+1, dash-1)
  125. Xpos = tonumber(string.sub(sPath, dash+1, colon-1)) - 2
  126. Ypos = tonumber(string.sub(sPath, colon+1, length))
  127. if Xpos ~= nil then
  128. term.setCursorPos(Xpos,Ypos)
  129. print("("..iconlabel..")")
  130. end
  131. end
  132. end
  133. buttons()
  134. end
  135.  
  136. listbuttons()
Advertisement
Add Comment
Please, Sign In to add comment