dragger.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. local Dragger = class()
  2. local function invoke(x, k, ...) return x.editor[k](x, ...) end
  3. function Dragger:init()
  4. self.dragging = false
  5. self.dragX = 0
  6. self.dragY = 0
  7. self.depth = -10000
  8. self.deselect = false
  9. end
  10. function Dragger:update()
  11. if self.dragging then
  12. ctx.selector:each(function(prop)
  13. local ox, oy = ctx.grid:snap(prop._dragX, prop._dragY)
  14. local x, y = ctx.grid:snap(ctx.view:worldMouseX() - self.dragX, ctx.view:worldMouseY() - self.dragY)
  15. prop.x, prop.y = ox + x, oy + y
  16. ctx.event:emit('collision.move', {object = prop})
  17. end)
  18. end
  19. end
  20. function Dragger:mousepressed(x, y, button)
  21. if button == 'l' then
  22. self.deselect = false
  23. if love.keyboard.isDown('lshift') then return end
  24. if #ctx.selector.selection == 0 then
  25. ctx.selector:select(unpack(ctx.selector:pointTest(x, y)))
  26. self.deselect = true
  27. end
  28. self.dragging = true
  29. self.dragX = ctx.view:worldMouseX()
  30. self.dragY = ctx.view:worldMouseY()
  31. ctx.selector:each(function(prop)
  32. prop._dragX = prop.x
  33. prop._dragY = prop.y
  34. end)
  35. end
  36. end
  37. function Dragger:mousereleased(x, y, button)
  38. self.dragging = false
  39. if self.deselect then ctx.selector:deselectAll() end
  40. end
  41. return Dragger