Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createButtonObject(name, shape, position, color1, color2)
- local o = {}
- o.name = name
- o.shape = shape
- o.position = position
- o.offColor = color1
- o.onColor = color2
- o.pressed = false
- function o:draw()
- local c = {}
- if o.pressed then c = o.onColor else c = o.offColor end
- love.graphics.setColor(c)
- if o.shape == "circle" then
- love.graphics.circle("fill", o.position[1], o.position[2], 10, 12)
- elseif o.shape == "bar" then
- love.graphics.rectangle("fill", o.position[1], o.position[2], 60, 10)
- end
- end
- return o
- end
- function createStickObject(name, position)
- local o = {}
- o.name = name
- o.position = position
- o.x = 0
- o.y = 0
- function o:setStickState(x, y)
- o.x = x
- o.y = y
- end
- function o:draw()
- love.graphics.setColor(30, 30, 30)
- love.graphics.circle("line", o.position[1], o.position[2], 30, 24)
- love.graphics.setColor(70, 0, 70)
- love.graphics.circle("line", o.position[1] + (o.x*30), o.position[2] + (o.y*30), 10, 12)
- end
- return o
- end
- function createTriggerObject(name, position)
- local o = {}
- o.name = name
- o.position = position
- o.a = 0
- function o:setTriggerState(a)
- o.a = a
- end
- function o:draw()
- local l = 80 + ((1 + o.a) * 50)
- local h = 60 - ((1 + o.a) * 29)
- love.graphics.setColor(l,l,l)
- love.graphics.rectangle("fill", o.position[1], o.position[2] + (60-h), 20, h)
- love.graphics.setColor(0,0,0)
- love.graphics.line(o.position[1], o.position[2] + 58, o.position[1] + 20, o.position[2] + 58)
- end
- return o
- end
- function createHatObject(name, position)
- local o = {}
- o.name = name
- o.position = position
- o.dir = "c"
- o.cases = {
- ["c"] = {0, 0},
- ["u"] = {0, -1.5},
- ["ru"] = {1, -1},
- ["r"] = {1.5, 0},
- ["rd"] = {1, 1},
- ["d"] = {0, 1.5},
- ["ld"] = {-1, 1},
- ["l"] = {-1.5, 0},
- ["lu"] = {-1, -1}
- }
- function o:setHatState(d)
- o.dir = d
- end
- function o:draw()
- love.graphics.setColor(70, 70, 70)
- love.graphics.circle("line", o.position[1], o.position[2], 30, 8)
- if o.dir == "c" then
- love.graphics.circle("fill", o.position[1], o.position[2], 5, 8)
- else
- local d = o.cases[o.dir]
- love.graphics.setLineWidth(3)
- love.graphics.line(o.position[1], o.position[2], o.position[1] + d[1] * 20, o.position[2] + d[2] * 20)
- love.graphics.setLineWidth(1)
- end
- end
- return o
- end
- function love.load()
- love.graphics.setBackgroundColor(10, 10, 10)
- joystick = love.joystick.getJoysticks()[1]
- buttons = {}
- table.insert(buttons, createButtonObject("a", "circle", {375, 165}, {10, 120, 10}, {20, 240, 20}))
- table.insert(buttons, createButtonObject("b", "circle", {400, 140}, {120, 10, 10}, {240, 20, 20}))
- table.insert(buttons, createButtonObject("x", "circle", {350, 140}, {10, 10, 120}, {20, 20, 240}))
- table.insert(buttons, createButtonObject("y", "circle", {375, 115}, {120, 120, 10}, {240, 240, 20}))
- table.insert(buttons, createButtonObject("left_shoulder", "bar", {100, 80}, {80, 80, 80}, {180, 180, 180}))
- table.insert(buttons, createButtonObject("right_shoulder", "bar", {340, 80}, {80, 80, 80}, {180, 180, 180}))
- table.insert(buttons, createButtonObject("back", "circle", {210, 140}, {130, 130, 130}, {230, 230, 230}))
- table.insert(buttons, createButtonObject("start", "circle", {290, 140}, {130, 130, 130}, {230, 230, 230}))
- table.insert(buttons, createButtonObject("left_stick", "circle", {130, 140}, {40, 40, 40}, {80, 80, 80}))
- table.insert(buttons, createButtonObject("right_stick", "circle", {325, 200}, {40, 40, 40}, {80, 80, 80}))
- table.insert(buttons, createButtonObject("xbox", "circle", {250, 140}, {30, 100, 50}, {50, 200, 80}))
- axes = {}
- table.insert(axes, createStickObject("left_stick", {130, 140}))
- table.insert(axes, createStickObject("right_stick", {325, 200}))
- table.insert(axes, createTriggerObject("left_trigger", {120, 10}))
- table.insert(axes, createTriggerObject("right_trigger", {360, 10}))
- hats = {}
- table.insert(hats, createHatObject("dpad", {170, 200}))
- end
- function love.update()
- lx, ly, lt, rx, ry, rt = joystick:getAxes()
- axes[1]:setStickState(lx, ly)
- axes[2]:setStickState(rx, ry)
- axes[3]:setTriggerState(lt)
- axes[4]:setTriggerState(rt)
- hats[1]:setHatState(joystick:getHat(1))
- end
- function love.draw()
- for k, v in pairs(buttons) do
- v:draw()
- end
- for k, v in pairs(axes) do
- v:draw()
- end
- hats[1]:draw()
- end
- function love.joystickpressed(joystick, button)
- print (buttons[button].name .. " pressed.")
- buttons[button].pressed = true
- end
- function love.joystickreleased(joystick, button)
- print (buttons[button].name .. " released.")
- buttons[button].pressed = false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement