annotate files.pde @ 228:c9a77d87e380

Cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Sep 2018 22:28:15 +0300
parents 1c9deae71fb1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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';
225
1c9deae71fb1 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
4 try
1c9deae71fb1 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
5 {
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 localStorage.setItem(test, test);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 if (localStorage.getItem(test) == test)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 localStorage.removeItem(test);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 return true;
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 }
225
1c9deae71fb1 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
13 catch (e)
1c9deae71fb1 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
14 {
195
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 return false;
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
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 function mpLoadFileSelector(fmtname, fmtexts, fcallback)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 {
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
23 var mpUI = mpDisplayGUI();
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 if (mpUI)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 stClearChildren(mpUI);
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",
225
1c9deae71fb1 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
37 function (evt)
195
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
225
1c9deae71fb1 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
44 freader.onloadend = (function (theFile)
1c9deae71fb1 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
45 {
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 fcallback(theFile, new Uint8Array(freader.result));
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 freader.readAsArrayBuffer(files[0]);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 }
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
51
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
52 mpHideGUI();
195
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 mpUI.appendChild(mobj);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
57 mobj = stCE("button", "mpFileSelectorCancel");
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
58 mobj.textContent = "Cancel";
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
59 stAddEventOb(mobj.name, mobj, "click",
225
1c9deae71fb1 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
60 function (evt)
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
61 {
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
62 mpHideGUI();
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
63 });
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
64 mpUI.appendChild(mobj);
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
65
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 mobj = stCE("span", "mpFileInfo");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 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
68 mpUI.appendChild(mobj);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 else
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 return null;
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 //
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 // 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
77 // 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
78 // 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
79 // our own design. --ccr
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 //
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 byte[] mpLoadBinaryFile(String url)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 var xhr = new XMLHttpRequest();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 xhr.open("GET", url, false);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 xhr.overrideMimeType("text/plain; charset=x-user-defined");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 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
87 xhr.send(null);
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 if (xhr.status !== 200 && xhr.status !== 0)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 return null;
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 var string = xhr.responseText;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 byte[] ret = new byte[string.length];
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 for (var i = 0; i < string.length; i++)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 ret[i] = string.charCodeAt(i) & 0xff;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 }
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 return ret;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 Blob mpMakeBinaryBlob(byte[] data)
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 var blob = null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 if (data == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 return null;
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 if (typeof(data) == "string")
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 blob = new Blob([data], {type: "application/octet-stream"});
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 else
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 if (typeof(data) == "object")
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 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
115
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 return blob;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 }
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 //
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 // "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
122 // 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
123 //
200
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
124 boolean mpSaveBinaryBlob(String name, void blob)
195
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 var url = window.URL.createObjectURL(blob);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 if (url == null)
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 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
130 return false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 var alink = stCE("a");
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
134 var mpUI = stGE("mpUI2");
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 stClearChildren(mpUI);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 mpUI.appendChild(alink);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 alink.style = "display: none";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 alink.href = url;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 alink.download = name;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 alink.click();
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 window.URL.revokeObjectURL(url);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 return true;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
200
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
148 boolean mpSaveBinaryFile(String name, byte[] data)
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
149 {
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
150 var blob = mpMakeBinaryBlob(data);
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
151 if (blob == null)
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
152 {
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
153 console.log("Could not create BLOB from data.");
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
154 return false;
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
155 }
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
156
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
157 return mpSaveBinaryBlob(name, blob);
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
158 }
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
159
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
160
228
c9a77d87e380 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
161 boolean mpImportAnyImage(PImage simg)
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 int lefth = g_farge;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 int righth = g_backg;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 storeparameters();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 g_data[int('d')] = 0;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 g_data[int('t')] = 0;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 g_data[int('b')] = 1; //old IQ
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 if (!mpImportFromImage(simg))
228
c9a77d87e380 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
172 return false;
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 restoreparameters();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 refreshpalette();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 refresh();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 g_boxreconstruct = 2;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 selectcolor(0, lefth);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 selectcolor(1, righth);
228
c9a77d87e380 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
180 return true;
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182
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 byte[] mpGetNativeImage()
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 //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
187 g_map[3] = byte(g_machine);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 g_map[5] = byte(MX);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 g_map[7] = byte(MY);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 return g_map;
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
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 int mpSetNativeImage(byte[] data, boolean noError)
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 if (data == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 return -1;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 if (data[3] != g_machine && !noError)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 return -2;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 store_undo();
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 g_map = data;
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 refreshpalette();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 consistency();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 g_farge = int(g_realfront);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 g_ofarge = g_farge;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 g_backg = int(g_realback);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 sussborder();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 return 0;
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
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 int mpLoadNativeImage(String name, boolean noError)
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 return mpSetNativeImage(
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 mpLoadBinaryFile(name), noError);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224
208
c6c1dd769566 Some work towards user-selectable alternate palettes in the launcher.
Matti Hamalainen <ccr@tnsp.org>
parents: 200
diff changeset
225 // XXX TODO: Make this support other platforms than C64
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 boolean mpLoadPalette(String fname)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 {
212
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
228 String ffname = "palettes/"+ g_map[13] +"/"+ fname;
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
229 byte fdata[] = mpLoadBinaryFile(ffname);
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
230
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 if (fdata == null ||
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 fdata.length != 772 ||
212
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
233 fdata[0x301] != 0x10 ||
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
234 fdata[0x302] != 0xff ||
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
235 fdata[0x303] != 0xff)
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 {
212
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
237 console.log("Could not load palette file '"+ ffname +"'.");
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 return false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 }
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 for (int n = 0; n < 16; n++)
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 makecolor(n,
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 int(fdata[n * 3]),
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 int(fdata[n * 3 + 1]),
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 int(fdata[n * 3 + 2]));
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 mpSetUIColors();
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 sussborder();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 refresh_all();
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 return true;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 }