Advertisement
alestane

Untitled

Jan 16th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.62 KB | None | 0 0
  1. ui = ui or {}
  2.  
  3. local function sliderTouch(self, event)
  4.     if event.phase == 'began' then
  5.         display.getCurrentStage():setFocus(self)
  6.     elseif event.phase == 'ended' or event.phase == 'cancelled' then
  7.         display.getCurrentStage():setFocus()
  8.     end
  9.     local x, y = self:contentToLocal(event.x, event.y)
  10.     self.Thumb.x = math.min(math.max(x, self.Bar.width / -2), self.Bar.width / 2)
  11.     local oldValue = self.Value
  12.     if event.phase ~= "cancelled" then
  13.         self.Value = self.Thumb.x / self.Bar.width + 0.5
  14.     end
  15.     self:dispatchEvent{name='Slider', target=self; value=self.Value, previous=oldValue, user=true, final=event.phase == 'ended' or event.phase == 'cancelled'}
  16.     return true
  17. end
  18.  
  19. local self = function(parent, width, height)
  20.     local self = display.newGroup(); parent:insert(self)
  21.     local barHeight = height * 0.6
  22.     local bar = display.newRoundedRect(self, width / -2, barHeight / -2, width, barHeight, barHeight / 2) do
  23.         bar:setStrokeColor(0, 0, 0)
  24.         bar.strokeWidth = barHeight / 16
  25.         bar:setFillColor(.827 * 127, .694 * 127, .286 * 127)
  26.     end self.Bar = bar
  27.     local thumb = display.newCircle(self, 0, 0, height / 2) do
  28.         thumb:setStrokeColor(0, 0, 0)
  29.         thumb.strokeWidth = height / 10
  30.         thumb:setFillColor(.827 * 255, .694 * 255, .286 * 255)
  31.     end self.Thumb = thumb
  32.     self.touch = sliderTouch
  33.     self:addEventListener('touch', self)
  34.     self.Value = 0.5
  35.     function self:SetValue(value)
  36.         self.Value, value = value, self.Value
  37.         self.Thumb.x = (self.Value - 0.5) * self.Bar.width
  38.         self:dispatchEvent{name='Slider', target=self; value=self.Value, previous=value, user=false, final=true}
  39.     end
  40.     return self
  41. end
  42.  
  43. ui.slider = self
  44.  
  45. return self
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement