CoreInput.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. require "Polycode/EventDispatcher"
  2. class "CoreInput" (EventDispatcher)
  3. MOUSE_BUTTON1 = 0
  4. MOUSE_BUTTON2 = 1
  5. MOUSE_BUTTON3 = 2
  6. function CoreInput:CoreInput(...)
  7. if type(arg[1]) == "table" and count(arg) == 1 then
  8. if ""..arg[1]:class() == "EventDispatcher" then
  9. self.__ptr = arg[1].__ptr
  10. return
  11. end
  12. end
  13. for k,v in pairs(arg) do
  14. if type(v) == "table" then
  15. if v.__ptr ~= nil then
  16. arg[k] = v.__ptr
  17. end
  18. end
  19. end
  20. if self.__ptr == nil and arg[1] ~= "__skip_ptr__" then
  21. self.__ptr = Polycore.CoreInput(unpack(arg))
  22. Polycore.__ptr_lookup[self.__ptr] = self
  23. end
  24. end
  25. function CoreInput:getMousePosition()
  26. local retVal = Polycore.CoreInput_getMousePosition(self.__ptr)
  27. if retVal == nil then return nil end
  28. if Polycore.__ptr_lookup[retVal] ~= nil then
  29. return Polycore.__ptr_lookup[retVal]
  30. else
  31. Polycore.__ptr_lookup[retVal] = Vector2("__skip_ptr__")
  32. Polycore.__ptr_lookup[retVal].__ptr = retVal
  33. return Polycore.__ptr_lookup[retVal]
  34. end
  35. end
  36. function CoreInput:getKeyState(keyCode)
  37. local retVal = Polycore.CoreInput_getKeyState(self.__ptr, keyCode.__ptr)
  38. return retVal
  39. end
  40. function CoreInput:getMouseDelta()
  41. local retVal = Polycore.CoreInput_getMouseDelta(self.__ptr)
  42. if retVal == nil then return nil end
  43. if Polycore.__ptr_lookup[retVal] ~= nil then
  44. return Polycore.__ptr_lookup[retVal]
  45. else
  46. Polycore.__ptr_lookup[retVal] = Vector2("__skip_ptr__")
  47. Polycore.__ptr_lookup[retVal].__ptr = retVal
  48. return Polycore.__ptr_lookup[retVal]
  49. end
  50. end
  51. function CoreInput:getMouseButtonState(mouseButton)
  52. local retVal = Polycore.CoreInput_getMouseButtonState(self.__ptr, mouseButton)
  53. return retVal
  54. end
  55. function CoreInput:mouseWheelUp(ticks)
  56. local retVal = Polycore.CoreInput_mouseWheelUp(self.__ptr, ticks)
  57. end
  58. function CoreInput:mouseWheelDown(ticks)
  59. local retVal = Polycore.CoreInput_mouseWheelDown(self.__ptr, ticks)
  60. end
  61. function CoreInput:setMouseButtonState(mouseButton, state, ticks)
  62. local retVal = Polycore.CoreInput_setMouseButtonState(self.__ptr, mouseButton, state, ticks)
  63. end
  64. function CoreInput:setMousePosition(x, y, ticks)
  65. local retVal = Polycore.CoreInput_setMousePosition(self.__ptr, x, y, ticks)
  66. end
  67. function CoreInput:setKeyState(keyCode, code, newState, ticks)
  68. local retVal = Polycore.CoreInput_setKeyState(self.__ptr, keyCode.__ptr, code.__ptr, newState, ticks)
  69. end
  70. function CoreInput:setDeltaPosition(x, y)
  71. local retVal = Polycore.CoreInput_setDeltaPosition(self.__ptr, x, y)
  72. end
  73. function CoreInput:createEvent(event)
  74. local retVal = Polycore.CoreInput_createEvent(event.__ptr)
  75. if retVal == nil then return nil end
  76. if Polycore.__ptr_lookup[retVal] ~= nil then
  77. return Polycore.__ptr_lookup[retVal]
  78. else
  79. Polycore.__ptr_lookup[retVal] = InputEvent("__skip_ptr__")
  80. Polycore.__ptr_lookup[retVal].__ptr = retVal
  81. return Polycore.__ptr_lookup[retVal]
  82. end
  83. end
  84. function CoreInput:__delete()
  85. Polycore.__ptr_lookup[self.__ptr] = nil
  86. Polycore.delete_CoreInput(self.__ptr)
  87. end