@mulsense/xnew 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +46 -0
  3. package/dist/addons/xmatter.d.ts +6 -0
  4. package/dist/addons/xmatter.js +45 -0
  5. package/dist/addons/xmatter.mjs +40 -0
  6. package/dist/addons/xpixi.d.ts +10 -0
  7. package/dist/addons/xpixi.js +101 -0
  8. package/dist/addons/xpixi.mjs +77 -0
  9. package/dist/addons/xrapier2d.d.ts +6 -0
  10. package/dist/addons/xrapier2d.js +66 -0
  11. package/dist/addons/xrapier2d.mjs +61 -0
  12. package/dist/addons/xthree.d.ts +9 -0
  13. package/dist/addons/xthree.js +77 -0
  14. package/dist/addons/xthree.mjs +53 -0
  15. package/dist/types/audio/audio.d.ts +13 -0
  16. package/dist/types/audio/loader.d.ts +1 -0
  17. package/dist/types/audio/synthesizer.d.ts +68 -0
  18. package/dist/types/basics/Accordion.d.ts +20 -0
  19. package/dist/types/basics/Block.d.ts +24 -0
  20. package/dist/types/basics/Bullet.d.ts +7 -0
  21. package/dist/types/basics/ControlPanel.d.ts +7 -0
  22. package/dist/types/basics/Controller.d.ts +28 -0
  23. package/dist/types/basics/Event.d.ts +3 -0
  24. package/dist/types/basics/File.d.ts +1 -0
  25. package/dist/types/basics/Input.d.ts +2 -0
  26. package/dist/types/basics/Modal.d.ts +11 -0
  27. package/dist/types/basics/Navigation.d.ts +1 -0
  28. package/dist/types/basics/Panel.d.ts +6 -0
  29. package/dist/types/basics/Popup.d.ts +8 -0
  30. package/dist/types/basics/ResizeEvent.d.ts +1 -0
  31. package/dist/types/basics/Screen.d.ts +13 -0
  32. package/dist/types/basics/SubWIndow.d.ts +6 -0
  33. package/dist/types/basics/Tab.d.ts +12 -0
  34. package/dist/types/basics/TabView.d.ts +18 -0
  35. package/dist/types/basics/Tabs.d.ts +8 -0
  36. package/dist/types/basics/Transition.d.ts +17 -0
  37. package/dist/types/basics/UserEvent.d.ts +2 -0
  38. package/dist/types/basics/WorkSpace.d.ts +16 -0
  39. package/dist/types/core/map.d.ts +34 -0
  40. package/dist/types/core/time.d.ts +27 -0
  41. package/dist/types/core/unit.d.ts +105 -0
  42. package/dist/types/core/util.d.ts +1 -0
  43. package/dist/types/core/xnew.d.ts +9 -0
  44. package/dist/types/index.d.ts +47 -0
  45. package/dist/types/xnew.d.ts +8 -0
  46. package/dist/xnew.d.ts +273 -0
  47. package/dist/xnew.js +2013 -0
  48. package/dist/xnew.mjs +2005 -0
  49. package/package.json +85 -0
