Newer
Older
//
// Jake Read at the Center for Bits and Atoms
// (c) Massachusetts Institute of Technology 2018
//
// This work may be reproduced, modified, distributed, performed, and
// displayed for any purpose, but must acknowledge the mods
// project. Copyright is retained and must be preserved. The work is
// provided as is; no warranty is provided, and users accept all
// liability.
modules are objects having inputs, outputs, and state
programs are assemblies of modules
const Reps = require('./reps.js')
const Programs = require('./programs.js')
program = Programs.open('programs/default.json')
const View = require('./views.js')
View.startHttp()
View.startWs()
// update state from UI to server
function changeState(data) {
// of one module
// should just recv all state, walk tree for differences
var oldState = modules[data.id].state
var newState = data.state
// rep only holds proper key-values w/o emitters, _values etc
if (oldState[key].isButton) {
if (oldState[key].isPressed != newState[key].isPressed) {
console.log('CHANGE BUTTON STATE', key, 'to', newState[key], 'in', data.id)
// this will trigger some 'onChange' f'ns
// that might change some state
// to prevent quickly changing it back, we'll exit now (one state change from UI per trx)
} else if (oldState[key].isMultiLine) {
if (oldState[key].value != newState[key].value) {
console.log('CHANGE MULTILINE STATE', key, 'to', newState[key].value, 'in', data.id)
oldState[key].value = newState[key].value
return true
console.log('CHANGE STATE', key, 'to', newState[key], 'in', data.id)
oldState[key] = newState[key]
return true
console.log("NO UI")
mod.ui = {}
}
mod.ui = data.ui
console.log('CHANGE UI', mod.id, mod.ui)
}
function setUiPos(module, left, top) {
if (module.ui == null) {
module.ui = {}
}
module.ui.left = left
module.ui.top = top
}
// push new state from server to UI
function putState(mod) {
// push just the state to the individual mod
for (var key in mod.state) {
if (isStateKey(key)) {
socketSend('change state', data)
function putLink(data) {
var fromModule = modules.find((module) => {
return module.id === data.from.id
})
var toModule = modules.find((module) => {
return module.id === data.to.id
})
if (fromModule.outputs[data.from.output].isLinked(toModule.inputs[data.to.input])) {
console.log("HOOKDOWN")
fromModule.outputs[data.from.output].remove(toModule.inputs[data.to.input])
fromModule.outputs[data.from.output].attach(toModule.inputs[data.to.input])
// replace it
changeRep(fromModule)