Advertisement
lvs

Button Class

lvs
Jan 7th, 2012
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 KB | None | 0 0
  1. --------------------------------------
  2. -- Button class
  3. --------------------------------------
  4. function newButton( params )
  5.     local src, width, height, align, unBounded
  6.     local unit = 32
  7.  
  8.     if not params.src then
  9.         -- No filename supplied
  10.         return false
  11.     else
  12.         src = params.src
  13.     end
  14.  
  15.     width = params.width
  16.     height = params.height
  17.         unBounded = params.unBounded
  18.  
  19.     local spriteSheet = sprite.newSpriteSheet(src, width, height)
  20.     local spriteSet = sprite.newSpriteSet(spriteSheet, 1, spriteSheet.frameCount)
  21.     local button = sprite.newSprite(spriteSet)
  22.  
  23.     if params.x then
  24.         button.x = params.x
  25.     end
  26.     if params.y then
  27.         button.y = params.y
  28.     end
  29.  
  30.     if params.onPress and type(params.onPress) == 'function' then
  31.         button._onPress = params.onPress
  32.     end
  33.     if params.onRelease and type(params.onRelease) == 'function' then
  34.         button._onRelease = params.onRelease
  35.     end
  36.     if params.onMove and type(params.onMove) == 'function' then
  37.         button._onMove = params.onMove
  38.     end
  39.     if params.onCancel and type(params.onCancel) == 'function' then
  40.         button._onCancel = params.onCancel
  41.     end
  42.     if params.onInactive and type(params.onInactive) == 'function' then
  43.         button._onInactive = params.onInactive
  44.     end
  45.  
  46.     -- set button to active (meaning, can be pushed)
  47.     button.isActive = true
  48.  
  49.     -- If it's a switch, state 0 is off, 1 - on.
  50.     button.state = 0
  51.     local default, pressed, inactive
  52.     if params.switch then
  53.         button.isSwitch = true
  54.         default, pressed, inactive = 1, 3, 5
  55.     else
  56.         default, pressed, inactive = 1, 2, 3
  57.     end
  58.  
  59.     function button:touch (event)
  60.         local result = true
  61.         if self.isActive then
  62.             local onPress = self._onPress
  63.             local onMove = self._onMove
  64.             local onRelease = self._onRelease
  65.             local onCancel = self._onCancel
  66.    
  67.             local phase = event.phase
  68.             if phase == 'began' then
  69.                 self.currentFrame = pressed + self.state
  70.                 if onPress then
  71.                     result = onPress(event)
  72.                 end
  73.                 -- Subsequent touch events will target button even if they are outside the stageBounds of button
  74.                 display.getCurrentStage():setFocus(self, event.id)
  75.                 self.isFocus = true
  76.             elseif self.isFocus then
  77.                 local bounds = self.contentBounds
  78.                 local x, y = event.x,event.y
  79.                 local isWithinBounds = bounds.xMin <= x and bounds.xMax >= x and bounds.yMin <= y and bounds.yMax >= y
  80.    
  81.                 if phase == 'moved' then
  82.                     if isWithinBounds or unBounded then
  83.                         -- The rollover image should only be visible while the finger is within button's stageBounds
  84.                         self.currentFrame = pressed + self.state
  85.                     else
  86.                         self.currentFrame = default + self.state
  87.                     end
  88.                     if onMove then
  89.                         result = onMove(event)
  90.                     end
  91.                 elseif phase == 'ended' or phase == 'cancelled' then
  92.                     self.currentFrame = default + self.state
  93.                     if phase == 'ended' then
  94.                         -- Only consider this a "click" if the user lifts their finger inside button's stageBounds
  95.                         if isWithinBounds or unBounded then
  96.                             if onRelease then
  97.                                 if self.isSwitch then
  98.                                     self:toggle()
  99.                                 end
  100.                                 result = onRelease(event)
  101.                             end
  102.                         end
  103.                     elseif phase == 'cancelled' then
  104.                         if onCancel then
  105.                             result = onCancel(event)
  106.                         end
  107.                     end
  108.                     -- Allow touch events to be sent normally to the objects they "hit"
  109.                     display.getCurrentStage():setFocus(self, nil)
  110.                     self.isFocus = false
  111.                 end
  112.             end
  113.         else
  114.             if self._onInactive and event.phase == 'began' then
  115.                 result = self._onInactive(event)
  116.             end
  117.         end
  118.         return result
  119.     end
  120.  
  121.     button:addEventListener('touch', button)
  122.  
  123.     function button:disable ()
  124.         self.currentFrame = inactive
  125.         self.isActive = false
  126.     end
  127.  
  128.     function button:enable ()
  129.         self.currentFrame = default + self.state
  130.         self.isActive = true
  131.     end
  132.  
  133.     function button:toggle ()
  134.         if self.state == 1 then
  135.             self.currentFrame = self.currentFrame - 1
  136.             self.state = 0
  137.         else
  138.             self.currentFrame = self.currentFrame + 1
  139.             self.state = 1
  140.         end
  141.     end
  142.  
  143.     return button
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement