Changeset - a460ce76b301
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 months ago 2023-05-29 18:45:06
drewp@bigasterisk.com
almost rm
1 file changed with 83 insertions and 83 deletions:
0 comments (0 inline, 0 general)
light9/live/GraphToControls.ts
Show inline comments
 
import debug from "debug";
 
import { NamedNode } from "n3";
 
import { SyncedGraph } from "../web/SyncedGraph";
 
import { ControlValue, Effect } from "./Effect";
 
const log = debug("g2c");
 
// import debug from "debug";
 
// import { NamedNode } from "n3";
 
// import { SyncedGraph } from "../web/SyncedGraph";
 
// import { ControlValue, Effect } from "./Effect";
 
// const log = debug("g2c");
 

	
 
// Callback for GraphToControls to set a ControlValue on a
 
// Light9LiveControl (widget for a Device+Attr)
 
type NewValueCb = (newValue: ControlValue | null) => void;
 
// // Callback for GraphToControls to set a ControlValue on a
 
// // Light9LiveControl (widget for a Device+Attr)
 
// type NewValueCb = (newValue: ControlValue | null) => void;
 

	
 
// More efficient bridge between liveControl widgets and graph edits (inside Effect),
 
// as opposed to letting each widget scan the graph and push lots of
 
// tiny patches to it.
 
//
 
// When you create a Light9LiveControl, it registers with GraphToControls (and does not
 
// watch value updates from the graph).
 
export class GraphToControls {
 
  // rename to PageControls?
 
  private effect: Effect | null = null; // this uri should sync to the editchoice
 
// // More efficient bridge between liveControl widgets and graph edits (inside Effect),
 
// // as opposed to letting each widget scan the graph and push lots of
 
// // tiny patches to it.
 
// //
 
// // When you create a Light9LiveControl, it registers with GraphToControls (and does not
 
// // watch value updates from the graph).
 
// export class GraphToControls {
 
//   // rename to PageControls?
 
//   private effect: Effect | null = null; // this uri should sync to the editchoice
 

	
 
  // This will fill with every device+attr in the show. Currently there's no unregister to forget a device or attr.
 
  private registeredWidgets: Map<
 
    NamedNode /*Device*/,
 
    Map<
 
      NamedNode /*DeviceAttr*/, //
 
      NewValueCb
 
    >
 
  > = new Map();
 
  constructor(public graph: SyncedGraph) {
 
//   // This will fill with every device+attr in the show. Currently there's no unregister to forget a device or attr.
 
//   private registeredWidgets: Map<
 
//     NamedNode /*Device*/,
 
//     Map<
 
//       NamedNode /*DeviceAttr*/, //
 
//       NewValueCb
 
//     >
 
//   > = new Map();
 
//   constructor(public graph: SyncedGraph) {
 
    
 
  }
 
//   }
 

	
 
  debugDump() {
 
    log("dump: effect", this.effect);
 
    log("registered widgets");
 
    for (let e of this.registeredWidgets.entries()) {
 
      log(" rw:", e[0], e[1]);
 
    }
 
  }
 
//   debugDump() {
 
//     log("dump: effect", this.effect);
 
//     log("registered widgets");
 
//     for (let e of this.registeredWidgets.entries()) {
 
//       log(" rw:", e[0], e[1]);
 
//     }
 
//   }
 

	
 
  setEffect(effect: NamedNode | null) {
 
    log(`setEffect ${effect?.value}`);
 
    this.effect = effect ? new Effect(this.graph, effect, this.onValuesChanged.bind(this)) : null;
 
  }
 
//   setEffect(effect: NamedNode | null) {
 
//     log(`setEffect ${effect?.value}`);
 
//     this.effect = effect ? new Effect(this.graph, effect, this.onValuesChanged.bind(this)) : null;
 
//   }
 

	
 
