node2d.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # author: Ethosa
  2. ## The base of other 2D nodes.
  3. import
  4. ../thirdparty/opengl,
  5. ../core/vector2,
  6. ../core/rect2,
  7. ../core/anchor,
  8. ../core/input,
  9. ../core/enums,
  10. ../nodes/node,
  11. ../nodes/canvas
  12. type
  13. Node2DObj* = object of CanvasObj
  14. centered*: bool
  15. rotation*: float
  16. scale*: Vector2Obj
  17. timed_position*: Vector2Obj
  18. relative_z_index*: bool
  19. z_index*, z_index_global*: float
  20. Node2DRef* = ref Node2DObj
  21. template node2dpattern*: untyped =
  22. result.centered = false
  23. result.timed_position = Vector2()
  24. result.rect_size = Vector2()
  25. result.rect_min_size = Vector2()
  26. result.position = Vector2()
  27. result.global_position = Vector2()
  28. result.anchor = Anchor(0, 0, 0, 0)
  29. result.size_anchor = Vector2()
  30. result.z_index = 0f
  31. result.z_index_global = 0f
  32. result.relative_z_index = true
  33. result.type_of_node = NODE_TYPE_2D
  34. proc Node2D*(name: string = "Node2D"): Node2DRef =
  35. ## Creates a new Node2D.
  36. ##
  37. ## Arguments:
  38. ## - `name` is a node name.
  39. runnableExamples:
  40. var node = Node2D("Node2D")
  41. nodepattern(Node2DRef)
  42. node2dpattern()
  43. result.scale = Vector2(1, 1)
  44. result.rotation = 0f
  45. result.kind = NODE2D_NODE
  46. method calcGlobalPosition*(self: Node2DRef) =
  47. ## Returns global node position.
  48. self.global_position = self.position
  49. var current: CanvasRef = self
  50. self.z_index_global = self.z_index
  51. while current.parent != nil:
  52. current = current.parent.CanvasRef
  53. self.global_position += current.position
  54. if self.relative_z_index and current.type_of_node == NODE_TYPE_2D:
  55. self.z_index_global += current.Node2DRef.z_index
  56. method draw*(self: Node2DRef, w, h: GLfloat) =
  57. ## this method uses in the `window.nim`.
  58. {.warning[LockLevel]: off.}
  59. self.position = self.timed_position
  60. self.calcGlobalPosition()
  61. if self.centered:
  62. self.position -= self.rect_size/2
  63. method move*(self: Node2DRef, x, y: float) =
  64. ## Moves Node2D object by `x` and `y`.
  65. self.timed_position.x += x
  66. self.timed_position.y += y
  67. self.position = self.timed_position
  68. method move*(self: Node2DRef, vec2: Vector2Obj) =
  69. ## Moves Node2D object by `vec2`.
  70. self.timed_position += vec2
  71. self.position = self.timed_position
  72. method duplicate*(self: Node2DRef): Node2DRef {.base.} =
  73. ## Duplicates Node2D object and create a new Node2D.
  74. self.deepCopy()
  75. method getGlobalMousePosition*(self: Node2DRef): Vector2Obj {.base, inline.} =
  76. ## Returns mouse position.
  77. Vector2Obj(x: last_event.x, y: last_event.y)
  78. method setZIndex*(self: Node2DRef, z_index: int) {.base.} =
  79. ## Changes Z index.
  80. self.z_index = z_index.float