Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------
- -- Button class
- --------------------------------------
- function newButton( params )
- local src, width, height, align, unBounded
- local unit = 32
- if not params.src then
- -- No filename supplied
- return false
- else
- src = params.src
- end
- width = params.width
- height = params.height
- unBounded = params.unBounded
- local spriteSheet = sprite.newSpriteSheet(src, width, height)
- local spriteSet = sprite.newSpriteSet(spriteSheet, 1, spriteSheet.frameCount)
- local button = sprite.newSprite(spriteSet)
- if params.x then
- button.x = params.x
- end
- if params.y then
- button.y = params.y
- end
- if params.onPress and type(params.onPress) == 'function' then
- button._onPress = params.onPress
- end
- if params.onRelease and type(params.onRelease) == 'function' then
- button._onRelease = params.onRelease
- end
- if params.onMove and type(params.onMove) == 'function' then
- button._onMove = params.onMove
- end
- if params.onCancel and type(params.onCancel) == 'function' then
- button._onCancel = params.onCancel
- end
- if params.onInactive and type(params.onInactive) == 'function' then
- button._onInactive = params.onInactive
- end
- -- set button to active (meaning, can be pushed)
- button.isActive = true
- -- If it's a switch, state 0 is off, 1 - on.
- button.state = 0
- local default, pressed, inactive
- if params.switch then
- button.isSwitch = true
- default, pressed, inactive = 1, 3, 5
- else
- default, pressed, inactive = 1, 2, 3
- end
- function button:touch (event)
- local result = true
- if self.isActive then
- local onPress = self._onPress
- local onMove = self._onMove
- local onRelease = self._onRelease
- local onCancel = self._onCancel
- local phase = event.phase
- if phase == 'began' then
- self.currentFrame = pressed + self.state
- if onPress then
- result = onPress(event)
- end
- -- Subsequent touch events will target button even if they are outside the stageBounds of button
- display.getCurrentStage():setFocus(self, event.id)
- self.isFocus = true
- elseif self.isFocus then
- local bounds = self.contentBounds
- local x, y = event.x,event.y
- local isWithinBounds = bounds.xMin <= x and bounds.xMax >= x and bounds.yMin <= y and bounds.yMax >= y
- if phase == 'moved' then
- if isWithinBounds or unBounded then
- -- The rollover image should only be visible while the finger is within button's stageBounds
- self.currentFrame = pressed + self.state
- else
- self.currentFrame = default + self.state
- end
- if onMove then
- result = onMove(event)
- end
- elseif phase == 'ended' or phase == 'cancelled' then
- self.currentFrame = default + self.state
- if phase == 'ended' then
- -- Only consider this a "click" if the user lifts their finger inside button's stageBounds
- if isWithinBounds or unBounded then
- if onRelease then
- if self.isSwitch then
- self:toggle()
- end
- result = onRelease(event)
- end
- end
- elseif phase == 'cancelled' then
- if onCancel then
- result = onCancel(event)
- end
- end
- -- Allow touch events to be sent normally to the objects they "hit"
- display.getCurrentStage():setFocus(self, nil)
- self.isFocus = false
- end
- end
- else
- if self._onInactive and event.phase == 'began' then
- result = self._onInactive(event)
- end
- end
- return result
- end
- button:addEventListener('touch', button)
- function button:disable ()
- self.currentFrame = inactive
- self.isActive = false
- end
- function button:enable ()
- self.currentFrame = default + self.state
- self.isActive = true
- end
- function button:toggle ()
- if self.state == 1 then
- self.currentFrame = self.currentFrame - 1
- self.state = 0
- else
- self.currentFrame = self.currentFrame + 1
- self.state = 1
- end
- end
- return button
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement