Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Button = class()
- function Button: init(displayName)
- -- displayName: Is the text displayed on your button. The button will scale up and down to fit the
- -- text.
- -- pos: Defines the x and y - coordinates of the button using a vector.
- -- size: Is a vector which contains the width and height of the button, which is set by the
- -- display name text, and is used to determine if a button has been hit.
- -- action: Is the function that you want called when the button is tapped.
- -- color: Is the color of the button fill.
- self.displayName = displayName
- self.pos = vec2(0,0)
- self.size = vec2(80,80)
- self.action = nil
- self.visible = true
- self.state = "normal"
- self.colour = color(236, 176, 56, 255) -- Orange colour from original arcade machine
- end
- function Button:draw()
- if self.visible then
- pushStyle()
- fill(self.colour)
- rect(self.pos.x, self.pos.y + 20, self.size.x, self.size.y)
- stroke(0)
- strokeWidth(2)
- ellipseMode(CENTER)
- ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.75)
- ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.65)
- ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.55)
- ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.45)
- ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.35)
- ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.25)
- fill(0)
- ellipse(self.pos.x + self.size.x/2, self.pos.y + 20 + self.size.y/2, self.size.x * 0.15)
- textMode(CENTER)
- font("AmericanTypewriter")
- fontSize(22)
- fill(255)
- text(self.displayName, self.pos.x + self.size.x/2, self.pos.y)
- popStyle()
- end
- end
- function Button:touched(touch)
- if self.visible then
- if touch.x >= self.pos.x and touch.x <= self.pos.x + self.size.x
- and touch.y >= self.pos.y and touch.y <= self.pos.y + self.size.y then
- if touch.state == BEGAN then
- self.state = "pressing"
- end
- if touch.state == ENDED then
- if self.state == "pressing" then
- self.state = "normal"
- end
- if self.action then
- self.action()
- end
- end
- else
- self.state = "normal"
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement