annotate web/timeline/adjusters.ts @ 2435:207fe0670952

+ bin/rdf2dot
author drewp@bigasterisk.com
date Wed, 29 May 2024 14:56:58 -0700
parents 4556eebe5d73
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 { debug } from "debug";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
2 import { LitElement } from "lit";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
3 import { customElement } from "lit/decorators.js";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
4 import { throttle } from "underscore";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
5 import * as d3 from "d3";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
6 import { Adjustable } from "./adjustable";
2128
e2ed5ce36253 double spectrum views have a connected cursor
drewp@bigasterisk.com
parents: 2062
diff changeset
7 import * as Drawing from "../drawing";
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
8 // https://www.npmjs.com/package/@types/sylvester Global values: $L, $M, $P, $V, Line, Matrix, Plane, Sylvester, Vector
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
9 const log = debug("adjusters");
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
11 const maxDist = 60;
1756
a73468c02bce new adjuster layout algorithm
Drew Perttula <drewp@bigasterisk.com>
parents: 1755
diff changeset
12
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
13 interface Drag {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
14 start: Vector;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
15 adj: Adjustable;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
16 cur?: Vector;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
17 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
18 type QTreeData = Vector & { adj: Adjustable };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
19 @customElement("light9-adjusters-canvas")
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
20 class AdjustersCanvas extends LitElement {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
21 static getter_properties: { setAdjuster: { type: any; notify: boolean } };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
22 static getter_observers: {};
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
23 redraw: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
24 adjs: { [id: string | number]: Adjustable };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
25 hoveringNear: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
26 ctx: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
27 $: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
28 setAdjuster: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
29 offsetParent: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
30 currentDrag?: Drag;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
31 qt?: d3.Quadtree<QTreeData>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
32 canvasCenter: any;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
33 static initClass() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
34 this.getter_properties = { setAdjuster: { type: Function, notify: true } };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
35 this.getter_observers = ["updateAllCoords(adjs)"];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
36 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
37 constructor() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
38 super();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
39 this.redraw = throttle(this._throttledRedraw.bind(this), 30, { leading: false });
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
40 this.adjs = {};
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
41 this.hoveringNear = null;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
42 }
1737
849599175e99 adjusters start displaying again. just timeline zoom ones.
Drew Perttula <drewp@bigasterisk.com>
parents: 1736
diff changeset
43
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
44 ready() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
45 this.addEventListener("iron-resize", this.resizeUpdate.bind(this));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
46 this.ctx = this.$.canvas.getContext("2d");
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 this.redraw();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
49 this.setAdjuster = this._setAdjuster.bind(this);
1766
5f9d22f9c85b fix adjuster-drag coordinate bug. highlight nearby adj.
drewp@bigasterisk.com
parents: 1761
diff changeset
50
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
51 // These don't fire; TimelineEditor calls the handlers for us.
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
52 this.addEventListener("mousedown", this.onDown.bind(this));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
53 this.addEventListener("mousemove", this.onMove.bind(this));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
54 return this.addEventListener("mouseup", this.onUp.bind(this));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
55 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
56 addEventListener(arg0: string, arg1: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
57 throw new Error("Method not implemented.");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
58 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
59
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
60 _mousePos(ev: MouseEvent) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
61 return $V([ev.clientX, ev.clientY - this.offsetParent.offsetTop]);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
62 }
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
63
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
64 onDown(ev: MouseEvent) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
65 if (ev.buttons === 1) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
66 const start = this._mousePos(ev);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
67 const adj = this._adjAtPoint(start);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
68 if (adj) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
69 ev.stopPropagation();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
70 this.currentDrag = { start, adj };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
71 return adj.startDrag();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
72 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
73 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
74 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
75
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
76 onMove(ev: MouseEvent) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
77 const pos = this._mousePos(ev);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
78 if (this.currentDrag) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
79 this.hoveringNear = null;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
80 this.currentDrag.cur = pos;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
81 this.currentDrag.adj.continueDrag(this.currentDrag.cur.subtract(this.currentDrag.start));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
82 this.redraw();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
83 } else {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
84 const near = this._adjAtPoint(pos);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
85 if (this.hoveringNear !== near) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
86 this.hoveringNear = near;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
87 this.redraw();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
88 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
89 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
90 }
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
91
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
92 onUp(ev: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
93 if (!this.currentDrag) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
94 return;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
95 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
96 this.currentDrag.adj.endDrag();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
97 this.currentDrag = undefined;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
98 }
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
99
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
100 _setAdjuster(adjId: string | number, makeAdjustable?: () => Adjustable) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
101 // callers register/unregister the Adjustables they want us to make
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
102 // adjuster elements for. Caller invents adjId. makeAdjustable is
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
103 // a function returning the Adjustable or it is undefined to clear any
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
104 // adjusters with this id.
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
105 if (makeAdjustable == null) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
106 if (this.adjs[adjId]) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
107 delete this.adjs[adjId];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
108 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
109 } else {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
110 // this might be able to reuse an existing one a bit
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
111 const adj = makeAdjustable();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
112 this.adjs[adjId] = adj;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
113 adj.id = adjId;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
114 }
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
115
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
116 this.redraw();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
117
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
118 (window as any).debug_adjsCount = Object.keys(this.adjs).length;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
119 }
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
120
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
121 updateAllCoords() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
122 this.redraw();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
123 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
124
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
125 _adjAtPoint(pt: Vector): Adjustable|undefined {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
126 const nearest = this.qt!.find(pt.e(1), pt.e(2));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
127 if (nearest == null || nearest.distanceFrom(pt) > maxDist) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
128 return undefined;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
129 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
130 return nearest != null ? nearest.adj : undefined;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
131 }
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
132
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
133 resizeUpdate(ev: { target: { offsetWidth: any; offsetHeight: any } }) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
134 this.$.canvas.width = ev.target.offsetWidth;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
135 this.$.canvas.height = ev.target.offsetHeight;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
136 this.canvasCenter = $V([this.$.canvas.width / 2, this.$.canvas.height / 2]);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
137 return this.redraw();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
138 }
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
139
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
140 _throttledRedraw() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
141 if (this.ctx == null) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
142 return;
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 console.time("adjs redraw");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
145 this._layoutCenters();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
146
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
147 this.ctx.clearRect(0, 0, this.$.canvas.width, this.$.canvas.height);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
148
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
149 for (let adjId in this.adjs) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
150 const adj = this.adjs[adjId];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
151 const ctr = adj.getHandle();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
152 const target = adj.getTarget();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
153 if (this._isOffScreen(target)) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
154 continue;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
155 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
156 this._drawConnector(ctr, target);
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
157
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
158 this._drawAdjuster(adj.getDisplayValue(), ctr.e(1) - 20, ctr.e(2) - 10, ctr.e(1) + 20, ctr.e(2) + 10, adj === this.hoveringNear);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
159 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
160 return console.timeEnd("adjs redraw");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
161 }
1761
3ddd5b4964b3 fix time adjs crossing each other. more simulation steps for adjs.
drewp@bigasterisk.com
parents: 1756
diff changeset
162
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
163 _layoutCenters() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
164 // push Adjustable centers around to avoid overlaps
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
165 // Todo: also don't overlap inlineattr boxes
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
166 // Todo: don't let their connector lines cross each other
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
167 const qt = d3.quadtree<QTreeData>(
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
168 [],
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
169 (d: QTreeData) => d.e(1),
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
170 (d: QTreeData) => d.e(2)
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 this.qt = qt;
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
173
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
174 qt.extent([
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
175 [0, 0],
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
176 [8000, 8000],
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 let _: string | number, adj: { handle: any; getSuggestedHandle: () => any };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
180 for (_ in this.adjs) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
181 adj = this.adjs[_];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
182 adj.handle = this._clampOnScreen(adj.getSuggestedHandle());
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
183 }
1761
3ddd5b4964b3 fix time adjs crossing each other. more simulation steps for adjs.
drewp@bigasterisk.com
parents: 1756
diff changeset
184
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
185 const numTries = 8;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
186 for (let tryn = 0; tryn < numTries; tryn++) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
187 for (_ in this.adjs) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
188 adj = this.adjs[_];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
189 let current = adj.handle;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
190 qt.remove(current);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
191 const nearest = qt.find(current.e(1), current.e(2), maxDist);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
192 if (nearest) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
193 const dist = current.distanceFrom(nearest);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
194 if (dist < maxDist) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
195 current = this._stepAway(current, nearest, 1 / numTries);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
196 adj.handle = current;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
197 }
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 current.adj = adj;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
200 qt.add(current);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
201 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
202 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
203 //if -50 < output.e(1) < 20 # mostly for zoom-left
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
204 // output.setElements([
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
205 // Math.max(20, output.e(1)),
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
206 // output.e(2)])
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
207 }
1761
3ddd5b4964b3 fix time adjs crossing each other. more simulation steps for adjs.
drewp@bigasterisk.com
parents: 1756
diff changeset
208
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
209
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
210 _stepAway(
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
211 current: Vector,
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
212 nearest: Vector,
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
213 dx: number
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
214 ) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
215 const away = current.subtract(nearest).toUnitVector();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
216 const toScreenCenter = this.canvasCenter.subtract(current).toUnitVector();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
217 const goalSpacingPx = 20;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
218 return this._clampOnScreen(current.add(away.x(goalSpacingPx * dx)));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
219 }
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 _isOffScreen(pos: Vector):boolean {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
222 return pos.e(1) < 0 || pos.e(1) > this.$.canvas.width || pos.e(2) < 0 || pos.e(2) > this.$.canvas.height;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
223 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
224
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
225 _clampOnScreen(pos: Vector): Vector {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
226 const marg = 30;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
227 return $V([Math.max(marg, Math.min(this.$.canvas.width - marg, pos.e(1))), Math.max(marg, Math.min(this.$.canvas.height - marg, pos.e(2)))]);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
228 }
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
229
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
230 _drawConnector(ctr: Vector, target: Vector) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
231 this.ctx.strokeStyle = "#aaa";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
232 this.ctx.lineWidth = 2;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
233 this.ctx.beginPath();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
234 Drawing.line(this.ctx, ctr, target);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
235 this.ctx.stroke();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
236 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
237
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
238 _drawAdjuster(label: any, x1: number, y1: number, x2: number, y2: number, hover: boolean) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
239 const radius = 8;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
240
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
241 this.ctx.shadowColor = "black";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
242 this.ctx.shadowBlur = 15;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
243 this.ctx.shadowOffsetX = 5;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
244 this.ctx.shadowOffsetY = 9;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
245
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
246 this.ctx.fillStyle = hover ? "#ffff88" : "rgba(255, 255, 0, 0.5)";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
247 this.ctx.beginPath();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
248 Drawing.roundRect(this.ctx, x1, y1, x2, y2, radius);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
249 this.ctx.fill();
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
250
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
251 this.ctx.shadowColor = "rgba(0,0,0,0)";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
252
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
253 this.ctx.strokeStyle = "yellow";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
254 this.ctx.lineWidth = 2;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
255 this.ctx.setLineDash([3, 3]);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
256 this.ctx.beginPath();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
257 Drawing.roundRect(this.ctx, x1, y1, x2, y2, radius);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
258 this.ctx.stroke();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
259 this.ctx.setLineDash([]);
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
260
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
261 this.ctx.font = "12px sans";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
262 this.ctx.fillStyle = "#000";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
263 this.ctx.fillText(label, x1 + 5, y2 - 5, x2 - x1 - 10);
1716
0dd23a0cbba1 extract adjusters.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
264
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
265 // coords from a center that's passed in
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
266 // # special layout for the thaeter ones with middinh
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
267 // l/r arrows
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
268 // mouse arrow cursor upon hover, and accent the hovered adjuster
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
269 // connector
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
270 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
271 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
272
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
273