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