JS to C# Array + Map Editor

A two-dimensional array converter from JS to C# (matrix format)

by Sam Fereday

HTML

<div class="container">

  <well>
    <ul>
      <li><span>width:</span>
        <input data-bind="value: w" value="" />
      </li>
      <li><span>height:</span>
        <input data-bind="value: h" value="" />
      </li>
    </ul>
    <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="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,
well {
  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 {
  text-align: center;
  margin: 0 auto;
}

#map .cell {
  width: 48px;
  height: 48px;
  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 {
  width: 100%;
  padding: 2em;
  display: block;
  background: rgba(33, 33, 33, 0.8);
}

well ul {
  margin-bottom: 1em;
  float: left;
}

well input {
  width: 32px;
}

well li {
  width: 50%;
  float: left;
}

button {
  padding: 1em;
  background: #777;
  color: #fff;
  border: 0;
  cursor: pointer;
}

button:hover {
  background: #444;
}

JavaScript

// The map editor
const TileTypes = {
  Null: 0,
  AirBlock: 1,
  Standard: 2,
}

// Number of const tiles (for indexing)
let tileTypeMax = 3;

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.Null, w, h, r, c, r === 0));
    }
  }

  return cells;

}

let App = function() {

  this.w = ko.observable(12);
  this.h = ko.observable(12);

  this.cells = makeMap(this.w(), this.h());

  this.changeType = function(item) {

    let nextType = item.tileType() + 1;

    if (nextType >= tileTypeMax)
      nextType = TileTypes.Null;

    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 < this.w(); i++) {
      let filteredCells = this.extractAsTypes(this.getCellsByRow(this.cells(), i));
      inflated.push(filteredCells);
    }

    reveal(inflated);

  }

};

ko.applyBindings(new App());

// Conversion (bolt on utility)
function convert(arr, dimensions) {

  let built = '';

  arr.forEach(function(row, i) {

    built += i !== arr.length - 1 ? '{' + row.toString() + '},\n' : '{' + 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>';
}