scaler.lua 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. local Scaler = class()
  2. local function invoke(x, k, ...) return x.editor[k](x, ...) end
  3. function Scaler:init()
  4. self.scaling = nil
  5. self.scaleOriginX = 0
  6. self.scaleOriginY = 0
  7. self.scaleHandleX = 0
  8. self.scaleHandleY = 0
  9. self.depth = -10000
  10. ctx.view:register(self)
  11. end
  12. function Scaler:update()
  13. if self.scaling then
  14. local prop = self.scaling
  15. local mx, my = ctx.view:worldPoint(love.mouse.getPosition())
  16. if prop.collision.shape == 'rectangle' then
  17. local xinc, yinc = ctx.grid:snap(mx - self.scaleOriginX, my - self.scaleOriginY)
  18. local newWidth = prop._scaleW + (xinc * math.sign(self.scaleHandleX))
  19. local newHeight = prop._scaleH + (yinc * math.sign(self.scaleHandleY))
  20. if newWidth > 0 and newHeight > 0 then
  21. prop.x = prop._scaleX
  22. prop.y = prop._scaleY
  23. prop.width = prop._scaleW + (xinc * math.sign(self.scaleHandleX))
  24. prop.height = prop._scaleH + (yinc * math.sign(self.scaleHandleY))
  25. if self.scaleHandleX < 0 then prop.x = prop.x + xinc end
  26. if self.scaleHandleY < 0 then prop.y = prop.y + yinc end
  27. ctx.event:emit('collision.detach', {object = prop})
  28. ctx.event:emit('collision.attach', {object = prop})
  29. end
  30. else
  31. --
  32. end
  33. end
  34. end
  35. function Scaler:draw()
  36. if self.scaling and self.scaling.collision.shape == 'rectangle' then
  37. love.graphics.setColor(0, 255, 255, 200)
  38. local x, y, w, h = self.scaling.x, self.scaling.y, self.scaling.width, self.scaling.height
  39. x, y = x - 2, y - 2
  40. w, h = w + 4, h + 4
  41. love.graphics.line(x, y, x + 16, y)
  42. love.graphics.line(x, y, x, y + 16)
  43. love.graphics.line(x + w - 16, y, x + w, y)
  44. love.graphics.line(x + w, y, x + w, y + 16)
  45. love.graphics.line(x, y + h - 16, x, y + h)
  46. love.graphics.line(x, y + h, x + 16, y + h)
  47. love.graphics.line(x + w - 16, y + h, x + w, y + h)
  48. love.graphics.line(x + w, y + h - 16, x + w, y + h)
  49. end
  50. end
  51. function Scaler:mousepressed(x, y, button)
  52. if button == 'r' and not love.keyboard.isDown('lshift') then
  53. local p = ctx.selector:pointTest(x, y)[1]
  54. if p then
  55. local x, y = ctx.view:worldPoint(x, y)
  56. self.scaling = p
  57. self.scaleOriginX = ctx.view:worldMouseX()
  58. self.scaleOriginY = ctx.view:worldMouseY()
  59. p._scaleX = p.x
  60. p._scaleY = p.y
  61. if p.collision.shape == 'rectangle' then
  62. p._scaleW = p.width
  63. p._scaleH = p.height
  64. self.scaleHandleX = x > p.x + (p.width / 2) and 1 or -1
  65. self.scaleHandleY = y > p.y + (p.height / 2) and 1 or -1
  66. else
  67. self.scaleHandleX, self.scaleHandleY = 1, 1
  68. end
  69. end
  70. end
  71. end
  72. function Scaler:mousereleased(x, y, button)
  73. self.scaling = nil
  74. end
  75. return Scaler