grid_box.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # author: Ethosa
  2. ## Contains children in grid.
  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. GridBoxObj* = object of BoxObj
  16. separator*: float
  17. row*: int
  18. GridBoxRef* = ref GridBoxObj
  19. proc GridBox*(name: string = "GridBox"): GridBoxRef =
  20. ## Creates a new GridBox.
  21. ##
  22. ## Arguments:
  23. ## - `name` is a node name.
  24. runnableExamples:
  25. var grid = GridBox("GridBox")
  26. nodepattern(GridBoxRef)
  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.separator = 4f
  32. result.row = 2
  33. result.kind = GRID_BOX_NODE
  34. method getMaxChildSize*(self: GridBoxRef): Vector2Ref {.base.} =
  35. result = Vector2()
  36. for child in self.children:
  37. if child.CanvasRef.rect_size.x > result.x:
  38. result.x = child.CanvasRef.rect_size.x
  39. if child.CanvasRef.rect_size.y > result.y:
  40. result.y = child.CanvasRef.rect_size.y
  41. method getChildSize*(self: GridBoxRef): Vector2Ref =
  42. ## Returns size with all childs.
  43. var
  44. row = 0
  45. maxsize = self.getMaxChildSize()
  46. y = maxsize.y
  47. w = maxsize.x * self.row.float
  48. for child in self.children:
  49. if row < self.row:
  50. inc row
  51. else:
  52. if self.row > 1:
  53. row = 1
  54. y += self.separator + maxsize.y
  55. if y > maxsize.y:
  56. y -= self.separator
  57. if self.children.len() > 0:
  58. w += self.separator * (self.row.float - 1)
  59. Vector2(w, y)
  60. method addChild*(self: GridBoxRef, child: NodeRef) =
  61. ## Adds new child in current node.
  62. ##
  63. ## Arguments:
  64. ## - `child`: other node.
  65. self.children.add(child)
  66. child.parent = self
  67. self.rect_size = self.getChildSize()
  68. method draw*(self: GridBoxRef, w, h: GLfloat) =
  69. ## This method uses in the `window.nim`.
  70. var
  71. row = 0
  72. fakesize = self.getChildSize()
  73. maxsize = self.getMaxChildSize()
  74. x = self.rect_size.x*self.child_anchor.x1 - fakesize.x*self.child_anchor.x2
  75. y = self.rect_size.y*self.child_anchor.y1 - fakesize.y*self.child_anchor.y2
  76. for child in self.children:
  77. if row < self.row:
  78. child.CanvasRef.position.x = x + maxsize.x*self.child_anchor.x1 - child.CanvasRef.rect_size.x*self.child_anchor.x2
  79. child.CanvasRef.position.y = y + maxsize.y*self.child_anchor.y1 - child.CanvasRef.rect_size.y*self.child_anchor.y2
  80. x += maxsize.x + self.separator
  81. inc row
  82. else:
  83. if self.row > 1:
  84. row = 1
  85. x = self.rect_size.x*self.child_anchor.x1 - fakesize.x*self.child_anchor.x2
  86. y += maxsize.y + self.separator
  87. child.CanvasRef.position.x = x + maxsize.x*self.child_anchor.x1 - child.CanvasRef.rect_size.x*self.child_anchor.x2
  88. child.CanvasRef.position.y = y + maxsize.y*self.child_anchor.y1 - child.CanvasRef.rect_size.y*self.child_anchor.y2
  89. x += maxsize.x + self.separator
  90. procCall self.ControlRef.draw(w, h)
  91. method duplicate*(self: GridBoxRef): GridBoxRef {.base.} =
  92. ## Duplicates GridBox object and create a new GridBox.
  93. self.deepCopy()
  94. method resize*(self: GridBoxRef, w, h: GLfloat) =
  95. ## Resizes GridBox.
  96. ##
  97. ## Arguments:
  98. ## - `w` is a new width.
  99. ## - `h` is a new height.
  100. var size = self.getChildSize()
  101. if size.x < w:
  102. size.x = w
  103. if size.y < h:
  104. size.y = h
  105. self.rect_size.x = size.x
  106. self.rect_size.y = size.y
  107. self.can_use_anchor = false
  108. self.can_use_size_anchor = false
  109. method setRow*(self: GridBoxRef, row: int) {.base.} =
  110. ## Changes gridBox row count.
  111. self.row = row
  112. self.rect_size = self.getChildSize()
  113. method setSeparator*(self: GridBoxRef, separator: float) {.base.} =
  114. ## Changes separator between child nodes.
  115. self.separator = separator
  116. self.rect_size = self.getChildSize()