forked from DDR0/editabled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditabled.js
96 lines (84 loc) · 3 KB
/
Editabled.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
/*global _, $, jQuery, console*/
//Loads the external scripts for the editor, and draws a little 'loading' text.
var c = console; //A shortcut, I'm tired of typing 'console.log' every damn time.
var editors = []; //The global 'library', how components talk to eachother.
window.loadEditabled = function() {
"use strict";
var loadOtherScriptsCount = 0;
var baseURL = "Editabled/";
var jsAr = [
{url: "Static Utilities.js", type: "script"},
{url: "Pixel Store.js", type: "canvas-worker", name: "pxStore"},
{url: "Utilities.js", type: "script"},
{url: "Setup.js", type: "script"},
{url: "User Interface.js", type: "script"},
{url: "Event Listener.js", type: "script"},
{url: "Tests.js", type: "script"},
];
var jsArRecieved = [];
var launchGame;
launchGame = function() {
jsArRecieved.map(function(data, index) {
var js = jsAr[index];
if(js.type === 'script') {
jQuery.globalEval(data);
} else if(js.type === 'canvas-worker') {
editors.map(function(index) {
var editor = editors[index];
editor.edLib[js.name] = new Worker(baseURL + js.url);
});
} else {
throw "script type '"+js.type+"' invalid";
}
});
};
var continueIfLoaded = function() {
loadOtherScriptsCount += 1;
if(loadOtherScriptsCount === jsAr.length) {
launchGame();
}
};
var failToLoad = _.once(function(err) {
c.warn('AJAX error ' + err.status + ": " + err.statusText + ".\n" + err.responseText);
window.alert('Well, bother. An error occured while downloading the editor. Check that we\'re still connected to the internet, then try again. If that didn\'t work, then we\'re hooped. Sorry.');
});
window.setTimeout(function() { //The 50ms delay is required, here. Sticking the ajax in the DOMContentLoaded event, after the drawing of the text, doesn't help one iota in chrome. Works in Firefox, though.
jsAr.map(function(js, index) {
js = js.url;
//c.log('loader: fetching ' + js);
$.ajax({
async: false,
type: "GET",
url: baseURL + js,
dataType: 'text',
error: failToLoad,
success: function(data){
jsArRecieved[index] = data;
continueIfLoaded();
},
});
});
}, 50);
window.addEventListener('DOMContentLoaded', function() {
//Draw 'loading' text.
editors = $(".image-editor");
editors.map(function(index) {
var editor = editors[index];
editor.width = $(editor).width();
editor.height = $(editor).height();
editor.edLib = {};
var ctx = editor.getContext('2d'); //ctx = ConTeXt.
ctx.strokeStyle = '#000';
ctx.fillStyle = '#FFF';
ctx.lineWidth = 2;
var textHeight = 15;
ctx.font = textHeight + "pt Fixedsys";
var text = "Loading Editabled";
var textWidth = ctx.measureText(text).width;
ctx.strokeText(text, editor.width/2 - textWidth/2, editor.height/2 + textHeight/4);
ctx.fillText( text, editor.width/2 - textWidth/2, editor.height/2 + textHeight/4);
});
});
return true;
}();
delete window.loadEditabled;