-
Notifications
You must be signed in to change notification settings - Fork 22
/
EasyBindingBlock.ts
293 lines (225 loc) · 8.11 KB
/
EasyBindingBlock.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import Type from "./Type";
import Updater from "./Updater";
import Curve from "./Curve";
import BindingBlock from "./BindingBlock";
import Binder from "./Binder";
import AnimationError from "./AnimationError";
import TypeMismatchError from "./TypeMismatchError";
/**
* アニメーションパラメータ設定のヘルパークラス
*/
class EasyBindingBlock extends BindingBlock
{
private _entries: Map<string, EasyBindingBlock.Entry>;
private _bounds: Map<string, Binder>;
private _descendant_unbinders: EasyBindingBlock.DescendantUnbinder[];
/**
*/
constructor()
{
super();
// アニメーション可能なパラメータ
this._entries = new Map<string, EasyBindingBlock.Entry>(); // Map<id, Entry>
// 結合中のパラメータ
this._bounds = new Map<string, Binder>(); // Map<id, Binder>
// すべての子孫の結合を解除するための関数のリスト
this._descendant_unbinders = []; // DescendantUnbinder[]
// 不変条件: this._bounds.has( id ) ⇒ this._entries.has( id )
// this._bounds.has( id ) ⇔ (this._bounds.get( id ) !== undefined) ⇔ this.isBound()
}
/**
* アニメーション可能パラメータを追加
*
* 識別子を id としてアニメーション可能なパラメータを登録する。
*
* types にはこのパラメータに結合可能なアニメーション関数の 1 つまたはそれ以上の型を配列で与える。
*
* types に 2 つ以上の型が存在するときは type_solver に型を決定する関数を指定しなければならない。
* 1 つしか型が存在しないとき type_solver は無視されるので null を与えてもよい。
*
* setter は実際のパラメータに値を設定する関数である。
*
* id に対応するパラメータがすでに結合されている場合はその結合が解除される。
*
* @param id パラメータ ID
* @param types サポートする型のリスト
* @param type_solver 型決定関数
* @param setter パラメータ設定関数
*
* @see [[BindingBlock.Parameter]]
*/
addEntry( id: string, types: Type[], type_solver: EasyBindingBlock.TypeSolver | null, setter: Binder.Setter )
{
// 上書きで追加
this._entries.set( id, new EasyBindingBlock.Entry( types, type_solver, setter ) );
// すでに結合されている場合は解除する
const binder = this._bounds.get( id );
if ( binder ) {
binder.unbind();
this._bounds.delete( id );
}
}
/**
* 子孫の結合を解除するための関数を追加
*
* @param unbinder 子孫の結合を解除するための関数
*
* @see [[BindingBlock.unbindAllRecursively]]
*/
addDescendantUnbinder( unbinder: EasyBindingBlock.DescendantUnbinder )
{
this._descendant_unbinders.push( unbinder );
}
/**
*/
override enumSupportedParameters()
{
let parameters = [];
for ( let [id, enrty] of this._entries ) {
parameters.push( new BindingBlock.Parameter( id, enrty.types ) );
}
return parameters;
}
/**
*/
override isBound( id: string )
{
// 不変条件により !this._entries.has( id ) ⇒ !this._bounds.has( id ) が
// 成り立つので、id がアニメーションに対応していないときは仕様通り false を返す
return this._bounds.has( id );
}
/**
*/
override getBoundUpdater( id: string )
{
const binder = this._bounds.get( id );
return (binder !== undefined) ? binder.updater : undefined;
}
/**
*/
override getBoundCurve( id: string )
{
const binder = this._bounds.get( id );
return (binder !== undefined) ? binder.curve : undefined;
}
/**
*/
override bind( id: string, updater: Updater, curve: Curve ): void
{
const entry = this._entries.get( id );
if ( entry === undefined ) {
// id のパラメータはアニメーションに非対応
throw new AnimationError( "unsupported parameter" );
}
// すでに結合されている場合は先に解除
this.unbind( id );
// 型を決定
const types = entry.types;
const type = (types.length === 1) ? types[0] : (() => {
if (!entry.type_solver) throw new TypeMismatchError( "" );
return entry.type_solver( curve );
})();
if ( type == null || !curve.isTypeSupported( type ) ) {
// curve は id のパラメータが要求する型に対応できない
throw new TypeMismatchError( "type mismatch error" );
}
// パラメータを結合
this._bounds.set( id, new Binder( updater, curve, type, entry.setter ) );
// assert: this.isBound( id )
// assert: this.getBoundUpdater( id ) === updater
// assert: this.getBoundCurve( id ) === curve
}
/**
*/
override unbind( id: string )
{
let binder = this._bounds.get( id );
if ( binder !== undefined ) {
binder.unbind();
this._bounds.delete( id );
}
// assert: !this.isBound( id )
}
/**
*/
override unbindAll()
{
for ( let [/*id*/, binder] of this._bounds ) {
binder.unbind();
}
this._bounds.clear();
// assert: 任意の id に対して !this.isBound( id )
}
/**
*/
override unbindAllRecursively()
{
// 子孫
for ( let unbinder of this._descendant_unbinders ) {
unbinder();
}
// 自身
this.unbindAll();
}
}
namespace EasyBindingBlock {
/**
* パラメータ情報
*
* @memberof mapray.animation.EasyBindingBlock
*/
export class Entry {
types: Type[]
type_solver?: EasyBindingBlock.TypeSolver;
setter: Binder.Setter;
/**
* @param types サポートする型のリスト
* @param type_solver 型決定関数
* @param setter パラメータ設定関数
*/
constructor( types: Type[], type_solver: EasyBindingBlock.TypeSolver | null, setter: Binder.Setter )
{
if ( types.length < 1 || (types.length >= 2 && !type_solver) ) {
// 型は 1 つ以上で、2 つ以上のときは TypeSolver が必要
// これは事前条件であるが、気付き用に投げる
throw new AnimationError( "bad parameter entry" );
}
this.types = types.slice(); // 複製
this.type_solver = type_solver || undefined;
this.setter = setter;
}
}
/**
* 型決定関数
*
* ここで説明する types と setter は [[EasyBindingBlock.addEntry addEntry()]]
* の引数、curve は [[EasyBindingBlock.bind bind()]] の引数である。
*
* types と curve がサポートする型から、setter 関数に渡されるパラメータの型 (curve から得る関数値の型も同じ)
* を決定して返す。
*
* この関数は types に含まれる型、かつ curve がサポートする型以外は返さない。そのような型が決定できなければ
* null を返す。
*
* この関数は types に複数の型を指定したときに、bind() の呼び出しのタイミングで呼び出される。types
* に 1 つの型しか存在しないときは呼び出されない。
*
* @param curve
*
* @see [[EasyBindingBlock.addEntry]]
* @see [[EasyBindingBlock.bind]]
* @see [[Curve.isTypeSupported]]
* @see [[Binder.Setter]]
*/
export type TypeSolver = (curve: Curve) => Type | null
/**
* 子孫の結合を解除するための関数
*
* 一般的な実装では、直接の子オブジェクトの animation.unbindAllRecursively() を呼び出す。
*
* @see [[EasyBindingBlock.addDescendantUnbinder]]
* @see [[BindingBlock.unbindAllRecursively]]
*/
export type DescendantUnbinder = () => void;
} // namespace EasyBindingBlock
export default EasyBindingBlock;