| 1 |
package org.libspark.gunyarapaint.framework.modules |
|---|
| 2 |
{ |
|---|
| 3 |
import flash.errors.IllegalOperationError; |
|---|
| 4 |
|
|---|
| 5 |
import org.libspark.gunyarapaint.framework.LayerBitmap; |
|---|
| 6 |
import org.libspark.gunyarapaint.framework.Painter; |
|---|
| 7 |
import org.libspark.gunyarapaint.framework.Recorder; |
|---|
| 8 |
import org.libspark.gunyarapaint.framework.commands.HorizontalMirrorCommand; |
|---|
| 9 |
import org.libspark.gunyarapaint.framework.commands.PenCommand; |
|---|
| 10 |
import org.libspark.gunyarapaint.framework.commands.RedoCommand; |
|---|
| 11 |
import org.libspark.gunyarapaint.framework.commands.UndoCommand; |
|---|
| 12 |
import org.libspark.gunyarapaint.framework.commands.VerticalMirrorCommand; |
|---|
| 13 |
import org.libspark.gunyarapaint.framework.commands.layer.CopyLayerCommand; |
|---|
| 14 |
import org.libspark.gunyarapaint.framework.commands.layer.CreateLayerCommand; |
|---|
| 15 |
import org.libspark.gunyarapaint.framework.commands.layer.MergeLayerCommand; |
|---|
| 16 |
import org.libspark.gunyarapaint.framework.commands.layer.RemoveLayerCommand; |
|---|
| 17 |
import org.libspark.gunyarapaint.framework.commands.layer.SetLayerAlphaCommand; |
|---|
| 18 |
import org.libspark.gunyarapaint.framework.commands.layer.SetLayerBlendModeCommand; |
|---|
| 19 |
import org.libspark.gunyarapaint.framework.commands.layer.SetLayerIndexCommand; |
|---|
| 20 |
import org.libspark.gunyarapaint.framework.commands.layer.SwapLayerCommand; |
|---|
| 21 |
|
|---|
| 22 |
internal class DrawModule |
|---|
| 23 |
{ |
|---|
| 24 |
public function DrawModule(recorder:Recorder) |
|---|
| 25 |
{ |
|---|
| 26 |
m_recorder = recorder; |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
public function undo():void |
|---|
| 30 |
{ |
|---|
| 31 |
m_recorder.commitCommand(UndoCommand.ID, {}); |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
public function redo():void |
|---|
| 35 |
{ |
|---|
| 36 |
m_recorder.commitCommand(RedoCommand.ID, {}); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
public function horizontalMirror(index:uint):void |
|---|
| 40 |
{ |
|---|
| 41 |
m_recorder.commitCommand( |
|---|
| 42 |
HorizontalMirrorCommand.ID, |
|---|
| 43 |
{ |
|---|
| 44 |
"index": index |
|---|
| 45 |
} |
|---|
| 46 |
); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
public function verticalMirror(index:uint):void |
|---|
| 50 |
{ |
|---|
| 51 |
m_recorder.commitCommand( |
|---|
| 52 |
VerticalMirrorCommand.ID, |
|---|
| 53 |
{ |
|---|
| 54 |
"index": index |
|---|
| 55 |
} |
|---|
| 56 |
); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
public function copyLayer():void |
|---|
| 60 |
{ |
|---|
| 61 |
m_recorder.commitCommand( |
|---|
| 62 |
CopyLayerCommand.ID, |
|---|
| 63 |
{} |
|---|
| 64 |
); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
public function createLayer():void |
|---|
| 68 |
{ |
|---|
| 69 |
m_recorder.commitCommand( |
|---|
| 70 |
CreateLayerCommand.ID, |
|---|
| 71 |
{} |
|---|
| 72 |
); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
public function mergeLayers():void |
|---|
| 76 |
{ |
|---|
| 77 |
m_recorder.commitCommand( |
|---|
| 78 |
MergeLayerCommand.ID, |
|---|
| 79 |
{} |
|---|
| 80 |
); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
public function removeLayer():void |
|---|
| 84 |
{ |
|---|
| 85 |
m_recorder.commitCommand( |
|---|
| 86 |
RemoveLayerCommand.ID, |
|---|
| 87 |
{} |
|---|
| 88 |
); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
public function swapLayers(from:uint, to:uint):void |
|---|
| 92 |
{ |
|---|
| 93 |
m_recorder.commitCommand( |
|---|
| 94 |
SwapLayerCommand.ID, |
|---|
| 95 |
{ |
|---|
| 96 |
"from": from, |
|---|
| 97 |
"to": to |
|---|
| 98 |
} |
|---|
| 99 |
); |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
/** |
|---|
| 103 |
* 最後に移動した座標を保存する |
|---|
| 104 |
* |
|---|
| 105 |
* @param x |
|---|
| 106 |
* @param y |
|---|
| 107 |
*/ |
|---|
| 108 |
public function saveCoordinate(x:Number, y:Number):void |
|---|
| 109 |
{ |
|---|
| 110 |
s_coordinateXWithButtonDown = s_coordinateX; |
|---|
| 111 |
s_coordinateYWithButtonDown = s_coordinateY; |
|---|
| 112 |
s_coordinateXWithButtonUp = x; |
|---|
| 113 |
s_coordinateYWithButtonUp = y; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
/** |
|---|
| 117 |
* 現在のレイヤーの状態を検証する |
|---|
| 118 |
* |
|---|
| 119 |
* 以下の状態であれば IllegalOperationError を送出する |
|---|
| 120 |
* - 現在のレイヤーが不可視 |
|---|
| 121 |
* - 現在のレイヤーがロックされている |
|---|
| 122 |
* |
|---|
| 123 |
*/ |
|---|
| 124 |
protected function validateLayerState():void |
|---|
| 125 |
{ |
|---|
| 126 |
var layer:LayerBitmap = m_recorder.painter.layers.currentLayer; |
|---|
| 127 |
if (!layer.visible) |
|---|
| 128 |
throw new IllegalOperationError(); |
|---|
| 129 |
else if (layer.locked) |
|---|
| 130 |
throw new IllegalOperationError(); |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
/** |
|---|
| 134 |
* 現在の座標を設定する |
|---|
| 135 |
* |
|---|
| 136 |
* @param x |
|---|
| 137 |
* @param y |
|---|
| 138 |
*/ |
|---|
| 139 |
protected function setCoordinate(x:Number, y:Number):void |
|---|
| 140 |
{ |
|---|
| 141 |
if (s_shouldStartAfterDrawing) { |
|---|
| 142 |
s_coordinateX = s_coordinateXWithButtonUp; |
|---|
| 143 |
s_coordinateY = s_coordinateYWithButtonUp; |
|---|
| 144 |
} |
|---|
| 145 |
else if (s_shouldStartBeforeDrawing) { |
|---|
| 146 |
s_coordinateX = s_coordinateXWithButtonDown; |
|---|
| 147 |
s_coordinateY = s_coordinateYWithButtonDown; |
|---|
| 148 |
} |
|---|
| 149 |
else { |
|---|
| 150 |
s_coordinateX = x; |
|---|
| 151 |
s_coordinateY = y; |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
/** |
|---|
| 156 |
* 指定された座標が現在の座標と一致するかを確認する |
|---|
| 157 |
* |
|---|
| 158 |
* @param x |
|---|
| 159 |
* @param y |
|---|
| 160 |
* @return 同じである場合は true |
|---|
| 161 |
*/ |
|---|
| 162 |
protected function equalsCoordinate(x:Number, y:Number):Boolean |
|---|
| 163 |
{ |
|---|
| 164 |
var painter:Painter = m_recorder.painter; |
|---|
| 165 |
return painter.roundPixel(x) == painter.roundPixel(s_coordinateX) && |
|---|
| 166 |
painter.roundPixel(y) == painter.roundPixel(s_coordinateY); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
/** |
|---|
| 170 |
* 現在の座標から ICommand#write に渡す引数に変換する |
|---|
| 171 |
* |
|---|
| 172 |
*/ |
|---|
| 173 |
protected function getArgumentsFromCurrentCoordinate():Object |
|---|
| 174 |
{ |
|---|
| 175 |
return getArgumentsFromCoordinate(s_coordinateX, s_coordinateY); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
/** |
|---|
| 179 |
* 指定された座標から ICommand#write に渡す引数に変換する |
|---|
| 180 |
* |
|---|
| 181 |
* @param x |
|---|
| 182 |
* @param y |
|---|
| 183 |
* @return args |
|---|
| 184 |
*/ |
|---|
| 185 |
protected function getArgumentsFromCoordinate(x:Number, y:Number):Object |
|---|
| 186 |
{ |
|---|
| 187 |
var painter:Painter = m_recorder.painter; |
|---|
| 188 |
var args:Object = { |
|---|
| 189 |
"x": painter.roundPixel(x), |
|---|
| 190 |
"y": painter.roundPixel(y) |
|---|
| 191 |
}; |
|---|
| 192 |
return args; |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
/** |
|---|
| 196 |
* 描写処理を終了する |
|---|
| 197 |
* |
|---|
| 198 |
*/ |
|---|
| 199 |
protected function stopDrawing():void |
|---|
| 200 |
{ |
|---|
| 201 |
m_recorder.painter.stopDrawingSession(); |
|---|
| 202 |
m_drawing = false; |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
/** |
|---|
| 206 |
* A ボタンがクリックされているかを設定する |
|---|
| 207 |
* |
|---|
| 208 |
* @param value |
|---|
| 209 |
*/ |
|---|
| 210 |
public function set keyA(value:Boolean):void |
|---|
| 211 |
{ |
|---|
| 212 |
s_keyA = value; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
/** |
|---|
| 216 |
* Q ボタンがクリックされているかを設定する |
|---|
| 217 |
* |
|---|
| 218 |
* @param value |
|---|
| 219 |
*/ |
|---|
| 220 |
public function set keyQ(value:Boolean):void |
|---|
| 221 |
{ |
|---|
| 222 |
s_keyQ = value; |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
/** |
|---|
| 226 |
* 開始座標を描写終了後の座標に設定するかどうか (R) |
|---|
| 227 |
* |
|---|
| 228 |
* @param value |
|---|
| 229 |
*/ |
|---|
| 230 |
public function set shouldStartAfterDrawing(value:Boolean):void |
|---|
| 231 |
{ |
|---|
| 232 |
s_shouldStartAfterDrawing = value; |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
/** |
|---|
| 236 |
* 開始座標を描写開始時の座標に設定するかどうか (T) |
|---|
| 237 |
* |
|---|
| 238 |
* @param value |
|---|
| 239 |
*/ |
|---|
| 240 |
public function set shouldStartBeforeDrawing(value:Boolean):void |
|---|
| 241 |
{ |
|---|
| 242 |
s_shouldStartBeforeDrawing = value; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
public function set alpha(value:Number):void |
|---|
| 246 |
{ |
|---|
| 247 |
m_recorder.commitCommand( |
|---|
| 248 |
PenCommand.ID, |
|---|
| 249 |
{ |
|---|
| 250 |
"type": PenCommand.ALPHA, |
|---|
| 251 |
"alpha": value |
|---|
| 252 |
} |
|---|
| 253 |
); |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
public function set blendMode(value:String):void |
|---|
| 257 |
{ |
|---|
| 258 |
|
|---|
| 259 |
m_recorder.commitCommand( |
|---|
| 260 |
PenCommand.ID, |
|---|
| 261 |
{ |
|---|
| 262 |
"type": PenCommand.BLEND_MODE, |
|---|
| 263 |
"blendMode": value |
|---|
| 264 |
} |
|---|
| 265 |
); |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
public function set color(value:uint):void |
|---|
| 269 |
{ |
|---|
| 270 |
m_recorder.commitCommand( |
|---|
| 271 |
PenCommand.ID, |
|---|
| 272 |
{ |
|---|
| 273 |
"type": PenCommand.COLOR, |
|---|
| 274 |
"color": value |
|---|
| 275 |
} |
|---|
| 276 |
); |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
public function set thickness(value:uint):void |
|---|
| 280 |
{ |
|---|
| 281 |
m_recorder.commitCommand( |
|---|
| 282 |
PenCommand.ID, |
|---|
| 283 |
{ |
|---|
| 284 |
"type": PenCommand.THICKNESS, |
|---|
| 285 |
"thickness": value |
|---|
| 286 |
} |
|---|
| 287 |
); |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
public function set layerAlpha(value:Number):void |
|---|
| 291 |
{ |
|---|
| 292 |
m_recorder.commitCommand( |
|---|
| 293 |
SetLayerAlphaCommand.ID, |
|---|
| 294 |
{ |
|---|
| 295 |
"alpha": value |
|---|
| 296 |
} |
|---|
| 297 |
); |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
public function set layerBlendMode(value:String):void |
|---|
| 301 |
{ |
|---|
| 302 |
m_recorder.commitCommand( |
|---|
| 303 |
SetLayerBlendModeCommand.ID, |
|---|
| 304 |
{ |
|---|
| 305 |
"blendMode": value |
|---|
| 306 |
} |
|---|
| 307 |
); |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
public function set layerIndex(value:uint):void |
|---|
| 311 |
{ |
|---|
| 312 |
m_recorder.commitCommand( |
|---|
| 313 |
SetLayerIndexCommand.ID, |
|---|
| 314 |
{ |
|---|
| 315 |
"index": value |
|---|
| 316 |
} |
|---|
| 317 |
); |
|---|
| 318 |
} |
|---|
| 319 |
|
|---|
| 320 |
protected function get coordinateX():Number |
|---|
| 321 |
{ |
|---|
| 322 |
return s_coordinateX; |
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
protected function get coordinateY():Number |
|---|
| 326 |
{ |
|---|
| 327 |
return s_coordinateY; |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
protected function get key_A():Boolean |
|---|
| 331 |
{ |
|---|
| 332 |
return s_keyA; |
|---|
| 333 |
} |
|---|
| 334 |
|
|---|
| 335 |
protected function get key_Q():Boolean |
|---|
| 336 |
{ |
|---|
| 337 |
return s_keyQ; |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
private static var s_coordinateX:Number = 0; |
|---|
| 341 |
private static var s_coordinateY:Number = 0; |
|---|
| 342 |
private static var s_coordinateXWithButtonUp:Number = 0; |
|---|
| 343 |
private static var s_coordinateYWithButtonUp:Number = 0; |
|---|
| 344 |
private static var s_coordinateXWithButtonDown:Number = 0; |
|---|
| 345 |
private static var s_coordinateYWithButtonDown:Number = 0; |
|---|
| 346 |
private static var s_keyA:Boolean = false; |
|---|
| 347 |
private static var s_keyQ:Boolean = false; |
|---|
| 348 |
private static var s_shouldStartBeforeDrawing:Boolean = false; |
|---|
| 349 |
private static var s_shouldStartAfterDrawing:Boolean = false; |
|---|
| 350 |
protected var m_recorder:Recorder; |
|---|
| 351 |
protected var m_drawing:Boolean; |
|---|
| 352 |
protected var m_drawingLine:Boolean; |
|---|
| 353 |
} |
|---|
| 354 |
} |
|---|