box.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # author: Ethosa
  2. ## Moves all child nodes at the center of the box.
  3. import
  4. ../thirdparty/gl,
  5. ../core/vector2,
  6. ../core/rect2,
  7. ../core/anchor,
  8. ../core/input,
  9. ../core/enums,
  10. ../private/templates,
  11. ../graphics/drawable,
  12. ../nodes/node,
  13. ../nodes/canvas,
  14. control
  15. type
  16. BoxObj* = object of ControlRef
  17. child_anchor*: AnchorObj
  18. BoxRef* = ref BoxObj
  19. proc Box*(name: string = "Box"): BoxRef =
  20. ## Creates a new Box.
  21. ##
  22. ## Arguments:
  23. ## - `name` is a node name.
  24. runnableExamples:
  25. var box1 = Box("My box")
  26. nodepattern(BoxRef)
  27. controlpattern()
  28. result.rect_size.x = 40
  29. result.rect_size.y = 40
  30. result.child_anchor = Anchor(0.5, 0.5, 0.5, 0.5)
  31. result.kind = BOX_NODE
  32. method getChildSize*(self: BoxRef): Vector2Obj {.base.} =
  33. ## Returns Vector2 of the minimal size of the box pointer.
  34. var
  35. x = 0f
  36. y = 0f
  37. for child in self.children:
  38. x += child.CanvasRef.rect_size.x
  39. y += child.CanvasRef.rect_size.y
  40. Vector2(x, y)
  41. method addChild*(self: BoxRef, child: NodeRef) =
  42. ## Adds new child in current node.
  43. ##
  44. ## Arguments:
  45. ## - `child`: other node.
  46. self.children.add(child)
  47. child.parent = self
  48. if child.CanvasRef.rect_size.x > self.rect_size.x:
  49. self.rect_size.x = child.CanvasRef.rect_size.x
  50. if child.CanvasRef.rect_size.y > self.rect_size.y:
  51. self.rect_size.y = child.CanvasRef.rect_size.y
  52. method draw*(self: BoxRef, w, h: GLfloat) =
  53. ## this method uses in the `window.nim`.
  54. {.warning[LockLevel]: off.}
  55. for child in self.children:
  56. child.CanvasRef.position.x = self.rect_size.x*self.child_anchor.x1 - child.CanvasRef.rect_size.x*self.child_anchor.x2 + self.padding.x1
  57. child.CanvasRef.position.y = self.rect_size.y*self.child_anchor.y1 - child.CanvasRef.rect_size.y*self.child_anchor.y2 + self.padding.y1
  58. if child.CanvasRef.rect_size.x > self.rect_size.x:
  59. self.rect_size.x = child.CanvasRef.rect_size.x
  60. if child.CanvasRef.rect_size.y > self.rect_size.y:
  61. self.rect_size.y = child.CanvasRef.rect_size.y
  62. procCall self.ControlRef.draw(w, h)
  63. method duplicate*(self: BoxRef): BoxRef {.base.} =
  64. ## Duplicates Box.
  65. self.deepCopy()
  66. method resize*(self: BoxRef, w, h: GLfloat, save_anchor: bool = false) =
  67. ## Resizes Box node.
  68. ##
  69. ## Arguments:
  70. ## - `w` is a new width.
  71. ## - `h` is a new height.
  72. var size = self.getChildSize()
  73. if size.x < w:
  74. size.x = w
  75. if size.y < h:
  76. size.y = h
  77. self.rect_size.x = size.x
  78. self.rect_size.y = size.y
  79. if not save_anchor:
  80. self.size_anchor.clear()
  81. method setChildAnchor*(self: BoxRef, anchor: AnchorObj) {.base.} =
  82. ## Changes child anchor.
  83. ##
  84. ## Arguments:
  85. ## - `anchor` - Anchor object.
  86. ##
  87. ## See also:
  88. ## - `setChildAnchor method <#setChildAnchor.e,BoxRef,float,float,float,float>`_
  89. self.child_anchor = anchor
  90. method setChildAnchor*(self: BoxRef, 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. ##
  97. ## See also:
  98. ## - `setChildAnchor method <#setChildAnchor.e,BoxRef,AnchorObj>`_
  99. self.child_anchor = Anchor(x1, y1, x2, y2)