home *** CD-ROM | disk | FTP | other *** search
/ Ice Age Fan CD 1 / CD1_Scrat.iso / flash / data / game.swf / scripts / com / terrypaton / ui / DropdownClass.as
Encoding:
Text File  |  2012-07-04  |  4.6 KB  |  153 lines

  1. package com.terrypaton.ui
  2. {
  3.    import com.terry.Broadcaster;
  4.    import com.terrypaton.events.DropdownEvents;
  5.    import flash.display.MovieClip;
  6.    import flash.events.MouseEvent;
  7.    import flash.geom.Rectangle;
  8.    
  9.    public class DropdownClass extends MovieClip
  10.    {
  11.       private static var DS_CLOSED:int = 0;
  12.       
  13.       private static var DS_OPEN:int = 1;
  14.       
  15.       public var back:MovieClip;
  16.       
  17.       private var btnHeight:Number = 27;
  18.       
  19.       private var btnWidth:Number = 226;
  20.       
  21.       private var _holder:MovieClip;
  22.       
  23.       private var _marker:MovieClip;
  24.       
  25.       private var _clip:MovieClip;
  26.       
  27.       private var _back:MovieClip;
  28.       
  29.       private var closedRect:Rectangle;
  30.       
  31.       private var openRect:Rectangle;
  32.       
  33.       private var currentSelectedItem:int = 0;
  34.       
  35.       private var dropDownState:int;
  36.       
  37.       private var buttonHeight:Number;
  38.       
  39.       public function DropdownClass()
  40.       {
  41.          closedRect = new Rectangle();
  42.          openRect = new Rectangle();
  43.          super();
  44.          currentSelectedItem = 0;
  45.          dropDownState = DS_CLOSED;
  46.          this.addEventListener(MouseEvent.CLICK,mouseClickHandler);
  47.          this.addEventListener(MouseEvent.ROLL_OUT,mouseLeaveHandler);
  48.          Broadcaster.addEventListener(DropdownEvents.CLOSE_DROP_DOWN,closeMenu);
  49.       }
  50.       
  51.       public function setup(param1:Array) : void
  52.       {
  53.          _holder = new MovieClip();
  54.          this.addChild(_holder);
  55.          _back = this.back;
  56.          _holder.addChild(_back);
  57.          var _loc2_:int = int(param1.length);
  58.          var _loc3_:int = 0;
  59.          var _loc4_:int = 0;
  60.          while(_loc4_ < _loc2_)
  61.          {
  62.             _clip = new DD_button();
  63.             _clip.id = _loc4_;
  64.             _clip.y = _loc3_;
  65.             _clip.mouseChildren = false;
  66.             _clip.textBox.text = param1[_loc4_];
  67.             _clip.addEventListener(MouseEvent.ROLL_OVER,btnRollOver);
  68.             _clip.addEventListener(MouseEvent.ROLL_OUT,btnRollOut);
  69.             _holder.addChild(_clip);
  70.             _loc3_ += btnHeight;
  71.             buttonHeight = btnHeight;
  72.             _loc4_++;
  73.          }
  74.          closedRect.height = btnHeight;
  75.          openRect.height = _loc3_;
  76.          closedRect.width = openRect.width = btnWidth;
  77.          _holder.scrollRect = closedRect;
  78.          this.back.height = openRect.height;
  79.          this.back.width = openRect.width;
  80.          _marker = new DD_marker();
  81.          _marker.mouseChildren = false;
  82.          _marker.mouseEnabled = false;
  83.          _marker.visible = false;
  84.          this.addChild(_marker);
  85.       }
  86.       
  87.       public function getCurrentItem() : int
  88.       {
  89.          return currentSelectedItem;
  90.       }
  91.       
  92.       public function btnRollOver(param1:MouseEvent) : void
  93.       {
  94.          if(dropDownState == DS_OPEN)
  95.          {
  96.             _clip = param1.target as MovieClip;
  97.             _clip.gotoAndPlay("over");
  98.          }
  99.       }
  100.       
  101.       public function btnRollOut(param1:MouseEvent) : void
  102.       {
  103.          if(dropDownState == DS_OPEN)
  104.          {
  105.             _clip = param1.target as MovieClip;
  106.             _clip.gotoAndPlay("out");
  107.          }
  108.       }
  109.       
  110.       public function mouseClickHandler(param1:MouseEvent) : void
  111.       {
  112.          var _loc2_:Object = null;
  113.          if(dropDownState == DS_CLOSED)
  114.          {
  115.             _holder.scrollRect = openRect;
  116.             dropDownState = DS_OPEN;
  117.             _marker.visible = true;
  118.             _marker.y = buttonHeight * currentSelectedItem;
  119.          }
  120.          else
  121.          {
  122.             _clip = param1.target as MovieClip;
  123.             if(_clip.id != null)
  124.             {
  125.                if(currentSelectedItem != _clip.id)
  126.                {
  127.                   currentSelectedItem = _clip.id;
  128.                   _clip.gotoAndPlay("out");
  129.                   _loc2_ = new Object();
  130.                   _loc2_.id = currentSelectedItem;
  131.                   Broadcaster.dispatchEvent(new DropdownEvents(DropdownEvents.NEW_ITEM_SELECTED,true,_loc2_));
  132.                }
  133.             }
  134.             Broadcaster.dispatchEvent(new DropdownEvents(DropdownEvents.CLOSE_DROP_DOWN));
  135.          }
  136.       }
  137.       
  138.       private function closeMenu(param1:DropdownEvents) : void
  139.       {
  140.          _marker.visible = false;
  141.          closedRect.y = buttonHeight * currentSelectedItem;
  142.          _holder.scrollRect = closedRect;
  143.          dropDownState = DS_CLOSED;
  144.       }
  145.       
  146.       public function mouseLeaveHandler(param1:MouseEvent) : void
  147.       {
  148.          Broadcaster.dispatchEvent(new DropdownEvents(DropdownEvents.CLOSE_DROP_DOWN));
  149.       }
  150.    }
  151. }
  152.  
  153.