Skip to content

Commit

Permalink
Merge pull request #300 from baaahs/edit-links
Browse files Browse the repository at this point in the history
Design mode fixes
  • Loading branch information
xian authored Oct 11, 2020
2 parents c25a61b + 00bba86 commit e36123d
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 53 deletions.
4 changes: 3 additions & 1 deletion src/commonMain/kotlin/baaahs/Gadget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ private class GadgetValueObserver<T>(
}

@Serializable()
class GadgetData(val name: String, @Polymorphic var gadget: Gadget, val topicName: String)
class GadgetData(val name: String, @Polymorphic var gadget: Gadget, val topicName: String) {
override fun toString(): String = "GadgetData(name='$name', gadget=$gadget, topicName='$topicName')"
}

val GadgetDataSerializer = MapSerializer(String.serializer(), JsonElement.serializer())

Expand Down
6 changes: 5 additions & 1 deletion src/commonMain/kotlin/baaahs/ShowPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class BaseShowPlayer(
private val shaders = mutableMapOf<Shader, OpenShader>()

override val dataSources: List<DataSource> get() = dataFeeds.keys.toList()
protected val dataSourceGadgets: MutableMap<DataSource, Gadget> = mutableMapOf()
private val dataSourceGadgets: MutableMap<DataSource, Gadget> = mutableMapOf()

override fun openDataFeed(id: String, dataSource: DataSource): GlslProgram.DataFeed {
return dataFeeds.getOrPut(dataSource) {
Expand All @@ -61,6 +61,10 @@ abstract class BaseShowPlayer(
return dataFeeds[dataSource]!!
}

override fun <T : Gadget> registerGadget(id: String, gadget: T, controlledDataSource: DataSource?) {
controlledDataSource?.let { dataSourceGadgets[controlledDataSource] = gadget }
}

override fun <T : Gadget> useGadget(dataSource: DataSource): T? {
@Suppress("UNCHECKED_CAST")
return dataSourceGadgets[dataSource] as? T
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/baaahs/StageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class StageManager(
gadget.listen(gadgetChannelListener)
val gadgetData = GadgetData(id, gadget, topic.name)
gadgets[id] = GadgetManager.GadgetInfo(topic, channel, gadgetData, gadgetChannelListener)
controlledDataSource?.let { dataSourceGadgets[controlledDataSource] = gadget }
super.registerGadget(id, gadget, controlledDataSource)
}

fun onGadgetChange() {
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/baaahs/client/ClientShowPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ClientShowPlayer(

override fun <T : Gadget> registerGadget(id: String, gadget: T, controlledDataSource: DataSource?) {
gadgets[id] = ClientGadget(id, pubSub, gadget)
controlledDataSource?.let { dataSourceGadgets[controlledDataSource] = gadget }
super.registerGadget(id, gadget, controlledDataSource)
}

override fun <T : Gadget> useGadget(id: String): T {
Expand Down
50 changes: 30 additions & 20 deletions src/commonMain/kotlin/baaahs/gl/preview/PreviewShaderBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import baaahs.show.ShaderType
import baaahs.show.mutable.MutableConstPort
import baaahs.show.mutable.MutablePatch
import baaahs.ui.Observable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class PreviewShaderBuilder(
val shader: Shader,
private val autoWirer: AutoWirer,
private val modelInfo: ModelInfo
private val modelInfo: ModelInfo,
private val coroutineScope: CoroutineScope = GlobalScope
) : Observable() {
var state: State =
State.Unbuilt
Expand All @@ -50,29 +52,11 @@ class PreviewShaderBuilder(
state = State.Linking
notifyChanged()

GlobalScope.launch {
coroutineScope.launch {
link()
}
}

fun startCompile(gl: GlContext) {
state = State.Compiling
notifyChanged()

GlobalScope.launch {
val showPlayer = object : BaseShowPlayer(autoWirer.plugins, modelInfo) {
override val glContext: GlContext get() = gl

override fun <T : Gadget> registerGadget(id: String, gadget: T, controlledDataSource: DataSource?) {
mutableGadgets.add(GadgetData(id, gadget, "/preview/gadgets/$id"))
}
}
compile(gl) { id, dataSource ->
dataSource.createFeed(showPlayer, autoWirer.plugins, id)
}
}
}

fun link() {
val screenCoordsProjection by lazy {
Shader(
Expand Down Expand Up @@ -115,6 +99,29 @@ class PreviewShaderBuilder(
notifyChanged()
}

fun startCompile(gl: GlContext) {
state = State.Compiling
notifyChanged()

coroutineScope.launch {
val showPlayer = object : BaseShowPlayer(autoWirer.plugins, modelInfo) {
override val glContext: GlContext get() = gl
override fun <T : Gadget> registerGadget(id: String, gadget: T, controlledDataSource: DataSource?) {
mutableGadgets.add(GadgetData(id, gadget, "/preview/gadgets/$id"))
super.registerGadget(id, gadget, controlledDataSource)
}
}

compile(gl) { id, dataSource ->
dataSource.buildControl()?.let {
showPlayer.registerGadget(dataSource.suggestId(), it.gadget, dataSource)
}

dataSource.createFeed(showPlayer, autoWirer.plugins, id)
}
}
}

fun compile(gl: GlContext, resolver: Resolver) {
try {
glslProgram = linkedPatch?.compile(gl, resolver)
Expand All @@ -130,6 +137,9 @@ class PreviewShaderBuilder(
notifyChanged()
}

/**
* Contrary to expectations, linking happens before compliling in this world.
*/
enum class State {
Unbuilt,
Linking,
Expand Down
19 changes: 12 additions & 7 deletions src/commonMain/kotlin/baaahs/show/live/ControlDisplay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class ControlDisplay(
show.allControls.flatMap { it.controlledDataSources() }
suggestedControls = dataSourcesWithoutControls.mapNotNull { it.buildControl()?.open() }
}
val unplacedControls = show.allControls.filter { !placedControls.contains(it) }
val unplacedControls = (show.allControls + suggestedControls)
.filter { !placedControls.contains(it) }

fun render(panelTitle: String, renderBucket: RenderBucket) {
allPanelBuckets.render(panelTitle, renderBucket)
Expand Down Expand Up @@ -185,11 +186,10 @@ class ControlDisplay(
}

inner class PlacedControl(val control: OpenControl, val index: Int) : PlaceableControl {
override lateinit var mutableShow: MutableShow
override val mutableShow: MutableShow by lazy { show.edit() }
override lateinit var mutableControl: MutableControl

override fun remove() {
mutableShow = show.edit()
val mutablePatchHolder = mutableShow.findPatchHolder(section.container)
mutableControl = mutablePatchHolder.removeControl(panelTitle, index)
}
Expand Down Expand Up @@ -226,12 +226,17 @@ class ControlDisplay(
}

inner class UnplacedControl(val index: Int) : PlaceableControl {
override lateinit var mutableShow: MutableShow
override lateinit var mutableControl: MutableControl
override val mutableShow: MutableShow by lazy { show.edit() }
override val mutableControl: MutableControl by lazy {
val subject = unplacedControls[index]
if (suggestedControls.contains(subject)) {
subject.toNewMutable(mutableShow)
} else {
mutableShow.findControl(subject.id)
}
}

override fun remove() {
mutableShow = show.edit()
mutableControl = mutableShow.findControl(unplacedControls[index].id)
}

override fun onMove() {
Expand Down
22 changes: 15 additions & 7 deletions src/commonMain/kotlin/baaahs/show/live/OpenControl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import baaahs.gadgets.Switch
import baaahs.show.ButtonControl
import baaahs.show.ButtonGroupControl
import baaahs.show.DataSource
import baaahs.show.mutable.MutableButtonControl
import baaahs.show.mutable.MutableButtonGroupControl
import baaahs.show.mutable.MutableControl
import baaahs.show.mutable.MutableShow
import baaahs.show.mutable.*
import baaahs.ui.Draggable
import baaahs.ui.DropTarget
import kotlinx.serialization.json.JsonElement
Expand All @@ -22,6 +19,7 @@ interface OpenControl {
fun controlledDataSources(): Set<DataSource> = emptySet()
fun addTo(activeSetBuilder: ActiveSetBuilder, panelId: String, depth: Int) {}
fun applyConstraints() {}
fun toNewMutable(mutableShow: MutableShow): MutableControl
}

class OpenGadgetControl(
Expand All @@ -30,11 +28,14 @@ class OpenGadgetControl(
val controlledDataSource: DataSource
) : OpenControl {
override fun controlledDataSources(): Set<DataSource> = setOf(controlledDataSource)
override fun toNewMutable(mutableShow: MutableShow): MutableControl {
return MutableGadgetControl(gadget, controlledDataSource)
}
}

class OpenButtonControl(
override val id: String,
buttonControl: ButtonControl,
private val buttonControl: ButtonControl,
openContext: OpenContext
) : OpenPatchHolder(buttonControl, openContext), OpenControl {
override val gadget: Switch = Switch(buttonControl.title)
Expand All @@ -51,6 +52,10 @@ class OpenButtonControl(
}
}

override fun toNewMutable(mutableShow: MutableShow): MutableControl {
return MutableButtonControl(buttonControl, mutableShow)
}

fun click() {
isPressed = !isPressed
}
Expand Down Expand Up @@ -88,6 +93,10 @@ class OpenButtonGroupControl(
}
}

override fun toNewMutable(mutableShow: MutableShow): MutableControl {
error("not implemented for button groups")
}

fun clickOn(buttonIndex: Int) {
buttons.forEachIndexed { index, openButtonControl ->
openButtonControl.isPressed = index == buttonIndex
Expand Down Expand Up @@ -119,13 +128,12 @@ class OpenButtonGroupControl(
}

override fun getDraggable(index: Int): Draggable = object : ControlDisplay.PlaceableControl {
override lateinit var mutableShow: MutableShow
override val mutableShow: MutableShow by lazy { controlDisplay.show.edit() }
override lateinit var mutableControl: MutableControl

override fun willMoveTo(destination: DropTarget): Boolean = true

override fun remove() {
mutableShow = controlDisplay.show.edit()
mutableControl = mutableShow.findButtonGroup()
.buttons
.removeAt(index)
Expand Down
86 changes: 86 additions & 0 deletions src/commonTest/kotlin/baaahs/gl/PreviewShaderBuilderSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package baaahs.gl

import baaahs.gadgets.Slider
import baaahs.gl.patch.AutoWirer
import baaahs.gl.preview.PreviewShaderBuilder
import baaahs.glsl.Shaders
import baaahs.model.ModelInfo
import baaahs.plugin.Plugins
import baaahs.shows.FakeGlContext
import baaahs.shows.FakeKgl
import describe
import ext.TestCoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.InternalCoroutinesApi
import org.spekframework.spek2.Spek
import kotlin.test.assertNotNull
import kotlin.test.expect

@InternalCoroutinesApi
object PreviewShaderBuilderSpec : Spek({
describe<PreviewShaderBuilder> {
val shader by value { Shaders.checkerboard }
val autoWirer by value { AutoWirer(Plugins.safe()) }
val testCoroutineContext by value { TestCoroutineContext("global") }
val previewShaderBuilder by value {
PreviewShaderBuilder(shader, autoWirer, ModelInfo.Empty, CoroutineScope(testCoroutineContext))
}
val glContext by value { FakeGlContext(FakeKgl()) }

it("is in Unbuilt state") {
expect(PreviewShaderBuilder.State.Unbuilt) { previewShaderBuilder.state }
}

context("when startBuilding() is called") {
beforeEachTest {
previewShaderBuilder.startBuilding()
}

it("is in Linking state") {
expect(PreviewShaderBuilder.State.Linking) { previewShaderBuilder.state }
}

context("after idle") {
beforeEachTest { testCoroutineContext.runAll() }

it("is in Linked state") {
expect(PreviewShaderBuilder.State.Linked) { previewShaderBuilder.state }
}

it("has a previewPatch") {
assertNotNull(previewShaderBuilder.previewPatch)
}

it("has a linkedPatch") {
assertNotNull(previewShaderBuilder.linkedPatch)
}

it("has no gadgets yet") {
expect(emptyList()) { previewShaderBuilder.gadgets }
}

context("when startCompile() is called") {
beforeEachTest { previewShaderBuilder.startCompile(glContext) }

it("is in Compiling state") {
expect(PreviewShaderBuilder.State.Compiling) { previewShaderBuilder.state }
}

context("after idle") {
beforeEachTest { testCoroutineContext.runAll() }

it("is in Success state") {
expect(PreviewShaderBuilder.State.Success) { previewShaderBuilder.state }
}

it("has gadgets") {
expect(listOf(
Slider("Checkerboard Size", .1f, .001f, 1f),
)) { previewShaderBuilder.gadgets.map { it.gadget } }
}
}
}
}
}
}
})
6 changes: 5 additions & 1 deletion src/jsMain/kotlin/baaahs/app/ui/controls/Gadget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ val Gadget = xComponent<GadgetProps>("Gadget") { props ->
val appContext = useContext(appContext)
val dataSource = props.control.controlledDataSource
val dataFeed = appContext.showPlayer.useDataFeed(dataSource)
val gadget = when (dataFeed) {
is CorePlugin.GadgetDataFeed -> dataFeed.gadget
else -> dataSource.buildControl()!!.gadget
}
val title = props.control.gadget.title

when (dataSource.getRenderType()) {
"Slider" -> {
RangeSlider {
attrs.gadget = (dataFeed as CorePlugin.GadgetDataFeed).gadget
attrs.gadget = gadget
}
div(+Styles.dataSourceTitle) { +title }
}
Expand Down
2 changes: 2 additions & 0 deletions src/jsMain/kotlin/baaahs/app/ui/editor/EditableStyles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ object EditableStyles : StyleSheet("app-ui-Editable", isStatic = true) {

val editorCol by css {
flex(4.0, flexBasis = FlexBasis.zero)
display = Display.flex
flexDirection = FlexDirection.column
marginLeft = 2.em
}
val patchOverview by css {
Expand Down
3 changes: 2 additions & 1 deletion src/jsMain/kotlin/baaahs/app/ui/editor/EditorPanels.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package baaahs.app.ui.editor

import baaahs.show.ButtonGroupControl
import baaahs.show.ShaderChannel
import baaahs.show.mutable.MutableButtonGroupControl
import baaahs.show.mutable.MutablePatch
import baaahs.show.mutable.MutablePatchHolder
Expand Down Expand Up @@ -65,7 +66,7 @@ actual fun getEditorPanelViews(): EditorPanelViews = object : EditorPanelViews {
attrs.editableManager = editableManager
attrs.mutablePatch = mutablePatch
attrs.mutableShaderInstance = mutableShaderInstance
attrs.shaderChannels = emptySet() // TODO
attrs.shaderChannels = (mutablePatch.findShaderChannels() + ShaderChannel.Main).toSet()
}
}

Expand Down
1 change: 0 additions & 1 deletion src/jsMain/kotlin/baaahs/app/ui/editor/LinkSourceEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ val LinkSourceEditor = xComponent<LinkSourceEditorProps>("LinkSourceEditor", isP
else -> value
}
)
this@xComponent.forceRender()
}

val sourcePortOptions = props.sourcePortOptions + suggestedDataSources.map { DataSourceOption(it) } +
Expand Down
Loading

0 comments on commit e36123d

Please sign in to comment.