| 1 |
package org.libspark.gunyarapaint.framework |
|---|
| 2 |
{ |
|---|
| 3 |
import flash.utils.ByteArray; |
|---|
| 4 |
import flash.utils.Endian; |
|---|
| 5 |
|
|---|
| 6 |
import org.libspark.gunyarapaint.framework.commands.ICommand; |
|---|
| 7 |
import org.libspark.gunyarapaint.framework.events.CommandEvent; |
|---|
| 8 |
|
|---|
| 9 |
public final class Recorder extends CanvasContext |
|---|
| 10 |
{ |
|---|
| 11 |
public static const DEFAULT_UNDO_MAX:uint = 16; |
|---|
| 12 |
|
|---|
| 13 |
public function Recorder(bytes:ByteArray, commands:CommandCollection = null) |
|---|
| 14 |
{ |
|---|
| 15 |
bytes.endian = Endian.BIG_ENDIAN; |
|---|
| 16 |
bytes.position = 0; |
|---|
| 17 |
m_bytes = bytes; |
|---|
| 18 |
m_command = commands ? commands : new CommandCollection(); |
|---|
| 19 |
super(); |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
/** |
|---|
| 23 |
* 画像の大きさを設定し、ヘッダーに書き込む |
|---|
| 24 |
* |
|---|
| 25 |
* @param width 画像の幅 |
|---|
| 26 |
* @param height 画像の高さ |
|---|
| 27 |
* @param undo やり直しできる回数 |
|---|
| 28 |
*/ |
|---|
| 29 |
public function prepare(width:int, height:int, undo:int):void |
|---|
| 30 |
{ |
|---|
| 31 |
m_command.loadCommands(); |
|---|
| 32 |
writeHeader(PAINTER_LOG_VERSION, width, height, undo); |
|---|
| 33 |
createPainter(width, height, undo); |
|---|
| 34 |
setUndo(new UndoStack(painter, undo)); |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
/** |
|---|
| 38 |
* ログヘッダーを書き出す |
|---|
| 39 |
* |
|---|
| 40 |
* @param version ログのバージョン番号 |
|---|
| 41 |
* @param width 画像の幅 |
|---|
| 42 |
* @param height 画像の高さ |
|---|
| 43 |
* @param undo やり直しできる回数 |
|---|
| 44 |
*/ |
|---|
| 45 |
private function writeHeader(version:uint, width:uint, height:uint, undo:uint):void |
|---|
| 46 |
{ |
|---|
| 47 |
var signature:String = "GUNYARA_PAINT:" |
|---|
| 48 |
+ (version / 100) + ":" |
|---|
| 49 |
+ ((version % 100) / 10) + ":" |
|---|
| 50 |
+ (version % 10) + ":" |
|---|
| 51 |
m_bytes.writeUTFBytes(signature); |
|---|
| 52 |
m_bytes.writeShort(width); |
|---|
| 53 |
m_bytes.writeShort(height); |
|---|
| 54 |
m_bytes.writeShort(undo); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
/** |
|---|
| 58 |
* コマンドの書き出し及び実行を同時に行う |
|---|
| 59 |
* |
|---|
| 60 |
* @param command コマンドオブジェクト |
|---|
| 61 |
* @param args コマンドに対する引数 |
|---|
| 62 |
*/ |
|---|
| 63 |
public function commitCommand(id:uint, args:Object):void |
|---|
| 64 |
{ |
|---|
| 65 |
var command:ICommand = m_command.getCommand(id); |
|---|
| 66 |
command.write(m_bytes, args); |
|---|
| 67 |
command.execute(this); |
|---|
| 68 |
if (hasEventListener(CommandEvent.COMMITTED)) |
|---|
| 69 |
dispatchEvent(new CommandEvent(CommandEvent.COMMITTED, command)); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
private var m_command:CommandCollection; |
|---|
| 73 |
private var m_bytes:ByteArray; |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|