  newEffect(): NamedNode {
 
    // wrong- this should be our editor's scratch effect, promoted to a
 
    // real one when you name it.
 
    const uri = this.graph.nextNumberedResource(this.graph.Uri("http://light9.bigasterisk.com/effect/effect"));
 
//   newEffect(): NamedNode {
 
//     // wrong- this should be our editor's scratch effect, promoted to a
 
//     // real one when you name it.
 
//     const uri = this.graph.nextNumberedResource(this.graph.Uri("http://light9.bigasterisk.com/effect/effect"));
 

	
 
    this.effect = new Effect(this.graph, uri, this.onValuesChanged.bind(this));
 
    log("add new eff");
 
    this.effect.addNewEffectToGraph();
 
    return this.effect.uri;
 
  }
 
//     this.effect = new Effect(this.graph, uri, this.onValuesChanged.bind(this));
 
//     log("add new eff");
 
//     this.effect.addNewEffectToGraph();
 
//     return this.effect.uri;
 
//   }
 

	
 
  emptyEffect() {
 
    throw new Error("not implemented");
 
  }
 
//   emptyEffect() {
 
//     throw new Error("not implemented");
 
//   }
 

	
 
  private onValuesChanged() {
 
    log(`i learned values changed for ${this.effect?.uri.value} `);
 
    this.registeredWidgets.forEach((d1: Map<NamedNode, NewValueCb>, device: NamedNode) => {
 
      d1.forEach((cb: NewValueCb, deviceAttr: NamedNode) => {
 
        const v = this.effect ? this.effect.currentValue(device, deviceAttr) : null;
 
        cb(v);
 
      });
 
    });
 
  }
 
//   private onValuesChanged() {
 
//     log(`i learned values changed for ${this.effect?.uri.value} `);
 
//     this.registeredWidgets.forEach((d1: Map<NamedNode, NewValueCb>, device: NamedNode) => {
 
//       d1.forEach((cb: NewValueCb, deviceAttr: NamedNode) => {
 
//         const v = this.effect ? this.effect.currentValue(device, deviceAttr) : null;
 
//         cb(v);
 
//       });
 
//     });
 
//   }
 

	
 
  register(device: NamedNode, deviceAttr: NamedNode, graphValueChanged: NewValueCb) {
 
    // log(`control for ${device.value}-${deviceAttr.value} registring with g2c`);
 
    let d1 = this.registeredWidgets.get(device);
 
    if (!d1) {
 
      d1 = new Map();
 
      this.registeredWidgets.set(device, d1);
 
    }
 
    d1.set(deviceAttr, graphValueChanged);
 
//   register(device: NamedNode, deviceAttr: NamedNode, graphValueChanged: NewValueCb) {
 
//     // log(`control for ${device.value}-${deviceAttr.value} registring with g2c`);
 
//     let d1 = this.registeredWidgets.get(device);
 
//     if (!d1) {
 
//       d1 = new Map();
 
//       this.registeredWidgets.set(device, d1);
 
//     }
 
//     d1.set(deviceAttr, graphValueChanged);
 

	
 
    if (this.effect) {
 
      const nv = this.effect.currentValue(device, deviceAttr);
 
      // log(`i have a a cb for ${device.value}-${deviceAttr.value}; start value is ${nv}`);
 
      graphValueChanged(nv);
 
    }
 
  }
 
//     if (this.effect) {
 
//       const nv = this.effect.currentValue(device, deviceAttr);
 
//       // log(`i have a a cb for ${device.value}-${deviceAttr.value}; start value is ${nv}`);
 
//       graphValueChanged(nv);
 
//     }
 
//   }
 

	
 
  controlChanged(device: NamedNode, deviceAttr: NamedNode, value: ControlValue) {
 
    // todo: controls should be disabled if there's no effect and they won't do anything.
 
    if (!this.effect) {
 
      log("controlChanged, no effect");
 
      return;
 
    }
 
    const p = this.effect.edit(device, deviceAttr, value);
 
    this.graph.applyAndSendPatch(p);
 
  }
 
}
 
//   controlChanged(device: NamedNode, deviceAttr: NamedNode, value: ControlValue) {
 
//     // todo: controls should be disabled if there's no effect and they won't do anything.
 
//     if (!this.effect) {
 
//       log("controlChanged, no effect");
 
//       return;
 
//     }
 
//     const p = this.effect.edit(device, deviceAttr, value);
 
//     this.graph.applyAndSendPatch(p);
 
//   }
 
// }
0 comments (0 inline, 0 general)