Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

216 lignes
5.9 KiB

  1. --[[
  2. Copyright (c) 2010-2015 Matthias Richter
  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. Except as contained in this notice, the name(s) of the above copyright holders
  12. shall not be used in advertising or otherwise to promote the sale, use or
  13. other dealings in this Software without prior written authorization.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ]]--
  22. local _PATH = (...):match('^(.*[%./])[^%.%/]+$') or ''
  23. local cos, sin = math.cos, math.sin
  24. local camera = {}
  25. camera.__index = camera
  26. -- Movement interpolators (for camera locking/windowing)
  27. camera.smooth = {}
  28. function camera.smooth.none()
  29. return function(dx,dy) return dx,dy end
  30. end
  31. function camera.smooth.linear(speed)
  32. assert(type(speed) == "number", "Invalid parameter: speed = "..tostring(speed))
  33. return function(dx,dy, s)
  34. -- normalize direction
  35. local d = math.sqrt(dx*dx+dy*dy)
  36. local dts = math.min((s or speed) * love.timer.getDelta(), d) -- prevent overshooting the goal
  37. if d > 0 then
  38. dx,dy = dx/d, dy/d
  39. end
  40. return dx*dts, dy*dts
  41. end
  42. end
  43. function camera.smooth.damped(stiffness)
  44. assert(type(stiffness) == "number", "Invalid parameter: stiffness = "..tostring(stiffness))
  45. return function(dx,dy, s)
  46. local dts = love.timer.getDelta() * (s or stiffness)
  47. return dx*dts, dy*dts
  48. end
  49. end
  50. local function new(x,y, zoom, rot, smoother)
  51. x,y = x or love.graphics.getWidth()/2, y or love.graphics.getHeight()/2
  52. zoom = zoom or 1
  53. rot = rot or 0
  54. smoother = smoother or camera.smooth.none() -- for locking, see below
  55. return setmetatable({x = x, y = y, scale = zoom, rot = rot, smoother = smoother}, camera)
  56. end
  57. function camera:lookAt(x,y)
  58. self.x, self.y = x, y
  59. return self
  60. end
  61. function camera:move(dx,dy)
  62. self.x, self.y = self.x + dx, self.y + dy
  63. return self
  64. end
  65. function camera:position()
  66. return self.x, self.y
  67. end
  68. function camera:rotate(phi)
  69. self.rot = self.rot + phi
  70. return self
  71. end
  72. function camera:rotateTo(phi)
  73. self.rot = phi
  74. return self
  75. end
  76. function camera:zoom(mul)
  77. self.scale = self.scale * mul
  78. return self
  79. end
  80. function camera:zoomTo(zoom)
  81. self.scale = zoom
  82. return self
  83. end
  84. function camera:attach(x,y,w,h, noclip)
  85. x,y = x or 0, y or 0
  86. w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
  87. self._sx,self._sy,self._sw,self._sh = love.graphics.getScissor()
  88. if not noclip then
  89. love.graphics.setScissor(x,y,w,h)
  90. end
  91. local cx,cy = x+w/2, y+h/2
  92. love.graphics.push()
  93. love.graphics.translate(cx, cy)
  94. love.graphics.scale(self.scale)
  95. love.graphics.rotate(self.rot)
  96. love.graphics.translate(-self.x, -self.y)
  97. end
  98. function camera:detach()
  99. love.graphics.pop()
  100. love.graphics.setScissor(self._sx,self._sy,self._sw,self._sh)
  101. end
  102. function camera:draw(...)
  103. local x,y,w,h,noclip,func
  104. local nargs = select("#", ...)
  105. if nargs == 1 then
  106. func = ...
  107. elseif nargs == 5 then
  108. x,y,w,h,func = ...
  109. elseif nargs == 6 then
  110. x,y,w,h,noclip,func = ...
  111. else
  112. error("Invalid arguments to camera:draw()")
  113. end
  114. self:attach(x,y,w,h,noclip)
  115. func()
  116. self:detach()
  117. end
  118. -- world coordinates to camera coordinates
  119. function camera:cameraCoords(x,y, ox,oy,w,h)
  120. ox, oy = ox or 0, oy or 0
  121. w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
  122. -- x,y = ((x,y) - (self.x, self.y)):rotated(self.rot) * self.scale + center
  123. local c,s = cos(self.rot), sin(self.rot)
  124. x,y = x - self.x, y - self.y
  125. x,y = c*x - s*y, s*x + c*y
  126. return x*self.scale + w/2 + ox, y*self.scale + h/2 + oy
  127. end
  128. -- camera coordinates to world coordinates
  129. function camera:worldCoords(x,y, ox,oy,w,h)
  130. ox, oy = ox or 0, oy or 0
  131. w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
  132. -- x,y = (((x,y) - center) / self.scale):rotated(-self.rot) + (self.x,self.y)
  133. local c,s = cos(-self.rot), sin(-self.rot)
  134. x,y = (x - w/2 - ox) / self.scale, (y - h/2 - oy) / self.scale
  135. x,y = c*x - s*y, s*x + c*y
  136. return x+self.x, y+self.y
  137. end
  138. function camera:mousePosition(ox,oy,w,h)
  139. local mx,my = love.mouse.getPosition()
  140. return self:worldCoords(mx,my, ox,oy,w,h)
  141. end
  142. -- camera scrolling utilities
  143. function camera:lockX(x, smoother, ...)
  144. local dx, dy = (smoother or self.smoother)(x - self.x, self.y, ...)
  145. self.x = self.x + dx
  146. return self
  147. end
  148. function camera:lockY(y, smoother, ...)
  149. local dx, dy = (smoother or self.smoother)(self.x, y - self.y, ...)
  150. self.y = self.y + dy
  151. return self
  152. end
  153. function camera:lockPosition(x,y, smoother, ...)
  154. return self:move((smoother or self.smoother)(x - self.x, y - self.y, ...))
  155. end
  156. function camera:lockWindow(x, y, x_min, x_max, y_min, y_max, smoother, ...)
  157. -- figure out displacement in camera coordinates
  158. x,y = self:cameraCoords(x,y)
  159. local dx, dy = 0,0
  160. if x < x_min then
  161. dx = x - x_min
  162. elseif x > x_max then
  163. dx = x - x_max
  164. end
  165. if y < y_min then
  166. dy = y - y_min
  167. elseif y > y_max then
  168. dy = y - y_max
  169. end
  170. -- transform displacement to movement in world coordinates
  171. local c,s = cos(-self.rot), sin(-self.rot)
  172. dx,dy = (c*dx - s*dy) / self.scale, (s*dx + c*dy) / self.scale
  173. -- move
  174. self:move((smoother or self.smoother)(dx,dy,...))
  175. end
  176. -- the module
  177. return setmetatable({new = new, smooth = camera.smooth},
  178. {__call = function(_, ...) return new(...) end})