浏览代码

add HBox and VBox nodes.

SakiKawasaki 5 年之前
父节点
当前提交
a55186490a
共有 7 个文件被更改,包括 265 次插入2 次删除
  1. 4 2
      src/nodesnim.nim
  2. 20 0
      src/nodesnim/nodescontrol/box.nim
  3. 75 0
      src/nodesnim/nodescontrol/hbox.nim
  4. 75 0
      src/nodesnim/nodescontrol/vbox.nim
  5. 3 0
      tests/README.md
  6. 43 0
      tests/test13.nim
  7. 45 0
      tests/test14.nim

+ 4 - 2
src/nodesnim.nim

@@ -23,11 +23,13 @@ import
   nodesnim/nodescontrol/texture_rect,
   nodesnim/nodescontrol/label,
   nodesnim/nodescontrol/button,
-  nodesnim/nodescontrol/box
+  nodesnim/nodescontrol/box,
+  nodesnim/nodescontrol/hbox,
+  nodesnim/nodescontrol/vbox
 
 export
   opengl, glut,
   window, environment,
   vector2, rect2, enums, anchor, color, exceptions, input, image,
   node, scene, canvas,
-  control, color_rect, texture_rect, label, button, box
+  control, color_rect, texture_rect, label, button, box, hbox, vbox

+ 20 - 0
src/nodesnim/nodescontrol/box.nim

@@ -29,6 +29,15 @@ proc Box*(obj: var BoxObj): BoxPtr {.inline.} =
   Box("Box", obj)
 
 
+method getChildSize*(self: BoxPtr): Vector2Ref {.base.} =
+  var
+    x = 0f
+    y = 0f
+  for child in self.children:
+    x += child.rect_size.x
+    y += child.rect_size.y
+  Vector2(x, y)
+
 method addChild*(self: BoxPtr, child: NodePtr) =
   ## Adds new child in current node.
   ##
@@ -47,3 +56,14 @@ method draw*(self: BoxPtr, w, h: GLfloat) =
     child.position.x = self.rect_size.x*self.child_anchor.x1 - child.rect_size.x*self.child_anchor.x2
     child.position.y = self.rect_size.y*self.child_anchor.y1 - child.rect_size.y*self.child_anchor.y2
   procCall self.ControlPtr.draw(w, h)
+
+method resize*(self: BoxPtr, w, h: GLfloat) =
+  var size = self.getChildSize()
+  if size.x < w:
+    size.x = w
+  if size.y < h:
+    size.y = h
+  self.rect_size.x = size.x
+  self.rect_size.y = size.y
+  self.can_use_anchor = false
+  self.can_use_size_anchor = false

+ 75 - 0
src/nodesnim/nodescontrol/hbox.nim

@@ -0,0 +1,75 @@
+# author: Ethosa
+import
+  ../thirdparty/opengl,
+
+  ../core/vector2,
+  ../core/rect2,
+  ../core/anchor,
+  ../core/input,
+  ../core/enums,
+
+  ../nodes/node,
+  control,
+  box
+
+
+type
+  HBoxObj* = object of BoxObj
+    separator*: float
+  HBoxPtr* = ptr HBoxObj
+
+
+proc HBox*(name: string, variable: var HBoxObj): HBoxPtr =
+  nodepattern(HBoxObj)
+  controlpattern()
+  variable.rect_size.x = 40
+  variable.rect_size.y = 40
+  variable.child_anchor = Anchor(0.5, 0.5, 0.5, 0.5)
+  variable.separator = 4f
+
+proc HBox*(obj: var HBoxObj): HBoxPtr {.inline.} =
+  HBox("HBox", obj)
+
+
+method getChildSize*(self: HBoxPtr): Vector2Ref =
+  var
+    x = 0f
+    y = 0f
+  for child in self.children:
+    x += child.rect_size.x + self.separator
+    if child.rect_size.y > y:
+      y = child.rect_size.y
+  if x > 0f:
+    x -= self.separator
+  Vector2(x, y)
+
+method addChild*(self: HBoxPtr, child: NodePtr) =
+  ## Adds new child in current node.
+  ##
+  ## Arguments:
+  ## - `child`: other node.
+  self.children.add(child)
+  child.parent = self
+  self.rect_size = self.getChildSize()
+
+
+method draw*(self: HBoxPtr, w, h: GLfloat) =
+  var
+    fakesize = self.getChildSize()
+    x = self.rect_size.x*self.child_anchor.x1 - fakesize.x*self.child_anchor.x2
+  for child in self.children:
+    child.position.x = x
+    child.position.y = self.rect_size.y*self.child_anchor.y1 - child.rect_size.y*self.child_anchor.y2
+    x += child.rect_size.x + self.separator
+  procCall self.ControlPtr.draw(w, h)
+
+method resize*(self: HBoxPtr, w, h: GLfloat) =
+  var size = self.getChildSize()
+  if size.x < w:
+    size.x = w
+  if size.y < h:
+    size.y = h
+  self.rect_size.x = size.x
+  self.rect_size.y = size.y
+  self.can_use_anchor = false
+  self.can_use_size_anchor = false

+ 75 - 0
src/nodesnim/nodescontrol/vbox.nim

