SteamyPotatoes

Untitled

Jun 1st, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1.  
  2. local coords1 = {}
  3. local coords2 = {}
  4. local text = {}
  5. local colors1 = {}
  6. local colors2 = {}
  7. local textInt, x, y, k, j, i, l
  8. local event, button, xCoord, yCoord, sSide
  9. local input
  10.  
  11. -- Draws a button on the screen
  12. function drawButton(coord1, coord2, coord3, coord4, text, color1, color2)
  13. x = coord3 - coord1 + 2
  14. y = coord4 - coord2 + 2
  15.  
  16. -- Draws the button itself
  17. term.setBackgroundColor(color1)
  18. k = 0
  19. while (k < y) do
  20. term.setCursorPos(coord1, coord2 + k)
  21. j = 0
  22. while (j < x) do
  23. write(" ")
  24. j = j + 1
  25. end
  26. k = k + 1
  27. end
  28.  
  29. -- Writes the text on the button
  30. if (string.len(text) % 2 == 0) then
  31. textInt = string.len(text) / 2
  32. else
  33. textInt = (string.len(text) - 1) / 2
  34. end
  35. if (x % 2 == 0) then
  36. if (y % 2 == 0) then
  37. term.setCursorPos(x / 2 + coord1 - textInt, y / 2 + coord2)
  38. else
  39. term.setCursorPos(x / 2 + coord1 - textInt, (y - 1) / 2 + coord2)
  40. end
  41. else
  42. if (y % 2 == 0) then
  43. term.setCursorPos((x - 1) / 2 + coord1 - textInt, y / 2 + coord2)
  44. else
  45. term.setCursorPos((x - 1) / 2 + coord1 - textInt, (y - 1) / 2 + coord2)
  46. end
  47. end
  48. term.setTextColor(color2)
  49. write(text)
  50. end
  51.  
  52. -- Shows the buttons on the computer
  53. -- Returns the number of the button
  54. function showComputer()
  55. -- Draws the buttons
  56. i = 1
  57. l = 1
  58. while (i < #coords1) do
  59. drawButton(coords1[i], coords1[i + 1], coords2[i], coords2[i + 1], text[l], colors1[l], colors2[l])
  60. i = i + 2
  61. l = l + 1
  62. end
  63.  
  64. -- Waits until there is clicked on one of the buttons
  65. local bool = true
  66. l = 0
  67. while (bool) and (#coords1 ~= 0) do
  68. event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  69. if (event == "mouse_click") then
  70. i = 1
  71. l = 1
  72. while (i <= #coords1) do
  73. if (xCoord >= coords1[i]) and (xCoord <= coords2[i]) and (yCoord >= coords1[i + 1]) and (yCoord <= coords2[i + 1]) then
  74. bool = false
  75. break
  76. end
  77. i = i + 2
  78. l = l + 1
  79. end
  80. end
  81. end
  82. return l
  83. end
  84.  
  85. -- Shows the buttons on a monitor
  86. -- Returns the number of the button
  87. function showMonitor(side)
  88. -- Draws the buttons
  89. i = 1
  90. l = 1
  91. while (i < #coords1) do
  92. term.redirect(peripheral.wrap(side))
  93. drawButton(coords1[i], coords1[i + 1], coords2[i], coords2[i + 1], text[l], colors1[l], colors2[l])
  94. term.restore()
  95.  
  96. i = i + 2
  97. l = l + 1
  98. end
  99.  
  100. -- Waits until there is clicked on one of the buttons
  101. local bool = true
  102. l = 0
  103. while (bool) and (#coords1 ~= 0) do
  104. event, sSide, xCoord, yCoord = os.pullEvent("monitor_touch")
  105. if (event == "monitor_touch") then
  106. if (sSide == side) then
  107. i = 1
  108. l = 1
  109. while (i <= #coords1) do
  110. if (xCoord >= coords1[i]) and (xCoord <= coords2[i]) and (yCoord >= coords1[i + 1]) and (yCoord <= coords2[i + 1]) then
  111. bool = false
  112. break
  113. end
  114. i = i + 2
  115. l = l + 1
  116. end
  117. end
  118. end
  119. end
  120. return l
  121. end
  122.  
  123. -- Shows the buttons on the computer
  124. -- Return on [enter]
  125. -- Returns the number of the button and the text input
  126. function showComputerRead(coord1, coord2, color1, color2)
  127. -- Draws the buttons
  128. i = 1
  129. l = 1
  130. while (i < #coords1) do
  131. drawButton(coords1[i], coords1[i + 1], coords2[i], coords2[i + 1], text[l], colors1[l], colors2[l])
  132. i = i + 2
  133. l = l + 1
  134. end
  135.  
  136. -- Waits until there is clicked on one of the buttons or someone pressed [enter]
  137. term.setCursorPos(coord1, coord2)
  138. term.setBackgroundColor(color1)
  139. term.setTextColor(color2)
  140. local bool = true
  141. input = ""
  142. l = 0
  143. while (bool) and (#coords1 ~= 0) do
  144. l = 0
  145. parallel.waitForAny(function() event, button, xCoord, yCoord = os.pullEvent("mouse_click") end, function() input = read() end)
  146. if (event == "mouse_click") then
  147. i = 1
  148. l = 1
  149. while (i <= #coords1) do
  150. if (xCoord >= coords1[i]) and (xCoord <= coords2[i]) and (yCoord >= coords1[i + 1]) and (yCoord <= coords2[i + 1]) then
  151. bool = false
  152. break
  153. end
  154. i = i + 2
  155. l = l + 1
  156. end
  157. elseif (input ~= "") then
  158. break
  159. end
  160. end
  161. return l, input
  162. end
  163.  
  164. -- Shows the buttons on a monitor
  165. -- Return on [enter]
  166. -- Returns the number of the button and the text input
  167. function showMonitorRead(coord1, coord2, side, color1, color2)
  168. -- Draws the buttons on the monitor
  169. i = 1
  170. l = 1
  171. while (i < #coords1) do
  172. term.redirect(peripheral.wrap(side))
  173. drawButton(coords1[i], coords1[i + 1], coords2[i], coords2[i + 1], text[l], colors1[l], colors2[l])
  174. term.restore()
  175.  
  176. i = i + 2
  177. l = l + 1
  178. end
  179.  
  180. -- Waits until there is clicked on one of the buttons or someone pressed [enter]
  181. term.setCursorPos(coord1, coord2)
  182. term.setBackgroundColor(color1)
  183. term.setTextColor(color2)
  184. local bool = true
  185. input = ""
  186. l = 0
  187. while (bool) and (#coords1 ~= 0) do
  188. l = 0
  189. parallel.waitForAny(function() event, sSide, xCoord, yCoord = os.pullEvent("monitor_touch") end, function() input = read() end)
  190. if (event == "monitor_touch") then
  191. if (sSide == side) then
  192. i = 1
  193. l = 1
  194. while (i <= #coords1) do
  195. if (xCoord >= coords1[i]) and (xCoord <= coords2[i]) and (yCoord >= coords1[i + 1]) and (yCoord <= coords2[i + 1]) then
  196. bool = false
  197. break
  198. end
  199. i = i + 2
  200. l = l + 1
  201. end
  202. end
  203. elseif (input ~= "") then
  204. break
  205. end
  206. end
  207. return l, input
  208. end
  209.  
  210. -- Adds a button to the table
  211. function addButton(coord1, coord2, coord3, coord4, sText, color1, color2)
  212. table.insert(coords1, coord1)
  213. table.insert(coords1, coord2)
  214. table.insert(coords2, coord3)
  215. table.insert(coords2, coord4)
  216. table.insert(text, sText)
  217. table.insert(colors1, color1)
  218. table.insert(colors2, color2)
  219. end
  220.  
  221. -- Clears the table
  222. function clearButton()
  223. coords1 = {}
  224. coords2 = {}
  225. text = {}
  226. colors1 = {}
  227. colors2 = {}
  228. end
Add Comment
Please, Sign In to add comment