Skip to content

Commit

Permalink
Moved SceneView childNodes to Set and added multiple nodes adding/r…
Browse files Browse the repository at this point in the history
…emoving
  • Loading branch information
ThomasGorisse committed Nov 27, 2023
1 parent 7b6a6cd commit 1804219
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ fun ARScene(
}
},
update = { sceneView ->
sceneView.childNodes = childNodes
sceneView.childNodes = childNodes.toSet()
sceneView.scene = scene
sceneView.setCameraNode(cameraNode)
sceneView.mainLightNode = mainLightNode
Expand Down
4 changes: 2 additions & 2 deletions sceneview/src/main/java/io/github/sceneview/Scene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun Scene(
/**
* List of the scene's nodes that can be linked to a `mutableStateOf<List<Node>>()`
*/
childNodes: List<Node> = listOf(),
childNodes: List<Node> = rememberNodes(),
/**
* Provide your own instance if you want to share Filament resources between multiple views.
*/
Expand Down Expand Up @@ -191,7 +191,7 @@ fun Scene(
}
},
update = { sceneView ->
sceneView.childNodes = childNodes
sceneView.childNodes = childNodes.toSet()
sceneView.scene = scene
sceneView.setCameraNode(cameraNode)
sceneView.mainLightNode = mainLightNode
Expand Down
42 changes: 36 additions & 6 deletions sceneview/src/main/java/io/github/sceneview/SceneView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,15 @@ open class SceneView @JvmOverloads constructor(
skybox = value?.skybox
}

var childNodes = listOf<Node>()
@Suppress("ConvertArgumentToSet")
var childNodes = setOf<Node>()
set(value) {
(field - value).forEach {
removeNode(it)
}
(value - field).forEach {
addNode(it)
}
field = value.toList()
field = value
}

/**
Expand Down Expand Up @@ -459,7 +458,7 @@ open class SceneView @JvmOverloads constructor(
}

/**
* Add a node to the scene as a direct child.
* Add a node to the [Scene] as a direct child.
*
* If the node is already in the scene, no change is made.
*
Expand All @@ -472,16 +471,47 @@ open class SceneView @JvmOverloads constructor(
}

/**
* Removes a node from the children of this NodeParent.
* Add multiple nodes to the [Scene] as a direct child.
*
* If the nodes are already in the scene, no change is made.
*
* @param nodes the nodes to add as children
* @throws IllegalArgumentException if the child is the same object as the parent, or if the
* parent is a descendant of the child
*/
fun addChildNodes(nodes: List<Node>) {
childNodes = childNodes + nodes
}

/**
* Removes a node from the children of this [Scene].
*
* If the node is not in the scene, no change is made.
*
* @param child the node to remove from the children
* @param node the node to remove from the children
*/
fun removeChildNode(node: Node) {
childNodes = childNodes - node
}

/**
* Removes multiple nodes from the children of this [Scene].
*
* If the nodes are not in the scene, no change is made.
*
* @param nodes the nodes to remove from the children
*/
fun removeChildNodes(nodes: List<Node>) {
childNodes = childNodes - nodes
}

/**
* Removes all nodes from the children of this [Scene].
*/
fun clearChildNodes() {
childNodes = setOf()
}

/**
* Set the background to transparent.
*/
Expand Down

0 comments on commit 1804219

Please sign in to comment.