Skip to content

Commit 3cdd7a3

Browse files
committed
feat: nodes that support View interface onUpdate() will now be updated by GameScene, even when added as a UI node
1 parent 7382f08 commit 3cdd7a3

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

fxgl/src/main/kotlin/com/almasb/fxgl/app/scene/GameScene.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package com.almasb.fxgl.app.scene
88

99
import com.almasb.fxgl.app.MainWindow
10+
import com.almasb.fxgl.core.View
1011
import com.almasb.fxgl.core.concurrent.Async
1112
import com.almasb.fxgl.core.math.FXGLMath
1213
import com.almasb.fxgl.dsl.FXGL.Companion.getAppHeight
@@ -81,6 +82,8 @@ internal constructor(width: Int, height: Int,
8182
val uiNodes: ObservableList<Node>
8283
get() = uiRoot.childrenUnmodifiable
8384

85+
private val updatableViews = arrayListOf<View>()
86+
8487
/**
8588
* If set to true, Game Scene will require calling step()
8689
* to advance each frame.
@@ -229,6 +232,9 @@ internal constructor(width: Int, height: Int,
229232
physicsWorld.onUpdate(tpf)
230233
viewport.onUpdate(tpf)
231234

235+
// update UI nodes
236+
updatableViews.forEach { it.onUpdate(tpf) }
237+
232238
if (!is3D && isZSortingNeeded) {
233239
sortZ()
234240
isZSortingNeeded = false
@@ -259,6 +265,9 @@ internal constructor(width: Int, height: Int,
259265
*/
260266
fun addUINode(node: Node) {
261267
uiRoot.children.add(node)
268+
269+
if (node is View)
270+
updatableViews += node
262271
}
263272

264273
/**
@@ -274,11 +283,13 @@ internal constructor(width: Int, height: Int,
274283
/**
275284
* Remove given node from the UI overlay.
276285
*
277-
* @param n node to remove
278-
* @return true iff the node has been removed
286+
* @param node node to remove
279287
*/
280-
fun removeUINode(n: Node): Boolean {
281-
return uiRoot.children.remove(n)
288+
fun removeUINode(node: Node) {
289+
uiRoot.children.remove(node)
290+
291+
if (node is View)
292+
updatableViews -= node
282293
}
283294

284295
/**
@@ -311,6 +322,7 @@ internal constructor(width: Int, height: Int,
311322
*/
312323
fun clearUINodes() {
313324
uiRoot.children.clear()
325+
updatableViews.clear()
314326
}
315327

316328
/**

fxgl/src/test/kotlin/com/almasb/fxgl/app/GameSceneTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package com.almasb.fxgl.app
88

99
import com.almasb.fxgl.app.scene.GameScene
1010
import com.almasb.fxgl.app.scene.GameView
11+
import com.almasb.fxgl.core.View
1112
import com.almasb.fxgl.entity.Entity
1213
import com.almasb.fxgl.entity.GameWorld
1314
import com.almasb.fxgl.particle.ParticleComponent
@@ -18,6 +19,7 @@ import com.almasb.fxgl.ui.UIController
1819
import javafx.scene.Group
1920
import javafx.scene.Node
2021
import javafx.scene.layout.Pane
22+
import javafx.scene.layout.Region
2123
import javafx.scene.shape.Rectangle
2224
import org.hamcrest.MatcherAssert.assertThat
2325
import org.hamcrest.Matchers.*
@@ -118,6 +120,19 @@ class GameSceneTest {
118120
assertTrue(gameScene.uiNodes.isEmpty())
119121
}
120122

123+
@Test
124+
fun `UI nodes are updated`() {
125+
val testView = TestView()
126+
127+
gameScene.addUINode(testView)
128+
129+
assertThat(testView.count, `is`(0.0))
130+
131+
gameScene.update(1.0)
132+
133+
assertThat(testView.count, `is`(1.0))
134+
}
135+
121136
@Test
122137
fun `Add and remove UI`() {
123138
val pane = Pane()
@@ -234,4 +249,19 @@ class GameSceneTest {
234249
assertThat(gameRoot.children[0], `is`(view1.node))
235250
assertThat(gameRoot.children[1], `is`(view2.node))
236251
}
252+
253+
private class TestView : Region(), View {
254+
var count = 0.0
255+
256+
override fun onUpdate(tpf: Double) {
257+
count = tpf
258+
}
259+
260+
override fun dispose() {
261+
}
262+
263+
override fun getNode(): Node {
264+
return this
265+
}
266+
}
237267
}

0 commit comments

Comments
 (0)