This repository was archived by the owner on Jul 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
295 lines (244 loc) · 8.85 KB
/
Copy pathindex.html
File metadata and controls
295 lines (244 loc) · 8.85 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<meta http-equive="Pragma" content="no-cache">
<title>Blocklords Map Editor</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/blog-post.css" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="#">Blocklords map editor – a tool to define the regions neighbourhood</a>
</div>
</nav>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Post Content Column -->
<div class="col-lg-8">
<!-- Side Widget -->
<div class="card my-4">
<h5 class="card-header">Graph Editor</h5>
<div class="card-body">
<div class="row">
<div class="col-lg-12 graph-container pre-scrollable" >
</div>
</div>
<div class="row">
<div class="col-lg-12">
<ul class="list-unstyled mb-0">
<li>
<p>Tip: set the background map for Graph editor at the source code!</p>
</li>
<li>
<p>Tip: Click on empty space to add node!</p>
</li>
<li>
<p>Tip: Drag from node to node to add edge!</p>
</li>
<li>
<p>Tip: Click/Select + Delete to delete node/edge!</p>
</li>
<li>
<p>Tip: Click/Select Edge + Enter to change edge's weight!</p>
</li>
<li>
<p>Tip: Press Ctrl to Drag node around!</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!-- Sidebar Widgets Column -->
<div class="col-md-4">
<!-- Side Widget -->
<div class="card my-4">
<h5 class="card-header">Vertices List</h5>
<div class="card-body">
<div class="row">
<div class="col-lg-12 form-group">
<textarea class="form-control" id="vertices-list">
</textarea>
</div>
</div>
</div>
</div>
<!-- Side Widget -->
<div class="card my-4">
<h5 class="card-header">Edges List</h5>
<div class="card-body">
<div class="row">
<div class="col-lg-12 form-group">
<textarea class="form-control" id="edges-list">
</textarea>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
<!-- Footer -->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">by Medet Ahmetson</p>
</div>
<!-- /.container -->
</footer>
<div class="canvas-container" id="canvas-container">
<canvas id="regions"></canvas>
</div>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="graphlib.min.js"></script>
<script>
var graph = new graphlib.Graph({directed: false});
var verticesList = document.getElementById("vertices-list");
var edgesList = document.getElementById("edges-list");
function addNodeToGraph(node) {
graph.setNode(node.id, node);
}
function isNodeOnGraph(node) {
return graph.node(node.id) != undefined;
}
function addEdgeToGraph(source, destination) {
graph.setEdge(source.id, destination.id);
}
function removeNodeFromGraph(node) {
// list of shortest path with one weight
// for each of the returned nodes, remove the edge
// graph.removeEdge(node.id, shortest[i]);
graph.removeNode(node.id);
}
function reListVertices() {
verticesList.innerHTML = "";
for (var i=0; i<nodes.length; i++) {
var id = nodes[i].id;
var node = nodes[i];
var nodeToSet = JSON.parse(JSON.stringify(node))
nodeToSet.y = imgHeight - node.y;
var vertex = "graph.setNode('"+id+"', "+JSON.stringify(nodeToSet)+");\n";
verticesList.innerHTML = verticesList.innerHTML + vertex;
}
verticesList.setAttribute("rows", nodes.length)
}
function reListEdges() {
edgesList.innerHTML = "";
for (var i=0; i<graph.edges().length; i++) {
var edge = graph.edges()[i];
var str = "graph.setEdge('"+edge.v+"', '"+edge.w+"');\n";
edgesList.innerHTML = edgesList.innerHTML + str;
}
verticesList.setAttribute("rows", graph.edges().length)
}
</script>
<script type="text/javascript">
/**
* Return the location of the element (x,y) being relative to the document.
*
* @param {Element} obj Element to be located
*/
function getElementPosition(obj) {
var curleft = 0, curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return { x: curleft, y: curtop };
}
return undefined;
}
/**
* return the location of the click (or another mouse event) relative to the given element (to increase accuracy).
* @param {DOM Object} element A dom element (button,canvas,input etc)
* @param {DOM Event} event An event generate by an event listener.
*/
function getEventLocation(element,event){
// Relies on the getElementPosition function.
var pos = getElementPosition(element);
return {
x: (event.pageX - pos.x),
y: (event.pageY - pos.y)
};
}
/**
* Draw an image (from your own domain or base64)
* @param {String} sourceurl
*/
function drawImageFromWebUrl(sourceurl, canvas){
var img = new Image(imgWidth, imgHeight);
img.addEventListener("load", function () {
// The image can be drawn from any source, the canvas will be filled with the image.
canvas.getContext("2d").drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
});
img.setAttribute("src", sourceurl);
}
function rgbToHex(r, g, b) {
if (r > 255 || g > 255 || b > 255)
throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16);
}
function hexToNumlist(hex) {
var ints = [];
for (var i=0; i<hex.length/2; i++) {
ints[i] = parseInt(hex.substring(i * 2, i*2+2), 16);
}
return ints;
}
</script>
<!-- Functions of game -->
<script type="text/javascript">
function getColorByCoord(x, y) {
var context = regions.getContext('2d');
var pixelData = context.getImageData(x, y, 1, 1).data;
// If transparency on the pixel , array = [0,0,0,0]
if((pixelData[0] == 0) && (pixelData[1] == 0) && (pixelData[2] == 0) && (pixelData[3] == 0)){
// Do something if the pixel is transparent
}
// Convert it to HEX if you want using the rgbToHex method.
var hex = ("000000" + rgbToHex(pixelData[0], pixelData[1], pixelData[2])).slice(-6);
var hashedHex = "#" + hex;
console.log("Region is "+hashedHex);
return hex;
}
</script>
<script>
let regionsUrl = "regions.png";
let bordersUrl = "borders.png";
let imgUrl = "world_back.jpg";
let imgWidth = 1015;
let imgHeight = 561;
var activeRegion = 0;
var imgPointSource = null;
// an array of points that defines the outline path (stroke path)
var regions = document.getElementById("regions");
regions.setAttribute("width", imgWidth);
regions.setAttribute("height", imgHeight);
drawImageFromWebUrl(regionsUrl, regions);
// ------------------------------
// Region drawing
// ------------------------------
window.onload = function(){
var container = document.getElementById("canvas-container");
container.setAttribute("style", "height: "+imgHeight+"px; width: "+imgWidth+"px;");
//container.addEventListener("mousemove", onMap, false);
};
</script>
<script src="visual.js"></script>
</body>
</html>