2
0

main.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. -- Display all input devices as floating text
  2. -- Sample contributed by andi mcc
  3. local function appendMovement(s, device)
  4. a,b,c = lovr.headset.getVelocity(device) a = a or -999 b = b or -999 c = c or -9999
  5. s = string.format("%s vel %1.1f,%1.1f,%1.1f", s,a,b,c)
  6. a,b,c = lovr.headset.getAngularVelocity(device) a = a or -999 b = b or -999 c = c or -9999
  7. s = string.format("%s ang %1.1f,%1.1f,%1.1f", s,a,b,c)
  8. return s
  9. end
  10. local lines = {}
  11. local function lineWidth(i, w)
  12. lines[i] = lines[i] and math.max(lines[i], w) or w
  13. return lines[i]
  14. end
  15. local controllerData = {}
  16. local function controllerFetch(k)
  17. if not controllerData[k] then controllerData[k] = {} end
  18. return controllerData[k]
  19. end
  20. local function drawText(pass)
  21. local hands = lovr.headset.getHands()
  22. -- Head
  23. local s = {string.format("%d controllers", #hands),
  24. appendMovement("head ", "head")}
  25. for i,controller in ipairs(hands) do
  26. local c = controllerFetch(controller)
  27. table.insert(s, false)
  28. -- Axes
  29. local axes = ""
  30. for i2, axis in ipairs{"touchpad", "thumbstick"} do
  31. local x,y = lovr.headset.getAxis(controller, axis)
  32. if x and y and (x ~= 0 or y ~= 0) then c[axis] = true end
  33. if c[axis] then -- Show an axis if it is, or ever has been, non-nil non-zero
  34. axes = string.format("%s%s x %1.1f, y %1.1f; ", axes, axis, x,y)
  35. end
  36. end
  37. -- Controller name and axes
  38. table.insert(s, string.format("%d (%s): %strig %1.1f; grip %1.1f", i, controller, axes, lovr.headset.getAxis(controller, "trigger") or -999, lovr.headset.getAxis(controller, "grip") or -999))
  39. s[#s] = appendMovement(s[#s], controller)
  40. -- Buttons
  41. table.insert(s, "DOWN")
  42. for i,name in ipairs({"grip", "trigger", "touchpad", "thumbstick", "menu", "a", "b", "x", "y"}) do
  43. s[#s] = string.format("%s %s:%d", s[#s], name, lovr.headset.isDown(controller, name) and 1 or 0)
  44. end
  45. table.insert(s, "TOUCH")
  46. for i,name in ipairs({"grip", "trigger", "touchpad", "thumbstick", "menu", "a", "b", "x", "y"}) do
  47. s[#s] = string.format("%s %s:%d", s[#s], name, lovr.headset.isTouched(controller, name) and 1 or 0)
  48. end
  49. end
  50. -- Display
  51. local font = lovr.graphics.getDefaultFont()
  52. local fh = font:getHeight()/2 -- Half size render
  53. local top = 2 + fh*#s/2
  54. for i,line in ipairs(s) do -- Manually center using maximum width of each line so it jiggles less
  55. if line then
  56. local w = lineWidth(i, font:getWidth(line))
  57. pass:text(line, -w/2/2, top-fh*i, -3, .5, 0,0,1,0,0, "left")
  58. end
  59. end
  60. end
  61. function lovr.draw(pass)
  62. drawText(pass)
  63. end