camera2d.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # author: Ethosa
  2. import
  3. ../thirdparty/opengl,
  4. ../core/vector2,
  5. ../core/rect2,
  6. ../core/anchor,
  7. ../core/input,
  8. ../core/enums,
  9. ../nodes/node,
  10. ../nodes/canvas,
  11. node2d
  12. type
  13. Camera2DObj* = object of Node2DObj
  14. current*, smooth*: bool
  15. smooth_speed*: float
  16. target*: NodeRef
  17. limit*: AnchorRef
  18. Camera2DRef* = ref Camera2DObj
  19. var nodes: seq[Camera2DRef] = @[]
  20. proc Camera2D*(name: string = "Camera2D"): Camera2DRef =
  21. ## Creates a new Camera2D.
  22. ##
  23. ## Arguments:
  24. ## - `name` is a node name.
  25. runnableExamples:
  26. var node = Camera2D("Camera2D")
  27. nodepattern(Camera2DRef)
  28. node2dpattern()
  29. result.limit = Anchor(-100000, -100000, 100000, 100000)
  30. result.current = false
  31. result.smooth = false
  32. result.smooth_speed = 0.1
  33. result.kind = CAMERA_2D_NODE
  34. nodes.add(result)
  35. method changeTarget*(self: Camera2DRef, target: NodeRef) {.base.} =
  36. ## Changes camera target (without camera position.)
  37. self.target = target
  38. method changeSmoothSpeed*(self: Camera2DRef, speed: float) {.base.} =
  39. ## Changes camera smooth speed.
  40. ##
  41. ## Arguments:
  42. ## - `speed` is a smooth speed. The closer to 0, the smoother the camera. The default is 0.1.
  43. self.smooth_speed = speed
  44. method disableSmooth*(self: Camera2DRef) {.base.} =
  45. ## Disables smooth mode.
  46. self.smooth = false
  47. method draw*(self: Camera2DRef, w, h: GLfloat) =
  48. ## this method uses in the `window.nim`.
  49. {.warning[LockLevel]: off.}
  50. procCall self.Node2DRef.draw(w, h)
  51. if self.target != nil and self.current:
  52. var root = self.getRootNode().CanvasRef
  53. let
  54. x = self.target.CanvasRef.position.x
  55. y = self.target.CanvasRef.position.y
  56. if self.smooth:
  57. if x-w/2 > self.limit.x1 and x+w/2 < self.limit.x2:
  58. let dx = (root.position.x - w/2) + x
  59. root.position.x -= dx*self.smooth_speed
  60. if y-h/2 > self.limit.y1 and y+h/2 < self.limit.y2:
  61. let dy = (root.position.y - h/2) + y
  62. root.position.y -= dy*self.smooth_speed
  63. else:
  64. root.position.x = if x-w/2 < self.limit.x1: root.position.x elif x+w/2 > self.limit.x2: root.position.x else: -(x - w/2)
  65. root.position.y = if y+h/2 < self.limit.y1: root.position.y elif y+h/2 > self.limit.y2: root.position.y else: -(y - h/2)
  66. method duplicate*(self: Camera2DRef): Camera2DRef {.base.} =
  67. ## Duplicates Camera2D and create a new Camera2D object.
  68. self.deepCopy()
  69. method enableSmooth*(self: Camera2DRef, speed: float = 0.1) {.base.} =
  70. ## Enables camera smooth mode.
  71. ##
  72. ## Arguments:
  73. ## - `speed` is a smooth speed. The closer to 0, the smoother the camera. The default is 0.1.
  74. self.smooth = true
  75. self.smooth_speed = speed
  76. method setCurrent*(self: Camera2DRef) {.base.} =
  77. ## Changes the current camera. It also automatically disable other cameras.
  78. for c in nodes:
  79. c.current = false
  80. self.current = true
  81. method setLimit*(self: Camera2DRef, x1, y1, x2, y2: float) {.base.} =
  82. ## Change camera limit.
  83. self.limit = Anchor(x1, y1, x2, y2)
  84. method setLimit*(self: Camera2DRef, limit: AnchorRef) {.base.} =
  85. ## Changes camera limit.
  86. self.limit = limit
  87. method setTarget*(self: Camera2DRef, target: NodeRef) {.base.} =
  88. ## Changes camera target node.
  89. self.target = target
  90. var root = self.getRootNode().CanvasRef
  91. self.CanvasRef.position = target.CanvasRef.global_position
  92. self.CanvasRef.position -= root.rect_size / 2