camera2d.nim 3.3 KB

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