-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.html
227 lines (203 loc) · 8.09 KB
/
editor.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<html>
<head>
<title>Patrick's Hyperbox editor</title>
<meta charset="UTF-8">
<style>
div {
margin-bottom: 5px;
}
.selected {
font-weight: bold;
}
.group {
margin-right: 10px;
}
canvas {
width: 1200px;
height: 800px;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="menu">
<input type="button" value="New" onclick="gameMap = newGameMap(4); updateHtml();" />
<input id="load" type="file" style="display:none;" />
<input type="button" value="Load" onclick="document.getElementById('load').click();" />
<a id="download" download="hyperbox.txt" style="display:none;"></a>
<input type="button" value="Save" onclick="download();" />
<input type="button" value="Play" onclick="window.open('index.html#' + btoa(serialize(gameMap)), '_blank');" />
</div>
<hr>
<div>
<span id="block-select"></span>
<input type="button" value="Add new block" onclick="createNewBlock();" />
</div>
<div id="block-info"></div>
<hr>
<div>
<span class="group">
<span>Add: </span>
<span id="item-select"></span>
</span>
<span class="group">
<span>Change: </span>
<span id="action-select"></span>
</span>
</div>
<canvas id="canvas"></canvas>
<script src="./math.js"></script>
<script src="./tree.js"></script>
<script src="./map.js"></script>
<script src="./rendering.js"></script>
<script src="./serialization.js"></script>
<script>
const canvas = document.getElementById('canvas');
canvas.width = WIDTH;
canvas.height = HEIGHT;
let gameMap;
let polygons;
let selectedBlockIndex = 0;
let selectableItems = [];
let selectableActions = [];
let selectionType = 0;
let selectionIndex = 0;
function reloadHash() {
gameMap = window.location.hash ? deserialize(atob(window.location.hash.substring(1))) : newGameMap(4);
updateHtml();
}
window.addEventListener('hashchange', reloadHash);
document.getElementById('load').addEventListener('change', e => {
const reader = new FileReader();
reader.onload = () => {
gameMap = deserialize(reader.result);
selectedBlockIndex = 0;
updateHtml();
};
reader.readAsText(e.target.files[0]);
});
function download() {
const downloadLink = document.getElementById('download');
downloadLink.href = window.URL.createObjectURL(new Blob([serialize(gameMap)], { type: 'text/plain' }));
downloadLink.click();
}
function createNewBlock() {
addBlockToGameMap(gameMap, {});
selectedBlockIndex = gameMap.blocks.length - 1;
updateHtml();
}
canvas.addEventListener('mousedown', e => {
const { left, top } = canvas.getBoundingClientRect();
const x = e.clientX - left, y = e.clientY - top;
const coordinate = findContainingNode(polygons, x, y, 0);
if (coordinate !== undefined) {
const { blockIndex, nodeIndex } = coordinate;
if (selectionType === 0) {
const [label, type, childBlockIndex] = selectableItems[selectionIndex];
updateContents(gameMap, blockIndex, nodeIndex, type, childBlockIndex);
} else if (selectionType === 1) {
const node = gameMap.blocks[blockIndex].nodes[nodeIndex];
node.facingNeighborIndex = (node.facingNeighborIndex + 1) % gameMap.p;
}
updateHtml();
}
e.preventDefault();
});
function updateBlockSelect() {
let innerHTML = '';
for (let i = 0; i < gameMap.blocks.length; i++) {
const style = i === selectedBlockIndex ? 'class="selected"' : '';
innerHTML += `<input type="button" ${style} value="Block ${i + 1}" onclick="selectedBlockIndex = ${i}; updateHtml();" />`;
}
document.getElementById('block-select').innerHTML = innerHTML;
}
function updateBlock(newProperties) {
updateBlockInGameMap(gameMap, gameMap.blocks[selectedBlockIndex], newProperties);
}
function updateBlockInfo() {
let innerHTML = '';
if (selectedBlockIndex < gameMap.blocks.length) {
const block = gameMap.blocks[selectedBlockIndex];
innerHTML = `
<span class="group">
Squares per vertex: <input type="number" min="5" max="7" value="${block.q}"
onchange="updateBlock({q: parseInt(value)}); updateCanvas();" />
</span>
<span class="group">
Max distance: <input type="number" min="1.0" max="6.0" value="${block.max_r}"
onchange="updateBlock({max_r: parseFloat(value)}); updateCanvas();" />
</span>
<span class="group">
Min steps to exit: <input type="number" min="0" max="5" value="${block.minRadius}"
onchange="updateBlock({minRadius: parseInt(value)}); updateCanvas();" />
</span>
<span class="group">
Hue: <input type="number" min="0" max="359" value="${block.hue}"
onchange="updateBlock({hue: parseInt(value)}); updateCanvas();" />
</span>
<span class="group">
Sat: <input type="number" min="0" max="1.00" value="${block.sat}"
onchange="updateBlock({sat: parseFloat(value)}); updateCanvas();" />
</span>
<span class="group">
Val: <input type="number" min="0" max="1.00" value="${block.val}"
onchange="updateBlock({val: parseFloat(value)}); updateCanvas();" />
</span>
<span class="group">
Is player: <input type="checkbox" ${block.player ? 'checked' : ''}
onchange="updateBlock({player: checked}); updateCanvas();"/>
</span>
`;
}
document.getElementById('block-info').innerHTML = innerHTML;
}
function updateItemSelect() {
selectableItems = [
['Empty space', 'Empty', undefined],
['Wall', 'Wall', undefined],
['Button', 'Button', undefined],
['Player button', 'PlayerButton', undefined],
];
for (let i = 0; i < gameMap.blocks.length; i++) {
selectableItems.push([`Block ${i + 1}`, 'Ref', i]);
}
let innerHTML = '';
for (let i = 0; i < selectableItems.length; i++) {
const [label, type, childBlockIndex] = selectableItems[i];
const style = selectionType === 0 && i === selectionIndex ? 'class="selected"' : '';
innerHTML += `
<input type="button" ${style} value="${label}" onclick="selectionType = 0; selectionIndex = ${i}; updateHtml();" />
`;
}
document.getElementById('item-select').innerHTML = innerHTML;
}
function updateActionSelect() {
selectableActions = [
['Orientation', 'Orientation'],
];
let innerHTML = '';
for (let i = 0; i < selectableActions.length; i++) {
const [label, type] = selectableActions[i];
const style = selectionType === 1 && i === selectionIndex ? 'class="selected"' : '';
innerHTML += `
<input type="button" ${style} value="${label}" onclick="selectionType = 1; selectionIndex = ${i}; updateHtml();" />
`;
}
document.getElementById('action-select').innerHTML = innerHTML;
}
function updateCanvas() {
polygons = toRenderTree(gameMap, undefined, selectedBlockIndex).getPolygons();
render(canvas, polygons);
history.replaceState(null, null, '#' + btoa(serialize(gameMap)));
}
function updateHtml() {
updateBlockSelect();
updateBlockInfo();
updateItemSelect();
updateActionSelect();
updateCanvas();
}
reloadHash();
</script>
</body>
</html>