소스 검색

add GridBox.

SakiKawasaki 5 년 전
부모
커밋
af8a1bddf9
5개의 변경된 파일159개의 추가작업 그리고 3개의 파일을 삭제
  1. 3 2
      src/nodesnim.nim
  2. 6 0
      src/nodesnim/nodescontrol/box.nim
  3. 105 0
      src/nodesnim/nodescontrol/grid_box.nim
  4. 1 1
      tests/test14.nim
  5. 44 0
      tests/test15.nim

+ 3 - 2
src/nodesnim.nim

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

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

@@ -67,3 +67,9 @@ method resize*(self: BoxPtr, w, h: GLfloat) =
   self.rect_size.y = size.y
   self.can_use_anchor = false
   self.can_use_size_anchor = false
+
+method setChildAnchor*(self: BoxPtr, anchor: AnchorRef) {.base.} =
+  self.child_anchor = anchor
+
+method setChildAnchor*(self: BoxPtr, x1, y1, x2, y2: float) {.base.} =
+  self.child_anchor = Anchor(x1, y1, x2, y2)

+ 105 - 0
src/nodesnim/nodescontrol/grid_box.nim

@@ -0,0 +1,105 @@
+# author: Ethosa
+import
+  ../thirdparty/opengl,
+
+  ../core/vector2,
+  ../core/rect2,
+  ../core/anchor,
+  ../core/input,
+  ../core/enums,
+
+  ../nodes/node,
+  control,
+  box
+
+
+type
+  GridBoxObj* = object of BoxObj
+    separator*: float
+    raw*: int
+  GridBoxPtr* = ptr GridBoxObj
+
+
+proc GridBox*(name: string, variable: var GridBoxObj): GridBoxPtr =
+  nodepattern(GridBoxObj)
+  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
+  variable.raw = 2
+
+proc GridBox*(obj: var GridBoxObj): GridBoxPtr {.inline.} =
+  GridBox("GridBox", obj)
+
+
+method getMaxChildSize*(self: GridBoxPtr): Vector2Ref {.base.} =
+  result = Vector2()
+  for child in self.children:
+    if child.rect_size.x > result.x:
+      result.x = child.rect_size.x
+    if child.rect_size.y > result.y:
+      result.y = child.rect_size.y
+
+method getChildSize*(self: GridBoxPtr): Vector2Ref =
+  var
+    raw = 0
+    maxsize = self.getMaxChildSize()
+    y = maxsize.y
+  for child in self.children:
+    if raw < self.raw:
+      inc raw
+    else:
+      if self.raw > 1:
+        raw = 0
+      y += self.separator + maxsize.y
+  if y > maxsize.y:
+    y -= self.separator
+  Vector2(maxsize.x * self.raw.float + self.separator*self.raw.float, y)
+
+method addChild*(self: GridBoxPtr, 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: GridBoxPtr, w, h: GLfloat) =
+  var
+    raw = 0
+    fakesize = self.getChildSize()
+    maxsize = self.getMaxChildSize()
+    x = self.rect_size.x*self.child_anchor.x1 - fakesize.x*self.child_anchor.x2
+    y = self.rect_size.y*self.child_anchor.y1 - fakesize.y*self.child_anchor.y2
+  for child in self.children:
+    if raw < self.raw:
+      child.position.x = x + maxsize.x*self.child_anchor.x1 - child.rect_size.x*self.child_anchor.x2
+      child.position.y = y + maxsize.y*self.child_anchor.y1 - child.rect_size.y*self.child_anchor.y2
+      x += maxsize.x + self.separator
+      inc raw
+    else:
+      if self.raw > 1:
+        raw = 0
+      x = self.rect_size.x*self.child_anchor.x1 - fakesize.x*self.child_anchor.x2
+      y += maxsize.y + self.separator
+      child.position.x = x + maxsize.x*self.child_anchor.x1 - child.rect_size.x*self.child_anchor.x2
+      child.position.y = y + maxsize.y*self.child_anchor.y1 - child.rect_size.y*self.child_anchor.y2
+  procCall self.ControlPtr.draw(w, h)
+
+method resize*(self: GridBoxPtr, 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
+
+method setRaw*(self: GridBoxPtr, raw: int) {.base.} =
+  self.raw = raw
+  self.rect_size = self.getChildSize()

+ 1 - 1
tests/test14.nim

@@ -36,7 +36,7 @@ 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.setChildAnchor(0, 1, 0, 1)
 vbox.setSizeAnchor(1, 1)
 
 

+ 44 - 0
tests/test15.nim

@@ -0,0 +1,44 @@
+# --- Test 15. Use GridBox node. --- #
+import nodesnim
+
+
+Window("hello world")
+
+var
+  mainobj: SceneObj
+  main = Scene("Main", mainobj)
+
+  grid_boxobj: GridBoxObj     # Create a GridBoxObj.
+  grid_box = GridBox(grid_boxobj)  # Create pointer to the GridBoxObj.
+
+  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.
+grid_box.addChild(red)
+grid_box.addChild(pink)
+grid_box.addChild(orange)
+
+main.addChild(grid_box)
+grid_box.setAnchor(0, 0.5, 0, 0.5)
+grid_box.setRaw(1)
+
+
+addScene(main)
+setMainScene("Main")
+windowLaunch()