Advertisement
Snusmumriken

love2d joysticks 0.3.2

Oct 31st, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. local manager = {}
  2. local OS = love.system.getOS()
  3. manager.joysticks = {}
  4. manager.buttons = {}
  5. manager.sliders = {}
  6. manager.touchList = {}
  7. manager.swapZones = {}
  8. manager.isPhone = OS == "Android"
  9. or OS == "iOS"
  10.  
  11. local function err(lvl, str, ...)
  12. error(str:format(...), lvl)
  13. end
  14.  
  15. local function isNumber(v, str)
  16. return type(v) == 'number' and true or err(3, str, v)
  17. end
  18.  
  19. local function isString(v, str)
  20. return type(v) == 'string' and true or err(3, str, v)
  21. end
  22.  
  23. local function dist(x1, y1, x2, y2)
  24. return x1 and y1 and x2 and y2 and ((x2 - x1)^2 + (y2 - y1)^2)^.5
  25. or x1 and y1 and (x1^2 + y1^2)^.5
  26. end
  27.  
  28. function manager:getTouches()
  29. local t = {}
  30. for k, v in pairs(self.touchList) do
  31. table.insert({id = k, x = v.x, y = v.y})
  32. end
  33. return t
  34. end
  35.  
  36. function manager:updateTouches()
  37. self.touchList = not self.isPhone and love.mouse.isDown(1)
  38. and {mouse = {x = love.mouse.getX(), y = love.mouse.getY()}}
  39. or {}
  40. for i, id in pairs(love.touch.getTouches()) do
  41. self.touchList[id] = {}
  42. self.touchList[id].x, self.touchList[id].y = love.touch.getPosition(id)
  43. end
  44. end
  45.  
  46. function manager:addJoy(name, x, y, rad, blind, sensivity)
  47. local o = {}
  48. o.x = x or 100
  49. o.y = y or 100
  50. o.rad, o.blind = rad or 50, blind or .2
  51. o.tp = 0.1
  52. o.axes = {x = 0, y = 0}
  53. o.touchID = false
  54. o.sensivity = not sensivity
  55. o.touchEnable = love.touch and true or false
  56. self.joysticks[name or ("%4d"):format(math.random(9999))] = o
  57. end
  58.  
  59. local touch -- minimize memory consumption per time (locals)
  60. local x, y, d -- some x, y and distance in update methods
  61.  
  62. function manager:updateJoy()
  63. for k, v in pairs(self.joysticks) do
  64. if v.touchID then
  65. if self.touchList[v.touchID] then
  66. touch = self.touchList[v.touchID] -- using local variable
  67. x, y = (touch.x - v.x)/v.rad, (touch.y - v.y)/v.rad
  68. d = dist(x, y)
  69. d = d < v.blind and math.huge -- gain distance to joy in %
  70. or v.sensivity and d < 1 and 1
  71. or d
  72. v.axes.x, v.axes.y = x / d, y / d
  73. else -- if touch is freed, drop axes and touch
  74. v.axes.x, v.axes.y = 0, 0
  75. v.touchID = false
  76. end
  77. else
  78. for id, t in pairs(self.touchList) do
  79. d = dist(v.x, v.y, t.x, t.y)
  80. if d < v.rad then v.touchID = id end
  81. end
  82. end
  83. end
  84. end
  85.  
  86. function manager:getJoyAxes(name)
  87. if self.joysticks[name] then
  88. return self.joysticks[name].axes.x, self.joysticks[name].axes.y
  89. or err(2, 'No joy has \'%s\' name', name)
  90. end
  91. end
  92.  
  93. function manager:getJoyPosition(name)
  94. if self.joysticks[name] then
  95. return self.joysticks[name].x, self.joysticks[name].y
  96. or err(2, 'No joy has \'%s\' name', name)
  97. end
  98. end
  99.  
  100. function manager:setJoyPosition(name, x, y)
  101. if self.joysticks[name] then
  102. isNumber(x, 'Joy:setPosition arg#1: number expected got %s')
  103. isNumber(x, 'Joy:setPosition arg#2: number expected got %s')
  104. self.joysticks[name].x, self.joysticks[name].y = x, y
  105. or err(2, 'No joy has \'%s\' name', name)
  106. end
  107. end
  108.  
  109. function manager:update()
  110. self:updateTouches()
  111. self:updateJoy()
  112. end
  113.  
  114. local graphics = love.graphics
  115. function manager:draw()
  116. for k, v in pairs(self.joysticks) do
  117. graphics.push('all')
  118. v.tp = v.touchID and 0.8 or v.tp + (0.1 - v.tp)*0.03
  119. graphics.setColor(255, 255, 255, v.tp*255)
  120. x, y = v.axes.x, v.axes.y
  121. graphics.circle('line', v.x, v.y, v.rad)
  122. graphics.circle('line', v.x, v.y, v.blind*v.rad)
  123. if v.touchID then
  124. graphics.print(tostring(v.touchID), v.x, v.y)
  125. end
  126. graphics.line(v.x, v.y, v.x + x*v.rad, v.y + y*v.rad)
  127. graphics.print(('%1.2f:%1.2f'):format(x, y), v.x + x*v.rad, v.y + y*v.rad)
  128. graphics.pop()
  129. end
  130. end
  131.  
  132. return manager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement