Prechádzať zdrojové kódy

fix 2d nodes :eyes:

Ethosa 3 rokov pred
rodič
commit
7debf015ea

+ 2 - 1
README.md

@@ -65,7 +65,7 @@ This section contains links to documentation for all nodes.
 |[AudioStream][astrm]|                          |[GridBox][GridBox]             |                            |                             |
 |[Animation][anim]   |                          |[Scroll][Scroll]               |                            |                             |
 |[Vector3][Vector3]  |                          |[ProgressBar][ProgressBar]     |                            |                             |
-|                    |                          |[Slider][Slider]               |                            |                             |
+|[SceneBuilder][SBld]|                          |[Slider][Slider]               |                            |                             |
 |                    |                          |[VProgressBar][VProgressBar]   |                            |                             |
 |                    |                          |[VSlider][VSlider]             |                            |                             |
 |                    |                          |[Popup][Popup]                 |                            |                             |
@@ -131,6 +131,7 @@ Use the [`Nim compiler user guide`](https://nim-lang.org/docs/nimc.html#dynlibov
 [astrm]:https://ethosa.github.io/nodesnim/nodesnim/core/audio_stream.html
 [anim]:https://ethosa.github.io/nodesnim/nodesnim/core/animation.html
 [Vector3]:https://ethosa.github.io/nodesnim/nodesnim/core/vector3.html
+[SBld]:https://ethosa.github.io/nodesnim/nodesnim/core/scene_builder.html
 
 [Node]:https://ethosa.github.io/nodesnim/nodesnim/nodes/node.html
 [Canvas]:https://ethosa.github.io/nodesnim/nodesnim/nodes/canvas.html

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

@@ -48,6 +48,7 @@ method drawScene*(scene: SceneRef, w, h: GLfloat, paused: bool) {.base.} =
       continue
     if child.visible:
       child.draw2stage(w, h)
+  scene.CanvasRef.calcGlobalPosition()
 
 method duplicate*(self: SceneRef): SceneRef {.base.} =
   ## Duplicates Scene object and create a new Scene.

+ 1 - 6
src/nodesnim/nodes2d/camera2d.nim

@@ -63,12 +63,7 @@ method disableSmooth*(self: Camera2DRef) {.base.} =
 method draw*(self: Camera2DRef, w, h: GLfloat) =
   ## this method uses in the `window.nim`.
   {.warning[LockLevel]: off.}
-  self.position = self.timed_position
-
-  if self.centered:
-    self.CanvasRef.position = self.timed_position - self.rect_size*2
-  else:
-    self.CanvasRef.position = self.timed_position
+  procCall self.Node2DRef.draw(w, h)
 
   if self.target != nil and self.current:
     var root = self.getRootNode().CanvasRef

+ 2 - 6
src/nodesnim/nodes2d/collision_shape2d.nim

@@ -51,6 +51,7 @@ proc CollisionShape2D*(name: string = "CollisionShape2D"): CollisionShape2DRef =
   result.y1 = 0
   result.radius = 20
   result.polygon = @[]
+  result.centered = false
   result.kind = COLLISION_SHAPE_2D_NODE
 
 
@@ -89,12 +90,7 @@ method setShapeTypePolygon*(self: CollisionShape2DRef, positions: varargs[Vector
 method draw*(self: CollisionShape2DRef, w, h: GLfloat) =
   ## this method uses in the `window.nim`.
   {.warning[LockLevel]: off.}
-  # Recalculate position.
-  self.position = self.timed_position
-  if self.centered:
-    self.position = self.timed_position - self.rect_size/2
-  else:
-    self.position = self.timed_position
+  procCall self.Node2DRef.draw(w, h)
 
   # debug draw
   when defined(debug):

+ 1 - 6
src/nodesnim/nodes2d/kinematic_body2d.nim

@@ -64,12 +64,7 @@ method getCollideCount*(self: KinematicBody2DRef): int {.base.} =
 method draw*(self: KinematicBody2DRef, w, h: GLfloat) =
   ## this method uses in the `window.nim`.
   {.warning[LockLevel]: off.}
-  self.position = self.timed_position
-
-  if self.centered:
-    self.position = self.timed_position - self.rect_size*2
-  else:
-    self.position = self.timed_position
+  procCall self.Node2DRef.draw(w, h)
 
 
 method duplicate*(self: KinematicBody2DRef): KinematicBody2DRef {.base.} =

+ 6 - 5
src/nodesnim/nodes2d/node2d.nim

@@ -23,8 +23,11 @@ type
 
 
 template node2dpattern*: untyped =
-  result.centered = true
+  result.centered = false
   result.timed_position = Vector2()
+  result.rect_size = Vector2()
+  result.position = Vector2()
+  result.global_position = Vector2()
   result.type_of_node = NODE_TYPE_2D
 
 proc Node2D*(name: string = "Node2D"): Node2DRef =
@@ -45,11 +48,9 @@ method draw*(self: Node2DRef, w, h: GLfloat) =
   ## this method uses in the `window.nim`.
   {.warning[LockLevel]: off.}
   self.position = self.timed_position
-
+  self.calcGlobalPosition()
   if self.centered:
-    self.position = self.timed_position - self.rect_size*2
-  else:
-    self.position = self.timed_position
+    self.position -= self.rect_size/2
 
 
 method move*(self: Node2DRef, x, y: float) =

+ 6 - 5
src/nodesnim/nodes2d/sprite.nim

@@ -12,6 +12,7 @@ import
   ../core/color,
 
   ../nodes/node,
+  ../nodes/canvas,
   node2d
 
 
@@ -35,6 +36,7 @@ proc Sprite*(name: string = "Sprite"): SpriteRef =
   result.texture = GlTextureObj()
   result.filter = Color(1f, 1f, 1f)
   result.kind = SPRITE_NODE
+  result.centered = true
 
 
 method draw*(self: SpriteRef, w, h: GLfloat) =
@@ -44,11 +46,8 @@ method draw*(self: SpriteRef, w, h: GLfloat) =
     self.rect_size = self.texture.size
 
   # Recalculate position.
-  self.position = self.timed_position
-  if self.centered:
-    self.position = self.timed_position - self.rect_size/2
-  else:
-    self.position = self.timed_position
+  procCall self.Node2DRef.draw(w, h)
+  self.CanvasRef.calcGlobalPosition()
 
   let
     x = -w/2 + self.global_position.x
@@ -106,6 +105,7 @@ method loadTexture*(self: SpriteRef, file: cstring, mode = GL_RGB) {.base.} =
   ## - `file` is a texture path.
   ## - `mode` is a GLenum. can be GL_RGB or GL_RGBA.
   self.texture = load(file, mode)
+  self.rect_size = self.texture.size
 
 method setTexture*(self: SpriteRef, texture: GlTextureObj) {.base.} =
   ## Loads a new texture from file.
@@ -113,3 +113,4 @@ method setTexture*(self: SpriteRef, texture: GlTextureObj) {.base.} =
   ## Arguments:
   ## - `texture` is a GlTexture object.
   self.texture = texture
+  self.rect_size = self.texture.size

+ 2 - 0
src/nodesnim/window.nim

@@ -209,6 +209,8 @@ proc addScene*(scene: SceneRef) =
   ## - `scene` - pointer to the Scene object.
   if scene notin scenes:
     scenes.add(scene)
+  scene.rect_size.x = width.float
+  scene.rect_size.y = height.float
 
 proc addMainScene*(scene: SceneRef) =
   ## Adds a new scene in the app and set it mark it as main scene.

+ 0 - 2
tests/test37.nim

@@ -24,7 +24,6 @@ sprite1.setTexture(img1)
 body.addChild(sprite)
 body.addChild(camera)
 
-sprite1.move(0, 400)
 camera.setTarget(body)
 camera.setLimit(-600, -400, 600, 400)
 camera.setCurrent()
@@ -44,7 +43,6 @@ body.on_process =
       if distance >= 5:
         body.moveAndCollide(direction*speed)
 
-
 main.addChild(body)
 main.addChild(sprite1)
 addScene(main)