Quantcast
Channel: Flex Monkey Patches » as3
Viewing all articles
Browse latest Browse all 10

Flex – simple drag selection base code

$
0
0

I have a few samples I am working on to demonstrate how to do some “drag selection” and I thought some of the base code I use in these samples would be good to post on its own.

So here it is. Pretty basic, but it will serve as the basis for a few samples that I should have out next week (hopefully).

Download a zipfile containing the source to this sample.

Browse the source of this example.

Or continue into the blog entry to see the source:

Here is the app code:

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*">
  4.         <myCanvas borderStyle="solid" backgroundColor="white" backgroundAlpha=".5"
  5.                 width="400" height="300"/>
  6.  
  7. </mx:Application>
  8.  

Here is the custom component code:

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
  4.         mouseMove="onMouseMove(event)" mouseDown="onMouseDown(event)"
  5.         mouseOut="onMouseOut(event)" mouseUp="onMouseUp(event)">
  6.         <mx:Script>
  7.                 <![CDATA[
  8.  
  9.                         import flash.events.MouseEvent;
  10.                         import mx.core.UIComponent;
  11.  
  12.                         private var startMouseX:int;
  13.                         private var startMouseY:int;
  14.                         private var isMouseDown:Boolean=false;
  15.  
  16.  
  17.                         private function onMouseDown(event:MouseEvent):void{
  18.                                 isMouseDown=true;
  19.                                 startMouseX=mouseX;
  20.                                 startMouseY=mouseY;
  21.  
  22.                         }
  23.  
  24.                         private function onMouseUp(event:MouseEvent):void{
  25.                                 isMouseDown=false;
  26.                                 graphics.clear();
  27.                         }
  28.  
  29.                         private function onMouseMove(event:MouseEvent):void{
  30.                                 if (isMouseDown){
  31.                                         graphics.clear();
  32.  
  33.                                         graphics.beginFill( 0xCCCCCC );
  34.                                         graphics.drawRect( startMouseX, startMouseY, event.localX – startMouseX, event.localY – startMouseY );
  35.  
  36.                                 }
  37.                         }
  38.  
  39.                         private function onMouseOut(event:MouseEvent):void{
  40.                                 isMouseDown=false;
  41.                                 dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
  42.                         }
  43.  
  44.  
  45.                 ]]>
  46.         </mx:Script>
  47. </mx:Canvas>
  48.  

Viewing all articles
Browse latest Browse all 10

Trending Articles