Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. -- Initialize some variables for later
  2. local xMax, yMax = term.getSize()
  3. local row = 4, 3, 1
  4.  
  5. -- Returns a table of possible icon origin positions
  6. function originTable(maxCol, maxRow)
  7. local tOrigins = {}
  8. for i=1, maxRow do
  9. for j=1, maxCol do
  10. table.insert(tOrigins, {3+9*(j-1), 2+6*(i-1)})
  11. end
  12. end
  13. return tOrigins
  14. end
  15.  
  16. -- Array search function
  17. function contains(t, key)
  18. for k,v in pairs(t) do
  19. if v == key then
  20. return true
  21. end
  22. end
  23. return false
  24. end
  25.  
  26. -- Writes text to the specified position
  27. function output(x, y, sText, color)
  28. if color ~= nil then
  29. term.setTextColor(color)
  30. else
  31. term.setTextColor(colors.white)
  32. end
  33. term.setCursorPos(x, y)
  34. term.write(sText)
  35. end
  36.  
  37. -- Draws a rectangular border
  38. function drawBorder(x, y, xLen, yLen, color)
  39. output(x, y, "/", color)
  40. output(x+xLen-1, y, "\\", color)
  41. output(x, y+yLen-1, "\\", color)
  42. output(x+xLen-1, y+yLen-1, "/", color)
  43. for i=x+1, x+xLen-2 do
  44. output(i, y, "-", color)
  45. output(i, y+yLen-1, "-", color)
  46. end
  47. for i=y+1, y+yLen-2 do
  48. output(x, i, "|", color)
  49. output(x+xLen-1, i, "|", color)
  50. end
  51. end
  52.  
  53. -- Draws a centered menu and returns origin, length, and height
  54. function drawMenu(tMenu, closeButton)
  55. local length, height = 0, #tMenu+2
  56. for _, sLine in ipairs(tMenu) do
  57. if string.len(sLine) >= length then
  58. length = string.len(sLine)+2
  59. end
  60. end
  61. local sBlank = ""
  62. for i=1, length-2 do sBlank = sBlank.." " end
  63. local x, y = math.floor(xMax/2) - math.ceil(length/2), math.floor(yMax/2) - math.ceil(#tMenu/2)
  64. drawBorder(x, y, length, height, colors.lime)
  65. if closeButton then
  66. output(x+length-3, y, "X", colors.red)
  67. end
  68. for i, sLine in ipairs(tMenu) do
  69. output(x+1, y+i, sBlank)
  70. output(x+1, y+i, sLine)
  71. end
  72. return {x, y}, length, height
  73. end
  74.  
  75. -- Runs a selection menu and returns a number or nil
  76. function runSelectionMenu(...)
  77. local tArgs = {...}
  78. local tMenu = {}
  79. for _, sOption in ipairs(tArgs) do
  80. table.insert(tMenu, " ( ) "..sOption.." ")
  81. end
  82. table.insert(tMenu, " [Okay] [Cancel] ")
  83. local origin, length, height = drawMenu(tMenu, true)
  84. local selection = 1
  85. local x, y = origin[1]+3, origin[2]+1
  86. local bMenu = true
  87. while bMenu do
  88. output(origin[1]+3, origin[2]+selection, "#")
  89. local sEvent, ret1, ret2, ret3 = os.pullEventRaw()
  90. if sEvent == "mouse_click" and ret1 == 1 then
  91. local x, y = ret2, ret3
  92. if y == origin[2] and x == origin[1]+length-3 then
  93. bMenu = false
  94. selection = nil
  95. elseif x > origin[1] and x < origin[1]+length-1 and y > origin[2] and y < origin[2]+height-1 then
  96. if y < origin[2]+height-2 then
  97. selection = y - origin[2]
  98. elseif x > origin[1]+1 and x < origin[1]+8 then
  99. bMenu = false
  100. elseif x > origin[1]+8 and x < origin[1]+17 then
  101. bMenu = false
  102. selection = nil
  103. end
  104. drawMenu(tMenu, true)
  105. end
  106. end
  107. end
  108. return selection
  109. end
  110.  
  111. -- Runs a text menu and returns a string
  112. function runTextMenu(sPrompt, sStartText)
  113. local tMenu = {" "..sPrompt..":", " { } ", " [Done] "}
  114. local origin, length, height = drawMenu(tMenu)
  115. local sText = ""
  116. if sStartText then sText = sStartText end
  117. local bMenu = true
  118. while bMenu do
  119. output(origin[1]+3, origin[2]+2, string.sub(sText, 1, 14))
  120. output(origin[1]+3, origin[2]+2, " ")
  121. output(origin[1]+3, origin[2]+2, string.sub(sText, -14))
  122. local sEvent, ret1, ret2, ret3 = os.pullEventRaw()
  123. if sEvent == "mouse_click" and ret1 == 1 then
  124. local x, y = ret2, ret3
  125. if y == origin[2]+height-2 and x > origin[1]+1 and x < origin[1]+8 then
  126. bMenu = false
  127. end
  128. elseif sEvent == "char" then
  129. sText = sText .. ret1
  130. elseif sEvent == "key" then
  131. if ret1 == 14 then
  132. sText = string.sub(sText, 1, string.len(sText)-1)
  133. end
  134. end
  135. end
  136. return sText
  137. end
  138.  
  139. -- Runs a confirmation menu and returns a boolean
  140. function runConfirmMenu(...)
  141. local tArgs = {...}
  142. local tMenu = {}
  143. for _, sLine in ipairs(tArgs) do
  144. tMenu[#tMenu+1] = " "..sLine.." "
  145. end
  146. table.insert(tMenu, " [Yes] [No] ")
  147. local origin, length, height = drawMenu(tMenu)
  148. local bMenu, bConfirm = true
  149. while bMenu do
  150. local sEvent, ret1, ret2, ret3 = os.pullEventRaw()
  151. if sEvent == "mouse_click" and ret1 == 1 then
  152. local x, y = ret2, ret3
  153. if y == origin[2]+height-2 and x > origin[1]+1 and x < origin[1]+7 then
  154. bMenu, bConfirm = false, true
  155. elseif y == origin[2]+height-2 and x > origin[1]+7 and x < origin[1]+12 then
  156. bMenu, bConfirm = false, false
  157. end
  158. end
  159. end
  160. return bConfirm
  161. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement