Skip to content

Commit 9ee0de9

Browse files
committed
Cache.addVideo allows you to add a loaded video into the Phaser Cache. This is called automatically by the Phaser Loader, but may be invoked directly as well.
Cache.checkVideoKey allows you to check if a video is stored in the cache based on the given key. Cache.getVideo allows you to extract a video from the Cache based on its key. The video element itself (or the Blob is loaded with asBlob true) will be found in the `data` property of the returned object. Cache.removeVideo will remove a video from the Cache based on the given key.
1 parent 77468e7 commit 9ee0de9

1 file changed

Lines changed: 196 additions & 117 deletions

File tree

src/loader/Cache.js

Lines changed: 196 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ Phaser.Cache = function (game) {
4848
*/
4949
this._sounds = {};
5050

51+
/**
52+
* @property {object} _videos - Video key-value container.
53+
* @private
54+
*/
55+
this._videos = {};
56+
5157
/**
5258
* @property {object} _text - Text key-value container.
5359
* @private
@@ -131,6 +137,7 @@ Phaser.Cache = function (game) {
131137
this._cacheMap[Phaser.Cache.CANVAS] = this._canvases;
132138
this._cacheMap[Phaser.Cache.IMAGE] = this._images;
133139
this._cacheMap[Phaser.Cache.TEXTURE] = this._textures;
140+
this._cacheMap[Phaser.Cache.VIDEO] = this._videos;
134141
this._cacheMap[Phaser.Cache.SOUND] = this._sounds;
135142
this._cacheMap[Phaser.Cache.TEXT] = this._text;
136143
this._cacheMap[Phaser.Cache.PHYSICS] = this._physics;
@@ -215,6 +222,12 @@ Phaser.Cache.JSON = 11;
215222
*/
216223
Phaser.Cache.XML = 12;
217224

225+
/**
226+
* @constant
227+
* @type {number}
228+
*/
229+
Phaser.Cache.VIDEO = 13;
230+
218231
Phaser.Cache.prototype = {
219232

220233
/**
@@ -624,127 +637,19 @@ Phaser.Cache.prototype = {
624637
},
625638

626639
/**
627-
* Get a canvas object from the cache by its key.
628-
*
629-
* @method Phaser.Cache#getCanvas
630-
* @param {string} key - Asset key of the canvas to retrieve from the Cache.
631-
* @return {object} The canvas object.
632-
*/
633-
getCanvas: function (key) {
634-
635-
if (this._canvases[key])
636-
{
637-
return this._canvases[key].canvas;
638-
}
639-
else
640-
{
641-
console.warn('Phaser.Cache.getCanvas: Invalid key: "' + key + '"');
642-
return null;
643-
}
644-
645-
},
646-
647-
/**
648-
* Get a BitmapData object from the cache by its key.
649-
*
650-
* @method Phaser.Cache#getBitmapData
651-
* @param {string} key - Asset key of the BitmapData object to retrieve from the Cache.
652-
* @return {Phaser.BitmapData} The requested BitmapData object if found, or null if not.
653-
*/
654-
getBitmapData: function (key) {
655-
656-
if (this._bitmapDatas[key])
657-
{
658-
return this._bitmapDatas[key].data;
659-
}
660-
else
661-
{
662-
console.warn('Phaser.Cache.getBitmapData: Invalid key: "' + key + '"');
663-
return null;
664-
}
665-
666-
},
667-
668-
/**
669-
* Get a BitmapFont object from the cache by its key.
670-
*
671-
* @method Phaser.Cache#getBitmapFont
672-
* @param {string} key - Asset key of the BitmapFont object to retrieve from the Cache.
673-
* @return {Phaser.BitmapFont} The requested BitmapFont object if found, or null if not.
674-
*/
675-
getBitmapFont: function (key) {
676-
677-
if (this._bitmapFont[key])
678-
{
679-
return this._bitmapFont[key];
680-
}
681-
else
682-
{
683-
console.warn('Phaser.Cache.getBitmapFont: Invalid key: "' + key + '"');
684-
return null;
685-
}
686-
687-
},
688-
689-
/**
690-
* Get a physics data object from the cache by its key. You can get either the entire data set, a single object or a single fixture of an object from it.
640+
* Adds a Video file into the Cache. The file must have already been loaded, typically via Phaser.Loader.
691641
*
692-
* @method Phaser.Cache#getPhysicsData
693-
* @param {string} key - Asset key of the physics data object to retrieve from the Cache.
694-
* @param {string} [object=null] - If specified it will return just the physics object that is part of the given key, if null it will return them all.
695-
* @param {string} fixtureKey - Fixture key of fixture inside an object. This key can be set per fixture with the Phaser Exporter.
696-
* @return {object} The requested physics object data if found.
642+
* @method Phaser.Cache#addVideo
643+
* @param {string} key - Asset key for the video.
644+
* @param {string} url - URL of this video file.
645+
* @param {object} data - Extra video data.
646+
* @param {boolean} isBlob - True if the file was preloaded via xhr and the data parameter is a Blob. false if a Video tag was created instead.
697647
*/
698-
getPhysicsData: function (key, object, fixtureKey) {
699-
700-
if (typeof object === 'undefined' || object === null)
701-
{
702-
// Get 'em all
703-
if (this._physics[key])
704-
{
705-
return this._physics[key].data;
706-
}
707-
else
708-
{
709-
console.warn('Phaser.Cache.getPhysicsData: Invalid key: "' + key + '"');
710-
}
711-
}
712-
else
713-
{
714-
if (this._physics[key] && this._physics[key].data[object])
715-
{
716-
var fixtures = this._physics[key].data[object];
717-
718-
//try to find a fixture by it's fixture key if given
719-
if (fixtures && fixtureKey)
720-
{
721-
for (var fixture in fixtures)
722-
{
723-
// This contains the fixture data of a polygon or a circle
724-
fixture = fixtures[fixture];
648+
addVideo: function (key, url, data, isBlob) {
725649

726-
// Test the key
727-
if (fixture.fixtureKey === fixtureKey)
728-
{
729-
return fixture;
730-
}
731-
}
650+
this._videos[key] = { url: url, data: data, isBlob: isBlob, locked: true };
732651

733-
// We did not find the requested fixture
734-
console.warn('Phaser.Cache.getPhysicsData: Could not find given fixtureKey: "' + fixtureKey + ' in ' + key + '"');
735-
}
736-
else
737-
{
738-
return fixtures;
739-
}
740-
}
741-
else
742-
{
743-
console.warn('Phaser.Cache.getPhysicsData: Invalid key/object: "' + key + ' / ' + object + '"');
744-
}
745-
}
746-
747-
return null;
652+
this._resolveURL(url, this._videos[key]);
748653

749654
},
750655

@@ -819,6 +724,19 @@ Phaser.Cache.prototype = {
819724

820725
},
821726

727+
/**
728+
* Checks if the given key exists in the Video Cache.
729+
*
730+
* @method Phaser.Cache#checkVideoKey
731+
* @param {string} key - Asset key of the video file to check is in the Cache.
732+
* @return {boolean} True if the key exists, otherwise false.
733+
*/
734+
checkVideoKey: function (key) {
735+
736+
return this.checkKey(Phaser.Cache.VIDEO, key);
737+
738+
},
739+
822740
/**
823741
* Checks if the given key exists in the Text Cache.
824742
*
@@ -944,6 +862,131 @@ Phaser.Cache.prototype = {
944862

945863
},
946864

865+
/**
866+
* Get a canvas object from the cache by its key.
867+
*
868+
* @method Phaser.Cache#getCanvas
869+
* @param {string} key - Asset key of the canvas to retrieve from the Cache.
870+
* @return {object} The canvas object.
871+
*/
872+
getCanvas: function (key) {
873+
874+
if (this._canvases[key])
875+
{
876+
return this._canvases[key].canvas;
877+
}
878+
else
879+
{
880+
console.warn('Phaser.Cache.getCanvas: Invalid key: "' + key + '"');
881+
return null;
882+
}
883+
884+
},
885+
886+
/**
887+
* Get a BitmapData object from the cache by its key.
888+
*
889+
* @method Phaser.Cache#getBitmapData
890+
* @param {string} key - Asset key of the BitmapData object to retrieve from the Cache.
891+
* @return {Phaser.BitmapData} The requested BitmapData object if found, or null if not.
892+
*/
893+
getBitmapData: function (key) {
894+
895+
if (this._bitmapDatas[key])
896+
{
897+
return this._bitmapDatas[key].data;
898+
}
899+
else
900+
{
901+
console.warn('Phaser.Cache.getBitmapData: Invalid key: "' + key + '"');
902+
return null;
903+
}
904+
905+
},
906+
907+
/**
908+
* Get a BitmapFont object from the cache by its key.
909+
*
910+
* @method Phaser.Cache#getBitmapFont
911+
* @param {string} key - Asset key of the BitmapFont object to retrieve from the Cache.
912+
* @return {Phaser.BitmapFont} The requested BitmapFont object if found, or null if not.
913+
*/
914+
getBitmapFont: function (key) {
915+
916+
if (this._bitmapFont[key])
917+
{
918+
return this._bitmapFont[key];
919+
}
920+
else
921+
{
922+
console.warn('Phaser.Cache.getBitmapFont: Invalid key: "' + key + '"');
923+
return null;
924+
}
925+
926+
},
927+
928+
/**
929+
* Get a physics data object from the cache by its key. You can get either the entire data set, a single object or a single fixture of an object from it.
930+
*
931+
* @method Phaser.Cache#getPhysicsData
932+
* @param {string} key - Asset key of the physics data object to retrieve from the Cache.
933+
* @param {string} [object=null] - If specified it will return just the physics object that is part of the given key, if null it will return them all.
934+
* @param {string} fixtureKey - Fixture key of fixture inside an object. This key can be set per fixture with the Phaser Exporter.
935+
* @return {object} The requested physics object data if found.
936+
*/
937+
getPhysicsData: function (key, object, fixtureKey) {
938+
939+
if (typeof object === 'undefined' || object === null)
940+
{
941+
// Get 'em all
942+
if (this._physics[key])
943+
{
944+
return this._physics[key].data;
945+
}
946+
else
947+
{
948+
console.warn('Phaser.Cache.getPhysicsData: Invalid key: "' + key + '"');
949+
}
950+
}
951+
else
952+
{
953+
if (this._physics[key] && this._physics[key].data[object])
954+
{
955+
var fixtures = this._physics[key].data[object];
956+
957+
//try to find a fixture by it's fixture key if given
958+
if (fixtures && fixtureKey)
959+
{
960+
for (var fixture in fixtures)
961+
{
962+
// This contains the fixture data of a polygon or a circle
963+
fixture = fixtures[fixture];
964+
965+
// Test the key
966+
if (fixture.fixtureKey === fixtureKey)
967+
{
968+
return fixture;
969+
}
970+
}
971+
972+
// We did not find the requested fixture
973+
console.warn('Phaser.Cache.getPhysicsData: Could not find given fixtureKey: "' + fixtureKey + ' in ' + key + '"');
974+
}
975+
else
976+
{
977+
return fixtures;
978+
}
979+
}
980+
else
981+
{
982+
console.warn('Phaser.Cache.getPhysicsData: Invalid key/object: "' + key + ' / ' + object + '"');
983+
}
984+
}
985+
986+
return null;
987+
988+
},
989+
947990
/**
948991
* Gets an image by its key. Note that this returns a DOM Image object, not a Phaser object.
949992
*
@@ -1224,6 +1267,27 @@ Phaser.Cache.prototype = {
12241267

12251268
},
12261269

1270+
/**
1271+
* Get video by key.
1272+
*
1273+
* @method Phaser.Cache#getVideo
1274+
* @param {string} key - Asset key of the video to retrieve from the Cache.
1275+
* @return {Phaser.Sound} The video object.
1276+
*/
1277+
getVideo: function (key) {
1278+
1279+
if (this._videos[key])
1280+
{
1281+
return this._videos[key];
1282+
}
1283+
else
1284+
{
1285+
console.warn('Phaser.Cache.getVideo: Invalid key: "' + key + '"');
1286+
return null;
1287+
}
1288+
1289+
},
1290+
12271291
/**
12281292
* Get the number of frames in this image.
12291293
*
@@ -1397,6 +1461,10 @@ Phaser.Cache.prototype = {
13971461
array = this._sounds;
13981462
break;
13991463

1464+
case Phaser.Cache.VIDEO:
1465+
array = this._videos;
1466+
break;
1467+
14001468
case Phaser.Cache.TEXT:
14011469
array = this._text;
14021470
break;
@@ -1489,6 +1557,16 @@ Phaser.Cache.prototype = {
14891557
delete this._sounds[key];
14901558
},
14911559

1560+
/**
1561+
* Removes a video from the cache.
1562+
*
1563+
* @method Phaser.Cache#removeVideo
1564+
* @param {string} key - Key of the asset you want to remove.
1565+
*/
1566+
removeVideo: function (key) {
1567+
delete this._videos[key];
1568+
},
1569+
14921570
/**
14931571
* Removes a text from the cache.
14941572
*
@@ -1621,6 +1699,7 @@ Phaser.Cache.prototype = {
16211699
var containers = [
16221700
this._canvases,
16231701
this._sounds,
1702+
this._videos,
16241703
this._text,
16251704
this._json,
16261705
this._xml,

0 commit comments

Comments
 (0)