annotate files.pde @ 216:d3eba28c26e5

Some launcher / UI improvements.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 04 Sep 2018 15:29:10 +0300
parents b3579297e04e
children 1c9deae71fb1
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';
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 {
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
21 var mpUI = mpDisplayGUI();
195
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 mobj = stCE("input", "mpFileSelector");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 mobj.type = "file";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 mobj.name = "name";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 mobj.multiple = false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 if (fmtexts != null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 mobj.accept = fmtexts;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 stAddEventOb(mobj.name, mobj, "change",
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 function(evt)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 var files = evt.target.files;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 if (files.length > 0)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 var freader = new FileReader();
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 freader.onloadend = (function(theFile) {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 fcallback(theFile, new Uint8Array(freader.result));
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 });
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 freader.readAsArrayBuffer(files[0]);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 }
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
48
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
49 mpHideGUI();
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 });
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 mpUI.appendChild(mobj);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
54 mobj = stCE("button", "mpFileSelectorCancel");
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
55 mobj.textContent = "Cancel";
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
56 stAddEventOb(mobj.name, mobj, "click",
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
57 function(evt)
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
58 {
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
59 mpHideGUI();
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
60 });
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
61 mpUI.appendChild(mobj);
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
62
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 mobj = stCE("span", "mpFileInfo");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 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
65 mpUI.appendChild(mobj);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 else
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 return null;
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
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 // 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
74 // 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
75 // 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
76 // our own design. --ccr
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 //
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 byte[] mpLoadBinaryFile(String url)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 var xhr = new XMLHttpRequest();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 xhr.open("GET", url, false);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 xhr.overrideMimeType("text/plain; charset=x-user-defined");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 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
84 xhr.send(null);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 if (xhr.status !== 200 && xhr.status !== 0)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 return 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 var string = xhr.responseText;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 byte[] ret = new byte[string.length];
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 for (var i = 0; i < string.length; i++)
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 ret[i] = string.charCodeAt(i) & 0xff;
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 return ret;
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 Blob mpMakeBinaryBlob(byte[] data)
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 var blob = null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 if (data == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 return null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 if (typeof(data) == "string")
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 blob = new Blob([data], {type: "application/octet-stream"});
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 else
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 if (typeof(data) == "object")
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 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
112
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 return blob;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 }
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
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 // "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
119 // 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
120 //
200
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
121 boolean mpSaveBinaryBlob(String name, void blob)
195
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");
216
d3eba28c26e5 Some launcher / UI improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
131 var mpUI = stGE("mpUI2");
195
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
200
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
145 boolean mpSaveBinaryFile(String name, byte[] data)
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
146 {
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
147 var blob = mpMakeBinaryBlob(data);
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
148 if (blob == null)
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 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
151 return false;
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
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
154 return mpSaveBinaryBlob(name, blob);
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
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 // bordh/v = 64, 32, omag = 1
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 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
160 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 PImage simg = mpRenderImage(border, bordh, bordv, omag);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 // if (g_data[int('Q')] == 0)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 if (simg !== null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 // XXX TODO .. actually save the image, something like ..
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 //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
168 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 }
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 int mpLoadPNGImage(String name)
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 PImage simg = null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 if (simg == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 return -1;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 int lefth = g_farge;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 int righth = g_backg;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 storeparameters();
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 g_data[int('d')] = 0;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 g_data[int('t')] = 0;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 g_data[int('b')] = 1; //old IQ
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 if (!mpImportFromImage(simg))
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 return -2;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 restoreparameters();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 refreshpalette();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 refresh();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 g_boxreconstruct = 2;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 selectcolor(0, lefth);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 selectcolor(1, righth);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 return 0;
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
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 byte[] mpGetNativeImage()
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 //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
202 g_map[3] = byte(g_machine);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 g_map[5] = byte(MX);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 g_map[7] = byte(MY);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 return g_map;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 }
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
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 int mpSetNativeImage(byte[] data, boolean noError)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 if (data == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 return -1;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 if (data[3] != g_machine && !noError)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 return -2;
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 store_undo();
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 g_map = data;
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 refreshpalette();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 consistency();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 g_farge = int(g_realfront);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 g_ofarge = g_farge;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 g_backg = int(g_realback);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 sussborder();
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 return 0;
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
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 int mpLoadNativeImage(String name, boolean noError)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 return mpSetNativeImage(
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 mpLoadBinaryFile(name), noError);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239
208
c6c1dd769566 Some work towards user-selectable alternate palettes in the launcher.
Matti Hamalainen <ccr@tnsp.org>
parents: 200
diff changeset
240 // 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
241 boolean mpLoadPalette(String fname)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 {
212
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
243 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
244 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
245
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 if (fdata == null ||
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 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
248 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
249 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
250 fdata[0x303] != 0xff)
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 {
212
b3579297e04e Implement rest of the stuff needed for selecting external palettes in the
Matti Hamalainen <ccr@tnsp.org>
parents: 210
diff changeset
252 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
253 return false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 for (int n = 0; n < 16; n++)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 makecolor(n,
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 int(fdata[n * 3]),
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 int(fdata[n * 3 + 1]),
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 int(fdata[n * 3 + 2]));
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 mpSetUIColors();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 sussborder();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 refresh_all();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 return true;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 }