Skip to content

Commit

Permalink
added vector property support
Browse files Browse the repository at this point in the history
  • Loading branch information
reinder committed May 20, 2024
1 parent 59a35e8 commit 7a3d265
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
20 changes: 18 additions & 2 deletions app/src/main/java/org/traintastic/app/network/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -280,6 +279,23 @@ object Connection {
}
}

private fun readValues(message: Message, valueType: ValueType): Array<Any> {
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<Any>()
}
}
}

private var onWorldChangedHandle = 0u

fun addOnWorldChanged(callback: (()->Unit)): UInt {
Expand Down
44 changes: 44 additions & 0 deletions app/src/main/java/org/traintastic/app/network/VectorProperty.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.traintastic.app.network

class VectorProperty(obj: Object, name: String, val type: ValueType, val flags: Set<PropertyFlags>, private var values: Array<Any>, val enumOrSetName: String = "") : InterfaceItem(obj, name) {

//private val onChanged: HashMap<UInt, ((property :VectorProperty)->Unit)> = 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<Any>) {
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
}
}*/
}

0 comments on commit 7a3d265

Please sign in to comment.