xytext.lslp.19 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. ////////////////////////////////////////////
  2. // XyText v1.2 Script (5 Face, Single Texture)
  3. //
  4. // Written by Xylor Baysklef
  5. //
  6. // Modified by Kermitt Quirk 19/01/2006
  7. // To add support for 5 face prim instead of 3
  8. //
  9. ////////////////////////////////////////////
  10. /////////////// CONSTANTS ///////////////////
  11. // XyText Message Map.
  12. integer DISPLAY_STRING = 204000;
  13. integer DISPLAY_EXTENDED = 204001;
  14. integer REMAP_INDICES = 204002;
  15. integer RESET_INDICES = 204003;
  16. integer SET_CELL_INFO = 204004;
  17. integer SET_FONT_TEXTURE = 204005;
  18. integer SET_THICKNESS = 204006;
  19. integer SET_COLOR = 204007;
  20. // This is an extended character escape sequence.
  21. string ESCAPE_SEQUENCE = "\\e";
  22. // This is used to get an index for the extended character.
  23. string EXTENDED_INDEX = "12345";
  24. // Face numbers.
  25. integer FACE_1 = 3;
  26. integer FACE_2 = 7;
  27. integer FACE_3 = 4;
  28. integer FACE_4 = 6;
  29. integer FACE_5 = 1;
  30. // Used to hide the text after a fade-out.
  31. //key TRANSPARENT = "701917a8-d614-471f-13dd-5f4644e36e3c";
  32. string TRANSPARENT = "Transparent";
  33. ///////////// END CONSTANTS ////////////////
  34. ///////////// GLOBAL VARIABLES ///////////////
  35. // This is the key of the font we are displaying.
  36. //key gFontTexture = "b2e7394f-5e54-aa12-6e1c-ef327b6bed9e";
  37. string gFontTexture = "FontsTexture";
  38. // All displayable characters. Default to ASCII order.
  39. string gCharIndex;
  40. // This is the channel to listen on while acting
  41. // as a cell in a larger display.
  42. integer gCellChannel = 2;
  43. // This is the starting character position in the cell channel message
  44. // to render.
  45. integer gCellCharPosition = 70;
  46. // This is whether or not to use the fade in/out special effect.
  47. integer gCellUseFading = FALSE;
  48. // This is how long to display the text before fading out (if using
  49. // fading special effect).
  50. // Note: < 0 means don't fade out.
  51. float gCellHoldDelay = 1.0;
  52. /////////// END GLOBAL VARIABLES ////////////
  53. ResetCharIndex() {
  54. gCharIndex = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`";
  55. // \" <-- Fixes LSL syntax highlighting bug.
  56. gCharIndex += "abcdefghijklmnopqrstuvwxyz{|}~";
  57. gCharIndex += "\n\n\n\n\n";
  58. }
  59. vector GetGridOffset(integer index) {
  60. // Calculate the offset needed to display this character.
  61. integer Row = index / 10;
  62. integer Col = index % 10;
  63. // Return the offset in the texture.
  64. return <-0.45 + 0.1 * Col, 0.45 - 0.1 * Row, 0.0>;
  65. }
  66. ShowChars(vector grid_offset1, vector grid_offset2, vector grid_offset3, vector grid_offset4, vector grid_offset5) {
  67. // Set the primitive textures directly.
  68. // <-0.256, 0, 0>
  69. // <0, 0, 0>
  70. // <0.130, 0, 0>
  71. // <0, 0, 0>
  72. // <-0.74, 0, 0>
  73. llSetPrimitiveParams( [
  74. PRIM_TEXTURE, FACE_1, (string)gFontTexture, <0.12, 0.1, 0>, grid_offset1 + <0.037, 0, 0>, 0.0,
  75. PRIM_TEXTURE, FACE_2, (string)gFontTexture, <0.05, 0.1, 0>, grid_offset2, 0.0,
  76. PRIM_TEXTURE, FACE_3, (string)gFontTexture, <-0.74, 0.1, 0>, grid_offset3 - <0.244, 0, 0>, 0.0,
  77. PRIM_TEXTURE, FACE_4, (string)gFontTexture, <0.05, 0.1, 0>, grid_offset4, 0.0,
  78. PRIM_TEXTURE, FACE_5, (string)gFontTexture, <0.12, 0.1, 0>, grid_offset5 - <0.037, 0, 0>, 0.0
  79. ]);
  80. }
  81. RenderString(string str) {
  82. // Get the grid positions for each pair of characters.
  83. vector GridOffset1 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 0, 0)) );
  84. vector GridOffset2 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 1, 1)) );
  85. vector GridOffset3 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 2, 2)) );
  86. vector GridOffset4 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 3, 3)) );
  87. vector GridOffset5 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 4, 4)) );
  88. // Use these grid positions to display the correct textures/offsets.
  89. ShowChars(GridOffset1, GridOffset2, GridOffset3, GridOffset4, GridOffset5);
  90. }
  91. RenderWithEffects(string str) {
  92. // Get the grid positions for each pair of characters.
  93. vector GridOffset1 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 0, 0)) );
  94. vector GridOffset2 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 1, 1)) );
  95. vector GridOffset3 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 2, 2)) );
  96. vector GridOffset4 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 3, 3)) );
  97. vector GridOffset5 = GetGridOffset( llSubStringIndex(gCharIndex, llGetSubString(str, 4, 4)) );
  98. // First set the alpha to the lowest possible.
  99. llSetAlpha(0.05, ALL_SIDES);
  100. // Use these grid positions to display the correct textures/offsets.
  101. ShowChars(GridOffset1, GridOffset2, GridOffset3, GridOffset4, GridOffset5); // Now turn up the alpha until it is at full strength.
  102. float Alpha;
  103. for (Alpha = 0.10; Alpha <= 1.0; Alpha += 0.05)
  104. llSetAlpha(Alpha, ALL_SIDES);
  105. // See if we want to fade out as well.
  106. if (gCellHoldDelay < 0.0)
  107. // No, bail out. (Just keep showing the string at full strength).
  108. return;
  109. // Hold the text for a while.
  110. llSleep(gCellHoldDelay);
  111. // Now fade out.
  112. for (Alpha = 0.95; Alpha >= 0.05; Alpha -= 0.05)
  113. llSetAlpha(Alpha, ALL_SIDES);
  114. // Make the text transparent to fully hide it.
  115. llSetTexture(TRANSPARENT, ALL_SIDES);
  116. }
  117. RenderExtended(string str) {
  118. // Look for escape sequences.
  119. list Parsed = llParseString2List(str, [], [ESCAPE_SEQUENCE]);
  120. integer ParsedLen = llGetListLength(Parsed);
  121. // Create a list of index values to work with.
  122. list Indices;
  123. // We start with room for 5 indices.
  124. integer IndicesLeft = 5;
  125. integer i;
  126. string Token;
  127. integer Clipped;
  128. integer LastWasEscapeSequence = FALSE;
  129. // Work from left to right.
  130. for (i = 0; i < ParsedLen && IndicesLeft > 0; i++) {
  131. Token = llList2String(Parsed, i);
  132. // If this is an escape sequence, just set the flag and move on.
  133. if (Token == ESCAPE_SEQUENCE) {
  134. LastWasEscapeSequence = TRUE;
  135. }
  136. else { // Token != ESCAPE_SEQUENCE
  137. // Otherwise this is a normal token. Check its length.
  138. Clipped = FALSE;
  139. integer TokenLength = llStringLength(Token);
  140. // Clip if necessary.
  141. if (TokenLength > IndicesLeft) {
  142. Token = llGetSubString(Token, 0, IndicesLeft - 1);
  143. TokenLength = llStringLength(Token);
  144. IndicesLeft = 0;
  145. Clipped = TRUE;
  146. }
  147. else
  148. IndicesLeft -= TokenLength;
  149. // Was the previous token an escape sequence?
  150. if (LastWasEscapeSequence) {
  151. // Yes, the first character is an escape character, the rest are normal.
  152. // This is the extended character.
  153. Indices += [llSubStringIndex(EXTENDED_INDEX, llGetSubString(Token, 0, 0)) + 95];
  154. // These are the normal characters.
  155. integer j;
  156. for (j = 1; j < TokenLength; j++)
  157. Indices += [llSubStringIndex(gCharIndex, llGetSubString(Token, j, j))];
  158. }
  159. else { // Normal string.
  160. // Just add the characters normally.
  161. integer j;
  162. for (j = 0; j < TokenLength; j++)
  163. Indices += [llSubStringIndex(gCharIndex, llGetSubString(Token, j, j))];
  164. }
  165. // Unset this flag, since this was not an escape sequence.
  166. LastWasEscapeSequence = FALSE;
  167. }
  168. }
  169. // Use the indices to create grid positions.
  170. vector GridOffset1 = GetGridOffset( llList2Integer(Indices, 0));
  171. vector GridOffset2 = GetGridOffset( llList2Integer(Indices, 1) );
  172. vector GridOffset3 = GetGridOffset( llList2Integer(Indices, 2) );
  173. vector GridOffset4 = GetGridOffset( llList2Integer(Indices, 3) );
  174. vector GridOffset5 = GetGridOffset( llList2Integer(Indices, 4) );
  175. // Use these grid positions to display the correct textures/offsets.
  176. ShowChars(GridOffset1, GridOffset2, GridOffset3, GridOffset4, GridOffset5);
  177. }
  178. integer ConvertIndex(integer index) {
  179. // This converts from an ASCII based index to our indexing scheme.
  180. if (index >= 32) // ' ' or higher
  181. index -= 32;
  182. else { // index < 32
  183. // Quick bounds check.
  184. if (index > 15)
  185. index = 15;
  186. index += 94; // extended characters
  187. }
  188. return index;
  189. }
  190. default {
  191. state_entry() {
  192. // Initialize the character index.
  193. llSetTexture(TRANSPARENT, ALL_SIDES);
  194. ResetCharIndex();
  195. }
  196. link_message(integer sender, integer channel, string data, key id) {
  197. if (channel == DISPLAY_STRING) {
  198. RenderString(data);
  199. return;
  200. }
  201. if (channel == DISPLAY_EXTENDED) {
  202. RenderExtended(data);
  203. return;
  204. }
  205. if (channel == gCellChannel) {
  206. // Extract the characters we are interested in, and use those to render.
  207. string TextToRender = llGetSubString(data, gCellCharPosition, gCellCharPosition + 4);
  208. // See if we need to show special effects.
  209. if (gCellUseFading)
  210. RenderWithEffects( TextToRender );
  211. else // !gCellUseFading
  212. RenderString( TextToRender );
  213. return;
  214. }
  215. if (channel == REMAP_INDICES) {
  216. // Parse the message, splitting it up into index values.
  217. list Parsed = llCSV2List(data);
  218. integer i;
  219. // Go through the list and swap each pair of indices.
  220. for (i = 0; i < llGetListLength(Parsed); i += 2) {
  221. integer Index1 = ConvertIndex( llList2Integer(Parsed, i) );
  222. integer Index2 = ConvertIndex( llList2Integer(Parsed, i + 1) );
  223. // Swap these index values.
  224. string Value1 = llGetSubString(gCharIndex, Index1, Index1);
  225. string Value2 = llGetSubString(gCharIndex, Index2, Index2);
  226. gCharIndex = llDeleteSubString(gCharIndex, Index1, Index1);
  227. gCharIndex = llInsertString(gCharIndex, Index1, Value2);
  228. gCharIndex = llDeleteSubString(gCharIndex, Index2, Index2);
  229. gCharIndex = llInsertString(gCharIndex, Index2, Value1);
  230. }
  231. return;
  232. }
  233. if (channel == RESET_INDICES) {
  234. // Restore the character index back to default settings.
  235. ResetCharIndex();
  236. return;
  237. }
  238. if (channel == SET_CELL_INFO) {
  239. // Change the channel we listen to for cell commands, the
  240. // starting character position to extract from, and
  241. // special effect attributes.
  242. list Parsed = llCSV2List(data);
  243. gCellChannel = (integer) llList2String(Parsed, 0);
  244. gCellCharPosition = (integer) llList2String(Parsed, 1);
  245. gCellUseFading = (integer) llList2String(Parsed, 2);
  246. gCellHoldDelay = (float) llList2String(Parsed, 3);
  247. return;
  248. }
  249. if (channel == SET_FONT_TEXTURE) {
  250. // Use the new texture instead of the current one.
  251. gFontTexture = id;
  252. // Change the currently shown texture.
  253. llSetTexture(gFontTexture, FACE_1);
  254. llSetTexture(gFontTexture, FACE_2);
  255. llSetTexture(gFontTexture, FACE_3);
  256. llSetTexture(gFontTexture, FACE_4);
  257. llSetTexture(gFontTexture, FACE_5);
  258. return;
  259. }
  260. if (channel == SET_THICKNESS) {
  261. // Set our z scale to thickness, while staying fixed
  262. // in position relative the prim below us.
  263. vector Scale = llGetScale();
  264. float Thickness = (float) data;
  265. // Reposition only if this isn't the root prim.
  266. integer ThisLink = llGetLinkNumber();
  267. if (ThisLink != 0 || ThisLink != 1) {
  268. // This is not the root prim.
  269. vector Up = llRot2Up(llGetLocalRot());
  270. float DistanceToMove = Thickness / 2.0 - Scale.z / 2.0;
  271. vector Pos = llGetLocalPos();
  272. llSetPos(Pos + DistanceToMove * Up);
  273. }
  274. // Apply the new thickness.
  275. Scale.z = Thickness;
  276. llSetScale(Scale);
  277. return;
  278. }
  279. if (channel == SET_COLOR) {
  280. vector newColor = (vector)data;
  281. llSetColor(newColor, ALL_SIDES);
  282. }
  283. }
  284. } // Please leave the following line intact to show where the script lives in Git:
  285. // SLOODLE LSL Script Git Location: lsl/xytext/xytext.lsl.19