| 1 |
private const DEBUG:Boolean = true; |
|---|
| 2 |
|
|---|
| 3 |
import flash.events.Event; |
|---|
| 4 |
import flash.events.MouseEvent; |
|---|
| 5 |
|
|---|
| 6 |
import gunyarapaint.Com; |
|---|
| 7 |
import gunyarapaint.entities.GPPen; |
|---|
| 8 |
import gunyarapaint.controls.GPCanvas; |
|---|
| 9 |
|
|---|
| 10 |
import mx.controls.Alert; |
|---|
| 11 |
import mx.events.FlexEvent; |
|---|
| 12 |
import mx.events.NumericStepperEvent; |
|---|
| 13 |
import mx.events.SliderEvent; |
|---|
| 14 |
import mx.managers.PopUpManager; |
|---|
| 15 |
|
|---|
| 16 |
private var gpCanvas:GPCanvas; |
|---|
| 17 |
private var basex:uint, basey:uint, baseWidth:uint, baseHeight:uint; |
|---|
| 18 |
|
|---|
| 19 |
private var comm:Com; |
|---|
| 20 |
private var oekakiId:uint; |
|---|
| 21 |
private var redirectUrl:String; |
|---|
| 22 |
private var onPost:Boolean = false; |
|---|
| 23 |
|
|---|
| 24 |
private var canvasWidth:uint; |
|---|
| 25 |
private var canvasHeight:uint; |
|---|
| 26 |
private var undoBufferSize:uint; |
|---|
| 27 |
|
|---|
| 28 |
private const ALERT_TITLE:String = 'お絵カキコ'; |
|---|
| 29 |
private const MAX_CANVAS_WIDTH:uint = 500; |
|---|
| 30 |
private const MAX_CANVAS_HEIGHT:uint = 500; |
|---|
| 31 |
private const MIN_CANVAS_WIDTH:uint = 16; |
|---|
| 32 |
private const MIN_CANVAS_HEIGHT:uint = 16; |
|---|
| 33 |
|
|---|
| 34 |
public function init():void { |
|---|
| 35 |
|
|---|
| 36 |
if (DEBUG) { |
|---|
| 37 |
versionLabel.text += 'debug'; |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
this.enabled = false; |
|---|
| 41 |
comm = new Com(); |
|---|
| 42 |
comm.addEventListener('completeGetData', commCompleteHandler); |
|---|
| 43 |
|
|---|
| 44 |
if (parameters['postUrl'] && parameters['cookie'] && parameters['magic'] && parameters['redirectUrl']) { |
|---|
| 45 |
comm.setPostEnv(parameters['postUrl'], parameters['cookie'], parameters['magic']); |
|---|
| 46 |
postOekakiButton.enabled = true; |
|---|
| 47 |
redirectUrl = parameters['redirectUrl']; |
|---|
| 48 |
} |
|---|
| 49 |
if (parameters['undoBufferSize']) { |
|---|
| 50 |
undoBufferSize = int(parameters['undoBufferSize']); |
|---|
| 51 |
if (undoBufferSize < 0) { |
|---|
| 52 |
Alert.show('最大アンドゥ回数が少なすぎます。', ALERT_TITLE); |
|---|
| 53 |
} |
|---|
| 54 |
if (undoBufferSize > 32) { |
|---|
| 55 |
Alert.show('最大アンドゥ回数が多すぎます。', ALERT_TITLE); |
|---|
| 56 |
} |
|---|
| 57 |
} else { |
|---|
| 58 |
if (DEBUG) { |
|---|
| 59 |
undoBufferSize = 16; |
|---|
| 60 |
} else { |
|---|
| 61 |
return; |
|---|
| 62 |
} |
|---|
| 63 |
} |
|---|
| 64 |
if (parameters['oekakiId'] && parameters['baseImgUrl']) { |
|---|
| 65 |
oekakiId = uint(parameters['oekakiId']); |
|---|
| 66 |
var url:String = parameters['baseImgUrl']; |
|---|
| 67 |
comm.loadUrl(url); |
|---|
| 68 |
this.enabled = false; |
|---|
| 69 |
} else { |
|---|
| 70 |
if (parameters['canvasWidth'] && parameters['canvasHeight']) { |
|---|
| 71 |
canvasWidth = int(parameters['canvasWidth']); |
|---|
| 72 |
canvasHeight = int(parameters['canvasHeight']); |
|---|
| 73 |
if (canvasWidth < MIN_CANVAS_WIDTH || canvasHeight < MIN_CANVAS_HEIGHT) { |
|---|
| 74 |
Alert.show('キャンバスサイズが小さすぎます。', ALERT_TITLE); |
|---|
| 75 |
return; |
|---|
| 76 |
} |
|---|
| 77 |
if (canvasWidth > MAX_CANVAS_WIDTH || canvasHeight > MAX_CANVAS_HEIGHT) { |
|---|
| 78 |
Alert.show('キャンバスサイズが大きすぎます。', ALERT_TITLE); |
|---|
| 79 |
return; |
|---|
| 80 |
} |
|---|
| 81 |
} else { |
|---|
| 82 |
if (DEBUG) { |
|---|
| 83 |
canvasWidth = 300; canvasHeight = 400; |
|---|
| 84 |
} else { |
|---|
| 85 |
return; |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
gpCanvas = gpCanvasWindow.createCanvas(canvasWidth, canvasHeight, undoBufferSize, null, null, penDetailWindow.pen); |
|---|
| 89 |
initGunyaraPaintCanvas(); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
// canvasのpopup |
|---|
| 93 |
PopUpManager.addPopUp(gpCanvasWindow, this); |
|---|
| 94 |
|
|---|
| 95 |
// ペン詳細 |
|---|
| 96 |
PopUpManager.addPopUp(penDetailWindow, this); |
|---|
| 97 |
// FIXME: 最終的にはWindowのほうがイベント投げる |
|---|
| 98 |
penDetailWindow.penDetail.addEventListener('changePen', changePenHandler); |
|---|
| 99 |
|
|---|
| 100 |
// キャンバス回転スライダ |
|---|
| 101 |
canvasRotate.addEventListener(SliderEvent.CHANGE, canvasRotateHandler); |
|---|
| 102 |
canvasRotate.addEventListener(SliderEvent.THUMB_DRAG, canvasRotateHandler); |
|---|
| 103 |
|
|---|
| 104 |
// アンドゥ・リドゥ |
|---|
| 105 |
undoButton.addEventListener(FlexEvent.BUTTON_DOWN, undoButtonHandler); |
|---|
| 106 |
redoButton.addEventListener(FlexEvent.BUTTON_DOWN, redoButtonHandler); |
|---|
| 107 |
|
|---|
| 108 |
// 補助線 |
|---|
| 109 |
additionalNumberStepper.addEventListener(NumericStepperEvent.CHANGE, additionalNumberStepperHandler); |
|---|
| 110 |
additionalBoxCheckBox.addEventListener(Event.CHANGE, additionalBoxCheckBoxHandler); |
|---|
| 111 |
additionalSkewCheckBox.addEventListener(Event.CHANGE, additionalSkewCheckBoxHandler); |
|---|
| 112 |
|
|---|
| 113 |
postOekakiButton.addEventListener(FlexEvent.BUTTON_DOWN, postOekakiButtonHandler); |
|---|
| 114 |
|
|---|
| 115 |
this.enabled = true; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
private function initGunyaraPaintCanvas():void { |
|---|
| 119 |
// undo/redoの状態変化 |
|---|
| 120 |
gpCanvas.addEventListener('changeUndoRedo', changeUndoRedoHandler); |
|---|
| 121 |
gpCanvas.rotation = 0; |
|---|
| 122 |
|
|---|
| 123 |
// マウスボタンが上がったとき、canvasに教えてあげる |
|---|
| 124 |
this.addEventListener(MouseEvent.MOUSE_UP, gpCanvas.externalMouseUp); |
|---|
| 125 |
|
|---|
| 126 |
toolCanvas.x = (this.width - toolCanvas.width) / 2; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
private function commCompleteHandler(evt:Event):void { |
|---|
| 130 |
if (onPost) { |
|---|
| 131 |
onPost = false; |
|---|
| 132 |
if (comm.getErrorStr()) { |
|---|
| 133 |
// error |
|---|
| 134 |
Alert.show(comm.getErrorStr(), ALERT_TITLE); |
|---|
| 135 |
this.enabled = true; |
|---|
| 136 |
} else if (comm.getStringData() != '') { |
|---|
| 137 |
Alert.show(comm.getStringData(), ALERT_TITLE); |
|---|
| 138 |
this.enabled = true; |
|---|
| 139 |
} else { |
|---|
| 140 |
// redirect |
|---|
| 141 |
Com.redirect(redirectUrl); |
|---|
| 142 |
} |
|---|
| 143 |
} else { |
|---|
| 144 |
// 続きを書く |
|---|
| 145 |
var baseImg:BitmapData = Bitmap(comm.getContent()).bitmapData; |
|---|
| 146 |
canvasWidth = baseImg.width; |
|---|
| 147 |
canvasHeight = baseImg.height; |
|---|
| 148 |
gpCanvas = gpCanvasWindow.createCanvas(0, 0, undoBufferSize, null, baseImg, penDetailWindow.pen); |
|---|
| 149 |
initGunyaraPaintCanvas(); |
|---|
| 150 |
this.enabled = true; |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
private function canvasRotateHandler(evt:SliderEvent):void { |
|---|
| 155 |
// changeGunyaraPaintRect(); |
|---|
| 156 |
} |
|---|
| 157 |
private function changeGunyaraPaintRect():void { |
|---|
| 158 |
// 外部のスクリプト呼び出し。主に回転時のサイズ変更 |
|---|
| 159 |
if (this.width < (toolCanvas.width + 4)) { |
|---|
| 160 |
this.width = toolCanvas.width + 4; |
|---|
| 161 |
} |
|---|
| 162 |
if (this.height < 312) { // for color picker |
|---|
| 163 |
this.height = 312; |
|---|
| 164 |
} |
|---|
| 165 |
if (ExternalInterface.available) { |
|---|
| 166 |
try { |
|---|
| 167 |
ExternalInterface.call("changeGunyaraPaintRect", this.width, this.height); |
|---|
| 168 |
} catch (e:SecurityError) { |
|---|
| 169 |
} catch (e:Error) { |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
private function undoButtonHandler(evt:FlexEvent):void { |
|---|
| 175 |
gpCanvas.undo(); |
|---|
| 176 |
} |
|---|
| 177 |
private function redoButtonHandler(evt:FlexEvent):void { |
|---|
| 178 |
gpCanvas.redo(); |
|---|
| 179 |
} |
|---|
| 180 |
private function additionalNumberStepperHandler(evt:NumericStepperEvent):void { |
|---|
| 181 |
gpCanvas.setAdditionalNumber(evt.value); |
|---|
| 182 |
} |
|---|
| 183 |
private function additionalBoxCheckBoxHandler(evt:Event):void { |
|---|
| 184 |
gpCanvas.setAdditionalBox(evt.target.selected); |
|---|
| 185 |
} |
|---|
| 186 |
private function additionalSkewCheckBoxHandler(evt:Event):void { |
|---|
| 187 |
gpCanvas.setAdditionalSkew(evt.target.selected); |
|---|
| 188 |
} |
|---|
| 189 |
private function changeUndoRedoHandler(evt:Event):void { |
|---|
| 190 |
var undoCount:Number = gpCanvas.getUndoCount(); |
|---|
| 191 |
var redoCount:Number = gpCanvas.getRedoCount(); |
|---|
| 192 |
if (undoCount > 0) { |
|---|
| 193 |
undoButton.label = 'アンドゥ (' + undoCount + ')'; |
|---|
| 194 |
undoButton.enabled = true; |
|---|
| 195 |
} else { |
|---|
| 196 |
undoButton.label = 'アンドゥ'; |
|---|
| 197 |
undoButton.enabled = false; |
|---|
| 198 |
} |
|---|
| 199 |
if (redoCount > 0) { |
|---|
| 200 |
redoButton.label = 'リドゥ (' + redoCount + ')'; |
|---|
| 201 |
redoButton.enabled = true; |
|---|
| 202 |
} else { |
|---|
| 203 |
redoButton.label = 'リドゥ'; |
|---|
| 204 |
redoButton.enabled = false; |
|---|
| 205 |
} |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
private function postOekakiButtonHandler(evt:Event):void { |
|---|
| 209 |
if (messageTextArea.text == '') { |
|---|
| 210 |
Alert.show('書き込みが空です。絵のタイトルなどの情報を書き込んでください。', ALERT_TITLE); |
|---|
| 211 |
return; |
|---|
| 212 |
} |
|---|
| 213 |
if (gpCanvas.getLogger().getLogCount() == 0) { |
|---|
| 214 |
Alert.show('絵が描かれていません。お絵かきしてください。', ALERT_TITLE); |
|---|
| 215 |
return; |
|---|
| 216 |
} |
|---|
| 217 |
this.enabled = false; |
|---|
| 218 |
onPost = true; |
|---|
| 219 |
comm.postOekaki(fromTextInput.text, |
|---|
| 220 |
messageTextArea.text, |
|---|
| 221 |
gpCanvas.getPNGImage(), |
|---|
| 222 |
gpCanvas.getCompressedLog(), |
|---|
| 223 |
oekakiId); |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
private function changePenHandler(evt:Event):void { |
|---|
| 227 |
gpCanvas.pen = penDetailWindow.pen; |
|---|
| 228 |
} |
|---|