Advertisement
HugoBDesigner

Slots example (updated)

May 10th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.77 KB | None | 0 0
  1. --Friendly reminder that this is only an example of code that should be read and analyzed so that you understand the core concepts behind it
  2. --Pasting it on top of your code will not fix whatever issue you're having :P
  3. function love.load()
  4.     --Decoration
  5.     width = 25
  6.     spacing = 5
  7.    
  8.     curSlot = 1 --Selected slot
  9.     numOfSlots = 8 --Total slots size
  10.    
  11.     slotsInUse = 8 --By default, each slot is in use
  12.    
  13.     love.window.setMode((width+spacing)*numOfSlots+spacing, spacing*2+width)
  14. end
  15.  
  16. function love.draw()
  17.     for i = 1, numOfSlots do
  18.         love.graphics.setColor(.5, .5, .5, 1) --Sets default slot color
  19.         if i > slotsInUse then --Sets inactive slot color
  20.             love.graphics.setColor(.25, .25, .25, 1)
  21.         end
  22.         if curSlot == i then --Sets highlighted slot color
  23.             love.graphics.setColor(.75, .75, .25, 1)
  24.         end
  25.         love.graphics.rectangle("fill", spacing + (i-1)*(width + spacing), spacing, width, width) --Draw slot
  26.        
  27.         --This is just the make-shift item representation via colored balls
  28.         love.graphics.setColor(.75, .1, 0, 1)
  29.         if i <= slotsInUse then
  30.             love.graphics.setColor(.1, .75, 0, 1)
  31.         end
  32.         love.graphics.circle("fill", spacing + (i-1)*(width + spacing)+width/2, spacing+width/2, width/4, 32)
  33.         love.graphics.setLineWidth(2)
  34.         if curSlot == i then
  35.             love.graphics.setColor(.3, 1, 0, 1)
  36.         end
  37.         love.graphics.circle("line", spacing + (i-1)*(width + spacing)+width/2, spacing+width/2, width/4, 32)
  38.        
  39.         if curSlot == i then --Highlight slot
  40.             love.graphics.setColor(.25, .25, 0, 1)
  41.             love.graphics.setLineWidth(5)
  42.             love.graphics.rectangle("line", spacing + (i-1)*(width + spacing), spacing, width, width)
  43.         end
  44.     end
  45. end
  46.  
  47. function love.wheelmoved(x, y)
  48.     if y ~= 0 then
  49.         y = y < 0 and -1 or 1
  50.        
  51.         if slotsInUse == 0 then
  52.             curSlot = false
  53.         elseif curSlot == false and slotsInUse >= 1 then
  54.             curSlot = 1 --If no slot was selected, make it start at 1
  55.         else
  56.             curSlot = curSlot + y --Moves the selected slot up or down depending on the mouse wheel movement
  57.             if curSlot > slotsInUse then --This makes the slot selection wrap around if you reach the end
  58.                 curSlot = 1
  59.             elseif curSlot < 1 then
  60.                 curSlot = slotsInUse
  61.             end
  62.         end
  63.     end
  64. end
  65.  
  66. function love.keypressed(key)
  67.     local number = false
  68.    
  69.     --This is a cheap way of checking if the key pressed is 1, 2, 3... or kp1, kp2, kp3...
  70.     if string.len(key) == 3 and string.sub(key, 1, 2) == "kp" and tonumber(string.sub(key, -1, -1)) then
  71.         number = tonumber(string.sub(key, -1, -1))
  72.     elseif tonumber(key) then
  73.         number = tonumber(key)
  74.     end
  75.    
  76.     if number and number >= 0 and number <= numOfSlots then
  77.         slotsInUse = number
  78.         if number == 0 then
  79.             curSlot = false
  80.         elseif curSlot == false and number > 0 then
  81.             curSlot = 1
  82.         elseif curSlot > slotsInUse then
  83.             curSlot = slotsInUse
  84.         end
  85.     end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement