Skip to content

Commit

Permalink
fix: UI loaded from fxml file now correctly loads Image tags, closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Feb 19, 2024
1 parent 2acffc4 commit 6ce3783
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ private class UIAssetLoader : AssetLoader<UI>(
val controller = (params as UIParams).controller

params.url.openStream().use {
val loader = FXMLLoader()
val loader = FXMLLoader(params.url)
loader.setController(controller)
val root = loader.load<Parent>(it)
controller.init()
Expand Down
16 changes: 16 additions & 0 deletions fxgl/src/test/kotlin/com/almasb/fxgl/app/AssetLoaderServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.almasb.fxgl.test.InjectInTest
import com.almasb.fxgl.test.RunWithFX
import com.almasb.fxgl.texture.getDummyImage
import com.almasb.fxgl.ui.UIController
import javafx.scene.image.ImageView
import org.hamcrest.CoreMatchers.*
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.*
Expand Down Expand Up @@ -321,6 +322,21 @@ class AssetLoaderServiceTest {
assertThat(ui.root, `is`(notNullValue()))
}

@Test
fun `Load UI with image element`() {
val controller = object : UIController {
override fun init() {
}
}

val ui = assetLoader.loadUI("fxml/test_ui_with_image.fxml", controller)

val imageView = ui.root.lookup("#imageView") as ImageView

assertThat(imageView, `is`(notNullValue()))
assertThat(imageView.image, `is`(notNullValue()))
}

@Test
fun loadCSS() {
var css = assetLoader.loadCSS("test.css")
Expand Down
Binary file added fxgl/src/test/resources/fxglassets/ui/fxml/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions fxgl/src/test/resources/fxglassets/ui/fxml/test_ui_with_image.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>

<Pane xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
<ImageView id="imageView">
<Image url="@test.png"/>
</ImageView>
</Pane>

0 comments on commit 6ce3783

Please sign in to comment.