@@ -0,0 +1,75 @@
+# author: Ethosa
+import
+  ../thirdparty/opengl,
+
+  ../core/vector2,
+  ../core/rect2,
+  ../core/anchor,
+  ../core/input,
+  ../core/enums,
+
+  ../nodes/node,
+  control,
+  box
+
+
+type
+  VBoxObj* = object of BoxObj
+    separator*: float
+  VBoxPtr* = ptr VBoxObj
+
+
+proc VBox*(name: string, variable: var VBoxObj): VBoxPtr =
+  nodepattern(VBoxObj)
+  controlpattern()
+  variable.rect_size.x = 40
+  variable.rect_size.y = 40
+  variable.child_anchor = Anchor(0.5, 0.5, 0.5, 0.5)
+  variable.separator = 4f
+
+proc VBox*(obj: var VBoxObj): VBoxPtr {.inline.} =
+  VBox("VBox", obj)
+
+
+method getChildSize*(self: VBoxPtr): Vector2Ref =
+  var
+    x = 0f
+    y = 0f
+  for child in self.children:
+    if child.rect_size.x > x:
+      x = child.rect_size.x
+    y += child.rect_size.y + self.separator
+  if y > 0f:
+    y -= self.separator
+  Vector2(x, y)
+
+method addChild*(self: VBoxPtr, child: NodePtr) =
+  ## Adds new child in current node.
+  ##
+  ## Arguments:
+  ## - `child`: other node.
+  self.children.add(child)
+  child.parent = self
+  self.rect_size = self.getChildSize()
+
+
+method draw*(self: VBoxPtr, w, h: GLfloat) =
+  var
+    fakesize = self.getChildSize()
+    y = self.rect_size.y*self.child_anchor.y1 - fakesize.y*self.child_anchor.y2
+  for child in self.children:
+    child.position.x = self.rect_size.x*self.child_anchor.x1 - child.rect_size.x*self.child_anchor.x2
+    child.position.y = y
+    y += child.rect_size.y + self.separator
+  procCall self.ControlPtr.draw(w, h)
+
+method resize*(self: VBoxPtr, w, h: GLfloat) =
+  var size = self.getChildSize()
+  if size.x < w:
+    size.x = w
+  if size.y < h:
+    size.y = h
+  self.rect_size.x = size.x
+  self.rect_size.y = size.y
+  self.can_use_anchor = false
+  self.can_use_size_anchor = false

+ 3 - 0
tests/README.md

@@ -11,3 +11,6 @@
 9. [Use Label node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test9.nim)
 10. [Use Button node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test10.nim)
 11. [Environment setting.](https://github.com/Ethosa/nodesnim/blob/master/tests/test11.nim)
+12. [Use Box node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test12.nim)
+13. [Use HBox node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test13.nim)
+14. [Use VBox node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test14.nim)

+ 43 - 0
tests/test13.nim

@@ -0,0 +1,43 @@
+# --- Test 13. Use HBox node. --- #
+import nodesnim
+
+
+Window("hello world")
+
+var
+  mainobj: SceneObj
+  main = Scene("Main", mainobj)
+
+  hboxobj: HBoxObj     # Create a HBoxObj.
+  hbox = HBox(hboxobj)  # Create pointer to the HBoxObj.
+
+  redobj: ColorRectObj
+  red = ColorRect(redobj)  # #ff6699
+
+  pinkobj: ColorRectObj
+  pink = ColorRect(pinkobj)  #ff64ff
+
+  orangeobj: ColorRectObj
+  orange = ColorRect(orangeobj)  # #ffaa00
+
+
+red.color = Color(0xff6699ff'u32)
+pink.color = Color(0xff64ffff'u32)
+orange.color = Color(0xffaa00ff'u32)
+
+red.resize(128, 128)
+pink.resize(64, 64)
+orange.resize(32, 32)
+
+# Add rects in the Box node.
+hbox.addChild(red)
+hbox.addChild(pink)
+hbox.addChild(orange)
+
+main.addChild(hbox)
+hbox.setAnchor(0, 0.5, 0, 0.5)  # Box anchor in the scene.
+
+
+addScene(main)
+setMainScene("Main")
+windowLaunch()

+ 45 - 0
tests/test14.nim

@@ -0,0 +1,45 @@
+# --- Test 14. Use VBox node. --- #
+import nodesnim
+
+
+Window("hello world")
+
+var
+  mainobj: SceneObj
+  main = Scene("Main", mainobj)
+
+  vboxobj: VBoxObj     # Create a VBoxObj.
+  vbox = VBox(vboxobj)  # Create pointer to the VBoxObj.
+
+  redobj: ColorRectObj
+  red = ColorRect(redobj)  # #ff6699
+
+  pinkobj: ColorRectObj
+  pink = ColorRect(pinkobj)  #ff64ff
+
+  orangeobj: ColorRectObj
+  orange = ColorRect(orangeobj)  # #ffaa00
+
+
+red.color = Color(0xff6699ff'u32)
+pink.color = Color(0xff64ffff'u32)
+orange.color = Color(0xffaa00ff'u32)
+
+red.resize(128, 128)
+pink.resize(64, 64)
+orange.resize(32, 32)
+
+# Add rects in the Box node.
+vbox.addChild(red)
+vbox.addChild(pink)
+vbox.addChild(orange)
+
+main.addChild(vbox)
+vbox.setAnchor(0, 0.5, 0, 0.5)  # Box anchor in the scene.
+vbox.child_anchor = Anchor(0, 1, 0, 1)
+vbox.setSizeAnchor(1, 1)
+
+
+addScene(main)
+setMainScene("Main")
+windowLaunch()