box.nim 3.1 KB

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