Files
@ 4556eebe5d73
Branch filter:
Location: light9/web/live/Effect.ts - annotation
4556eebe5d73
9.8 KiB
video/MP2T
topdir reorgs; let pdm have its src/ dir; separate vite area from light9/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 4556eebe5d73 | import debug from "debug";
import { Literal, NamedNode, Quad, Quad_Object, Quad_Predicate, Quad_Subject, Term } from "n3";
import { some } from "underscore";
import { Patch } from "../patch";
import { SyncedGraph } from "../SyncedGraph";
import { shortShow } from "../show_specific";
import { SubEvent } from "sub-events";
// todo: Align these names with newtypes.py, which uses HexColor and VTUnion.
type Color = string;
export type ControlValue = number | Color | NamedNode;
const log = debug("effect");
function isUri(x: Term | number | string): x is NamedNode {
return typeof x == "object" && x.termType == "NamedNode";
}
// todo: eliminate this. address the scaling when we actually scale
// stuff, instead of making a mess of every setting
function valuePred(graph: SyncedGraph, attr: NamedNode): NamedNode {
const U = graph.U();
const scaledAttributeTypes = [U(":color"), U(":brightness"), U(":uv")];
if (some(scaledAttributeTypes, (x: NamedNode) => attr.equals(x))) {
return U(":value");
} else {
return U(":value");
}
}
// also see resourcedisplay's version of this
function effContext(graph: SyncedGraph, uri: NamedNode): NamedNode {
return graph.Uri(uri.value.replace("light9.bigasterisk.com/effect", `light9.bigasterisk.com/show/${shortShow}/effect`));
}
export function newEffect(graph: SyncedGraph): NamedNode {
// wrong- this should be our editor's scratch effect, promoted to a
// real one when you name it.
const uri = graph.nextNumberedResource(graph.Uri("http://light9.bigasterisk.com/effect/effect"));
const effect = new Effect(graph, uri);
const U = graph.U();
const ctx = effContext(graph, uri);
const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => graph.Quad(s, p, o, ctx);
const addQuads = [
quad(uri, U("rdf:type"), U(":Effect")),
quad(uri, U("rdfs:label"), graph.Literal(uri.value.replace(/.*\//, ""))),
quad(uri, U(":publishAttr"), U(":strength")),
quad(uri, U(":effectFunction"), U(":effectFunction/scale")),
];
const patch = new Patch([], addQuads);
log("init new effect", patch);
graph.applyAndSendPatch(patch);
return effect.uri;
}
// effect settings data; r/w sync with the graph
export class Effect {
// :effect1 a Effect; :setting ?eset . ?eset :effectAttr :deviceSettings; :value ?dset . ?dset :device ..
private eset?: NamedNode;
private dsettings: Array<{ dset: NamedNode; device: NamedNode; deviceAttr: NamedNode; value: ControlValue }> = [];
private ctxForEffect: NamedNode;
settingsChanged: SubEvent<void> = new SubEvent();
constructor(public graph: SyncedGraph, public uri: NamedNode) {
this.ctxForEffect = effContext(this.graph, this.uri);
graph.runHandler(this.rebuildSettingsFromGraph.bind(this), `effect sync ${uri.value}`);
}
private getExistingEset(): NamedNode | null {
const U = this.graph.U();
for (let eset of this.graph.objects(this.uri, U(":setting"))) {
if (this.graph.uriValue(eset as Quad_Subject, U(":effectAttr")).equals(U(":deviceSettings"))) {
return eset as NamedNode;
}
}
return null;
}
private getExistingEsetValueNode(): NamedNode | null {
const U = this.graph.U();
const eset = this.getExistingEset();
if (eset === null) return null;
try {
return this.graph.uriValue(eset, U(":value"));
} catch (e) {
return null;
}
}
private patchForANewEset(): { p: Patch; eset: NamedNode } {
const U = this.graph.U();
const eset = this.graph.nextNumberedResource(U(":e_set"));
return {
eset: eset,
p: new Patch(
[],
[
//
new Quad(this.uri, U(":setting"), eset, this.ctxForEffect),
new Quad(eset, U(":effectAttr"), U(":deviceSettings"), this.ctxForEffect),
]
),
};
}
private rebuildSettingsFromGraph(patch?: Patch) {
const U = this.graph.U();
log("syncFromGraph", this.uri);
// this repeats work- it gathers all settings when really some values changed (and we might even know about them). maybe push the value-fetching into a secnod phase of the run, and have the 1st phase drop out early
const newSettings = [];
const deviceSettingsNode = this.getExistingEsetValueNode();
if (deviceSettingsNode !== null) {
for (let dset of Array.from(this.graph.objects(deviceSettingsNode, U(":setting"))) as NamedNode[]) {
// // log(` setting ${setting.value}`);
// if (!isUri(dset)) throw new Error();
let value: ControlValue;
const device = this.graph.uriValue(dset, U(":device"));
const deviceAttr = this.graph.uriValue(dset, U(":deviceAttr"));
const pred = valuePred(this.graph, deviceAttr);
try {
value = this.graph.uriValue(dset, pred);
if (!(value as NamedNode).id.match(/^http/)) {
throw new Error("not uri");
}
} catch (error) {
try {
value = this.graph.floatValue(dset, pred);
} catch (error1) {
value = this.graph.stringValue(dset, pred); // this may find multi values and throw
}
}
// log(`change: graph contains ${deviceAttr.value} ${value}`);
newSettings.push({ dset, device, deviceAttr, value });
}
}
this.dsettings = newSettings;
log(`settings is rebuilt to length ${this.dsettings.length}`);
this.settingsChanged.emit(); // maybe one emitter per dev+attr?
// this.onValuesChanged();
}
currentValue(device: NamedNode, deviceAttr: NamedNode): ControlValue | null {
for (let s of this.dsettings) {
if (device.equals(s.device) && deviceAttr.equals(s.deviceAttr)) {
return s.value;
}
}
return null;
}
// change this object now, but return the patch to be applied to the graph so it can be coalesced.
edit(device: NamedNode, deviceAttr: NamedNode, newValue: ControlValue | null): Patch {
log(`edit: value=${newValue}`);
let existingSetting: NamedNode | null = null;
let result = new Patch([], []);
for (let s of this.dsettings) {
if (device.equals(s.device) && deviceAttr.equals(s.deviceAttr)) {
if (existingSetting !== null) {
// this is corrupt. There was only supposed to be one setting per (dev,attr) pair. But we can fix it because we're going to update existingSetting to the user's requested value.
log(`${this.uri.value} had two settings for ${device.value} - ${deviceAttr.value} - deleting ${s.dset}`);
result = result.update(this.removeEffectSetting(s.dset));
}
existingSetting = s.dset;
}
}
if (newValue !== null && this.shouldBeStored(deviceAttr, newValue)) {
if (existingSetting === null) {
result = result.update(this.addEffectSetting(device, deviceAttr, newValue));
} else {
result = result.update(this.patchExistingDevSetting(existingSetting, deviceAttr, newValue));
}
} else {
if (existingSetting !== null) {
result = result.update(this.removeEffectSetting(existingSetting));
}
}
return result;
}
shouldBeStored(deviceAttr: NamedNode, value: ControlValue | null): boolean {
// this is a bug for zoom=0, since collector will default it to
// stick at the last setting if we don't explicitly send the
// 0. rx/ry similar though not the exact same deal because of
// their remap.
return value != null && value !== 0 && value !== "#000000";
}
private addEffectSetting(device: NamedNode, deviceAttr: NamedNode, value: ControlValue): Patch {
log(" _addEffectSetting", deviceAttr.value, value);
const U = (x: string) => this.graph.Uri(x);
const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => this.graph.Quad(s, p, o, this.ctxForEffect);
let patch = new Patch([], []);
let eset = this.getExistingEset();
if (eset === null) {
const ret = this.patchForANewEset();
patch = patch.update(ret.p);
eset = ret.eset;
}
let dsValue;
try {
dsValue = this.graph.uriValue(eset, U(":value"));
} catch (e) {
dsValue = this.graph.nextNumberedResource(U(":ds_val"));
patch = patch.update(new Patch([], [quad(eset, U(":value"), dsValue)]));
}
const dset = this.graph.nextNumberedResource(this.uri.value + "_set");
patch = patch.update(
new Patch(
[],
[
quad(dsValue, U(":setting"), dset),
quad(dset, U(":device"), device),
quad(dset, U(":deviceAttr"), deviceAttr),
quad(dset, valuePred(this.graph, deviceAttr), this.nodeForValue(value)),
]
)
);
log(" save", patch);
this.dsettings.push({ dset, device, deviceAttr, value });
return patch;
}
private patchExistingDevSetting(devSetting: NamedNode, deviceAttr: NamedNode, value: ControlValue): Patch {
log(" patch existing", devSetting.value);
return this.graph.getObjectPatch(
devSetting, //
valuePred(this.graph, deviceAttr),
this.nodeForValue(value),
this.ctxForEffect
);
}
private removeEffectSetting(effectSetting: NamedNode): Patch {
const U = (x: string) => this.graph.Uri(x);
log(" _removeEffectSetting", effectSetting.value);
const eset = this.getExistingEset();
if (eset === null) throw "unexpected";
const dsValue = this.graph.uriValue(eset, U(":value"));
if (dsValue === null) throw "unexpected";
const toDel = [this.graph.Quad(dsValue, U(":setting"), effectSetting, this.ctxForEffect)];
for (let q of this.graph.subjectStatements(effectSetting)) {
toDel.push(q);
}
return new Patch(toDel, []);
}
clearAllSettings() {
for (let s of this.dsettings) {
this.graph.applyAndSendPatch(this.removeEffectSetting(s.dset));
}
}
private nodeForValue(value: ControlValue): NamedNode | Literal {
if (value === null) {
throw new Error("no value");
}
if (isUri(value)) {
return value;
}
return this.graph.prettyLiteral(value);
}
}
|