// RhinoTrip's funky volumebar control script
// contact:  jkaczmar@wso.williams.edu  and  www.stefanweb.com
//
// This script will control an animated volume bar
//
//
// Add the following lines to your .xml
//
//   <layer x="25" y="1" id="volumeaniminfo" image="player.anim.volume" move="0" ghost="1"/>
//
//   <AnimatedLayer 
//      x="121" y="231" 
//      w="25" h="25"
//      id="VolumeAnim" 
//      image="player.anim.volume" 
//      move="0"
//      autoplay="0"
//      autoreplay="0"
//   />
//
//   <slider id="HiddenVolume" 
//       ACTION="Volume" x="400" y="400" w="40" h="2"
//       thumb="player.anim.volume" />
//
// You change the x and y, w and h of the animatedlayer to reflect the specifics of your 
// animation.  w and h define the frame size, btw.  Not the entire image.  
//
// volumeaniminfo:  set the x = the number of frames of animation
// volumeaniminfo:  the y value:
//                               set to 0 if you do NOT have a song title ticker text area
//				 set to 1 if you DO have a song title ticker text area
//
//       IMPORTANT NOTE:  this song ticker text area MUST HAVE an id="songticker" of you
//                        will die.
//
//       The reason I include this option is so the script can write the volume level to 
//       the songticker as the volume changes.
//
// Do NOT change the id= of either the volumeaniminfo or VolumeAnim or the script won't work.
//
// Make sure that the slider HiddenVolume is NOT showing on your skin by adjusting x and y if
// necessary.  Don't change anything else though.
//
// You must also create a map file on a gradient to place underneath the animation.  You 
// don't need to put it in the container section of the xml file, but you do have to load
// it in the elements:
//
// <bitmap id="player.map.volume" file="player/player-map-volume.png"/>
//
// Again, DO NOT change the id.  It must be called player.map.volume for the script to work.
//
// If you need help creating a map file, check http://www.stefanweb.com/wa3/tutorials.html#UsingMaps
//
// Don't forget to include the script in your .xml
//  <script id="volumeanim" file="volume.maki"/>
//
// Finally, if you want to use this script for something other than the "main" container, 
// "normal" layout, you must change the names in the System.getContainer().getLayout() lines
// just below to reflect what you want and recompile.  The compiler can be found at
// http://www.stefanweb.com/wa3/index.html on the front page.
//

#include "../../../lib/std.mi"

Function updateVolume(int v);
Function initVolume();
Function initTicker();

Global AnimatedLayer Volume;
Global Layer VolumeInfo;
Global Map volMap;
Global Timer SongTickerTimer;
Global Text SongTicker;
Global Slider HiddenVolume;

Global Int VolChanging, VolFrames, VolFlag, TickerExist;

System.onScriptLoaded() {

volMap = new Map;
volMap.loadMap("player.map.volume");

Volume = System.getContainer("main").getLayout("normal").getObject("VolumeAnim");
VolumeInfo = System.getContainer("main").getLayout("normal").getObject("volumeaniminfo");
HiddenVolume = System.getContainer("main").getLayout("normal").getObject("HiddenVolume");

initVolume();

}

// Initialzes song ticker
initTicker() {
SongTicker = System.getContainer("main").getLayout("normal").getObject("songticker");

SongTickerTimer = new Timer;
SongTickerTimer.setDelay(1000);
}

// Clears text area
SongTickerTimer.onTimer() {
  SongTicker.setText("");
}

// Initializes volume anim
initVolume() {

  VolumeInfo.setAlpha(0);

  VolChanging = 0;
  VolFlag = 0;
  VolFrames = Volume.getLength();
  VolFrames = VolFrames - 1;
  TickerExist = VolumeInfo.getTop();

  float u;
  u = System.getVolume();
  u = u / 255;
  int k = u * VolFrames;
  Volume.gotoFrame(k);

  if (TickerExist) initTicker();
}

// Reads mouse drag on volume knob
Volume.onMouseMove(int x, int y) {

    if (VolChanging) 
    {
	x = x - Volume.getLeft();
	y = y - Volume.getTop();

	if (volMap.inRegion(x, y)) { 
          float v = volMap.getValue(x, y);
          if (!VolFlag) { 
            updateVolume(v); 
            if (v > 245) { 
              VolFlag = 2;
              updateVolume(255);
            }
            if (v < 10) { 
              VolFlag = 1;
              updateVolume(0);
            }
          }
          else {
            if ((VolFlag == 2) && (v > 120) && (v < 252)) VolFlag = 0;
            if ((VolFlag == 1) && (v < 120) && (v > 2)) VolFlag = 0;
          }
        }
    }
}

// Reads mouse clicks on volume knob
Volume.onLeftButtonDown(int x, int y) {
    VolChanging = 1;
}


Volume.onLeftButtonUp(int x, int y) {

   if (VolChanging) {
    x = x - Volume.getLeft();
    y = y - Volume.getTop();

    if (volMap.inRegion(x, y)) { 
       float v = volMap.getValue(x, y);
       if (!VolFlag) { 
         updateVolume(v); 
         if (v > 245) { 
           VolFlag = 2;
           updateVolume(255);
         }
         if (v < 10) { 
           VolFlag = 1;
           updateVolume(0);
         }
       }
       else {
         if ((VolFlag == 2) && (v > 120) && (v < 252)) VolFlag = 0;
         if ((VolFlag == 1) && (v < 120) && (v > 2)) VolFlag = 0;
       }
     }

    VolChanging = 0;
   }
}

// Updates Volume after drag/click
updateVolume(float v) {

    if (v >= 0)
    {
	float k = v / 255;
        int g = k * VolFrames;
	Volume.gotoFrame(g);

    	System.setVolume(v); 

        int p = k * 100;
	SongTickerTimer.stop();
	SongTickerTimer.start();
	SongTicker.setText("Vol: " + System.integerToString(p) + "%");
     }
}

// Reads System Volume Changes
HiddenVolume.onPostedPosition(int p) {
  if (!VolChanging) {
    Float f = getVolume();
    f = f * VolFrames;
    f = f / 255;
    Volume.gotoFrame(f);    
  }
}

