Ethosa 5 éve
szülő
commit
6be74b0bf7

+ 2 - 0
src/nodesnim/nodes/canvas.nim

@@ -150,3 +150,5 @@ method fill*(canvas: CanvasPtr, color: ColorRef) {.base.} =
 method resize*(canvas: CanvasPtr, w, h: GLfloat) {.base.} =
   canvas.rect_size.x = w
   canvas.rect_size.y = h
+  canvas.anchor = nil
+  canvas.size_anchor = nil

+ 3 - 1
src/nodesnim/nodes/node.nim

@@ -20,6 +20,7 @@ type
     position*: Vector2Ref            ## Node position, by default is Vector2(0, 0).
     global_position*: Vector2Ref     ## Node global position.
     rect_size*: Vector2Ref           ## Node size.
+    size_anchor*: Vector2Ref         ## Node size anchor.
     anchor*: AnchorRef               ## Node anchor.
     parent*: NodePtr                 ## Node parent.
     children*: seq[NodePtr]          ## Node children.
@@ -42,7 +43,7 @@ template nodepattern*(nodetype: untyped): untyped =
     enter: proc() = discard,
     exit: proc() = discard,
     is_ready: false, pausemode: INHERIT, visible: true,
-    anchor: nil
+    anchor: nil, size_anchor: nil
   )
   result = variable.addr
 
@@ -202,6 +203,7 @@ method move*(self: NodePtr, vec2: Vector2Ref) {.base, inline.} =
   ## - `vec2`: how much to add to the position on the X,Y axes.
   self.position += vec2
   self.anchor = nil
+  self.size_anchor = nil
 
 method removeChild*(self: NodePtr, index: int) {.base.} =
   ## Removes node child at a specific position.

+ 1 - 1
src/nodesnim/nodes/scene.nim

@@ -58,5 +58,5 @@ method reAnchorScene*(scene: ScenePtr, w, h: GLfloat, paused: bool) {.base.} =
   for child in scene.getChildIter():
     if paused and child.getPauseMode() != PROCESS:
       continue
-    if child.visible and child.anchor != nil:
+    if child.visible:
       child.calcPositionAnchor()

+ 8 - 2
src/nodesnim/nodescontrol/control.nim

@@ -50,8 +50,14 @@ proc Control*(obj: var ControlObj): ControlPtr {.inline.} =
 
 method calcPositionAnchor*(self: ControlPtr) =
   if self.parent != nil:
-    self.position.x = self.parent.rect_size.x*self.anchor.x1 - self.rect_size.x*self.anchor.x2
-    self.position.y = self.parent.rect_size.y*self.anchor.y1 - self.rect_size.y*self.anchor.y2
+    if self.size_anchor != nil:
+      if self.size_anchor.x > 0.0:
+        self.rect_size.x = self.parent.rect_size.x * self.size_anchor.x
+      if self.size_anchor.y > 0.0:
+        self.rect_size.y = self.parent.rect_size.y * self.size_anchor.y
+    if self.anchor != nil:
+      self.position.x = self.parent.rect_size.x*self.anchor.x1 - self.rect_size.x*self.anchor.x2
+      self.position.y = self.parent.rect_size.y*self.anchor.y1 - self.rect_size.y*self.anchor.y2
 
 method draw*(self: ControlPtr, w, h: GLfloat) =
   {.warning[LockLevel]: off.}

+ 19 - 14
tests/test6.nim

@@ -9,27 +9,32 @@ var
   main = Scene("Main", mainobj)
 
   colorrectobj: ColorRectObj
-  colorrect = ColorRect(colorrectobj)
+  lightblue = ColorRect(colorrectobj)
 
   colorrect1obj: ColorRectObj
-  colorrect1 = ColorRect(colorrect1obj)
+  violet = ColorRect(colorrect1obj)
 
-main.addChild(colorrect)
-colorrect.addChild(colorrect1)
+main.addChild(lightblue)
+lightblue.addChild(violet)
 
 
-colorrect1.anchor = Anchor(  # Try to change it! ^^
-  0.5,  # parent anchor at X axis.
-  0.5,  # parent anchor at Y axis.
-  0.5,  # anchor at X axis.
-  0.5   # anchor at Y axis.
+lightblue.resize(256, 128)
+lightblue.move(128, 64)
+
+violet.anchor = Anchor(  # Try to change it! ^^
+  0.5,  # parent anchor at X-axis.
+  0.5,  # parent anchor at Y-axis.
+  0.5,  # anchor at X-axis.
+  0.5   # anchor at Y-axis.
+)
+lightblue.anchor = Anchor(1, 1, 1, 1)
+lightblue.size_anchor = Vector2(
+  0,  # size anchor at X-axis. If 0 then not used.
+  1   # size anchor at Y-axis. If 0 then not used.
 )
-colorrect.anchor = Anchor(1, 1, 1, 1)
 
-colorrect.resize(256, 128)
-colorrect.move(128, 64)
-colorrect.color = Color(0xaaccffff'u32)
-colorrect1.color = Color(0xccaaffff'u32)
+lightblue.color = Color(0xaaccffff'u32)
+violet.color = Color(0xccaaffff'u32)
 
 
 addScene(main)