Skip to content
Snippets Groups Projects
main.js 3.58 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jake Read's avatar
    Jake Read committed
    //
    
    Jake Read's avatar
    Jake Read committed
    //
    
    Jake Read's avatar
    Jake Read committed
    // new node controller / HEAP 
    
    Jake Read's avatar
    Jake Read committed
    //
    // main.js
    
    Jake Read's avatar
    Jake Read committed
    //
    
    Jake Read's avatar
    Jake Read committed
    //
    // 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.
    
    
    Jake Read's avatar
    Jake Read committed
    modules are objects having inputs, outputs, and state 
    programs are assemblies of modules 
    
    Jake Read's avatar
    Jake Read committed
    const Reps = require('./reps.js')
    const Programs = require('./programs.js')
    
    Jake Read's avatar
    Jake Read committed
    
    
    Jake Read's avatar
    Jake Read committed
    var program = {} 
    
    Jake Read's avatar
    Jake Read committed
    
    
    program = Programs.open('programs/default.json')
    
    Jake Read's avatar
    Jake Read committed
    const View = require('./views.js')
    View.startHttp() 
    View.startWs() 
    
    Jake Read's avatar
    Jake Read committed
    Programs.assignSocket(View.uiSocket)
    View.assignProgram(program)
    
    Jake Read's avatar
    Jake Read committed
    
    
    Jake Read's avatar
    Jake Read committed
    /*
    
    
    Jake Read's avatar
    Jake Read committed
    PROGRAM REPRESENT 
    
    Jake Read's avatar
    Jake Read committed
    
    */
    
    
    Jake Read's avatar
    Jake Read committed
    /*
    
    Jake Read's avatar
    Jake Read committed
    
    // update state from UI to server 
    function changeState(data) {
    
    Jake Read's avatar
    Jake Read committed
        // should just recv all state, walk tree for differences
    
    Jake Read's avatar
    Jake Read committed
        var oldState = modules[data.id].state
        var newState = data.state
        // rep only holds proper key-values w/o emitters, _values etc
    
    Jake Read's avatar
    Jake Read committed
        for (var key in newState) {
    
    Jake Read's avatar
    Jake Read committed
            if (isStateKey(key)) {
    
    Jake Read's avatar
    Jake Read committed
                if (oldState[key].isButton) {
                    if (oldState[key].isPressed != newState[key].isPressed) {
    
                        console.log('CHANGE BUTTON STATE', key, 'to', newState[key], 'in', data.id)
    
    Jake Read's avatar
    Jake Read committed
                        // this will trigger some 'onChange' f'ns
                        // that might change some state
    
    Jake Read's avatar
    Jake Read committed
                        oldState[key] = newState[key]
    
    Jake Read's avatar
    Jake Read committed
                        // 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
    
    Jake Read's avatar
    Jake Read committed
                } else if (oldState[key] !== newState[key]) {
    
    Jake Read's avatar
    Jake Read committed
                    console.log('CHANGE STATE', key, 'to', newState[key], 'in', data.id)
    
                    oldState[key] = newState[key]
                    return true
    
    Jake Read's avatar
    Jake Read committed
                }
    
    Jake Read's avatar
    Jake Read committed
            }
    
    Jake Read's avatar
    Jake Read committed
    function changeUi(data) {
    
        var mod = modules[data.id]
    
    Jake Read's avatar
    Jake Read committed
        if (mod.ui == null) {
    
            console.log("NO UI")
            mod.ui = {}
        }
        mod.ui = data.ui
        console.log('CHANGE UI', mod.id, mod.ui)
    }
    
    
    Jake Read's avatar
    Jake Read committed
    function setUiPos(module, left, top) {
        if (module.ui == null) {
            module.ui = {}
        }
        module.ui.left = left
        module.ui.top = top
    }
    
    
    Jake Read's avatar
    Jake Read committed
    // push new state from server to UI
    function putState(mod) {
        // push just the state to the individual mod
    
    Jake Read's avatar
    Jake Read committed
        // copy only the keys we want to see 
        var state = {}
    
    Jake Read's avatar
    Jake Read committed
        for (var key in mod.state) {
            if (isStateKey(key)) {
    
    Jake Read's avatar
    Jake Read committed
                state[key] = mod.state[key]
            }
        }
    
    Jake Read's avatar
    Jake Read committed
        var data = {
            id: mod.id,
    
    Jake Read's avatar
    Jake Read committed
            state: state
    
    Jake Read's avatar
    Jake Read committed
        }
    
        socketSend('change state', data)
    
    Jake Read's avatar
    Jake Read committed
    }
    
    
    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])
    
        } else {
    
            fromModule.outputs[data.from.output].attach(toModule.inputs[data.to.input])
    
        // replace it 
        changeRep(fromModule)
    
    Jake Read's avatar
    Jake Read committed
    */
    
    Jake Read's avatar
    Jake Read committed
    // put