Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. --Button API by Tobolobo (M3inNam3)
  2.  
  3. --Side HAS to be changed manually!!!
  4. local monitor = peripheral.wrap("left")
  5. local button = {}
  6. local buttoncount = 0
  7.  
  8. --privateDrawButton: A function that should
  9. --only be used by the API itself. It draws
  10. --a button.
  11. function privateDrawButton(colour, text, xMin, xMax, yMin, yMax)
  12. --Calculate difference
  13. local ydif = yMax - yMin
  14. local xdif = xMax - xMin
  15. --Draw button
  16. monitor.setBackgroundColor(colour)
  17. for i = yMin, yMax do
  18. for j = xMin, xMax do
  19. monitor.setCursorPos(j, i)
  20. monitor.write(" ")
  21. end
  22. end
  23. --Write text
  24. monitor.setCursorPos(xMin + math.floor((xdif + 1 - string.len(text)) / 2), yMin + math.floor(ydif / 2))
  25. monitor.write(text)
  26. monitor.setBackgroundColor(colors.black)
  27. return true
  28. end
  29.  
  30. --register: Registers a single button
  31. --and draws it on the monitor.
  32. function register(text, xMin, xMax, yMin, yMax, notActiveColour, activeColour)
  33. --Fill button table with new row and insert data
  34. button[buttoncount] = {}
  35. --The text shown on the button
  36. button[buttoncount][0] = text
  37. --The lowest x value of the button
  38. button[buttoncount][1] = xMin
  39. --The highest x value of the button
  40. button[buttoncount][2] = xMax
  41. --The lowest y value of the button
  42. button[buttoncount][3] = yMin
  43. --The highest y value of the button
  44. button[buttoncount][4] = yMax
  45. --Is the button enabled?
  46. button[buttoncount][5] = true
  47. --Color when not active(when the button is created)
  48. if notActiveColour == -1 then
  49. button[buttoncount][6] = colors.red
  50. else
  51. button[buttoncount][6] = notActiveColour
  52. end
  53. --Color when active (when the button is pressed once)
  54. if activeColour == -1 then
  55. button[buttoncount][7] = colors.green
  56. else
  57. button[buttoncount][7] = activeColour
  58. end
  59. --Is the button active or not?
  60. button[buttoncount][8] = false
  61. --Draw the button
  62. privateDrawButton(button[buttoncount][6], text, xMin, xMax, yMin, yMax)
  63. buttoncount = buttoncount + 1
  64. return buttoncount - 1
  65. end
  66.  
  67. --redraw: Redraws a single button.
  68. function redraw(id)
  69. --Check if button exists and is enabled
  70. if (button[id] == nil) or (button[id][5] == false) then
  71. return false
  72. end
  73. --Get data and draw
  74. local drawColour = 0
  75. if button[id][8] == false then
  76. drawColour = button[id][6]
  77. else
  78. drawColour = button[id][7]
  79. end
  80. privateDrawButton(drawColour, button[id][0], button[id][1], button[id][2], button[id][3], button[id][4])
  81. return true
  82. end
  83.  
  84. --getCollisions: It tests each enabled button
  85. --if the x and y coordinates collide with it!
  86. --It returns the id of the button. So instead
  87. --of overriding buttons you should disable them!
  88. --Flaw here: It can only be colliding with one
  89. --button (the one which registered first). But
  90. --who would let 2 buttons overlap each other?
  91.  
  92. function getCollisions(x, y)
  93. --Return -1 if no buttons registered yet
  94. if buttoncount == 0 then
  95. return -1
  96. end
  97. for i = 0, buttoncount - 1 do
  98. if button[i][5] == true then
  99. --Check if it collides with a button
  100. if (button[i][1] <= x) and (button[i][2] >= x) and (button[i][3] <= y) and (button[i][4] >= y) then
  101. return i
  102. end
  103. end
  104. end
  105. return -1
  106. end
  107.  
  108. --flash: Flashes the button.
  109. function flash(id, seconds)
  110. if button[id] == nil then
  111. return false
  112. end
  113. switch(id)
  114. --I hate using os.sleep() in this....
  115. os.sleep(seconds)
  116. switch(id)
  117. return true
  118. end
  119.  
  120. function exists(id)
  121. if button[id] == nil then
  122. return false
  123. else
  124. return true
  125. end
  126. end
  127.  
  128. --Properties get/set
  129.  
  130.  
  131. --setText: Sets the text of the button.
  132. function setText(id, text)
  133. if button[id] == nil then
  134. return false
  135. end
  136. button[id][0] = text
  137. redraw(id)
  138. return true
  139. end
  140.  
  141. --getText: Gets the text of the button.
  142. function getText(id)
  143. if button[id] == nil then
  144. return false
  145. end
  146. return button[id][0]
  147. end
  148.  
  149. --setPosition: Sets the new position of the button.
  150. --The old button will be erased!
  151. function setPosition(id, xMin, xMax, yMin, yMax)
  152. if button[id] == nil then
  153. return false
  154. end
  155. disable(id)
  156. if not(xMin == -1) then
  157. button[id][1] = xMin
  158. end
  159. if not(xMin == -1) then
  160. button[id][2] = xMax
  161. end
  162. if not(yMin == -1) then
  163. button[id][3] = yMin
  164. end
  165. if not(yMin == -1) then
  166. button[id][4] = yMax
  167. end
  168. enable(id)
  169. return true
  170. end
  171.  
  172. --getPosition: Gets the position of the button.
  173. --Returns an array with left, right, top, bottom
  174. --coordinates starting at 0
  175. function getPosition(id)
  176. if button[id] == nil then
  177. return false
  178. end
  179. local coords = {}
  180. for i = 0, 3 do
  181. coords[i] = button[id][i + 1]
  182. end
  183. return coords
  184. end
  185.  
  186. --enable: Enables a single button.
  187. function enable(id)
  188. if button[id] == nil then
  189. return false
  190. end
  191. button[id][5] = true
  192. redraw(id)
  193. return true
  194. end
  195.  
  196. --disable: Disables a single button.
  197. function disable(id)
  198. if button[id] == nil then
  199. return false
  200. end
  201. local colourBuffer = button[id][6]
  202. local textBuffer = button[id][0]
  203. local activeOrNot = button[id][8]
  204. button[id][8] = false
  205. button[id][6] = colors.black
  206. button[id][0] = ""
  207. redraw(id)
  208. button[id][6] = colourBuffer
  209. button[id][0] = textBuffer
  210. button[id][8] = activeOrNot
  211. button[id][5] = false
  212. return true
  213. end
  214.  
  215. --getEnabled: Gets the enabled-state of
  216. --a button.
  217. function getEnabled(id)
  218. if button[id] == nil then
  219. return -1
  220. end
  221. return button[id][5]
  222. end
  223.  
  224. --setNotActiveColour: Changes the color
  225. --when the button is not active.
  226. function setNotActiveColour(id, colour)
  227. if button[id] == nil then
  228. return false
  229. end
  230. button[id][6] = colour
  231. if button[id][8] == false then
  232. redraw(id)
  233. end
  234. return true
  235. end
  236.  
  237. --getNotActiveColour: Returns the
  238. --color of the button when it's not active.
  239. function getNotActiveColour(id)
  240. if button[id] == nil then
  241. return -1
  242. end
  243. return button[id][6]
  244. end
  245.  
  246. --setActiveColour: Changes the color
  247. --when the button is active
  248. function setActiveColour(id, colour)
  249. if button[id] == nil then
  250. return false
  251. end
  252. button[id][7] = colour
  253. if button[id][8] == true then
  254. redraw(id)
  255. end
  256. return true
  257. end
  258.  
  259. --getActiveColour: Returns the
  260. --color of the button when it's active.
  261. function getActiveColour(id)
  262. if button[id] == nil then
  263. return -1
  264. end
  265. return button[id][7]
  266. end
  267.  
  268. --changeCurrentColour: Changes the color
  269. --of the button!
  270. function changeCurrentColour(id, colour)
  271. if button[id] == nil then
  272. return false
  273. end
  274. if button[id][8] == false then
  275. button[id][6] = colour
  276. else
  277. button[id][7] = colour
  278. end
  279. redraw(id)
  280. return true
  281. end
  282.  
  283. --getCurrentColour: Returns the current
  284. --color of the button.
  285. function getCurrentColour(id)
  286. if button[id] == nil then
  287. return -1
  288. end
  289. if button[id][8] == false then
  290. return button[id][6]
  291. else
  292. return button[id][7]
  293. end
  294. end
  295.  
  296. --switch: Switches the state of the button.
  297. function switch(id)
  298. if button[id] == nil then
  299. return false
  300. end
  301. --Switch colors of the button
  302. button[id][8] = not button[id][8]
  303. redraw(id)
  304. return true
  305. end
  306.  
  307. --setActive: Sets the button state to active.
  308. function setActive(id)
  309. if button[id] == nil then
  310. return false
  311. end
  312. button[id][8] = true
  313. redraw(id)
  314. return true
  315. end
  316.  
  317. --setNotActive: Sets the button state to not active.
  318. function setNotActive(id)
  319. if button[id] == nil then
  320. return false
  321. end
  322. button[id][8] = false
  323. redraw(id)
  324. return true
  325. end
  326.  
  327. --isActive: Returns if the button is active
  328. function isActive(id)
  329. if button[id] == nil then
  330. return -1
  331. end
  332. return button[id][8]
  333. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement