vbox.nim 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. box
  12. type
  13. VBoxObj* = object of BoxObj
  14. separator*: float
  15. VBoxPtr* = ptr VBoxObj
  16. proc VBox*(name: string, variable: var VBoxObj): VBoxPtr =
  17. nodepattern(VBoxObj)
  18. controlpattern()
  19. variable.rect_size.x = 40
  20. variable.rect_size.y = 40
  21. variable.child_anchor = Anchor(0.5, 0.5, 0.5, 0.5)
  22. variable.separator = 4f
  23. proc VBox*(obj: var VBoxObj): VBoxPtr {.inline.} =
  24. VBox("VBox", obj)
  25. method getChildSize*(self: VBoxPtr): Vector2Ref =
  26. var
  27. x = 0f
  28. y = 0f
  29. for child in self.children:
  30. if child.rect_size.x > x:
  31. x = child.rect_size.x
  32. y += child.rect_size.y + self.separator
  33. if y > 0f:
  34. y -= self.separator
  35. Vector2(x, y)
  36. method addChild*(self: VBoxPtr, child: NodePtr) =
  37. ## Adds new child in current node.
  38. ##
  39. ## Arguments:
  40. ## - `child`: other node.
  41. self.children.add(child)
  42. child.parent = self
  43. self.rect_size = self.getChildSize()
  44. method draw*(self: VBoxPtr, w, h: GLfloat) =
  45. let
  46. x = -w/2 + self.global_position.x
  47. y1 = h/2 - self.global_position.y
  48. glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
  49. glRectf(x, y1, x+self.rect_size.x, y1-self.rect_size.y)
  50. var
  51. fakesize = self.getChildSize()
  52. y = self.rect_size.y*self.child_anchor.y1 - fakesize.y*self.child_anchor.y2
  53. for child in self.children:
  54. child.position.x = self.rect_size.x*self.child_anchor.x1 - child.rect_size.x*self.child_anchor.x2
  55. child.position.y = y
  56. y += child.rect_size.y + self.separator
  57. procCall self.ControlPtr.draw(w, h)
  58. method dublicate*(self: VBoxPtr, obj: var VBoxObj): VBoxPtr {.base.} =
  59. obj = self[]
  60. obj.addr
  61. method resize*(self: VBoxPtr, w, h: GLfloat) =
  62. var size = self.getChildSize()
  63. if size.x < w:
  64. size.x = w
  65. if size.y < h:
  66. size.y = h
  67. self.rect_size.x = size.x
  68. self.rect_size.y = size.y
  69. self.can_use_anchor = false
  70. self.can_use_size_anchor = false