Pages

Wednesday, October 17, 2012

AS3: Custom Button Controller - 2


Here is the pseudo code for the flash application architecture of custom-button movieclip.
<movieclip name="controller_mc">
 <frameset>
  <frame name="normal">
   <textfield name="caption" framespan="4"></textfield>
   <graphics color="#333333"></graphics>
  </frame>
  <frame name="over">
   <graphics color="#666666"></graphics>
  </frame>
  <frame name="down">
   <graphics color="#999999"></graphics>
  </frame>
  <frame name="disabled">
   <graphics color="#333333" alpha="0.7"></graphics>
  </frame>  
 </frameset>
</movieclip>
The controller class of the custom button.
package engine.ui 
{
 import flash.display.MovieClip;
 import flash.events.MouseEvent;
 /**
  * ...
  * @author Abhishek Kumar
  */
 public class CButton 
 {
  private var Container:MovieClip;
  private var proceed:Function;
  
  public function CButton(mc:MovieClip) 
  {
   Container = mc;
  }
  
  public function initiate():void
  {
   Container.mouseChildren = false;
   Container.buttonMode = true;
   Container.gotoAndStop('normal');
  }
  
  public function setLabel(label:String):void
  {
   Container.caption.text = label;
  }
  
  public function attachListener(callback:Function):void
  {
   proceed = callback;
   Container.addEventListener(MouseEvent.MOUSE_UP, onUp);
   Container.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
   Container.addEventListener(MouseEvent.MOUSE_OVER, onOver);
   Container.addEventListener(MouseEvent.MOUSE_OUT, onOut);
  }
  
  public function detachListener():void
  {
   Container.removeEventListener(MouseEvent.MOUSE_UP, onUp);
   Container.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
   Container.removeEventListener(MouseEvent.MOUSE_OVER, onOver);
   Container.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
  }
  
  private function onUp(me:MouseEvent):void
  {
   Container.gotoAndStop('normal');   
   proceed();
  }

  private function onOver(me:MouseEvent):void
  {
   Container.gotoAndStop('over');
  }

  private function onOut(me:MouseEvent):void
  {
   Container.gotoAndStop('normal');
  }

  private function onDown(me:MouseEvent):void
  {
   Container.gotoAndStop('down');
  }

 }

}

No comments:

Post a Comment