-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
385 lines (337 loc) · 16.3 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8 />
<meta name="description" content="A simple tool for planning rooms in RimWorld. Click and drag to lay out rooms horizontally or vertically." />
<title>RimWorld Room Planner</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="jquery-3.1.1.min.js"></script>
</head>
<body>
<canvas height="600" width="600"></canvas>
<div id="sidePanel">
<button id="optionsButton">Options</button>
<p class="helpText">(Click to toggle options)</p>
<div id="optionsDiv">
<div id="shapeRow" class="optionRow">
<form>
<label id="rectangularRadioLabel" class="switch">
<input id="rectangularRadioButton" type="radio" name="shape" value="rectangular" checked="checked"> Rectangular
</label>
<br />
<label id="circularRadioLabel" class="switch">
<input id="circularRadioButton" type="radio" name="shape" value="circular"> Circular (unfinished)
</label>
</form>
</div>
<div id="rectangularOptionsDiv">
<div id="roomSizeRow" class="optionRow">
<button id="decreaseRoomSize">-</button>
<div id="roomSizeDiv">Room size: 3</div>
<button id="increaseRoomSize">+</button>
</div>
<div>
<label id="squareRoomsLabel">
<input id="squareRoomsCheckBox" type="checkbox"> Square rooms
</label>
</div>
</div>
<div id="circularOptionsDiv">
</div>
</div>
</div>
<script>
var canvas = document.getElementsByTagName('canvas')[0];
var body = document.getElementsByTagName('body')[0];
var pressX = 0;
var pressY = 0;
var isPressed = false;
var currentMouseX = 0;
var currentMouseY = 0;
var releaseX = 0;
var releaseY = 0;
var drawRoom = false;
var isRectangular = true;
// Rectangular options
var roomSize = 3;
var hasSquareRooms = false;
var ctx = canvas.getContext('2d');
if (ctx) {
setupEvents();
draw();
}
// Set up events for buttons.
var optionsButton = document.getElementById("optionsButton");
var optionsDiv = document.getElementById("optionsDiv");
optionsButton.onmousedown = function() {
optionsDiv.style.display = optionsDiv.style.display === "none" ? "block" : "none";
}
document.getElementById("rectangularRadioLabel").onclick = function() {
isRectangular = true;
document.getElementById("rectangularOptionsDiv").style.display = "block";
document.getElementById("circularOptionsDiv").style.display = "none";
draw();
}
document.getElementById("circularRadioLabel").onclick = function() {
isRectangular = false;
document.getElementById("rectangularOptionsDiv").style.display = "none";
document.getElementById("circularOptionsDiv").style.display = "block";
draw();
}
// Rectangular options
var roomSizeDiv = $("#roomSizeDiv");
var decreaseRoomSizeButton = document.getElementById("decreaseRoomSize");
decreaseRoomSizeButton.onmousedown = function() {
--roomSize;
if (roomSize < 1)
roomSize = 1;
roomSizeDiv.text("Room size: " + roomSize);
draw();
}
var increaseRoomSize = document.getElementById("increaseRoomSize");
increaseRoomSize.onmousedown = function() {
++roomSize;
roomSizeDiv.text("Room size: " + roomSize);
draw();
}
document.getElementById("squareRoomsLabel").onclick = function() {
hasSquareRooms = document.getElementById("squareRoomsCheckBox").checked;
$("#roomSizeRow *").prop("disabled", hasSquareRooms);
roomSizeDiv.css({ opacity: hasSquareRooms ? 0.4 : 1.0 });
draw();
}
// Fill the screen with the canvas.
window.onload = function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
// http://stackoverflow.com/a/17130415/904422
function getMousePos(canvas, clientX, clientY) {
var rect = canvas.getBoundingClientRect(), // abs. size of element
scaleX = canvas.width / rect.width, // relationship bitmap vs. element for X
scaleY = canvas.height / rect.height; // relationship bitmap vs. element for Y
return {
x: (clientX - rect.left) * scaleX, // scale mouse coordinates after they have
y: (clientY - rect.top) * scaleY // been adjusted to be relative to element
}
}
// Handle user interaction with the canvas.
function setupEvents() {
function handleMouseDown(clientX, clientY, button) {
if (button === undefined)
button = 0;
if (button === 0) {
isPressed = true;
drawRoom = true;
var mousePos = getMousePos(canvas, clientX, clientY);
pressX = mousePos.x;
pressY = mousePos.y;
} else {
isPressed = false;
drawRoom = false;
draw();
}
}
function handleMouseMove(clientX, clientY) {
var mousePos = getMousePos(canvas, clientX, clientY);
currentMouseX = mousePos.x;
currentMouseY = mousePos.y;
draw();
}
function handleMouseUp(clientX, clientY) {
currentMouseX = -1;
currentMouseY = -1;
var mousePos = getMousePos(canvas, clientX, clientY);
releaseX = mousePos.x;
releaseY = mousePos.y;
isPressed = false;
draw();
}
canvas.onmousedown = function(event) {
handleMouseDown(event.clientX, event.clientY);
};
canvas.onmousemove = function(event) {
handleMouseMove(event.clientX, event.clientY);
};
canvas.onmouseup = function(event) {
handleMouseUp(event.clientX, event.clientY);
};
function handleTouchStart(event) {
event.preventDefault();
handleMouseDown(event.touches[0].clientX, event.touches[0].clientY);
}
function handleTouchMove(event) {
event.preventDefault();
handleMouseMove(event.touches[0].clientX, event.touches[0].clientY);
}
function handleTouchEnd(event) {
event.preventDefault();
handleMouseUp(event.changedTouches[0].clientX, event.changedTouches[0].clientY);
}
canvas.addEventListener("touchstart", handleTouchStart, false);
canvas.addEventListener("touchmove", handleTouchMove, false);
canvas.addEventListener("touchend", handleTouchEnd, false);
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!drawRoom)
return;
var tileWidth = 30;
var tileHeight = 30;
var areaX = pressX;
var areaY = pressY;
var areaWidth = (isPressed ? currentMouseX : releaseX) - pressX;
var areaHeight = (isPressed ? currentMouseY : releaseY) - pressY;
if (!isRectangular) {
// If it's circular, its width and height have to be equal, so pick the lesser of the two.
areaWidth = Math.min(areaWidth, areaHeight);
areaHeight = areaWidth;
}
var tilesWide = Math.max(1, Math.ceil(areaWidth / tileWidth));
var tilesHigh = Math.max(1, Math.ceil(areaHeight / tileHeight));
var innerTilesWide = Math.max(0, tilesWide - 2);
var innerTilesHigh = Math.max(0, tilesHigh - 2);
var wallColour = "#aa3333";
ctx.strokeStyle = "black";
if (isRectangular) {
// Draw rectangular area
var distributeHorizontally = tilesWide > tilesHigh;
var effectiveRoomSize = hasSquareRooms ? (distributeHorizontally ? innerTilesHigh : innerTilesWide) : roomSize;
var canDrawRoomsHorizontally = false;
var canDrawRoomsVertically = false;
// Can't make rooms with less than 5 inner tiles.
if (distributeHorizontally && innerTilesWide >= 5) {
// We need effectiveRoomSize - 1 walls as well.
canDrawRoomsHorizontally = ((innerTilesWide - (effectiveRoomSize - 1)) / effectiveRoomSize) > 0;
} else if (!distributeHorizontally && innerTilesHigh >= 5) {
canDrawRoomsVertically = ((innerTilesHigh - (effectiveRoomSize - 1)) / effectiveRoomSize) > 0;
}
for (var row = 0; row < tilesHigh; ++row) {
for (var column = 0; column < tilesWide; ++column) {
var isEdge = (column == 0 || column == tilesWide - 1 || row == 0 || row == tilesHigh - 1);
if (isEdge)
ctx.fillStyle = wallColour;
else {
ctx.fillStyle = "#222222";
if (canDrawRoomsHorizontally) {
var wallsEncountered = column / (effectiveRoomSize + 1);
if (wallsEncountered - Math.floor(wallsEncountered) == 0)
ctx.fillStyle = wallColour;
} else if (canDrawRoomsVertically) {
var wallsEncountered = row / (effectiveRoomSize + 1);
if (wallsEncountered - Math.floor(wallsEncountered) == 0)
ctx.fillStyle = wallColour;
}
}
ctx.fillRect(areaX + (column * tileWidth), areaY + (row * tileHeight), tileWidth, tileHeight);
ctx.strokeRect(areaX + (column * tileWidth), areaY + (row * tileHeight), tileWidth, tileHeight);
}
}
} else {
// Draw circular area
function distance(p1, p2) {
dx = p2.x - p1.x; dx *= dx;
dy = p2.y - p1.y; dy *= dy;
return Math.sqrt(dx + dy);
}
var radius = tilesWide / 2;
var x = 0;
var y = 0;
// http://stackoverflow.com/a/14612409/904422
for (var j = x - radius; j <= x + radius; j++) {
for (var k = y - radius; k <= y + radius; k++) {
if (distance({ x: j, y: k }, { x: x, y: y}) <= radius) {
var row = k;
var column = j;
ctx.fillStyle = wallColour;
var tileX = areaX + (column * tileWidth) + (radius * tileWidth);
var tileY = areaY + (row * tileHeight) + (radius * tileHeight);
ctx.fillRect(tileX, tileY, tileWidth, tileHeight);
ctx.strokeRect(tileX, tileY, tileWidth, tileHeight);
}
}
}
}
// Draw total height text guide
ctx.font = "30px sans-serif";
ctx.fillStyle = "white";
ctx.textAlign = "right";
ctx.textBaseline = "hanging";
var mWidth = ctx.measureText("M").width;
var totalHeightTextY = Math.max(areaY, areaY + areaHeight / 2 - mWidth / 2);
ctx.fillText(tilesHigh, areaX - 10, totalHeightTextY);
// Draw upper total height line guide.
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.moveTo(areaX - 20, areaY);
ctx.lineTo(areaX - 20, Math.max(areaY, totalHeightTextY - 10))
ctx.stroke();
// Draw lower total height line guide.
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.moveTo(areaX - 20, totalHeightTextY + mWidth + 4);
ctx.lineTo(areaX - 20, areaY + (tilesHigh * tileHeight));
ctx.stroke();
// Draw total width text guide
ctx.textAlign = "left";
var widthTextWidth = ctx.measureText(tilesWide).width;
var totalWidthTextX = Math.max(areaX, areaX + areaWidth / 2 - widthTextWidth / 2);
ctx.fillText(tilesWide, totalWidthTextX, areaY - mWidth);
// Draw left total height line guide.
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.moveTo(areaX, areaY - 20);
ctx.lineTo(Math.max(areaX, totalWidthTextX - 10), areaY - 20);
ctx.stroke();
// Draw right total height line guide.
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.moveTo(totalWidthTextX + widthTextWidth + 10, areaY - 20);
ctx.lineTo(areaX + (tilesWide * tileWidth), areaY - 20);
ctx.stroke();
// Inner guides don't make sense for a circle.
if (innerTilesHigh > 0 && innerTilesWide > 0 && isRectangular) {
if (innerTilesHigh > 1) {
// Draw inner height text guide
ctx.textAlign = "left";
ctx.textBaseline = "hanging";
var innerHeightTextY = Math.max(areaY + tileHeight * 2, areaY + areaHeight / 2 - mWidth / 2);
ctx.fillText(innerTilesHigh, areaX + tileWidth + 10, innerHeightTextY);
// Draw upper inner height line guide.
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.moveTo(areaX + tileWidth + 20, areaY + tileHeight + 20);
ctx.lineTo(areaX + tileWidth + 20, Math.max(areaY + tileHeight + 20, innerHeightTextY - 10))
ctx.stroke();
// Draw lower inner height line guide.
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.moveTo(areaX + tileWidth + 20, innerHeightTextY + mWidth + 4);
ctx.lineTo(areaX + tileWidth + 20, areaY + ((tilesHigh - 1) * tileHeight));
ctx.stroke();
}
if (innerTilesWide > 1) {
// Draw inner width text guide
ctx.textAlign = "left";
var innerWidthTextWidth = ctx.measureText(innerTilesWide).width;
var innerWidthTextX = Math.max(areaX + tileWidth * 2, areaX + areaWidth / 2 - innerWidthTextWidth / 2);
ctx.fillText(innerTilesWide, innerWidthTextX, areaY + tileHeight + 10);
// Draw left inner width line guide.
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.moveTo(areaX + tileWidth + 20, areaY + tileHeight + 20);
ctx.lineTo(innerWidthTextX - 10, areaY + tileHeight + 20);
ctx.stroke();
// Draw right inner width line guide.
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.moveTo(innerWidthTextX + innerWidthTextWidth + 10, areaY + tileHeight + 20);
ctx.lineTo(areaX + tileWidth + (innerTilesWide * tileWidth), areaY + tileHeight + 20);
ctx.stroke();
}
}
}
</script>
<!-- <a href="http://github.com/mitchcurtis/rimworld-room-planner"><img style="position: absolute; top: 0; left: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_left_darkblue_121621.png" alt="Fork me on GitHub" /></a> -->
</body>
</html>