view.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. local View = extend(app.media.view)
  2. function View:init()
  3. self.targetScale = 1
  4. self.xVel = 0
  5. self.yVel = 0
  6. self.maxSpeed = 20
  7. app.media.view.init(self)
  8. end
  9. function View:update()
  10. app.media.view.update(self)
  11. if not love.keyboard.isDown('lctrl') then
  12. if love.keyboard.isDown('w') then
  13. self.yVel = math.lerp(self.yVel, -self.maxSpeed, 10 * tickRate)
  14. elseif love.keyboard.isDown('s') then
  15. self.yVel = math.lerp(self.yVel, self.maxSpeed, 10 * tickRate)
  16. else
  17. self.yVel = math.lerp(self.yVel, 0, .1)
  18. end
  19. if love.keyboard.isDown('a') then
  20. self.xVel = math.lerp(self.xVel, -self.maxSpeed, 10 * tickRate)
  21. elseif love.keyboard.isDown('d') then
  22. self.xVel = math.lerp(self.xVel, self.maxSpeed, 10 * tickRate)
  23. else
  24. self.xVel = math.lerp(self.xVel, 0, .1)
  25. end
  26. end
  27. self.x = self.x + (self.xVel / (self.targetScale ^ 0.5))
  28. self.y = self.y + (self.yVel / (self.targetScale ^ 0.5))
  29. local prevw, prevh = self.width, self.height
  30. local xf, yf = love.mouse.getX() / love.graphics.getWidth(), love.mouse.getY() / love.graphics.getHeight()
  31. self.scale = math.round(math.lerp(self.scale, self.targetScale, 10 * tickRate) / .01) * .01
  32. self.width = love.graphics.getWidth() / self.scale
  33. self.height = love.graphics.getHeight() / self.scale
  34. self.x = self.x + (prevw - self.width) * xf
  35. self.y = self.y + (prevh - self.height) * yf
  36. end
  37. function View:mousepressed(x, y, button)
  38. if button == 'wu' then
  39. self.targetScale = math.min(self.targetScale + .2, 4)
  40. elseif button == 'wd' then
  41. self.targetScale = math.max(self.targetScale - .2, .2)
  42. end
  43. end
  44. View.contain = f.empty
  45. return View