Skip to content

Commit

Permalink
Merge pull request #525 from baaahs/grid-swap-and-stagger
Browse files Browse the repository at this point in the history
Grid swap and stagger
  • Loading branch information
xian authored Feb 12, 2023
2 parents 3b71a77 + d89de6b commit 7c82d15
Show file tree
Hide file tree
Showing 35 changed files with 557 additions and 189 deletions.
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/baaahs/client/document/SceneManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import baaahs.io.resourcesFs
import baaahs.mapper.Storage
import baaahs.plugin.Plugins
import baaahs.scene.*
import baaahs.show.SceneMigrator
import baaahs.show.mutable.MutableDocument
import baaahs.ui.DialogHolder
import baaahs.ui.DialogMenuItem
Expand Down Expand Up @@ -88,7 +89,7 @@ class SceneManager(
}

override suspend fun onUpload(name: String, content: String) {
val scene = plugins.json.decodeFromString(Scene.serializer(), content)
val scene = plugins.json.decodeFromString(SceneMigrator, content)
onNew(scene)
}

Expand Down
5 changes: 3 additions & 2 deletions src/commonMain/kotlin/baaahs/mapper/Storage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import baaahs.libraries.ShaderLibraryIndexFile
import baaahs.plugin.Plugins
import baaahs.scene.OpenScene
import baaahs.scene.Scene
import baaahs.show.SceneMigrator
import baaahs.show.Show
import baaahs.show.ShowMigrator
import baaahs.sim.MergedFs
Expand Down Expand Up @@ -144,11 +145,11 @@ class Storage(val fs: Fs, val plugins: Plugins) {
}

suspend fun loadScene(file: Fs.File): Scene? {
return loadJson(file, Scene.serializer())
return loadJson(file, SceneMigrator)
}

suspend fun saveScene(file: Fs.File, scene: Scene) {
file.write(plugins.json.encodeToString(Scene.serializer(), scene), true)
file.write(plugins.json.encodeToString(SceneMigrator, scene), true)
}

