<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="930" height="690" creationComplete="init()" viewSourceURL="srcview/index.html">
<mx:SoundEffect id="ding_snd" source="@Embed(source='Microwave Bing.mp3')"/>
<mx:Script>
<![CDATA[
import mx.events.ColorPickerEvent;
import mx.events.SliderEvent;
import mx.controls.Alert;
import org.alivepdf.saving.Method;
import org.alivepdf.events.ProcessingEvent;
import org.alivepdf.layout.Size;
import org.alivepdf.layout.Unit;
import org.alivepdf.layout.Orientation;
import org.alivepdf.pdf.PDF;
private const HOMEPAGE:String = "http://www.yahoo.co.jp/";
private var container:Sprite;
private var captureData:BitmapData;
private var board:Sprite;
private var g:Graphics;
private var penSize:uint;
private var penColor:uint;
private var file:File;
private function init():void {
htmlContent.addEventListener(Event.LOCATION_CHANGE, locationChangeHandler);
urlInput.text = HOMEPAGE;
changePage();
captureData = new BitmapData(450, 600, false, 0xffffff);
container = new Sprite();
container.addChild(new Bitmap(captureData));
board = new Sprite();
g = board.graphics;
setLineStyle();
container.addChild(board);
preview.source = container;
}
private function showScroll(event:FocusEvent):void {
}
private function hideScroll(event:FocusEvent):void {
}
private function locationChangeHandler(event:Event):void {
urlInput.text = htmlContent.location;
}
private function changePage():void {
htmlContent.location = urlInput.text;
}
private function capture():void {
captureData.lock();
captureData.draw(htmlContent);
captureData.unlock();
}
private function save():void {
file = File.desktopDirectory;
file.browseForSave("ファイルを保存");
file.addEventListener(Event.SELECT, fileSelectHandler);
}
private function fileSelectHandler(event:Event):void {
var myPDF:PDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
myPDF.addEventListener(ProcessingEvent.COMPLETE, createPDFHandler);
myPDF.addPage();
myPDF.addImage(preview, 5, 5, 120, 160);
try {
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
var bytes:ByteArray = myPDF.save(Method.LOCAL);
stream.writeBytes(bytes);
} catch(e:Error) {
Alert.show("書き込みに失敗しました");
} finally {
stream.close();
}
}
private function createPDFHandler(event:ProcessingEvent):void {
ding_snd.sound.play();
}
private function changeColor(event:ColorPickerEvent):void {
setLineStyle();
}
private function changeSize(event:SliderEvent):void {
setLineStyle();
}
private function setLineStyle():void {
penSize = slider.value;
penColor = cp.selectedColor;
g.lineStyle(penSize, penColor);
}
private function clear():void {
g.clear();
setLineStyle();
}
private function drawStart(event:MouseEvent):void {
container.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
g.moveTo(board.mouseX, board.mouseY);
}
private function drawStop(event:MouseEvent):void {
container.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
}
private function moveHandler(event:MouseEvent):void {
g.lineTo(board.mouseX, board.mouseY);
}
]]>
</mx:Script>
<mx:HBox width="100%" height="100%" top="10" left="10" right="10" bottom="10">
<mx:VBox width="100%" height="100%">
<mx:HBox width="100%" height="100%">
<mx:TextInput id="urlInput" enter="changePage()" width="100%"/>
<mx:Button label="Go" click="changePage()"/>
</mx:HBox>
<mx:HTML id="htmlContent" width="450" height="600" focusIn="showScroll(event)" focusOut="hideScroll(event)" horizontalScrollPolicy="off" verticalScrollPolicy="off"/>
</mx:VBox>
<mx:VBox width="100%" height="100%" verticalAlign="middle">
<mx:HBox width="100%" height="100%">
<mx:Button label="capture" click="capture()"/>
<mx:Spacer width="10" />
<mx:Label text="pen size"/>
<mx:HSlider id="slider" thumbRelease="changeSize(event)" minimum="1" maximum="10" snapInterval="1" width="80"/>
<mx:ColorPicker id="cp" change="changeColor(event)" selectedColor="0xff0000" showTextField="false"/>
<mx:Button label="clear" click="clear()"/>
<mx:Spacer/>
<mx:Spacer width="100%" />
<mx:Button label="save" click="save()" />
</mx:HBox>
<mx:Image id="preview" width="450" height="600" mouseDown="drawStart(event)" mouseUp="drawStop(event)"/>
</mx:VBox>
</mx:HBox>
</mx:WindowedApplication>