Sunteți pe pagina 1din 6

13

/* Pencil Tool shape, everything drawn with this tool and the eraser tool is
stored inside board.pencilDraw */
var pencilDraw:Shape = new Shape();
public function Main():void
{
textformat.font = "Quicksand Bold Regular"; // You can use any font you
like
textformat.bold = true;
textformat.size = 16;
/* We create these functions later */
convertToBMD();
addListeners();
/* Hide tools highlights */
pencil.visible = false;
hideTools(eraser, txt);
}

16
private function PencilTool(e:MouseEvent):void
{
/* Quit active tool */
quitActiveTool(); //This function will be created later
/* Set to Active */
active = "Pencil"; //Sets the active variable to
"Pencil"
ct.color = activeColor;
shapeSize.transform.colorTransform = ct;
}

-------private function startPencilTool(e:MouseEvent):void


{
pencilDraw = new Shape(); //We add a new shape to draw always in top (in
case of text, or eraser drawings)
board.addChild(pencilDraw); //Add that shape to the board MovieClip
pencilDraw.graphics.moveTo(mouseX, mouseY); //Moves the Drawing Position
to the Mouse Position

pencilDraw.graphics.lineStyle(shapeSize.width, activeColor);//Sets the


line thickness to the ShapeSize MovieClip size and sets its color to the
current active color
board.addEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool); //Adds a
listener to the next function
}

--------private function drawPencilTool(e:MouseEvent):void


{
pencilDraw.graphics.lineTo(mouseX, mouseY); //Draws a line from the
current Mouse position to the moved Mouse position
}

---------private function stopPencilTool(e:MouseEvent):void


{
board.removeEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool); //Stops
the drawing
}

17
private function EraserTool(e:MouseEvent):void
{
/* Quit active tool */
quitActiveTool();
/* Set to Active */
active = "Eraser";
/* Listeners */
board.addEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);
board.addEventListener(MouseEvent.MOUSE_UP, stopEraserTool);
/* Highlight */
highlightTool(eraser);
hideTools(pencil, txt);
/* Use White Color */
ct.color = 0x000000;
shapeSize.transform.colorTransform = ct;

}
private function startEraserTool(e:MouseEvent):void
{
pencilDraw = new Shape();
board.addChild(pencilDraw);
pencilDraw.graphics.moveTo(mouseX, mouseY);
pencilDraw.graphics.lineStyle(shapeSize.width, 0xFFFFFF); //White Color
board.addEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);
}
private function drawEraserTool(e:MouseEvent):void
{
pencilDraw.graphics.lineTo(mouseX, mouseY);
}
function stopEraserTool(e:MouseEvent):void
{
board.removeEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);
}

18
private function TextTool(e:MouseEvent):void
{
/* Quit active tool */
quitActiveTool();
/* Set to Active */
active = "Text";
/* Listener */
board.addEventListener(MouseEvent.MOUSE_UP, writeText);
/* Highlight */
highlightTool(txt);
hideTools(pencil, eraser);
}
private function writeText(e:MouseEvent):void
{
/* Create a new TextField Object, this way we won't replace older
instances */
var textfield = new TextField();
/* Set Formats, position, and focus */

textfield.type = TextFieldType.INPUT;
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.defaultTextFormat = textformat;
textfield.textColor = activeColor;
textfield.x = mouseX;
textfield.y = mouseY;
stage.focus = textfield;
/* Add text to Board */
board.addChild(textfield);
}

19
private function export():void
{
var bmd:BitmapData = new BitmapData(600, 290);//Creates a new BitmapData
with the board size
bmd.draw(board);//Draws the board MovieClip into a BitmapImage in the
BitmapData

private function saveSuccessful(e:Event):void


{
saveDialog = new SaveDialog();// Instantiates a new SaveDialog Class
addChild(saveDialog); //Adds the SaveDialog MovieClip to the Stage
saveDialog.closeBtn.addEventListener(MouseEvent.MOUSE_UP,
closeSaveDialog);//Adds a listener to the close button of the dialog
}
private function closeSaveDialog(e:MouseEvent):void
{
removeChild(saveDialog); //Removes the dialog of the Stage
}
private function save(e:MouseEvent):void
{
export(); //Calls the export function to begin the saving process
}

20
private function clearBoard(e:MouseEvent):void
{
/* Create a white rectangle on top of everything */
var blank:Shape = new Shape();

blank.graphics.beginFill(0xFFFFFF);
blank.graphics.drawRect(0, 0, board.width, board.height);
blank.graphics.endFill();
board.addChild(blank);
}

21
private function convertToBMD():void
{
colorsBmd = new BitmapData(colors.width,colors.height);
colorsBmd.draw(colors);
}
private function chooseColor(e:MouseEvent):void
{
pixelValue = colorsBmd.getPixel(colors.mouseX,colors.mouseY);//Gets the
cliked pixel RGB value
activeColor = pixelValue;
/* Use a ColorTransform object to change the shapeSize MovieClip color */
ct.color = activeColor;
shapeSize.transform.colorTransform = ct;
}

22
private function quitActiveTool():void
{
switch (active)
{
case "Pencil" :
board.removeEventListener(MouseEvent.MOUSE_DOWN,
startPencilTool);
board.removeEventListener(MouseEvent.MOUSE_UP, stopPencilTool);
case "Eraser" :
board.removeEventListener(MouseEvent.MOUSE_DOWN,
startEraserTool);
board.removeEventListener(MouseEvent.MOUSE_UP, stopEraserTool);
case "Text" :
board.removeEventListener(MouseEvent.MOUSE_UP, writeText);
default :
}
}

------private function highlightTool(tool:DisplayObject):void

{
tool.visible=true; //Highlights tool in the parameter
}
/* Hides the tools in the parameters */
private function hideTools(tool1:DisplayObject, tool2:DisplayObject):void
{
tool1.visible=false;
tool2.visible=false;
}

23
private function changeShapeSize(e:MouseEvent):void
{
if (shapeSize.width >= 50)
{
shapeSize.width = 1;
shapeSize.height = 1;
/* TextFormat */
textformat.size = 16;
}
else
{
shapeSize.width += 5;
shapeSize.height=shapeSize.width;
/* TextFormat */
textformat.size+=5;
}
}

24
private function addListeners():void
{
pencilTool.addEventListener(MouseEvent.MOUSE_UP, PencilTool);
eraserTool.addEventListener(MouseEvent.MOUSE_UP, EraserTool);
textTool.addEventListener(MouseEvent.MOUSE_UP, TextTool);
saveButton.addEventListener(MouseEvent.MOUSE_UP, save);
clearTool.addEventListener(MouseEvent.MOUSE_UP, clearBoard);
colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
sizePanel.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);
shapeSize.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);
}

S-ar putea să vă placă și