-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcDoc.js
397 lines (377 loc) · 9.72 KB
/
vcDoc.js
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
386
387
388
389
390
391
392
393
394
395
396
//// utils: is the crappiness of JS boring? yes.
function strEq(a,b) { // string compare
var a1 = new String(a).valueOf();
var b1 = new String(b).valueOf();
return (a1===b1);
}
// object type
const VC_N = "VC_N"; // null
const VC_A = "VC_A";
const VC_B = "VC_B";
function vcObj() {
this.id = 0;
this.isSelected = false;
this.tp = VC_N;
this.rem = "note";
this.x = 100.0;
this.y = 100.0;
this.z = 0.0;
this.szx = 16.0; // size of thing onscreen
this.szy = 16.0; // set at redraw by tile params
this.tx = 0; //-- /// sprites top-left tile
this.ty = 0; // top-left tile
this.tsx = 1.0; // tile spans
this.tsy = 1.0;
this.tmag = 1.0; // scr pix per tx pix
this.target = 0;
this.mass = 0.0; //-- /// particles(?)
this.k = 0.0;
this.amp = 0.0;
this.offset = 0.0;
this.freq = 1.0;
}
vcObj.prototype.typeColor = function() {
res = "#888888";
switch (this.tp) {
case VC_N: res = "#000000"; break;
case VC_A: res = "#ff8888"; break;
case VC_B: res = "#88ff88"; break;
}
return res;
}
vcObj.prototype.copy = function(tmpl) {
this.id = tmpl.id;
this.isSelected = tmpl.isSelected;
this.tp = tmpl.tp;
this.rem = tmpl.rem;
this.x = tmpl.x;
this.y = tmpl.y;
this.z = tmpl.z;
this.szx = tmpl.szx;
this.szy = tmpl.szy;
this.tx = tmpl.tx;
this.ty = tmpl.ty;
this.tsx = tmpl.tsx;
this.tsy = tmpl.tsy;
this.tmag = tmpl.tmag;
this.target = tmpl.target;
this.mass = tmpl.mass;
this.k = tmpl.k;
this.amp = tmpl.amp;
this.offset = tmpl.offset;
this.freq = tmpl.freq;
}
vcObj.prototype.draw = function(theCx, theTiles) { // except, use the texture
var is = 32.0;
var itx = this.tx * is;
var ity = this.ty * is;
this.szx = this.tsx * is * this.tmag;
this.szy = this.tsy * is * this.tmag;
if (this.isSelected) {
theCx.fillStyle = "#ffff0088";
theCx.fillRect(this.x-2, this.y-2, this.szx+4, this.szy+4);
}
theCx.fillStyle = "#ffff00";
theCx.drawImage(theTiles, itx, ity, this.tsx*is, this.tsy*is, this.x, this.y, this.szx, this.szy);
}
vcObj.prototype.getAttribsHtml = function() {
var res = "";
for (var prop in this) {
if (Object.prototype.hasOwnProperty.call(this, prop)) {
res += '<div class="conAttributeItem">';
res += '<div class="conAttributeNameSide">'+prop+':</div>';
res += '<div class="conAttributeValueSide">';
res += '<input class="conAttributeInputBox" id="atr' +this.id+prop+ '" value="' + this[prop];
var qprop = "'" + prop + "'";
res += '" onchange="atrKeyIn(' + this.id+','+qprop+')" /></div>';
res += '<div class="clear"></div></div>';
}
}
return res;
}
vcObj.prototype.setAttr = function(which,toWhat) {
switch (which) {
case "tp": this[which] = toWhat; break;
case "rem": this[which] = toWhat; break;
case "isSelected": this[which] = strEq("true",toWhat); break;
case "tmag": this[which] = parseFloat(toWhat); break;
case "mass": this[which] = parseFloat(toWhat); break;
case "k": this[which] = parseFloat(toWhat); break;
case "amp": this[which] = parseFloat(toWhat); break;
case "offset": this[which] = parseFloat(toWhat); break;
case "freq": this[which] = parseFloat(toWhat); break;
default: this[which]=parseInt(toWhat);
}
}
vcObj.prototype.hitTest = function(cx,cy) {
var res = true;
if (cx<this.x) { res = false; }
if (cy<this.y) { res = false; }
if (cx>this.x+this.szx) { res = false; }
if (cy>this.y+this.szy) { res = false; }
return res;
}
vcObj.prototype.move = function(cx,cy) { this.x=cx; this.y=cy; }
vcObj.prototype.selectBox = function(xlo,ylo,xhi,yhi) {
var res = true;
if (xhi<this.x) { res = false; }
if (yhi<this.y) { res = false; }
if (xlo>this.x+this.szx) { res = false; }
if (xlo>this.y+this.szy) { res = false; }
return res;
}
vcObj.prototype.toText = function() {
var res = "";
for (var prop in this) {
if (Object.prototype.hasOwnProperty.call(this, prop)) {
res += prop + "%" + this[prop] + "~";
}
}
return res;
}
vcObj.prototype.fromText = function(line) {
var pairs = line.split('~');
var ct = pairs.length;
if (ct>1) {
for (var i=0; i<ct; ++i) {
var aPair = pairs[i].split("%");
if (aPair.length===2) {
this.setAttr(aPair[0],aPair[1]);
}
}
}
}
////////////////////////////////////////// the thing made of objs
////////////////////////////////////////// the thing made of objs
////////////////////////////////////////// the thing made of objs
////////////////////////////////////////// the thing made of objs
function vcDoc(context) {
this.cx = context;
this.cxw = this.cx.canvas.width;
this.cxh = this.cx.canvas.height;
this.cx.lineWidth =1;
this.idCounter = 2;
this.nodes = [];
this.loaded = 0;
}
vcDoc.prototype.edTileSet = function(img) {
this.tiles = img;
this.redraw();
}
vcDoc.prototype.newClear = function() {
this.nodes = [];
var tm = new vcObj();
tm.x = 100.0; tm.y = 100.0;
tm.isSelected = true;
tm.id = 1;
this.nodes.push(tm);
return "newClear";
}
vcDoc.prototype.redraw = function(mode) {
this.cx.fillStyle = "#888888";
this.cx.fillRect(0,0, this.cxw, this.cxh);
var len = this.nodes.length;
this.cx.strokeStyle = "#aaaaaa";
for (var i=0;i<len; ++i) {
var ot = this.findById(this.nodes[i].target);
if (ot!=-1) {
var x1 = this.nodes[i].x;
var x2 = this.nodes[ot].x;
var y1 = this.nodes[i].y;
var y2 = this.nodes[ot].y;
var dx = x2-x1;
var dy = y2-y1;
var ax = x1+(0.3*dx);
var ay = y1+(0.3*dy);
this.cx.beginPath();
this.cx.moveTo(x1,y1);
this.cx.lineTo(ax,ay);
this.cx.lineTo(ax+1,ay);
this.cx.lineTo(ax,ay+1);
this.cx.lineTo(ax,ay);
this.cx.lineTo(x2,y2);
this.cx.stroke();
}
}
for (var i=0;i<len; ++i) {
this.nodes[i].draw(this.cx, this.tiles);
}
if (mode ===3) {
this.cx.drawImage(this.tiles, 0,0, 512,512, 128,128, 512,512);
for (var i=0; i<17; ++i) {
this.cx.strokeStyle = "#000000aa";
this.cx.beginPath();
this.cx.moveTo(128+(i*32), 128);
this.cx.lineTo(128+(i*32), 640);
this.cx.moveTo(128, 128+(i*32));
this.cx.lineTo(640, 128+(i*32));
this.cx.stroke();
}
}
}
vcDoc.prototype.firstSelected = function() {
var len = this.nodes.length;
for (var i=0;i<len; ++i) {
if (this.nodes[i].isSelected) {
return i;
}
}
return -1;
}
vcDoc.prototype.findById = function(id) {
var len = this.nodes.length;
for (var i=0;i<len; ++i) {
if (this.nodes[i].id === id) {
return i;
}
}
return -1;
}
vcDoc.prototype.getInputsHTML = function() {
var s = this.firstSelected();
if (s>-1) {
return this.nodes[s].getAttribsHtml();
}
return "";
}
vcDoc.prototype.duplicateNode = function() {
var len = this.nodes.length;
for (var i=0;i<len; ++i) {
if (this.nodes[i].isSelected) {
var nn = new vcObj();
nn.copy(this.nodes[i]);
this.nodes[i].isSelected = false;
nn.x += 40.0;
nn.isSelected = true;
this.nodes.push(nn);
}
}
}
vcDoc.prototype.deleteNode = function() {
var len = this.nodes.length;
var j=0;
var newNodes = [];
for (var i=0;i<len; ++i) {
if (!this.nodes[i].isSelected) {
newNodes[j] = this.nodes[i];
++j;
}
}
this.nodes = newNodes;
}
vcDoc.prototype.edNew = function() {
this.newClear();
return "like new.";
}
vcDoc.prototype.edLoad = function(blob) {
var lines = blob.split('\n');
var nob = new vcObj();
var isFirst = true;
for (var i=0; i<lines.length; ++i) {
if (lines[i].length>2) {
if (isFirst) { isFirst = false; }
else {
this.nodes.push(nob);
nob = new vcObj();
}
nob.fromText(lines[i]);
this.nodes.push(nob);
}
}
}
vcDoc.prototype.edReport = function(pth) {
var blob = "";
var len=this.nodes.length;
for (var i=0; i<len; ++i) {
blob += this.nodes[i].toText() + "\n";
}
return blob;
}
vcDoc.prototype.setAttr = function(nID, atr, val) {
var s = this.findById(nID);
if (s>-1) {
this.nodes[s].setAttr(atr, val);
}
}
vcDoc.prototype.whichHit = function(cx,cy) {
var hitObject = -1;
var len = this.nodes.length;
for (var i=0; i<len; i=i+1) {
if (this.nodes[i].hitTest(cx,cy)) {
hitObject = i;
}
}
return hitObject;
}
vcDoc.prototype.clearSelection = function() {
var len = this.nodes.length;
for (var i=0; i<len; i=i+1) {
this.nodes[i].isSelected = false;
}
}
vcDoc.prototype.selectClick = function(cx,cy) {
this.clearSelection();
var h = this.whichHit(cx,cy);
if (h>-1) { this.nodes[h].isSelected = true; }
}
vcDoc.prototype.addClick = function(cx,cy) {
var tm = new vcObj();
var s = this.firstSelected();
if (s>-1) { tm.copy(this.nodes[s]); }
tm.x = cx; tm.y = cy;
tm.id = this.idCounter;
tm.isSelected = false;
this.idCounter = this.idCounter +1;
this.nodes.push(tm);
}
vcDoc.prototype.addDrag = function(cx,cy) {
var h = this.whichHit(cx,cy);
if (h>-1) {
// don't let drag make many in the same place.
var dx = this.nodes[h].x - cx;
var dy = this.nodes[h].y - cy;
if ((dx*dx)+(dy*dy)>3.0) {
this.addClick(cx, cy);
}
} else {
this.addClick(cx, cy);
}
}
vcDoc.prototype.textureClick = function(cx,cy) {
var h = this.firstSelected(cx,cy);
if (h>-1) {
var gx = cx - (cx%32.0);
var gy = cy - (cy%32.0);
this.nodes[h].tx = (gx-128)/32.0;
this.nodes[h].ty = (gy-128)/32.0;
}
}
vcDoc.prototype.move = function(cx,cy) {
var h = this.firstSelected(cx,cy);
if (h>-1) {
this.nodes[h].move(cx,cy);
}
}
vcDoc.prototype.deleteCommand = function() {
var newnds = [];
var len = this.nodes.length;
for (var i=0; i<len; i=i+1) {
if (!this.nodes[i].isSelected) {
newnds.push(this.nodes[i]);
}
}
this.nodes = newnds;
}
vcDoc.prototype.command = function(which) {
switch (which) {
case 0: this.deleteCommand(); break; // delete
case 1: break; // copy
case 2: break; // paste
}
}
vcDoc.prototype.select = function(which) {
var len = this.nodes.length;
for (var i=0; i<len; i=i+1) {
this.nodes[i].isSelected = (this.nodes[i].id===which);
}
}