box.nim 2.7 KB

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