-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
4171 lines (3626 loc) · 138 KB
/
ui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use-strict'
class UI {
constructor() {
this._tabs = {};
this._settings = {};
let allTabs = document.querySelectorAll('.tab[id]');
for(let i = 0; i < allTabs.length; i++) {
let elem = allTabs[i];
let id = elem.id;
let instance;
if(id == "") continue;
if(this._tabs[id]) {console.warn("Duplicate ID in UI tabs!", id); continue;}
switch(id) {
case 'tags': instance = new TagsTab(elem, this); break;
case 'parts': instance = new PartsTab(elem, this); break;
case 'states': instance = new StatesTab(elem, this); break;
case 'particles': instance = new ParticlesTab(elem, this); break;
case 'groups': instance = new GroupsTab(elem, this); break;
case 'sounds': instance = new SoundsTab(elem, this); break;
case 'preview': instance = new PreviewTab(elem, this); break;
default: continue; break;
}
this._tabs[id] = instance;
}
this._boundHandleTabChange = this.handleTabChange.bind(this);
document.getElementById('navigation').addEventListener('click', this._boundHandleTabChange);
}
destroy() {
document.getElementById('navigation').removeEventListener('click', this._boundHandleTabChange);
for(let i in this._tabs) {
try{this._tabs[i].destroy();}
catch(e) {console.error("Could not destroy tab!", e);}
}
this._tabs = null;
}
handleTabChange(e) {
let target = e.target || null;
if(target == null) return false;
if(!target.classList.contains('tab_header')) return true;
let forId = target.dataset['for'] || null;
if(forId == null) return true;
let tab = document.getElementById(forId);
if(tab == null || tab.classList.contains('active')) return true;
let activeTabHeaders = document.querySelectorAll('.tab_header.active');
for(let i = 0; i < activeTabHeaders.length; i++) {activeTabHeaders[i].classList.remove('active');}
target.classList.add('active');
for(let i in this._tabs) {
if(i == forId) continue;
this._tabs[i].onUnload();
}
if(this._tabs[forId]) {
try {this._tabs[forId].onLoad();}
catch(e) {console.error("Error in "+forId+"'s onLoad handler", e);}
}
}
setSetting(name, value) {
this._settings[name] = value;
}
getSetting(name, defaultValue) {
return Object.hasOwn(this._settings, name) ? this._settings[name] : defaultValue;
}
}
class UITab {
constructor(elem, parent) {
if(new.target === UITab) throw TypeError("Trying to instantiate abstract class UITab");
this._parent = parent;
this._tab = elem;
this._navbar = elem.children[0];
this._content = elem.children[1];
this._nests = [];
this._active = false;
this._boundHandleNavigation = this.handleNavigation.bind(this);
this._navbar.addEventListener('click', this._boundHandleNavigation);
this._boundResetNavigationLayers = this.resetNavigationLayers.bind(this);
}
destroy() {
this._navbar.removeEventListener('click', this._boundHandleNavigation);
this._parent = null;
}
get tab() {return this._tab;}
set tab(val) {
this._tab = val;
this._navbar = this._tab.children[0];
this._content = this._tab.children[1];
}
get navbar() {return this._navbar;}
get content() {return this._content;}
addNavigationLayer(name, data) {
this._nests.push({name: name, data: data});
this.buildNavbar();
this.buildLayeredContent(data);
}
removeNavigationLayer(depth = 1) {
depth = parseInt(depth) || 1;
this._nests.splice(depth * -1);
let data = (this._nests[Math.max(this._nests.length - 1, 0)] || {}).data || null;
this.buildNavbar();
this.buildLayeredContent(data);
}
resetNavigationLayers() {
this._nests = [];
}
buildNavbar() {
let highest = this._nests.length - 1;
let backButton = document.createElement('div');
backButton.classList.add('back_button');
if(highest == -1) backButton.classList.add('invisible');
backButton.append("↩");
this._navbar.replaceChildren(backButton);
let navelem = document.createElement('div');
navelem.classList.add('nav_element');
navelem.append("Overview");
navelem.dataset['depth'] = highest + 1;
this._navbar.append(navelem);
for(let i = 0; i < this._nests.length; i++) {
let data = this._nests[i];
let marker = document.createElement('span');
marker.classList.add('no_select');
marker.innerText = "—→";
this._navbar.append(marker);
navelem = document.createElement('div');
navelem.classList.add('nav_element');
navelem.append(String(data.name));
navelem.dataset['depth'] = (highest - i);
this._navbar.append(navelem);
}
navelem.classList.add('current');
}
handleNavigation(e) {
let target = e.target;
if(target == null) return false;
if(target.classList.contains('back_button') && !target.classList.contains('invisible')) this.removeNavigationLayer();
else if(target.classList.contains('nav_element') && !target.classList.contains('current')) {
let depth = target.dataset['depth'];
this.removeNavigationLayer(depth);
}
return true;
}
buildLayeredContent(data) {return;}
onLoad() {if(this._active) return; this._tab.classList.add('active'); this._active = true; animator.addEventListener('load', this._boundResetNavigationLayers);}
onUnload() {if(!this._active) return; this._tab.classList.remove('active'); this._active = false; animator.removeEventListener('load', this._boundResetNavigationLayers);}
}
class PreviewTab extends UITab {
#_isClean = true;
constructor(elem, parent) {
super(elem, parent);
this._animator = animator;
this._output = this._content.querySelector('pre');
this._anchor = this._output.parentElement;
if(this._output.innerHTML != "") this.#_isClean = false;
this._wasFull = false;
this._boundUpdatePreview = this.updatePreview.bind(this);
this._boundResetPreview = this.resetPreview.bind(this);
this._boundScrollLoad = this.scrollLoad.bind(this);
this._boundDynamicLoad = this.dynamicLoad.bind(this);
if(elem.classList.contains('active')) this.onLoad();
}
destroy() {
this.resetPreview();
this._animator = null;
super.destroy();
}
onLoad() {
if(this._active) return;
super.onLoad();
if(!this._animator) return;
this.updatePreview();
this._wasFull = this._parent.getSetting('fullPreview', false);
if(!this._wasFull) document.addEventListener('scroll', this._boundScrollLoad);
animator.addEventListener('load', this._boundUpdatePreview);
}
onUnload() {
if(!this._active) return;
super.onUnload();
document.removeEventListener('scroll', this._boundScrollLoad);
animator.removeEventListener('load', this._boundUpdatePreview);
let parentElem = this._output.parentElement;
this.resetPreview();
}
updatePreview() {
let result = this._animator.print(Animator.COMPRESSION_NONE);
this.resetPreview();
if(result == null) {this._output.innerText = "Project is empty."; this.#_isClean = false; return;}
// Colorization
let lines = result.split("\n");
this._currentList = lines;
for(let i = 0; i < lines.length; i+=100) {
let segment = document.createElement('div');
segment.classList.add('segment');
segment.style.setProperty('--line_count', Math.min(lines.length - i, 100));
this._output.append(segment);
}
this._wasFull = this._parent.getSetting('fullPreview', false);
// Delay to next frame for anti-lag
if(this._wasFull) {
let modalId = ToastModal.open("<span class='loading'>...</span>", false, 0);
window.requestAnimationFrame((e) => {this._boundDynamicLoad(0,100,0,modalId)});
} else window.requestAnimationFrame(this._boundScrollLoad);
let highestIndex = String(lines.length).length;
let indentWidth = ''+(highestIndex*8)+'px';
this._output.style.setProperty('--indent_width', indentWidth);
this.#_isClean = false;
}
softUpdatePreview() {
let full = this._parent.getSetting('fullPreview', false);
if(full == this._wasFull) return;
if(full) {
document.removeEventListener('scroll', this._boundScrollLoad);
let modalId = ToastModal.open("<span class='loading'>...</span>", false, 0);
this._output.style.setProperty('--counter_offset', '0');
window.requestAnimationFrame((e) => {this._boundDynamicLoad(0,100,0,modalId)});
} else {
document.addEventListener('scroll', this._boundScrollLoad);
window.requestAnimationFrame(this._boundScrollLoad);
}
this._wasFull = full;
}
resetPreview() {
if(this.#_isClean) return;
this._output.remove();
this._output = document.createElement('pre');
this._anchor.appendChild(this._output);
this._currentList = null;
this.#_isClean = true;
}
stylizeBlock(i=0,n=100,o=0) {
if(!this._currentList || this._currentList.length == 0) return;
// i = start index array, n = number of entries from array, o = offset segment
i = Math.max(parseInt(i)||0,0);
n = Math.max(parseInt(n)||0,0) + i;
o = Math.max(parseInt(o)||0,0)
let fragment = document.createDocumentFragment();
for(true; i < n && i < this._currentList.length; i++) {
let line = this._currentList[i];
let matches = line.match(/^(\s*)("[\w-]+": ?)?("[^"]*"|[\w.+-]*)?([[{}\]]*,?)?$/m);
let result = document.createElement('label');
result.classList.add('line');
let sub;
if(matches[1]) result.append(matches[1]);
if(matches[2]) {
sub = document.createElement('span');
sub.classList.add('json_key');
sub.append(matches[2].replace(/[: ]/g, ''));
result.append(sub);
result.append(": ");
}
if(matches[3]) {
sub = document.createElement('span');
if(matches[3].match(/^(?:true|false|null)$/i)) sub.classList.add('json_expression');
else if(matches[3].match(/^[0-9+\-\.,e]+$/)) sub.classList.add('json_number');
sub.append(matches[3]);
result.append(sub);
}
if(matches[4]) result.append(matches[4]);
fragment.append(result);
}
this._output.children[o].append(fragment);
let actHeight = this._output.children[o].offsetHeight;
let overflow = (actHeight - 1500);
overflow = overflow == 0 ? 0 : overflow / 15;
this._output.children[o].style.setProperty('--overflowing_lines', overflow);
}
scrollLoad(e) {
let start = 0, end = 0;
let isIn = false;
let vh = window.innerHeight;
let o = window.scrollY;
for(let i = Math.floor(o==0?0:o/2000); i < this._output.children.length; i++) { // Begin check higher based on offset. 2000px instead of 1500px to have conservative overflow wiggleroom
let elem = this._output.children[i];
let rect = elem.getBoundingClientRect();
if(isIn) {
if(rect.bottom < -50 || rect.top > vh + 50) {
// Bottom further than 50 pixels out top or top further than 50 pixels out bottom. Consider invisible.
isIn = false;
end = i;
break;
}
} else {
if(rect.bottom >= -50 && rect.top <= vh + 50) {
// Bottom is nearly visible top or top nearly visible on bottom. Consider visible.
isIn = true;
start = i;
}
}
}
let unloadStart = start - 2, unloadEnd = end + 2;
// Load currently visible if not loaded.
for(let i = start; i <= end && i < this._output.children.length; i++) {
let elem = this._output.children[i];
if(elem.children.length > 0) continue;
this.stylizeBlock(100*(start+1), 100, i);
}
// Unload everything outside the boundaries
if(unloadStart >= 0) {
for(let i = unloadStart; i >= 0; i--) {
this._output.children[i].innerText = "";
}
}
if(unloadEnd < this._output.children.length) {
for(let i = unloadEnd; i < this._output.children.length; i++) {
this._output.children[i].innerText = "";
}
}
let offset = 0;
for(let i = start; i >= 0; i--) {
if(this._output.children[i].children.length > 0) continue;
offset = (i+1) * 100;
break;
}
this._output.style.setProperty('--counter_offset', offset);
}
dynamicLoad(i=0,n=100,o=0,modalId=null) {
i = Math.max(parseInt(i)||0,0);
o = Math.max(parseInt(o)||0,0);
// i = start index string, n = chunk size, j = segment offset
let handleToast = modalId==null ? function(){} : function(){ToastModal.change(modalId,null,null,1);}
let elem = this._output.children[o];
if(!elem) {handleToast(); return;}
if(!this._currentList[i]) {handleToast(); return;}
while(elem.children.length > 0) {
// Skip already populated segments this tick
o++;
i += n;
elem = this._output.children[o];
if(!elem) {handleToast(); return;}
if(!this._currentList[i]) {handleToast(); return;}
}
this.stylizeBlock(i,n,o); // Load current block
window.requestAnimationFrame((e) => {this._boundDynamicLoad(i+n,n,++o,modalId)}); // Next tick, load next block
}
}
class TagsTab extends UITab {
constructor(elem, parent) {
super(elem, parent);
this._animator = animator;
this._boundHandleAction = this.handleAction.bind(this);
this._boundHandleDblClick = this.handleDblClick.bind(this);
this._content.addEventListener('click', this._boundHandleAction);
this._content.addEventListener('dblclick', this._boundHandleDblClick);
this._boundUpdateView = this.updateView.bind(this);
this._tagsElem = this._content.querySelector('.tags');
this._tags = [];
if(elem.classList.contains('active')) this.onLoad();
}
destroy() {
this._content.removeEventListener('click', this._boundHandleAction);
this._content.removeEventListener('dblclick', this._boundHandleDblClick);
this._animator.removeEventListener('load', this._boundUpdateView);
this._tagsElem.innerHTML = "";
this._tags = [];
this._animator = null;
super.destroy();
}
onLoad() {
super.onLoad();
this._animator.addEventListener('load', this._boundUpdateView);
this.updateView();
}
onUnload() {
this._tagsElem.innerHTML = "";
this._tags = [];
this._animator.removeEventListener('load', this._boundUpdateView);
super.onUnload();
}
handleAction(e) {
let target = e.target;
if(target == null) return false;
if(target.tagName != "BUTTON") return false;
let action = target.dataset['action'];
if(action == "addTag") this.addTag();
else if (action == "deleteTag") this.removeTag(target);
return true;
}
handleDblClick(e) {
let target = e.target;
if(target == null) return false;
if(!target.classList.contains('editable')) return false;
this.editTag(target);
}
addTag() {
Modal.confirm(
"Add Global Tag",
"Enter name and value of new global tag.<br><br><input class='textfield large' type='text' id='tagName' placeholder='Tag Name' /><br><input class='textfield large' type='text' id='tagVal' placeholder='Value' />",
(function() {
let name = document.getElementById('tagName').value;
let val = document.getElementById('tagVal').value;
if(name != "") {
this.checkAddTag(name, val);
return true;
}
}).bind(this),
null,
"Add",
"Abort",
"medium",
false
);
}
checkAddTag(name, val) {
if(animator._tags[name]) {
let id = this._tags.indexOf(name);
Modal.confirm(
"Overwrite Global Tag",
"A tag with this name already exists!<br><br>Do you want to overwrite the value instead?",
(function() {
if(id == -1) this.doAddTag(name, val);
else this.doEditTag(id, val);
return true;
}).bind(this),
null,
"Overwrite",
"Abort",
"small",
true
);
}
else this.doAddTag(name, val);
}
doAddTag(name, val, apply=true) {
let newTag = document.createElement('div');
newTag.classList.add('tag');
newTag.dataset['name'] = name;
newTag.dataset['value'] = val;
let cell1 = document.createElement('div');
cell1.classList.add('cell');
cell1.innerText = name + ":";
newTag.append(cell1);
let cell2 = document.createElement('div');
cell2.classList.add('cell', 'grow', 'editable');
cell2.innerText = val;
newTag.append(cell2);
let cell3 = document.createElement('div');
cell3.classList.add('cell', 'controls', 'hover');
let button = document.createElement('button');
button.classList.add('modern', 'tiny');
button.innerText = '-';
button.title = "Remove Tag";
button.dataset['action'] = "deleteTag";
cell3.append(button);
newTag.append(cell3);
this._tagsElem.append(newTag);
this._tags.push(name);
if(apply) animator.setTag(name, val);
}
editTag(elem) {
let orgValue = elem.parentElement.dataset['value'];
let name = elem.parentElement.dataset['name'];
let id = this._tags.indexOf(name);
if(id == -1) return;
let input = document.createElement('input');
input.type = "text";
input.classList.add('textfield', 'large');
input.placeholder = "Value";
input.value = orgValue;
let self = this;
input.addEventListener('blur', function() {
let val = this.value;
if(val == orgValue) this.parentElement.innerText = orgValue;
else self.doEditTag(id, val);
});
input.addEventListener('keydown', function(e) {if(e.key == "Enter") this.blur();});
elem.replaceChildren(input);
input.focus();
}
doEditTag(id, val) {
if(id < 0) return;
this._tagsElem.children[id].children[1].innerText = val;
this._animator.setTag(this._tags[id], val);
}
removeTag(elem) {
let name = elem.parentElement.parentElement.dataset['name']; // Button -> Cell -> Tag (with data)
let id = this._tags.indexOf(name);
if(id == -1) return;
Modal.confirm(
"Delete Global Tag",
"Are you sure you want to delete the global tag \""+name+"\"?",
(function() {this.doRemoveTag(id); return true;}).bind(this),
null,
"Delete",
"Abort",
"small",
true
);
}
doRemoveTag(id) {
this._tagsElem.children[id].remove();
this._animator.setTag(this._tags[id], null);
this._tags.splice(id, 1);
}
updateView() {
this._tagsElem.innerHTML = "";
for(let i in this._animator._tags) {
this.doAddTag(i, this._animator._tags[i], false);
}
}
}
class PartsTab extends UITab {
constructor(elem, parent) {
super(elem, parent);
this._animator = animator;
this._boundHandleAction = this.handleAction.bind(this);
this._boundHandleDblClick = this.handleDblClick.bind(this);
this._boundHandleChange = this.handleChange.bind(this);
this._content.addEventListener('click', this._boundHandleAction);
this._content.addEventListener('dblclick', this._boundHandleDblClick);
this._content.addEventListener('change', this._boundHandleChange);
this._boundBuildNavbar = this.buildNavbar.bind(this);
this._boundBuildLayeredContent = this.buildLayeredContent.bind(this);
this._contentList = [];
if(elem.classList.contains('active')) this.onLoad();
}
destroy() {
this._content.removeEventListener('click', this._boundHandleAction);
this._content.removeEventListener('dblclick', this._boundHandleDblClick);
this._content.removeEventListener('change', this._boundHandleChange);
this._content.innerHTML = '';
this._contentList = [];
this._animator.removeEventListener('load', this._boundBuildNavbar);
this._animator.removeEventListener('load', this._boundBuildLayeredContent);
this._animator = null;
super.destroy();
}
onLoad() {
super.onLoad();
this._animator.addEventListener('load', this._boundBuildNavbar);
this._animator.addEventListener('load', this._boundBuildLayeredContent);
this.buildNavbar();
this.buildLayeredContent();
}
onUnload() {
this._content.innerHTML = '';
this._contentList = [];
this._nests = [];
this._animator.removeEventListener('load', this._boundBuildNavbar);
this._animator.removeEventListener('load', this._boundBuildLayeredContent);
super.onUnload();
}
handleAction(e) {
let target = e.target;
if(target == null) return false;
if(target.tagName == "BUTTON") {
let action = target.dataset['action'];
if(action == "zEdit") this.addNavigationLayer("Z-Editor", {layer: 0});
else if(action == "zEditReload") this.buildLayeredContent(this._nests[this._nests.length-1].data);
else if(action == "addPart") this.addPart(target);
else if(action == "editPart") this.openPart(target);
else if(action == "deletePart") this.removePart(target);
else if(action == "addGroup") this.addGroup(target);
else if(action == "deleteGroup") this.removeGroup(target);
else if(action == "addPartState") this.addPartState(target);
else if(action == "editPartState") this.openPartState(target);
else if(action == "deletePartState") this.removePartState(target);
else if(action == "addAnimationState") this.addAnimationState(target);
else if(action == "editAnimationState") this.openAnimationState(target);
else if(action == "deleteAnimationState") this.removeAnimationState(target);
else if(action == "addProperty") this.addProperty(target);
else if(action == "deleteProperty") this.removeProperty(target);
else if(action == "addFrameProperty") this.addFrameProperty(target);
else if(action == "deleteFrameProperty") this.removeFrameProperty(target);
else if(action == "addFramePropertyValue") this.addFramePropertyValue(target);
else if(action == "deleteFramePropertyValue") this.removeFramePropertyValue(target);
}
return true;
}
handleDblClick(e) {
let target = e.target;
if(target == null) return false;
if(!target.classList.contains('editable')) return false;
if(target.parentElement.classList.contains('stateType')) this.renameStateType(target);
else if(target.parentElement.classList.contains('state')) this.renameState(target);
}
handleChange(e) {
}
updateInput(e) {
let elem = e.target;
let id = elem.id;
let value = elem.value;
let checked = elem.checked || false;
let partId = parseInt(this._content.children[0].dataset['partId'])||0;
let part = this._animator._parts[partId];
switch(id) {
case 'centered': part.centered = checked; break;
case 'image': part.image = value == "" ? null : value; break;
case 'offset1': case 'offset2':
let o1 = document.getElementById('offset1').value;
let o2 = document.getElementById('offset2').value;
if(o1 == "" && o2 == "") part.offset = null;
else {
o1 = parseFloat(o1)||0;
o2 = parseFloat(o2)||0;
part.offset = [o1,o2];
}
break;
case 'zLevel': part.zLevel = value == "" ? null : value; break;
}
}
updateSelect(e) {
let elem = e.target;
let id = elem.id;
let value = parseInt(elem.value)||0;
let partId = parseInt(this._content.children[0].dataset['partId'])||0;
let part = this._animator._parts[partId];
switch(id) {
case 'anchorPart': part.anchorPart = value == -1 ? null : value; break;
}
}
buildLayeredContent(data = null) {
this._content.innerHTML = '';
this._contentList = [];
if(data == null) this.loadList();
else if(data.layer == 0) this.loadListZEdit();
else if(data.layer == 1) this.loadPart(data.id);
else if(data.layer == 2) this.loadPartState(data.id, data.refId);
else if(data.layer == 3) this.loadAnimationState(data.id, data.refId, data.subId);
else this.loadList();
}
loadList() {
this._contentElem = document.createElement('div');
this._contentElem.classList.add('parts');
this._content.append(this._contentElem);
let warning = document.createElement('div');
warning.classList.add('warning');
warning.innerText = 'No parts defined.';
this._content.append(warning);
let controls = document.createElement('div');
controls.classList.add('sticky_controls');
let button = document.createElement('button');
button.type = 'button';
button.classList.add('modern', 'small');
button.title = "Quickly adjust\nz-levels of parts";
button.innerText = 'Z-Editor';
button.dataset['action'] = 'zEdit';
controls.append(button);
button = document.createElement('button');
button.type = 'button';
button.classList.add('modern', 'tiny');
button.title = "Add new\npart";
button.innerText = '+';
button.dataset['action'] = 'addPart';
controls.append(button);
this._content.append(controls);
for(let i = 0; i < this._animator._parts.length; i++) {
this.doAddPart(this._animator._parts[i].name, i);
}
}
loadListZEdit() {
this._contentElem = document.createElement('div');
this._contentElem.classList.add('parts');
this._content.append(this._contentElem);
let warning = document.createElement('div');
warning.classList.add('warning');
warning.innerText = 'No parts defined.';
this._content.append(warning);
let controls = document.createElement('div');
controls.classList.add('sticky_controls');
let button = document.createElement('button');
button.type = 'button';
button.classList.add('modern', 'small');
button.title = "Reload\nZ-Levels";
button.innerText = 'Reload';
button.dataset['action'] = 'zEditReload';
controls.append(button);
button = document.createElement('button');
button.type = 'button';
button.classList.add('modern', 'tiny');
button.title = "Add new\npart";
button.innerText = '+';
button.dataset['action'] = 'addPart';
controls.append(button);
this._content.append(controls);
let list = this.getSortedList();
for(let i = 0; i < list.length; i++) {
this.doAddPartZEdit(list[i]);
}
}
openPart(elem) {
let partId = parseInt(elem.parentElement.parentElement.dataset['id']) || 0; // Button -> cell -> Part (with data)
let name = this._animator._parts[partId].name;
this.addNavigationLayer("Part: "+name, {layer: 1, id: partId});
}
loadPart(partId) {
this._contentElem = null;
let template = document.getElementById('fragment_part').content;
let clone = document.importNode(template, true);
let part = this._animator._parts[partId];
clone.children[0].dataset['partId'] = partId;
if(part.centered) clone.getElementById('centered').checked = true;
if(part.image != null) clone.getElementById('image').value = part.image;
let offset = part.offset;
if(offset != null) {
clone.getElementById('offset1').value = offset[0];
clone.getElementById('offset2').value = offset[1]
}
if(part.zLevel != null) clone.getElementById('zLevel').value = part.zLevel;
let select = clone.getElementById('anchorPart');
let parts = [];
let partIds = {};
for(let i = 0; i < this._animator._parts.length; i++) {
if(i == partId) continue;
let part = this._animator._parts[i];
parts.push(part.name);
partIds[part.name] = part.id;
}
parts.sort();
for(let i = 0; i < parts.length; i++) {
let option = document.createElement('option');
option.value = part.id;
option.innerText = parts[i];
if(part.anchorPart != null && part.anchorPart == partIds[parts[i]]) option.selected = true;
select.appendChild(option);
}
let inputs = clone.children[0].getElementsByTagName('input');
for(let i = 0; i < inputs.length; i++) {
if(inputs[i].type == "checkbox") inputs[i].addEventListener('change', this.updateInput.bind(this));
else inputs[i].addEventListener('blur', this.updateInput.bind(this));
}
let selects = clone.children[0].getElementsByTagName('select');
for(let i = 0; i < selects.length; i++) {selects[i].addEventListener('change', this.updateSelect.bind(this));}
this._content.appendChild(clone);
/* TODO transformation groups, part states */
let groups = part.groups;
for(let i = 0; i < groups.length; i++) {
this.doAddGroup(groups[i], false);
}
let states = part.partStates;
for(let i = 0; i < states.length; i++) {
this.doAddPartState(states[i].id, i);
}
}
openPartState(elem) {
let partId = parseInt(this._content.children[0].dataset['partId']) || 0;
let stateId = parseInt(elem.parentElement.parentElement.dataset['sid']) || 0; //Button -> cell -> state (with data)
let name = IDManager.getById(stateId).referenceName();
this.addNavigationLayer("Part State: "+name, {layer: 2, id: partId, refId: stateId});
}
loadPartState(partId, stateId) {
let container = document.createElement('div');
container.dataset['partId'] = partId;
container.dataset['partStateId'] = stateId;
this._contentElem = document.createElement('div');
this._contentElem.classList.add('animationStates');
container.append(this._contentElem);
let warning = document.createElement('div');
warning.classList.add('warning');
warning.innerText = 'No animation states defined.';
container.append(warning);
let controls = document.createElement('div');
controls.classList.add('sticky_controls');
let button = document.createElement('button');
button.type = 'button';
button.classList.add('modern', 'tiny');
button.title = "Add new\nanimation state";
button.innerText = '+';
button.dataset['action'] = 'addAnimationState';
controls.append(button);
container.append(controls);
this._content.append(container);
let states = IDManager.getById(stateId).animationStates;
for(let i = 0; i < states.length; i++) {
this.doAddAnimationState(states[i].id, i);
}
}
openAnimationState(elem) {
let partId = parseInt(this._content.children[0].dataset['partId']) || 0;
let stateId = parseInt(this._content.children[0].dataset['partStateId']) || 0;
let animId = parseInt(elem.parentElement.parentElement.dataset['aid']) || 0; //Button -> cell -> anim state (with data)
let name = IDManager.getById(animId).referenceName();
this.addNavigationLayer("Anim. State: "+name, {layer: 3, id: partId, refId: stateId, subId: animId});
}
loadAnimationState(partId, stateId, animId) {
this._contentElem = null;
this._content.innerHTML = "";
let template = document.getElementById('fragment_animation_state').content;
let clone = document.importNode(template, true);
let state = IDManager.getById(animId);
clone.children[0].dataset['partId'] = partId;
clone.children[0].dataset['partStateId'] = stateId;
clone.children[0].dataset['animStateId'] = animId;
this._content.appendChild(clone);
for(let i in state.properties) {
this.doAddProperty(i, state.properties[i]);
}
for(let i in state.frameProperties) {
this.doAddFrameProperty(i, state.frameProperties[i]);
}
}
addPart() {
Modal.confirm(
"Add Part",
"Enter name of the new part.<br><br><input class='textfield large' type='text' id='partName' placeholder='Part Name' />",
(function() {
let name = document.getElementById('partName').value;
if(name != "") {
this.checkAddPart(name);
return true;
}
}).bind(this),
null,
"Add",
"Abort",
"small",
false
);
}
checkAddPart(name) {
let isTaken = false;
for(let i = 0; i < this._animator._parts.length; i++) {
if(this._animator._parts[i].name == name) {isTaken = true; break;}
}
if(isTaken) ToastModal.open("Part name taken", true);
else this.doAddPart(name);
}
doAddPart(name, refId=-1) {
if(refId == -1) {this._animator.addPart(name); refId = this._animator._parts.length - 1;}
let part = this._animator._parts[refId];
let newPart = document.createElement('div');
newPart.classList.add('part');
newPart.dataset['name'] = name;
newPart.dataset['id'] = refId;
let cell1 = document.createElement('div');
cell1.classList.add('cell', 'editable');
cell1.innerText = name;
newPart.append(cell1);
let cell2 = document.createElement('div');
cell2.classList.add('cell', 'grow', 'right');
cell2.innerHTML = "<span>Anchor: "+part.anchorPartName()+"</span><br><span>Centered: "+(part.centered?'Yes':'No')+"</span>";
newPart.append(cell2);
let cell3 = document.createElement('div');
cell3.classList.add('cell', 'controls', 'hover');
let button = document.createElement('button');
button.classList.add('modern', 'tiny');
button.innerText = '✎';
button.title = "Edit Part";
button.dataset['action'] = "editPart";
cell3.append(button);
button = document.createElement('button');
button.classList.add('modern', 'tiny');
button.innerText = '-';
button.title = "Remove Part";
button.dataset['action'] = "deletePart";
cell3.append(button);
newPart.append(cell3);
this._contentElem.append(newPart);
this._contentList.push(name);
}
renamePart(elem) {
let orgValue = elem.parentElement.dataset['name'];
let id = this._contentList.indexOf(orgValue);
if(id == -1) return;
let input = document.createElement('input');
input.type = "text";
input.classList.add('textfield', 'large');
input.placeholder = "Part Name";
input.value = orgValue;
let self = this;
input.addEventListener('blur', function() {
let name = this.value;
if(name == orgValue || name == "") this.parentElement.innerText = orgValue;