Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
977 lines (762 loc) · 24 KB
/
Copy pathmain.js
File metadata and controls
977 lines (762 loc) · 24 KB
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
const FUNCOES_IMAGEM = [
{ nome: 'Remover', fn: 'removerImagem' }
];
const FUNCOES_FICHA = [
{ nome: 'Duplicar', fn: 'duplicar' },
{ nome: 'Remover', fn: 'remover' }
];
const FUNCAO_REMOVER_COMBATE = { nome: 'Remover do combate', fn: 'combate', param: 'false' };
const FUNCAO_INSERIR_COMBATE = { nome: 'Inserir do combate', fn: 'combate', param: 'true' };
const FORMULARIO = 'form';
const IMAGEM = 'img';
const CONFIRMACAO = 'conf';
// localStorage.setItem('sessao', undefined);
var sessao;
var sessaoImportacao;
var estilizarAlinhamento = true;
var ocultarMortos = false;
var ocultarForaCombate = false;
load();
// carrega do armazenamento local o objeto de sessão
function load() {
let session = localStorage.getItem('sessao');
if (session && session != "undefined") {
sessao = JSON.parse(session);
validarSessao();
carregarConfiguracoes();
render();
} else {
create();
}
}
// obtem da sessão uma ficha pelo id
function get(id) {
for (const ficha of sessao.fichas) {
if (ficha.id == id) {
return ficha;
}
}
}
// cria o objeto de sessão
function create() {
sessao = {
ultimaFicha: 0,
ultimaImagem: 0,
fichas: [],
imagens: obterPacoteImagem()
}
}
// salva o objeto de sessão
function save() {
localStorage.setItem('sessao', JSON.stringify(sessao));
}
// valida os dados para inicio da sessao, os criando se necessário
function validarSessao() {
if (!sessao) {
create();
save();
}
if (!sessao.fichas) {
sessao.fichas = [];
save();
}
if (!sessao.ultimaFicha) {
sessao.ultimaFicha = 0;
save();
}
if (!sessao.imagens) {
sessao.imagens = obterPacoteImagem();
save();
}
if (!sessao.ultimaImagem) {
sessao.ultimaImagem = 0;
save();
}
}
function carregarConfiguracoes() {
let configuracoes = sessao.configuracoes;
if (!configuracoes) {
return;
}
if (configuracoes.ocultarMortos !== undefined) {
ocultarMortos = configuracoes.ocultarMortos;
}
if (configuracoes.ocultarForaCombate !== undefined) {
ocultarForaCombate = configuracoes.ocultarForaCombate;
}
if (configuracoes.estilizarAlinhamento !== undefined) {
estilizarAlinhamento = configuracoes.estilizarAlinhamento;
}
}
// renderiza as fichas em tela
function render() {
let fichas = sessao.fichas;
let fichasHTML = document.getElementsByClassName("ficha");
// remove todas as fichas da tela
while (fichasHTML.length > 0) {
fichasHTML[0].remove();
}
// ordena as fichas por iniciativa
fichas.sort(compare);
for (i = 0; i < fichas.length; i++) {
document.getElementById("add").before(buildFicha(fichas[i], i));
}
document.querySelectorAll('input').forEach(input => {
input.addEventListener('dblclick', (event) => { prevent(event) });
});
}
function add() {
abrirFormulario();
}
function abrirFormulario() {
abrirBack(FORMULARIO);
let base = document.getElementById("formBase");
let novoPopup = document.createElement("div");
novoPopup.innerHTML = base.innerHTML.replaceAll("?=id", "form");
novoPopup.setAttribute("class", "popup");
novoPopup.setAttribute("id", "fichaForm");
document.body.append(novoPopup);
}
function abrirBack(tipo) {
let back = document.createElement('div');
let fn;
let classe;
if (tipo === CONFIRMACAO) {
fn = "removerConfirmacao()";
classe = "back";
} else if (tipo === IMAGEM) {
fn = "removerFormularioImagem()";
classe = "imgback";
} else if (tipo === FORMULARIO) {
fn = "removerFormulario()";
classe = "back";
}
back.setAttribute("class", classe);
back.setAttribute("onclick", fn);
document.body.append(back);
}
function removerFormulario() {
document.getElementById("fichaForm").remove();
let back = document.getElementsByClassName("back");
while (back.length > 0) {
back[0].remove();
}
}
function removerConfirmacao() {
let confirmacao = document.getElementsByClassName("confirmacao")
while (confirmacao.length > 0) {
confirmacao[0].remove();
}
let back = document.getElementsByClassName("back");
while (back.length > 0) {
back[0].remove();
}
}
function confirmar() {
let ficha = obterDadosFormulario();
if (ficha.id) {
let fichaSessao = get(ficha.id);
fichaSessao.nome = ficha.nome;
fichaSessao.iniciativa = ficha.iniciativa;
fichaSessao.vida = ficha.vida;
fichaSessao.extra = ficha.extra;
fichaSessao.armadura = ficha.armadura;
fichaSessao.anotacao = ficha.anotacao;
fichaSessao.alinhamento = ficha.alinhamento;
if (getImagem(ficha.imagem)) {
fichaSessao.imagem = ficha.imagem;
}
} else {
sessao.ultimaFicha++;
ficha.id = sessao.ultimaFicha;
sessao.fichas.push(ficha);
}
save();
render();
removerFormulario();
console.log('INSERT: inserção de ficha -> ' + JSON.stringify(ficha));
}
function buildFicha(ficha, index) {
let base = document.getElementById("fichaBase");
let novaFichaHTML = document.createElement("div");
novaFichaHTML.setAttribute("id", "ficha_" + ficha.id);
novaFichaHTML.setAttribute("class", "ficha");
let html = base.innerHTML;
html = html.replaceAll("?=ordem", "#" + (index + 1));
html = html.replaceAll("?=id", ficha.id);
html = html.replaceAll("?=nome", ficha.nome);
html = html.replaceAll("?=iniciativa", ficha.iniciativa);
html = html.replaceAll("?=vida", ficha.vida);
html = html.replaceAll("?=extra", ficha.extra);
html = html.replaceAll("?=armadura", ficha.armadura);
html = html.replaceAll("?=anotacao", ficha.anotacao);
if (getImagem(ficha.imagem)) {
let imagem = getImagem(ficha.imagem);
html = html.replaceAll("default.png", imagem.url);
}
if (ficha.alinhamento && estilizarAlinhamento) {
let alinhamentos = ['bom', 'mau', 'neutro']
if (alinhamentos.indexOf(ficha.alinhamento) >= 0) {
novaFichaHTML.classList.add(ficha.alinhamento);
}
}
if (ficha.vida < 1) {
novaFichaHTML.classList.add('morte');
if (ocultarMortos) {
novaFichaHTML.classList.add('hidden');
}
} else if (!ficha.combate) {
novaFichaHTML.classList.add('foracombate');
if (ocultarForaCombate) {
novaFichaHTML.classList.add('hidden');
}
}
novaFichaHTML.setAttribute('oncontextmenu', 'marcar(' + ficha.id + ')');
novaFichaHTML.setAttribute('title', ficha.anotacao);
novaFichaHTML.innerHTML = html;
return novaFichaHTML;
}
function obterDadosFormulario() {
let ficha = {
id: obterDados('form', 'idFicha'),
nome: obterDados('form', 'nome'),
iniciativa: obterDados('form', 'iniciativa'),
extra: obterDados('form', 'extra'),
vida: obterDados('form', 'vida'),
armadura: obterDados('form', 'armadura'),
anotacao: obterDados('form', 'anotacao'),
imagem: obterDados('form', 'imagem'),
alinhamento: obterDados('form', 'alinhamento')
}
return ficha;
}
function obterDados(id, nomeCampo) {
let campo = document.querySelector(`[data-id="${id}"][data-campo="${nomeCampo}"]`);
if (campo && campo.value) {
return campo.value;
}
if (campo && campo.innerText) {
return campo.innerText;
}
return '';
}
function definirDados(id, nomeCampo, valor) {
let campo = document.querySelector(`[data-id="${id}"][data-campo="${nomeCampo}"]`);
campo.value = valor;
}
// --------------------------------------------------------------------------------------------
// - formulário de imagens
// --------------------------------------------------------------------------------------------
function abrirFormularioImagem() {
abrirBack(IMAGEM);
let base = document.getElementById("formImagem");
let formImagem = document.createElement("div");
formImagem.innerHTML = base.innerHTML.replaceAll("?=addImage", "addImage");
formImagem.setAttribute("class", "popup");
formImagem.setAttribute("id", "fichaImagem");
document.body.append(formImagem);
renderImages();
}
function removerFormularioImagem() {
let formImagem = document.getElementById("fichaImagem");
formImagem.remove();
let back = document.getElementsByClassName("imgback");
while (back.length > 0) {
back[0].remove();
}
}
function recarregarFormularioImagem() {
// remove todas as imagens da lista de seleção
let imagens = document.getElementsByClassName("imagemItem");
while (imagens.length > 0) {
imagens[0].remove();
}
// renderiza as imagens novamente
renderImages();
}
function renderImages() {
let upload = document.getElementById("addImage");
for (const imagem of sessao.imagens) {
upload.before(buildImagem(imagem));
}
}
function buildImagem(imagem) {
let imagemHTML = document.createElement("img");
let fnEscolher = "marcarEscolhida(" + imagem.id + ")";
let fnRemover = "abrirContexto(" + imagem.id + ", 'imagem', this)";
imagemHTML.setAttribute("onclick", fnEscolher);
imagemHTML.setAttribute("oncontextmenu", fnRemover);
imagemHTML.setAttribute("src", imagem.url);
imagemHTML.setAttribute("class", "imagemItem");
return imagemHTML;
}
function compare(a, b) {
if (a.combate != b.combate) {
if (b.combate) {
return 1;
} else {
return -1;
}
}
if ((a.vida < 1) != (b.vida < 1)) {
if (a.vida < 1) {
return 1;
}
return -1;
}
if (parseInt(a.iniciativa) > parseInt(b.iniciativa)) {
return -1;
}
if (parseInt(a.iniciativa) < parseInt(b.iniciativa)) {
return 1;
}
if (parseInt(a.extra) > parseInt(b.extra)) {
return -1;
}
if (parseInt(a.extra) < parseInt(b.extra)) {
return 1;
}
return 0;
}
function abrirContexto(id, lista, me) {
let isFicha = lista !== "imagem";
removerContexto();
let funcoes = [...FUNCOES_IMAGEM];
if (isFicha) {
funcoes = [...FUNCOES_FICHA];
let fichaHTML = document.getElementById('ficha_' + id);
if (fichaHTML.classList.contains('foracombate')) {
funcoes.push(FUNCAO_INSERIR_COMBATE);
} else {
funcoes.push(FUNCAO_REMOVER_COMBATE);
}
}
let base = document.getElementById("contextoBase");
let contexto = document.createElement("div");
let html = base.innerHTML;
contexto.innerHTML = html;
for (const func of funcoes) {
let li = document.createElement('li');
let param = id;
if (func.param) {
param += ', ' + func.param;
}
li.setAttribute('onclick', func.fn + '(' + param + ')');
li.innerText = func.nome;
contexto.children[0].append(li);
}
let top = me.offsetTop + 10;
let left = me.offsetLeft + 10;
contexto.setAttribute("class", "contexto");
contexto.setAttribute("style", "top: " + top + "px; left: " + left + "px");
document.body.append(contexto);
}
function combate(id, acao) {
let ficha = get(id);
ficha.combate = acao;
save();
render();
}
function abrirForm(id, me) {
abrirFormulario();
let ficha = get(id);
if (getImagem(ficha.imagem)) {
let form = document.getElementById("fichaForm");
let imagem = getImagem(ficha.imagem);
form.innerHTML = form.innerHTML.replace("default.png", imagem.url);
definirDados('form', 'imagem', ficha.imagem);
}
definirDados('form', 'idFicha', ficha.id);
definirDados('form', 'nome', ficha.nome);
definirDados('form', 'iniciativa', ficha.iniciativa);
definirDados('form', 'extra', ficha.extra);
definirDados('form', 'vida', ficha.vida);
definirDados('form', 'armadura', ficha.armadura);
definirDados('form', 'anotacao', ficha.anotacao);
definirDados('form', 'alinhamento', ficha.alinhamento)
}
function removerContexto(e) {
if (e && e.target.className == "menu") {
return;
}
let contextos = document.getElementsByClassName("contexto");
while (contextos.length > 0) {
contextos[0].remove();
}
}
function remover(id) {
let ficha = get(id);
sessao.fichas.splice(sessao.fichas.indexOf(ficha), 1);
save();
render();
console.log('DELETE: remoção de ficha -> ' + JSON.stringify(ficha));
}
function removerImagem(id) {
let imagem = getImagem(id);
sessao.imagens.splice(sessao.imagens.indexOf(imagem), 1);
save();
render();
recarregarFormularioImagem();
// se a imagem removida for a do formulário ela é removida
if (obterDados('form', 'imagem') == id) {
atribuirImagemFormulario('default');
}
}
function duplicar(id) {
let ficha = get(id);
let fichaClonada = { ...ficha };
sessao.ultimaFicha++;
fichaClonada.id = sessao.ultimaFicha;
sessao.fichas.push(fichaClonada);
save();
render();
}
function marcarEscolhida(id) {
definirDados('form', 'imagem', id);
atribuirImagemFormulario(id);
removerFormularioImagem();
}
function atribuirImagemFormulario(id) {
let imagem = getImagem(id);
let url = imagem ? imagem.url : 'default.png';
let imagemHTML = document.querySelector('#fichaForm img');
imagemHTML.setAttribute("src", url);
}
function getImagem(id) {
for (const imagem of sessao.imagens) {
if (imagem.id == id) {
return imagem;
}
}
}
function exportar() {
let botoes = [{
texto: 'Sessão completa', fn: 'exportarSessao(false)', classe: 'confirmar'
}, {
texto: 'Apenas personagens marcados', fn: 'exportarSessao(true)', classe: 'confirmar'
}, {
texto: 'Cancelar', fn: 'removerConfirmacao()', classe: 'cancelar'
}];
confirmacao('Este processo irá exportar as fichas da sessão. Que tipo de exportação você deseja fazer?', botoes);
}
function exportarSessao(apenasMarcados) {
removerConfirmacao();
let sessaoExportacao;
if (apenasMarcados) {
sessaoExportacao = obterMarcados();
} else {
sessaoExportacao = sessao;
}
if (sessaoExportacao.fichas.length == 0) {
alerta("Nenhuma ficha foi encontrada.")
return;
}
let nome = "backup_" + new Date().getTime();
let link = document.createElement('a');
let json = JSON.stringify(sessaoExportacao);
link.setAttribute('href', 'data:text/json;charset=utf-8,' + encodeURIComponent(json));
link.setAttribute('download', nome + '.json');
link.click();
}
function marcar(id) {
let fichaHTML = document.getElementById('ficha_' + id);
fichaHTML.classList.toggle('marcado');
}
function obterMarcados() {
let fichasHTML = document.getElementsByClassName('marcado');
let imagens = [];
let fichas = [];
for (const fichaHTML of fichasHTML) {
let id = fichaHTML.getAttribute('id').replace('ficha_', '');
let ficha = get(id);
if (!ficha) {
continue;
}
fichas.push(ficha);
let imagem = getImagem(ficha.imagem);
if (!imagem) {
continue;
}
imagens.push(imagem);
}
return {
ultimaFicha: sessao.ultimaFicha,
ultimaImagem: sessao.ultimaImagem,
configuracoes: sessao.configuracoes,
fichas: fichas,
imagens: imagens
}
}
function importarImagem() {
let uploader = document.getElementById("uploaderImage");
uploader.click();
}
function importarSessao() {
let uploader = document.getElementById("uploaderJson");
uploader.value = '';
uploader.click();
}
function processarJson(e) {
let arquivo = e.target.files[0];
const fileReader = new FileReader();
fileReader.onload = function (event) {
const conteudo = event.target.result;
let sessaoUpload;
try {
sessaoUpload = JSON.parse(conteudo);
} catch (e) {
alerta('O arquivo não é um JSON válido.');
return;
}
if (validarUpload(sessaoUpload)) {
sessaoImportacao = sessaoUpload;
let botoes = [{
texto: 'Sobrescrever sessão', fn: 'importarCompleto()', classe: 'confirmar'
}, {
texto: 'Importar a sessão existente', fn: 'importarExistente()', classe: 'confirmar'
}, {
texto: 'Cancelar', fn: 'removerConfirmacao()', classe: 'cancelar'
}];
confirmacao('O processo vai reescrever sua sessão atual.\nGaranta uma cópia de segurança exportando a sessão atual para JSON. Escolha uma opção para continuar:', botoes);
}
};
fileReader.readAsText(arquivo);
}
function importarCompleto() {
sessao = sessaoImportacao;
save();
carregarConfiguracoes();
render();
alerta('Sessão carregada com sucesso!');
}
function importarExistente() {
let imagensImportacao = sessaoImportacao.imagens;
let imagens = sessao.imagens;
let fichasImportacao = sessaoImportacao.fichas;
let fichas = sessao.fichas;
let ultimaImagem = sessao.ultimaImagem;
let ultimaFicha = sessao.ultimaFicha;
for (const imagem of imagensImportacao) {
let idAntigo = imagem.id;
let idNovo;
for (const img of imagens) {
if (imagem.url == img.url) {
idNovo = img.id;
break;
}
}
if (!idNovo) {
ultimaImagem++;
idNovo = ultimaImagem;
imagens.push({ id: idNovo, url: imagem.url });
}
for (const ficha of fichasImportacao) {
if (ficha.imagem == idAntigo) {
ficha.imagem = idNovo;
}
}
}
for (const ficha of fichasImportacao) {
ultimaFicha++;
ficha.id = ultimaFicha;
fichas.push(ficha);
}
sessao.ultimaFicha = ultimaFicha;
sessao.ultimaImagem = ultimaImagem;
save();
carregarConfiguracoes();
render();
alerta('Sessão carregada com sucesso!');
}
function alerta(mensagem) {
confirmacao(mensagem, [{ texto: 'Ok', fn: 'removerConfirmacao()', classe: 'confirmar' }])
}
function confirmacao(mensagem, botoes, manterConfirmacao) {
if (!manterConfirmacao) {
removerConfirmacao();
}
let p = document.createElement('p');
p.innerText = mensagem;
let modalHTML = document.createElement('div');
modalHTML.setAttribute('class', 'confirmacao');
modalHTML.append(p);
for (const botao of botoes) {
let button = document.createElement('button');
if (botao.classe) {
button.setAttribute('class', botao.classe);
}
button.innerText = botao.texto;
button.setAttribute('onclick', botao.fn);
modalHTML.append(button);
}
abrirBack(CONFIRMACAO);
document.body.append(modalHTML);
}
function validarUpload(sessaoUpload) {
if (!sessaoUpload.ultimaFicha && sessaoUpload.ultimaFicha != 0) {
alerta('O JSON não possui o campo numérico "ultimaFicha".');
return false;
}
if (isNaN(sessaoUpload.ultimaFicha)) {
alerta('O campo "ultimaFicha" deve ser do tipo numérico.');
return false;
}
if (!sessaoUpload.ultimaImagem && sessaoUpload.ultimaImagem != 0) {
alerta('O JSON não possui o campo numérico "ultimaImagem".');
return false;
}
if (isNaN(sessaoUpload.ultimaImagem)) {
alerta('O campo "ultimaImagem" deve ser do tipo numérico.');
return false;
}
if (!sessaoUpload.fichas) {
alerta('O JSON não possui o campo array "fichas".');
return false;
}
if (!Array.isArray(sessaoUpload.fichas)) {
alerta('O campo "fichas" deve ser do tipo array.');
return false;
}
if (!sessaoUpload.imagens) {
alerta('O JSON não possui o campo array "imagens".');
return false;
}
if (!Array.isArray(sessaoUpload.imagens)) {
alerta('O campo "imagens" deve ser do tipo array.');
return false;
}
for (const ficha of sessaoUpload.fichas) {
if (!ficha.id) {
alerta('As fichas no campo "fichas" devem ter o campo "id".');
return false;
}
}
for (const imagem of sessaoUpload.imagens) {
if (!imagem.id) {
alerta('As imagens no campo "imagens" devem ter o campo "id".');
return false;
}
}
corrigirSessao(sessaoUpload);
return true;
}
function processarImagem(e) {
let arquivo = e.target.files[0];
salvarImagem(arquivo);
}
function salvarImagem(arquivo) {
const canvas = document.getElementById('canvas');
const contexto = canvas.getContext('2d');
const imageURL = URL.createObjectURL(arquivo);
const image = new Image();
image.onload = function () {
const size = 64;
let largura = image.width;
let altura = image.height;
// redimensiona para o tamanho final
if (largura > altura) {
largura = size * (largura / altura);
altura = size;
} else {
altura = size * (altura / largura);
largura = size;
}
// centraliza a imagem
const x = 0 - ((largura - size) / 2);
const y = 0 - ((altura - size) / 2);
canvas.width = size;
canvas.height = size;
contexto.drawImage(image, x, y, largura, altura);
URL.revokeObjectURL(imageURL);
sessao.ultimaImagem++;
let url = canvas.toDataURL("image/jpeg");
let imagem = {
id: sessao.ultimaImagem,
url: url
}
sessao.imagens.push(imagem);
save();
recarregarFormularioImagem();
};
image.src = imageURL;
}
function isPaisagem(altura, largura) {
if (largura > altura) {
return true;
}
return false;
}
function edit(id) {
let ficha = get(id);
ficha.iniciativa = processar(id, 'iniciativa', ficha.iniciativa);
ficha.vida = processar(id, 'vida', ficha.vida);
ficha.extra = processar(id, 'extra', ficha.extra);
ficha.armadura = processar(id, 'armadura', ficha.armadura);
save();
render();
}
function processar(id, campo, valor) {
let novoValor = obterDados(id, campo);
let retorno;
try {
let calculo = eval(novoValor);
if (novoValor.startsWith('+') || novoValor.startsWith('-')) {
retorno = parseInt(valor) + parseInt(calculo);
} else {
retorno = calculo;
}
} catch (error) {
returno = novoValor;
}
if (valor != retorno) {
let ficha = get(id);
console.log(`UPDATE: ${campo} de '${valor}' para '${retorno}' de '${ficha.nome}'.`);
}
return retorno || '';
}
function atalhosTeclado(e) {
let formulario = document.getElementById("fichaForm");
if (e.key === "Escape") {
removerContexto(e);
formulario && removerFormulario();
}
if (e.key === "Enter" && e.ctrlKey) {
formulario && confirmar();
}
}
function corrigirSessao(sessaoUpload) {
let ultimaFicha = 0;
let ultimaImagem = 0;
for (const ficha of sessaoUpload.fichas) {
if (ficha.id > ultimaFicha) {
ultimaFicha = ficha.id;
}
}
if (sessaoUpload.ultimaFicha < ultimaFicha) {
sessaoUpload.ultimaFicha = ultimaFicha;
}
for (const imagem of sessaoUpload.imagens) {
if (imagem.id > ultimaImagem) {
ultimaImagem = imagem.id;
}
}
if (sessaoUpload.ultimaImagem < ultimaImagem) {
sessaoUpload.ultimaImagem = ultimaImagem;
}
}
function prevent(e) {
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
};
document.addEventListener('contextmenu', event => event.preventDefault());
document.addEventListener("click", (event) => { removerContexto(event) });
document.addEventListener("keydown", (event) => { atalhosTeclado(event) });
class StringBuilder {
string = '';
append(string) {
this.string += string;
}
toString() {
return this.string;
}
}