Advertisement
zitot

List1:selectFromTouch

Aug 7th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.71 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.selected = {}
  75.     self.touches = {}
  76.     self.deltas = {}
  77.     self.vel = vec2(0,0)
  78.     self.friction = .976
  79.     self.dt = 1
  80. end
  81. function list1:scroll()
  82.     self.dt = self.dt + 1
  83.     if self.deltas[11] then
  84.         table.remove(self.deltas, 1)
  85.     end
  86.     if self.deltas[1] and self.dt % 2 == 0 then
  87.         table.remove(self.deltas, 1)
  88.     end
  89.     self.y = self.y + self.vel.y
  90.     self.vel.y = self.vel.y * self.friction
  91.     if not self.maxY or not self.minY then return end
  92.     if self.y > self.maxY + 32 then
  93.         if self.tween then tween.stop(self.tween) end
  94.         self.vel.y = 0
  95.         self.tween = tween(.25,self,{y=self.maxY},tween.easing.quadOut)
  96.     elseif self.y < self.minY - 32 then
  97.         if self.tween then tween.stop(self.tween) end
  98.         self.vel.y = 0
  99.         self.tween = tween(.25,self,{y=self.minY},tween.easing.quadOut)
  100.     end
  101. end
  102. function list1:draw(t)
  103.     self:scroll()
  104.     fill(0, 155)
  105.     noStroke()
  106.     rect(0, 0, WIDTH/7*2, HEIGHT-46)
  107.    
  108.     pushStyle()
  109.     textMode(CORNER)
  110.     textWrapWidth(WIDTH/7*2-80)
  111.     lineCapMode(ROUND)
  112.     smooth()
  113.     for i,v in ipairs(items1) do
  114.        
  115.         local y = self.y - (i-.5) * HEIGHT/9 -- center point
  116.         font("HelveticaNeue")
  117.         fontSize(16)    
  118.         local tw, th = textSize(v.description)
  119.         font("HelveticaNeue-CondensedBold")
  120.         fontSize(20)
  121.         local tw2, th2 = textSize(v.title)
  122.         local textheight = th + th2 -- total height of title and description
  123.        
  124.         local topY = y + textheight/2
  125.         local titleY = topY - th2
  126.         local bottomY = y - textheight/2
  127.  
  128.         fill(255)
  129.         text(v.title, 80, titleY)
  130.         sprite(v.image, 40, y, 64, 64)
  131.        
  132.         fill(150)
  133.         font("HelveticaNeue")
  134.         fontSize(16)    
  135.         text(v.description, 80, bottomY)
  136.         stroke(255,100)
  137.         strokeWidth(1)
  138.         line(80, self.y - i * HEIGHT/9, WIDTH / 7 * 2, self.y - i * HEIGHT/9)
  139.        
  140.         if debugging[1] then
  141.         noFill()
  142.         stroke(0,0,200)
  143.         strokeWidth(2)
  144.         rectMode(CORNERS)
  145.         rect(80, bottomY, WIDTH/7*2, topY)
  146.         end
  147.         if debugging[2] then
  148.         for k, touch in pairs(self.touches) do
  149.             fill(touch.x, touch.y, (touch.x*touch.y)^.5)
  150.             noStroke()
  151.             ellipse(touch.x, touch.y, 64)
  152.         end
  153.         end
  154.     end
  155.     popStyle()
  156. end
  157.    
  158. function list1:touched(touch)
  159.  
  160.     if touch.state == BEGAN and touch.x < WIDTH/7*2 then
  161.         self:selectFromTouch(touch)
  162.  
  163.         self.deltas = {}
  164.         list1.isTouched = true
  165.         if self.tween then tween.stop(self.tween) end
  166.         self.touches[touch.id] = touch
  167.     end
  168.     if not list1.isTouched then return end
  169.     if touch.state == MOVING then
  170.         if not self.touches[touch.id] then return end
  171.  
  172.         self.y = self.y + touch.deltaY
  173.         table.insert(self.deltas, touch.deltaY)
  174.     elseif touch.state == ENDED then
  175.         if not self.touches[touch.id] then return end
  176.         self.touches[touch.id] = nil
  177.  
  178.         local h, lh, h9 = HEIGHT-46, HEIGHT/9 * #items1, HEIGHT/9
  179.  
  180.         self.maxY = lh < h and h or lh > h and lh
  181.         self.minY = h
  182.         local newY = self.y + average(self.deltas)
  183.         local minty = self.minY - h9 -- min target y, minty
  184.         local maxty = self.maxY + h9 -- max target y, maxty
  185.         local duration = newY < self.minY and .5 or newY > self.maxY and .5 or 4.0
  186.  
  187.         newY = newY < minty and self.minY or newY > maxty and self.maxY or nil
  188.  
  189.         if self.y < self.minY then
  190.             if self.tween then tween.stop(self.tween) end
  191.             self.tween = tween(0.5, self, {y=self.minY}, tween.easing.quadOut)
  192.         elseif self.y > self.maxY then
  193.             if self.tween then tween.stop(self.tween) end
  194.             self.tween = tween(0.5, self, {y=self.maxY}, tween.easing.quadOut)
  195.         elseif newY then
  196.             self:edgeTween(newY,self.minY,self.maxY)
  197.         else
  198.             self:setVel()
  199.             list1.isTouched = false
  200.         end
  201.     end
  202. end
  203.  
  204. function list1:edgeTween(newY,minY,maxY)
  205.     self.tween = tween(.5, self, {y=newY}, tween.easing.quadOut)
  206. end
  207. function list1:setVel()
  208.     local v = average(self.deltas)
  209.     self.deltas = {}
  210.     self.vel.y = v/10
  211. end
  212. function average(t)
  213.     local n = 0
  214.     for i,v in ipairs(t) do  n = n + v end
  215.     return n
  216. end
  217. function list1:selectFromTouch(touch)
  218.     local top=self.y
  219.     local bottom=self.y-(#items1*HEIGHT/9)
  220.     self.height=top-bottom
  221.     local listY=touch.y-(top-self.height)
  222.     if listY>top or listY<bottom then return end
  223.     local index=math.ceil(listY/(HEIGHT/9))
  224.     index=#items1-index+1
  225.     self.selected=items1[index]
  226.     self.selected.state = touch.state
  227.     print("selected index: "..index, self.selected.title, "at pos:y - "..listY)
  228. end
  229.    
  230. list2 = {}
  231. function list2:draw()
  232.     fill(0, 155)
  233.     noStroke()
  234.     rect(WIDTH/7*2, 0, WIDTH/7*2, HEIGHT-46)
  235.     for i,v in ipairs(list1.selected) do        end
  236. end
  237. list3 = {}
  238. function list3:draw()
  239.     fill(0,155)
  240.     noStroke()
  241.     rect(WIDTH/7*4, 0, WIDTH/7*3, HEIGHT-46)
  242. end
  243. -- This function gets called once every frame
  244. function draw()
  245.     -- This sets a dark background color
  246.     background(40, 40, 50)
  247.    
  248.     -- This sets the line thickness
  249.     strokeWidth(5)
  250.    
  251.     -- Do your drawing here
  252.     bar.draw()
  253.     list1:draw()
  254.     list2:draw()
  255.     list3:draw()
  256. end
  257.    
  258. function touched(touch)
  259.     list1:touched(touch)
  260. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement