-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
86 lines (71 loc) · 1.99 KB
/
util.js
File metadata and controls
86 lines (71 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function toBinary(string) {
const codeUnits = Uint16Array.from({ length: string.length }, (element, index) =>
string.charCodeAt(index)
);
const charCodes = new Uint8Array(codeUnits.buffer);
let result = "";
charCodes.forEach((char) => {
result += String.fromCharCode(char);
});
return result;
}
function fromBinary(binary) {
const bytes = Uint8Array.from({ length: binary.length }, (element, index) =>
binary.charCodeAt(index)
);
const charCodes = new Uint16Array(bytes.buffer);
let result = "";
charCodes.forEach((char) => {
result += String.fromCharCode(char);
});
return result;
}
function JJStypeof(obj) {
if (obj instanceof Vec2) {
return "Vec2"
}
if (obj instanceof JJS_Object) {
return "JJS_Object"
}
return typeof obj
}
function stringToVal(str, type) {
switch (type) {
case "Vec2":
return Vec2.fromString(str)
case "number":
return parseFloat(str)
case "string":
return str
case "boolean":
return str == "true"
}
}
function isColor(strColor) {
const s = new Option().style
s.color = strColor
return s.color !== ''
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function upload() {
var input = document.createElement('input');
input.type = 'file'
input.onchange = e => {
var file = e.target.files[0]
var reader = new FileReader()
reader.readAsText(file,'UTF-8')
reader.onload = readerEvent => {
var content = readerEvent.target.result
deserializeGameState(content)
}
}
input.click();
}