Mercurial > hg > forks > multipaint-js
comparison multipaint.pde @ 195:b36cfb497223
Move file related functions to files.pde
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 23 Aug 2018 20:23:46 +0300 |
parents | 6fba2c255319 |
children | 3b1afb0b9b30 |
comparison
equal
deleted
inserted
replaced
194:d67a79d6e11c | 195:b36cfb497223 |
---|---|
285 { | 285 { |
286 console.log("TITLE: '" + str + "'"); | 286 console.log("TITLE: '" + str + "'"); |
287 } | 287 } |
288 | 288 |
289 | 289 |
290 boolean mpHaveLocalStorage() | |
291 { | |
292 var test = 'mpLSTest'; | |
293 try { | |
294 localStorage.setItem(test, test); | |
295 if (localStorage.getItem(test) == test) | |
296 { | |
297 localStorage.removeItem(test); | |
298 return true; | |
299 } | |
300 } | |
301 catch (e) { | |
302 return false; | |
303 } | |
304 return false; | |
305 } | |
306 | |
307 | |
308 function mpLoadFileSelector(fmtname, fmtexts, fcallback) | |
309 { | |
310 var mpUI = stGE("mpUI"); | |
311 if (mpUI) | |
312 { | |
313 stClearChildren(mpUI); | |
314 mpUI.style.background = "red"; | |
315 mpUI.style.padding = "0.5em"; | |
316 | |
317 mobj = stCE("input", "mpFileSelector"); | |
318 mobj.type = "file"; | |
319 mobj.name = "name"; | |
320 mobj.multiple = false; | |
321 | |
322 if (fmtexts != null) | |
323 mobj.accept = fmtexts; | |
324 | |
325 stAddEventOb(mobj.name, mobj, "change", | |
326 function(evt) | |
327 { | |
328 var files = evt.target.files; | |
329 if (files.length > 0) | |
330 { | |
331 var freader = new FileReader(); | |
332 | |
333 freader.onloadend = (function(theFile) { | |
334 fcallback(theFile, new Uint8Array(freader.result)); | |
335 }); | |
336 | |
337 freader.readAsArrayBuffer(files[0]); | |
338 } | |
339 stClearChildren(mpUI); | |
340 mpUI.style.background = null; | |
341 }); | |
342 | |
343 mpUI.appendChild(mobj); | |
344 | |
345 mobj = stCE("span", "mpFileInfo"); | |
346 mobj.innerHTML = "Load / import an '<b>"+ fmtname +"</b>' file."; | |
347 mpUI.appendChild(mobj); | |
348 } | |
349 else | |
350 return null; | |
351 } | |
352 | |
353 | |
354 // | |
355 // Basically the same as Processing loadBytes(), but it seems | |
356 // that Processing.JS's loadBytes() is broken at least in v1.4.8 | |
357 // and does not return byte-clean data. So roll a replacement of | |
358 // our own design. --ccr | |
359 // | |
360 byte[] mpLoadBinaryFile(String url) | |
361 { | |
362 var xhr = new XMLHttpRequest(); | |
363 xhr.open("GET", url, false); | |
364 xhr.overrideMimeType("text/plain; charset=x-user-defined"); | |
365 xhr.setRequestHeader("If-Modified-Since", "Fri, 01 Jan 1960 00:00:00 GMT"); | |
366 xhr.send(null); | |
367 | |
368 if (xhr.status !== 200 && xhr.status !== 0) | |
369 return null; | |
370 | |
371 var string = xhr.responseText; | |
372 byte[] ret = new byte[string.length]; | |
373 | |
374 for (var i = 0; i < string.length; i++) | |
375 { | |
376 ret[i] = string.charCodeAt(i) & 0xff; | |
377 } | |
378 | |
379 return ret; | |
380 } | |
381 | |
382 | |
383 Blob mpMakeBinaryBlob(byte[] data) | |
384 { | |
385 var blob = null; | |
386 if (data == null) | |
387 return null; | |
388 | |
389 if (typeof(data) == "string") | |
390 blob = new Blob([data], {type: "application/octet-stream"}); | |
391 else | |
392 if (typeof(data) == "object") | |
393 blob = new Blob([new Uint8Array(data)], {type: "application/octet-stream"}); | |
394 | |
395 return blob; | |
396 } | |
397 | |
398 | |
399 // | |
400 // "Save" a byte array to file. Basically creates a blob URI | |
401 // and dumps it in the DOM, giving user a download. | |
402 // | |
403 boolean mpSaveBinaryFile(String name, byte[] data) | |
404 { | |
405 var blob = mpMakeBinaryBlob(data); | |
406 if (blob == null) | |
407 { | |
408 console.log("Could not create BLOB from data."); | |
409 return false; | |
410 } | |
411 | |
412 var url = window.URL.createObjectURL(blob); | |
413 if (url == null) | |
414 { | |
415 console.log("Could not create URL from BLOB object."); | |
416 return false; | |
417 } | |
418 | |
419 var alink = stCE("a"); | |
420 var mpUI = stGE("mpUI"); | |
421 stClearChildren(mpUI); | |
422 mpUI.appendChild(alink); | |
423 | |
424 alink.style = "display: none"; | |
425 alink.href = url; | |
426 alink.download = name; | |
427 alink.click(); | |
428 | |
429 window.URL.revokeObjectURL(url); | |
430 return true; | |
431 } | |
432 | |
433 | |
434 // bordh/v = 64, 32, omag = 1 | |
435 void mpSavePNGImage(String name, int fmt, boolean border, int bordh, int bordv, int omag) | |
436 { | |
437 PImage simg = mpRenderImage(border, bordh, bordv, omag); | |
438 // if (g_data[int('Q')] == 0) | |
439 | |
440 if (simg !== null) | |
441 { | |
442 // XXX TODO .. actually save the image, something like .. | |
443 //simg.canvas.toBlob(function(idata){ mpSaveBinaryFile(name, idata); }, "image/png", 0.95); | |
444 } | |
445 } | |
446 | |
447 | |
448 int mpLoadPNGImage(String name) | |
449 { | |
450 PImage simg = null; | |
451 if (simg == null) | |
452 return -1; | |
453 | |
454 int lefth = g_farge; | |
455 int righth = g_backg; | |
456 storeparameters(); | |
457 | |
458 g_data[int('d')] = 0; | |
459 g_data[int('t')] = 0; | |
460 g_data[int('b')] = 1; //old IQ | |
461 | |
462 if (!mpImportFromImage(simg)) | |
463 return -2; | |
464 | |
465 restoreparameters(); | |
466 refreshpalette(); | |
467 refresh(); | |
468 g_boxreconstruct = 2; | |
469 selectcolor(0, lefth); | |
470 selectcolor(1, righth); | |
471 return 0; | |
472 } | |
473 | |
474 | |
475 byte[] mpGetNativeImage() | |
476 { | |
477 //save the picture page g_map[], make sure some essential parameters are correct | |
478 g_map[3] = byte(g_machine); | |
479 g_map[5] = byte(MX); | |
480 g_map[7] = byte(MY); | |
481 | |
482 return g_map; | |
483 } | |
484 | |
485 | |
486 int mpSetNativeImage(byte[] data, boolean noError) | |
487 { | |
488 if (data == null) | |
489 return -1; | |
490 | |
491 if (data[3] != g_machine && !noError) | |
492 return -2; | |
493 | |
494 store_undo(); | |
495 | |
496 g_map = data; | |
497 | |
498 refreshpalette(); | |
499 consistency(); | |
500 g_farge = int(g_realfront); | |
501 g_ofarge = g_farge; | |
502 g_backg = int(g_realback); | |
503 sussborder(); | |
504 | |
505 return 0; | |
506 } | |
507 | |
508 | |
509 int mpLoadNativeImage(String name, boolean noError) | |
510 { | |
511 return mpSetNativeImage( | |
512 mpLoadBinaryFile(name), noError); | |
513 } | |
514 | |
515 | |
516 boolean mpLoadPalette(String fname) | |
517 { | |
518 if (g_map[13] != C64) | |
519 { | |
520 return false; | |
521 } | |
522 | |
523 byte fdata[] = mpLoadbinaryFile(fname); | |
524 if (fdata == null || | |
525 fdata.length != 772 || | |
526 fdata[0x301] != byte(0x10) || | |
527 fdata[0x302] != byte(0xff) || | |
528 fdata[0x303] != byte(0xff)) | |
529 { | |
530 return false; | |
531 } | |
532 | |
533 for (int n = 0; n < 16; n++) | |
534 { | |
535 makecolor(n, | |
536 int(fdata[n * 3]), | |
537 int(fdata[n * 3 + 1]), | |
538 int(fdata[n * 3 + 2])); | |
539 } | |
540 | |
541 mpSetUIColors(); | |
542 | |
543 sussborder(); | |
544 refresh_all(); | |
545 | |
546 return true; | |
547 } | |
548 | |
549 | |
550 void draw() | 290 void draw() |
551 { | 291 { |
552 if (!focused) | 292 if (!focused) |
553 { | 293 { |
554 g_control = false; | 294 g_control = false; |