vbox.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # author: Ethosa
  2. ## Contains children in the vertical 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. ../nodes/canvas,
  12. control,
  13. box
  14. type
  15. VBoxObj* = object of BoxObj
  16. separator*: float
  17. VBoxRef* = ref VBoxObj
  18. proc VBox*(name: string = "VBox"): VBoxRef =
  19. ## Creates a new VBox.
  20. ##
  21. ## Arguments:
  22. ## - `name` is a node name.
  23. runnableExamples:
  24. var box = VBox("VBox")
  25. nodepattern(VBoxRef)
  26. controlpattern()
  27. result.rect_size.x = 40
  28. result.rect_size.y = 40
  29. result.separator = 4f
  30. result.kind = VBOX_NODE
  31. method getChildSize*(self: VBoxRef): Vector2Obj =
  32. ## Returns size of all children.
  33. var
  34. x = 0f
  35. y = 0f
  36. for child in self.children:
  37. if child.CanvasRef.rect_size.x > x:
  38. x = child.CanvasRef.rect_size.x
  39. if child.type_of_node == NODE_TYPE_CONTROL:
  40. y += child.ControlRef.margin.y1 + child.ControlRef.margin.y2
  41. let x_sizem = child.ControlRef.margin.x1 + child.ControlRef.margin.x2 + child.ControlRef.rect_size.x
  42. if x < x_sizem:
  43. x = x_sizem
  44. y += child.CanvasRef.rect_size.y + self.separator
  45. if y > 0f:
  46. y -= self.separator
  47. Vector2(x, y)
  48. method addChild*(self: VBoxRef, child: NodeRef) =
  49. ## Adds new child in current node.
  50. ##
  51. ## Arguments:
  52. ## - `child`: other node.
  53. self.children.add(child)
  54. child.parent = self
  55. self.resize(self.rect_size.x, self.rect_size.y, true)
  56. method draw*(self: VBoxRef, w, h: GLfloat) =
  57. ## This uses in the `window.nim`.
  58. procCall self.ControlRef.draw(w, h)
  59. var
  60. fakesize = self.getChildSize()
  61. y = self.rect_size.y*self.child_anchor.y1 - fakesize.y*self.child_anchor.y2
  62. for child in self.children:
  63. if child.type_of_node == NODE_TYPE_CONTROL:
  64. y += child.ControlRef.margin.y1
  65. child.CanvasRef.position.y = y + self.padding.y1
  66. child.CanvasRef.position.x = self.rect_size.x*self.child_anchor.x1 - child.CanvasRef.rect_size.x*self.child_anchor.x2 + self.padding.x1 + child.ControlRef.margin.x1
  67. y += child.ControlRef.margin.y2
  68. else:
  69. child.CanvasRef.position.y = y + self.padding.y1
  70. child.CanvasRef.position.x = self.rect_size.x*self.child_anchor.x1 - child.CanvasRef.rect_size.x*self.child_anchor.x2 + self.padding.x1
  71. y += child.CanvasRef.rect_size.y + self.separator
  72. method duplicate*(self: VBoxRef): VBoxRef {.base.} =
  73. ## Duplicate VBox object and create a new VBox.
  74. self.deepCopy()
  75. method resize*(self: VBoxRef, w, h: GLfloat, save_anchor: bool = false) =
  76. ## Resizes VBox, if available.
  77. ##
  78. ## Arguments:
  79. ## - `w` is a new width.
  80. ## - `h` is a new height.
  81. var size = self.getChildSize()
  82. if size.x < w:
  83. size.x = w
  84. if size.y < h:
  85. size.y = h
  86. self.rect_size.x = size.x
  87. self.rect_size.y = size.y
  88. if not save_anchor:
  89. self.size_anchor.clear()