/**
* Copyright tjoen ( http://wonderfl.net/user/tjoen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/itwu
*/
package {
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.display.Loader;
import flash.events.Event;
import flash.events.*;
import flash.ui.Keyboard;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.system.System;
import flash.net.FileReference;
import flash.utils.ByteArray;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import com.bit101.components.PushButton;
[SWF(backgroundColor = "0x3388ff", width = 860, height = 600, frameRate = 30)]
public class FlashTest extends Sprite {
public var _fm:TextFormat = new TextFormat("_typewriter",14,0x000000, true); //0xCACF43
public var _tf3:TextField;
public var temporaryDrawing:Shape = new Shape();
public var myDrawing:Sprite;
public var mouseHolding:Boolean = false;
public var clickedX:Number;
public var clickedY:Number;
public var drawMode:String = "ellip";
public var NumCirc:Number = 0;
public var holder:Boolean = true;
public var naMe:String;
public var strContent:String = "";
public var ptRotationPoint2:Point;
public var f:FileReference;
public var img:MovieClip;
public var loader:Loader;
public function FlashTest() {
AddTxt();
var loadBtn:PushButton = new PushButton(this, 0, 0, "load Image", openFile);
stage.addEventListener(MouseEvent.CLICK, mouseClickLoc);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveLoc);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
}
public function openFile(e:Event):void{
f = new FileReference();
f.addEventListener(Event.SELECT, loadFile);
f.addEventListener(ProgressEvent.PROGRESS, onProgress);
f.addEventListener(Event.COMPLETE, loaded);
f.browse();
}
public function loadFile(e:Event):void {
f.load();
}
private function onProgress(e:ProgressEvent):void
{
_tf3.text = "Loading:" + e.bytesLoaded + "bytes van totaal:" + e.bytesTotal;
}
public function loaded(e:Event):void{
// var b:ByteArray = f.data;
// img.data = b;
loader= new Loader();
loader.loadBytes(e.target.data);
addChild(loader);
//loader.width = 400;
//loader.height = 400;
loader.x = 20;
loader.y = 30;
f.removeEventListener(Event.COMPLETE, loaded);
_tf3.text = e.target.name;
// Bitmap(img.content).bitmapData
//bvar image:BitmapData = Bitmap(img.content).bitmapData;
//_loadBitmap.draw(img, new Matrix(scale, 0, 0, scale));
}
public function Setupeditor():void
{
clickedX = mouseX;
clickedY = mouseY;
addChild(temporaryDrawing);
temporaryDrawing.graphics.lineStyle(1, 0x00FFFF, 1);
myDrawing = new Sprite();
myDrawing.name = "pnt"+NumCirc+"-"+ clickedX+"-"+ clickedY;
addChild(myDrawing);
myDrawing.graphics.lineStyle(1, 0x000000, 1);
myDrawing.addEventListener("mouseOver", function():void {
myDrawing.buttonMode = true;
myDrawing.useHandCursor = true;
holder = false;
});
myDrawing.addEventListener("mouseDown", function(e:MouseEvent):void {
myDrawing.buttonMode = true;
myDrawing.useHandCursor = true;
e.currentTarget.startDrag();
holder = false;
});
myDrawing.addEventListener("mouseOut", function():void {
holder = true;
});
myDrawing.addEventListener("mouseUp", function(e:MouseEvent):void {
e.currentTarget.stopDrag();
var theContent:String = e.currentTarget.name;
var contentArray:Array = theContent.split("-");
var btName:String = contentArray[0];
var btNx:Number = contentArray[1];
var btNy:Number = contentArray[2];
_tf3.text = "\t<" + btName + ">" + Math.round(btNx+e.currentTarget.x) + "," + Math.round(btNy+e.currentTarget.y) + "," + Math.round(e.currentTarget.width) + "," + Math.round(e.currentTarget.height) + ","+ Math.round(e.currentTarget.rotation) + "</" + btName + ">\n";
System.setClipboard(_tf3.text );
ptRotationPoint2 = new Point(Math.round(btNx+e.currentTarget.x) + Math.round(e.currentTarget.width / 2), Math.round(btNy+e.currentTarget.y) + Math.round(e.currentTarget.height /2));
naMe = e.currentTarget.name;
});
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode == Keyboard.C){
drawMode = "ellip";
}
if (e.keyCode == Keyboard.B){
drawMode = "rect";
}
if (e.keyCode == Keyboard.SPACE){
removeChild(getChildByName(naMe))
}
if (e.keyCode == Keyboard.LEFT){
rotateAroundCenter(getChildByName(naMe), rotation+1, ptRotationPoint2);
}
if (e.keyCode == Keyboard.RIGHT){
rotateAroundCenter(getChildByName(naMe), rotation-1, ptRotationPoint2);
}
}
}
public function mDown(MouseEvent:Event):void
{
if (holder)Setupeditor();
mouseHolding = true;
myDrawing.graphics.moveTo(mouseX, mouseY);
temporaryDrawing.graphics.moveTo(mouseX, mouseY);
}
public function mUp(MouseEvent:Event):void
{
if (holder && mouseY < 500)
{
switch (drawMode)
{
case "line":
myDrawing.graphics.lineTo(mouseX, mouseY);
break;
case "rect":
myDrawing.graphics.beginFill(0x222222, 0.5);
myDrawing.graphics.drawRect(clickedX, clickedY, mouseX - clickedX, mouseY - clickedY);
myDrawing.graphics.endFill();
var twidth2:Number = mouseX - clickedX;
var theight2:Number = mouseY - clickedY;
_tf3.htmlText = "myDrawing.graphics.drawRect(" + clickedX + "," + clickedY + "," + twidth2 + "," + theight2 + ");";
NumCirc++;
break;
case "ellip":
myDrawing.graphics.beginFill(0x222222, 0.5);
myDrawing.graphics.drawEllipse(clickedX, clickedY, mouseX - clickedX, mouseY - clickedY);
myDrawing.graphics.endFill();
var twidth:Number = mouseX - clickedX;
var theight:Number = mouseY - clickedY;
_tf3.htmlText = "myDrawing.graphics.drawEllipse(" + clickedX + "," + clickedY + "," + twidth + "," + theight + ");";
//_tf3.text = "\t<pnt" + NumCirc + ">" + clickedX + "," + clickedY + "," + twidth + "," + theight + ",0</pnt" + NumCirc + ">\n";
NumCirc++;
break;
}
}
mouseHolding = false;
clearTemp();
}
public function clearTemp():void
{
temporaryDrawing.graphics.clear();
temporaryDrawing.graphics.lineStyle(2, 0x666666, 1);
temporaryDrawing.graphics.moveTo(clickedX, clickedY);
}
public function mouseClickLoc(evt:MouseEvent):void
{
//_tf2.htmlText = "The mouse click at: " + mouseX + "," + mouseY;
}
// Keep tracking the location of moving Mouse Pointer
public function mouseMoveLoc(motion:MouseEvent):void
{
if (holder && mouseHolding && mouseY < 500)
{
clearTemp();
switch (drawMode)
{
case "line":
temporaryDrawing.graphics.lineTo(mouseX, mouseY);
break;
case "pen":
myDrawing.graphics.lineTo(mouseX, mouseY);
break;
case "rect":
temporaryDrawing.graphics.drawRect(clickedX, clickedY, mouseX - clickedX, mouseY - clickedY);
break;
case "ellip":
temporaryDrawing.graphics.drawEllipse(clickedX, clickedY, mouseX - clickedX, mouseY - clickedY);
break;
}
}
}
public function rotateAroundCenter(ob:*, angleDegrees:Number, ptRotationPoint:Point):void
{
var m:Matrix = ob.transform.matrix;
m.tx -= ptRotationPoint.x;
m.ty -= ptRotationPoint.y;
m.rotate(angleDegrees * (Math.PI / 180));
m.tx += ptRotationPoint.x;
m.ty += ptRotationPoint.y;
ob.transform.matrix = m;
}
public function AddTxt():void {
_fm.align = TextFormatAlign.LEFT;
_fm.kerning = true;
_fm.letterSpacing = 1;
_tf3 = new TextField();
_tf3.autoSize = "left";
//_tf3.selectable = false;
_tf3.multiline = true;
_tf3.defaultTextFormat = _fm;
_tf3.htmlText = "Waiting for input...";
addChild(_tf3);
_tf3.x = 10;
_tf3.y = 510;
}
}
}