Skip to content

Commit df925ca

Browse files
committed
fix: added missing implementations in EmbeddedPaneWindow
1 parent 16c6721 commit df925ca

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

fxgl/src/main/kotlin/com/almasb/fxgl/app/MainWindow.kt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.almasb.fxgl.logging.Logger
1515
import com.almasb.fxgl.scene.CSS
1616
import com.almasb.fxgl.scene.Scene
1717
import com.almasb.fxgl.scene.SubScene
18+
import com.almasb.fxgl.texture.toImage
1819
import javafx.beans.binding.Bindings
1920
import javafx.beans.property.*
2021
import javafx.event.Event
@@ -64,6 +65,7 @@ sealed class MainWindow(
6465
var onClose: (() -> Unit)? = null
6566
var defaultCursor: ImageCursor? = null
6667

68+
abstract val stage: Stage
6769
abstract val x: Double
6870
abstract val y: Double
6971
abstract val width: Double
@@ -104,7 +106,11 @@ sealed class MainWindow(
104106
* and most suitable will be chosen.
105107
* Can only be called before [show].
106108
*/
107-
abstract fun addIcons(vararg images: Image)
109+
fun addIcons(vararg images: Image) {
110+
if (!settings.isNative) {
111+
stage.icons += images
112+
}
113+
}
108114

109115
abstract fun addCSS(vararg cssList: CSS)
110116

@@ -273,7 +279,7 @@ internal class PrimaryStageWindow(
273279
/**
274280
* Primary stage.
275281
*/
276-
val stage: Stage,
282+
override val stage: Stage,
277283
scene: FXGLScene,
278284
settings: ReadOnlyGameSettings
279285

@@ -472,12 +478,6 @@ internal class PrimaryStageWindow(
472478
stage.close()
473479
}
474480

475-
override fun addIcons(vararg images: Image) {
476-
if (!settings.isNative) {
477-
stage.icons += images
478-
}
479-
}
480-
481481
override fun addCSS(vararg cssList: CSS) {
482482
fxScene.stylesheets += cssList.map { it.externalForm }
483483
}
@@ -519,6 +519,9 @@ internal class EmbeddedPaneWindow(
519519
private val backgroundRect = Rectangle()
520520
private val clipRect = Rectangle()
521521

522+
override val stage: Stage
523+
get() = fxglPane?.scene?.window as Stage? ?: Stage().also { log.warning("EmbeddedPane not attached to Stage") }
524+
522525
override val x: Double
523526
get() = fxglPane.localToScene(0.0, 0.0).x
524527

@@ -635,9 +638,6 @@ internal class EmbeddedPaneWindow(
635638
return ReadOnlyBooleanWrapper().readOnlyProperty
636639
}
637640

638-
override fun addIcons(vararg images: Image) {
639-
}
640-
641641
override fun addCSS(vararg cssList: CSS) {
642642
fxglPane.stylesheets += cssList.map { it.externalForm }
643643
}
@@ -683,8 +683,6 @@ internal class EmbeddedPaneWindow(
683683
log.debug("Window border size: ($windowBorderWidth, $windowBorderHeight)")
684684
log.debug("Scaled size: ${scaledWidth.value} x ${scaledHeight.value}")
685685
log.debug("Scaled ratio: (${scaleRatioX.value}, ${scaleRatioY.value})")
686-
//log.debug("Scene size: ${stage.scene.width} x ${stage.scene.height}")
687-
//log.debug("Stage size: ${stage.width} x ${stage.height}")
688686
}
689687

690688
/**
@@ -705,7 +703,7 @@ internal class EmbeddedPaneWindow(
705703
}
706704

707705
override fun takeScreenshot(): Image {
708-
return WritableImage(1, 1)
706+
return toImage(fxglPane)
709707
}
710708

711709
override fun close() {

fxgl/src/main/kotlin/com/almasb/fxgl/dsl/FXGL.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import com.almasb.fxgl.input.UserAction
3636
import com.almasb.fxgl.input.virtual.VirtualButton
3737
import com.almasb.fxgl.io.FileSystemService
3838
import com.almasb.fxgl.localization.LocalizationService
39-
import com.almasb.fxgl.logging.Logger
4039
import com.almasb.fxgl.minigames.MiniGameService
4140
import com.almasb.fxgl.net.NetService
4241
import com.almasb.fxgl.notification.NotificationService
@@ -72,7 +71,6 @@ import javafx.stage.Stage
7271
import javafx.util.Duration
7372
import java.net.URL
7473
import java.util.*
75-
import java.util.concurrent.Callable
7674
import java.util.function.BiConsumer
7775
import java.util.function.Consumer
7876

@@ -190,12 +188,7 @@ class FXGL private constructor() { companion object {
190188
@JvmStatic fun getPrimaryStage(): Stage {
191189
val window = engine.getService(FXGLApplication.GameApplicationService::class.java).window
192190

193-
if (window is PrimaryStageWindow)
194-
return window.stage
195-
196-
Logger.get("FXGL").warning("Started via embeddedLaunch(). getPrimaryStage() returning dummy stage.")
197-
198-
return getExecutor().startAsyncFX(Callable { Stage() }).await()
191+
return window.stage
199192
}
200193

201194
@JvmStatic fun <T : EngineService> getService(serviceClass: Class<T>): T = engine.getService(serviceClass)

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import javafx.event.EventHandler
1717
import javafx.event.EventType
1818
import javafx.scene.Parent
1919
import javafx.scene.image.Image
20+
import javafx.scene.layout.StackPane
2021
import javafx.stage.Stage
2122
import org.hamcrest.CoreMatchers.`is`
2223
import org.hamcrest.CoreMatchers.not
@@ -37,22 +38,25 @@ class MainWindowTest {
3738

3839
companion object {
3940

40-
private lateinit var window: PrimaryStageWindow
41+
private lateinit var window: MainWindow
4142
private lateinit var stage: Stage
43+
private lateinit var fxglPane: FXGLPane
4244
private lateinit var scene: FXGLScene
4345

46+
private val settings = GameSettings()
47+
4448
private const val WIDTH = 600
4549
private const val HEIGHT = 400
4650

4751
@BeforeAll
4852
@JvmStatic fun before() {
4953
Async.startAsyncFX {
50-
51-
val settings = GameSettings()
5254
settings.width = WIDTH
5355
settings.height = HEIGHT
5456

5557
stage = Stage()
58+
fxglPane = FXGLPane(WIDTH.toDouble(), HEIGHT.toDouble())
59+
5660
scene = object : FXGLScene(WIDTH, HEIGHT) {}
5761

5862
window = PrimaryStageWindow(stage, scene, settings.toReadOnly())
@@ -61,7 +65,7 @@ class MainWindowTest {
6165
}
6266

6367
@Test
64-
@EnabledIfEnvironmentVariable(named = "CI", matches = "true")
68+
//@EnabledIfEnvironmentVariable(named = "CI", matches = "true")
6569
fun runTests() {
6670
var count = 0
6771

@@ -79,6 +83,22 @@ class MainWindowTest {
7983
}.await()
8084

8185
assertThat(count, `is`(1))
86+
87+
Async.startAsyncFX {
88+
window = EmbeddedPaneWindow(fxglPane, scene, settings.toReadOnly())
89+
stage.scene = javafx.scene.Scene(StackPane(fxglPane))
90+
stage.icons.clear()
91+
92+
`Add icon`()
93+
`Show Window`()
94+
`Set scene`()
95+
`Take screenshot`()
96+
`Push and pop subscene`()
97+
98+
count++
99+
}.await()
100+
101+
assertThat(count, `is`(2))
82102
}
83103

84104
fun `Add icon`() {
@@ -103,13 +123,17 @@ class MainWindowTest {
103123
fun `Show Window`() {
104124
window.show()
105125

106-
assertThat(window.stage, `is`(stage))
126+
if (window is PrimaryStageWindow) {
127+
assertThat((window as PrimaryStageWindow).stage, `is`(stage))
128+
assertThat(stage.scene.root, `is`<Parent>(scene.root))
129+
} else {
130+
assertThat((window as EmbeddedPaneWindow).fxglPane, `is`(fxglPane))
131+
assertThat(stage.scene, `is`(fxglPane.scene))
132+
}
107133

108134
assertTrue(stage.isShowing, "Window is not showing")
109135
assertTrue(stage.width >= WIDTH, "Window is not at least $WIDTH wide")
110136
assertTrue(stage.height >= HEIGHT, "Window is not at least $HEIGHT high")
111-
112-
assertThat(stage.scene.root, `is`<Parent>(scene.root))
113137
}
114138

115139
/**
@@ -151,8 +175,6 @@ class MainWindowTest {
151175
val filterConsume = EventHandler<Event> {
152176
count -= 5
153177
it.consume()
154-
155-
println(it.isConsumed)
156178
}
157179

158180
scene.input.addEventFilter(EventType.ROOT, filterConsume)
@@ -168,7 +190,7 @@ class MainWindowTest {
168190

169191
window.setScene(scene2)
170192

171-
assertThat(stage.scene.root, `is`<Parent>(scene2.root))
193+
assertThat(stage.scene, `is`(scene2.root.scene))
172194
}
173195

174196
fun `Take screenshot`() {

0 commit comments

Comments
 (0)