-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphics.js
executable file
·419 lines (328 loc) · 9.33 KB
/
Graphics.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// Each Window (gWIN) has the following internal properties
//
// 0 Window ID
// 1 X Position on screen
// 2 Y Position on Screen
// 3 Width
// 4 Height
// 5 Visibility
// 6 Colour Depth
// 7 Whether the Window is a bitmap
// 8 gPosX
// 9 gPosY
// 10 Canvas
// 11 Render Context
// 12 gFont
// 13 gStyle
// Note: Canvas line widths are done from 0.5 so co-ords are adjusted
var Renderer = {
// Graphical Window Stack
comCanvas: null,
comContext: null,
comBBCanvas: null,
comBBContext: null,
Fonts: ['8px Arial','Bold 8px Arial','8px Courier New','8px Courier New','8px Times New Roman','11px Times New Roman','13px Times New Roman','15px Times New Roman','8px Arial','11px Arial','13px Arial','15px Arial','6px Courier New'],
Width: 480,
Height: 160,
RenderBorder: true,
gWIN: [],
AWnd: 0,
ActiveID: 0,
UpdateMode: 1,
changed: false,
// Property functions
gX:function(){return this.AWnd[8];},
gY:function(){return this.AWnd[9];},
gORIGINX:function(){return this.AWnd[1];},
gORIGINY:function(){return this.AWnd[2];},
gWIDTH:function(){return this.AWnd[3];},
gHEIGHT:function(){return this.AWnd[4];},
gIDENTITY:function(){return this.ActiveID;},
// Public functions
gVISIBLE:function(v){this.AWnd[5]=v;},
gAT:function(x,y){this.AWnd[8]=x;this.AWnd[9]=y;},
gMOVE:function(x,y){this.AWnd[8]+=x;this.AWnd[9]+=y;},
gLINETO:function(x,y){this.gLINEBY(x-this.gX(),y-this.gY());},
gFONT:function(f){this.AWnd[12]=f;this.SetFont();},
gSTYLE:function(s){this.AWnd[13]=s;this.SetFont();},
gTWIDTH:function(t){return this.AWnd[11].measureText(t).width;},
}
// Graphical Layer
Renderer.gCREATE = function(x, y, width, height, visible, flags) {
// Get an available Window ID
var winID = -1;
for(var i = 1; i < 65; i++) {
var found = false;
for (var j=0; j < this.gWIN.length; j++) {
if (this.gWIN[j][0]==i) {
found = true;
break;
}
}
if (!found) {
winID = i;
break;
}
}
// Create a window object.
this.gWIN.unshift([winID, x, y, width, height, visible, flags, 0, 0, 0, document.createElement('canvas'), null, 1, 0]);
document.write('Creating Window: ID: ' + winID + ', X: ' + x + ', Y: ' + y + ', Height: ' + height + ', Visibility: ' + visible + '<br>');
this.ActiveID = winID;
this.AWnd = this.gWIN[0];
this.AWnd[10].width = width;
this.AWnd[10].height = height;
// Add the render context
this.AWnd[11] = this.AWnd[10].getContext('2d');
this.SetFont();
this.gCLS();
return winID;
}
Renderer.Init = function() {
wO('');
// Create the Foreground buffer
this.comCanvas = document.createElement('canvas');
this.comCanvas.width = this.Width;
this.comCanvas.height = this.Height;
this.comContext = this.comCanvas.getContext('2d');
// Create the backing buffer
this.comBBCanvas = document.createElement('canvas');
this.comBBCanvas.width = this.Width;
this.comBBCanvas.height = this.Height;
this.comBBContext = this.comBBCanvas.getContext('2d');
// Clear
this.comBBContext.fillStyle="#FF0000";
this.comBBContext.fillRect(0,0,this.comBBCanvas.width,this.comBBCanvas.height);
document.body.appendChild(this.comCanvas);
wO('');
}
Renderer.Composite = function(force) {
if (force || (this.changed && this.UpdateMode == 1)) {
// Backtrack through the screens, laying out those that are visible to the back buffer.
for(var i = this.gWIN.length - 1; i>=0; i--) {
if (this.gWIN[i][5] == 1) {
this.comBBContext.drawImage(this.gWIN[i][10], this.gWIN[i][1], this.gWIN[i][2]);
}
}
// Reset the flag as the backbuffer has been updated
this.changed = false;
}
// Render to foreground buffer
this.comContext.drawImage(this.comBBCanvas, 0, 0);
// Draw border
if (this.RenderBorder) {
this.comContext.strokeStyle="#FF0000";
this.comContext.strokeRect(0,0,this.comCanvas.width,this.comCanvas.height);
}
}
// Return the Index in gWIN for a particular ID.
Renderer.GetIndexForID = function(id) {
for(var i = 0; i< this.gWIN.length; i++) {
if (this.gWIN[i][0] == id) {
return i;
}
}
return -1;
}
Renderer.gUPDATE = function(m) {
if (m == 0 || m == 1) {
this.UpdateMode = m;
} else {
this.Composite(true);
}
}
Renderer.gCLS = function() {
// Clear the screen
var oFill = this.AWnd[11].fillStyle;
this.AWnd[11].fillStyle="#FFFFFF";
this.AWnd[11].fillRect(0,0,this.gWIDTH(),this.gHEIGHT());
// Restore fill style
this.AWnd[11].fillStyle = oFill;
this.changed = true;
}
Renderer.gLINEBY = function(x, y) {
// Draws a line from the current position to the end offset
this.AWnd[11].moveTo(this.gX() + 0.5,this.gY() + 0.5);
this.AWnd[11].lineTo(x,y);
this.gMOVE(x, y);
this.changed = true;
}
Renderer.gFILL = function(width, height, fillMode) {
// Clear the screen
var oFill = this.AWnd[11].fillStyle;
// Restore fill style
this.AWnd[11].fillStyle = oFill;
if (fillMode == 0) {
// Pixels are set
this.AWnd[11].fillStyle="#000000";
this.AWnd[11].fillRect(this.gX(),this.gY(), width, height);
} else if (fillMode == 1) {
// Pixels are cleared
this.AWnd[11].fillStyle="#FFFFFF";
this.AWnd[11].fillRect(this.gX(),this.gY(), width, height);
} else {
// Pixels are inverted
}
// Restore fill style
this.AWnd[11].fillStyle = oFill;
this.changed = true;
}
Renderer.gBOX = function(w, h) {
this.AWnd[11].strokeRect(this.gX() + 0.5, this.gY() + 0.5, w, h);
this.changed = true;
}
// Creates the new font based on the gFONT and gSTYLE values
Renderer.SetFont = function() {
var fontName = '';
// Set the value based on gSTYLE
var gFONTStyle = this.AWnd[13];
if (gFONTStyle >= 32) {
fontName += 'italic ';
gFONTStyle-=32;
}
if (gFONTStyle >= 16) {
// TODO: Monospaced - cant do
fontName += '';
gFONTStyle-=16;
}
if (gFONTStyle >= 8) {
// TODO: Double Height - cant do
fontName += '';
gFONTStyle-=8;
}
if (gFONTStyle >= 4) {
// TODO: Inverse - cant do
fontName += '';
gFONTStyle-=4;
}
if (gFONTStyle >= 2) {
// TODO: Underlined - cant do
fontName += '';
gFONTStyle-=2;
}
if (gFONTStyle == 1) {
fontName += 'bold';
}
var gFID = this.AWnd[12];
if (gFID < 4 || gFID == 13) {
fontName = '';
} else if (gFID > 13) {
gFID = 6;
}
fontName += this.Fonts[gFID];
this.AWnd[11].font = fontName;
}
Renderer.gPRINT = function(text) {
// WORKAROUND: Offset text upwards slightly
this.AWnd[11].fillText(text, this.gX(), this.gY() - 2);
var txtwidth = this.AWnd[11].measureText(text).width;
// Move the draw position once finished writing, no new line
this.gMOVE(txtwidth, 0);
this.changed = true;
}
Renderer.gPRINTCLIP = function(text, maxWidth) {
var allowedChars = 0;
// Work out the maximum number of letters that can fit in maxWidth
for (var i = 0; i < text.length; i++) {
if (this.gTWIDTH(text.substring(0, i)) <= maxWidth) {
allowedChars = i;
} else {
break;
}
}
this.gPRINT(text.substring(0, allowedChars));
}
Renderer.gBORDER = function(args) {
}
Renderer.gCLOSE = function(i) {
if (i == 1 || this.GetIndexForID(i) == -1) {
wO('ERROR: gCLOSE request out of bounds');
return;
}
if (this.gWIN.length == 1) {
wO('ERROR: gCLOSE cannot close all available graphics windows');
return;
}
// Remove the Wnd
this.gWIN.splice(this.GetIndexForID(i), 1);
if (this.ActiveID == i)
{
// Reset to Wnd ID 1 as the current drawable is being closed
this.ActiveID = 1;
this.AWnd = this.gWIN[this.GetIndexForID(1)];
}
this.changed = true;
}
Renderer.gUSE = function(i) {
var gID = this.GetIndexForID(i);
if (gID == -1) {
wO('ERROR: gUSE request out of bounds');
return;
}
this.ActiveID = i;
this.AWnd = this.gWIN[gID];
}
// Usage: gCOPY id%, x%, y%, width%, height%, mode%
Renderer.gCOPY = function(i, x, y, w, h, m) {
var gID = this.GetIndexForID(i);
if (gID == -1) {
wO('ERROR: gCOPY request out of bounds');
return;
}
// Copy area from id% to current drawable depending on mode
if (m == 3)
{
// Pure copy
this.AWnd[11].drawImage(Renderer.gWIN[gID][10], x, y, w, h, this.gX(), this.gY(), w, h);
}
else if (m == 0)
{
// Set
wO('TODO!');
}
else if (m == 1)
{
// Clear
wO('TODO!');
}
else if (m == 2)
{
// Invert
wO('TODO!');
}
}
// Usage: gORDER id%, position%
Renderer.gORDER = function(i, p) {
var gID = Renderer.GetIndexForID(i);
if (i < 0 || gOD == -1 || p < 1) {
wO('ERROR: gORDER request out of bounds');
return;
}
if (Renderer.gWIN[gID][7] == 1) {
wO('ERROR: Can not order Bitmap');
return;
}
if (p == i) {
// The new and old locations are the same position
return;
}
// Extract the window to be reordered
var gWINrom = Renderer.gWIN.splice(gID - 1 ,1);
var normP = p;
if (p >= Renderer.gWIN.length) {
// Push the new entry to the end of the list
Renderer.gWIN.push(gWINrom[0]);
} else if (p < i){
// New entry is infront of the removed
Renderer.gWIN.splice(p - 1, 0, gWINrom[0]);
} else {
// New entry is behind the removed
Renderer.gWIN.splice(p - 2, 0, gWINrom[0]);
}
// Update the active ID if it was the window that changed.
if (Renderer.ActiveID == i) {
this.ActiveID == p;
this.AWnd = this.gWIN[this.GetIndexForID(p)];
}
this.changed = true;
}