package/dist/xnew.mjs ADDED
@@ -0,0 +1,2005 @@
1
+ //----------------------------------------------------------------------------------------------------
2
+ // ticker
3
+ //----------------------------------------------------------------------------------------------------
4
+ class Ticker {
5
+ static ticker() {
6
+ const time = Date.now();
7
+ const interval = 1000 / 60;
8
+ if (time - Ticker.previous > interval * 0.8) {
9
+ Ticker.callbacks.forEach((callback) => callback(time));
10
+ Ticker.previous = time;
11
+ }
12
+ Ticker.animation = requestAnimationFrame(Ticker.ticker);
13
+ }
14
+ static set(callback) {
15
+ if (Ticker.animation === null && typeof requestAnimationFrame === 'function' && typeof cancelAnimationFrame === 'function') {
16
+ Ticker.previous = Date.now();
17
+ Ticker.animation = requestAnimationFrame(Ticker.ticker);
18
+ }
19
+ Ticker.callbacks.add(callback);
20
+ }
21
+ static clear(callback) {
22
+ Ticker.callbacks.delete(callback);
23
+ }
24
+ }
25
+ Ticker.animation = null;
26
+ Ticker.callbacks = new Set;
27
+ Ticker.previous = 0.0;
28
+ //----------------------------------------------------------------------------------------------------
29
+ // timer
30
+ //----------------------------------------------------------------------------------------------------
31
+ class Timer {
32
+ constructor(timeout, transition, delay, loop = false) {
33
+ var _a, _b;
34
+ this.timeout = timeout;
35
+ this.transition = transition;
36
+ this.delay = delay;
37
+ this.loop = loop;
38
+ this.id = null;
39
+ this.time = 0.0;
40
+ this.offset = 0.0;
41
+ this.status = 0;
42
+ this.ticker = (time) => {
43
+ var _a;
44
+ (_a = this.transition) === null || _a === void 0 ? void 0 : _a.call(this, this.elapsed() / this.delay);
45
+ };
46
+ (_a = this.transition) === null || _a === void 0 ? void 0 : _a.call(this, 0.0);
47
+ if (this.delay <= 0) {
48
+ timeout();
49
+ (_b = this.transition) === null || _b === void 0 ? void 0 : _b.call(this, 1.0);
50
+ }
51
+ else {
52
+ if (document instanceof Document) {
53
+ this.visibilitychange = () => document.hidden === false ? this._start() : this._stop();
54
+ document.addEventListener('visibilitychange', this.visibilitychange);
55
+ }
56
+ this.start();
57
+ Ticker.set(this.ticker);
58
+ }
59
+ }
60
+ clear() {
61
+ if (this.id !== null) {
62
+ clearTimeout(this.id);
63
+ this.id = null;
64
+ }
65
+ if (document instanceof Document && this.visibilitychange !== undefined) {
66
+ document.removeEventListener('visibilitychange', this.visibilitychange);
67
+ }
68
+ Ticker.clear(this.ticker);
69
+ }
70
+ elapsed() {
71
+ return this.offset + (this.id !== null ? (Date.now() - this.time) : 0);
72
+ }
73
+ start() {
74
+ this.status = 1;
75
+ this._start();
76
+ }
77
+ stop() {
78
+ this._stop();
79
+ this.status = 0;
80
+ }
81
+ _start() {
82
+ if (this.status === 1 && this.id === null) {
83
+ this.id = setTimeout(() => {
84
+ this.timeout();
85
+ this.id = null;
86
+ this.time = 0.0;
87
+ this.offset = 0.0;
88
+ if (this.loop) {
89
+ this.start();
90
+ }
91
+ }, this.delay - this.offset);
92
+ this.time = Date.now();
93
+ }
94
+ }
95
+ _stop() {
96
+ if (this.status === 1 && this.id !== null) {
97
+ this.offset = this.offset + Date.now() - this.time;
98
+ clearTimeout(this.id);
99
+ this.id = null;
100
+ this.time = 0.0;
101
+ }
102
+ }
103
+ }
104
+
105
+ //----------------------------------------------------------------------------------------------------
106
+ // map ex
107
+ //----------------------------------------------------------------------------------------------------
108
+ class MapEx {
109
+ constructor() {
110
+ this.map = new Map;
111
+ }
112
+ get size() {
113
+ return this.map.size;
114
+ }
115
+ forEach(callback) {
116
+ this.map.forEach(callback);
117
+ }
118
+ clear() {
119
+ this.map.clear();
120
+ }
121
+ }
122
+ //----------------------------------------------------------------------------------------------------
123
+ // map set
124
+ //----------------------------------------------------------------------------------------------------
125
+ class MapSet extends MapEx {
126
+ has(key, value) {
127
+ var _a, _b;
128
+ if (value === undefined) {
129
+ return this.map.has(key);
130
+ }
131
+ else {
132
+ return (_b = (_a = this.map.get(key)) === null || _a === void 0 ? void 0 : _a.has(value)) !== null && _b !== void 0 ? _b : false;
133
+ }
134
+ }
135
+ get(key) {
136
+ return this.map.get(key);
137
+ }
138
+ keys() {
139
+ return this.map.keys();
140
+ }
141
+ add(key, value) {
142
+ var _a;
143
+ this.map.set(key, ((_a = this.map.get(key)) !== null && _a !== void 0 ? _a : new Set).add(value));
144
+ return this;
145
+ }
146
+ delete(key, value) {
147
+ var _a, _b, _c, _d;
148
+ let ret = false;
149
+ if (value === undefined) {
150
+ ret = (((_a = this.map.get(key)) === null || _a === void 0 ? void 0 : _a.size) === 0) ? this.map.delete(key) : false;
151
+ }
152
+ else {
153
+ ret = (_c = (_b = this.map.get(key)) === null || _b === void 0 ? void 0 : _b.delete(value)) !== null && _c !== void 0 ? _c : false;
154
+ (((_d = this.map.get(key)) === null || _d === void 0 ? void 0 : _d.size) === 0) && this.map.delete(key);
155
+ }
156
+ return ret;
157
+ }
158
+ }
159
+ //----------------------------------------------------------------------------------------------------
160
+ // map map
161
+ //----------------------------------------------------------------------------------------------------
162
+ class MapMap extends MapEx {
163
+ has(key1, key2) {
164
+ var _a, _b;
165
+ if (key2 === undefined) {
166
+ return this.map.has(key1);
167
+ }
168
+ else {
169
+ return (_b = (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.has(key2)) !== null && _b !== void 0 ? _b : false;
170
+ }
171
+ }
172
+ set(key1, key2, value) {
173
+ var _a;
174
+ this.map.set(key1, ((_a = this.map.get(key1)) !== null && _a !== void 0 ? _a : new Map).set(key2, value));
175
+ return this;
176
+ }
177
+ get(key1, key2) {
178
+ var _a;
179
+ if (key2 === undefined) {
180
+ return this.map.get(key1);
181
+ }
182
+ else {
183
+ return (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.get(key2);
184
+ }
185
+ }
186
+ keys(key1) {
187
+ var _a, _b;
188
+ if (key1 === undefined) {
189
+ return this.map.keys();
190
+ }
191
+ else {
192
+ return (_b = (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.keys()) !== null && _b !== void 0 ? _b : (function* () { })();
193
+ }
194
+ }
195
+ delete(key1, key2) {
196
+ var _a, _b, _c, _d;
197
+ let ret = false;
198
+ if (key2 === undefined) {
199
+ ret = (((_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.size) === 0) ? this.map.delete(key1) : false;
200
+ }
201
+ else {
202
+ ret = (_c = (_b = this.map.get(key1)) === null || _b === void 0 ? void 0 : _b.delete(key2)) !== null && _c !== void 0 ? _c : false;
203
+ (((_d = this.map.get(key1)) === null || _d === void 0 ? void 0 : _d.size) === 0) && this.map.delete(key1);
204
+ }
205
+ return ret;
206
+ }
207
+ }
208
+ //----------------------------------------------------------------------------------------------------
209
+ // map map map
210
+ //----------------------------------------------------------------------------------------------------
211
+ class MapMapMap extends MapEx {
212
+ has(key1, key2, key3) {
213
+ var _a, _b;
214
+ if (key2 === undefined) {
215
+ return this.map.has(key1);
216
+ }
217
+ else {
218
+ return (_b = (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.has(key2, key3)) !== null && _b !== void 0 ? _b : false;
219
+ }
220
+ }
221
+ set(key1, key2, key3, value) {
222
+ var _a;
223
+ this.map.set(key1, ((_a = this.map.get(key1)) !== null && _a !== void 0 ? _a : new MapMap).set(key2, key3, value));
224
+ return this;
225
+ }
226
+ get(key1, key2, key3) {
227
+ var _a, _b;
228
+ if (key2 === undefined) {
229
+ return this.map.get(key1);
230
+ }
231
+ else if (key3 === undefined) {
232
+ return (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.get(key2);
233
+ }
234
+ else {
235
+ return (_b = this.map.get(key1)) === null || _b === void 0 ? void 0 : _b.get(key2, key3);
236
+ }
237
+ }
238
+ keys(key1, key2) {
239
+ var _a, _b, _c, _d, _e;
240
+ if (key1 === undefined) {
241
+ return this.map.keys();
242
+ }
243
+ else if (key2 === undefined) {
244
+ return (_b = (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.keys()) !== null && _b !== void 0 ? _b : (function* () { })();
245
+ }
246
+ else {
247
+ return (_e = (_d = (_c = this.map.get(key1)) === null || _c === void 0 ? void 0 : _c.get(key2)) === null || _d === void 0 ? void 0 : _d.keys()) !== null && _e !== void 0 ? _e : (function* () { })();
248
+ }
249
+ }
250
+ delete(key1, key2, key3) {
251
+ var _a, _b, _c, _d;
252
+ let ret = false;
253
+ if (key2 === undefined) {
254
+ ret = (((_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.size) === 0) ? this.map.delete(key1) : false;
255
+ }
256
+ else {
257
+ ret = (_c = (_b = this.map.get(key1)) === null || _b === void 0 ? void 0 : _b.delete(key2, key3)) !== null && _c !== void 0 ? _c : false;
258
+ (((_d = this.map.get(key1)) === null || _d === void 0 ? void 0 : _d.size) === 0) && this.map.delete(key1);
259
+ }
260
+ return ret;
261
+ }
262
+ }
263
+
264
+ //----------------------------------------------------------------------------------------------------
265
+ // Constants
266
+ //----------------------------------------------------------------------------------------------------
267
+ const LIFECYCLE_EVENTS = ['start', 'update', 'stop', 'finalize'];
268
+ const LIFECYCLE_STATES = {
269
+ INVOKED: 'invoked',
270
+ INITIALIZED: 'initialized',
271
+ STARTED: 'started',
272
+ STOPPED: 'stopped',
273
+ PRE_FINALIZED: 'pre finalized',
274
+ FINALIZED: 'finalized',
275
+ };
276
+ const CUSTOM_EVENT_PREFIX = {
277
+ GLOBAL: '+',
278
+ INTERNAL: '-',
279
+ };
280
+ //----------------------------------------------------------------------------------------------------
281
+ // unit main
282
+ //----------------------------------------------------------------------------------------------------
283
+ class Unit {
284
+ constructor(target, component, props) {
285
+ var _a, _b, _c, _d, _e;
286
+ const parent = UnitScope.current;
287
+ let baseElement;
288
+ if (target instanceof HTMLElement || target instanceof SVGElement) {
289
+ baseElement = target;
290
+ }
291
+ else if (parent !== null) {
292
+ baseElement = (_a = parent._.currentElement) !== null && _a !== void 0 ? _a : parent._.baseElement;
293
+ }
294
+ else {
295
+ baseElement = (_c = (_b = document.currentScript) === null || _b === void 0 ? void 0 : _b.parentElement) !== null && _c !== void 0 ? _c : document.body;
296
+ }
297
+ let baseComponent = null;
298
+ if (typeof component === 'function') {
299
+ baseComponent = component;
300
+ }
301
+ else if (typeof component === 'string') {
302
+ baseComponent = (self) => { self.element.textContent = component; };
303
+ }
304
+ this._ = {
305
+ root: (_d = parent === null || parent === void 0 ? void 0 : parent._.root) !== null && _d !== void 0 ? _d : this,
306
+ parent,
307
+ target,
308
+ baseComponent,
309
+ props,
310
+ baseElement,
311
+ nextNest: { element: baseElement, position: 'beforeend' },
312
+ baseContext: UnitScope.get(parent),
313
+ };
314
+ ((_e = parent === null || parent === void 0 ? void 0 : parent._.children) !== null && _e !== void 0 ? _e : Unit.roots).push(this);
315
+ Unit.initialize(this);
316
+ }
317
+ get element() {
318
+ return this._.currentElement;
319
+ }
320
+ start() {
321
+ this._.tostart = true;
322
+ }
323
+ stop() {
324
+ this._.tostart = false;
325
+ Unit.stop(this);
326
+ }
327
+ finalize() {
328
+ Unit.stop(this);
329
+ Unit.finalize(this);
330
+ if (this._.parent) {
331
+ this._.parent._.children = this._.parent._.children.filter((unit) => unit !== this);
332
+ }
333
+ else {
334
+ Unit.roots = Unit.roots.filter((unit) => unit !== this);
335
+ }
336
+ }
337
+ reboot() {
338
+ Unit.stop(this);
339
+ let trace = this._.currentElement;
340
+ while (trace.parentElement && trace.parentElement !== this._.baseElement) {
341
+ trace = trace.parentElement;
342
+ }
343
+ if (trace.parentElement === this._.baseElement) {
344
+ this._.nextNest.element = trace.nextElementSibling;
345
+ this._.nextNest.position = 'beforebegin';
346
+ }
347
+ Unit.finalize(this);
348
+ Unit.initialize(this);
349
+ }
350
+ get components() {
351
+ return this._.components;
352
+ }
353
+ on(type, listener, options) {
354
+ try {
355
+ if (typeof type === 'string') {
356
+ const filtered = type.trim().split(/\s+/).filter((type) => LIFECYCLE_EVENTS.includes(type));
357
+ filtered.forEach((type) => {
358
+ this._.system[type].push(listener);
359
+ });
360
+ }
361
+ UnitEvent.on(this, type, listener, options);
362
+ }
363
+ catch (error) {
364
+ console.error('unit.on(type, listener, option?): ', error);
365
+ }
366
+ return this;
367
+ }
368
+ off(type, listener) {
369
+ try {
370
+ if (type === undefined) {
371
+ this._.system = { start: [], update: [], stop: [], finalize: [] };
372
+ }
373
+ else if (typeof type === 'string') {
374
+ const filtered = type.trim().split(/\s+/).filter((type) => LIFECYCLE_EVENTS.includes(type));
375
+ filtered.forEach((type) => {
376
+ if (listener === undefined) {
377
+ this._.system[type] = [];
378
+ }
379
+ else {
380
+ this._.system[type] = this._.system[type].filter((l) => l !== listener);
381
+ }
382
+ });
383
+ }
384
+ UnitEvent.off(this, type, listener);
385
+ }
386
+ catch (error) {
387
+ console.error('unit.off(type, listener): ', error);
388
+ }
389
+ return this;
390
+ }
391
+ emit(type, ...args) {
392
+ try {
393
+ UnitEvent.emit(this, type, ...args);
394
+ }
395
+ catch (error) {
396
+ console.error('unit.emit(type, ...args): ', error);
397
+ }
398
+ }
399
+ //----------------------------------------------------------------------------------------------------
400
+ // internal
401
+ //----------------------------------------------------------------------------------------------------
402
+ static initialize(unit) {
403
+ var _a;
404
+ unit._ = Object.assign(unit._, {
405
+ children: [],
406
+ components: [],
407
+ captures: [],
408
+ state: LIFECYCLE_STATES.INVOKED,
409
+ tostart: true,
410
+ currentElement: unit._.baseElement,
411
+ upcount: 0,
412
+ resolved: false,
413
+ defines: {},
414
+ system: { start: [], update: [], stop: [], finalize: [] },
415
+ });
416
+ UnitScope.initialize(unit, unit._.baseContext);
417
+ // nest html element
418
+ if (typeof unit._.target === 'string') {
419
+ Unit.nest(unit, unit._.target);
420
+ }
421
+ // setup component
422
+ if (typeof unit._.baseComponent === 'function') {
423
+ UnitScope.execute({ unit, context: null, element: null }, () => Unit.extend(unit, unit._.baseComponent, unit._.props));
424
+ }
425
+ // whether the unit promise was resolved
426
+ (_a = UnitPromise.get(unit)) === null || _a === void 0 ? void 0 : _a.then(() => {
427
+ unit._.resolved = true;
428
+ });
429
+ unit._.state = LIFECYCLE_STATES.INITIALIZED;
430
+ let current = unit;
431
+ while (current !== null) {
432
+ let captured = false;
433
+ for (const capture of current._.captures) {
434
+ if (capture.checker(unit)) {
435
+ capture.execute(unit);
436
+ captured = true;
437
+ }
438
+ }
439
+ if (captured === false) {
440
+ current = current._.parent;
441
+ }
442
+ else {
443
+ break;
444
+ }
445
+ }
446
+ }
447
+ static finalize(unit) {
448
+ if (unit._.state !== LIFECYCLE_STATES.FINALIZED && unit._.state !== LIFECYCLE_STATES.PRE_FINALIZED) {
449
+ unit._.state = LIFECYCLE_STATES.PRE_FINALIZED;
450
+ unit._.children.forEach((child) => child.finalize());
451
+ unit._.system.finalize.forEach((listener) => {
452
+ UnitScope.execute(UnitScope.snapshot(unit), listener);
453
+ });
454
+ UnitEvent.off(unit);
455
+ UnitEvent.suboff(unit, null);
456
+ UnitScope.finalize(unit);
457
+ UnitComponent.finalize(unit);
458
+ UnitPromise.finalize(unit);
459
+ while (unit._.currentElement !== unit._.baseElement && unit._.currentElement.parentElement !== null) {
460
+ const parent = unit._.currentElement.parentElement;
461
+ parent.removeChild(unit._.currentElement);
462
+ unit._.currentElement = parent;
463
+ }
464
+ // reset defines
465
+ Object.keys(unit._.defines).forEach((key) => {
466
+ if (!LIFECYCLE_EVENTS.includes(key)) {
467
+ delete unit[key];
468
+ }
469
+ });
470
+ unit._.defines = {};
471
+ unit._.state = LIFECYCLE_STATES.FINALIZED;
472
+ }
473
+ }
474
+ static nest(unit, tag) {
475
+ const match = tag.match(/<((\w+)[^>]*?)\/?>/);
476
+ if (match !== null) {
477
+ let element;
478
+ unit._.nextNest.element.insertAdjacentHTML(unit._.nextNest.position, `<${match[1]}></${match[2]}>`);
479
+ if (unit._.nextNest.position === 'beforebegin') {
480
+ element = unit._.nextNest.element.previousElementSibling;
481
+ }
482
+ else {
483
+ element = unit.element.children[unit.element.children.length - 1];
484
+ }
485
+ unit._.nextNest.element = element;
486
+ unit._.nextNest.position = 'beforeend';
487
+ unit._.currentElement = element;
488
+ }
489
+ return unit.element;
490
+ }
491
+ static extend(unit, component, props) {
492
+ var _a;
493
+ unit._.components.push(component);
494
+ UnitComponent.add(unit, component);
495
+ const defines = (_a = component(unit, props)) !== null && _a !== void 0 ? _a : {};
496
+ const snapshot = UnitScope.snapshot(unit);
497
+ Object.keys(defines).forEach((key) => {
498
+ const descriptor = Object.getOwnPropertyDescriptor(defines, key);
499
+ if (unit[key] !== undefined && unit._.defines[key] === undefined) {
500
+ throw new Error(`The property "${key}" already exists.`);
501
+ }
502
+ const newDescriptor = { configurable: true, enumerable: true };
503
+ if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.get) {
504
+ newDescriptor.get = (...args) => UnitScope.execute(snapshot, descriptor.get, ...args);
505
+ }
506
+ if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.set) {
507
+ newDescriptor.set = (...args) => UnitScope.execute(snapshot, descriptor.set, ...args);
508
+ }
509
+ if (typeof (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) === 'function') {
510
+ newDescriptor.value = (...args) => UnitScope.execute(snapshot, descriptor.value, ...args);
511
+ }
512
+ else if ((descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) !== undefined) {
513
+ newDescriptor.writable = true;
514
+ newDescriptor.value = descriptor.value;
515
+ }
516
+ Object.defineProperty(unit._.defines, key, newDescriptor);
517
+ Object.defineProperty(unit, key, newDescriptor);
518
+ });
519
+ }
520
+ static start(unit, time) {
521
+ if (!unit._.resolved || !unit._.tostart)
522
+ return;
523
+ const { state } = unit._;
524
+ if (state === LIFECYCLE_STATES.INVOKED || state === LIFECYCLE_STATES.INITIALIZED || state === LIFECYCLE_STATES.STOPPED) {
525
+ unit._.state = LIFECYCLE_STATES.STARTED;
526
+ unit._.children.forEach((child) => Unit.start(child, time));
527
+ unit._.system.start.forEach((listener) => {
528
+ UnitScope.execute(UnitScope.snapshot(unit), listener);
529
+ });
530
+ }
531
+ else if (state === LIFECYCLE_STATES.STARTED) {
532
+ unit._.children.forEach((child) => Unit.start(child, time));
533
+ }
534
+ }
535
+ static stop(unit) {
536
+ if (unit._.state === LIFECYCLE_STATES.STARTED) {
537
+ unit._.state = LIFECYCLE_STATES.STOPPED;
538
+ unit._.children.forEach((child) => Unit.stop(child));
539
+ unit._.system.stop.forEach((listener) => {
540
+ UnitScope.execute(UnitScope.snapshot(unit), listener);
541
+ });
542
+ }
543
+ }
544
+ static update(unit, time) {
545
+ if (unit._.state === LIFECYCLE_STATES.STARTED) {
546
+ unit._.children.forEach((child) => Unit.update(child, time));
547
+ if (unit._.state === LIFECYCLE_STATES.STARTED) {
548
+ unit._.system.update.forEach((listener) => {
549
+ UnitScope.execute(UnitScope.snapshot(unit), listener, unit._.upcount);
550
+ });
551
+ unit._.upcount++;
552
+ }
553
+ }
554
+ }
555
+ static ticker(time) {
556
+ Unit.roots.forEach((unit) => {
557
+ Unit.start(unit, time);
558
+ Unit.update(unit, time);
559
+ });
560
+ }
561
+ static reset() {
562
+ Unit.roots.forEach((unit) => unit.finalize());
563
+ Unit.roots = [];
564
+ Ticker.clear(Unit.ticker);
565
+ Ticker.set(Unit.ticker);
566
+ }
567
+ }
568
+ Unit.roots = [];
569
+ Unit.reset();
570
+ //----------------------------------------------------------------------------------------------------
571
+ // unit scope
572
+ //----------------------------------------------------------------------------------------------------
573
+ class UnitScope {
574
+ static initialize(unit, context) {
575
+ if (context !== null) {
576
+ UnitScope.contexts.set(unit, context);
577
+ }
578
+ }
579
+ static finalize(unit) {
580
+ UnitScope.contexts.delete(unit);
581
+ }
582
+ static set(unit, context) {
583
+ UnitScope.contexts.set(unit, context);
584
+ }
585
+ static get(unit) {
586
+ var _a;
587
+ return (_a = UnitScope.contexts.get(unit)) !== null && _a !== void 0 ? _a : null;
588
+ }
589
+ static execute(snapshot, func, ...args) {
590
+ if (!snapshot)
591
+ return;
592
+ const current = UnitScope.current;
593
+ let context = null;
594
+ let element = null;
595
+ try {
596
+ UnitScope.current = snapshot.unit;
597
+ if (snapshot.unit !== null) {
598
+ if (snapshot.context !== null) {
599
+ context = UnitScope.get(snapshot.unit);
600
+ UnitScope.contexts.set(snapshot.unit, snapshot.context);
601
+ }
602
+ if (snapshot.element !== null) {
603
+ element = snapshot.unit._.currentElement;
604
+ snapshot.unit._.currentElement = snapshot.element;
605
+ }
606
+ }
607
+ return func(...args);
608
+ }
609
+ catch (error) {
610
+ throw error;
611
+ }
612
+ finally {
613
+ UnitScope.current = current;
614
+ if (snapshot.unit !== null) {
615
+ if (context !== null) {
616
+ UnitScope.contexts.set(snapshot.unit, context);
617
+ }
618
+ if (element !== null) {
619
+ snapshot.unit._.currentElement = element;
620
+ }
621
+ }
622
+ }
623
+ }
624
+ static snapshot(unit = UnitScope.current) {
625
+ if (unit !== null) {
626
+ return { unit, context: UnitScope.get(unit), element: unit.element };
627
+ }
628
+ return null;
629
+ }
630
+ static stack(unit, key, value) {
631
+ UnitScope.contexts.set(unit, { stack: UnitScope.get(unit), key, value });
632
+ }
633
+ static trace(unit, key) {
634
+ for (let context = UnitScope.get(unit); context !== null; context = context.stack) {
635
+ if (context.key === key) {
636
+ return context.value;
637
+ }
638
+ }
639
+ }
640
+ }
641
+ UnitScope.current = null;
642
+ UnitScope.contexts = new Map();
643
+ //----------------------------------------------------------------------------------------------------
644
+ // unit component
645
+ //----------------------------------------------------------------------------------------------------
646
+ class UnitComponent {
647
+ static finalize(unit) {
648
+ var _a;
649
+ (_a = UnitComponent.components.get(unit)) === null || _a === void 0 ? void 0 : _a.forEach((component) => {
650
+ UnitComponent.units.delete(component, unit);
651
+ });
652
+ UnitComponent.components.delete(unit);
653
+ }
654
+ static add(unit, component) {
655
+ UnitComponent.components.add(unit, component);
656
+ UnitComponent.units.add(component, unit);
657
+ }
658
+ static find(component) {
659
+ var _a;
660
+ return [...((_a = UnitComponent.units.get(component)) !== null && _a !== void 0 ? _a : [])];
661
+ }
662
+ }
663
+ UnitComponent.components = new MapSet();
664
+ UnitComponent.units = new MapSet();
665
+ //----------------------------------------------------------------------------------------------------
666
+ // unit event
667
+ //----------------------------------------------------------------------------------------------------
668
+ class UnitEvent {
669
+ static on(unit, type, listener, options) {
670
+ if (typeof type !== 'string' || type.trim() === '') {
671
+ throw new Error('"type" is invalid.');
672
+ }
673
+ else if (typeof listener !== 'function') {
674
+ throw new Error('"listener" is invalid.');
675
+ }
676
+ const snapshot = UnitScope.snapshot();
677
+ const types = type.trim().split(/\s+/);
678
+ types.forEach((type) => {
679
+ if (!UnitEvent.listeners.has(unit, type, listener)) {
680
+ const execute = (...args) => {
681
+ UnitScope.execute(snapshot, listener, ...args);
682
+ };
683
+ UnitEvent.listeners.set(unit, type, listener, [unit.element, execute]);
684
+ UnitEvent.units.add(type, unit);
685
+ if (/^[A-Za-z]/.test(type)) {
686
+ unit.element.addEventListener(type, execute, options);
687
+ }
688
+ }
689
+ });
690
+ }
691
+ static off(unit, type, listener) {
692
+ if (typeof type === 'string' && type.trim() === '') {
693
+ throw new Error('"type" is invalid.');
694
+ }
695
+ else if (listener !== undefined && typeof listener !== 'function') {
696
+ throw new Error('"listener" is invalid.');
697
+ }
698
+ const types = typeof type === 'string' ? type.trim().split(/\s+/) : [...UnitEvent.listeners.keys(unit)];
699
+ types.forEach((type) => {
700
+ const listeners = listener ? [listener] : [...UnitEvent.listeners.keys(unit, type)];
701
+ listeners.forEach((lis) => {
702
+ const tuple = UnitEvent.listeners.get(unit, type, lis);
703
+ if (tuple !== undefined) {
704
+ const [target, execute] = tuple;
705
+ UnitEvent.listeners.delete(unit, type, lis);
706
+ if (/^[A-Za-z]/.test(type)) {
707
+ target.removeEventListener(type, execute);
708
+ }
709
+ }
710
+ });
711
+ if (!UnitEvent.listeners.has(unit, type)) {
712
+ UnitEvent.units.delete(type, unit);
713
+ }
714
+ });
715
+ }
716
+ static emit(unit, type, ...args) {
717
+ var _a, _b;
718
+ if (typeof type !== 'string') {
719
+ throw new Error('The argument [type] is invalid.');
720
+ }
721
+ else if ((unit === null || unit === void 0 ? void 0 : unit._.state) === LIFECYCLE_STATES.FINALIZED) {
722
+ throw new Error('This function can not be called after finalized.');
723
+ }
724
+ if (type[0] === CUSTOM_EVENT_PREFIX.GLOBAL) {
725
+ (_a = UnitEvent.units.get(type)) === null || _a === void 0 ? void 0 : _a.forEach((unit) => {
726
+ var _a;
727
+ (_a = UnitEvent.listeners.get(unit, type)) === null || _a === void 0 ? void 0 : _a.forEach(([_, execute]) => execute(...args));
728
+ });
729
+ }
730
+ else if (type[0] === CUSTOM_EVENT_PREFIX.INTERNAL && unit !== null) {
731
+ (_b = UnitEvent.listeners.get(unit, type)) === null || _b === void 0 ? void 0 : _b.forEach(([_, execute]) => execute(...args));
732
+ }
733
+ }
734
+ static subon(unit, target, type, listener, options) {
735
+ if (typeof type !== 'string' || type.trim() === '') {
736
+ throw new Error('"type" is invalid.');
737
+ }
738
+ else if (typeof listener !== 'function') {
739
+ throw new Error('"listener" is invalid.');
740
+ }
741
+ const snapshot = UnitScope.snapshot();
742
+ const types = type.trim().split(/\s+/);
743
+ types.forEach((type) => {
744
+ if (!UnitEvent.sublisteners.has(unit, type, listener)) {
745
+ const execute = (...args) => {
746
+ UnitScope.execute(snapshot, listener, ...args);
747
+ };
748
+ UnitEvent.sublisteners.set(unit, type, listener, [target, execute]);
749
+ target.addEventListener(type, execute, options);
750
+ }
751
+ });
752
+ }
753
+ static suboff(unit, target, type, listener) {
754
+ if (typeof type === 'string' && type.trim() === '') {
755
+ throw new Error('"type" is invalid.');
756
+ }
757
+ else if (listener !== undefined && typeof listener !== 'function') {
758
+ throw new Error('"listener" is invalid.');
759
+ }
760
+ const types = typeof type === 'string' ? type.trim().split(/\s+/) : [...UnitEvent.sublisteners.keys(unit)];
761
+ types.forEach((type) => {
762
+ const listeners = listener ? [listener] : [...UnitEvent.sublisteners.keys(unit, type)];
763
+ listeners.forEach((lis) => {
764
+ const tuple = UnitEvent.sublisteners.get(unit, type, lis);
765
+ if (tuple !== undefined) {
766
+ const [element, execute] = tuple;
767
+ if (target === null || target === element) {
768
+ UnitEvent.sublisteners.delete(unit, type, lis);
769
+ element.removeEventListener(type, execute);
770
+ }
771
+ }
772
+ });
773
+ });
774
+ }
775
+ }
776
+ UnitEvent.units = new MapSet;
777
+ UnitEvent.listeners = new MapMapMap;
778
+ UnitEvent.sublisteners = new MapMapMap;
779
+ //----------------------------------------------------------------------------------------------------
780
+ // unit promise
781
+ //----------------------------------------------------------------------------------------------------
782
+ class UnitPromise {
783
+ constructor(executor) {
784
+ this.promise = new Promise(executor);
785
+ }
786
+ then(callback) {
787
+ const snapshot = UnitScope.snapshot();
788
+ this.promise = this.promise.then((...args) => UnitScope.execute(snapshot, callback, ...args));
789
+ return this;
790
+ }
791
+ catch(callback) {
792
+ const snapshot = UnitScope.snapshot();
793
+ this.promise = this.promise.catch((...args) => UnitScope.execute(snapshot, callback, ...args));
794
+ return this;
795
+ }
796
+ finally(callback) {
797
+ const snapshot = UnitScope.snapshot();
798
+ this.promise = this.promise.finally((...args) => UnitScope.execute(snapshot, callback, ...args));
799
+ return this;
800
+ }
801
+ static get(unit) {
802
+ var _a;
803
+ return Promise.all([...((_a = UnitPromise.promises.get(unit)) !== null && _a !== void 0 ? _a : [])].map((unitPromise) => unitPromise.promise));
804
+ }
805
+ static finalize(unit) {
806
+ UnitPromise.promises.delete(unit);
807
+ }
808
+ static execute(unit, promise) {
809
+ if (promise !== undefined) {
810
+ const upromise = new UnitPromise((resolve, reject) => {
811
+ promise.then((...args) => resolve(...args)).catch((...args) => reject(...args));
812
+ });
813
+ UnitPromise.promises.add(unit, upromise);
814
+ return upromise;
815
+ }
816
+ else {
817
+ const promiseall = UnitPromise.get(unit);
818
+ const upromise = new UnitPromise((resolve, reject) => {
819
+ promiseall.then((...args) => resolve(...args)).catch((...args) => reject(...args));
820
+ });
821
+ return upromise;
822
+ }
823
+ }
824
+ }
825
+ UnitPromise.promises = new MapSet();
826
+
827
+ const xnew$1 = (() => {
828
+ const fn = function (...args) {
829
+ let target;
830
+ if (args[0] instanceof HTMLElement || args[0] instanceof SVGElement) {
831
+ target = args.shift(); // an existing html element
832
+ }
833
+ else if (typeof args[0] === 'string') {
834
+ const str = args.shift(); // a selector for an existing html element
835
+ const match = str.match(/<([^>]*)\/?>/);
836
+ if (match) {
837
+ target = str;
838
+ }
839
+ else {
840
+ target = document.querySelector(str);
841
+ if (target == null) {
842
+ throw new Error(`'${str}' can not be found.`);
843
+ }
844
+ }
845
+ }
846
+ else {
847
+ target = null;
848
+ }
849
+ const unit = new Unit(target, ...args);
850
+ return unit;
851
+ };
852
+ fn.nest = (tag) => {
853
+ const current = UnitScope.current;
854
+ if ((current === null || current === void 0 ? void 0 : current._.state) === 'invoked') {
855
+ const element = Unit.nest(current, tag);
856
+ if (element instanceof HTMLElement || element instanceof SVGElement) {
857
+ return element;
858
+ }
859
+ else {
860
+ throw new Error('');
861
+ }
862
+ }
863
+ else {
864
+ throw new Error('This function can not be called after initialized.');
865
+ }
866
+ };
867
+ fn.extend = (component, props) => {
868
+ const current = UnitScope.current;
869
+ if ((current === null || current === void 0 ? void 0 : current._.state) === 'invoked') {
870
+ return Unit.extend(current, component, props);
871
+ }
872
+ else {
873
+ throw new Error('This function can not be called after initialized.');
874
+ }
875
+ };
876
+ fn.context = (key, value = undefined) => {
877
+ try {
878
+ const unit = UnitScope.current;
879
+ if (typeof key !== 'string') {
880
+ throw new Error('The argument [key] is invalid.');
881
+ }
882
+ else if (unit !== null) {
883
+ if (value !== undefined) {
884
+ UnitScope.stack(unit, key, value);
885
+ }
886
+ else {
887
+ return UnitScope.trace(unit, key);
888
+ }
889
+ }
890
+ else {
891
+ return undefined;
892
+ }
893
+ }
894
+ catch (error) {
895
+ console.error('xnew.context(key, value?): ', error);
896
+ }
897
+ };
898
+ fn.promise = (promise) => {
899
+ try {
900
+ if (UnitScope.current !== null) {
901
+ return UnitPromise.execute(UnitScope.current, promise);
902
+ }
903
+ else {
904
+ throw new Error('No current unit.');
905
+ }
906
+ }
907
+ catch (error) {
908
+ console.error('xnew.promise(mix): ', error);
909
+ throw error;
910
+ }
911
+ };
912
+ fn.then = (callback) => {
913
+ try {
914
+ if (UnitScope.current !== null) {
915
+ return UnitPromise.execute(UnitScope.current).then(callback);
916
+ }
917
+ else {
918
+ throw new Error('No current unit.');
919
+ }
920
+ }
921
+ catch (error) {
922
+ console.error('xnew.then(mix): ', error);
923
+ throw error;
924
+ }
925
+ };
926
+ fn.catch = (callback) => {
927
+ try {
928
+ if (UnitScope.current !== null) {
929
+ return UnitPromise.execute(UnitScope.current).catch(callback);
930
+ }
931
+ else {
932
+ throw new Error('No current unit.');
933
+ }
934
+ }
935
+ catch (error) {
936
+ console.error('xnew.catch(mix): ', error);
937
+ throw error;
938
+ }
939
+ };
940
+ fn.finally = (callback) => {
941
+ try {
942
+ if (UnitScope.current !== null) {
943
+ return UnitPromise.execute(UnitScope.current).finally(callback);
944
+ }
945
+ else {
946
+ throw new Error('No current unit.');
947
+ }
948
+ }
949
+ catch (error) {
950
+ console.error('xnew.finally(mix): ', error);
951
+ throw error;
952
+ }
953
+ };
954
+ fn.fetch = (url, options) => {
955
+ try {
956
+ const promise = fetch(url, options);
957
+ if (UnitScope.current !== null) {
958
+ return UnitPromise.execute(UnitScope.current, promise);
959
+ }
960
+ else {
961
+ throw new Error('No current unit.');
962
+ }
963
+ }
964
+ catch (error) {
965
+ console.error('xnew.promise(mix): ', error);
966
+ throw error;
967
+ }
968
+ };
969
+ fn.scope = (callback) => {
970
+ const snapshot = UnitScope.snapshot();
971
+ return (...args) => UnitScope.execute(snapshot, callback, ...args);
972
+ };
973
+ fn.find = (component) => {
974
+ if (typeof component === 'function') {
975
+ return UnitComponent.find(component);
976
+ }
977
+ else {
978
+ throw new Error(`The argument [component] is invalid.`);
979
+ }
980
+ };
981
+ fn.append = (base, ...args) => {
982
+ if (typeof base === 'function') {
983
+ for (let unit of UnitComponent.find(base)) {
984
+ UnitScope.execute(UnitScope.snapshot(unit), xnew$1, ...args);
985
+ }
986
+ }
987
+ else if (base instanceof Unit) {
988
+ UnitScope.execute(UnitScope.snapshot(base), xnew$1, ...args);
989
+ }
990
+ else {
991
+ throw new Error(`The argument [component] is invalid.`);
992
+ }
993
+ };
994
+ fn.timeout = (callback, delay) => {
995
+ const snapshot = UnitScope.snapshot();
996
+ const unit = xnew$1((self) => {
997
+ const timer = new Timer(() => {
998
+ UnitScope.execute(snapshot, callback);
999
+ self.finalize();
1000
+ }, null, delay);
1001
+ self.on('finalize', () => {
1002
+ timer.clear();
1003
+ });
1004
+ });
1005
+ return { clear: () => unit.finalize() };
1006
+ };
1007
+ fn.interval = (callback, delay) => {
1008
+ const snapshot = UnitScope.snapshot();
1009
+ const unit = xnew$1((self) => {
1010
+ const timer = new Timer(() => {
1011
+ UnitScope.execute(snapshot, callback);
1012
+ }, null, delay, true);
1013
+ self.on('finalize', () => {
1014
+ timer.clear();
1015
+ });
1016
+ });
1017
+ return { clear: () => unit.finalize() };
1018
+ };
1019
+ fn.transition = (callback, interval, easing = 'linear') => {
1020
+ const snapshot = UnitScope.snapshot();
1021
+ let stacks = [];
1022
+ let unit = xnew$1(Local, { callback, interval, easing });
1023
+ let isRunning = true;
1024
+ function Local(self, { callback, interval, easing }) {
1025
+ const timer = new Timer(() => {
1026
+ UnitScope.execute(snapshot, callback, 1.0);
1027
+ self.finalize();
1028
+ }, (progress) => {
1029
+ if (progress < 1.0) {
1030
+ if (easing === 'ease-out') {
1031
+ progress = Math.pow((1.0 - Math.pow((1.0 - progress), 2.0)), 0.5);
1032
+ }
1033
+ else if (easing === 'ease-in') {
1034
+ progress = Math.pow((1.0 - Math.pow((1.0 - progress), 0.5)), 2.0);
1035
+ }
1036
+ else if (easing === 'ease') {
1037
+ progress = (1.0 - Math.cos(progress * Math.PI)) / 2.0;
1038
+ }
1039
+ else if (easing === 'ease-in-out') {
1040
+ progress = (1.0 - Math.cos(progress * Math.PI)) / 2.0;
1041
+ }
1042
+ UnitScope.execute(snapshot, callback, progress);
1043
+ }
1044
+ }, interval);
1045
+ self.on('finalize', () => {
1046
+ timer.clear();
1047
+ isRunning = false;
1048
+ execute();
1049
+ });
1050
+ }
1051
+ let timer = null;
1052
+ function execute() {
1053
+ if (isRunning === false && stacks.length > 0) {
1054
+ const props = stacks.shift();
1055
+ unit = xnew$1(Local, props);
1056
+ isRunning = true;
1057
+ }
1058
+ }
1059
+ function clear() {
1060
+ stacks = [];
1061
+ unit.finalize();
1062
+ }
1063
+ function next(callback, interval, easing = 'linear') {
1064
+ stacks.push({ callback, interval, easing });
1065
+ execute();
1066
+ return timer;
1067
+ }
1068
+ timer = { clear, next };
1069
+ return timer;
1070
+ };
1071
+ fn.listener = function (target) {
1072
+ return {
1073
+ on(type, listener, options) {
1074
+ UnitEvent.subon(UnitScope.current, target, type, listener, options);
1075
+ },
1076
+ off(type, listener) {
1077
+ UnitEvent.suboff(UnitScope.current, target, type, listener);
1078
+ }
1079
+ };
1080
+ };
1081
+ fn.capture = function (checker, execute) {
1082
+ const current = UnitScope.current;
1083
+ const snapshot = UnitScope.snapshot();
1084
+ current._.captures.push({ checker, execute: (unit) => UnitScope.execute(snapshot, execute, unit) });
1085
+ };
1086
+ return fn;
1087
+ })();
1088
+
1089
+ function ResizeEvent(self) {
1090
+ const observer = new ResizeObserver(xnew$1.scope((entries) => {
1091
+ for (const entry of entries) {
1092
+ self.emit('-resize');
1093
+ break;
1094
+ }
1095
+ }));
1096
+ if (self.element) {
1097
+ observer.observe(self.element);
1098
+ }
1099
+ self.on('finalize', () => {
1100
+ if (self.element) {
1101
+ observer.unobserve(self.element);
1102
+ }
1103
+ });
1104
+ }
1105
+
1106
+ function UserEvent(self) {
1107
+ const unit = xnew$1();
1108
+ unit.on('pointerdown', (event) => self.emit('-pointerdown', { event, position: getPosition(self.element, event) }));
1109
+ unit.on('pointermove', (event) => self.emit('-pointermove', { event, position: getPosition(self.element, event) }));
1110
+ unit.on('pointerup', (event) => self.emit('-pointerup', { event, position: getPosition(self.element, event) }));
1111
+ unit.on('wheel', (event) => self.emit('-wheel', { event, delta: { x: event.wheelDeltaX, y: event.wheelDeltaY } }));
1112
+ const drag = xnew$1(DragEvent);
1113
+ drag.on('-dragstart', (...args) => self.emit('-dragstart', ...args));
1114
+ drag.on('-dragmove', (...args) => self.emit('-dragmove', ...args));
1115
+ drag.on('-dragend', (...args) => self.emit('-dragend', ...args));
1116
+ drag.on('-dragcancel', (...args) => self.emit('-dragcancel', ...args));
1117
+ const gesture = xnew$1(GestureEvent);
1118
+ gesture.on('-gesturestart', (...args) => self.emit('-gesturestart', ...args));
1119
+ gesture.on('-gesturemove', (...args) => self.emit('-gesturemove', ...args));
1120
+ gesture.on('-gestureend', (...args) => self.emit('-gestureend', ...args));
1121
+ gesture.on('-gesturecancel', (...args) => self.emit('-gesturecancel', ...args));
1122
+ const keyborad = xnew$1(Keyboard);
1123
+ keyborad.on('-keydown', (...args) => self.emit('-keydown', ...args));
1124
+ keyborad.on('-keyup', (...args) => self.emit('-keyup', ...args));
1125
+ keyborad.on('-arrowkeydown', (...args) => self.emit('-arrowkeydown', ...args));
1126
+ keyborad.on('-arrowkeyup', (...args) => self.emit('-arrowkeyup', ...args));
1127
+ }
1128
+ function DragEvent(self) {
1129
+ xnew$1().on('pointerdown', (event) => {
1130
+ const id = event.pointerId;
1131
+ const position = getPosition(self.element, event);
1132
+ let previous = position;
1133
+ xnew$1(() => {
1134
+ xnew$1.listener(window).on('pointermove', (event) => {
1135
+ if (event.pointerId === id) {
1136
+ const position = getPosition(self.element, event);
1137
+ const delta = { x: position.x - previous.x, y: position.y - previous.y };
1138
+ self.emit('-dragmove', { event, position, delta });
1139
+ previous = position;
1140
+ }
1141
+ });
1142
+ xnew$1.listener(window).on('pointerup', (event) => {
1143
+ if (event.pointerId === id) {
1144
+ const position = getPosition(self.element, event);
1145
+ self.emit('-dragend', { event, position, });
1146
+ xnew$1.listener(window).off();
1147
+ }
1148
+ });
1149
+ xnew$1.listener(window).on('pointercancel', (event) => {
1150
+ if (event.pointerId === id) {
1151
+ const position = getPosition(self.element, event);
1152
+ self.emit('-dragcancel', { event, position, });
1153
+ xnew$1.listener(window).off();
1154
+ }
1155
+ });
1156
+ });
1157
+ self.emit('-dragstart', { event, position });
1158
+ });
1159
+ }
1160
+ function GestureEvent(self) {
1161
+ const drag = xnew$1(DragEvent);
1162
+ let isActive = false;
1163
+ const map = new Map();
1164
+ drag.on('-dragstart', ({ event, position }) => {
1165
+ map.set(event.pointerId, Object.assign({}, position));
1166
+ isActive = map.size === 2 ? true : false;
1167
+ if (isActive === true) {
1168
+ self.emit('-gesturestart', {});
1169
+ }
1170
+ });
1171
+ drag.on('-dragmove', ({ event, position, delta }) => {
1172
+ if (map.size >= 2 && isActive === true) {
1173
+ const a = map.get(event.pointerId);
1174
+ const b = getOthers(event.pointerId)[0];
1175
+ let scale = 0.0;
1176
+ {
1177
+ const v = { x: a.x - b.x, y: a.y - b.y };
1178
+ const s = v.x * v.x + v.y * v.y;
1179
+ scale = 1 + (s > 0.0 ? (v.x * delta.x + v.y * delta.y) / s : 0);
1180
+ }
1181
+ // let rotate = 0.0;
1182
+ // {
1183
+ // const c = { x: a.x + delta.x, y: a.y + delta.y };
1184
+ // const v1 = { x: a.x - b.x, y: a.y - b.y };
1185
+ // const v2 = { x: c.x - b.x, y: c.y - b.y };
1186
+ // const l1 = Math.sqrt(v1.x * v1.x + v1.y * v1.y);
1187
+ // const l2 = Math.sqrt(v2.x * v2.x + v2.y * v2.y);
1188
+ // if (l1 > 0.0 && l2 > 0.0) {
1189
+ // const angle = Math.acos((v1.x * v2.x + v1.y * v2.y) / (l1 * l2));
1190
+ // const sign = v1.x * v2.y - v1.y * v2.x;
1191
+ // rotate = sign > 0.0 ? +angle : -angle;
1192
+ // }
1193
+ // }
1194
+ self.emit('-gesturemove', { event, position, delta, scale });
1195
+ }
1196
+ map.set(event.pointerId, position);
1197
+ });
1198
+ drag.on('-dragend', ({ event }) => {
1199
+ if (isActive === true) {
1200
+ self.emit('-gestureend', {});
1201
+ }
1202
+ isActive = false;
1203
+ map.delete(event.pointerId);
1204
+ });
1205
+ drag.on('-dragcancel', ({ event }) => {
1206
+ if (isActive === true) {
1207
+ self.emit('-gesturecancel', { event });
1208
+ }
1209
+ isActive = false;
1210
+ map.delete(event.pointerId);
1211
+ });
1212
+ function getOthers(id) {
1213
+ const backup = map.get(id);
1214
+ map.delete(id);
1215
+ const others = [...map.values()];
1216
+ map.set(id, backup);
1217
+ return others;
1218
+ }
1219
+ }
1220
+ function Keyboard(self) {
1221
+ const state = {};
1222
+ xnew$1.listener(window).on('keydown', (event) => {
1223
+ state[event.code] = 1;
1224
+ self.emit('-keydown', { event, code: event.code });
1225
+ });
1226
+ xnew$1.listener(window).on('keyup', (event) => {
1227
+ state[event.code] = 0;
1228
+ self.emit('-keyup', { event, code: event.code });
1229
+ });
1230
+ xnew$1.listener(window).on('keydown', (event) => {
1231
+ self.emit('-arrowkeydown', { event, code: event.code, vector: getVector() });
1232
+ });
1233
+ xnew$1.listener(window).on('keyup', (event) => {
1234
+ self.emit('-arrowkeyup', { event, code: event.code, vector: getVector() });
1235
+ });
1236
+ function getVector() {
1237
+ return {
1238
+ x: (state['ArrowLeft'] ? -1 : 0) + (state['ArrowRight'] ? +1 : 0),
1239
+ y: (state['ArrowUp'] ? -1 : 0) + (state['ArrowDown'] ? +1 : 0)
1240
+ };
1241
+ }
1242
+ }
1243
+ function getPosition(element, event) {
1244
+ const rect = element.getBoundingClientRect();
1245
+ return { x: event.clientX - rect.left, y: event.clientY - rect.top };
1246
+ }
1247
+
1248
+ function Screen(screen, { width = 640, height = 480, fit = 'contain' } = {}) {
1249
+ const size = { width, height };
1250
+ const wrapper = xnew$1.nest('<div style="position: relative; width: 100%; height: 100%; overflow: hidden;">');
1251
+ const absolute = xnew$1.nest('<div style="position: absolute; margin: auto;">');
1252
+ const canvas = xnew$1.nest(`<canvas width="${width}" height="${height}" style="width: 100%; height: 100%; vertical-align: bottom; user-select: none; user-drag: none;">`);
1253
+ xnew$1(wrapper, ResizeEvent).on('-resize', resize);
1254
+ resize();
1255
+ function resize() {
1256
+ const aspect = size.width / size.height;
1257
+ const style = { width: '100%', height: '100%', top: 0, left: 0, bottom: 0, right: 0 };
1258
+ if (fit === 'contain') {
1259
+ if (wrapper.clientWidth < wrapper.clientHeight * aspect) {
1260
+ style.height = Math.floor(wrapper.clientWidth / aspect) + 'px';
1261
+ }
1262
+ else {
1263
+ style.width = Math.floor(wrapper.clientHeight * aspect) + 'px';
1264
+ }
1265
+ }
1266
+ else if (fit === 'cover') {
1267
+ if (wrapper.clientWidth < wrapper.clientHeight * aspect) {
1268
+ style.width = Math.floor(wrapper.clientHeight * aspect) + 'px';
1269
+ style.left = Math.floor((wrapper.clientWidth - wrapper.clientHeight * aspect) / 2) + 'px';
1270
+ style.right = 'auto';
1271
+ }
1272
+ else {
1273
+ style.height = Math.floor(wrapper.clientWidth / aspect) + 'px';
1274
+ style.top = Math.floor((wrapper.clientHeight - wrapper.clientWidth / aspect) / 2) + 'px';
1275
+ style.bottom = 'auto';
1276
+ }
1277
+ }
1278
+ else ;
1279
+ Object.assign(absolute.style, style);
1280
+ }
1281
+ return {
1282
+ get canvas() {
1283
+ return canvas;
1284
+ },
1285
+ resize(width, height) {
1286
+ size.width = width;
1287
+ size.height = height;
1288
+ canvas.setAttribute('width', width + 'px');
1289
+ canvas.setAttribute('height', height + 'px');
1290
+ resize();
1291
+ },
1292
+ get scale() {
1293
+ return { x: size.width / canvas.clientWidth, y: size.height / canvas.clientHeight };
1294
+ }
1295
+ };
1296
+ }
1297
+
1298
+ function InputFrame(frame, {} = {}) {
1299
+ xnew$1.nest('<div>');
1300
+ xnew$1.capture((unit) => unit.element.tagName.toLowerCase() === 'input', (unit) => {
1301
+ const element = unit.element;
1302
+ xnew$1.listener(element).on('input', (event) => {
1303
+ frame.emit('-input', { event });
1304
+ });
1305
+ xnew$1.listener(element).on('change', (event) => {
1306
+ frame.emit('-change', { event });
1307
+ });
1308
+ xnew$1.listener(element).on('click', (event) => {
1309
+ frame.emit('-click', { event });
1310
+ });
1311
+ });
1312
+ }
1313
+
1314
+ function ModalFrame(frame, {} = {}) {
1315
+ xnew$1.context('xnew.modalframe', frame);
1316
+ xnew$1.nest('<div style="position: fixed; inset: 0;">');
1317
+ let content = null;
1318
+ xnew$1.capture((unit) => unit.components.includes(ModalContent), (unit) => {
1319
+ content = unit;
1320
+ });
1321
+ xnew$1().on('click', (event) => {
1322
+ frame === null || frame === void 0 ? void 0 : frame.close();
1323
+ });
1324
+ return {
1325
+ close() {
1326
+ frame.emit('-close');
1327
+ content === null || content === void 0 ? void 0 : content.deselect();
1328
+ }
1329
+ };
1330
+ }
1331
+ function ModalContent(content, { duration = 200, easing = 'ease', background = 'rgba(0, 0, 0, 0.1)' } = {}) {
1332
+ const frame = xnew$1.context('xnew.modalframe');
1333
+ const div = xnew$1.nest('<div style="width: 100%; height: 100%; opacity: 0;">');
1334
+ div.style.background = background;
1335
+ xnew$1.nest('<div style="position: absolute; inset: 0; margin: auto; width: max-content; height: max-content;">');
1336
+ xnew$1().on('click', (event) => {
1337
+ event.stopPropagation();
1338
+ });
1339
+ let status = 0;
1340
+ xnew$1.timeout(() => frame.emit('-open'));
1341
+ frame.on('-open', () => {
1342
+ xnew$1.transition((x) => {
1343
+ status = x;
1344
+ frame.emit('-transition', { status });
1345
+ content.transition(status);
1346
+ }, duration, easing);
1347
+ });
1348
+ frame.on('-close', () => {
1349
+ xnew$1.transition((x) => {
1350
+ status = 1.0 - x;
1351
+ frame.emit('-transition', { status });
1352
+ content.transition(status);
1353
+ }, duration, easing).next(() => frame.finalize());
1354
+ });
1355
+ return {
1356
+ transition(status) {
1357
+ div.style.opacity = status.toString();
1358
+ }
1359
+ };
1360
+ }
1361
+
1362
+ function TabFrame(frame, { select = 0 } = {}) {
1363
+ xnew$1.context('xnew.tabframe', frame);
1364
+ const buttons = [];
1365
+ const contents = [];
1366
+ xnew$1.capture((unit) => unit.components.includes(TabButton), (unit) => {
1367
+ buttons.push(unit);
1368
+ });
1369
+ xnew$1.capture((unit) => unit.components.includes(TabContent), (unit) => {
1370
+ contents.push(unit);
1371
+ });
1372
+ frame.on('-click', ({ unit }) => execute(buttons.indexOf(unit)));
1373
+ const timeout = xnew$1.timeout(() => execute(select));
1374
+ function execute(index) {
1375
+ timeout.clear();
1376
+ const button = buttons[index];
1377
+ const content = contents[index];
1378
+ buttons.filter((item) => item !== button).forEach((item) => item.deselect());
1379
+ contents.filter((item) => item !== content).forEach((item) => item.deselect());
1380
+ button.select();
1381
+ content.select();
1382
+ }
1383
+ }
1384
+ function TabButton(button, {} = {}) {
1385
+ const frame = xnew$1.context('xnew.tabframe');
1386
+ xnew$1.nest('<div>');
1387
+ button.on('click', () => frame.emit('-click', { unit: button }));
1388
+ return {
1389
+ select() {
1390
+ Object.assign(button.element.style, { opacity: 1.0, cursor: 'text' });
1391
+ },
1392
+ deselect() {
1393
+ Object.assign(button.element.style, { opacity: 0.6, cursor: 'pointer' });
1394
+ }
1395
+ };
1396
+ }
1397
+ function TabContent(self, {} = {}) {
1398
+ xnew$1.context('xnew.tabframe');
1399
+ xnew$1.nest('<div>');
1400
+ return {
1401
+ select() {
1402
+ Object.assign(self.element.style, { display: 'block' });
1403
+ },
1404
+ deselect() {
1405
+ Object.assign(self.element.style, { display: 'none' });
1406
+ }
1407
+ };
1408
+ }
1409
+
1410
+ function AccordionFrame(frame, {} = {}) {
1411
+ xnew$1.context('xnew.accordionframe', frame);
1412
+ let content = null;
1413
+ xnew$1.capture((unit) => unit.components.includes(AccordionContent), (unit) => {
1414
+ content = unit;
1415
+ });
1416
+ return {
1417
+ toggle() {
1418
+ if ((content === null || content === void 0 ? void 0 : content.status) === 1.0) {
1419
+ frame.emit('-close');
1420
+ }
1421
+ else if ((content === null || content === void 0 ? void 0 : content.status) === 0.0) {
1422
+ frame.emit('-open');
1423
+ }
1424
+ },
1425
+ open() {
1426
+ if ((content === null || content === void 0 ? void 0 : content.status) === 0.0) {
1427
+ frame.emit('-open');
1428
+ }
1429
+ },
1430
+ close() {
1431
+ if ((content === null || content === void 0 ? void 0 : content.status) === 1.0) {
1432
+ frame.emit('-close');
1433
+ }
1434
+ }
1435
+ };
1436
+ }
1437
+ function AccordionButton(button, {} = {}) {
1438
+ const frame = xnew$1.context('xnew.accordionframe');
1439
+ xnew$1.nest('<button style="display: flex; align-items: center; margin: 0; padding: 0; width: 100%; text-align: left; border: none; font: inherit; color: inherit; background: none; cursor: pointer;">');
1440
+ button.on('click', () => frame.toggle());
1441
+ }
1442
+ function AccordionBullet(bullet, { type = 'arrow' } = {}) {
1443
+ const frame = xnew$1.context('xnew.accordionframe');
1444
+ xnew$1.nest('<div style="display:inline-block; position: relative; width: 0.5em; margin: 0 0.3em;">');
1445
+ frame.on('-transition', ({ status }) => { var _a; return (_a = bullet.transition) === null || _a === void 0 ? void 0 : _a.call(bullet, status); });
1446
+ if (type === 'arrow') {
1447
+ const arrow = xnew$1(`<div style="width: 100%; height: 0.5em; border-right: 0.12em solid currentColor; border-bottom: 0.12em solid currentColor; box-sizing: border-box; transform-origin: center center;">`);
1448
+ return {
1449
+ transition(status) {
1450
+ arrow.element.style.transform = `rotate(${status * 90 - 45}deg)`;
1451
+ }
1452
+ };
1453
+ }
1454
+ else if (type === 'plusminus') {
1455
+ xnew$1(`<div style="position: absolute; width: 100%; border-top: 0.06em solid currentColor; border-bottom: 0.06em solid currentColor; box-sizing: border-box; transform-origin: center center;">`);
1456
+ const line2 = xnew$1(`<div style="position: absolute; width: 100%; border-top: 0.06em solid currentColor; border-bottom: 0.06em solid currentColor; box-sizing: border-box; transform-origin: center center;">`);
1457
+ line2.element.style.transform = `rotate(90deg)`;
1458
+ return {
1459
+ transition(status) {
1460
+ line2.element.style.opacity = `${1.0 - status}`;
1461
+ }
1462
+ };
1463
+ }
1464
+ }
1465
+ function AccordionContent(content, { open = false, duration = 200, easing = 'ease' } = {}) {
1466
+ const frame = xnew$1.context('xnew.accordionframe');
1467
+ const outer = xnew$1.nest('<div>');
1468
+ const inner = xnew$1.nest('<div style="padding: 0; display: flex; flex-direction: column; box-sizing: border-box;">');
1469
+ let status = open ? 1.0 : 0.0;
1470
+ outer.style.display = status ? 'block' : 'none';
1471
+ frame.emit('-transition', { status });
1472
+ frame.on('-open', () => {
1473
+ xnew$1.transition((x) => {
1474
+ status = x;
1475
+ frame.emit('-transition', { status });
1476
+ content.transition(status);
1477
+ }, duration, easing);
1478
+ });
1479
+ frame.on('-close', () => {
1480
+ xnew$1.transition((x) => {
1481
+ status = 1.0 - x;
1482
+ frame.emit('-transition', { status });
1483
+ content.transition(status);
1484
+ }, duration, easing);
1485
+ });
1486
+ return {
1487
+ get status() {
1488
+ return status;
1489
+ },
1490
+ transition(status) {
1491
+ outer.style.display = 'block';
1492
+ if (status === 0.0) {
1493
+ outer.style.display = 'none';
1494
+ }
1495
+ else if (status < 1.0) {
1496
+ Object.assign(outer.style, { height: inner.offsetHeight * status + 'px', overflow: 'hidden', opacity: status });
1497
+ }
1498
+ else {
1499
+ Object.assign(outer.style, { height: 'auto', overflow: 'visible', opacity: 1.0 });
1500
+ }
1501
+ },
1502
+ };
1503
+ }
1504
+
1505
+ function PanelFrame(frame) {
1506
+ xnew$1.context('xnew.panelframe', frame);
1507
+ }
1508
+ function PanelGroup(group, { name, open = false } = {}) {
1509
+ xnew$1.extend(AccordionFrame);
1510
+ xnew$1((button) => {
1511
+ xnew$1.nest('<div style="margin: 0.2em 0;">');
1512
+ xnew$1.extend(AccordionButton);
1513
+ xnew$1(AccordionBullet);
1514
+ xnew$1('<div>', name);
1515
+ });
1516
+ xnew$1.extend(AccordionContent, { open });
1517
+ }
1518
+
1519
+ function DragFrame(frame, { x = 0, y = 0 } = {}) {
1520
+ const absolute = xnew$1.nest(`<div style="position: absolute; top: ${y}px; left: ${x}px;">`);
1521
+ xnew$1.context('xnew.dragframe', { frame, absolute });
1522
+ }
1523
+ function DragTarget(target, {} = {}) {
1524
+ const { frame, absolute } = xnew$1.context('xnew.dragframe');
1525
+ xnew$1.nest('<div>');
1526
+ const user = xnew$1(absolute.parentElement, UserEvent);
1527
+ const current = { x: 0, y: 0 };
1528
+ user.on('-dragstart', ({ event, position }) => {
1529
+ current.x = parseFloat(absolute.style.left || '0') + position.x;
1530
+ current.y = parseFloat(absolute.style.top || '0') + position.y;
1531
+ });
1532
+ user.on('-dragmove', ({ event, delta }) => {
1533
+ current.x += delta.x;
1534
+ current.y += delta.y;
1535
+ absolute.style.left = `${current.x}px`;
1536
+ absolute.style.top = `${current.y}px`;
1537
+ });
1538
+ }
1539
+
1540
+ //----------------------------------------------------------------------------------------------------
1541
+ // controller
1542
+ //----------------------------------------------------------------------------------------------------
1543
+ function SVGTemplate(self, { fill = null, fillOpacity = 0.8, stroke = null, strokeOpacity = 0.8, strokeWidth = 2, strokeLinejoin = 'round' }) {
1544
+ xnew$1.nest(`<svg
1545
+ viewBox="0 0 100 100"
1546
+ style="position: absolute; width: 100%; height: 100%; user-select: none;
1547
+ ${fill ? `fill: ${fill}; fill-opacity: ${fillOpacity};` : ''}
1548
+ ${stroke ? `stroke: ${stroke}; stroke-opacity: ${strokeOpacity}; stroke-width: ${strokeWidth}; stroke-linejoin: ${strokeLinejoin};` : ''}
1549
+ ">`);
1550
+ }
1551
+ function VirtualStick(self, { size = 130, fill = '#FFF', fillOpacity = 0.8, stroke = '#000', strokeOpacity = 0.8, strokeWidth = 2, strokeLinejoin = 'round' } = {}) {
1552
+ strokeWidth /= (size / 100);
1553
+ xnew$1.nest(`<div style="position: relative; width: ${size}px; height: ${size}px; cursor: pointer; user-select: none; overflow: hidden;">`);
1554
+ xnew$1((self) => {
1555
+ xnew$1.extend(SVGTemplate, { fill, fillOpacity, stroke, strokeOpacity, strokeWidth, strokeLinejoin });
1556
+ xnew$1('<polygon points="50 7 40 18 60 18">');
1557
+ xnew$1('<polygon points="50 93 40 83 60 83">');
1558
+ xnew$1('<polygon points=" 7 50 18 40 18 60">');
1559
+ xnew$1('<polygon points="93 50 83 40 83 60">');
1560
+ });
1561
+ const target = xnew$1((self) => {
1562
+ xnew$1.extend(SVGTemplate, { fill, fillOpacity, stroke, strokeOpacity, strokeWidth, strokeLinejoin });
1563
+ xnew$1('<circle cx="50" cy="50" r="23">');
1564
+ });
1565
+ const user = xnew$1(UserEvent);
1566
+ user.on('-dragstart', ({ event, position }) => {
1567
+ const vector = getVector(position);
1568
+ target.element.style.filter = 'brightness(90%)';
1569
+ target.element.style.left = vector.x * size / 4 + 'px';
1570
+ target.element.style.top = vector.y * size / 4 + 'px';
1571
+ self.emit('-down', { vector });
1572
+ });
1573
+ user.on('-dragmove', ({ event, position }) => {
1574
+ const vector = getVector(position);
1575
+ target.element.style.filter = 'brightness(90%)';
1576
+ target.element.style.left = vector.x * size / 4 + 'px';
1577
+ target.element.style.top = vector.y * size / 4 + 'px';
1578
+ self.emit('-move', { vector });
1579
+ });
1580
+ user.on('-dragend', ({ event }) => {
1581
+ const vector = { x: 0, y: 0 };
1582
+ target.element.style.filter = '';
1583
+ target.element.style.left = vector.x * size / 4 + 'px';
1584
+ target.element.style.top = vector.y * size / 4 + 'px';
1585
+ self.emit('-up', { vector });
1586
+ });
1587
+ function getVector(position) {
1588
+ const x = position.x - size / 2;
1589
+ const y = position.y - size / 2;
1590
+ const d = Math.min(1.0, Math.sqrt(x * x + y * y) / (size / 4));
1591
+ const a = (y !== 0 || x !== 0) ? Math.atan2(y, x) : 0;
1592
+ return { x: Math.cos(a) * d, y: Math.sin(a) * d };
1593
+ }
1594
+ }
1595
+ function VirtualDPad(self, { size = 130, fill = '#FFF', fillOpacity = 0.8, stroke = '#000', strokeOpacity = 0.8, strokeWidth = 2, strokeLinejoin = 'round' } = {}) {
1596
+ strokeWidth /= (size / 100);
1597
+ xnew$1.nest(`<div style="position: relative; width: ${size}px; height: ${size}px; cursor: pointer; user-select: none; overflow: hidden;">`);
1598
+ const polygons = [
1599
+ '<polygon points="50 50 35 35 35 5 37 3 63 3 65 5 65 35">',
1600
+ '<polygon points="50 50 35 65 35 95 37 97 63 97 65 95 65 65">',
1601
+ '<polygon points="50 50 35 35 5 35 3 37 3 63 5 65 35 65">',
1602
+ '<polygon points="50 50 65 35 95 35 97 37 97 63 95 65 65 65">'
1603
+ ];
1604
+ const targets = polygons.map((polygon) => {
1605
+ return xnew$1((self) => {
1606
+ xnew$1.extend(SVGTemplate, { fill, fillOpacity });
1607
+ xnew$1(polygon);
1608
+ });
1609
+ });
1610
+ xnew$1((self) => {
1611
+ xnew$1.extend(SVGTemplate, { fill: 'none', stroke, strokeOpacity, strokeWidth, strokeLinejoin });
1612
+ xnew$1('<polyline points="35 35 35 5 37 3 63 3 65 5 65 35">');
1613
+ xnew$1('<polyline points="35 65 35 95 37 97 63 97 65 95 65 65">');
1614
+ xnew$1('<polyline points="35 35 5 35 3 37 3 63 5 65 35 65">');
1615
+ xnew$1('<polyline points="65 35 95 35 97 37 97 63 95 65 65 65">');
1616
+ xnew$1('<polygon points="50 11 42 20 58 20">');
1617
+ xnew$1('<polygon points="50 89 42 80 58 80">');
1618
+ xnew$1('<polygon points="11 50 20 42 20 58">');
1619
+ xnew$1('<polygon points="89 50 80 42 80 58">');
1620
+ });
1621
+ const user = xnew$1(UserEvent);
1622
+ user.on('-dragstart', ({ event, position }) => {
1623
+ const vector = getVector(position);
1624
+ targets[0].element.style.filter = (vector.y < 0) ? 'brightness(90%)' : '';
1625
+ targets[1].element.style.filter = (vector.y > 0) ? 'brightness(90%)' : '';
1626
+ targets[2].element.style.filter = (vector.x < 0) ? 'brightness(90%)' : '';
1627
+ targets[3].element.style.filter = (vector.x > 0) ? 'brightness(90%)' : '';
1628
+ self.emit('-down', { vector });
1629
+ });
1630
+ user.on('-dragmove', ({ event, position }) => {
1631
+ const vector = getVector(position);
1632
+ targets[0].element.style.filter = (vector.y < 0) ? 'brightness(90%)' : '';
1633
+ targets[1].element.style.filter = (vector.y > 0) ? 'brightness(90%)' : '';
1634
+ targets[2].element.style.filter = (vector.x < 0) ? 'brightness(90%)' : '';
1635
+ targets[3].element.style.filter = (vector.x > 0) ? 'brightness(90%)' : '';
1636
+ self.emit('-move', { vector });
1637
+ });
1638
+ user.on('-dragend', ({ event }) => {
1639
+ const vector = { x: 0, y: 0 };
1640
+ targets[0].element.style.filter = '';
1641
+ targets[1].element.style.filter = '';
1642
+ targets[2].element.style.filter = '';
1643
+ targets[3].element.style.filter = '';
1644
+ self.emit('-up', { vector });
1645
+ });
1646
+ function getVector(position) {
1647
+ const x = position.x - size / 2;
1648
+ const y = position.y - size / 2;
1649
+ const a = (y !== 0 || x !== 0) ? Math.atan2(y, x) : 0;
1650
+ const d = Math.min(1.0, Math.sqrt(x * x + y * y) / (size / 4));
1651
+ const vector = { x: Math.cos(a) * d, y: Math.sin(a) * d };
1652
+ vector.x = Math.abs(vector.x) > 0.5 ? Math.sign(vector.x) : 0;
1653
+ vector.y = Math.abs(vector.y) > 0.5 ? Math.sign(vector.y) : 0;
1654
+ return vector;
1655
+ }
1656
+ }
1657
+ function VirtualButton(self, { size = 80, fill = '#FFF', fillOpacity = 0.8, stroke = '#000', strokeOpacity = 0.8, strokeWidth = 2, strokeLinejoin = 'round' } = {}) {
1658
+ strokeWidth /= (size / 100);
1659
+ xnew$1.nest(`<div style="position: relative; width: ${size}px; height: ${size}px; cursor: pointer; user-select: none; overflow: hidden;">`);
1660
+ const target = xnew$1((self) => {
1661
+ xnew$1.extend(SVGTemplate, { fill, fillOpacity, stroke, strokeOpacity, strokeWidth, strokeLinejoin });
1662
+ xnew$1('<circle cx="50" cy="50" r="40">');
1663
+ });
1664
+ const user = xnew$1(UserEvent);
1665
+ user.on('-dragstart', (event) => {
1666
+ target.element.style.filter = 'brightness(90%)';
1667
+ self.emit('-down', event);
1668
+ });
1669
+ user.on('-dragend', (event) => {
1670
+ target.element.style.filter = '';
1671
+ self.emit('-up', event);
1672
+ });
1673
+ }
1674
+
1675
+ class Audio {
1676
+ static initialize() {
1677
+ var _a;
1678
+ if (typeof window !== 'undefined' && window instanceof Window) {
1679
+ Audio.context = new ((_a = window.AudioContext) !== null && _a !== void 0 ? _a : window.webkitAudioContext)();
1680
+ Audio.master = Audio.context.createGain();
1681
+ Audio.master.gain.value = 1.0;
1682
+ Audio.master.connect(Audio.context.destination);
1683
+ }
1684
+ }
1685
+ static connect(params) {
1686
+ if (!Audio.context)
1687
+ throw new Error("Audio context not initialized");
1688
+ const nodes = {};
1689
+ Object.keys(params).forEach((key) => {
1690
+ const [type, props, ...to] = params[key];
1691
+ nodes[key] = Audio.context[`create${type}`]();
1692
+ Object.keys(props).forEach((name) => {
1693
+ var _a;
1694
+ if (((_a = nodes[key][name]) === null || _a === void 0 ? void 0 : _a.value) !== undefined) {
1695
+ nodes[key][name].value = props[name];
1696
+ }
1697
+ else {
1698
+ nodes[key][name] = props[name];
1699
+ }
1700
+ });
1701
+ });
1702
+ Object.keys(params).forEach((key) => {
1703
+ const [type, props, ...to] = params[key];
1704
+ to.forEach((to) => {
1705
+ let dest = null;
1706
+ if (to.indexOf('.') > 0) {
1707
+ dest = nodes[to.split('.')[0]][to.split('.')[1]];
1708
+ }
1709
+ else if (nodes[to]) {
1710
+ dest = nodes[to];
1711
+ }
1712
+ else if (to === 'master') {
1713
+ dest = Audio.master;
1714
+ }
1715
+ nodes[key].connect(dest);
1716
+ });
1717
+ });
1718
+ return nodes;
1719
+ }
1720
+ }
1721
+ Audio.context = null;
1722
+ Audio.master = null;
1723
+ Audio.initialize();
1724
+
1725
+ function synthesizer(props, effects) {
1726
+ return new Synthesizer(props, effects);
1727
+ }
1728
+ function clamp(value, min, max) {
1729
+ return Math.min(Math.max(value, min), max);
1730
+ }
1731
+ function isObject(val) {
1732
+ return val !== null && typeof val === 'object';
1733
+ }
1734
+ function isNumber(val) {
1735
+ return typeof val === 'number' && !isNaN(val);
1736
+ }
1737
+ class Synthesizer {
1738
+ static initialize() {
1739
+ window.addEventListener('touchstart', initialize, true);
1740
+ window.addEventListener('mousedown', initialize, true);
1741
+ function initialize() {
1742
+ new Synthesizer().press(440);
1743
+ window.removeEventListener('touchstart', initialize, true);
1744
+ window.removeEventListener('mousedown', initialize, true);
1745
+ }
1746
+ }
1747
+ constructor({ oscillator = null, filter = null, amp = null } = {}, { bmp = null, reverb = null, delay = null } = {}) {
1748
+ this.oscillator = isObject(oscillator) ? oscillator : {};
1749
+ this.oscillator.type = setType(this.oscillator.type, ['sine', 'triangle', 'square', 'sawtooth']);
1750
+ this.oscillator.envelope = setEnvelope(this.oscillator.envelope, -36, +36);
1751
+ this.oscillator.LFO = setLFO(this.oscillator.LFO, 36);
1752
+ this.filter = isObject(filter) ? filter : {};
1753
+ this.filter.type = setType(this.filter.type, ['lowpass', 'highpass', 'bandpass']);
1754
+ this.filter.Q = isNumber(this.filter.Q) ? clamp(this.filter.Q, 0, 32) : 0;
1755
+ // cutoffはundefinedを使う
1756
+ this.filter.cutoff = isNumber(this.filter.cutoff) ? clamp(this.filter.cutoff, 4, 8192) : undefined;
1757
+ this.filter.envelope = setEnvelope(this.filter.envelope, -36, +36);
1758
+ this.filter.LFO = setLFO(this.filter.LFO, 36);
1759
+ this.amp = isObject(amp) ? amp : {};
1760
+ this.amp.envelope = setEnvelope(this.amp.envelope, 0, 1);
1761
+ this.amp.LFO = setLFO(this.amp.LFO, 36);
1762
+ this.bmp = isNumber(bmp) ? clamp(bmp, 60, 240) : 120;
1763
+ this.options = { bmp: this.bmp };
1764
+ this.reverb = isObject(reverb) ? reverb : {};
1765
+ this.reverb.time = isNumber(this.reverb.time) ? clamp(this.reverb.time, 0, 2000) : 0.0;
1766
+ this.reverb.mix = isNumber(this.reverb.mix) ? clamp(this.reverb.mix, 0, 1.0) : 0.0;
1767
+ this.delay = isObject(delay) ? delay : {};
1768
+ this.delay.time = isNumber(this.delay.time) ? clamp(this.delay.time, 0, 2000) : 0.0;
1769
+ this.delay.feedback = isNumber(this.delay.feedback) ? clamp(this.delay.feedback, 0.0, 0.9) : 0.0;
1770
+ this.delay.mix = isNumber(this.delay.mix) ? clamp(this.delay.mix, 0.0, 1.0) : 0.0;
1771
+ function setType(type, list, value = 0) {
1772
+ return list.includes(type) ? type : list[value];
1773
+ }
1774
+ function setEnvelope(envelope, minAmount, maxAmount) {
1775
+ var _a, _b, _c, _d;
1776
+ if (!isObject(envelope))
1777
+ return null;
1778
+ const result = {
1779
+ amount: isNumber(envelope.amount) ? clamp(envelope.amount, minAmount, maxAmount) : 0,
1780
+ ADSR: [
1781
+ isNumber((_a = envelope.ADSR) === null || _a === void 0 ? void 0 : _a[0]) ? clamp(envelope.ADSR[0], 0, 8000) : 0,
1782
+ isNumber((_b = envelope.ADSR) === null || _b === void 0 ? void 0 : _b[1]) ? clamp(envelope.ADSR[1], 0, 8000) : 0,
1783
+ isNumber((_c = envelope.ADSR) === null || _c === void 0 ? void 0 : _c[2]) ? clamp(envelope.ADSR[2], 0, 1) : 0,
1784
+ isNumber((_d = envelope.ADSR) === null || _d === void 0 ? void 0 : _d[3]) ? clamp(envelope.ADSR[3], 0, 8000) : 0,
1785
+ ]
1786
+ };
1787
+ return result;
1788
+ }
1789
+ function setLFO(LFO, maxAmount) {
1790
+ if (!isObject(LFO))
1791
+ return null;
1792
+ const oscTypes = ['sine', 'triangle', 'square', 'sawtooth'];
1793
+ const type = oscTypes.includes(LFO.type)
1794
+ ? LFO.type
1795
+ : 'sine';
1796
+ const result = {
1797
+ amount: isNumber(LFO.amount) ? clamp(LFO.amount, 0, maxAmount) : 0,
1798
+ type,
1799
+ rate: isNumber(LFO.rate) ? clamp(LFO.rate, 1, 128) : 1,
1800
+ };
1801
+ return result;
1802
+ }
1803
+ }
1804
+ press(frequency, duration = null, wait = 0.0) {
1805
+ frequency = typeof frequency === 'string' ? Synthesizer.keymap[frequency] : frequency;
1806
+ duration = typeof duration === 'string' ? (Synthesizer.notemap[duration] * 60 / this.options.bmp) : (duration !== null ? (duration / 1000) : duration);
1807
+ const start = Audio.context.currentTime + wait / 1000;
1808
+ let stop = null;
1809
+ const params = {};
1810
+ if (this.filter.type && this.filter.cutoff) {
1811
+ params.oscillator = ['Oscillator', {}, 'filter'];
1812
+ params.filter = ['BiquadFilter', {}, 'amp'];
1813
+ }
1814
+ else {
1815
+ params.oscillator = ['Oscillator', {}, 'amp'];
1816
+ }
1817
+ params.amp = ['Gain', { gain: 0.0 }, 'target'];
1818
+ params.target = ['Gain', { gain: 1.0 }, 'master'];
1819
+ if (this.reverb.time > 0.0 && this.reverb.mix > 0.0) {
1820
+ params.amp.push('convolver');
1821
+ params.convolver = ['Convolver', { buffer: impulseResponse({ time: this.reverb.time }) }, 'convolverDepth'];
1822
+ params.convolverDepth = ['Gain', { gain: 1.0 }, 'master'];
1823
+ }
1824
+ if (this.delay.time > 0.0 && this.delay.mix > 0.0) {
1825
+ params.amp.push('delay');
1826
+ params.delay = ['Delay', {}, 'delayDepth', 'delayFeedback'];
1827
+ params.delayDepth = ['Gain', { gain: 1.0 }, 'master'];
1828
+ params.delayFeedback = ['Gain', { gain: this.delay.feedback }, 'delay'];
1829
+ }
1830
+ if (this.oscillator.LFO) {
1831
+ params.oscillatorLFO = ['Oscillator', {}, 'oscillatorLFODepth'];
1832
+ params.oscillatorLFODepth = ['Gain', {}, 'oscillator.frequency'];
1833
+ }
1834
+ if (this.filter.LFO) {
1835
+ params.filterLFO = ['Oscillator', {}, 'filterLFODepth'];
1836
+ params.filterLFODepth = ['Gain', {}, 'filter.frequency'];
1837
+ }
1838
+ if (this.amp.LFO) {
1839
+ params.ampLFO = ['Oscillator', {}, 'ampLFODepth'];
1840
+ params.ampLFODepth = ['Gain', {}, 'amp.gain'];
1841
+ }
1842
+ const nodes = Audio.connect(params);
1843
+ nodes.oscillator.type = this.oscillator.type;
1844
+ nodes.oscillator.frequency.value = clamp(frequency, 10.0, 5000.0);
1845
+ if (this.filter.type && this.filter.cutoff) {
1846
+ nodes.filter.type = this.filter.type;
1847
+ nodes.filter.frequency.value = this.filter.cutoff;
1848
+ }
1849
+ if (this.reverb.time > 0.0 && this.reverb.mix > 0.0) {
1850
+ nodes.target.gain.value *= (1.0 - this.reverb.mix);
1851
+ nodes.convolverDepth.gain.value *= this.reverb.mix;
1852
+ }
1853
+ if (this.delay.time > 0.0 && this.delay.mix > 0.0) {
1854
+ console.log(this.delay.time / 1000);
1855
+ nodes.delay.delayTime.value = this.delay.time / 1000;
1856
+ nodes.target.gain.value *= (1.0 - this.delay.mix);
1857
+ nodes.delayDepth.gain.value *= this.delay.mix;
1858
+ }
1859
+ {
1860
+ if (this.oscillator.LFO) {
1861
+ nodes.oscillatorLFODepth.gain.value = frequency * (Math.pow(2.0, this.oscillator.LFO.amount / 12.0) - 1.0);
1862
+ nodes.oscillatorLFO.type = this.oscillator.LFO.type;
1863
+ nodes.oscillatorLFO.frequency.value = this.oscillator.LFO.rate;
1864
+ nodes.oscillatorLFO.start(start);
1865
+ }
1866
+ if (this.filter.LFO) {
1867
+ nodes.filterLFODepth.gain.value = frequency * (Math.pow(2.0, this.filter.LFO.amount / 12.0) - 1.0);
1868
+ nodes.filterLFO.type = this.filter.LFO.type;
1869
+ nodes.filterLFO.frequency.value = this.filter.LFO.rate;
1870
+ nodes.filterLFO.start(start);
1871
+ }
1872
+ if (this.amp.LFO) {
1873
+ nodes.ampLFODepth.gain.value = this.amp.LFO.amount;
1874
+ nodes.ampLFO.type = this.amp.LFO.type;
1875
+ nodes.ampLFO.frequency.value = this.amp.LFO.rate;
1876
+ nodes.ampLFO.start(start);
1877
+ }
1878
+ if (this.oscillator.envelope) {
1879
+ const amount = frequency * (Math.pow(2.0, this.oscillator.envelope.amount / 12.0) - 1.0);
1880
+ startEnvelope(nodes.oscillator.frequency, frequency, amount, this.oscillator.envelope.ADSR);
1881
+ }
1882
+ if (this.filter.envelope) {
1883
+ const amount = this.filter.cutoff * (Math.pow(2.0, this.filter.envelope.amount / 12.0) - 1.0);
1884
+ startEnvelope(nodes.filter.frequency, this.filter.cutoff, amount, this.filter.envelope.ADSR);
1885
+ }
1886
+ if (this.amp.envelope) {
1887
+ startEnvelope(nodes.amp.gain, 0.0, this.amp.envelope.amount, this.amp.envelope.ADSR);
1888
+ }
1889
+ nodes.oscillator.start(start);
1890
+ }
1891
+ if (duration !== null) {
1892
+ release.call(this);
1893
+ }
1894
+ function release() {
1895
+ duration = duration !== null && duration !== void 0 ? duration : (Audio.context.currentTime - start);
1896
+ if (this.amp.envelope) {
1897
+ const ADSR = this.amp.envelope.ADSR;
1898
+ const adsr = [ADSR[0] / 1000, ADSR[1] / 1000, ADSR[2], ADSR[3] / 1000];
1899
+ const rate = adsr[0] === 0.0 ? 1.0 : Math.min(duration / adsr[0], 1.0);
1900
+ stop = start + Math.max((adsr[0] + adsr[1]) * rate, duration) + adsr[3];
1901
+ }
1902
+ else {
1903
+ stop = start + duration;
1904
+ }
1905
+ if (this.oscillator.LFO) {
1906
+ nodes.oscillatorLFO.stop(stop);
1907
+ }
1908
+ if (this.amp.LFO) {
1909
+ nodes.ampLFO.stop(stop);
1910
+ }
1911
+ if (this.oscillator.envelope) {
1912
+ const amount = frequency * (Math.pow(2.0, this.oscillator.envelope.amount / 12.0) - 1.0);
1913
+ stopEnvelope(nodes.oscillator.frequency, frequency, amount, this.oscillator.envelope.ADSR);
1914
+ }
1915
+ if (this.filter.envelope) {
1916
+ const amount = this.filter.cutoff * (Math.pow(2.0, this.filter.envelope.amount / 12.0) - 1.0);
1917
+ stopEnvelope(nodes.filter.frequency, this.filter.cutoff, amount, this.filter.envelope.ADSR);
1918
+ }
1919
+ if (this.amp.envelope) {
1920
+ stopEnvelope(nodes.amp.gain, 0.0, this.amp.envelope.amount, this.amp.envelope.ADSR);
1921
+ }
1922
+ nodes.oscillator.stop(stop);
1923
+ }
1924
+ function startEnvelope(param, base, amount, ADSR) {
1925
+ const adsr = [ADSR[0] / 1000, ADSR[1] / 1000, ADSR[2], ADSR[3] / 1000];
1926
+ param.value = base;
1927
+ param.setValueAtTime(base, start);
1928
+ param.linearRampToValueAtTime(base + amount, start + adsr[0]);
1929
+ param.linearRampToValueAtTime(base + amount * adsr[2], start + (adsr[0] + adsr[1]));
1930
+ }
1931
+ function stopEnvelope(param, base, amount, ADSR) {
1932
+ const adsr = [ADSR[0] / 1000, ADSR[1] / 1000, ADSR[2], ADSR[3] / 1000];
1933
+ const rate = adsr[0] === 0.0 ? 1.0 : Math.min(duration / adsr[0], 1.0);
1934
+ if (rate < 1.0) {
1935
+ param.cancelScheduledValues(start);
1936
+ param.setValueAtTime(base, start);
1937
+ param.linearRampToValueAtTime(base + amount * rate, start + adsr[0] * rate);
1938
+ param.linearRampToValueAtTime(base + amount * rate * adsr[2], start + (adsr[0] + adsr[1]) * rate);
1939
+ }
1940
+ param.linearRampToValueAtTime(base + amount * rate * adsr[2], start + Math.max((adsr[0] + adsr[1]) * rate, duration));
1941
+ param.linearRampToValueAtTime(base, start + Math.max((adsr[0] + adsr[1]) * rate, duration) + adsr[3]);
1942
+ }
1943
+ return {
1944
+ release: release.bind(this),
1945
+ };
1946
+ }
1947
+ }
1948
+ Synthesizer.keymap = {
1949
+ 'A0': 27.500, 'A#0': 29.135, 'B0': 30.868,
1950
+ 'C1': 32.703, 'C#1': 34.648, 'D1': 36.708, 'D#1': 38.891, 'E1': 41.203, 'F1': 43.654, 'F#1': 46.249, 'G1': 48.999, 'G#1': 51.913, 'A1': 55.000, 'A#1': 58.270, 'B1': 61.735,
1951
+ 'C2': 65.406, 'C#2': 69.296, 'D2': 73.416, 'D#2': 77.782, 'E2': 82.407, 'F2': 87.307, 'F#2': 92.499, 'G2': 97.999, 'G#2': 103.826, 'A2': 110.000, 'A#2': 116.541, 'B2': 123.471,
1952
+ 'C3': 130.813, 'C#3': 138.591, 'D3': 146.832, 'D#3': 155.563, 'E3': 164.814, 'F3': 174.614, 'F#3': 184.997, 'G3': 195.998, 'G#3': 207.652, 'A3': 220.000, 'A#3': 233.082, 'B3': 246.942,
1953
+ 'C4': 261.626, 'C#4': 277.183, 'D4': 293.665, 'D#4': 311.127, 'E4': 329.628, 'F4': 349.228, 'F#4': 369.994, 'G4': 391.995, 'G#4': 415.305, 'A4': 440.000, 'A#4': 466.164, 'B4': 493.883,
1954
+ 'C5': 523.251, 'C#5': 554.365, 'D5': 587.330, 'D#5': 622.254, 'E5': 659.255, 'F5': 698.456, 'F#5': 739.989, 'G5': 783.991, 'G#5': 830.609, 'A5': 880.000, 'A#5': 932.328, 'B5': 987.767,
1955
+ 'C6': 1046.502, 'C#6': 1108.731, 'D6': 1174.659, 'D#6': 1244.508, 'E6': 1318.510, 'F6': 1396.913, 'F#6': 1479.978, 'G6': 1567.982, 'G#6': 1661.219, 'A6': 1760.000, 'A#6': 1864.655, 'B6': 1975.533,
1956
+ 'C7': 2093.005, 'C#7': 2217.461, 'D7': 2349.318, 'D#7': 2489.016, 'E7': 2637.020, 'F7': 2793.826, 'F#7': 2959.955, 'G7': 3135.963, 'G#7': 3322.438, 'A7': 3520.000, 'A#7': 3729.310, 'B7': 3951.066,
1957
+ 'C8': 4186.009,
1958
+ };
1959
+ Synthesizer.notemap = {
1960
+ '1m': 4.000, '2n': 2.000, '4n': 1.000, '8n': 0.500, '16n': 0.250, '32n': 0.125,
1961
+ };
1962
+ Synthesizer.initialize();
1963
+ function impulseResponse({ time, decay = 2.0 }) {
1964
+ const length = Audio.context.sampleRate * time / 1000;
1965
+ const impulse = Audio.context.createBuffer(2, length, Audio.context.sampleRate);
1966
+ const ch0 = impulse.getChannelData(0);
1967
+ const ch1 = impulse.getChannelData(1);
1968
+ for (let i = 0; i < length; i++) {
1969
+ ch0[i] = (2 * Math.random() - 1) * Math.pow(1 - i / length, decay);
1970
+ ch1[i] = (2 * Math.random() - 1) * Math.pow(1 - i / length, decay);
1971
+ }
1972
+ return impulse;
1973
+ }
1974
+
1975
+ const basics = {
1976
+ Screen,
1977
+ UserEvent,
1978
+ ResizeEvent,
1979
+ ModalFrame,
1980
+ ModalContent,
1981
+ AccordionFrame,
1982
+ AccordionButton,
1983
+ AccordionBullet,
1984
+ AccordionContent,
1985
+ TabFrame,
1986
+ TabButton,
1987
+ TabContent,
1988
+ PanelFrame,
1989
+ PanelGroup,
1990
+ InputFrame,
1991
+ DragFrame,
1992
+ DragTarget,
1993
+ VirtualStick,
1994
+ VirtualDPad,
1995
+ VirtualButton,
1996
+ };
1997
+ const audio = {
1998
+ synthesizer
1999
+ };
2000
+ const xnew = Object.assign(xnew$1, {
2001
+ basics,
2002
+ audio,
2003
+ });
2004
+
2005
+ export { xnew as default };