annotate web/timeline/adjustable.ts @ 2376:4556eebe5d73

topdir reorgs; let pdm have its src/ dir; separate vite area from light9/
author drewp@bigasterisk.com
date Sun, 12 May 2024 19:02:10 -0700
parents light9/web/timeline/adjustable.ts@d991f7c3485a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
1 import * as d3 from "d3";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
2 import { debug } from "debug";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
3 import * as ko from "knockout";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
4 const log = debug("adjustable");
1901
7fe81130b735 move web logging to https://github.com/visionmedia/debug/ so it can have channels that can be turned off
Drew Perttula <drewp@bigasterisk.com>
parents: 1815
diff changeset
5
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
6 interface Config {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
7 // getTarget -> vec2 of current target position
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
8 getTarget: () => Vector;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
9 // getSuggestedTargetOffset -> vec2 pixel offset from target
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
10 getSuggestedTargetOffset: () => Vector;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
11 // emptyBox -> true if you want no value display
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
12 emptyBox: boolean;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
13 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
15 export class Adjustable {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
16 config: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
17 handle: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
18 initialTarget: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
19 targetDraggedTo: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
20 root: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
21 // Some value you can edit in the UI, probably by dragging
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
22 // stuff. Drawn by light9-adjusters-canvas. This object does the
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
23 // layout and positioning.
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
24 //
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
25 // The way dragging should work is that you start in the yellow *adj
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
26 // widget*, wherever it is, but your drag is moving the *target*. The
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
27 // adj will travel around too, but it may do extra moves to not bump
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
28 // into stuff or to get out from under your finger.
1753
3c997bc6d380 fix some coffee lint
Drew Perttula <drewp@bigasterisk.com>
parents: 1740
diff changeset
29
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
30 constructor(config: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
31 this.config = config;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
32 this.ctor2();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
33 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
34
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
35 ctor2() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
36 // updated later by layout algoritm
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
37 return (this.handle = $V([0, 0]));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
38 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
39
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
40 getDisplayValue() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
41 if (this.config.emptyBox) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
42 return "";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
43 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
44 const defaultFormat = d3.format(".4g")(this._getValue());
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
45 if (this.config.getDisplayValue != null) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
46 return this.config.getDisplayValue(this._getValue(), defaultFormat);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
47 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
48 return defaultFormat;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
49 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
50 _getValue(): any {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
51 throw new Error("Method not implemented.");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
52 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
53
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
54 getSuggestedHandle() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
55 return this.getTarget().add(this.config.getSuggestedTargetOffset());
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
56 }
1753
3c997bc6d380 fix some coffee lint
Drew Perttula <drewp@bigasterisk.com>
parents: 1740
diff changeset
57
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
58 getHandle() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
59 // vec2 of pixels
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
60 return this.handle;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
61 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
62
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
63 getTarget() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
64 // vec2 of pixels
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
65 return this.config.getTarget();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
66 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
67
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
68 subscribe(onChange: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
69 // change could be displayValue or center or target. This likely
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
70 // calls onChange right away if there's any data yet.
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
71 throw new Error("not implemented");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
72 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
73
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
74 startDrag() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
75 return (this.initialTarget = this.getTarget());
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
76 }
1753
3c997bc6d380 fix some coffee lint
Drew Perttula <drewp@bigasterisk.com>
parents: 1740
diff changeset
77
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
78 continueDrag(pos: { add: (arg0: any) => any }) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
79 //# pos is vec2 of pixels relative to the drag start
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
80 return (this.targetDraggedTo = pos.add(this.initialTarget));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
81 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
82
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
83 endDrag() {}
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
84 // override
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
85
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
86 _editorCoordinates() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
87 // vec2 of mouse relative to <l9-t-editor>
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
88 let rootElem: { getBoundingClientRect: () => any };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
89 return this.targetDraggedTo;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
90 // let ev = d3.event.sourceEvent;
1328
e4825767a4bf fixes to RDF adjusters. put graph load in an element.
Drew Perttula <drewp@bigasterisk.com>
parents: 1327
diff changeset
91
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
92 // if (ev.target.tagName === "LIGHT9-TIMELINE-EDITOR") {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
93 // rootElem = ev.target;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
94 // } else {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
95 // rootElem = ev.target.closest("light9-timeline-editor");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
96 // }
1328
e4825767a4bf fixes to RDF adjusters. put graph load in an element.
Drew Perttula <drewp@bigasterisk.com>
parents: 1327
diff changeset
97
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
98 // if (ev.touches != null ? ev.touches.length : undefined) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
99 // ev = ev.touches[0];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
100 // }
1753
3c997bc6d380 fix some coffee lint
Drew Perttula <drewp@bigasterisk.com>
parents: 1740
diff changeset
101
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
102 // // storing root on the object to remember it across calls in case
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
103 // // you drag outside the editor.
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
104 // if (rootElem) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
105 // this.root = rootElem.getBoundingClientRect();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
106 // }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
107 // const offsetParentPos = $V([ev.pageX - this.root.left, ev.pageY - this.root.top]);
1328
e4825767a4bf fixes to RDF adjusters. put graph load in an element.
Drew Perttula <drewp@bigasterisk.com>
parents: 1327
diff changeset
108
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
109 // return offsetParentPos;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
110 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
111 }
1328
e4825767a4bf fixes to RDF adjusters. put graph load in an element.
Drew Perttula <drewp@bigasterisk.com>
parents: 1327
diff changeset
112
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
113 class AdjustableFloatObservable extends Adjustable {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
114 constructor(config: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
115 // config also has:
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
116 // observable -> ko.observable we will read and write
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
117 // getValueForPos(pos) -> what should we set to if the user
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
118 // moves target to this coord?
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
119 this.config = config;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
120 super();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
121 this.ctor2();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
122 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
123
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
124 _getValue() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
125 return this.config.observable();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
126 }
1753
3c997bc6d380 fix some coffee lint
Drew Perttula <drewp@bigasterisk.com>
parents: 1740
diff changeset
127
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
128 continueDrag(pos: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
129 // pos is vec2 of pixels relative to the drag start.
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
130 super.continueDrag(pos);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
131 const epos = this._editorCoordinates();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
132 const newValue = this.config.getValueForPos(epos);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
133 return this.config.observable(newValue);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
134 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
135
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
136 subscribe(onChange: () => any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
137 log("AdjustableFloatObservable subscribe", this.config);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
138 return ko.computed(() => {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
139 this.config.observable();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
140 return onChange();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
141 });
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
142 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
143 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
144
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
145 class AdjustableFloatObject extends Adjustable {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
146 _currentValue: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
147 _onChange: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
148 constructor(config: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
149 // config also has:
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
150 // graph
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
151 // subj
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
152 // pred
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
153 // ctx
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
154 // getTargetPosForValue(value) -> getTarget result for value
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
155 // getValueForPos
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
156 this.config = config;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
157 super();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
158 this.ctor2();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
159 if (this.config.ctx == null) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
160 throw new Error("missing ctx");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
161 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
162 // this seems to not fire enough.
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
163 this.config.graph.runHandler(this._syncValue.bind(this), `adj sync ${this.config.subj.value} ${this.config.pred.value}`);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
164 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
165
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
166 _syncValue() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
167 this._currentValue = this.config.graph.floatValue(this.config.subj, this.config.pred);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
168 if (this._onChange) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
169 return this._onChange();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
170 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
171 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
172
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
173 _getValue() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
174 // this is a big speedup- callers use _getValue about 4x as much as
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
175 // the graph changes and graph.floatValue is slow
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
176 return this._currentValue;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
177 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
178
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
179 getTarget() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
180 return this.config.getTargetPosForValue(this._getValue());
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
181 }
1385
748a2e8afd30 rdf graph adjuster, use new runHandler api
Drew Perttula <drewp@bigasterisk.com>
parents: 1380
diff changeset
182
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
183 subscribe(onChange: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
184 // only works on one subscription at a time
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
185 if (this._onChange) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
186 throw new Error("multi subscribe not implemented");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
187 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
188 return (this._onChange = onChange);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
189 }
1327
d6396679c121 move to adjustable.coffee and also simplify the getTarget code
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
190
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
191 continueDrag(pos: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
192 // pos is vec2 of pixels relative to the drag start
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
193 super.continueDrag(pos);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
194 const newValue = this.config.getValueForPos(this._editorCoordinates());
1753
3c997bc6d380 fix some coffee lint
Drew Perttula <drewp@bigasterisk.com>
parents: 1740
diff changeset
195
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
196 return this.config.graph.patchObject(this.config.subj, this.config.pred, this.config.graph.LiteralRoundedFloat(newValue), this.config.ctx);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
197 //@_syncValue()
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
198 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
199 }
1753
3c997bc6d380 fix some coffee lint
Drew Perttula <drewp@bigasterisk.com>
parents: 1740
diff changeset
200
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
201 class AdjustableFade extends Adjustable {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
202 yForV: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
203 zoomInX: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
204 i0: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
205 i1: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
206 note: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
207 constructor(yForV: any, zoomInX: any, i0: any, i1: any, note: any, offset: any, ctx: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
208 this.yForV = yForV;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
209 this.zoomInX = zoomInX;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
210 this.i0 = i0;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
211 this.i1 = i1;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
212 this.note = note;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
213 super();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
214 this.config = {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
215 getSuggestedTargetOffset() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
216 return offset;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
217 },
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
218 getTarget: this.getTarget.bind(this),
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
219 ctx,
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
220 };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
221 this.ctor2();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
222 }
1753
3c997bc6d380 fix some coffee lint
Drew Perttula <drewp@bigasterisk.com>
parents: 1740
diff changeset
223
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
224 getTarget() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
225 const mid = this.note.midPoint(this.i0, this.i1);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
226 return $V([this.zoomInX(mid.e(1)), this.yForV(mid.e(2))]);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
227 }
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
228
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
229 _getValue() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
230 return this.note.midPoint(this.i0, this.i1).e(1);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
231 }
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
232
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
233 continueDrag(pos: { e: (arg0: number) => any }) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
234 // pos is vec2 of pixels relative to the drag start
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
235 super.continueDrag(pos);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
236 const { graph } = this.note;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
237 const U = (x: string) => graph.Uri(x);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
238
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
239 const goalCenterSec = this.zoomInX.invert(this.initialTarget.e(1) + pos.e(1));
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
240
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
241 const diamSec = this.note.worldPts[this.i1].e(1) - this.note.worldPts[this.i0].e(1);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
242 const newSec0 = goalCenterSec - diamSec / 2;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
243 const newSec1 = goalCenterSec + diamSec / 2;
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
244
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
245 const originSec = graph.floatValue(this.note.uri, U(":originTime"));
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
246
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
247 const p0 = this._makePatch(graph, this.i0, newSec0, originSec, this.config.ctx);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
248 const p1 = this._makePatch(graph, this.i1, newSec1, originSec, this.config.ctx);
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
249
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
250 return graph.applyAndSendPatch(this._addPatches(p0, p1));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
251 }
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
252
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
253 _makePatch(
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
254 graph: { getObjectPatch: (arg0: any, arg1: any, arg2: any, arg3: any) => any; Uri: (arg0: string) => any; LiteralRoundedFloat: (arg0: number) => any },
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
255 idx: string | number,
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
256 newSec: number,
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
257 originSec: number,
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
258 ctx: any
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
259 ) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
260 return graph.getObjectPatch(this.note.worldPts[idx].uri, graph.Uri(":time"), graph.LiteralRoundedFloat(newSec - originSec), ctx);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
261 }
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
262
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
263 _addPatches(p0: { addQuads: { concat: (arg0: any) => any }; delQuads: { concat: (arg0: any) => any } }, p1: { addQuads: any; delQuads: any }) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
264 return {
1644
9f7e31bf3f0c new fade adjusters
Drew Perttula <drewp@bigasterisk.com>
parents: 1623
diff changeset
265 addQuads: p0.addQuads.concat(p1.addQuads),
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
266 delQuads: p0.delQuads.concat(p1.delQuads),
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
267 };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
268 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
269 }