CoreInput.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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:getNumJoysticks()
  56. local retVal = Polycore.CoreInput_getNumJoysticks(self.__ptr)
  57. return retVal
  58. end
  59. function CoreInput:getJoystickInfoByIndex(index)
  60. local retVal = Polycore.CoreInput_getJoystickInfoByIndex(self.__ptr, index)
  61. if retVal == nil then return nil end
  62. if Polycore.__ptr_lookup[retVal] ~= nil then
  63. return Polycore.__ptr_lookup[retVal]
  64. else
  65. Polycore.__ptr_lookup[retVal] = JoystickInfo("__skip_ptr__")
  66. Polycore.__ptr_lookup[retVal].__ptr = retVal
  67. return Polycore.__ptr_lookup[retVal]
  68. end
  69. end
  70. function CoreInput:getJoystickInfoByID(deviceID)
  71. local retVal = Polycore.CoreInput_getJoystickInfoByID(self.__ptr, deviceID)
  72. if retVal == nil then return nil end
  73. if Polycore.__ptr_lookup[retVal] ~= nil then
  74. return Polycore.__ptr_lookup[retVal]
  75. else
  76. Polycore.__ptr_lookup[retVal] = JoystickInfo("__skip_ptr__")
  77. Polycore.__ptr_lookup[retVal].__ptr = retVal
  78. return Polycore.__ptr_lookup[retVal]
  79. end
  80. end
  81. function CoreInput:addJoystick(deviceID)
  82. local retVal = Polycore.CoreInput_addJoystick(self.__ptr, deviceID)
  83. end
  84. function CoreInput:removeJoystick(deviceID)
  85. local retVal = Polycore.CoreInput_removeJoystick(self.__ptr, deviceID)
  86. end
  87. function CoreInput:joystickAxisMoved(axisID, value, deviceID)
  88. local retVal = Polycore.CoreInput_joystickAxisMoved(self.__ptr, axisID, value, deviceID)
  89. end
  90. function CoreInput:joystickButtonDown(buttonID, deviceID)
  91. local retVal = Polycore.CoreInput_joystickButtonDown(self.__ptr, buttonID, deviceID)
  92. end
  93. function CoreInput:joystickButtonUp(buttonID, deviceID)
  94. local retVal = Polycore.CoreInput_joystickButtonUp(self.__ptr, buttonID, deviceID)
  95. end
  96. function CoreInput:mouseWheelUp(ticks)
  97. local retVal = Polycore.CoreInput_mouseWheelUp(self.__ptr, ticks)
  98. end
  99. function CoreInput:mouseWheelDown(ticks)
  100. local retVal = Polycore.CoreInput_mouseWheelDown(self.__ptr, ticks)
  101. end
  102. function CoreInput:setMouseButtonState(mouseButton, state, ticks)
  103. local retVal = Polycore.CoreInput_setMouseButtonState(self.__ptr, mouseButton, state, ticks)
  104. end
  105. function CoreInput:setMousePosition(x, y, ticks)
  106. local retVal = Polycore.CoreInput_setMousePosition(self.__ptr, x, y, ticks)
  107. end
  108. function CoreInput:setKeyState(keyCode, code, newState, ticks)
  109. local retVal = Polycore.CoreInput_setKeyState(self.__ptr, keyCode.__ptr, code.__ptr, newState, ticks)
  110. end
  111. function CoreInput:setDeltaPosition(x, y)
  112. local retVal = Polycore.CoreInput_setDeltaPosition(self.__ptr, x, y)
  113. end
  114. function CoreInput:createEvent(event)
  115. local retVal = Polycore.CoreInput_createEvent(event.__ptr)
  116. if retVal == nil then return nil end
  117. if Polycore.__ptr_lookup[retVal] ~= nil then
  118. return Polycore.__ptr_lookup[retVal]
  119. else
  120. Polycore.__ptr_lookup[retVal] = InputEvent("__skip_ptr__")
  121. Polycore.__ptr_lookup[retVal].__ptr = retVal
  122. return Polycore.__ptr_lookup[retVal]
  123. end
  124. end
  125. function CoreInput:__delete()
  126. Polycore.__ptr_lookup[self.__ptr] = nil
  127. Polycore.delete_CoreInput(self.__ptr)
  128. end