1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| package
{
import flash.display.*;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.ui.Keyboard;
/**
* @author Lorenzo
*
* Test des nouvelles possibilités de l'API de dessin de Flash 10 -> drawGraphicsData + GraphicsPath + drawPath + GraphicsStroke + ....
*
* -on dessine ce que l'on veut
* -on appuye DELETE pour effacer le dessin
* -on appuye sur SPACE pour recreer le dessin
* -on appuye sur ENTER pour tout remettre a zero
*
* une miniature du dessin est affiché en bas a droite
*/
public class Main5 extends Sprite
{
private static const THICKNESS:uint = 1;
private static const COLOR:uint = 0xFF0000;
private static const MAX_LIGNES:uint = 1000;
private var _nbLignes:uint = 0;
private var _txCode:TextField;
private var _vecCommandes:Vector.<int> = new Vector.<int>();
private var _vecPositions:Vector.<Number> = new Vector.<Number>();
private var _shapeMiniature:Shape;
private var _shapeMiniatureContour:Shape;
private var _path:GraphicsPath = new GraphicsPath(new Vector.<int>(), new Vector.<Number>());
public function Main5():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
trace("INIT -> " + new Date());
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = "showAll";
this._txCode = new TextField();
this._txCode.width = 100;
this._txCode.height = 800;
this._txCode.background = true;
this._txCode.defaultTextFormat = new TextFormat("Arial", 11);
this._txCode.mouseWheelEnabled = true;
this.addChild(this._txCode);
this.graphics.lineStyle(THICKNESS, COLOR);
// conteneur miniature
this._shapeMiniatureContour = new Shape();
this._shapeMiniatureContour.graphics.lineStyle(1, 0xFFFFFF, 0.5);
this._shapeMiniatureContour.graphics.beginFill(0xAAAAAA, 0.5);
this._shapeMiniatureContour.graphics.drawRect(0, 0, 200, 200);
this._shapeMiniatureContour.x = 600;
this._shapeMiniatureContour.y = 400;
this.addChild(this._shapeMiniatureContour);
this._shapeMiniature = new Shape();
this._shapeMiniature.x = 600;
this._shapeMiniature.y = 400;
this.addChild(this._shapeMiniature);
stage.addEventListener(MouseEvent.MOUSE_DOWN, evtStageMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, evtStageMouseUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, evtStageKeyDown);
}
private function evtStageMouseDown(event:MouseEvent):void
{
if ( this._nbLignes >= MAX_LIGNES ) {
return;
}
this.graphics.moveTo(this.mouseX, this.mouseY);
this._vecCommandes.push(1);
this._vecPositions.push(this.mouseX, this.mouseY);
this._path.commands.push(1);
this._path.data.push(this.mouseX, this.mouseY);
this._txCode.appendText("C = 1 -> " + this.mouseX + " x " + this.mouseY + "\n");
stage.addEventListener(MouseEvent.MOUSE_MOVE, evtStageMouseMove);
}
private function evtStageMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, evtStageMouseMove);
}
private function evtStageMouseMove(event:MouseEvent):void
{
if ( this._nbLignes >= MAX_LIGNES ) {
return;
}
this.graphics.lineTo(this.mouseX, this.mouseY);
this._vecCommandes.push(2);
this._vecPositions.push(this.mouseX, this.mouseY);
this._path.commands.push(2);
this._path.data.push(this.mouseX, this.mouseY);
this._txCode.appendText("C = 2 -> " + this.mouseX + " x " + this.mouseY + "\n");
this._nbLignes++;
}
private function evtStageKeyDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.SPACE :
// recreer le dessin a partir des données GraphicsPath
var stroke:GraphicsStroke = new GraphicsStroke(THICKNESS);
stroke.fill = new GraphicsSolidFill(COLOR);
var drawing:Vector.<IGraphicsData> = new Vector.<IGraphicsData>();
drawing.push(stroke, this._path);
this.graphics.drawGraphicsData(drawing);
// pareil mais en miniature et en utilisant drawPath
var tbPos:Vector.<Number> = new Vector.<Number>();
var nbPos:uint = this._path.data.length;
for (var i:int = 0; i < nbPos; i++)
{
tbPos[i] = i % 2 ? this._path.data[i] / 3 : (this._path.data[i] - 100) / 3.5;
trace(this._path.data[i] + " -> " + tbPos[i]);
}
this._shapeMiniature.graphics.lineStyle(THICKNESS, COLOR);
this._shapeMiniature.graphics.drawPath(this._vecCommandes, tbPos);
break;
case Keyboard.ENTER :
// effacer le code
this._txCode.text = "";
this._vecCommandes = new Vector.<int>();
this._vecPositions = new Vector.<Number>();
this._path = new GraphicsPath(new Vector.<int>(), new Vector.<Number>());
case Keyboard.DELETE :
// effacer le dessin
this.graphics.clear();
this.graphics.lineStyle(THICKNESS, COLOR);
this._shapeMiniature.graphics.clear();
break;
}
}
}
} |