box.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # author: Ethosa
  2. ## Moves all child nodes at the center of the box.
  3. import
  4. ../thirdparty/opengl,
  5. ../core/vector2,
  6. ../core/rect2,
  7. ../core/anchor,
  8. ../core/input,
  9. ../core/enums,
  10. ../nodes/node,
  11. control
  12. type
  13. BoxObj* = object of ControlPtr
  14. child_anchor*: AnchorRef
  15. BoxPtr* = ptr BoxObj
  16. proc Box*(name: string, variable: var BoxObj): BoxPtr =
  17. ## Creates a new Box pointer.
  18. ##
  19. ## Arguments:
  20. ## - `name` is a node name.
  21. ## - `variable` is a BoxObj variable.
  22. runnableExamples:
  23. var
  24. box1_obj: BoxObj
  25. box1 = Box("My box", box1_obj)
  26. nodepattern(BoxObj)
  27. controlpattern()
  28. variable.rect_size.x = 40
  29. variable.rect_size.y = 40
  30. variable.child_anchor = Anchor(0.5, 0.5, 0.5, 0.5)
  31. variable.kind = BOX_NODE
  32. proc Box*(obj: var BoxObj): BoxPtr {.inline.} =
  33. ## Creates a new Box pointer with default name "Box"
  34. ##
  35. ## Arguments:
  36. ## - `obj` is a BoxObj variable.
  37. runnableExamples:
  38. var
  39. box1_obj: BoxObj
  40. box1 = Box(box1_obj)
  41. Box("Box", obj)
  42. method getChildSize*(self: BoxPtr): Vector2Ref {.base.} =
  43. ## Returns Vector2 of the minimal size of the box pointer.
  44. var
  45. x = 0f
  46. y = 0f
  47. for child in self.children:
  48. x += child.rect_size.x
  49. y += child.rect_size.y
  50. Vector2(x, y)
  51. method addChild*(self: BoxPtr, child: NodePtr) =
  52. ## Adds new child in current node.
  53. ##
  54. ## Arguments:
  55. ## - `child`: other node.
  56. self.children.add(child)
  57. child.parent = self
  58. if child.rect_size.x > self.rect_size.x:
  59. self.rect_size.x = child.rect_size.x
  60. if child.rect_size.y > self.rect_size.y:
  61. self.rect_size.y = child.rect_size.y
  62. method draw*(self: BoxPtr, w, h: GLfloat) =
  63. ## this method uses in the `window.nim`.
  64. let
  65. x = -w/2 + self.global_position.x
  66. y = h/2 - self.global_position.y
  67. glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
  68. glRectf(x, y, x+self.rect_size.x, y-self.rect_size.y)
  69. for child in self.children:
  70. child.position.x = self.rect_size.x*self.child_anchor.x1 - child.rect_size.x*self.child_anchor.x2
  71. child.position.y = self.rect_size.y*self.child_anchor.y1 - child.rect_size.y*self.child_anchor.y2
  72. procCall self.ControlPtr.draw(w, h)
  73. method duplicate*(self: BoxPtr, obj: var BoxObj): BoxPtr {.base.} =
  74. ## Duplicates Box pointer.
  75. ##
  76. ## Arguments:
  77. ## - `obj` is BoxObj variable.
  78. obj = self[]
  79. obj.addr
  80. method resize*(self: BoxPtr, w, h: GLfloat) =
  81. ## Resizes Box node.
  82. ##
  83. ## Arguments:
  84. ## - `w` is a new width.
  85. ## - `h` is a new height.
  86. var size = self.getChildSize()
  87. if size.x < w:
  88. size.x = w
  89. if size.y < h:
  90. size.y = h
  91. self.rect_size.x = size.x
  92. self.rect_size.y = size.y
  93. self.can_use_anchor = false
  94. self.can_use_size_anchor = false
  95. method setChildAnchor*(self: BoxPtr, anchor: AnchorRef) {.base.} =
  96. ## Changes child anchor.
  97. ##
  98. ## Arguments:
  99. ## - `anchor` - Anchor object.
  100. self.child_anchor = anchor
  101. method setChildAnchor*(self: BoxPtr, x1, y1, x2, y2: float) {.base.} =
  102. ## Changes child anchor.
  103. ##
  104. ## Arguments:
  105. ## - `x1` and `y1` is an anchor relative to Box size.
  106. ## - `x2` and `y2` is an anchor relative to child size.
  107. self.child_anchor = Anchor(x1, y1, x2, y2)