suspend fun loadShow(file: Fs.File): Show? {
Expand Down
7 changes: 4 additions & 3 deletions src/commonMain/kotlin/baaahs/model/ModelData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ data class GridData(
val columns: Int,
val rowGap: Float,
val columnGap: Float,
val direction: Direction = Direction.ColumnsThenRows,
val zigZag: Boolean = false
val direction: Direction = Direction.RowsThenColumns,
val zigZag: Boolean = false,
val stagger: Int = 1
) : EntityData {
override fun edit(): MutableEntity = MutableGridData(this)

Expand All @@ -184,7 +185,7 @@ data class GridData(
override fun open(position: Vector3F, rotation: EulerAngle, scale: Vector3F) =
Grid(
title, description, position, rotation, scale,
rows, columns, rowGap, columnGap, direction, zigZag, id
rows, columns, rowGap, columnGap, direction, zigZag, stagger, id
)
}

Expand Down
20 changes: 12 additions & 8 deletions src/commonMain/kotlin/baaahs/model/PolyLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class Grid(
columnGap: Float,
direction: GridData.Direction,
zigZag: Boolean,
stagger: Int = 1,
id: EntityId = Model.Entity.nextId()
): PolyLine(
name, description,
calcSegments(rows, columns, rowGap, columnGap, direction, zigZag),
calcSegments(rows, columns, rowGap, columnGap, direction, zigZag, stagger),
position, rotation, scale, columnGap, rowGap, id) {
}

Expand All @@ -37,26 +38,29 @@ fun calcSegments(
rowGap: Float,
columnGap: Float,
direction: GridData.Direction,
zigZag: Boolean
zigZag: Boolean,
stagger: Int = 1
): List<PolyLine.Segment> {
return when (direction) {
GridData.Direction.ColumnsThenRows ->
GridData.Direction.RowsThenColumns ->
(0 until rows).map { yI ->
val staggerAmount = columnGap / stagger * (yI % stagger)
val y = yI * rowGap
PolyLine.Segment(
Vector3F(0f, y, 0f),
Vector3F((columns - 1) * columnGap, y, 0f),
Vector3F(0f + staggerAmount, y, 0f),
Vector3F((columns - 1) * columnGap + staggerAmount, y, 0f),
columns
).let {
if (zigZag && yI % 2 == 1) it.reverse() else it
}
}
GridData.Direction.RowsThenColumns ->
GridData.Direction.ColumnsThenRows ->
(0 until columns).map { xI ->
val staggerAmount = rowGap / stagger * (xI % stagger)
val x = xI * columnGap
PolyLine.Segment(
Vector3F(x, 0f, 0f),
Vector3F(x, (rows - 1) * rowGap, 0f),
Vector3F(x, 0f + staggerAmount, 0f),
Vector3F(x, (rows - 1) * rowGap + staggerAmount, 0f),
rows
).let {
if (zigZag && xI % 2 == 1) it.reverse() else it
Expand Down
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/baaahs/models/DemoModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ val demoModelData = ModelData(
EulerAngle(1.0, .5, .25),
Vector3F.unit3d,
Model.Entity.nextId(),
20, 25, 1f, 1f
20, 25, 1f, 1f,
GridData.Direction.RowsThenColumns
),
LightRingData(
"Light Ring", null,
Expand Down
4 changes: 3 additions & 1 deletion src/commonMain/kotlin/baaahs/models/Playa2021Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ val playa2021ModelData = ModelData(
EulerAngle.identity,
Vector3F.unit3d,
Model.Entity.nextId(),
7, 11, 1f, 1f, zigZag = true
7, 11, 1f, 1f,
direction = GridData.Direction.RowsThenColumns,
zigZag = true
)
),
ModelUnit.Inches
Expand Down
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/baaahs/scene/MutableScene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,10 @@ class MutableGridData(
var columnGap = baseGridData.columnGap
var direction = baseGridData.direction
var zigZag = baseGridData.zigZag
var stagger = baseGridData.stagger

override fun build(): EntityData =
GridData(title, description, position, rotation, scale, id, rows, columns, rowGap, columnGap, direction, zigZag)
GridData(title, description, position, rotation, scale, id, rows, columns, rowGap, columnGap, direction, zigZag, stagger)

override fun getEditorPanels() =
listOf(
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/baaahs/show/ShowMigrator.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package baaahs.show

import baaahs.scene.Scene
import baaahs.show.migration.AllSceneMigrations
import baaahs.show.migration.AllShowMigrations
import baaahs.show.migration.scene.AllSceneMigrations
import baaahs.show.migration.toJsonObj
import baaahs.util.Logger
import kotlinx.serialization.KSerializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ val AllShowMigrations: List<DataMigrator.Migration> = listOf(
V7_LegacyTabs,
V8_RenameShaderChannelToStream,
V9_RenameDataSourceToFeed
)

val AllSceneMigrations: List<DataMigrator.Migration> = listOf(
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package baaahs.show.migration
import baaahs.show.DataMigrator
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonPrimitive

@Suppress("ClassName")
object V1_UpdateDataSourceRefs : DataMigrator.Migration(1) {
Expand All @@ -25,7 +23,7 @@ object V1_UpdateDataSourceRefs : DataMigrator.Migration(1) {
override fun migrate(from: JsonObject): JsonObject {
return from.toMutableMap().apply {
mapObjsInDict("dataSources") { _, dataSource ->
val type = dataSource["type"]?.jsonPrimitive?.contentOrNull
val type = dataSource.type
if (type == "baaahs.plugin.CorePlugin.ModelInfoDataSource") {
dataSource.remove("structType")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object V4_FlattenGadgetControls : DataMigrator.Migration(4) {
override fun migrate(from: JsonObject): JsonObject {
return from.toMutableMap().apply {
mapObjsInDict("controls") { _, control ->
val type = control["type"]?.jsonPrimitive?.contentOrNull
val type = control.type
if (type == "baaahs.Core:Gadget") {
val gadgetData = control["gadget"]?.jsonObject
control.remove("gadget")!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package baaahs.show.migration
import baaahs.show.DataMigrator
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonPrimitive

@Suppress("ClassName")
object V5_FixFixtureInfoRefs : DataMigrator.Migration(5) {
Expand All @@ -15,7 +13,7 @@ object V5_FixFixtureInfoRefs : DataMigrator.Migration(5) {
override fun migrate(from: JsonObject): JsonObject {
return from.toMutableMap().apply {
mapObjsInDict("dataSources") { _, dataSource ->
val type = dataSource["type"]?.jsonPrimitive?.contentOrNull
val type = dataSource.type
if (type != null) {
dataSourceTypeMap[type]?.let { dataSource["type"] = JsonPrimitive(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object V8_RenameShaderChannelToStream : DataMigrator.Migration(8) {
patch.toMutableMap().apply {
replaceJsonObj("incomingLinks") { incomingLinks ->
JsonObject(incomingLinks.mapValues { (_, format) ->
if (format.jsonObject["type"]?.jsonPrimitive?.contentOrNull == "shader-channel") {
if (format.type == "shader-channel") {
format.jsonObject.edit {
put("type", JsonPrimitive("stream"))
put("stream", remove("shaderChannel")!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object V9_RenameDataSourceToFeed : DataMigrator.Migration(9) {
patch.jsonObject.edit {
replaceMapValues("incomingLinks") { _, incomingLink ->
incomingLink.jsonObject.edit {
if (this["type"]?.jsonPrimitive?.contentOrNull == "datasource") {
if (this.type == "datasource") {
put("type", JsonPrimitive("feed"))
rename("dataSourceId", "feedId")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package baaahs.show.migration.scene

import baaahs.show.DataMigrator

val AllSceneMigrations: List<DataMigrator.Migration> = listOf(
V1_GridDirectionBackwards
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package baaahs.show.migration.scene

import baaahs.show.DataMigrator
import baaahs.show.migration.*
import kotlinx.serialization.json.*

/**
* Grid directions were backwards.
*/
@Suppress("ClassName")
object V1_GridDirectionBackwards : DataMigrator.Migration(1) {
override fun migrate(from: JsonObject): JsonObject {
return from.edit {
replaceJsonObj("model") { model ->
model.jsonObject.edit {
mapObjsInArray("entities") { entity ->
entity.apply {
if (this.type == "Grid") {
// Grid's direction was backwards
val wrongDirection = this["direction"]?.jsonPrimitive?.contentOrNull
?: "ColumnsThenRows"
when(wrongDirection) {
"RowsThenColumns" -> this["direction"] = JsonPrimitive("ColumnsThenRows")
"ColumnsThenRows" -> this.remove("direction")
else -> error("Unknown direction \"$wrongDirection\".")
}

// Grid's zigzag might as well default to true.
val oldZigZag = this.remove("zigZag")?.jsonPrimitive?.booleanOrNull ?: false
if (!oldZigZag) this["zigZag"] = JsonPrimitive(false)
}
}
}
}
}
}
}
}
18 changes: 13 additions & 5 deletions src/commonMain/kotlin/baaahs/show/migration/util.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package baaahs.show.migration

import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.*

fun MutableMap<String, JsonElement>.mapObjsInDict(
key: String,
Expand Down Expand Up @@ -35,9 +32,20 @@ fun Map<String, JsonElement>.toJsonObj(): JsonObject = buildJsonObject {
}

fun MutableMap<String, JsonElement>.replaceJsonObj(name: String, block: (JsonObject) -> JsonElement) {
this[name] = block((this[name] ?: buildJsonObject { }) as JsonObject)
val origEl = this[name] ?: buildJsonObject { }
if (origEl !is JsonObject) error("\"$name\" entry is a ${origEl::class.simpleName}, not an object.")
this[name] = block(origEl)
}

val JsonElement.type get(): String? =
this.jsonObject["type"]?.jsonPrimitive?.contentOrNull

val JsonObject.type get(): String? =
this["type"]?.jsonPrimitive?.contentOrNull

val MutableMap<String, JsonElement>.type get(): String? =
this["type"]?.jsonPrimitive?.contentOrNull

fun MutableMap<String, JsonElement>.replaceMapValues(
name: String,
block: (key: String, value: JsonElement) -> JsonElement
Expand Down
53 changes: 53 additions & 0 deletions src/commonTest/kotlin/baaahs/model/GridSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package baaahs.model

import baaahs.describe
import baaahs.geom.Vector3F
import baaahs.gl.override
import baaahs.model.PolyLine.Segment
import ch.tutteli.atrium.api.fluent.en_GB.containsExactly
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek

object GridSpec : Spek({
describe<Grid> {
val grid by value {
Grid(
"grid",
columns = 4, rows = 5, columnGap = 1f, rowGap = 1f,
direction = GridData.Direction.RowsThenColumns,
zigZag = true
)
}

it("calculates segments") {
expect(grid.segments).containsExactly(
Segment(Vector3F(0.0, 0.0, 0.0), Vector3F(3.0, 0.0, 0.0), 4),
Segment(Vector3F(3.0, 1.0, 0.0), Vector3F(0.0, 1.0, 0.0), 4),
Segment(Vector3F(0.0, 2.0, 0.0), Vector3F(3.0, 2.0, 0.0), 4),
Segment(Vector3F(3.0, 3.0, 0.0), Vector3F(0.0, 3.0, 0.0), 4),
Segment(Vector3F(0.0, 4.0, 0.0), Vector3F(3.0, 4.0, 0.0), 4)
)
}

context("with stagger") {
override(grid) {
Grid(
"grid",
columns = 4, rows = 5, columnGap = 1f, rowGap = 1f,
direction = GridData.Direction.RowsThenColumns,
zigZag = true, stagger = 2
)
}

it("calculates segments with stagger") {
expect(grid.segments).containsExactly(
Segment(Vector3F(0.0, 0.0, 0.0), Vector3F(3.0, 0.0, 0.0), 4),
Segment(Vector3F(3.5, 1.0, 0.0), Vector3F(0.5, 1.0, 0.0), 4),
Segment(Vector3F(0.0, 2.0, 0.0), Vector3F(3.0, 2.0, 0.0), 4),
Segment(Vector3F(3.5, 3.0, 0.0), Vector3F(0.5, 3.0, 0.0), 4),
Segment(Vector3F(0.0, 4.0, 0.0), Vector3F(3.0, 4.0, 0.0), 4)
)
}
}
}
})
Loading

0 comments on commit 7c82d15

Please sign in to comment.