sloodle_choice_option.lslp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle Choice (for Sloodle 0.3)
  4. // This script represents a single bar on the bar graph display.
  5. //
  6. // Copyright (c) 2008 Sloodle
  7. // Released under the GNU GPL
  8. //
  9. // Contributors:
  10. // Peter R. Bloomfield
  11. //
  12. integer SLOODLE_CHANNEL_OBJECT_CHOICE = -1639270051;
  13. // Choice commands
  14. // Update the specified option. Followed by "|num|text|colour|count|prop"
  15. // - num is a local option identifier
  16. // - text is the caption to display for this option
  17. // - colour is a colour vector (cast to a string)
  18. // - count is the number selected so far (or -1 if we don't want to display any)
  19. // - prop is the proportion of maximum size to show (between 0 and 1)
  20. string SLOODLE_CHOICE_UPDATE_OPTION = "do:updateoption";
  21. // Select the specified option. Followed by "|num" (num is a local option identifier)
  22. string SLOODLE_CHOICE_SELECT_OPTION = "do:selectoption";
  23. // The option scales on on the X axis.
  24. // Define the boundary sizes of this option, on the X axis.
  25. // (This actually represents double the visible size: only half will be visible due to path cutting).
  26. // (That easily allows a bar graph that grows only in one direction.)
  27. float MAX_SIZE = 5.0;
  28. float MIN_SIZE = 0.1;
  29. // This value determines if a number should be shown on this bar to indicate the number of selections so far.
  30. integer SHOW_SELECTIONS_NUM = FALSE;
  31. // The local number of this option
  32. integer myoptionnum = -1;
  33. // Set the scale of this option to the specified proportion
  34. set_scale(float prop)
  35. {
  36. // Validate the proportion
  37. if (prop < 0.0) prop = 0.0;
  38. if (prop > 1.0) prop = 1.0;
  39. // Get the current scale
  40. vector myscale = llGetScale();
  41. // Interpolate between the minimum and maximum sizes on the appropriate axis
  42. myscale.x = MIN_SIZE + ((MAX_SIZE - MIN_SIZE) * prop);
  43. // Set the scale
  44. llSetScale(myscale);
  45. }
  46. // Process an update command.
  47. // (parts should be a list of parts from the incoming command).
  48. // Returns true if successful, or false otherwise.
  49. integer process_update_command(list parts)
  50. {
  51. // Get our option number
  52. myoptionnum = (integer)llGetObjectDesc();
  53. // We should have several parameters: "command|num|text|colour|count|prop"
  54. if (llGetListLength(parts) < 6) return FALSE;
  55. // Make sure the option number matches this one
  56. if (myoptionnum != (integer)llList2String(parts, 1)) return FALSE;
  57. // Extract the data
  58. vector mycol = (vector)llList2String(parts, 3);
  59. integer mycount = (integer)llList2String(parts, 4);
  60. float myprop = (float)llList2String(parts, 5);
  61. // Set our attributes
  62. llSetColor(mycol, ALL_SIDES);
  63. if (mycount >= 0 && SHOW_SELECTIONS_NUM) llSetText((string)mycount, mycol, 0.9);
  64. else llSetText("", mycol, 0.0);
  65. set_scale(myprop);
  66. return TRUE;
  67. }
  68. // Reset this option back to defaults
  69. reset_option()
  70. {
  71. myoptionnum = -1;
  72. llSetText("", <0.0, 0.0, 0.0>, 0.0);
  73. llSetColor(<1.0,1.0,1.0>, ALL_SIDES);
  74. set_scale(0.0);
  75. }
  76. // Uninitialised
  77. default
  78. {
  79. state_entry()
  80. {
  81. reset_option();
  82. }
  83. link_message(integer sender_num, integer num, string sval, key kval)
  84. {
  85. // Check the channel
  86. if (num == SLOODLE_CHANNEL_OBJECT_CHOICE) {
  87. // Parse the string
  88. list parts = llParseString2List(sval, ["|"], []);
  89. string cmd = llList2String(parts, 0);
  90. // Check the command
  91. if (cmd == "do:reset") {
  92. reset_option();
  93. } else if (cmd == SLOODLE_CHOICE_UPDATE_OPTION) {
  94. if (process_update_command(parts)) state ready;
  95. }
  96. }
  97. }
  98. }
  99. // Ready and responding to interactions
  100. state ready
  101. {
  102. link_message(integer sender_num, integer num, string sval, key kval)
  103. {
  104. // Check the channel
  105. if (num == SLOODLE_CHANNEL_OBJECT_CHOICE) {
  106. // Parse the string
  107. list parts = llParseString2List(sval, ["|"], []);
  108. string cmd = llList2String(parts, 0);
  109. // Check the command
  110. if (cmd == "do:reset") {
  111. state default;
  112. } else if (cmd == SLOODLE_CHOICE_UPDATE_OPTION) {
  113. process_update_command(parts);
  114. }
  115. }
  116. }
  117. touch_start(integer num)
  118. {
  119. // Go through each toucher, and notify the choice
  120. integer i;
  121. for (i = 0; i < num; i++) {
  122. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_CHOICE, SLOODLE_CHOICE_SELECT_OPTION + "|" + (string)myoptionnum, llDetectedKey(i));
  123. }
  124. }
  125. }
  126. // Please leave the following line intact to show where the script lives in Git:
  127. // SLOODLE LSL Script Git Location: mod/choice-1.0/objects/default/assets/sloodle_choice_option.lslp