-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
177 lines (168 loc) · 4.6 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
<!DOCTYPE html>
<html>
<head>
<title>Malgate - Map Make</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
html, body, table {
margin:0;
padding:0;
}
table {
overflow: scroll;
max-width: 500px;
border: 1px solid black;
padding: 100px;
}
tr {
overflow: scroll;
}
td {
border: 1px dashed grey;
min-width: 20px;max-width: 20px;
min-height: 20px;max-height: 20px;
display: table-cell;
text-align: center;
overflow: hidden;
cursor: pointer;
-webkit-user-select: none; /* Chrome all / Safari all */
-moz-user-select: none; /* Firefox all */
-ms-user-select: none; /* IE 10+ */
}
</style>
</head>
<script type="text/javascript">
</script>
<body onload="BrushController.loadButtons(document.getElementById('buttonHolder'));">
<nav>
<button onclick="giveLayout(100,100);">New File</button>
<input type="file" name="File Upload" id="upload" accept=".gate" />
<button onclick="exportLayout();">Export File</button>
</nav>
<nav id="buttonHolder"></nav>
<table id="table"></table>
<script type="text/javascript">
var BrushController = {
brushes: [
{
name: 'empty',
data: {
color: 'white',
}
},
{
name: 'trees',
data: {
name: 'tree',
color: 'green',
amount: 15
}
},
{
name: 'rocks',
data: {
name: 'rock',
color: 'grey',
amount: 10
}
}
],
loadButtons: function(_element){
var scope = this;
for (i in this.brushes){
var btn = document.createElement('button');
btn.innerHTML = this.brushes[i].name;
btn.brush = this.brushes[i];
btn.addEventListener('click', function(){
BrushController.currentBrush = this.brush;
})
_element.appendChild(btn);
}
},
currentBrush: null
}
document.getElementById('upload').addEventListener('change', importLayoutFromFile, false);
function giveLayout(_w, _h){
var table = document.getElementById('table');
$("#table").empty();
for (var w = 0; w < _w; w++) {
var tr = document.createElement("TR");
for (var h = 0; h < _h; h++) {
var td = document.createElement("TD");
td.innerHTML = '-';
td.gameData = '';
td.addEventListener('mousemove',function(e){
if(e.buttons < 1) return;
this.innerHTML = BrushController.currentBrush.name;
this.gameData = BrushController.currentBrush.data;
this.style.background = BrushController.currentBrush.data.color;
})
tr.appendChild(td);
}
table.appendChild(tr);
}
}
function exportLayout(_table){
var table = document.getElementById('table');
if (_table) table = _table;
var grid = {};
for (var w = 0; w < table.childNodes.length; w++) {
var tr = table.childNodes[w];
var new_row = {};
for (var h = 0; h < tr.childNodes.length; h++) {
var td = tr.childNodes[h];
var cell_data = td.gameData;
new_row[h] = cell_data;
}
grid[w] = new_row;
}
console.log(grid)
saveText(JSON.stringify(grid), 'output.gate')
}
function saveText(text, filename){
var a = document.createElement('a');
a.setAttribute('href', 'data:text/plain;charset=utf-u,'+(text));
a.setAttribute('download', filename);
a.click()
}
function importLayoutFromFile(_e){
console.log(_e)
var filePath = $("#upload").val();
var startIndex = filePath.indexOf('\\') >= 0 ? filePath.lastIndexOf('\\') : filePath.lastIndexOf('/');
var filename = filePath.substring(startIndex);
if(filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1);
}
$.ajax({
url: filename,
success: onOpenLoad
});
}
function onOpenLoad(fileContent) {
var data = JSON.parse(fileContent);
console.log(data);
var table = document.getElementById('table');
$("#table").empty();
for (w in data) {
var tr = document.createElement("TR");
for (h in data[w]) {
var td = document.createElement("TD");
td.innerHTML = data[w][h].name;
if (!data[w][h].name) td.innerHTML = '-';
td.gameData = data[w][h];
td.style.background = data[w][h].color;
td.addEventListener('mousemove',function(e){
if(e.buttons < 1) return;
this.innerHTML = BrushController.currentBrush.name;
this.gameData = BrushController.currentBrush.data;
this.style.background = BrushController.currentBrush.data.color;
})
tr.appendChild(td);
}
table.appendChild(tr);
}
// do something with the data
}
</script>
</body>
</html>