topical media & game development

talk show tell print

#graphic-flex-draw-classes-PreservingPathData.ax

#graphic-flex-draw-classes-PreservingPathData.ax [swf] [flash] flex


  package {
  
          import flash.display.*;
          import flash.events.KeyboardEvent;
          import flash.events.MouseEvent;
          import flash.events.*;
          import flash.ui.Keyboard;
  
          [SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
  
          
Demonstrates how path data can be saved as commands and easily redrawn using drawGraphicsData().

  
          public class @ax-graphic-flex-draw-classes-PreservingPathData extends Sprite {
  
                  private static const INIT_THICKNESS:uint = 5;
                  private static const MAX_THICKNESS:uint = 50;
                  private static const MIN_THICKNESS:uint = 1;
  
                  private var _thickness:uint;
                  private var _drawing:Boolean;
                  private var _commands:Vector.<IGraphicsData>;
                  private var _lineStyle:GraphicsStroke;
                  private var _currentPath:GraphicsPath;
  
                  
Constructor. Calls init().

  
                  public function @ax-graphic-flex-draw-classes-PreservingPathData() {
                          this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
                  }
                  
                  private function onAddedToStage(e:Event):void
                  {
                          this.init();
                  }
  
                  
Initializes properties and sets up listeners.

  
                  private function init():void {
                          var color:uint = Math.random()*0xFFFFFF;
                          _thickness = INIT_THICKNESS;
                          graphics.lineStyle(_thickness, color);
                          // creates GraphicsStoke to save into vector _commands
                          _lineStyle = new GraphicsStroke(_thickness);
                          _lineStyle.fill = new GraphicsSolidFill(color);
                          _commands = new Vector.<IGraphicsData>();
                          _commands.push(_lineStyle);
                          stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
                          stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
                          stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
                  }
  
                  
Redraws the path using the saved commands with drawGraphicsData().

  
                  private function redrawPath():void {
                          graphics.clear();
                          graphics.drawGraphicsData(_commands);
                  }
  
                  
Handler for when a key is pressed. This alters the color or thickness and forces the redrawing of the path.
parameter: event Event fired by stage.

  
                  private function onStageKeyDown(event:KeyboardEvent):void {
                          if (!_drawing) {
                                  switch (event.keyCode) {
                                          // increase thickness with UP arrow
                                          case Keyboard.UP:
                                                  if (_thickness < MAX_THICKNESS) {
                                                          _lineStyle.thickness = ++_thickness;
                                                          redrawPath();
                                                  }
                                                  break;
                                          // decrease thickness with DOWN arrow
                                          case Keyboard.DOWN:
                                                  if (_thickness > MIN_THICKNESS) {
                                                          _lineStyle.thickness = --_thickness;
                                                          redrawPath();
                                                  }
                                                  break;
                                          // recolor with SPACE
                                          case Keyboard.SPACE:
                                                  _lineStyle.fill = new GraphicsSolidFill(Math.random()*0xFFFFFF);
                                                  redrawPath();
                                                  break;
                                  }
                          }
                  }
  
                  
Handler for when the mouse is clicked on the stage. This sets up listeners for drawing while the mouse is depressed.
parameter: event Event fired by stage.

  
                  private function onStageMouseDown(event:MouseEvent):void {
                          _drawing = true;
                          var x:Number = stage.mouseX;
                          var y:Number = stage.mouseY;
                          _currentPath = new GraphicsPath();
                          _currentPath.moveTo(x, y);
                          _commands.push(_currentPath);
                          graphics.moveTo(x, y);
                          stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                  }
  
                  
Handler for when the mouse is moved on the stage. This adds to the drawn path.
parameter: event Event fired by stage.

  
                  private function onStageMouseMove(event:MouseEvent):void {
                          var x:Number = stage.mouseX;
                          var y:Number = stage.mouseY;
                          _currentPath.lineTo(x, y);
                          graphics.lineTo(x, y);
                          event.updateAfterEvent();
                  }
  
                  
Handler for when the mouse is released on the stage. This stops the drawing.
parameter: event Event fired by stage.

  
                  private function onStageMouseUp(event:MouseEvent):void {
                          _drawing = false;
                          stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                  }
  
          }
  
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.