-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
149 lines (135 loc) · 4.65 KB
/
index.html
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<html>
<head>
<meta charset="utf-8" />
<title>dentidoo</title>
<link rel="icon" type="image/png" href="favicon.ico" />
<style>
body, html { margin:0; padding: 0; }
</style>
</head>
<body onresize="onResize()">
<div id="cr-stage"></div>
<script type="text/javascript" src="crafty-min.js">
comment="From https://github.com/craftyjs/Crafty/releases/download/0.9.0/crafty-min.js"
</script>
<script type="text/javascript" src="levels.js" charset="utf-8"></script>
<script type="text/javascript">
levels = standardLevels; // by default take standard levels
</script>
<script type="text/javascript">
// initial available dimensions, will be used in dentidoo.js to fit grid to window dimensions
var available_width = document.body.clientWidth;
var available_height = document.body.clientHeight*0.9; // leave 10% for custom level buttons
function onResize(){
// update available dimensions
available_width = document.body.clientWidth;
available_height = document.body.clientHeight*0.9;
// call the resize function from dentidoo.js, using Crafty framework
resizeViewport();
}
</script>
<script type="text/javascript" src="solver.js" charset="utf-8"></script>
<script type="text/javascript" src="dentidoo.js" charset="utf-8"></script>
<script type="text/javascript">
// resize the div, mmm did not work well
/*craftyStageDiv = document.getElementById("cr-stage");
craftyStageDiv.style['transform']='scale(0.5)';
craftyStageDiv.style['height'] = '200px';*/
</script>
<!-- Code insipred from https://stackoverflow.com/questions/7346563/loading-local-json-file -->
<p></p>
<input
type="checkbox"
id="useCustomLevels"
onChange="onChangeCheckBox();"
/>
<label for="useCustomLevels">Custom levels</label>
<input
type="file"
id="fileSelector"
accept=".json"
onChange="onChangeFileSelector();"
/>
<script type="text/javascript">
function onChangeFileSelector() {
if (document.getElementById("useCustomLevels").checked) {
loadFile();
}
}
function onChangeCheckBox() {
if (document.getElementById("useCustomLevels").checked) {
loadFile();
} else {
// Restart game with standard levels
levels = standardLevels;
level = 0;
Crafty.scene("main");
}
}
// load custom or standard levels depending on checkbox state at init
// in Firefox, the checkbox state is preserved between sessions.
onChangeCheckBox();
function isValidCustomLevelsJSON(aCustomLevelsJSON) {
isValid = true;
if ("levels" in aCustomLevelsJSON) {
if (Array.isArray(aCustomLevelsJSON["levels"])) {
// each element of the array must contain "name" and "grid" keys
for (i = 0; i < aCustomLevelsJSON["levels"].length; i++) {
if (
"name" in aCustomLevelsJSON["levels"][i] &&
"grid" in aCustomLevelsJSON["levels"][i]
) {
// so far so good
} else {
isValid = false;
break;
}
}
} else {
isValid = false;
}
} else {
isValid = false;
}
return isValid;
}
function loadFile() {
var input, file, fr;
if (typeof window.FileReader !== "function") {
alert("The file API isn't supported on this browser yet.");
return;
}
input = document.getElementById("fileSelector");
if (!input.files) {
alert(
"This browser doesn't seem to support the `files` property of file inputs."
);
} else if (!input.files[0]) {
alert("Please select a file first");
} else {
file = input.files[0];
fr = new FileReader();
fr.onload = onFileLoaded;
fr.readAsText(file);
}
function onFileLoaded(e) {
let lines = e.target.result;
try {
customLevelsJSON = JSON.parse(lines);
// Restart Crafty game with the custom levels, if valid levels
if (isValidCustomLevelsJSON(customLevelsJSON)) {
customLevels = customLevelsJSON["levels"];
levels = customLevels;
level = 0;
Crafty.scene("main");
} else {
alert("Invalid file");
}
} catch (e) {
alert(e);
}
}
}
</script>
</body>
</html>