Files
@ cc69faa87c27
Branch filter:
Location: light9/web/fade/Light9EffectFader.ts
cc69faa87c27
5.7 KiB
video/MP2T
tear up and rewrite ascoltami to emit player state into the graph. web ui works but displays nothing but songs
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 | import debug from "debug";
import { css, html, LitElement } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { NamedNode, Quad } from "n3";
import { getTopGraph } from "../RdfdbSyncedGraph";
import { showRoot } from "../show_specific";
import { SyncedGraph } from "../SyncedGraph";
import { Patch } from "../patch";
import { Literal } from "n3";
export { Light9Fader } from "./Light9Fader";
const log = debug("efffader")
//////////////////////////////////////
const RETURN_URI = new NamedNode("");
const RETURN_FLOAT = 1;
function get2Step<T extends NamedNode | number>(returnWhat: T, graph: SyncedGraph, subj1: NamedNode, pred1: NamedNode, pred2: NamedNode): T | undefined {
// ?subj1 ?pred1 ?x . ?x ?pred2 ?returned .
let x: NamedNode;
try {
x = graph.uriValue(subj1, pred1);
} catch (e) {
return undefined;
}
try {
if (typeof returnWhat === "object" && (returnWhat as NamedNode).termType == "NamedNode") {
return graph.uriValue(x, pred2) as T;
} else if (typeof returnWhat === "number") {
return graph.floatValue(x, pred2) as T;
}
} catch (e) {
return undefined;
}
}
function set2Step(
graph: SyncedGraph, //
subj1: NamedNode,
pred1: NamedNode,
baseName: string,
pred2: NamedNode,
newObjLiteral: Literal
) { }
function maybeUriValue(graph: SyncedGraph, s: NamedNode, p: NamedNode): NamedNode | undefined {
try {
return graph.uriValue(s, p);
} catch (e) {
return undefined;
}
}
function maybeStringValue(graph: SyncedGraph, s: NamedNode, p: NamedNode): string | undefined {
try {
return graph.stringValue(s, p);
} catch (e) {
return undefined;
}
}
function maybeFloatValue(graph: SyncedGraph, s: NamedNode, p: NamedNode): number | undefined {
try {
return graph.floatValue(s, p);
} catch (e) {
return undefined;
}
}
//////////////////////////////////////
class EffectFader {
constructor(public uri: NamedNode) { }
column: string = "unset";
effect?: NamedNode;
effectAttr?: NamedNode; // :strength
setting?: NamedNode; // we assume fader always has exactly one setting
value?: number;
}
@customElement("light9-effect-fader")
export class Light9EffectFader extends LitElement {
static styles = [
css`
:host {
display: inline-block;
border: 2px gray outset;
background: #272727;
}
light9-fader {
margin: 0px;
width: 100%;
}
`,
];
render() {
if (this.conf === undefined || this.conf.value === undefined) {
return html`...`;
}
return html`
<div><resource-display .uri=${this.uri}></resource-display>
<light9-fader .value=${this.conf.value} @change=${this.onSliderInput}></light9-fader>
<div>${this.conf.value.toPrecision(3)}</div>
<div>effect <edit-choice nounlink .uri=${this.conf.effect} @edited=${this.onEffectChange}></edit-choice></div>
<div>attr <edit-choice nounlink .uri=${this.conf.effectAttr} @edited=${this.onEffectAttrChange}></edit-choice></div>
`;
}
graph?: SyncedGraph;
ctx: NamedNode = new NamedNode(showRoot + "/fade");
@property() uri!: NamedNode;
@state() conf?: EffectFader; // compiled from graph
constructor() {
super();
getTopGraph().then((g) => {
this.graph = g;
this.graph.runHandler(this.compile.bind(this, this.graph), `fader config ${this.uri.value}`);
});
}
private compile(graph: SyncedGraph) {
const U = graph.U();
this.conf = undefined;
const conf = new EffectFader(this.uri);
if (!graph.contains(this.uri, U("rdf:type"), U(":Fader"))) {
// not loaded yet, perhaps
return;
}
conf.column = maybeStringValue(graph, this.uri, U(":column")) || "unset";
conf.effect = maybeUriValue(graph, this.uri, U(":effect"));
conf.effectAttr = get2Step(RETURN_URI, graph, this.uri, U(":setting"), U(":effectAttr"));
this.conf = conf;
graph.runHandler(this.compileValue.bind(this, graph, this.conf), `fader config.value ${this.uri.value}`);
}
private compileValue(graph: SyncedGraph, conf: EffectFader) {
// external graph change -> conf.value
const U = graph.U();
conf.value = get2Step(RETURN_FLOAT, graph, this.uri, U(":setting"), U(":value"));
// since conf attrs aren't watched as property:
this.requestUpdate()
}
onSliderInput(ev: CustomEvent) {
// slider user input -> graph
if (this.conf === undefined) return;
this.conf.value = ev.detail.value
this.writeValueToGraph()
}
writeValueToGraph() {
// this.value -> graph
if (this.graph === undefined) {
return;
}
const U = this.graph.U();
if (this.conf === undefined) {
return;
}
if (this.conf.value === undefined) {
log(`value of ${this.uri} is undefined`)
return;
}
log('writeValueToGraph', this.conf.value)
const valueTerm = this.graph.LiteralRoundedFloat(this.conf.value);
const settingNode = this.graph.uriValue(this.uri, U(":setting"));
this.graph.patchObject(settingNode, this.graph.Uri(":value"), valueTerm, this.ctx);
}
onEffectChange(ev: CustomEvent) {
if (this.graph === undefined) {
return;
}
const { newValue } = ev.detail;
this.graph.patchObject(this.uri, this.graph.Uri(":effect"), newValue, this.ctx);
}
onEffectAttrChange(ev: CustomEvent) {
if (this.graph === undefined) {
return;
}
// const { newValue } = ev.detail;
// if (this.setting === undefined) {
// this.setting = this.graph.nextNumberedResource(this.graph.Uri(":fade_set"));
// this.graph.patchObject(this.uri, this.graph.Uri(":setting"), this.setting, this.ctx);
// }
// this.graph.patchObject(this.setting, this.graph.Uri(":effectAttr"), newValue, this.ctx);
}
}
|