Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.load()
- local prelayout = {"abcdefg", "hijklmn", "opqrstu", "vwxyz"}
- layout = {}
- for y = 1, #prelayout do
- layout[y] = {}
- for x = 1, string.len(prelayout[y]) do
- layout[y][x] = {key = string.sub(prelayout[y], x, x), pressed = false}
- end
- end
- startX, startY = 10, 10
- keySize = 20
- spacing = 5
- windowW = #layout[1]*(keySize + spacing) - spacing + 2*startX
- windowH = #layout*(keySize + spacing) - spacing + 2*startY
- love.graphics.setBackgroundColor(1, 1, 1)
- font = love.graphics.newFont(12)
- love.graphics.setFont(font)
- love.window.setMode(windowW, windowH)
- end
- function love.draw()
- for y = 1, #layout do
- for x = 1, #layout[y] do
- local px, py = startX + (x-1)*(keySize + spacing), startY + (y-1)*(keySize + spacing)
- local v = layout[y][x].key
- love.graphics.setColor(39/255, 151/255, 229/255, 155/255)
- if layout[y][x].pressed then
- love.graphics.setColor(229/255, 218/255, 39/255, 155/255)
- end
- love.graphics.rectangle("fill", px, py, keySize, keySize)
- love.graphics.setColor(0, 0, 0, 1)
- love.graphics.rectangle("line", px, py, keySize, keySize)
- love.graphics.print(v, px + keySize/2 - font:getWidth(v)/2, py + keySize/2 - font:getHeight()/2)
- end
- end
- end
- function love.mousepressed(x, y, button)
- local px, py = x - startX, y - startY
- --Clicked inside keyboard
- if px >= 0 and py >= 0 and px <= (keySize+spacing)*#layout[1] and py <= (keySize+spacing)*#layout then
- local tx, ty = math.floor(px/(keySize+spacing)) + 1, math.floor(py/(keySize+spacing)) + 1
- px = px - (tx-1)*(keySize+spacing)
- py = py - (ty-1)*(keySize+spacing)
- --If in-between keys, ignore
- if px <= keySize and py <= keySize then
- print("KEY: " .. layout[ty][tx].key)
- layout[ty][tx].pressed = not layout[ty][tx].pressed
- return
- end
- end
- print("Outside keyboard")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement