forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pane.view.ts
178 lines (142 loc) · 4.32 KB
/
pane.view.ts
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
namespace $.$$ {
export class $mol_plot_pane extends $.$mol_plot_pane {
@ $mol_mem
dimensions() {
const graphs = this.graphs()
let next = new this.$.$mol_vector_2d(
$mol_vector_range_full.inversed,
$mol_vector_range_full.inversed
)
for( let graph of graphs ) {
next = next.expanded2(graph.dimensions())
}
return next
}
@ $mol_mem
size() {
const dims = this.dimensions()
return new this.$.$mol_vector_2d(
( dims.x.max - dims.x.min ) || 1 ,
( dims.y.max - dims.y.min ) || 1 ,
)
}
graph_hue( index : number ) {
return ( 360 + ( this.hue_base() + this.hue_shift() * index ) % 360 ) % 360
}
@ $mol_mem
graphs_colored() {
const graphs = this.graphs_positioned()
for (let index = 0; index < graphs.length; index++) {
graphs[index].hue = () => this.graph_hue( index )
}
return graphs
}
size_real() {
return new this.$.$mol_vector_2d(this.width() , this.height())
}
view_box() {
const size = this.size_real()
return `0 0 ${ size.x } ${ size.y }`
}
@ $mol_mem
scale_limit() {
const {
x: {max: right},
y: {max: top}
} = super.scale_limit()
const gap = this.gap()
const size = this.size()
const real = this.size_real()
const left = + ( real.x - gap.x.min - gap.x.max ) / size.x
const bottom = - ( real.y - gap.y.max - gap.y.min) / size.y
return new this.$.$mol_vector_2d(
new this.$.$mol_vector_range(left, right),
new this.$.$mol_vector_range(bottom, top),
)
}
scale_default() {
const limits = this.scale_limit()
return [limits.x.min, limits.y.min] as const
}
@ $mol_mem
scale(next?: readonly [number, number], force?: $mol_mem_force) : readonly [number, number] {
if (next === undefined) {
if (!this.graph_touched) return this.scale_default()
next = $mol_mem_cached( ()=> this.scale() ) || this.scale_default()
}
this.graph_touched = true
return new this.$.$mol_vector_2d( ...next ).limited(this.scale_limit())
}
scale_x(next?: number): number {
return this.scale( next === undefined ? undefined : [ next , this.scale()[1] ] )[0]
}
scale_y(next?: number): number {
return this.scale( next === undefined ? undefined : [ this.scale()[0] , next ] )[1]
}
@ $mol_mem
shift_limit() {
const dims = this.dimensions()
const [scale_x, scale_y] = this.scale()
const size = this.size_real()
const gap = this.gap()
const left = gap.x.min - dims.x.min * scale_x
const right = size.x - gap.x.max - dims.x.max * scale_x
const top = gap.y.max - dims.y.max * scale_y
const bottom = size.y - gap.y.min - dims.y.min * scale_y
return new this.$.$mol_vector_2d(
new this.$.$mol_vector_range(right, left),
new this.$.$mol_vector_range(bottom, top),
)
}
@ $mol_mem
shift_default() {
const limits = this.shift_limit()
return [limits.x.min, limits.y.min] as const
}
graph_touched: boolean = false
@ $mol_mem
shift(next?: readonly [number, number], force?: $mol_mem_force) : readonly [number, number]{
if (next === undefined) {
if (!this.graph_touched) return this.shift_default()
next = $mol_mem_cached( ()=> this.shift() ) || this.shift_default()
}
this.graph_touched = true
return new this.$.$mol_vector_2d( ...next! ).limited(this.shift_limit())
}
reset(event?: Event) {
this.graph_touched = false
this.scale(this.scale_default(), $mol_mem_force_cache)
this.shift(this.shift_default(), $mol_mem_force_cache)
}
@ $mol_mem
graphs_positioned() {
const graphs = this.graphs()
for (let graph of graphs) {
graph.shift = ()=> this.shift()
graph.scale = ()=> this.scale()
graph.dimensions_pane = () => this.dimensions()
graph.viewport = () => this.viewport()
graph.size_real = ()=> this.size_real()
graph.cursor_position = ()=> this.cursor_position()
graph.gap = () => this.gap()
}
return graphs
}
@ $mol_mem
viewport() {
const size = this.size_real()
return new this.$.$mol_vector_2d(
new this.$.$mol_vector_range(0, size.x),
new this.$.$mol_vector_range(0, size.y),
)
}
@ $mol_mem
graphs_sorted() {
const graphs = this.graphs_colored()
const sorted = [] as $.$mol_svg[]
for( let graph of graphs ) sorted.push(...graph.back())
for( let graph of graphs ) sorted.push(...graph.front())
return sorted
}
}
}