| 1 |
import flash.events.Event; |
|---|
| 2 |
|
|---|
| 3 |
import gunyarapaint.Canvas; |
|---|
| 4 |
import gunyarapaint.Com; |
|---|
| 5 |
|
|---|
| 6 |
import mx.controls.Alert; |
|---|
| 7 |
import mx.events.ColorPickerEvent; |
|---|
| 8 |
import mx.events.FlexEvent; |
|---|
| 9 |
import mx.events.ListEvent; |
|---|
| 10 |
import mx.events.NumericStepperEvent; |
|---|
| 11 |
import mx.events.SliderEvent; |
|---|
| 12 |
|
|---|
| 13 |
private var gpCanvas:gunyarapaint.Canvas; |
|---|
| 14 |
private var basex:uint, basey:uint, baseWidth:uint, baseHeight:uint; |
|---|
| 15 |
|
|---|
| 16 |
private var comm:Com; |
|---|
| 17 |
private var oekakiId:uint; |
|---|
| 18 |
private var redirectUrl:String; |
|---|
| 19 |
private var onPost:Boolean = false; |
|---|
| 20 |
|
|---|
| 21 |
private var canvasWidth:uint; |
|---|
| 22 |
private var canvasHeight:uint; |
|---|
| 23 |
private var undoBufferSize:uint; |
|---|
| 24 |
|
|---|
| 25 |
private const ALERT_TITLE:String = 'お絵カキコ'; |
|---|
| 26 |
private const DEBUG:Boolean = false; |
|---|
| 27 |
|
|---|
| 28 |
public function init():void { |
|---|
| 29 |
// 通信系 |
|---|
| 30 |
this.enabled = false; |
|---|
| 31 |
comm = new Com(); |
|---|
| 32 |
comm.addEventListener('completeGetData', commCompleteHandler); |
|---|
| 33 |
|
|---|
| 34 |
if (parameters['postUrl'] && parameters['cookie'] && parameters['magic'] && parameters['redirectUrl']) { |
|---|
| 35 |
comm.setPostEnv(parameters['postUrl'], parameters['cookie'], parameters['magic']); |
|---|
| 36 |
postOekakiButton.enabled = true; |
|---|
| 37 |
redirectUrl = parameters['redirectUrl']; |
|---|
| 38 |
} |
|---|
| 39 |
if (parameters['undoBufferSize']) { |
|---|
| 40 |
undoBufferSize = int(parameters['undoBufferSize']); |
|---|
| 41 |
if (undoBufferSize < 0) { |
|---|
| 42 |
Alert.show('最大アンドゥ回数が少なすぎます。', ALERT_TITLE); |
|---|
| 43 |
} |
|---|
| 44 |
if (undoBufferSize > 32) { |
|---|
| 45 |
Alert.show('最大アンドゥ回数が多すぎます。', ALERT_TITLE); |
|---|
| 46 |
} |
|---|
| 47 |
} else { |
|---|
| 48 |
if (DEBUG) { |
|---|
| 49 |
undoBufferSize = 16; |
|---|
| 50 |
} else { |
|---|
| 51 |
return; |
|---|
| 52 |
} |
|---|
| 53 |
} |
|---|
| 54 |
if (parameters['oekakiId'] && parameters['baseImgUrl']) { |
|---|
| 55 |
oekakiId = uint(parameters['oekakiId']); |
|---|
| 56 |
var url:String = parameters['baseImgUrl']; |
|---|
| 57 |
comm.loadUrl(url); |
|---|
| 58 |
this.enabled = false; |
|---|
| 59 |
} else { |
|---|
| 60 |
if (parameters['canvasWidth'] && parameters['canvasHeight']) { |
|---|
| 61 |
canvasWidth = int(parameters['canvasWidth']); |
|---|
| 62 |
canvasHeight = int(parameters['canvasHeight']); |
|---|
| 63 |
if (canvasWidth < 16 || canvasHeight < 16) { |
|---|
| 64 |
Alert.show('キャンバスサイズが小さすぎます。', ALERT_TITLE); |
|---|
| 65 |
return; |
|---|
| 66 |
} |
|---|
| 67 |
if (canvasWidth > 600 || canvasHeight > 600) { |
|---|
| 68 |
Alert.show('キャンバスサイズが大きすぎます。', ALERT_TITLE); |
|---|
| 69 |
return; |
|---|
| 70 |
} |
|---|
| 71 |
} else { |
|---|
| 72 |
if (DEBUG) { |
|---|
| 73 |
canvasWidth = 300; canvasHeight = 400; |
|---|
| 74 |
} else { |
|---|
| 75 |
return; |
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|
| 78 |
gpCanvas = new gunyarapaint.Canvas(canvasWidth, canvasHeight, undoBufferSize, null, null); |
|---|
| 79 |
initGunyaraPaintCanvas(); |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
// 太さ |
|---|
| 83 |
thicknessStepper.addEventListener(NumericStepperEvent.CHANGE, thicknessStepperHandler); |
|---|
| 84 |
// カラーピッカ |
|---|
| 85 |
colorPicker.addEventListener(ColorPickerEvent.CHANGE, colorPickerHandler); |
|---|
| 86 |
// スポイト |
|---|
| 87 |
dropperButton.addEventListener(Event.CHANGE, dropperButtonHandler); |
|---|
| 88 |
// 透明度 |
|---|
| 89 |
alphaSlider.addEventListener(SliderEvent.CHANGE, alphaSliderHandler); |
|---|
| 90 |
// キャンバス回転 |
|---|
| 91 |
canvasRotate.addEventListener(SliderEvent.CHANGE, canvasRotateHandler); |
|---|
| 92 |
canvasRotate.addEventListener(SliderEvent.THUMB_DRAG, canvasRotateHandler); |
|---|
| 93 |
// 回転のときに勝手にApplicationの領域が拡大しないように |
|---|
| 94 |
canvas.explicitWidth = 0; |
|---|
| 95 |
canvas.explicitHeight = 0; |
|---|
| 96 |
|
|---|
| 97 |
// ブレンドモード |
|---|
| 98 |
blendModeComboBox.dataProvider = [ |
|---|
| 99 |
{label: '通常', data: flash.display.BlendMode.NORMAL}, |
|---|
| 100 |
// {label: '消去', data: flash.display.BlendMode.ERASE}, |
|---|
| 101 |
{label: '比較(暗)', data: flash.display.BlendMode.DARKEN}, |
|---|
| 102 |
{label: '乗算', data: flash.display.BlendMode.MULTIPLY}, |
|---|
| 103 |
{label: '比較(明)', data: flash.display.BlendMode.LIGHTEN}, |
|---|
| 104 |
{label: 'スクリーン', data: flash.display.BlendMode.SCREEN}, |
|---|
| 105 |
{label: '覆い焼き', data: flash.display.BlendMode.ADD}, |
|---|
| 106 |
{label: 'オーバーレイ', data: flash.display.BlendMode.OVERLAY}, |
|---|
| 107 |
{label: 'ハードライト', data: flash.display.BlendMode.HARDLIGHT}, |
|---|
| 108 |
{label: '差の絶対値', data: flash.display.BlendMode.DIFFERENCE}, |
|---|
| 109 |
{label: '除外', data: flash.display.BlendMode.SUBTRACT}, |
|---|
| 110 |
{label: '反転', data: flash.display.BlendMode.INVERT}]; |
|---|
| 111 |
blendModeComboBox.addEventListener(ListEvent.CHANGE, blendModeComboBoxHandler); |
|---|
| 112 |
|
|---|
| 113 |
// アンドゥ・リドゥ |
|---|
| 114 |
undoButton.addEventListener(FlexEvent.BUTTON_DOWN, undoButtonHandler); |
|---|
| 115 |
redoButton.addEventListener(FlexEvent.BUTTON_DOWN, redoButtonHandler); |
|---|
| 116 |
|
|---|
| 117 |
// 補助線 |
|---|
| 118 |
additionalNumberStepper.addEventListener(NumericStepperEvent.CHANGE, additionalNumberStepperHandler); |
|---|
| 119 |
additionalBoxCheckBox.addEventListener(Event.CHANGE, additionalBoxCheckBoxHandler); |
|---|
| 120 |
additionalSkewCheckBox.addEventListener(Event.CHANGE, additionalSkewCheckBoxHandler); |
|---|
| 121 |
|
|---|
| 122 |
postOekakiButton.addEventListener(FlexEvent.BUTTON_DOWN, postOekakiButtonHandler); |
|---|
| 123 |
|
|---|
| 124 |
this.enabled = true; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
private function initGunyaraPaintCanvas():void { |
|---|
| 128 |
canvas.width = canvasWidth; |
|---|
| 129 |
canvas.height = canvasHeight; |
|---|
| 130 |
canvas.addChild(gpCanvas); |
|---|
| 131 |
// スポイトで色取ったイベント |
|---|
| 132 |
gpCanvas.addEventListener('changeColor', changeColorHandler); |
|---|
| 133 |
// undo/redoの状態変化 |
|---|
| 134 |
gpCanvas.addEventListener('changeUndoRedo', changeUndoRedoHandler); |
|---|
| 135 |
gpCanvas.rotation = 0; |
|---|
| 136 |
|
|---|
| 137 |
this.width = canvasWidth + 20; |
|---|
| 138 |
this.height = canvasHeight + canvas.y + 10; |
|---|
| 139 |
|
|---|
| 140 |
canvas.x = 10; |
|---|
| 141 |
basex = canvas.x; |
|---|
| 142 |
basey = canvas.y; |
|---|
| 143 |
changeGunyaraPaintRect(); |
|---|
| 144 |
|
|---|
| 145 |
// マウスボタンが上がったとき、canvasに教えてあげる |
|---|
| 146 |
this.addEventListener(MouseEvent.MOUSE_UP, gpCanvas.externalMouseUp); |
|---|
| 147 |
|
|---|
| 148 |
toolCanvas.x = (this.width - toolCanvas.width) / 2; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
private function commCompleteHandler(evt:Event):void { |
|---|
| 152 |
if (onPost) { |
|---|
| 153 |
onPost = false; |
|---|
| 154 |
if (comm.getErrorStr()) { |
|---|
| 155 |
// error |
|---|
| 156 |
Alert.show(comm.getErrorStr(), ALERT_TITLE); |
|---|
| 157 |
this.enabled = true; |
|---|
| 158 |
} else if (comm.getStringData() != '') { |
|---|
| 159 |
Alert.show(comm.getStringData(), ALERT_TITLE); |
|---|
| 160 |
this.enabled = true; |
|---|
| 161 |
} else { |
|---|
| 162 |
// redirect |
|---|
| 163 |
Com.redirect(redirectUrl); |
|---|
| 164 |
} |
|---|
| 165 |
} else { |
|---|
| 166 |
var baseImg:BitmapData = Bitmap(comm.getContent()).bitmapData; |
|---|
| 167 |
canvasWidth = baseImg.width; |
|---|
| 168 |
canvasHeight = baseImg.height; |
|---|
| 169 |
gpCanvas = new gunyarapaint.Canvas(0, 0, undoBufferSize, null, baseImg); |
|---|
| 170 |
initGunyaraPaintCanvas(); |
|---|
| 171 |
this.enabled = true; |
|---|
| 172 |
} |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
private function thicknessStepperHandler(evt:NumericStepperEvent):void { |
|---|
| 176 |
gpCanvas.setLineThickness(evt.value); |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
private function colorPickerHandler(evt:ColorPickerEvent):void { |
|---|
| 180 |
gpCanvas.setLineColor(evt.target.selectedColor); |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
private function alphaSliderHandler(evt:SliderEvent):void { |
|---|
| 184 |
gpCanvas.setLineAlpha(evt.value); |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
private function canvasRotateHandler(evt:SliderEvent):void { |
|---|
| 188 |
gpCanvas.rotation = evt.value; |
|---|
| 189 |
|
|---|
| 190 |
var deg:int = gpCanvas.rotation; |
|---|
| 191 |
var rad:Number = deg * Math.PI / 180; |
|---|
| 192 |
// TODO: dasai |
|---|
| 193 |
if (deg >= 0) { |
|---|
| 194 |
if (deg <= 90) { |
|---|
| 195 |
canvas.x = Math.sin(rad) * canvasHeight + 10; |
|---|
| 196 |
canvas.y = basey; |
|---|
| 197 |
this.width = canvasHeight * Math.sin(rad) + |
|---|
| 198 |
canvasWidth * Math.cos(rad) + 20; |
|---|
| 199 |
this.height = canvasHeight * Math.cos(rad) + |
|---|
| 200 |
canvasWidth * Math.abs(Math.sin(rad)) + basey + 10; |
|---|
| 201 |
} else { |
|---|
| 202 |
canvas.x = canvasHeight * Math.sin(rad) - Math.cos(rad) * canvasWidth + 10; |
|---|
| 203 |
canvas.y = basey - Math.cos(rad) * canvasHeight; |
|---|
| 204 |
this.width = canvasHeight * Math.sin(rad) - Math.cos(rad) * canvasWidth + 20; |
|---|
| 205 |
this.height = -canvasHeight * Math.cos(rad) + |
|---|
| 206 |
canvasWidth * Math.sin(rad) + basey + 10; |
|---|
| 207 |
} |
|---|
| 208 |
} else { |
|---|
| 209 |
if (deg >= -90) { |
|---|
| 210 |
canvas.x = basex; |
|---|
| 211 |
canvas.y = basey - Math.sin(rad) * canvasWidth; |
|---|
| 212 |
this.width = canvasHeight * Math.sin(Math.abs(rad)) + |
|---|
| 213 |
canvasWidth * Math.cos(rad) + 20; |
|---|
| 214 |
this.height = canvasHeight * Math.cos(rad) + |
|---|
| 215 |
canvasWidth * Math.abs(Math.sin(rad)) + basey + 10; |
|---|
| 216 |
} else { |
|---|
| 217 |
canvas.x = basex - Math.cos(rad) * canvasWidth; |
|---|
| 218 |
canvas.y = basey - Math.sin(rad) * canvasWidth - Math.cos(rad) * canvasHeight; |
|---|
| 219 |
this.width = basex - Math.cos(rad) * canvasWidth - Math.sin(rad) * canvasHeight + 20; |
|---|
| 220 |
this.height = basey - Math.sin(rad) * canvasWidth - Math.cos(rad) * canvasHeight + 10; |
|---|
| 221 |
} |
|---|
| 222 |
} |
|---|
| 223 |
changeGunyaraPaintRect(); |
|---|
| 224 |
} |
|---|
| 225 |
private function changeGunyaraPaintRect():void { |
|---|
| 226 |
// 外部のスクリプト呼び出し。主に回転時のサイズ変更 |
|---|
| 227 |
if (this.width < (toolCanvas.width + 4)) { |
|---|
| 228 |
this.width = toolCanvas.width + 4; |
|---|
| 229 |
} |
|---|
| 230 |
if (this.height < 312) { // for color picker |
|---|
| 231 |
this.height = 312; |
|---|
| 232 |
} |
|---|
| 233 |
if (ExternalInterface.available) { |
|---|
| 234 |
try { |
|---|
| 235 |
ExternalInterface.call("changeGunyaraPaintRect", this.width, this.height); |
|---|
| 236 |
} catch (e:SecurityError) { |
|---|
| 237 |
} catch (e:Error) { |
|---|
| 238 |
} |
|---|
| 239 |
} |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
private function blendModeComboBoxHandler(evt:ListEvent):void { |
|---|
| 243 |
gpCanvas.setLineBlendMode(String(evt.currentTarget.value)); |
|---|
| 244 |
} |
|---|
| 245 |
private function undoButtonHandler(evt:FlexEvent):void { |
|---|
| 246 |
gpCanvas.undo(); |
|---|
| 247 |
} |
|---|
| 248 |
private function redoButtonHandler(evt:FlexEvent):void { |
|---|
| 249 |
gpCanvas.redo(); |
|---|
| 250 |
} |
|---|
| 251 |
private function dropperButtonHandler(evt:Event):void { |
|---|
| 252 |
if (evt.target.selected) { |
|---|
| 253 |
gpCanvas.setPenMode(gpCanvas.MODE_DROPPER); |
|---|
| 254 |
} else { |
|---|
| 255 |
gpCanvas.setPenMode(gpCanvas.MODE_PEN); |
|---|
| 256 |
} |
|---|
| 257 |
} |
|---|
| 258 |
private function changeColorHandler(evt:Event):void { |
|---|
| 259 |
colorPicker.selectedColor = gpCanvas.getLineColor(); |
|---|
| 260 |
alphaSlider.value = 1.0; |
|---|
| 261 |
} |
|---|
| 262 |
private function additionalNumberStepperHandler(evt:NumericStepperEvent):void { |
|---|
| 263 |
gpCanvas.setAdditionalNumber(evt.value); |
|---|
| 264 |
} |
|---|
| 265 |
private function additionalBoxCheckBoxHandler(evt:Event):void { |
|---|
| 266 |
gpCanvas.setAdditionalBox(evt.target.selected); |
|---|
| 267 |
} |
|---|
| 268 |
private function additionalSkewCheckBoxHandler(evt:Event):void { |
|---|
| 269 |
gpCanvas.setAdditionalSkew(evt.target.selected); |
|---|
| 270 |
} |
|---|
| 271 |
private function changeUndoRedoHandler(evt:Event):void { |
|---|
| 272 |
var undoCount:Number = gpCanvas.getUndoCount(); |
|---|
| 273 |
var redoCount:Number = gpCanvas.getRedoCount(); |
|---|
| 274 |
if (undoCount > 0) { |
|---|
| 275 |
undoButton.label = 'アンドゥ (' + undoCount + ')'; |
|---|
| 276 |
undoButton.enabled = true; |
|---|
| 277 |
} else { |
|---|
| 278 |
undoButton.label = 'アンドゥ'; |
|---|
| 279 |
undoButton.enabled = false; |
|---|
| 280 |
} |
|---|
| 281 |
if (redoCount > 0) { |
|---|
| 282 |
redoButton.label = 'リドゥ (' + redoCount + ')'; |
|---|
| 283 |
redoButton.enabled = true; |
|---|
| 284 |
} else { |
|---|
| 285 |
redoButton.label = 'リドゥ'; |
|---|
| 286 |
redoButton.enabled = false; |
|---|
| 287 |
} |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
private function postOekakiButtonHandler(evt:Event):void { |
|---|
| 291 |
if (messageTextArea.text == '') { |
|---|
| 292 |
Alert.show('書き込みが空です。絵のタイトルなどの情報を書き込んでください。', ALERT_TITLE); |
|---|
| 293 |
return; |
|---|
| 294 |
} |
|---|
| 295 |
this.enabled = false; |
|---|
| 296 |
onPost = true; |
|---|
| 297 |
comm.postOekaki(fromTextInput.text, |
|---|
| 298 |
messageTextArea.text, |
|---|
| 299 |
gpCanvas.getPNGImage(), |
|---|
| 300 |
gpCanvas.getCompressedLog(), |
|---|
| 301 |
oekakiId); |
|---|
| 302 |
} |
|---|