Skip to content

Commit

Permalink
fix: added missing implementations in EmbeddedPaneWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Apr 11, 2023
1 parent 16c6721 commit df925ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
26 changes: 12 additions & 14 deletions fxgl/src/main/kotlin/com/almasb/fxgl/app/MainWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.almasb.fxgl.logging.Logger
import com.almasb.fxgl.scene.CSS
import com.almasb.fxgl.scene.Scene
import com.almasb.fxgl.scene.SubScene
import com.almasb.fxgl.texture.toImage
import javafx.beans.binding.Bindings
import javafx.beans.property.*
import javafx.event.Event
Expand Down Expand Up @@ -64,6 +65,7 @@ sealed class MainWindow(
var onClose: (() -> Unit)? = null
var defaultCursor: ImageCursor? = null

abstract val stage: Stage
abstract val x: Double
abstract val y: Double
abstract val width: Double
Expand Down Expand Up @@ -104,7 +106,11 @@ sealed class MainWindow(
* and most suitable will be chosen.
* Can only be called before [show].
*/
abstract fun addIcons(vararg images: Image)
fun addIcons(vararg images: Image) {
if (!settings.isNative) {
stage.icons += images
}
}

abstract fun addCSS(vararg cssList: CSS)

Expand Down Expand Up @@ -273,7 +279,7 @@ internal class PrimaryStageWindow(
/**
* Primary stage.
*/
val stage: Stage,
override val stage: Stage,
scene: FXGLScene,
settings: ReadOnlyGameSettings

Expand Down Expand Up @@ -472,12 +478,6 @@ internal class PrimaryStageWindow(
stage.close()
}

override fun addIcons(vararg images: Image) {
if (!settings.isNative) {
stage.icons += images
}
}

override fun addCSS(vararg cssList: CSS) {
fxScene.stylesheets += cssList.map { it.externalForm }
}
Expand Down Expand Up @@ -519,6 +519,9 @@ internal class EmbeddedPaneWindow(
private val backgroundRect = Rectangle()
private val clipRect = Rectangle()

override val stage: Stage
get() = fxglPane?.scene?.window as Stage? ?: Stage().also { log.warning("EmbeddedPane not attached to Stage") }

override val x: Double
get() = fxglPane.localToScene(0.0, 0.0).x

Expand Down Expand Up @@ -635,9 +638,6 @@ internal class EmbeddedPaneWindow(
return ReadOnlyBooleanWrapper().readOnlyProperty
}

override fun addIcons(vararg images: Image) {
}

override fun addCSS(vararg cssList: CSS) {
fxglPane.stylesheets += cssList.map { it.externalForm }
}
Expand Down Expand Up @@ -683,8 +683,6 @@ internal class EmbeddedPaneWindow(
log.debug("Window border size: ($windowBorderWidth, $windowBorderHeight)")
log.debug("Scaled size: ${scaledWidth.value} x ${scaledHeight.value}")
log.debug("Scaled ratio: (${scaleRatioX.value}, ${scaleRatioY.value})")
//log.debug("Scene size: ${stage.scene.width} x ${stage.scene.height}")
//log.debug("Stage size: ${stage.width} x ${stage.height}")
}

/**
Expand All @@ -705,7 +703,7 @@ internal class EmbeddedPaneWindow(
}

override fun takeScreenshot(): Image {
return WritableImage(1, 1)
return toImage(fxglPane)
}

override fun close() {
Expand Down
9 changes: 1 addition & 8 deletions fxgl/src/main/kotlin/com/almasb/fxgl/dsl/FXGL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import com.almasb.fxgl.input.UserAction
import com.almasb.fxgl.input.virtual.VirtualButton
import com.almasb.fxgl.io.FileSystemService
import com.almasb.fxgl.localization.LocalizationService
import com.almasb.fxgl.logging.Logger
import com.almasb.fxgl.minigames.MiniGameService
import com.almasb.fxgl.net.NetService
import com.almasb.fxgl.notification.NotificationService
Expand Down Expand Up @@ -72,7 +71,6 @@ import javafx.stage.Stage
import javafx.util.Duration
import java.net.URL
import java.util.*
import java.util.concurrent.Callable
import java.util.function.BiConsumer
import java.util.function.Consumer

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

if (window is PrimaryStageWindow)
return window.stage

Logger.get("FXGL").warning("Started via embeddedLaunch(). getPrimaryStage() returning dummy stage.")

return getExecutor().startAsyncFX(Callable { Stage() }).await()
return window.stage
}

@JvmStatic fun <T : EngineService> getService(serviceClass: Class<T>): T = engine.getService(serviceClass)
Expand Down
42 changes: 32 additions & 10 deletions fxgl/src/test/kotlin/com/almasb/fxgl/app/MainWindowTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import javafx.event.EventHandler
import javafx.event.EventType
import javafx.scene.Parent
import javafx.scene.image.Image
import javafx.scene.layout.StackPane
import javafx.stage.Stage
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.not
Expand All @@ -37,22 +38,25 @@ class MainWindowTest {

companion object {

private lateinit var window: PrimaryStageWindow
private lateinit var window: MainWindow
private lateinit var stage: Stage
private lateinit var fxglPane: FXGLPane
private lateinit var scene: FXGLScene

private val settings = GameSettings()

private const val WIDTH = 600
private const val HEIGHT = 400

@BeforeAll
@JvmStatic fun before() {
Async.startAsyncFX {

val settings = GameSettings()
settings.width = WIDTH
settings.height = HEIGHT

stage = Stage()
fxglPane = FXGLPane(WIDTH.toDouble(), HEIGHT.toDouble())

scene = object : FXGLScene(WIDTH, HEIGHT) {}

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

@Test
@EnabledIfEnvironmentVariable(named = "CI", matches = "true")
//@EnabledIfEnvironmentVariable(named = "CI", matches = "true")
fun runTests() {
var count = 0

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

assertThat(count, `is`(1))

Async.startAsyncFX {
window = EmbeddedPaneWindow(fxglPane, scene, settings.toReadOnly())
stage.scene = javafx.scene.Scene(StackPane(fxglPane))
stage.icons.clear()

`Add icon`()
`Show Window`()
`Set scene`()
`Take screenshot`()
`Push and pop subscene`()

count++
}.await()

assertThat(count, `is`(2))
}

fun `Add icon`() {
Expand All @@ -103,13 +123,17 @@ class MainWindowTest {
fun `Show Window`() {
window.show()

assertThat(window.stage, `is`(stage))
if (window is PrimaryStageWindow) {
assertThat((window as PrimaryStageWindow).stage, `is`(stage))
assertThat(stage.scene.root, `is`<Parent>(scene.root))
} else {
assertThat((window as EmbeddedPaneWindow).fxglPane, `is`(fxglPane))
assertThat(stage.scene, `is`(fxglPane.scene))
}

assertTrue(stage.isShowing, "Window is not showing")
assertTrue(stage.width >= WIDTH, "Window is not at least $WIDTH wide")
assertTrue(stage.height >= HEIGHT, "Window is not at least $HEIGHT high")

assertThat(stage.scene.root, `is`<Parent>(scene.root))
}

/**
Expand Down Expand Up @@ -151,8 +175,6 @@ class MainWindowTest {
val filterConsume = EventHandler<Event> {
count -= 5
it.consume()

println(it.isConsumed)
}

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

window.setScene(scene2)

assertThat(stage.scene.root, `is`<Parent>(scene2.root))
assertThat(stage.scene, `is`(scene2.root.scene))
}

fun `Take screenshot`() {
Expand Down

0 comments on commit df925ca

Please sign in to comment.