Guest User

Untitled

a guest
Mar 27th, 2015
290
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createButtonObject(name, shape, position, color1, color2)
  2.     local o = {}
  3.     o.name = name
  4.     o.shape = shape
  5.     o.position = position
  6.     o.offColor = color1
  7.     o.onColor = color2
  8.     o.pressed = false
  9.     function o:draw()
  10.         local c = {}
  11.         if o.pressed then c = o.onColor else c = o.offColor end
  12.         love.graphics.setColor(c)
  13.         if o.shape == "circle" then
  14.             love.graphics.circle("fill", o.position[1], o.position[2], 10, 12)
  15.         elseif o.shape == "bar" then
  16.             love.graphics.rectangle("fill", o.position[1], o.position[2], 60, 10)
  17.         end
  18.     end
  19.     return o
  20. end
  21.  
  22. function createStickObject(name, position)
  23.     local o = {}
  24.     o.name = name
  25.     o.position = position
  26.     o.x = 0
  27.     o.y = 0
  28.     function o:setStickState(x, y)
  29.         o.x = x
  30.         o.y = y
  31.     end
  32.     function o:draw()
  33.         love.graphics.setColor(30, 30, 30)
  34.         love.graphics.circle("line", o.position[1], o.position[2], 30, 24)
  35.         love.graphics.setColor(70, 0, 70)
  36.         love.graphics.circle("line", o.position[1] + (o.x*30), o.position[2] + (o.y*30), 10, 12)
  37.     end
  38.     return o
  39. end
  40.  
  41. function createTriggerObject(name, position)
  42.     local o = {}
  43.     o.name = name
  44.     o.position = position
  45.     o.a = 0
  46.     function o:setTriggerState(a)
  47.         o.a = a
  48.     end
  49.     function o:draw()
  50.         local l = 80 + ((1 + o.a) * 50)
  51.         local h = 60 - ((1 + o.a) * 29)
  52.         love.graphics.setColor(l,l,l)
  53.         love.graphics.rectangle("fill", o.position[1], o.position[2] + (60-h), 20, h)
  54.         love.graphics.setColor(0,0,0)
  55.         love.graphics.line(o.position[1], o.position[2] + 58, o.position[1] + 20, o.position[2] + 58)
  56.     end
  57.     return o
  58. end
  59.  
  60. function createHatObject(name, position)
  61.     local o = {}
  62.     o.name = name
  63.     o.position = position
  64.     o.dir = "c"
  65.     o.cases = {
  66.         ["c"] = {0, 0},
  67.         ["u"] = {0, -1.5},
  68.         ["ru"] = {1, -1},
  69.         ["r"] = {1.5, 0},
  70.         ["rd"] = {1, 1},
  71.         ["d"] = {0, 1.5},
  72.         ["ld"] = {-1, 1},
  73.         ["l"] = {-1.5, 0},
  74.         ["lu"] = {-1, -1}
  75.     }
  76.     function o:setHatState(d)
  77.         o.dir = d
  78.     end
  79.     function o:draw()
  80.         love.graphics.setColor(70, 70, 70)
  81.         love.graphics.circle("line", o.position[1], o.position[2], 30, 8)
  82.  
  83.         if o.dir == "c" then
  84.             love.graphics.circle("fill", o.position[1], o.position[2], 5, 8)
  85.         else
  86.             local d = o.cases[o.dir]
  87.             love.graphics.setLineWidth(3)
  88.             love.graphics.line(o.position[1], o.position[2], o.position[1] + d[1] * 20, o.position[2] + d[2] * 20)
  89.             love.graphics.setLineWidth(1)
  90.         end
  91.     end
  92.     return o
  93. end
  94.  
  95. function love.load()
  96.     love.graphics.setBackgroundColor(10, 10, 10)
  97.     joystick = love.joystick.getJoysticks()[1]
  98.     buttons = {}
  99.     table.insert(buttons, createButtonObject("a", "circle", {375, 165}, {10, 120, 10}, {20, 240, 20}))
  100.     table.insert(buttons, createButtonObject("b", "circle", {400, 140}, {120, 10, 10}, {240, 20, 20}))
  101.     table.insert(buttons, createButtonObject("x", "circle", {350, 140}, {10, 10, 120}, {20, 20, 240}))
  102.     table.insert(buttons, createButtonObject("y", "circle", {375, 115}, {120, 120, 10}, {240, 240, 20}))
  103.     table.insert(buttons, createButtonObject("left_shoulder", "bar", {100, 80}, {80, 80, 80}, {180, 180, 180}))
  104.     table.insert(buttons, createButtonObject("right_shoulder", "bar", {340, 80}, {80, 80, 80}, {180, 180, 180}))
  105.     table.insert(buttons, createButtonObject("back", "circle", {210, 140}, {130, 130, 130}, {230, 230, 230}))
  106.     table.insert(buttons, createButtonObject("start", "circle", {290, 140}, {130, 130, 130}, {230, 230, 230}))
  107.     table.insert(buttons, createButtonObject("left_stick", "circle", {130, 140}, {40, 40, 40}, {80, 80, 80}))
  108.     table.insert(buttons, createButtonObject("right_stick", "circle", {325, 200}, {40, 40, 40}, {80, 80, 80}))
  109.     table.insert(buttons, createButtonObject("xbox", "circle", {250, 140}, {30, 100, 50}, {50, 200, 80}))
  110.     axes = {}
  111.     table.insert(axes, createStickObject("left_stick", {130, 140}))
  112.     table.insert(axes, createStickObject("right_stick", {325, 200}))
  113.     table.insert(axes, createTriggerObject("left_trigger", {120, 10}))
  114.     table.insert(axes, createTriggerObject("right_trigger", {360, 10}))
  115.     hats = {}
  116.     table.insert(hats, createHatObject("dpad", {170, 200}))
  117. end
  118.  
  119.  
  120. function love.update()
  121.     lx, ly, lt, rx, ry, rt = joystick:getAxes()
  122.     axes[1]:setStickState(lx, ly)
  123.     axes[2]:setStickState(rx, ry)
  124.     axes[3]:setTriggerState(lt)
  125.     axes[4]:setTriggerState(rt)
  126.     hats[1]:setHatState(joystick:getHat(1))
  127. end
  128.  
  129. function love.draw()
  130.     for k, v in pairs(buttons) do
  131.         v:draw()
  132.     end
  133.     for k, v in pairs(axes) do
  134.         v:draw()
  135.     end
  136.     hats[1]:draw()
  137. end
  138.  
  139.  
  140.  
  141. function love.joystickpressed(joystick, button)
  142.     print (buttons[button].name .. " pressed.")
  143.     buttons[button].pressed = true
  144. end
  145.  
  146. function love.joystickreleased(joystick, button)
  147.     print (buttons[button].name .. " released.")
  148.     buttons[button].pressed = false
  149. end
RAW Paste Data