sloodle_mod_presenter-shared-media-2.0.lslp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle Presenter (for Sloodle 0.4.1)
  4. // Lets the educator display a presentation of images. videos and webpages hosted on the web.
  5. // Part of the Sloodle project (www.sloodle.org)
  6. //
  7. // Copyright (c) 2008-9 Sloodle project contributors
  8. // Released under the GNU GPL
  9. //
  10. // Contributors:
  11. // Peter R. Bloomfield
  12. // Paul G. Preibisch (Fire Centaur)
  13. //
  14. integer SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST=-1828374651; // this channel is used to send status codes for translation to the error_messages lsl script
  15. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  16. string SLOODLE_PRESENTER_LINKER = "/mod/sloodle/mod/presenter-2.0/linker.php";
  17. string SLOODLE_EOF = "sloodleeof";
  18. list parcelInfo = []; //var for parcel owner
  19. integer MENU_CHANNEL = -1;
  20. string SLOODLE_OBJECT_TYPE = "presenter-2.0";
  21. integer reset;
  22. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  23. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  24. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  25. string sloodleserverroot = "";
  26. string sloodlepwd = "";
  27. integer sloodlecontrollerid = 0;
  28. integer sloodlemoduleid = 0;
  29. integer sloodleobjectaccesslevelctrl = 1; // Who can control this object?
  30. string PRESENTER_TEXTURE= "sloodle_presenter_texture";
  31. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  32. integer eof = FALSE; // Have we reached the end of the configuration data?
  33. key httploadpresentation = NULL_KEY; // Request for presentation data
  34. string presentername = ""; // The name of the Presenter
  35. integer numslides = 0; // Total number of slides in the presentation
  36. integer curslidenum = 0; // Number of the current slide in the presentation (starting from 1)
  37. string curslidetype = ""; // Mimetype of the current slide
  38. string curslidesource = ""; // Source of the current slide media (usually a URL)
  39. string curslidename = ""; // Name of the current slide
  40. // requestConfigData = 0 this is set to 1 after the presenter has been deeded - afterwhich if "update" is
  41. // selected, the presenter will go back into the default state and re-request config data. This is necessary since the teacher may change prentations the presenter is pointing to via the web.
  42. integer requestConfigData = 0;
  43. key myOwnerKey; // The key of the agent who initially rezzed this object -- useful for remembering permission after group deeding.
  44. ///// TRANSLATION /////
  45. // Link message channels
  46. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  47. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  48. // Translation output methods
  49. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  50. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  51. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  52. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  53. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  54. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  55. string SLOODLE_TRANSLATE_DIALOG = "dialog"; // Recipient avatar should be identified in link message keyval. At least 2 output parameters: first the channel number for the dialog, and then 1 to 12 button label strings.
  56. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  57. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  58. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  59. string SLOODLE_TRANSLATE_HOVER_TEXT_BASIC = "hovertextbasic";
  60. // Send a translation request link message
  61. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  62. {
  63. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  64. }
  65. // Request the translation of a standard SLOODLE error code
  66. sloodle_error_code(string method, key avuuid,integer statuscode)
  67. {
  68. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode, NULL_KEY);
  69. }
  70. ///// ----------- /////
  71. ///// FUNCTIONS /////
  72. // Just returns a random integer - used for setting channels
  73. integer random_integer( integer min, integer max )
  74. {
  75. return min + (integer)( llFrand( max - min + 1 ) );
  76. }
  77. sloodle_debug(string msg)
  78. {
  79. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  80. }
  81. // Configure by receiving a linked message from another script in the object
  82. // Returns TRUE if the object has all the data it needs
  83. integer sloodle_handle_command(string str)
  84. {
  85. list bits = llParseString2List(str,["|"],[]);
  86. integer numbits = llGetListLength(bits);
  87. string name = llList2String(bits,0);
  88. string value1 = "";
  89. string value2 = "";
  90. if (numbits > 1) value1 = llList2String(bits,1);
  91. if (numbits > 2) value2 = llList2String(bits,2);
  92. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  93. else if (name == "set:sloodlepwd") {
  94. // The password may be a single prim password, or a UUID and a password
  95. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  96. else sloodlepwd = value1;
  97. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  98. else if (name == "set:sloodlemoduleid") sloodlemoduleid = (integer)value1;
  99. else if (name == "set:sloodleobjectaccesslevelctrl") sloodleobjectaccesslevelctrl = (integer)value1;
  100. else if (name == SLOODLE_EOF) eof = TRUE;
  101. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0 && sloodlemoduleid > 0);
  102. }
  103. // Checks if the given agent is permitted to control this object
  104. // Returns TRUE if so, or FALSE if not
  105. integer sloodle_check_access_ctrl(key id)
  106. {
  107. // Check the access mode
  108. if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  109. return llSameGroup(id);
  110. } else if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  111. return TRUE;
  112. }
  113. // Assume it's owner mode
  114. return (id == llGetOwner());
  115. }
  116. // Update the image display (e.g. change the media URL).
  117. // Does nothing if the data we currently have isn't valid
  118. update_image_display()
  119. {
  120. // Make sure an error didn't occur with the slide plugin
  121. if (curslidetype == "ERROR") return;
  122. // Make sure we have a valid slide number and valid data
  123. if (curslidenum < 1 || curslidenum > numslides) return;
  124. if (curslidetype == "" || curslidesource == "") return;
  125. // llOwnerSay(curslidetype);
  126. string showurl;
  127. if (curslidetype == "image/*") {
  128. // Wrap in an HTML page to force the scaling to happen right.
  129. showurl = "data:text/html," + "<html><body><img width=\"1000\" height=\"1000\" src=\""+curslidesource+"\"></body></html>";
  130. } else {
  131. showurl = curslidesource;
  132. }
  133. llSetPrimMediaParams( 2, [ PRIM_MEDIA_CONTROLS, PRIM_MEDIA_CONTROLS_MINI, PRIM_MEDIA_CURRENT_URL, showurl, PRIM_MEDIA_AUTO_PLAY, 1, PRIM_MEDIA_AUTO_SCALE, 1, PRIM_MEDIA_PERMS_INTERACT, PRIM_MEDIA_PERM_NONE, PRIM_MEDIA_PERMS_CONTROL, PRIM_MEDIA_PERM_NONE ] );
  134. vector size = llGetScale();
  135. doAlign((integer)size.x,(integer)size.y,(integer)size.x);
  136. }
  137. // Request the specified slide
  138. request_slide(integer num)
  139. {
  140. // Indicate that we are requesting a slide
  141. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<1.29037, 1.42417, 0.00000>, 0.9], "loadingslide", [num], NULL_KEY, "presenter");
  142. // Construct and send our HTTP request for data
  143. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  144. body += "&sloodlepwd=" + sloodlepwd;
  145. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  146. body += "&sloodleslidenum=" + (string)num;
  147. httploadpresentation = llHTTPRequest(sloodleserverroot + SLOODLE_PRESENTER_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  148. llSetTimerEvent(8.0);
  149. }
  150. doAlign(integer w, integer h, integer face)
  151. {
  152. // compute scale factors based on next bigger power of 2
  153. integer div;
  154. div = 2;
  155. while (div < w) div*=2;
  156. float scale_s = (float)w / div;
  157. div = 2;
  158. while (div < h) div*=2;
  159. float scale_t = (float)h / div;
  160. // compute offset from scale
  161. float offset_u = -(1.0 - scale_s) / 2.0;
  162. float offset_v = -(1.0 - scale_t) / 2.0;
  163. // do the "Align":
  164. llOffsetTexture(offset_u, offset_v, face);
  165. llScaleTexture(scale_s, scale_t, face);
  166. }
  167. // Move to the next slide
  168. next_slide()
  169. {
  170. if (curslidenum < 1 || curslidenum >= numslides) request_slide(1);
  171. else request_slide(curslidenum + 1);
  172. }
  173. // Move to the previous slide
  174. previous_slide()
  175. {
  176. if (curslidenum <= 1 || curslidenum > numslides) request_slide(numslides);
  177. else request_slide(curslidenum - 1);
  178. }
  179. // Update the hover text to indicate which image is being displayed
  180. update_hover_text()
  181. {
  182. // Make sure we actually have some slide data
  183. if (numslides == 0) {
  184. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.5, 0.0, 0.0>, 1.0], "error:nodata", [], NULL_KEY, "presenter");
  185. return;
  186. }
  187. // Make sure an error didn't occur with the slide plugin
  188. if (curslidetype == "ERROR") {
  189. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.5, 0.0, 0.0>, 1.0], "error:pluginnotfound", [presentername, curslidenum, numslides], NULL_KEY, "presenter");
  190. return;
  191. }
  192. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0, 1.0, 0.0>, 1.0], "showingslidename", [presentername, curslidenum, numslides, curslidename], NULL_KEY, "presenter");
  193. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "showingentryurl", [curslidenum, numslides, curslidesource], NULL_KEY, "presenter");
  194. }
  195. ///// STATES /////
  196. // Default state - waiting for configuration
  197. // Default state - waiting for configuration
  198. default{
  199. on_rez(integer start_param) {
  200. myOwnerKey = llGetOwner();
  201. }
  202. state_entry() {
  203. myOwnerKey = llGetOwner();
  204. // llOwnerSay("Owner set to: " + llKey2Name(llGetOwner())+ " with UUID: " + (string)llGetOwner());
  205. reset=FALSE; //used in the last state - if someone deletes the sloodle_presenter_texture, we set a timer, so need to distinguish in timer event, what to do - ie: was the timer set because the texture was deleted? then set reset to TRUE.
  206. state initialize;
  207. }
  208. }
  209. state initialize
  210. {
  211. on_rez(integer start_param) {
  212. // llOwnerSay("Reseting...");
  213. myOwnerKey = llGetOwner();
  214. llResetScript();
  215. }
  216. state_entry()
  217. {
  218. llClearPrimMedia(2);
  219. if ((string)llGetInventoryKey("sloodle_config")!="00000000-0000-0000-0000-000000000000")
  220. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT_BASIC, [<0.00000,1, 0>, 1.0], "touchforwebconfig", [], NULL_KEY, "presenter");
  221. // Starting again with a new configuration
  222. llSetText("", <0.0,0.0,0.0>, 0.0);
  223. isconfigured = FALSE;
  224. eof = FALSE;
  225. // Reset our data
  226. sloodleserverroot = "";
  227. sloodlepwd = "";
  228. sloodlecontrollerid = 0;
  229. sloodlemoduleid = 0;
  230. sloodleobjectaccesslevelctrl = 0;
  231. if (requestConfigData==1){
  232. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  233. }
  234. requestConfigData=1;
  235. }
  236. link_message( integer sender_num, integer num, string str, key id)
  237. {
  238. // Check the channel
  239. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  240. // Split the message into lines
  241. list lines = llParseString2List(str, ["\n"], []);
  242. integer numlines = llGetListLength(lines);
  243. integer i;
  244. for (i = 0; i < numlines; i++) {
  245. isconfigured = sloodle_handle_command(llList2String(lines, i));
  246. }
  247. // If we've got all our data AND reached the end of the configuration data, then move on
  248. if (eof == TRUE) {
  249. if (isconfigured == TRUE) {
  250. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  251. state running;
  252. } else {
  253. // Go all configuration but, it's not complete... request reconfiguration
  254. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  255. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  256. eof = FALSE;
  257. }
  258. }
  259. }
  260. }
  261. touch_start(integer num_detected)
  262. {
  263. // Attempt to request a reconfiguration
  264. //if (llDetectedKey(0) == llGetOwner()) {
  265. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  266. //}
  267. }
  268. }
  269. state running
  270. {
  271. state_entry()
  272. {
  273. // Request the first slide
  274. request_slide(1);
  275. }
  276. state_exit()
  277. {
  278. llSetText("", <0.0, 0.0, 0.0>, 0.0);
  279. llSetTimerEvent(0.0);
  280. }
  281. touch_start(integer num)
  282. {
  283. // Find out what was touched
  284. string buttonname = llGetLinkName(llDetectedLinkNumber(0));
  285. if (buttonname == "next") {
  286. next_slide();
  287. } else if (buttonname == "previous") {
  288. previous_slide();
  289. } else if (buttonname == "reset") {
  290. request_slide(1);
  291. } else if (buttonname == "update") {
  292. requestConfigData=1; // in default data state request config data to ensure
  293. //controller settings proliferate into SL - ie: someone changed the presentation this presenter is pointing to via the requestConfigData=1; // in default data state request config data to ensure
  294. //controller settings proliferate into SL - ie: someone changed the presentation this presenter is pointing to via the web
  295. state default;
  296. }
  297. }
  298. on_rez(integer par) { llResetScript(); }
  299. timer()
  300. {
  301. llSetTimerEvent(0.0);
  302. if (reset == TRUE){
  303. reset = FALSE;
  304. llResetScript();
  305. } else {
  306. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httptimeout", [], NULL_KEY, "");
  307. update_hover_text();
  308. update_image_display();
  309. }
  310. }
  311. http_response(key id, integer status, list meta, string body)
  312. {
  313. // Is this the expected data?
  314. if (id != httploadpresentation) {
  315. return;
  316. }
  317. httploadpresentation = NULL_KEY;
  318. llSetTimerEvent(0.0);
  319. // Make sure the request worked
  320. if (status != 200) {
  321. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  322. return;
  323. }
  324. // Make sure there is a body to the request
  325. if (body == "") {
  326. sloodle_debug("Body of HTTP response was empty.");
  327. return;
  328. }
  329. // Split the data up into lines
  330. list lines = llParseStringKeepNulls(body, ["\n"], []);
  331. integer numlines = llGetListLength(lines);
  332. if (numlines < 2) {
  333. sloodle_debug("HTTP response contained only 1 line - expected at least 2 for a correctly formatted response.");
  334. return;
  335. }
  336. // Extract all the status fields
  337. list statusfields = llParseStringKeepNulls( llList2String(lines,0), ["|"], [] );
  338. // Get the statuscode
  339. integer statuscode = llList2Integer(statusfields,0);
  340. // Was it an error code?
  341. if (statuscode == -131) {
  342. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0, 1.0, 0.0>, 1.0], "error:noplugins", [], NULL_KEY, "presenter");
  343. return;
  344. } else if (statuscode == -10501) {
  345. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0, 1.0, 0.0>, 1.0], "error:noslides", [], NULL_KEY, "presenter");
  346. return;
  347. } else if (statuscode <= 0) {
  348. // Do we have an error message to go with it?
  349. if (numlines > 1) {
  350. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "sloodleerror:desc", [statuscode, llList2String(lines, 1)], NULL_KEY, "");
  351. } else {
  352. // sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "sloodleerror", [statuscode], NULL_KEY, "");
  353. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  354. }
  355. return;
  356. }
  357. // The first data line contains two things: the number of slides, and the name of the Presenter
  358. list fields = llParseStringKeepNulls(llList2String(lines, 1), ["|"], []);
  359. if (llGetListLength(fields) < 2) {
  360. sloodle_debug("Expected at least 2 fields on first data line.");
  361. return;
  362. }
  363. numslides = (integer)llList2String(fields, 0);
  364. presentername = llList2String(fields, 1);
  365. fields = [];
  366. // Make sure we have another line, which will be the current slide data.
  367. if (numlines < 3) {
  368. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0, 1.0, 0.0>, 1.0], "error:noslides", [], NULL_KEY, "presenter");
  369. return;
  370. }
  371. // This line should contain at least 4 fields: num | type | source | name.
  372. // If the "type" value is "ERROR" then the plugin could not be loaded.
  373. fields = llParseStringKeepNulls(llList2String(lines, 2), ["|"], []);
  374. if (llGetListLength(fields) < 4) {
  375. sloodle_debug("Expected at least 4 fields of slide data in second data line.");
  376. return;
  377. }
  378. curslidenum = (integer)llList2String(fields, 0);
  379. curslidetype = llList2String(fields, 1);
  380. curslidesource = llList2String(fields, 2);
  381. curslidename = llList2String(fields, 3);
  382. // Now that we've got all that data, update the display
  383. update_hover_text();
  384. update_image_display();
  385. }
  386. changed(integer change) {
  387. if (change ==CHANGED_INVENTORY){
  388. if ((string)llGetInventoryKey(PRESENTER_TEXTURE)=="00000000-0000-0000-0000-000000000000"){
  389. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT_BASIC, [<0.92748, 0.00000, 0.42705>, 1.0], "stilldoesntexistininventory", [], NULL_KEY, "presenter");
  390. }else{
  391. llSetTexture(PRESENTER_TEXTURE,ALL_SIDES);
  392. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT_BASIC, [<0.92748, 0.00000, 0.42705>, 1.0], "foundsloodletexture", [], NULL_KEY, "presenter");
  393. reset=TRUE;
  394. llSetTimerEvent(3.0);
  395. }
  396. }
  397. }
  398. }
  399. // Please leave the following line intact to show where the script lives in Git:
  400. // SLOODLE LSL Script Git Location: mod/presenter-2.0/objects/default/assets/sloodle_mod_presenter-shared-media-2.0.lslp