home *** CD-ROM | disk | FTP | other *** search
/ Canadian Forces: Prepare for Takeoff as a Pilot / CF_FS_Air.iso.iso / pc / data / swf / flashpaper / 100.swf / scripts / FSliderSymbol.as next >
Text File  |  2004-08-24  |  11KB  |  399 lines

  1. FSliderClass = function()
  2. {
  3.    this.width = !this.horizontal ? this._height : this._width;
  4.    super();
  5.    this._xscale = this._yscale = 100;
  6.    this._rotation = 0;
  7.    this.attachMovie("fsl_scrollTrack","scrollTrack_mc",this.depth++,{_y:1});
  8.    this.setThumbSymbol(this.thumbSymbol);
  9.    this.setEnabled(this.enabled);
  10.    if(this.minValue >= this.maxValue)
  11.    {
  12.       this.minValue = 0;
  13.       this.maxValue = 100;
  14.    }
  15.    this.setSliderProperties(this.minValue,this.maxValue);
  16.    this.setTickFrequency(this.tickFrequency);
  17.    this.setStyleProperty("fillFace",this.fillColor,true);
  18.    delete this.fillColor;
  19.    this.setSize(this.width);
  20.    this.setHorizontal(this.horizontal);
  21.    this.setChangeHandler(this.changeHandler);
  22. };
  23. FSliderClass.prototype = new FUIComponentClass();
  24. Object.registerClass("FSliderSymbol",FSliderClass);
  25. FSliderClass.prototype.depth = 0;
  26. FSliderClass.prototype.minValue = 0;
  27. FSliderClass.prototype.maxValue = 100;
  28. FSliderClass.prototype.range = 100;
  29. FSliderClass.prototype.value = 0;
  30. FSliderClass.prototype.tickFrequency = 2;
  31. FSliderClass.prototype.tickJumpSize = 10;
  32. FSliderClass.prototype.tickHeight = 2;
  33. FSliderClass.prototype.tickSpacing = 3;
  34. FSliderClass.prototype.tickStyle = "none";
  35. FSliderClass.prototype.tickSnapping = false;
  36. FSliderClass.prototype.sm_stepAmount = 1;
  37. FSliderClass.prototype.lg_stepAmount = 5;
  38. FSliderClass.prototype.horizontal = true;
  39. FSliderClass.prototype.thumbSymbol = "fsl_squareThumb";
  40. FSliderClass.prototype.fillColor = 13421772;
  41. FSliderClass.prototype.getMinValue = function()
  42. {
  43.    return this.minValue;
  44. };
  45. FSliderClass.prototype.getSnapToTicks = function()
  46. {
  47.    return this.tickSnapping;
  48. };
  49. FSliderClass.prototype.getIntValue = function()
  50. {
  51.    return Math.round(this.value);
  52. };
  53. FSliderClass.prototype.getTickStyle = function()
  54. {
  55.    return this.tickStyle;
  56. };
  57. FSliderClass.prototype.getThumbSymbol = function()
  58. {
  59.    return this.thumbSymbol;
  60. };
  61. FSliderClass.prototype.getHorizontal = function()
  62. {
  63.    return this.horizontal;
  64. };
  65. FSliderClass.prototype.getTickFrequency = function()
  66. {
  67.    return this.tickFrequency;
  68. };
  69. FSliderClass.prototype.getSmallStepAmount = function()
  70. {
  71.    return this.sm_stepAmount;
  72. };
  73. FSliderClass.prototype.getLargeStepAmount = function()
  74. {
  75.    return this.lg_stepAmount;
  76. };
  77. FSliderClass.prototype.getMaxValue = function()
  78. {
  79.    return this.maxValue;
  80. };
  81. FSliderClass.prototype.getValue = function()
  82. {
  83.    return this.value;
  84. };
  85. FSliderClass.prototype.setSize = function(width)
  86. {
  87.    if(width < 4)
  88.    {
  89.       return false;
  90.    }
  91.    this.width = width;
  92.    this.scrollableArea = !this.thumb_mc ? this.width - 5 : this.width - 2 - this.thumb_mc._width;
  93.    this.scrollTrack_mc._x = this.width / 2;
  94.    this._drawTicks();
  95.    this.setValue(this.value);
  96. };
  97. FSliderClass.prototype.setSnapToTicks = function(snap)
  98. {
  99.    this.tickSnapping = snap;
  100.    this.setValue(this.value);
  101. };
  102. FSliderClass.prototype.setSliderProperties = function(min, max)
  103. {
  104.    if(max <= min)
  105.    {
  106.       return false;
  107.    }
  108.    this.maxValue = Number(max);
  109.    this.minValue = Number(min);
  110.    if(isNaN(this.maxValue) || isNaN(this.minValue))
  111.    {
  112.       return false;
  113.    }
  114.    this.range = this.maxValue - this.minValue;
  115.    this.setTickFrequency(this.tickFrequency);
  116.    this.setValue(this.value);
  117. };
  118. FSliderClass.prototype.setEnabled = function(enable)
  119. {
  120.    super.setEnabled(enable);
  121.    this.enabled = enable;
  122.    if(!enable)
  123.    {
  124.       this.scrollTrack_mc.gotoAndStop("disabled");
  125.       this.thumb_mc.gotoAndStop("disabled");
  126.       delete this.onPress;
  127.       delete this.onRelease;
  128.       delete this.onReleaseOutside;
  129.       delete this.onMouseMove;
  130.    }
  131.    else
  132.    {
  133.       this.scrollTrack_mc.gotoAndStop(1);
  134.       this.thumb_mc.gotoAndStop(1);
  135.       this.onPress = this._onPress;
  136.       this.onRollOver = this._onRollOver;
  137.       this.onRollOut = this._onRollOut;
  138.       this.onRelease = this.onReleaseOutside = this._onRelease;
  139.    }
  140.    this.setSize(this.width);
  141. };
  142. FSliderClass.prototype.setTickStyle = function(style)
  143. {
  144.    this.tickStyle = style;
  145.    this._drawTicks();
  146. };
  147. FSliderClass.prototype.setThumbSymbol = function(symbol)
  148. {
  149.    var depth;
  150.    this.thumbSymbol = symbol;
  151.    if(this.thumb_mc)
  152.    {
  153.       depth = this.thumb_mc.getDepth();
  154.       this.thumb_mc.removeMovieClip();
  155.    }
  156.    else
  157.    {
  158.       depth = this.depth++;
  159.    }
  160.    this.attachMovie(symbol,"thumb_mc",depth);
  161.    this.setSize(this.width);
  162. };
  163. FSliderClass.prototype.setHorizontal = function(horizontal)
  164. {
  165.    if(horizontal)
  166.    {
  167.       this._rotation = 0;
  168.       this._xscale = 100;
  169.       this.scrollTrack_mc._rotation = 0;
  170.       this.scrollTrack_mc._yscale = 100;
  171.    }
  172.    else if(!horizontal)
  173.    {
  174.       this._rotation = 90;
  175.       this.scrollTrack_mc._rotation = 180;
  176.       this.scrollTrack_mc._yscale = -100;
  177.    }
  178.    this.horizontal = horizontal;
  179.    this.setValue(this.getValue());
  180. };
  181. FSliderClass.prototype.setSmallStepAmount = function(step)
  182. {
  183.    if(step < 1)
  184.    {
  185.       return false;
  186.    }
  187.    this.sm_stepAmount = Number(step);
  188. };
  189. FSliderClass.prototype.setLargeStepAmount = function(step)
  190. {
  191.    if(step < 1)
  192.    {
  193.       return false;
  194.    }
  195.    this.lg_stepAmount = Number(step);
  196. };
  197. FSliderClass.prototype.setValue = function(value)
  198. {
  199.    var callHandler = false;
  200.    value = Math.min(Math.max(value,this.minValue),this.maxValue);
  201.    if(this.tickSnapping)
  202.    {
  203.       value = this._snapValue(value);
  204.    }
  205.    if(this.snapValues != undefined && this.snapValues.length > 0)
  206.    {
  207.       var i = 0;
  208.       while(i < this.snapValues.length)
  209.       {
  210.          if(value >= this.snapValues[i] - this.snapThreshold && value <= this.snapValues[i] + this.snapThreshold)
  211.          {
  212.             value = this.snapValues[i];
  213.             break;
  214.          }
  215.          i++;
  216.       }
  217.    }
  218.    if(this.value != value)
  219.    {
  220.       callHandler = true;
  221.    }
  222.    this.value = value;
  223.    var xPos = this.valueToCoord(value);
  224.    this.thumb_mc._x = xPos;
  225.    this.scrollTrack_mc.left_mc._x = (- this.width) / 2;
  226.    var rightElement = this.scrollTrack_mc.middleRight_mc;
  227.    var leftElement = this.scrollTrack_mc.middleLeft_mc;
  228.    if(!this.horizontal)
  229.    {
  230.       xPos = this.valueToCoord(this.range - value + this.minValue * 2);
  231.    }
  232.    leftElement._width = xPos + this.thumb_mc._width / 2;
  233.    leftElement._x = this.scrollTrack_mc.left_mc._x + this.scrollTrack_mc.left_mc._width;
  234.    rightElement._width = this.width - xPos - this.scrollTrack_mc.left_mc._width - this.scrollTrack_mc.right_mc._width - (this.thumb_mc._width - 2) / 2;
  235.    rightElement._x = leftElement._x + leftElement._width;
  236.    this.scrollTrack_mc.right_mc._x = rightElement._x + rightElement._width;
  237.    this.executeCallBack();
  238. };
  239. FSliderClass.prototype.setTickFrequency = function(frequency)
  240. {
  241.    frequency = Number(frequency);
  242.    if(isNaN(frequency))
  243.    {
  244.       return false;
  245.    }
  246.    frequency = Math.max(frequency,2);
  247.    this.tickFrequency = frequency;
  248.    this.tickJumpSize = this.range / (this.tickFrequency - 1);
  249.    this._drawTicks();
  250.    this.setValue(this.value);
  251. };
  252. FSliderClass.prototype._onMouseMove = function()
  253. {
  254.    this.setValueFromThumb();
  255. };
  256. FSliderClass.prototype._onRelease = function()
  257. {
  258.    this.focused = false;
  259.    this.setValueFromThumb();
  260.    delete this.onMouseMove;
  261.    delete this.thumbPressed;
  262.    delete this.distanceFromCenter;
  263.    this.thumb_mc.gotoAndStop(1);
  264.    this.myOnKillFocus();
  265.    Selection.setFocus(null);
  266. };
  267. FSliderClass.prototype._snapValue = function(value)
  268. {
  269.    return Math.round((value - this.minValue) / this.tickJumpSize) * this.tickJumpSize + this.minValue;
  270. };
  271. FSliderClass.prototype.getValueFromThumb = function()
  272. {
  273.    var xMouse;
  274.    if(!this.thumbPressed)
  275.    {
  276.       xMouse = this._xmouse - this.thumb_mc._width / 2;
  277.    }
  278.    else
  279.    {
  280.       xMouse = this._xmouse - this.distanceFromCenter;
  281.    }
  282.    xMouse = Math.max(1,Math.min(xMouse,this.scrollableArea + 1));
  283.    return this.coordToValue(xMouse);
  284. };
  285. FSliderClass.prototype._onPress = function()
  286. {
  287.    this.pressFocus();
  288.    this.thumbPressed = this.thumb_mc.hitTest(_root._xmouse,_root._ymouse);
  289.    if(this.thumbPressed)
  290.    {
  291.       this.distanceFromCenter = this._xmouse - this.thumb_mc._x;
  292.    }
  293.    this.setValueFromThumb();
  294.    this.onMouseMove = this._onMouseMove;
  295.    this.thumb_mc.gotoAndStop("pressed");
  296.    this.myOnSetFocus();
  297. };
  298. FSliderClass.prototype._onRollOver = function()
  299. {
  300.    this.thumb_mc.gotoAndStop("over");
  301. };
  302. FSliderClass.prototype._onRollOut = function()
  303. {
  304.    this.thumb_mc.gotoAndStop(1);
  305. };
  306. FSliderClass.prototype.setValueFromThumb = function()
  307. {
  308.    this.setValue(this.getValueFromThumb());
  309. };
  310. FSliderClass.prototype.coordToValue = function(coord)
  311. {
  312.    if(this.horizontal)
  313.    {
  314.       var value = (coord - 1) / this.scrollableArea * this.range + this.minValue;
  315.    }
  316.    else
  317.    {
  318.       var value = Math.abs(coord - 1 - this.scrollableArea) / this.scrollableArea * this.range + this.minValue;
  319.    }
  320.    return value;
  321. };
  322. FSliderClass.prototype.valueToCoord = function(value)
  323. {
  324.    if(this.horizontal)
  325.    {
  326.       return (value - this.minValue) / this.range * this.scrollableArea + 1;
  327.    }
  328.    return Math.abs(value - (this.range + this.minValue)) / this.range * this.scrollableArea + 1;
  329. };
  330. FSliderClass.prototype._drawTicks = function()
  331. {
  332.    var depth;
  333.    if(!this.tick_mc)
  334.    {
  335.       this.createEmptyMovieClip("tick_mc",this.depth++);
  336.       this.registerSkinElement(this.tick_mc,"tickColor");
  337.    }
  338.    this.tick_mc.clear();
  339.    var curValue = this.minValue;
  340.    var y = this.scrollTrack_mc._height + this.scrollTrack_mc._y + this.tickSpacing;
  341.    var i = 0;
  342.    while(i < this.tickFrequency)
  343.    {
  344.       var x = this.valueToCoord(curValue);
  345.       x += this.thumb_mc._width / 2;
  346.       curValue += this.tickJumpSize;
  347.       if(this.tickStyle == "bottom" || this.tickStyle == "both")
  348.       {
  349.          with(this.tick_mc)
  350.          {
  351.             lineStyle(0,0);
  352.             moveTo(x,y);
  353.             lineTo(x,y + this.tickHeight);
  354.          }
  355.       }
  356.       if(this.tickStyle == "top" || this.tickStyle == "both")
  357.       {
  358.          with(this.tick_mc)
  359.          {
  360.             lineStyle(0,0);
  361.             moveTo(x,- this.tickSpacing + 1);
  362.             lineTo(x,- this.tickSpacing - this.tickHeight + 1);
  363.          }
  364.       }
  365.       i++;
  366.    }
  367. };
  368. FSliderClass.prototype.myOnKeyDown = function()
  369. {
  370.    if(!this.enable)
  371.    {
  372.       return undefined;
  373.    }
  374.    var increase = !!this.horizontal ? 39 : 38;
  375.    var decrease = !!this.horizontal ? 37 : 40;
  376.    var code = Key.getCode();
  377.    var step = !Key.isDown(17) ? this.sm_stepAmount : this.lg_stepAmount;
  378.    if(this.tickSnapping && step < this.tickJumpSize)
  379.    {
  380.       step = this.tickJumpSize;
  381.    }
  382.    if(code == increase)
  383.    {
  384.       this.setValue(this.value + step);
  385.    }
  386.    else if(code == decrease)
  387.    {
  388.       this.setValue(this.value - step);
  389.    }
  390. };
  391. FSliderClass.prototype.setSnapValues = function(_array)
  392. {
  393.    this.snapValues = _array;
  394. };
  395. FSliderClass.prototype.setSnapThreshold = function(_i)
  396. {
  397.    this.snapThreshold = _i;
  398. };
  399.