Files
@ 485148ef5686
Branch filter:
Location: light9/web/timeline/adjustable.ts
485148ef5686
7.5 KiB
video/MP2T
reformat
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 | import * as d3 from "d3";
import { debug } from "debug";
import * as ko from "knockout";
const log = debug("adjustable");
interface Config {
// getTarget -> vec2 of current target position
getTarget: () => Vector;
// getSuggestedTargetOffset -> vec2 pixel offset from target
getSuggestedTargetOffset: () => Vector;
// emptyBox -> true if you want no value display
emptyBox: boolean;
}
export class Adjustable {
config: any;
handle: any;
initialTarget: any;
targetDraggedTo: any;
root: any;
// Some value you can edit in the UI, probably by dragging
// stuff. Drawn by light9-adjusters-canvas. This object does the
// layout and positioning.
//
// The way dragging should work is that you start in the yellow *adj
// widget*, wherever it is, but your drag is moving the *target*. The
// adj will travel around too, but it may do extra moves to not bump
// into stuff or to get out from under your finger.
constructor(config: any) {
this.config = config;
this.ctor2();
}
ctor2() {
// updated later by layout algoritm
return (this.handle = $V([0, 0]));
}
getDisplayValue() {
if (this.config.emptyBox) {
return "";
}
const defaultFormat = d3.format(".4g")(this._getValue());
if (this.config.getDisplayValue != null) {
return this.config.getDisplayValue(this._getValue(), defaultFormat);
}
return defaultFormat;
}
_getValue(): any {
throw new Error("Method not implemented.");
}
getSuggestedHandle() {
return this.getTarget().add(this.config.getSuggestedTargetOffset());
}
getHandle() {
// vec2 of pixels
return this.handle;
}
getTarget() {
// vec2 of pixels
return this.config.getTarget();
}
subscribe(onChange: any) {
// change could be displayValue or center or target. This likely
// calls onChange right away if there's any data yet.
throw new Error("not implemented");
}
startDrag() {
return (this.initialTarget = this.getTarget());
}
continueDrag(pos: { add: (arg0: any) => any }) {
//# pos is vec2 of pixels relative to the drag start
return (this.targetDraggedTo = pos.add(this.initialTarget));
}
endDrag() {}
// override
_editorCoordinates() {
// vec2 of mouse relative to <l9-t-editor>
let rootElem: { getBoundingClientRect: () => any };
return this.targetDraggedTo;
// let ev = d3.event.sourceEvent;
// if (ev.target.tagName === "LIGHT9-TIMELINE-EDITOR") {
// rootElem = ev.target;
// } else {
// rootElem = ev.target.closest("light9-timeline-editor");
// }
// if (ev.touches != null ? ev.touches.length : undefined) {
// ev = ev.touches[0];
// }
// // storing root on the object to remember it across calls in case
// // you drag outside the editor.
// if (rootElem) {
// this.root = rootElem.getBoundingClientRect();
// }
// const offsetParentPos = $V([ev.pageX - this.root.left, ev.pageY - this.root.top]);
// return offsetParentPos;
}
}
class AdjustableFloatObservable extends Adjustable {
constructor(config: any) {
// config also has:
// observable -> ko.observable we will read and write
// getValueForPos(pos) -> what should we set to if the user
// moves target to this coord?
this.config = config;
super();
this.ctor2();
}
_getValue() {
return this.config.observable();
}
continueDrag(pos: any) {
// pos is vec2 of pixels relative to the drag start.
super.continueDrag(pos);
const epos = this._editorCoordinates();
const newValue = this.config.getValueForPos(epos);
return this.config.observable(newValue);
}
subscribe(onChange: () => any) {
log("AdjustableFloatObservable subscribe", this.config);
return ko.computed(() => {
this.config.observable();
return onChange();
});
}
}
class AdjustableFloatObject extends Adjustable {
_currentValue: any;
_onChange: any;
constructor(config: any) {
// config also has:
// graph
// subj
// pred
// ctx
// getTargetPosForValue(value) -> getTarget result for value
// getValueForPos
this.config = config;
super();
this.ctor2();
if (this.config.ctx == null) {
throw new Error("missing ctx");
}
// this seems to not fire enough.
this.config.graph.runHandler(this._syncValue.bind(this), `adj sync ${this.config.subj.value} ${this.config.pred.value}`);
}
_syncValue() {
this._currentValue = this.config.graph.floatValue(this.config.subj, this.config.pred);
if (this._onChange) {
return this._onChange();
}
}
_getValue() {
// this is a big speedup- callers use _getValue about 4x as much as
// the graph changes and graph.floatValue is slow
return this._currentValue;
}
getTarget() {
return this.config.getTargetPosForValue(this._getValue());
}
subscribe(onChange: any) {
// only works on one subscription at a time
if (this._onChange) {
throw new Error("multi subscribe not implemented");
}
return (this._onChange = onChange);
}
continueDrag(pos: any) {
// pos is vec2 of pixels relative to the drag start
super.continueDrag(pos);
const newValue = this.config.getValueForPos(this._editorCoordinates());
return this.config.graph.patchObject(this.config.subj, this.config.pred, this.config.graph.LiteralRoundedFloat(newValue), this.config.ctx);
//@_syncValue()
}
}
class AdjustableFade extends Adjustable {
yForV: any;
zoomInX: any;
i0: any;
i1: any;
note: any;
constructor(yForV: any, zoomInX: any, i0: any, i1: any, note: any, offset: any, ctx: any) {
this.yForV = yForV;
this.zoomInX = zoomInX;
this.i0 = i0;
this.i1 = i1;
this.note = note;
super();
this.config = {
getSuggestedTargetOffset() {
return offset;
},
getTarget: this.getTarget.bind(this),
ctx,
};
this.ctor2();
}
getTarget() {
const mid = this.note.midPoint(this.i0, this.i1);
return $V([this.zoomInX(mid.e(1)), this.yForV(mid.e(2))]);
}
_getValue() {
return this.note.midPoint(this.i0, this.i1).e(1);
}
continueDrag(pos: { e: (arg0: number) => any }) {
// pos is vec2 of pixels relative to the drag start
super.continueDrag(pos);
const { graph } = this.note;
const U = (x: string) => graph.Uri(x);
const goalCenterSec = this.zoomInX.invert(this.initialTarget.e(1) + pos.e(1));
const diamSec = this.note.worldPts[this.i1].e(1) - this.note.worldPts[this.i0].e(1);
const newSec0 = goalCenterSec - diamSec / 2;
const newSec1 = goalCenterSec + diamSec / 2;
const originSec = graph.floatValue(this.note.uri, U(":originTime"));
const p0 = this._makePatch(graph, this.i0, newSec0, originSec, this.config.ctx);
const p1 = this._makePatch(graph, this.i1, newSec1, originSec, this.config.ctx);
return graph.applyAndSendPatch(this._addPatches(p0, p1));
}
_makePatch(
graph: { getObjectPatch: (arg0: any, arg1: any, arg2: any, arg3: any) => any; Uri: (arg0: string) => any; LiteralRoundedFloat: (arg0: number) => any },
idx: string | number,
newSec: number,
originSec: number,
ctx: any
) {
return graph.getObjectPatch(this.note.worldPts[idx].uri, graph.Uri(":time"), graph.LiteralRoundedFloat(newSec - originSec), ctx);
}
_addPatches(p0: { addQuads: { concat: (arg0: any) => any }; delQuads: { concat: (arg0: any) => any } }, p1: { addQuads: any; delQuads: any }) {
return {
addQuads: p0.addQuads.concat(p1.addQuads),
delQuads: p0.delQuads.concat(p1.delQuads),
};
}
}
|