Advertisement
pfgpastebin

RedButton_REUPLOAD

Sep 23rd, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. --REUPLOAD
  2. --Origional paste: http://pastebin.com/uks1ZeP2
  3. --By Redxone : http://www.computercraft.info/forums2/index.php?/topic/24576-simple-button-api/http://www.computercraft.info/forums2/index.php?/topic/24576-simple-button-api/
  4.  
  5.  
  6. -- Easy term access --
  7. easyterms = {
  8. sbc = term.setBackgroundColor,
  9. stc = term.setTextColor,
  10. scp = term.setCursorPos,
  11. }
  12.  
  13. -- Create button method --
  14.  
  15. function addButton(x,y,width,height,
  16. textcolor, backcolor, text)
  17. local button = {
  18.  
  19. x = x,
  20. y = y,
  21. w = width,
  22. h = height,
  23. text = text,
  24. tcol = textcolor,
  25. bcol = backcolor,
  26. sbc = easyterms.sbc,
  27. scp = easyterms.scp,
  28. stc = easyterms.stc,
  29. s_width, s_height = term.getSize(),
  30.  
  31. -- Draw Button
  32. draw = function(self)
  33. -- loop through height and draw by width
  34.  
  35. for h = 0, self.h do
  36. self.sbc(self.bcol)
  37. self.scp(self.x,self.y+h)
  38. write(string.rep(" ",self.w))
  39. end
  40. -- draw the text centered
  41. self.scp( ((self.w/2 + self.x))
  42. - #text/2,
  43. ((self.h/2) + (self.y))
  44.  
  45. )
  46. self.stc(self.tcol)
  47. write(self.text)
  48.  
  49. end,
  50.  
  51. rename = function(self,newname)
  52. self.text = newname
  53. self:draw()
  54. end,
  55.  
  56. clear = function(self,color)
  57. for nh = 0, self.h do
  58. self.sbc(color)
  59. self.scp(self.x,self.y+nh)
  60. write(string.rep(" ",self.w))
  61. end
  62. end,
  63.  
  64. setPos = function(self,newx, newy,clearcolor)
  65. self:clear(clearcolor)
  66. self.x = newx
  67. self.y = newy
  68. self:draw()
  69. end,
  70.  
  71. colorize = function(self,tcolor,bcolor)
  72. self.tcol = tcolor
  73. self.bcol = bcolor
  74. self:draw()
  75. end,
  76.  
  77. -- check if button was clicked --
  78.  
  79. pressed = function(self,event)
  80. if(event[1] == "mouse_click")then
  81.  
  82.  
  83. -- store event information
  84. local mx = event[3]
  85. local my = event[4]
  86. local mb = event[2]
  87.  
  88. -- check if the button as clicked
  89. if(mb == 1 and mx >= self.x
  90. and mx <= (self.w + self.x)
  91. and my >= self.y
  92. and my <= (self.y + self.h))then
  93. return true
  94. end
  95.  
  96. else
  97. -- if the event wasnt mouse related
  98. return false
  99. end
  100. end
  101.  
  102.  
  103. }
  104.  
  105. -- add ofcourse what would be the point
  106. -- if we didnt return the sucker?
  107.  
  108. return button
  109.  
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement