LayerEx.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. --[[
  2. Copyright (c) 2011-2014 chukong-inc.com
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. ]]
  19. local Layer = cc.Layer
  20. function Layer:onTouch(callback, isMultiTouches, swallowTouches)
  21. if type(isMultiTouches) ~= "boolean" then isMultiTouches = false end
  22. if type(swallowTouches) ~= "boolean" then swallowTouches = false end
  23. self:registerScriptTouchHandler(function(state, ...)
  24. local args = {...}
  25. local event = {name = state}
  26. if isMultiTouches then
  27. args = args[1]
  28. local points = {}
  29. for i = 1, #args, 3 do
  30. local x, y, id = args[i], args[i + 1], args[i + 2]
  31. points[id] = {x = x, y = y, id = id}
  32. end
  33. event.points = points
  34. else
  35. event.x = args[1]
  36. event.y = args[2]
  37. end
  38. return callback( event )
  39. end, isMultiTouches, 0, swallowTouches)
  40. self:setTouchEnabled(true)
  41. return self
  42. end
  43. function Layer:removeTouch()
  44. self:unregisterScriptTouchHandler()
  45. self:setTouchEnabled(false)
  46. return self
  47. end
  48. function Layer:onKeypad(callback)
  49. self:registerScriptKeypadHandler(callback)
  50. self:setKeyboardEnabled(true)
  51. return self
  52. end
  53. function Layer:removeKeypad()
  54. self:unregisterScriptKeypadHandler()
  55. self:setKeyboardEnabled(false)
  56. return self
  57. end
  58. function Layer:onAccelerate(callback)
  59. self:registerScriptAccelerateHandler(callback)
  60. self:setAccelerometerEnabled(true)
  61. return self
  62. end
  63. function Layer:removeAccelerate()
  64. self:unregisterScriptAccelerateHandler()
  65. self:setAccelerometerEnabled(false)
  66. return self
  67. end