- Next »
- « Previous
SecondLife x Voxのスライドショーのコード公開
Mashup Awardで受賞したSecondLife x Voxのコードを公開します。
WebサービスとSecondLifeをマッシュアップする際のコードの例として参考になるのではないかと思います。ジュークボックスのほうについては殆ど同じコードなので、特に公開はしませんが、要望があれば公開します。
Webサービスとマッシュアップするときの肝は、
- llHttpRequestによるリクエストの送信
リクエストを送信した後は、サーバーサイドで他サービスへの問い合わせを行い何らかの処理をして、http_responseメソッドで受信し処理をするだけです。
SecondLifeとWebサービスをマッシュアップする場合、LSLのpoorさから殆どこのような形になるかと思います。
では、以下コードです。それでは、皆さんもSecondLifeでのHackを楽しんでみてください。
Happy Hacking with pleasure!
//----------------------------------------------
// Constants/Global variables
//----------------------------------------------
// global variables
key requestId;
list photoUrls = [];
integer currentPhoto=0;// Constants
key VIDEO_DEFAULT = "6e0f05ad-1809-4edc-df29-fae3d2a6c9b8";
float PHOTO_TIMER_PERIOD = 5.0;
string API_BASE = "http://mashup4u.net/v2r/search_photo";
string DEFAULT_TAG = "dogs";
string DEFAULT_PHOTO_URL = "http://aka-static.vox.com/.shared:v21.4:vox:en_us/themes/minimalist-grey/logo.gif";//----------------------------------------------
// Functions
//----------------------------------------------
string apiUrl(string tagName)
{
string url = API_BASE + "?tag=" + tagName;
return url;
}setUrl(string url)
{
key video_texture = llList2Key(llParcelMediaQuery( [PARCEL_MEDIA_COMMAND_TEXTURE]), 0);
if(video_texture == NULL_KEY)
{
video_texture = VIDEO_DEFAULT;
llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_TEXTURE, VIDEO_DEFAULT]);
}
llSetTexture(video_texture,ALL_SIDES);
llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_URL,url]);
llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_PLAY]);
llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_AUTO_ALIGN,TRUE]);
}showPhoto(string url)
{
setUrl(url);
}startSliedShow(string tagName)
{
string apiUrl = apiUrl(llEscapeURL(tagName));
llSetText("Now showing photos tagged with " + tagName,<255,255,255>,1);
requestId = llHTTPRequest(apiUrl,[HTTP_METHOD,"GET"],"");
}stopSlideShow()
{
llSetTimerEvent(0);
initializeVariables();
showPhoto(DEFAULT_PHOTO_URL);
}initializeVariables()
{
currentPhoto=0;
photoUrls = [];
llSetText("Vox Slideshow",<255,255,255>,1);
}help()
{
llWhisper(0,"Type '/1 tag' to tell me what tag to search for on vox.");
llWhisper(0,"Touch the Vox SlideShow to stop slide show");
}//----------------------------------------------
// Main
//----------------------------------------------
default
{
state_entry()
{
llSetText("Vox SlideShow",<255,255,255>,1);
showPhoto(DEFAULT_PHOTO_URL);
llListen(1,"",NULL_KEY,"");
}
listen(integer channel, string name, key id, string message)
{
if(message == "") {
help();} else {
llSay(0,"Searching photos tagged with "+message + " ...");
initializeVariables();
startSliedShow(message);
}
}touch_start(integer nd)
{
help();
stopSlideShow();
//startSliedShow(DEFAULT_TAG);
}timer()
{
string photoUrl = llList2String(photoUrls, currentPhoto);
showPhoto(photoUrl);
currentPhoto = currentPhoto + 1;
integer photoNums = llGetListLength(photoUrls);
if(photoNums == 0) {
return;
}
if (currentPhoto > photoNums - (integer)1)
{
currentPhoto = 0;
}
// for loop
llSetTimerEvent(PHOTO_TIMER_PERIOD);
}http_response(key request_id, integer status, list metadata, string body)
{
if (request_id == requestId)
{
if(status == 200) {
photoUrls = llParseString2List(body,["|"],[]);
integer photoNums = llGetListLength(photoUrls);
if(photoNums == 0) {
llSay(0,"Found no photos. Please search with another tags.");
initializeVariables();
showPhoto(DEFAULT_PHOTO_URL);
return;
}
llSetTimerEvent(0.01);
}
}}
}
Comments
SL内でも是非フレンドシップお願いしますー