Skip to content

Commit

Permalink
feat: Add SerializableType to PropertyMap, closes #1250
Browse files Browse the repository at this point in the history
  • Loading branch information
BeckFW committed Feb 12, 2024
1 parent ed749b8 commit 1b20694
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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 (almaslvl@gmail.com)
*/
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) ->
this.setValue(key, toValue(bundle.get(key)));
}
}

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,8 @@
package com.almasb.fxgl.core.collection

import com.almasb.fxgl.core.math.Vec2
import com.almasb.fxgl.core.serialization.Bundle
import javafx.beans.property.Property
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.StringProperty
import org.hamcrest.MatcherAssert.assertThat
Expand Down Expand Up @@ -412,6 +414,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

0 comments on commit 1b20694

Please sign in to comment.