Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. Button = class()
  2.  
  3. function Button: init(displayName)
  4.  
  5. -- displayName: Is the text displayed on your button. The button will scale up and down to fit the
  6. -- text.
  7. -- pos: Defines the x and y - coordinates of the button using a vector.
  8. -- size: Is a vector which contains the width and height of the button, which is set by the
  9. -- display name text, and is used to determine if a button has been hit.
  10. -- action: Is the function that you want called when the button is tapped.
  11. -- color: Is the color of the button fill.
  12.  
  13. self.displayName = displayName
  14. self.pos = vec2(0,0)
  15. self.size = vec2(80,80)
  16. self.action = nil
  17. self.visible = true
  18. self.state = "normal"
  19. self.colour = color(236, 176, 56, 255) -- Orange colour from original arcade machine
  20.  
  21. end
  22.  
  23. function Button:draw()
  24.  
  25. if self.visible then
  26. pushStyle()
  27. fill(self.colour)
  28. rect(self.pos.x, self.pos.y + 20, self.size.x, self.size.y)
  29.  
  30. stroke(0)
  31. strokeWidth(2)
  32. ellipseMode(CENTER)
  33.  
  34. ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.75)
  35. ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.65)
  36. ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.55)
  37. ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.45)
  38. ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.35)
  39. ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.25)
  40.  
  41. fill(0)
  42. ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.15)
  43.  
  44. textMode(CENTER)
  45. font("AmericanTypewriter")
  46. fontSize(22)
  47. fill(255)
  48. text(self.displayName, self.pos.x + self.size.x/2, self.pos.y)
  49.  
  50. popStyle()
  51.  
  52. end
  53.  
  54. end
  55.  
  56. function Button:touched(touch)
  57. if self.visible then
  58. if touch.x >= self.pos.x and touch.x <= self.pos.x + self.size.x
  59. and touch.y >= self.pos.y and touch.y <= self.pos.y + self.size.y then
  60. if touch.state == BEGAN then
  61. self.state = "pressing"
  62. end
  63. if touch.state == ENDED then
  64. if self.state == "pressing" then
  65. self.state = "normal"
  66. end
  67. if self.action then
  68. self.action()
  69. end
  70. end
  71. else
  72. self.state = "normal"
  73. end
  74. end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement