Mercurial > hg > forks > multipaint-js
annotate files.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 | |
children | dffdbdb4ded6 |
rev | line source |
---|---|
195
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 boolean mpHaveLocalStorage() |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 var test = 'mpLSTest'; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 try { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 localStorage.setItem(test, test); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 if (localStorage.getItem(test) == test) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 localStorage.removeItem(test); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 return true; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 catch (e) { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 return false; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 return false; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 function mpLoadFileSelector(fmtname, fmtexts, fcallback) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 var mpUI = stGE("mpUI"); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 if (mpUI) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 stClearChildren(mpUI); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 mpUI.style.background = "red"; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 mpUI.style.padding = "0.5em"; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 mobj = stCE("input", "mpFileSelector"); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 mobj.type = "file"; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 mobj.name = "name"; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 mobj.multiple = false; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 if (fmtexts != null) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 mobj.accept = fmtexts; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 stAddEventOb(mobj.name, mobj, "change", |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 function(evt) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 var files = evt.target.files; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 if (files.length > 0) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 var freader = new FileReader(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 freader.onloadend = (function(theFile) { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 fcallback(theFile, new Uint8Array(freader.result)); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 }); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 freader.readAsArrayBuffer(files[0]); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 stClearChildren(mpUI); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 mpUI.style.background = null; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 }); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 mpUI.appendChild(mobj); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 mobj = stCE("span", "mpFileInfo"); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 mobj.innerHTML = "Load / import an '<b>"+ fmtname +"</b>' file."; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 mpUI.appendChild(mobj); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 else |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 return null; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 // |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 // Basically the same as Processing loadBytes(), but it seems |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 // that Processing.JS's loadBytes() is broken at least in v1.4.8 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 // and does not return byte-clean data. So roll a replacement of |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 // our own design. --ccr |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 // |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 byte[] mpLoadBinaryFile(String url) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 var xhr = new XMLHttpRequest(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 xhr.open("GET", url, false); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 xhr.overrideMimeType("text/plain; charset=x-user-defined"); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 xhr.setRequestHeader("If-Modified-Since", "Fri, 01 Jan 1960 00:00:00 GMT"); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 xhr.send(null); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 if (xhr.status !== 200 && xhr.status !== 0) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 return null; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 var string = xhr.responseText; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 byte[] ret = new byte[string.length]; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 for (var i = 0; i < string.length; i++) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 ret[i] = string.charCodeAt(i) & 0xff; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 return ret; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 Blob mpMakeBinaryBlob(byte[] data) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 var blob = null; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 if (data == null) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 return null; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 if (typeof(data) == "string") |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 blob = new Blob([data], {type: "application/octet-stream"}); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 else |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 if (typeof(data) == "object") |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 blob = new Blob([new Uint8Array(data)], {type: "application/octet-stream"}); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 return blob; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 // |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 // "Save" a byte array to file. Basically creates a blob URI |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 // and dumps it in the DOM, giving user a download. |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 // |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 boolean mpSaveBinaryFile(String name, byte[] data) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 var blob = mpMakeBinaryBlob(data); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 if (blob == null) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 console.log("Could not create BLOB from data."); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 return false; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 var url = window.URL.createObjectURL(blob); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 if (url == null) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 console.log("Could not create URL from BLOB object."); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 return false; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 var alink = stCE("a"); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 var mpUI = stGE("mpUI"); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 stClearChildren(mpUI); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 mpUI.appendChild(alink); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 alink.style = "display: none"; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 alink.href = url; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 alink.download = name; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 alink.click(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 window.URL.revokeObjectURL(url); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 return true; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 // bordh/v = 64, 32, omag = 1 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 void mpSavePNGImage(String name, int fmt, boolean border, int bordh, int bordv, int omag) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 PImage simg = mpRenderImage(border, bordh, bordv, omag); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 // if (g_data[int('Q')] == 0) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 if (simg !== null) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 // XXX TODO .. actually save the image, something like .. |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 //simg.canvas.toBlob(function(idata){ mpSaveBinaryFile(name, idata); }, "image/png", 0.95); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 int mpLoadPNGImage(String name) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 PImage simg = null; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 if (simg == null) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 return -1; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 int lefth = g_farge; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 int righth = g_backg; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 storeparameters(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 g_data[int('d')] = 0; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 g_data[int('t')] = 0; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 g_data[int('b')] = 1; //old IQ |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 if (!mpImportFromImage(simg)) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 return -2; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 restoreparameters(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 refreshpalette(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 refresh(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 g_boxreconstruct = 2; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 selectcolor(0, lefth); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 selectcolor(1, righth); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 return 0; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 byte[] mpGetNativeImage() |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 //save the picture page g_map[], make sure some essential parameters are correct |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
189 g_map[3] = byte(g_machine); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 g_map[5] = byte(MX); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 g_map[7] = byte(MY); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 return g_map; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 int mpSetNativeImage(byte[] data, boolean noError) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 if (data == null) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 return -1; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 if (data[3] != g_machine && !noError) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 return -2; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 store_undo(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 g_map = data; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 refreshpalette(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
210 consistency(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 g_farge = int(g_realfront); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 g_ofarge = g_farge; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 g_backg = int(g_realback); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 sussborder(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 return 0; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
217 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
218 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
219 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 int mpLoadNativeImage(String name, boolean noError) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 return mpSetNativeImage( |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 mpLoadBinaryFile(name), noError); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
226 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 boolean mpLoadPalette(String fname) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 if (g_map[13] != C64) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 return false; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 byte fdata[] = mpLoadbinaryFile(fname); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 if (fdata == null || |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 fdata.length != 772 || |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 fdata[0x301] != byte(0x10) || |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 fdata[0x302] != byte(0xff) || |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 fdata[0x303] != byte(0xff)) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 return false; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 for (int n = 0; n < 16; n++) |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 { |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
246 makecolor(n, |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 int(fdata[n * 3]), |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 int(fdata[n * 3 + 1]), |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
249 int(fdata[n * 3 + 2])); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 } |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 mpSetUIColors(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 sussborder(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 refresh_all(); |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 return true; |
b36cfb497223
Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 } |