box.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. var boxes: seq[BoxObj] = @[]
  17. proc Box*(name: string = "Box"): BoxPtr =
  18. ## Creates a new Box pointer.
  19. ##
  20. ## Arguments:
  21. ## - `name` is a node name.
  22. runnableExamples:
  23. var box1 = Box("My box")
  24. var variable: BoxObj
  25. nodepattern(BoxObj)
  26. controlpattern()
  27. variable.rect_size.x = 40
  28. variable.rect_size.y = 40
  29. variable.child_anchor = Anchor(0.5, 0.5, 0.5, 0.5)
  30. variable.kind = BOX_NODE
  31. boxes.add(variable)
  32. return addr boxes[^1]
  33. method getChildSize*(self: BoxPtr): Vector2Ref {.base.} =
  34. ## Returns Vector2 of the minimal size of the box pointer.
  35. var
  36. x = 0f
  37. y = 0f
  38. for child in self.children:
  39. x += child.rect_size.x
  40. y += child.rect_size.y
  41. Vector2(x, y)
  42. method addChild*(self: BoxPtr, child: NodePtr) =
  43. ## Adds new child in current node.
  44. ##
  45. ## Arguments:
  46. ## - `child`: other node.
  47. self.children.add(child)
  48. child.parent = self
  49. if child.rect_size.x > self.rect_size.x:
  50. self.rect_size.x = child.rect_size.x
  51. if child.rect_size.y > self.rect_size.y:
  52. self.rect_size.y = child.rect_size.y
  53. method draw*(self: BoxPtr, w, h: GLfloat) =
  54. ## this method uses in the `window.nim`.
  55. let
  56. x = -w/2 + self.global_position.x
  57. y = h/2 - self.global_position.y
  58. glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
  59. glRectf(x, y, x+self.rect_size.x, y-self.rect_size.y)
  60. for child in self.children:
  61. child.position.x = self.rect_size.x*self.child_anchor.x1 - child.rect_size.x*self.child_anchor.x2
  62. child.position.y = self.rect_size.y*self.child_anchor.y1 - child.rect_size.y*self.child_anchor.y2
  63. procCall self.ControlPtr.draw(w, h)
  64. method duplicate*(self: BoxPtr): BoxPtr {.base.} =
  65. ## Duplicates Box pointer.
  66. var obj = self[]
  67. boxes.add(obj)
  68. return addr boxes[^1]
  69. method resize*(self: BoxPtr, w, h: GLfloat) =
  70. ## Resizes Box node.
  71. ##
  72. ## Arguments:
  73. ## - `w` is a new width.
  74. ## - `h` is a new height.
  75. var size = self.getChildSize()
  76. if size.x < w:
  77. size.x = w
  78. if size.y < h:
  79. size.y = h
  80. self.rect_size.x = size.x
  81. self.rect_size.y = size.y
  82. self.can_use_anchor = false
  83. self.can_use_size_anchor = false
  84. method setChildAnchor*(self: BoxPtr, anchor: AnchorRef) {.base.} =
  85. ## Changes child anchor.
  86. ##
  87. ## Arguments:
  88. ## - `anchor` - Anchor object.
  89. self.child_anchor = anchor
  90. method setChildAnchor*(self: BoxPtr, x1, y1, x2, y2: float) {.base.} =
  91. ## Changes child anchor.
  92. ##
  93. ## Arguments:
  94. ## - `x1` and `y1` is an anchor relative to Box size.
  95. ## - `x2` and `y2` is an anchor relative to child size.
  96. self.child_anchor = Anchor(x1, y1, x2, y2)