Created
August 28, 2014 18:38
-
-
Save lardratboy/ebfb6a6820b0ea09b0f4 to your computer and use it in GitHub Desktop.
Phaser flickr sprite helpers (work in progress)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author Brad P. Taylor (bradptaylor+github@gmail.com) license MIT | |
///<reference path="../../../../bower_components/phaser-official/build/phaser.d.ts"/> | |
///<reference path="./prefab.ts"/> | |
///<reference path="./basic.ts"/> | |
///<reference path="../libs/collections.ts"/> | |
///<reference path="../../DefinitelyTyped/async/async.d.ts"/> | |
///<reference path="../../DefinitelyTyped/lodash/lodash.d.ts"/> | |
module bpt { | |
//https://www.flickr.com/about/goodies/ | |
//https://www.flickr.com/services/api/misc.urls.html | |
//https://www.flickr.com/services/api/flickr.photos.search.html | |
//https://www.flickr.com/services/api/flickr.photos.licenses.getInfo.html | |
//https://www.flickr.com/services/developer/attributions/ | |
export module Flickr { | |
export var api_key = "yourflickrapikey"; | |
export var license_no_known_copyright = "No known copyright restrictions"; | |
export var license_attribution = "Attribution License"; | |
export var keys:string[] = []; | |
export var info:any = {}; | |
export var splitTagsCache_ = {}; | |
export var byTags_ = {}; | |
export function searchFor( game, names:string[], tags:string, tagMode = 'any', howMany:number = 1, sizeSufix:string = "n", assetPrefix = "Flickr", license = license_no_known_copyright ) { | |
var splitTags = Flickr.splitTagsCache_[tags]; | |
if ( undefined === splitTags ) { | |
splitTags = tags.split(','); | |
Flickr.splitTagsCache_[ tags ] = splitTags; | |
} | |
var defered = $.Deferred(); | |
$.getJSON( | |
"https://api.flickr.com/services/rest/?jsoncallback=?" | |
, { | |
method: "flickr.photos.search", | |
tags: tags, | |
tag_mode: tagMode, | |
format: "json", | |
safe_search: true, | |
api_key: Flickr.api_key, | |
crossDomain: true, | |
license: license, | |
per_page: howMany | |
} | |
).then((d) => { | |
if ("ok" === d.stat) { | |
async.forEachLimit(d.photos.photo, 5, (item:any, callback) => { | |
var src = "https://farm" + item.farm + ".static.flickr.com/" + item.server + "/" + item.id + "_" + item.secret + "_" + sizeSufix + ".jpg"; | |
var img:HTMLImageElement = new Image(); | |
img.crossOrigin = ''; | |
img.src = src; | |
var key = assetPrefix + item.id; | |
game.cache.addImage(key, img.src, img); | |
Flickr.info[key] = item; | |
Flickr.keys.push( key ); | |
names.push( key ); | |
for ( var t in splitTags ) { | |
var tag = splitTags[t]; | |
Flickr.byTags_[tag] = Flickr.byTags_[tag] || []; | |
Flickr.byTags_[tag].push( key ); | |
} | |
callback(); | |
}, ( err ) => { | |
if (err) { | |
defered.reject( err ); | |
} else { | |
defered.resolve( names ); | |
} | |
}); | |
} | |
}, (e) => { | |
defered.reject( JSON.stringify(e) ); | |
}); | |
return defered.promise(); | |
} | |
// TODO - make search by tags | |
} | |
export class FlickrSprite extends SpritePrefab { | |
config():any { | |
return [ super.config(), 'FlickrSprite.json' ]; | |
} | |
constructor(game, public search = "phaser", x?, y?) { | |
super(game, x, y ); | |
var names:string[] = []; | |
Flickr.searchFor( game, names, this.search ).then( () => { | |
this.loadTexture( names[0], undefined ); | |
// this should be a tooltip like object | |
bpt.prefab.addOverlayAsChild( this, 'flickr.png', 1, 1 ); | |
}, () => { | |
throw "FlickrSprite failure"; | |
}); | |
} | |
} | |
if (bpt && bpt.prefab.Factory) { | |
bpt.prefab.Factory.add('bpt.FlickrSprite', FlickrSprite, | |
[ ["search"], ["x"], ["y"] ], | |
bpt.prefab.helper_transformed_named_args_to_config); | |
bpt.prefab.Factory.addAlias('FlickrSprite', 'bpt.FlickrSprite'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This Sprite was designed to work with bpt_prefab.ts ( gist https://gist.github.com/lardratboy/aa0f25e6741929b1b33f ) and the base type is provided by ( gist https://gist.github.com/lardratboy/437007799a573541226a )