Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property map serializable type#1250 #1340

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package com.almasb.fxgl.core.collection
import javafx.beans.property.*
import javafx.beans.value.ChangeListener
import javafx.beans.value.ObservableValue
import com.almasb.fxgl.core.serialization.SerializableType
import com.almasb.fxgl.core.serialization.Bundle
import java.util.*


Expand All @@ -23,10 +25,11 @@ import java.util.*
* SimpleObjectProperty.
*
* Null values are not allowed.
* Object Properties are not supported for Serialization.
*
* @author Almas Baimagambetov ([email protected])
*/
class PropertyMap {
class PropertyMap : SerializableType {

companion object {
@JvmStatic fun fromStringMap(map: Map<String, String>): PropertyMap {
Expand Down Expand Up @@ -309,6 +312,20 @@ class PropertyMap {
}
}

override fun write(bundle: Bundle) {
// Convert to string map
this.toStringMap().forEach { (key, value) ->
// write to bundle
bundle.put(key, value)
}
}

override fun read(bundle: Bundle) {
bundle.data.forEach { (key, value) ->
this.setValue(key, toValue(value.toString()))
}
}

override fun toString(): String {
return properties.toMap().toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.almasb.fxgl.core.collection

import com.almasb.fxgl.core.math.Vec2
import com.almasb.fxgl.core.serialization.Bundle
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.StringProperty
import org.hamcrest.MatcherAssert.assertThat
Expand Down Expand Up @@ -412,6 +413,33 @@ class PropertyMapTest {
assertThat(map2.getBoolean("key5"), `is`(true))
}

@Test
fun `Serialize Property Map`() {
// Set test values
map.setValue("key1", "ABC")
map.setValue("key2", 100)
map.setValue("key3", 10.0)
map.setValue("key4", true)

// Create a bundle
val testBundle = Bundle("propertyMap")

// Write to bundle
map.write(testBundle)

// Create a new PropertyMap to test
val map2 = PropertyMap()

// Read from bundle
map2.read(testBundle)

// Check test values
assertThat(map2.getString("key1"), `is`("ABC"))
assertThat(map2.getInt("key2"), `is`(100))
assertThat(map2.getDouble("key3"), `is`(10.0))
assertThat(map2.getBoolean("key4"), `is`(true))
}

private class MyClass(val i: Int)


Expand Down
Loading