SHARE
TWEET

Untitled

zitot Aug 8th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     self.highlighttimer = "stopped"
  82.     self.items = items1
  83.     self.highlighted = false
  84. end
  85. function list1:update_delta()
  86.     self.deltatimer = self.deltatimer + 1
  87.     if self.deltas[11] then
  88.         table.remove(self.deltas, 1)
  89.     end
  90.     if self.deltas[1] and self.deltatimer % 2 == 0 then
  91.         table.remove(self.deltas, 1)
  92.     end
  93. end
  94. function list1:update_highlighttimer()
  95.     if self.highlighttimer == "stopped" then return end
  96.     self.highlighttimer = self.highlighttimer + 1
  97.     if self.highlighttimer > 5 then
  98.         self.highlighted = true
  99.        
  100.         self.highlighttimer = "stopped"
  101.     end
  102. end
  103. function list1:scroll()
  104.     self.y = self.y + self.vel.y
  105.     self.vel.y = self.vel.y * self.friction
  106.     self.vel.y=self.vel.y<.5 and self.vel.y>-.5 and 0 or self.vel.y
  107. end
  108. function list1:update()
  109.     self:update_delta()
  110.     self:update_highlighttimer()
  111.     self:scroll()
  112.     self:checkBounds()
  113. end
  114. function list1:checkBounds()
  115.     -- if y isn't within bounds, tween to the closest boundary
  116.     if not self.maxY or not self.minY then return end
  117.     if self.y > self.maxY + 32 then
  118.         if self.tween then tween.stop(self.tween) end
  119.         self.vel.y = 0
  120.         self.tween = tween(.25,self,{y=self.maxY},tween.easing.quadOut)
  121.     elseif self.y < self.minY - 32 then
  122.         if self.tween then tween.stop(self.tween) end
  123.         self.vel.y = 0
  124.         self.tween = tween(.25,self,{y=self.minY},tween.easing.quadOut)
  125.     end
  126. end
  127. function list1:highlight()
  128.     if self.selected and not self.selected.unselectable and self.highlighted then
  129.         fill(80, 80, 111, 155)
  130.         rect(0,     self.y - HEIGHT/9 - (self.selected.index-1) * HEIGHT/9,
  131.         WIDTH/7*2,  HEIGHT/9)
  132.     end
  133. end
  134. function list1:draw(t)
  135.     self:update()
  136.     fill(0, 155)
  137.     noStroke()
  138.     rect(0, 0, WIDTH/7*2, HEIGHT-46)
  139.     self:highlight()
  140.     pushStyle()
  141.     textMode(CORNER)
  142.     textWrapWidth(WIDTH/7*2-80)
  143.     lineCapMode(ROUND)
  144.     smooth()
  145.     for i,v in ipairs(self.items) do
  146.        
  147.         local y = self.y - (i-.5) * HEIGHT/9 -- center point
  148.         font("HelveticaNeue")
  149.         fontSize(16)    
  150.         local tw, th = textSize(v.description)
  151.         font("HelveticaNeue-CondensedBold")
  152.         fontSize(20)
  153.         local tw2, th2 = textSize(v.title)
  154.         local textheight = th + th2 -- total height of title and description
  155.        
  156.         local topY = y + textheight/2
  157.         local titleY = topY - th2
  158.         local bottomY = y - textheight/2
  159.  
  160.         fill(255)
  161.         text(v.title, 80, titleY)
  162.         sprite(v.image, 40, y, 64, 64)
  163.        
  164.         fill(150)
  165.         font("HelveticaNeue")
  166.         fontSize(16)    
  167.         text(v.description, 80, bottomY)
  168.         stroke(255,100)
  169.         strokeWidth(1)
  170.         line(80, self.y - i * HEIGHT/9, WIDTH / 7 * 2, self.y - i * HEIGHT/9)
  171.        
  172.         if debugging[1] then
  173.         noFill()
  174.         stroke(0,0,200)
  175.         strokeWidth(2)
  176.         rectMode(CORNERS)
  177.         rect(80, bottomY, WIDTH/7*2, topY)
  178.         end
  179.         if debugging[2] then
  180.         for k, touch in pairs(self.touches) do
  181.             fill(touch.x, touch.y, (touch.x*touch.y)^.5)
  182.             noStroke()
  183.             ellipse(touch.x, touch.y, 64)
  184.         end
  185.         end
  186.     end
  187.     popStyle()
  188. end
  189.    
  190. function list1:touched(touch)
  191.  
  192.     if touch.state == BEGAN and touch.x < WIDTH/7*2 then
  193.         self:selectFromTouchBegan(touch)
  194.         self.deltas = {}
  195.         list1.isTouched = true
  196.         if self.tween then tween.stop(self.tween) end
  197.         self.touches[touch.id] = touch
  198.     end
  199.     if not list1.isTouched then return end
  200.     if touch.state == MOVING then
  201.         if not self.touches[touch.id] then return end
  202.  
  203.         self.y = self.y + touch.deltaY
  204.         table.insert(self.deltas, touch.deltaY)
  205.         self.highlighttimer = "stopped"
  206.        
  207.     elseif touch.state == ENDED then
  208.        
  209.         if not self.touches[touch.id] then return end
  210.         self.touches[touch.id] = nil
  211.  
  212.         local h, lh, h9 = HEIGHT-46, HEIGHT/9 * #self.items, HEIGHT/9
  213.  
  214.         self.maxY = lh < h and h or lh > h and lh
  215.         self.minY = h
  216.         local newY = self.y + average(self.deltas)
  217.         local minty = self.minY - h9 -- min target y, minty
  218.         local maxty = self.maxY + h9 -- max target y, maxty
  219.         local duration = newY < self.minY and .5 or newY > self.maxY and .5 or 4.0
  220.  
  221.         newY = newY < minty and self.minY or newY > maxty and self.maxY or nil
  222.         -- print("is this touch nil?", touch)
  223.         self:highlightOnTouchEnded(touch)
  224.         --self.highlighttimer = "stopped"
  225.        
  226.         if self.y < self.minY then
  227.             if self.tween then tween.stop(self.tween) end
  228.             self.tween = tween(0.5, self, {y=self.minY}, tween.easing.quadOut)
  229.         elseif self.y > self.maxY then
  230.             if self.tween then tween.stop(self.tween) end
  231.             self.tween = tween(0.5, self, {y=self.maxY}, tween.easing.quadOut)
  232.         elseif newY then
  233.             if self.tween then tween.stop(self.tween) end
  234.             self:edgeTween(newY)
  235.         else
  236.             if self.tween then tween.stop(self.tween) end
  237.             self:setVel()
  238.             list1.isTouched = false
  239.         end
  240.     end
  241. end
  242.  
  243. function list1:edgeTween(newY)
  244.     self.tween = tween(.5, self, {y=newY}, tween.easing.quadOut)
  245. end
  246. function list1:setVel()
  247.     local v = average(self.deltas)
  248.     self.deltas = {}
  249.     self.vel.y = v/10
  250. end
  251. function average(t)
  252.     local n = 0
  253.     for i,v in ipairs(t) do  n = n + v end
  254.     return n
  255. end
  256. function list1:selectFromTouchBegan(touch)
  257.     if not touch.state == BEGAN then return end
  258.     local top=self.y
  259.     local bottom=self.y-(#self.items*HEIGHT/9)
  260.     self.height=top-bottom
  261.     local listY=touch.y-(top-self.height)-10
  262.     local index=math.ceil(listY/(HEIGHT/9))
  263.     index=#self.items-index+1 -- reverses the order
  264.     if not self.items[index] or self.items[index].unselectable then return end
  265.     if self.items[index] == self.selected then return end -- this line was really to keeps whats highlighted, highlighted, and saves some assignments
  266.    
  267.     self.selected=self.items[index]
  268.     self.selected.state = touch.state
  269.     self.highlighttimer = 1 -- see update_highlighttimer
  270.     self.highlighted = false
  271.     print("selected index: "..index, self.selected.title, "at pos:y - "..listY)
  272. end
  273. function list1:highlightOnTouchEnded(touch)
  274.     if not touch.state == ENDED then return end
  275.     if self.selected.unselectable then return end
  276.     --highlightOnTouchEnded is different from selectfromtouchbegan in that it requires if something is selected then that it not be unselectable
  277.    
  278.    
  279.     -- checks if the index where the touch ends is the same as the index of what is already selected
  280.     -- if that is the case, then set self.highlighted to true
  281.  
  282.     local top=self.y
  283.     local bottom=self.y-(#self.items*HEIGHT/9)
  284.     self.height=top-bottom
  285.     local listY=touch.y-(top-self.height)-10
  286.     local index=math.ceil(listY/(HEIGHT/9))
  287.     index=#self.items-index+1 -- reverses the order
  288.     if not self.items[index] --item does not exist
  289.     or self.items[index].unselectable then --item is unselectable
  290.         -- print"cannot highlight item"
  291.         return --the item at this index doesn't exist or its unselectable
  292.     elseif self.items[index] == self.selected then
  293.         -- print("highlight:",self.selected.index)
  294.         --the item we ended the touch on matches what is already selected, lets highlight it!
  295.         self.highlighttimer = "stopped"
  296.         self.highlighted = true
  297.     end
  298. end
  299. list2 = {}
  300. function list2:draw()
  301.     fill(0, 155)
  302.     noStroke()
  303.     rect(WIDTH/7*2, 0, WIDTH/7*2, HEIGHT-46)
  304.     for i,v in ipairs(list1.selected) do        end
  305. end
  306. list3 = {}
  307. function list3:draw()
  308.     fill(0,155)
  309.     noStroke()
  310.     rect(WIDTH/7*4, 0, WIDTH/7*3, HEIGHT-46)
  311. end
  312. -- This function gets called once every frame
  313. function draw()
  314.     -- This sets a dark background color
  315.     background(40, 40, 50)
  316.    
  317.     -- This sets the line thickness
  318.     strokeWidth(5)
  319.    
  320.     -- Do your drawing here
  321.     bar.draw()
  322.     list1:draw()
  323.     list2:draw()
  324.     list3:draw()
  325. end
  326.    
  327. function touched(touch)
  328.     list1:touched(touch)
  329. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top