-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
167 lines (146 loc) Β· 6.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html>
<head>
<title>World Editor</title>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
<h1>World Editor</h1>
<canvas id="myCanvas"></canvas>
<div id="controls">
<button onclick="dispose()">ποΈ</button>
<button onclick="save()">πΎ</button>
<label for="fileInput" class="file-input-label">
π
<input
type="file"
id="fileInput"
accept=".world"
onchange="load(event)"
/>
</label>
<button id="graphBtn" onclick="setMode('graph')">π</button>
<button id="stopBtn" onclick="setMode('stop')">π</button>
<button id="yieldBtn" onclick="setMode('yield')">β οΈ</button>
<button id="crossingBtn" onclick="setMode('crossing')">πΆ</button>
<button id="parkingBtn" onclick="setMode('parking')">π
ΏοΈ</button>
<button id="lightBtn" onclick="setMode('light')">π¦</button>
<button id="startBtn" onclick="setMode('start')">π</button>
<button id="targetBtn" onclick="setMode('target')">π―</button>
</div>
<script src="js/world.js"></script>
<script src="js/viewport.js"></script>
<script src="js/markings/marking.js"></script>
<script src="js/markings/stop.js"></script>
<script src="js/markings/start.js"></script>
<script src="js/markings/crossing.js"></script>
<script src="js/markings/parking.js"></script>
<script src="js/markings/light.js"></script>
<script src="js/markings/target.js"></script>
<script src="js/markings/yield.js"></script>
<script src="js/editors/markingEditor.js"></script>
<script src="js/editors/graphEditor.js"></script>
<script src="js/editors/crossingEditor.js"></script>
<script src="js/editors/stopEditor.js"></script>
<script src="js/editors/startEditor.js"></script>
<script src="js/editors/parkingEditor.js"></script>
<script src="js/editors/lightEditor.js"></script>
<script src="js/editors/targetEditor.js"></script>
<script src="js/editors/yieldEditor.js"></script>
<script src="js/items/tree.js"></script>
<script src="js/items/building.js"></script>
<script src="js/math/utils.js"></script>
<script src="js/math/graph.js"></script>
<script src="js/primitives/point.js"></script>
<script src="js/primitives/segment.js"></script>
<script src="js/primitives/polygon.js"></script>
<script src="js/primitives/envelope.js"></script>
<script>
myCanvas.width = 600;
myCanvas.height = 600;
const ctx = myCanvas.getContext("2d");
const worldString = localStorage.getItem("world");
const worldInfo = worldString ? JSON.parse(worldString) : null;
let world = worldInfo
? World.load(worldInfo)
: new World(new Graph());
const graph = world.graph;
const viewport = new Viewport(myCanvas, world.zoom, world.offset);
const tools = {
graph: { button: graphBtn, editor: new GraphEditor(viewport, graph) },
stop: { button: stopBtn, editor: new StopEditor(viewport, world) },
crossing: { button: crossingBtn, editor: new CrossingEditor(viewport, world) },
start: { button: startBtn, editor: new StartEditor(viewport, world) },
parking: { button: parkingBtn, editor: new ParkingEditor(viewport, world) },
light: { button: lightBtn, editor: new LightEditor(viewport, world) },
target: { button: targetBtn, editor: new TargetEditor(viewport, world) },
yield: { button: yieldBtn, editor: new YieldEditor(viewport, world) },
};
let oldGraphHash = graph.hash();
setMode("graph");
animate();
function animate() {
viewport.reset();
if (graph.hash() != oldGraphHash) {
world.generate();
oldGraphHash = graph.hash();
}
const viewPoint = scale(viewport.getOffset(), -1);
world.draw(ctx, viewPoint);
ctx.globalAlpha = 0.3;
for (const tool of Object.values(tools)) {
tool.editor.display();
}
requestAnimationFrame(animate);
}
function dispose() {
tools["graph"].editor.dispose();
world.markings.length = 0;
}
function save() {
world.zoom = viewport.zoom;
world.offset = viewport.offset;
const element = document.createElement("a");
element.setAttribute(
"href",
"data:application/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(world))
);
const fileName = "name.world";
element.setAttribute("download", fileName);
element.click();
localStorage.setItem("world", JSON.stringify(world));
}
function load(event) {
const file = event.target.files[0];
if (!file) {
alert("No file selected.");
return;
}
const reader = new FileReader();
reader.readAsText(file);
reader.onload = (evt) => {
const fileContent = evt.target.result;
const jsonData = JSON.parse(fileContent);
world = World.load(jsonData);
localStorage.setItem("world", JSON.stringify(world));
location.reload();
}
}
function setMode(mode) {
dissableEditors();
tools[mode].button.style.backgroundColor = "white";
tools[mode].button.style.filter = "";
tools[mode].editor.enable();
}
function dissableEditors() {
for (const tool of Object.values(tools)) {
tool.button.style.backgroundColor = "gray";
tool.button.style.filter = "grayscale(100%)";
tool.editor.disable();
}
}
</script>
</body>
</html>