forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.js
More file actions
148 lines (121 loc) · 3.36 KB
/
Copy pathTexture.js
File metadata and controls
148 lines (121 loc) · 3.36 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A Texture consists of a source, usually an Image from the Cache, or a Canvas, and a collection
* of Frames. The Frames represent the different areas of the Texture. For example a texture atlas
* may have many Frames, one for each element within the atlas. Where-as a single image would have
* just one frame, that encompasses the whole image.
*
* Textures are managed by the global TextureManager. This is a singleton class that is
* responsible for creating and delivering Textures and their corresponding Frames to Game Objects.
*
* Sprites and other Game Objects get the texture data they need from the TextureManager.
*
* @class Phaser.Texture
* @constructor
* @param {object} source
* @param {number} scaleMode
*/
Phaser.Texture = function (manager, key, source)
{
this.manager = manager;
if (!Array.isArray(source))
{
source = [ source ];
}
this.key = key;
/**
* The source that is used to create the texture.
* Usually an Image, but can also be a Canvas.
*
* @property source
* @type array
*/
this.source = [];
/**
* @property {object} frames - Frames
*/
this.frames = {};
this.frameTotal = 0;
// Load the Sources
for (var i = 0; i < source.length; i++)
{
this.source.push(new Phaser.TextureSource(this, source[i]));
}
};
Phaser.Texture.prototype.constructor = Phaser.Texture;
Phaser.Texture.prototype = {
add: function (name, sourceIndex, x, y, width, height)
{
var frame = new Phaser.TextureFrame(this, name, sourceIndex, x, y, width, height);
this.frames[name] = frame;
this.frameTotal++;
return frame;
},
get: function (name)
{
if (name === undefined || name === null || this.frameTotal === 1)
{
name = '__BASE';
}
var frame = this.frames[name];
if (!frame)
{
console.warn('No Texture.frame found with name ' + name);
}
else
{
return frame;
}
},
setTextureIndex: function (index)
{
for (var i = 0; i < this.source.length; i++)
{
this.source[i].glTextureIndex = index;
index++;
}
return index;
},
/**
* Destroys this base texture
*
* @method destroy
*/
destroy: function ()
{
// Need to iterate though the TextureSources, and unload each one
// then clear out the frames
/*
if (this.source)
{
Phaser.CanvasPool.removeByCanvas(this.source);
}
this.source = null;
*/
}
};
/**
* Helper function that creates a base texture from the given canvas element.
*
* @static
* @method fromCanvas
* @param canvas {Canvas} The canvas element source of the texture
* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}Phaser.scaleModes{{/crossLink}} for possible values
* @return {BaseTexture}
*/
Phaser.Texture.fromCanvas = function (canvas, scaleMode)
{
if (canvas.width === 0)
{
canvas.width = 1;
}
if (canvas.height === 0)
{
canvas.height = 1;
}
return new Phaser.Texture(canvas, scaleMode);
};