home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Scripting_Resources / Samples / Automation_Scripts / Radio Toggle.js < prev    next >
Encoding:
Text File  |  2002-05-13  |  8.5 KB  |  212 lines

  1. //////////////////////////////////////////////////
  2. //
  3. // ADOBE SYSTEMS INCORPORATED 
  4. // Copyright 2002 Adobe Systems Incorporated 
  5. // All Rights Reserved 
  6. //
  7. // NOTICE:  Adobe permits you to use, modify, and 
  8. // distribute this file in accordance with the terms
  9. // of the Adobe license agreement accompanying it.  
  10. // If you have received this file from a source 
  11. // other than Adobe, then your use, modification,
  12. // or distribution of it requires the prior 
  13. // written permission of Adobe. 
  14. //
  15. //////////////////////////////////////////////////
  16.  
  17. //////////////////////////////////////////////////
  18. // 
  19. // Radio Toggle.js
  20. //
  21. // DESCRIPTION
  22. //
  23. // This script takes one small object and creates a group
  24. // of two radio buttons from it. It add a dynamic text box
  25. // which displays which button is on during execution.
  26. // It shows adding different states to objects and adding 
  27. // scripts to the states.
  28. //
  29. // HOW TO USE
  30. //
  31. // Select Automation > Run Automation Scripts > Radio Toggle.js
  32. // 
  33. // Click on the preview button and view the radio buttons
  34. // and their function.
  35. //
  36. //////////////////////////////////////////////////
  37.  
  38. // Main Code [Execution of script begins here]
  39. // Check if any composition is open
  40. if(application.compositions.length > 0){
  41.     comp = application.currentComposition;
  42.     if(comp.selection.length >= 1){ // Checks if at least one object is selected
  43.         button = comp.selection[0]; // First selected object is assigned to button_in
  44.                 
  45.         // saves the current selection
  46.         application.currentComposition.saveSelection(); 
  47.         
  48.         createButtonsAndStates(button); // Function createButtonandStates called
  49.         
  50.         application.currentComposition.restoreSelection();
  51.     }
  52.     else{ // If no objects are selected brings the Console window up 
  53.         Console.show();
  54.         // Writes to the Console
  55.         Console.write("\nPlease select at least one object which becomes the model for the shape for the radio buttons and run the script again\n");
  56.     }
  57. }
  58. else{// if no composition open
  59.     // opens a new composition
  60.     comp = application.newComposition();
  61.     Console.show();
  62.     Console.write("New Composition opened\nPlease create and select at least one object which becomes the model for the shape for the radio buttons and run script again.\n");
  63. }
  64.  
  65.  
  66. // Add your own functions here
  67.  
  68. //////////////////////////////////////////////////
  69. //
  70. // createButtonsAndStates:
  71. // 
  72. // Creates a group of two radio buttons taking as arguement just one inner object 
  73. // for a radio button. Give them differents effects and states. Then calls another
  74. // function which in turn adds the functionality to the radio buttons.
  75. //
  76. // button: The selected object which is the inner object in a radio button.
  77. //
  78. //////////////////////////////////////////////////
  79.  
  80. function createButtonsAndStates(button_in){
  81.     button_in.name = "dot"; // Named as dot on the timeline for use in player script
  82.     button_in.isMovieClip = true; // Make it a movieclip
  83.  
  84.     // Inner object is filled
  85.     button_in.stroke.type = LMStrokeType.fill;
  86.     
  87.     // Add off state and add script to the off state for dot
  88.     button_in.addState("off"); // Adds a new state "off"
  89.     button_in.currentState = "off" ; // Makes off the current state
  90.     script1 = 'this._alpha = 0;' ; // Adds statement in variable script1
  91.     // Adds script in script1 to the current state of button_in
  92.     button_in.setStateChangeScript("off", script1); 
  93.     
  94.     // Add on state and add script to the on state for dot
  95.     button_in.addState("on"); // Adds a new state "on"
  96.     button_in.currentState = "on"; // Makes on the current state
  97.     script1 = 'this._alpha = 100;'; // Adds statement in variable script1
  98.     // Adds script in script1 to the current state of button_in
  99.     button_in.setStateChangeScript("on", script1);
  100.     
  101.     
  102.     // Create an outer ring for the radio button and position it on the top of it
  103.     button_out = button_in.duplicate();
  104.     button_out.deleteState("off"); // does not work till build 67
  105.     button_out.deleteState("on"); // does not work till build 67
  106.     // Workaround: Delete the states manually in LiveMotion
  107.     
  108.     button_out.currentState = "normal";
  109.     // Note: When an object is duplicated and it has multiple states
  110.     // To edit setting for the duplicated object select its normal state
  111.     // Else the settings will be changed for the last current state only.
  112.     // Outer ring is just an outline
  113.     button_out.stroke.type = LMStrokeType.outline;
  114.     button_out.stroke.width = 5;
  115.     
  116.     // Size of the outer ring is double the size of the inner 
  117.     button_out.size.x = (button_in.size.x + button_in.size.x);
  118.     button_out.size.y = (button_in.size.y + button_in.size.y);
  119.  
  120.     button_out.name = "Outer";// Name the outer ring on the timeline 
  121.     button_out.isMovieClip = true; // Convert it to a movieclip
  122.     button_out.addState(LMStateType.mouseDown);// Add Down state to the outer ring
  123.  
  124.     // Group the inner and outer rings to form one radio button
  125.     // Create an array of objects to group.
  126.     var obj1 = new Array(button_in, button_out);
  127.     
  128.     // Using the group method of composition to group the array.
  129.     var Group1 = comp.group(obj1);
  130.         
  131.     // Naming the group using the name property. Every object has a name property.
  132.     Group1.name = "Radio1"; 
  133.  
  134.     Group1.isMovieClip = true;// Converting group to a movieclip
  135.  
  136.     // Note: Since scripts for the down states for outer buttons are different we are not
  137.     // setting them before duplicating as in the inner case.
  138.  
  139.     // Create Group2 for radio button 2
  140.     // The Group is created with all states and effects that Group1 has!
  141.     Group2 = Group1.duplicate(); // Duplicate group 1
  142.     Group2.name = "Radio2"; // Naming group2 to Radio2 on the timeline
  143.  
  144.     // Positioning Group2
  145.     Group2.position.x = Group1.position.x + Group1.size.x + 50 ;
  146.     Group2.position.y = Group1.position.y ;
  147.     
  148.     AddScript(Group1, Group2); // Calls funtion AddScript. Passes the buttons to it.
  149. }
  150.  
  151. //////////////////////////////////////////////////
  152. //
  153. // AddScript:
  154. // 
  155. // This is a function that adds the functionality to the radio buttons created.
  156. //
  157. // Group1: First radio button.
  158. // Group2: Second radio button.
  159. //
  160. //////////////////////////////////////////////////
  161.  
  162. function AddScript(Group1, Group2){
  163.     // Create a dynamic text object to display which button is on at a time
  164.     // Position it above the buttons - This is approximate, edit if it goes off the composition
  165.     display = comp.createObject(LMObjectType.dynamicText, 100, Group1.Outer.position.y-Group1.Outer.size.y);
  166.     display.text = "Display Box";// Displays this text in the dynamic text box
  167.     display.variable = "disp"; // Name the variable in the dynamic text box
  168.     // Sets the horizontal and vertical size of the objects text reflow box - LMPoint object
  169.     display.reflowSize.x = 100; 
  170.     display.reflowSize.y = 20;
  171.  
  172.  
  173.     // Setting current state within groups of objects
  174.     // Setting current state of Outer in Group1 to Down 
  175.     Group1.Outer.currentState = LMStateType.mouseDown;
  176.     
  177.     // Accumulating the script statements to be written in the down state in variable script1
  178.     script1 = '// This radio button is selected so set the state of the dot for this button to on \n';
  179.     script1 += '_root.Radio1.dot.lmSetCurrentState("on");\n';
  180.     script1 += '// Set the state of dot of the other radio button to off, since only on is on at a time \n';
  181.     script1 += '_root.Radio2.dot.lmSetCurrentState("off");\n';
  182.     script1 += '// Set variable disp to display message - button 1 is on \n';
  183.     script1 += '_root.disp = "Radio Button 1 on";\n';
  184.     
  185.     // Writing script to the Down state of Outer in Group1
  186.     Group1.Outer.setStateChangeScript(LMStateType.mouseDown, script1);
  187.     
  188.     // Setting current state of Outer in Group2 to Down
  189.     Group2.Outer.currentState = LMStateType.mouseDown;
  190.     
  191.     // Accumulating the script statements to be written in the down state in variable script1
  192.     script2 = '// This radio button is selected so set the state of the dot to on \n';
  193.     script2 += '_root.Radio2.dot.lmSetCurrentState("on");\n';
  194.     script2 += '// Set the state of dot of the other radio button to off, since only on is on at a time \n';
  195.     script2 += '_root.Radio1.dot.lmSetCurrentState("off");\n';
  196.     script2 += '// Set variable disp to display message - button 2 is on \n';
  197.     script2 += '_root.disp = "Radio Button 2 on";\n';
  198.     
  199.     // Writing script to the Down state of Outer in Group2
  200.     Group2.Outer.setStateChangeScript(LMStateType.mouseDown, script2); 
  201.  
  202.  
  203.     // Setting script on the onLoad handler so that we have only one button on when the script is loaded
  204.     scriptHandler = '_root.Radio1.dot.lmSetCurrentState("off");\n';
  205.     scriptHandler += '_root.disp = "Radio Button 2 on";\n';
  206.     
  207.     // Set the script on the specific handlers.
  208.     comp.setHandlerScript(LMHandlerType.onLoad, scriptHandler);
  209. }
  210.  
  211.     
  212.