Advertisement
zitot

List Scroll ver2

Aug 7th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.11 KB | None | 0 0
  1.  
  2. function setup()
  3.     displayMode(FULLSCREEN)
  4.     debugging = {false, false}
  5.     list1:init()
  6. end
  7.  
  8. bar = {}
  9. function bar:draw()
  10.     fill(0)
  11.     noStroke()
  12.     rect(0, HEIGHT-45, WIDTH, 46)
  13.     font("Courier-Bold")
  14.     fontSize(20)
  15.     fill(255)
  16.     text('Löve2d Reference', WIDTH/7*3, HEIGHT-22)
  17. end
  18.  
  19. items1 = {
  20.     {title="love", image=readImage("Blocks:Blank White"),
  21.     description="Modules, Functions, Types and Callbacks"},
  22.    
  23.     {title="love.audio",image=readImage("Blocks:Brick Grey"),
  24.     description="Interface to output sound"},
  25.     {title="love.data",image=readImage("Blocks:Brick Red"),
  26.     description="Provides functionality for creating and transforming data"},
  27.     {title="love.event",image=readImage("Cargo Bot:Codea Icon"),
  28.     description="Manages events, like keypresses"},
  29.     {title="love.filesystem",image=readImage("Environments:Icon"),
  30.     description="Interface to the filesystem"},
  31.     {title="love.font",image=readImage("Environments:Night Back"),
  32.     description="Work with and create fonts"},
  33.     {title="love.graphics",image=readImage("Cargo Bot:Game Lower BG"),
  34.     description="Drawing Shapes, Sprites and Styles"},
  35.     {title="love.image",image=readImage("Cargo Bot:Made With Codea"),
  36.     description="Decode encoded image data"},
  37.     {title="love.joystick",image=readImage("Cargo Bot:Game Upper BG"),
  38.     description="Interface to Connected Joysticks"},
  39.     {title="love.keyboard",image=readImage("Cargo Bot:Level Select BG"),
  40.     description="Interface to Connected Keyboard"},
  41.     {title="love.math",image=readImage("Environments:Night Up"),
  42.     description="System Independent Mathematical-Functions"},
  43.     {title="love.mouse",image=readImage("Cargo Bot:Pack Hard"),
  44.     description="Detect and React to Mouse Events"},
  45.     {title="love.physics",image=readImage("Surfaces:Basic Bricks Color"),
  46.     description="Binding to Box2d Physics"},
  47.     {title="love.sound",image=readImage("Planet Cute:Water Block"),
  48.     description="Encode and decode sounds! To play sounds, see love.audio"},
  49.     {title="love.system",image=readImage("Blocks:Error"),
  50.     description="Information about the user's system"},
  51.     {title="love.thread",image=readImage("Cargo Bot:Play Solution Icon"),
  52.     description="Create and use Threads and Channels"},
  53.     {title="love.timer",image=readImage("Cargo Bot:Stop Button"),
  54.     description="High resolution timing functions"},
  55.     {title="love.touch",image=readImage("Cargo Bot:Icon"),
  56.     description="Detecting and Reacting to Touches on the Screen"},
  57.     {title="love.video",image=readImage("Cargo Bot:Play Button"),
  58.     description="Decoding, controlling and streaming video files"},
  59.     {title="love.window",image=readImage("Blocks:Missing"),
  60.     description="Modifying and retrieving information on the Window"},
  61.     {title="lua-enet",image=readImage("UI:Green Button 09"),
  62.     description="Network Communication over UDP"},
  63.     {title="luasocket",image=readImage("Environments:Sunny Front"),
  64.     description="Networking on TCP/UDP"},
  65.     {title="utf8",image=readImage("Environments:Sunny Up"),
  66.     description="Handling UTF-8 Strings"};
  67. }
  68. do for i, v in ipairs(items1) do v.index = i end end
  69.    
  70. list1={}
  71. function list1:init()
  72.     self.y = HEIGHT - 46
  73.     self.height = #items1*HEIGHT/9
  74.     self.frameHeight = HEIGHT - 46
  75.     self.selected = {}
  76.     self.touches = {}
  77.     self.deltas = {}
  78.     self.vel = vec2(0,0)
  79.     self.friction = .976
  80.     self.deltatimer = 1
  81. end
  82. function list1:update_delta()
  83.     self.deltatimer = self.deltatimer + 1
  84.     if self.deltas[11] then
  85.         table.remove(self.deltas, 1)
  86.     end
  87.     if self.deltas[1] and self.deltatimer % 2 == 0 then
  88.         table.remove(self.deltas, 1)
  89.     end
  90. end
  91. function list1:scroll()
  92.     self.y = self.y + self.vel.y
  93.     self.vel.y = self.vel.y * self.friction
  94. end
  95. function list1:update()
  96.     self:update_delta()
  97.     self:scroll()
  98.     self:checkBounds()  
  99. end
  100. function list1:checkBounds()
  101.     -- if y isn't within bounds, tween to the closest boundary
  102.     if not self.maxY or not self.minY then return end
  103.     if self.y > self.maxY + 32 then
  104.         if self.tween then tween.stop(self.tween) end
  105.         self.vel.y = 0
  106.         self.tween = tween(.25,self,{y=self.maxY},tween.easing.quadOut)
  107.     elseif self.y < self.minY - 32 then
  108.         if self.tween then tween.stop(self.tween) end
  109.         self.vel.y = 0
  110.         self.tween = tween(.25,self,{y=self.minY},tween.easing.quadOut)
  111.     end
  112. end
  113. function list1:draw(t)
  114.     self:update()
  115.     fill(0, 155)
  116.     noStroke()
  117.     rect(0, 0, WIDTH/7*2, HEIGHT-46)
  118.    
  119.     pushStyle()
  120.     textMode(CORNER)
  121.     textWrapWidth(WIDTH/7*2-80)
  122.     lineCapMode(ROUND)
  123.     smooth()
  124.     for i,v in ipairs(items1) do
  125.        
  126.         local y = self.y - (i-.5) * HEIGHT/9 -- center point
  127.         font("HelveticaNeue")
  128.         fontSize(16)    
  129.         local tw, th = textSize(v.description)
  130.         font("HelveticaNeue-CondensedBold")
  131.         fontSize(20)
  132.         local tw2, th2 = textSize(v.title)
  133.         local textheight = th + th2 -- total height of title and description
  134.        
  135.         local topY = y + textheight/2
  136.         local titleY = topY - th2
  137.         local bottomY = y - textheight/2
  138.  
  139.         fill(255)
  140.         text(v.title, 80, titleY)
  141.         sprite(v.image, 40, y, 64, 64)
  142.        
  143.         fill(150)
  144.         font("HelveticaNeue")
  145.         fontSize(16)    
  146.         text(v.description, 80, bottomY)
  147.         stroke(255,100)
  148.         strokeWidth(1)
  149.         line(80, self.y - i * HEIGHT/9, WIDTH / 7 * 2, self.y - i * HEIGHT/9)
  150.        
  151.         if debugging[1] then
  152.         noFill()
  153.         stroke(0,0,200)
  154.         strokeWidth(2)
  155.         rectMode(CORNERS)
  156.         rect(80, bottomY, WIDTH/7*2, topY)
  157.         end
  158.         if debugging[2] then
  159.         for k, touch in pairs(self.touches) do
  160.             fill(touch.x, touch.y, (touch.x*touch.y)^.5)
  161.             noStroke()
  162.             ellipse(touch.x, touch.y, 64)
  163.         end
  164.         end
  165.     end
  166.     popStyle()
  167. end
  168.    
  169. function list1:touched(touch)
  170.  
  171.     if touch.state == BEGAN and touch.x < WIDTH/7*2 then
  172.         self:selectFromTouch(touch)
  173.  
  174.         self.deltas = {}
  175.         list1.isTouched = true
  176.         if self.tween then tween.stop(self.tween) end
  177.         self.touches[touch.id] = touch
  178.     end
  179.     if not list1.isTouched then return end
  180.     if touch.state == MOVING then
  181.         if not self.touches[touch.id] then return end
  182.  
  183.         self.y = self.y + touch.deltaY
  184.         table.insert(self.deltas, touch.deltaY)
  185.     elseif touch.state == ENDED then
  186.         if not self.touches[touch.id] then return end
  187.         self.touches[touch.id] = nil
  188.  
  189.         local h, lh, h9 = HEIGHT-46, HEIGHT/9 * #items1, HEIGHT/9
  190.  
  191.         self.maxY = lh < h and h or lh > h and lh
  192.         self.minY = h
  193.         local newY = self.y + average(self.deltas)
  194.         local minty = self.minY - h9 -- min target y, minty
  195.         local maxty = self.maxY + h9 -- max target y, maxty
  196.         local duration = newY < self.minY and .5 or newY > self.maxY and .5 or 4.0
  197.  
  198.         newY = newY < minty and self.minY or newY > maxty and self.maxY or nil
  199.  
  200.         if self.y < self.minY then
  201.             if self.tween then tween.stop(self.tween) end
  202.             self.tween = tween(0.5, self, {y=self.minY}, tween.easing.quadOut)
  203.         elseif self.y > self.maxY then
  204.             if self.tween then tween.stop(self.tween) end
  205.             self.tween = tween(0.5, self, {y=self.maxY}, tween.easing.quadOut)
  206.         elseif newY then
  207.             if self.tween then tween.stop(self.tween) end
  208.             self:edgeTween(newY)
  209.         else
  210.             if self.tween then tween.stop(self.tween) end
  211.             self:setVel()
  212.             list1.isTouched = false
  213.         end
  214.     end
  215. end
  216.  
  217. function list1:edgeTween(newY)
  218.     self.tween = tween(.5, self, {y=newY}, tween.easing.quadOut)
  219. end
  220. function list1:setVel()
  221.     local v = average(self.deltas)
  222.     self.deltas = {}
  223.     self.vel.y = v/10
  224. end
  225. function average(t)
  226.     local n = 0
  227.     for i,v in ipairs(t) do  n = n + v end
  228.     return n
  229. end
  230. function list1:selectFromTouch(touch)
  231.     local top=self.y
  232.     local bottom=self.y-(#items1*HEIGHT/9)
  233.     self.height=top-bottom
  234.     local listY=touch.y-(top-self.height)-10
  235.     local index=math.ceil(listY/(HEIGHT/9))
  236.     index=#items1-index+1 -- reverses the order
  237.     if not items1[index] then return end
  238.     self.selected=items1[index]
  239.     self.selected.state = touch.state
  240.     print("selected index: "..index, self.selected.title, "at pos:y - "..listY)
  241. end
  242.    
  243. list2 = {}
  244. function list2:draw()
  245.     fill(0, 155)
  246.     noStroke()
  247.     rect(WIDTH/7*2, 0, WIDTH/7*2, HEIGHT-46)
  248.     for i,v in ipairs(list1.selected) do        end
  249. end
  250. list3 = {}
  251. function list3:draw()
  252.     fill(0,155)
  253.     noStroke()
  254.     rect(WIDTH/7*4, 0, WIDTH/7*3, HEIGHT-46)
  255. end
  256. -- This function gets called once every frame
  257. function draw()
  258.     -- This sets a dark background color
  259.     background(40, 40, 50)
  260.    
  261.     -- This sets the line thickness
  262.     strokeWidth(5)
  263.    
  264.     -- Do your drawing here
  265.     bar.draw()
  266.     list1:draw()
  267.     list2:draw()
  268.     list3:draw()
  269. end
  270.    
  271. function touched(touch)
  272.     list1:touched(touch)
  273. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement