From 7a3d265dbd3582fcc54e2dcdad55437fcbb832fb Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Mon, 20 May 2024 23:44:50 +0200 Subject: [PATCH] added vector property support --- .../org/traintastic/app/network/Connection.kt | 20 ++++++++- .../traintastic/app/network/VectorProperty.kt | 44 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/traintastic/app/network/VectorProperty.kt diff --git a/app/src/main/java/org/traintastic/app/network/Connection.kt b/app/src/main/java/org/traintastic/app/network/Connection.kt index 4c993ab..589a1f2 100644 --- a/app/src/main/java/org/traintastic/app/network/Connection.kt +++ b/app/src/main/java/org/traintastic/app/network/Connection.kt @@ -165,8 +165,7 @@ object Connection { val enumOrSetName = if (valueType == ValueType.Enum || valueType == ValueType.Set) message.readString() else "" if (type == InterfaceItemType.VectorProperty) { - //val length = message.readInt32() // read UInt as Int, Kotlin uses Int for length - assert(false) // TODO: implement VectorProperty + item = VectorProperty(obj, name, valueType, flags, readValues(message, valueType), enumOrSetName) } else { assert(type == InterfaceItemType.Property || type == InterfaceItemType.UnitProperty) @@ -280,6 +279,23 @@ object Connection { } } + private fun readValues(message: Message, valueType: ValueType): Array { + val length = message.readInt32() // read UInt as Int, Kotlin uses Int for length + return when (valueType) { + ValueType.Boolean -> Array(length) { i -> message.readBool() } + ValueType.Enum, + ValueType.Integer, + ValueType.Set -> Array(length) { i -> message.readInt64() } + ValueType.Float -> Array(length) { i -> message.readDouble() } + ValueType.String, + ValueType.Object -> Array(length) { i -> message.readString() } + else -> { + assert(false) + emptyArray() + } + } + } + private var onWorldChangedHandle = 0u fun addOnWorldChanged(callback: (()->Unit)): UInt { diff --git a/app/src/main/java/org/traintastic/app/network/VectorProperty.kt b/app/src/main/java/org/traintastic/app/network/VectorProperty.kt new file mode 100644 index 0000000..7a75e68 --- /dev/null +++ b/app/src/main/java/org/traintastic/app/network/VectorProperty.kt @@ -0,0 +1,44 @@ +package org.traintastic.app.network + +class VectorProperty(obj: Object, name: String, val type: ValueType, val flags: Set, private var values: Array, val enumOrSetName: String = "") : InterfaceItem(obj, name) { + + //private val onChanged: HashMapUnit)> = HashMap() + + fun getBool(index: Int) : Boolean { + assert(type == ValueType.Boolean) + return if (values[index] is Boolean) values[index] as Boolean else false + } + + fun getInt(index: Int) : Int { + assert(getLong(index) >= Int.MIN_VALUE && getLong(index) <= Int.MAX_VALUE) + return getLong(index).toInt() + } + + fun getLong(index: Int) : Long { + assert(type == ValueType.Integer) + return if (values[index] is Long) values[index] as Long else 0 + } + + fun getDouble(index: Int) : Double { + assert(type == ValueType.Float) + return if (values[index] is Double) values[index] as Double else Double.NaN + } + + fun getString(index: Int): String { + assert(type == ValueType.String) + return if (values[index] is String) values[index] as String else "" + } + + internal fun updateValue(newValues: Array) { + values = newValues + //onChanged.forEach { (_, callback) -> callback.invoke(this) } + } + + /*companion object { + private var onChangedHandle: UInt = 0u + + private fun getHandle(): UInt { + return if (onChangedHandle < UInt.MAX_VALUE) ++onChangedHandle else 1u + } + }*/ +} \ No newline at end of file