JS to C# Array + Map Editor
A two-dimensional array converter from JS to C# (matrix format)
by Sam Fereday
HTML
<div class="container">
<ul>
<li><span>width:</span>
<input data-bind="value: w" value="16" />
</li>
<li><span>height:</span>
<input data-bind="value: h" value="16" />
</li>
</ul>
<well>
<button data-bind="click: extract">
Extract
</button>
<div id="output">
...
</div>
</well>
<div id="map">
<ul class="list-group" data-bind="foreach: cells">
<li class="cell" data-bind="style: { width: cssWidth }, css: { rowStart: isStart }">
<!--x:<small data-bind="text: x"></small>
y:<small data-bind="text: y"></small>-->
<span data-bind="text: tileType, click: $parent.changeType">...</span>
</li>
</ul>
</div>
</div>
CSS
body {
padding: 0;
margin: 0;
background: #333;
color: #fff;
font: 75%/1.4em arial;
text-align: center;
}
.container {
width: 75%;
padding: 3em 0;
margin: 0 auto;
overflow: auto;
}
div, ul, li, span {
box-sizing: border-box;
}
pre {
font-size: 1.2em;
}
#output {
width: 100%;
clear: both;
}
ul {
width: 100%;
list-style: none;
padding: 0;
margin: 0;
}
span {
display: block;
clear: both;
}
input {
clear: both;
}
#map .cell {
float: left;
text-align: center;
}
.cell span {
width: 100%;
height: 100%;
padding: 1em;
margin: 0;
background: #ccc;
color: #333;
border: 0;
cursor: pointer;
transition: all 0.2s ease;
}
.cell span:hover {
background: #ddd;
}
.cell.rowStart {
clear: left;
}
well {
padding: 2em;
position: absolute;
left: 0;
top: 0;
background: rgba(33,33,33,0.8);
}
button {
background: #777;
color: #fff;
border: 0;
}
JavaScript
// The map editor
const TileTypes = {
AirBlock: 0,
Standard: 1,
EndTile: 2
}
let CellModel = function(t, w, h, x, y, st) {
this.tileType = ko.observable(t);
this.w = ko.observable(w);
this.h = ko.observable(h);
this.x = ko.observable(x);
this.y = ko.observable(y);
this.isStart = ko.observable(st);
this.cssWidth = ko.pureComputed(function() {
return 100 / this.w() + '%';
}, this);
}
function makeMap(w, h) {
var cells = ko.observableArray();
for (var c = 0; c < h; c++) {
for (var r = 0; r < w; r++) {
cells.push(new CellModel(TileTypes.AirBlock, w, h, r, c, r === 0));
}
}
return cells;
}
let App = function() {
this.w = ko.observable(16);
this.h = ko.observable(16);
this.cells = makeMap(this.w(), this.h());
this.changeType = function(item) {
let nextType = item.tileType() + 1;
if (nextType > TileTypes.EndTile)
nextType = TileTypes.AirBlock;
item.tileType(nextType);
}
this.getCellsByRow = function(arr, n) {
return arr.filter(function(item) {
return item.y() === n;
});
}
this.extractAsTypes = function(arr) {
return arr.map(function(item) {
return item.tileType();
});
};
this.extract = function() {
let inflated = [];
for (var i = 0; i < 16; i++) {
let filteredCells = this.extractAsTypes(this.getCellsByRow(this.cells(), i));
inflated.push(filteredCells);
}
reveal(inflated);
}
};
ko.applyBindings(new App());
// Conversion
function convert(arr, dimensions) {
let built = '';
arr.forEach(function(row, i) {
built += i !== arr.length - 1 ? '{' + row.toString() + '},' : '{' + row.toString() + '}';
});
return 'int[,] yourArray = {' + built + '};';
}
// Put it to the test
function reveal(arr) {
let res = convert(arr);
document.getElementById('output').innerHTML = '<pre>' + res + '</pre>';
}