Skip to content

Instantly share code, notes, and snippets.

@park-brian
Last active January 30, 2021 20:12
Show Gist options
  • Save park-brian/913be75f4ee1b7730c9d3f487879f598 to your computer and use it in GitHub Desktop.
Save park-brian/913be75f4ee1b7730c9d3f487879f598 to your computer and use it in GitHub Desktop.
RWS Editor Test 2
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<input id="height" placeholder="Height">
<input id="width" placeholder="Width">
<button id="load">Load</button>
<button id="save">Save</button>
<textarea id="input" style="width: 100%" rows="20">7Z3LzTy3DcABH9NGrm7AJbiOHF1BKvDFp1RgwDX44lYMGB+QGpxjnCwEgZRESqTEx8zumPD/26dE8idSz5n9619/Ocr3/y3/fv/f
f/xhI68SX+XVv36aQw2c/jv1g+7wuH3vJd/9gEVfT/0GVZ6n13otrJiDyPyusxE+C0RmzE/aX/+NWk5L/LsfSkvXlt1KyzyGOvab
ZdxX6jMrdMxxhNTX1u3Pgnmt9VV2yxzatq6WbOqgAbbNNvJX9He15tsatuy87r6mwnvMKrv6V+a2HtLpQnmzbd8n/G1aM8+8/ZSm
RP4dWVY70f67H6xJav3J8bahznlBqyXFfa89zduiJPudMv/HHx4k9/Tpee8xb6nzXj/TdTeDrPKPrMfTjk/aMvMye9GH4mwX6XOv
t5+D3rjtpUceu31GO9egW6Z0lCPVoX7uvLXa8G7HkrbMZxm8j9fR9zBeb0fx0tnh3OoZOx/mFhnKivma+A71dW7nmPfj8FH0Xhvn
4FQL1REHve/GXEZ8h3nxLG0d5fORep+FeU4Sn/O147J0c9d5q27r17eVezEHShbM57VomVNlQk+kXa/g+bX2tGthJb/mUPdjTnkb
hPfdLLJnNa2Zw6hg1kZ3hLLxz9+g1vK4tzNrXcaHeT8eGmu187qU+TlZjRZQ04v36/++fVTPU6v9Y1lxzG08QPuDo66va+4RnD+8
iI/MS2T/+VvPHGtbmbd9WdsfWPDue5+aY+x4Ux6QMd+vTbYG8PqkB+9Rh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P5n4lX585tYfTxnod1dky5z8TwdxP7sCcP5UDvE93HynPREX0dblnyuwXWc5PhHP+
6B+/k2QTPaWuuyvtSHx8xLeH95FsohLms3O2nmfIstl8LnOKe//buP5nS7I5fSZ1erYWd24wm9PnkedOZUSeGcxm9GnU+7uqZGic
TejTqMf9ktxMsgl9GvNrXJ2dzeizuOfzLjJ6K5vZO1O/ioyeymb2cJdzO/nu/dbk1/c2zqbiK2dWUt/NJmrB/L25n9lIfy+bqZVk
s/ETewuzWT3UpYysy3sHySbjKw/zh/lJSe8j2VR8xcq+bEoP82jJZvQwj5dsRg/zaMkm9DCPlmw+D/NIySbzUI+VbCYP83jJZvIw
j5ZsIg/1eMnm8TCPlmwaD/F4yebxMI+XbB7RzP/8rZdsAg9zP+L/+RuWT6aeTSSS+Td//+bv9d/PJf4JzCt1Ls6zCTzM/ZiPPfmn
Mv8E6sVOOs6zvf8w96Te9+fZfn+YRzBv4zzb7w9zf+r9iD3b7w/zCObjzPxTyWfTiJJxZv5Qf3eh4/xh/s7yzM8/jzrH/KH+7vIQ
/0TqRbK9fQ3JpvBQjxcfz/4P</textarea>
<fieldset>
<legend>Roofs</legend>
<label><input type="radio" name="cellType" onchange="window.cellType = event.target.value" value="0">NONE</input></label>
<label><input type="radio" name="cellType" onchange="window.cellType = event.target.value" value="5133">CONSTRUCTED_ROOF</input></label>
<label><input type="radio" name="cellType" onchange="window.cellType = event.target.value" value="6699">THIN_ROCK_ROOF</input></label>
<label><input type="radio" name="cellType" onchange="window.cellType = event.target.value" value="10820">MOUNTAIN_ROOF</input></label>
</fieldset>
<fieldset>
<legend>Terrains</legend>
<label><input type="radio" name="cellType" onchange="window.cellType = event.target.value" value="37631">NONE</input></label>
<label><input type="radio" name="cellType" onchange="window.cellType = event.target.value" value="27449">MARBLE</input></label>
<label><input type="radio" name="cellType" onchange="window.cellType = event.target.value" value="56931">SANDSTONE</input></label>
</fieldset>
<canvas id="edit" width="400" height="400"></canvas>
<textarea id="output" style="width: 100%" rows="20" readonly></textarea>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pako.min.js"></script>
<script src="script.js"></script>
</body>
</html>
var heightInput = document.querySelector('#height');
var widthInput = document.querySelector('#width');
var loadButton = document.querySelector('#load');
var saveButton = document.querySelector('#save');
var inputTextArea = document.querySelector('#input');
var editCanvas = document.querySelector('#edit');
var outputTextArea = document.querySelector('#output');
var brushActive = false;
var cellType = 0;
/**
* Inflates a deflated base64 string as an ArrayBuffer
* @param {string} base64 - A base64 string
* @returns {ArrayBuffer} - Inflated data
*/
function loadDeflatedBase64(base64) {
base64 = base64.replace(/\n/g, '');
return pako.inflateRaw(atob(base64)).buffer;
}
/**
* Deflates an ArrayBuffer as a base64 string
* Returns base64 encoded lines, suitable for saving
*/
function saveAsDeflatedBase64(arrayBuffer, lineWidth) {
var uInt8Array = new Uint8Array(arrayBuffer);
var data = btoa(pako.deflateRaw(uInt8Array, {to: 'string'}));
return chunk(data, lineWidth || 100).join('\n');
}
/**
* Splits an array-like object (eg: strings, buffers)
* into chunks of the specified length
*/
function chunk(array, size) {
var parts = [];
for (var i = 0; i < array.length; i += size)
parts.push(array.slice(i, i + size))
return parts;
}
function drawEditor(arrayBuffer, width, height) {
var cells = new Uint16Array(arrayBuffer);
var ctx = editCanvas.getContext('2d');
// do not proceed if the data does not match the dimensions provided
if (cells.length != (width * height)) return;
var cellSize = 8;
var cellDefs = {
0: {
type: 'NONE',
color: '#fff',
},
5133: {
type: 'CONSTRUCTED_ROOF',
color: '#ddd',
},
6699: {
type: 'THIN_ROCK_ROOF',
color: '#666',
},
10820: {
type: 'MOUNTAIN_ROOF',
color: '#222',
},
37631: {
type: 'NONE',
color: '#fff',
},
27449: {
type: 'MARBLE',
color: '#eee',
},
56931: {
type: 'SANDSTONE',
color: '#ddd',
},
};
renderCells(cells, width, height);
editCanvas.onmousedown = function() {
brushActive = true;
}
editCanvas.onmouseup = function() {
brushActive = false;
}
editCanvas.onmouseleave = function() {
brushActive = false;
}
editCanvas.onmousemove = function(event) {
var canvas = event.target;
var x = Math.floor((event.pageX - canvas.offsetLeft) / cellSize);
var y = height - Math.floor((event.pageY - canvas.offsetTop) / cellSize);
var index = y * width + x;
console.log(x, y, cells[index], getColor(cells[index]));
if (!brushActive) return false;
var cell = cells[index];
var cellTypes = Object.keys(cellDefs);
cells[index] = cellType;
renderCell(cells, width, height, x, y);
}
function renderCells(cells, width, height) {
editCanvas.width = cellSize * width;
editCanvas.height = cellSize * height;
for (var y = 0; y < height; y ++) {
for (var x = 0; x < width; x ++) {
renderCell(cells, width, height, x, y)
}
}
}
function renderCell(cells, width, height, x, y) {
// ctx.fillStyle = cellDefs[cells[y * width + x]].color;
var value = cells[y * width + x];
var color = getColor(value);
ctx.fillStyle = color;
ctx.fillRect(x * cellSize, (height - y) * cellSize, cellSize, cellSize);
}
function getColor(value) {
this.colors = this.colors || {};
var randomValue = Math.floor(Math.random() * 255);
if (cellDefs[value])
this.colors[value] = cellDefs[value].color;
if (!this.colors[value]) {
this.colors[value] = `rgb(${randomValue}, ${randomValue}, ${randomValue})`;
console.log(value, this.colors[value])
}
return this.colors[value]
}
}
loadButton.onclick = function() {
var height = +heightInput.value || 250;
var width = +widthInput.value || 250;
var arrayBuffer = loadDeflatedBase64(inputTextArea.value);
drawEditor(arrayBuffer, width, height);
saveButton.onclick = function() {
var output = saveAsDeflatedBase64(arrayBuffer);
outputTextArea.value = output;
}
}
canvas {
border: 1px solid #ddd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment