-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
130 lines (115 loc) · 3.63 KB
/
util.js
File metadata and controls
130 lines (115 loc) · 3.63 KB
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
"use strict";
function makeOrthoMatrix(left, top, right, bottom)
{
var tx = - (right + left) / (right - left);
var ty = - (top + bottom) / (top - bottom);
return new Float32Array([
2 / (right - left), 0, 0,
0, 2 / (top - bottom), 0,
tx, ty, 0
]);
}
// Smoothstep.
function smoothStep(t, t1, t2, v1, v2) {
if (t < t1)
return v1;
else if (t < t2) {
var x = (t - t1) / (t2 - t1);
return v1 + (3 * x * x - 2 * x * x * x) * (v2 - v1);
} else
return v2;
}
// Simulates classical inheritance. Creates a new prototype by extending base object with new
// properties from "derived", and returns a reference to the "constructor" method.
function inherit(base, derived)
{
derived.constructor.prototype = Object.create(base.prototype);
derived.constructor.prototype.constructor = derived.constructor;
for (var k in derived) {
if (derived.hasOwnProperty(k))
derived.constructor.prototype[k] = derived[k];
}
return derived.constructor;
}
function getFileExtension(filename)
{
return filename.substr(filename.lastIndexOf('.') + 1);
}
function copyShallow(obj)
{
var ret = {};
for (var key in obj) {
if (obj.hasOwnProperty(key))
ret[key] = obj[key];
}
return ret;
}
// Creates a new "class" from a base class (first argument) and trait objects. Returns a function for
// initializing an object of the new type. Traits behave similar to mixins, i.e. the properties are added
// to the new class. If several traits have a method with the same name, then a new method is created
// that calls all of them in the order of their priority properties.
function extend(/*arguments*/)
{
var proto = Object.create(arguments[0].prototype);
// Deep copy the traits object.
proto._traitMethods = Object.create(null);
for (key in arguments[0].prototype._traitMethods)
proto._traitMethods[key] = arguments[0].prototype._traitMethods[key].slice(0);
// Add new traits.
for (var i = 1; i < arguments.length; ++i) {
if (!arguments[i])
throw new Error("Trait is undefined.");
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
if (typeof arguments[i][key] === "function") {
// Keep list of methods with same name instead of overwriting.
if (!proto._traitMethods[key])
proto._traitMethods[key] = [];
var len = proto._traitMethods[key].push(arguments[i][key]);
// Save priority in function object.
proto._traitMethods[key][len - 1]._priority = arguments[i].priority || 0;
} else {
// Copy/overwrite normal properties.
proto[key] = arguments[i][key];
}
}
}
}
// Add methods to the new prototype.
for (key in proto._traitMethods) {
// If there's only one method use it directly.
if (proto._traitMethods[key].length === 1) {
proto[key] = proto._traitMethods[key][0];
} else {
// Sort methods according to priority;
proto._traitMethods[key].sort(function(method1, method2) {
return method1._priority - method2._priority;
});
// Use a closure to create a new function that calls all the methods.
(function(methods) {
proto[key] = function() {
for (var i = 0; i < methods.length - 1; ++i)
methods[i].apply(this, arguments);
return methods[methods.length - 1].apply(this, arguments);
};
})(proto._traitMethods[key]);
}
}
// Create a wrapper for the init function that sets the prototype.
if (proto.init) {
proto.constructor = function(param) {
param = param || {};
param.__proto__ = proto;
proto.init.call(param);
return param;
};
} else {
proto.constructor = function(param) {
param = param || {};
param.__proto__ = proto;
return param;
};
}
proto.constructor.prototype = proto;
return proto.constructor;
}