Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --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
- --Pasting it on top of your code will not fix whatever issue you're having :P
- function love.load()
- --Decoration
- width = 25
- spacing = 5
- slots = {} --Where your items go
- curSlot = 1 --Selected slot
- numOfSlots = 7 --Total slots size
- for i = 1, numOfSlots do
- slots[i] = {inUse = true} --By default, each slot is in use
- end
- love.window.setMode((width+spacing)*numOfSlots+spacing, spacing*2+width)
- end
- function love.draw()
- for i, v in ipairs(slots) do
- love.graphics.setColor(.5, .5, .5, 1) --Sets default slot color
- if not v.inUse then --Sets inactive slot color
- love.graphics.setColor(.25, .25, .25, 1)
- end
- if curSlot == i then --Sets highlighted slot color
- love.graphics.setColor(.75, .75, .25, 1)
- end
- love.graphics.rectangle("fill", spacing + (i-1)*(width + spacing), spacing, width, width) --Draw slot
- --This is just the make-shift item representation via colored balls
- love.graphics.setColor(.75, .1, 0, 1)
- if v.inUse then
- love.graphics.setColor(.1, .75, 0, 1)
- end
- love.graphics.circle("fill", spacing + (i-1)*(width + spacing)+width/2, spacing+width/2, width/4, 32)
- love.graphics.setLineWidth(2)
- if curSlot == i then
- love.graphics.setColor(.3, 1, 0, 1)
- end
- love.graphics.circle("line", spacing + (i-1)*(width + spacing)+width/2, spacing+width/2, width/4, 32)
- if curSlot == i then --Highlight slot
- love.graphics.setColor(.25, .25, 0, 1)
- love.graphics.setLineWidth(5)
- love.graphics.rectangle("line", spacing + (i-1)*(width + spacing), spacing, width, width)
- end
- end
- end
- function love.wheelmoved(x, y)
- if y ~= 0 then
- y = y < 0 and -1 or 1
- local foundSlot = false
- for i = 1, numOfSlots do --Ensures you try all items
- if curSlot then
- curSlot = curSlot + y --Moves the selected slot up or down depending on the mouse wheel movement
- else
- curSlot = 1 --If no slot was selected, make it start at 1
- end
- if curSlot > numOfSlots then --This makes the slot selection wrap around if you reach the end
- curSlot = 1
- elseif curSlot < 1 then
- curSlot = numOfSlots
- end
- if slots[curSlot].inUse then --It'll go on until it finds a valid slot. Once it's found, leave it selected and exit the loop
- foundSlot = true
- break
- end
- end
- if not foundSlot then --In case no slot is in use, we don't highlight any of them
- curSlot = false
- end
- end
- end
- function love.keypressed(key)
- local number = false
- --This is a cheap way of checking if the key pressed is 1, 2, 3... or kp1, kp2, kp3...
- if string.len(key) == 3 and string.sub(key, 1, 2) == "kp" and tonumber(string.sub(key, -1, -1)) then
- number = tonumber(string.sub(key, -1, -1))
- elseif tonumber(key) then
- number = tonumber(key)
- end
- if number and number >= 1 and number <= numOfSlots then
- slots[number].inUse = not slots[number].inUse
- if slots[number].inUse == false and curSlot == number then --Guarantees that the selection will be over a usable slot
- love.wheelmoved(0, 1) --Kind of a cheat
- elseif slots[number].inUse == true and curSlot == false then
- curSlot = number
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement