チェンジセット 1081
- コミット日時:
- 2008/08/27 18:15:19 (5 年前)
- ファイル:
凡例:
- 変更無し
- 追加
- 削除
- 更新
- コピー
- 移動
as3/gunyarapaint/trunk/gunyarapaint/src/gunyarapaint.as
r1071 r1081 2 2 3 3 import gunyarapaint.Canvas; 4 import gunyarapaint.Com;5 4 6 5 import mx.events.ColorPickerEvent; … … 9 8 import mx.events.NumericStepperEvent; 10 9 import mx.events.SliderEvent; 11 import mx.managers.PopUpManager;12 10 13 11 private var gpCanvas:gunyarapaint.Canvas; … … 18 16 19 17 public function init():void { 20 gpCanvas = new gunyarapaint.Canvas(CANVAS_WIDTH, CANVAS_HEIGHT, 12 );18 gpCanvas = new gunyarapaint.Canvas(CANVAS_WIDTH, CANVAS_HEIGHT, 12, true); 21 19 // gpCanvas.scrollRect = new Rectangle(0, 0, 300, 300); 22 20 canvas.addChild(gpCanvas); … … 68 66 69 67 postOekakiButton.addEventListener(FlexEvent.BUTTON_DOWN, postOekakiButtonHandler); 68 69 // ログ再生 70 playLogButton.addEventListener(FlexEvent.BUTTON_DOWN, playLogButtonHandler); 70 71 71 72 // いろんな初期値を保存… … … 164 165 } 165 166 166 167 private function playLogButtonHandler(evt:Event):void { 168 gpCanvas.playLog(); 169 } as3/gunyarapaint/trunk/gunyarapaint/src/gunyarapaint.mxml
r1072 r1081 34 34 <mx:Canvas id="canvas" x="114" width="400" height="300" y="172"></mx:Canvas> 35 35 <mx:Button id="postOekakiButton" x="510" y="7" label="お絵カキコする!" fillAlphas="[1.0, 1.0, 1.0, 1.0]" fillColors="[#FFFFFF, #FF9999, #FFFFFF, #FFCCCC]"/> 36 <mx:Button id="playLogButton" x="396" y="7" label="ログ再生"/> 36 37 </mx:Application> as3/gunyarapaint/trunk/gunyarapaint/src/gunyarapaint/Canvas.as
r1080 r1081 32 32 private var additionalNumber:uint = 4; // 補助線の分割数 33 33 34 private var history:Array; // 描画コマンド履歴34 private var log:Logger; // 描画コマンド履歴 35 35 36 public function Canvas(width:uint, height:uint, undoBufferSize:uint) 36 private var canWidth:uint, canHeight:uint; 37 38 public function Canvas(width:uint, height:uint, undoBufferSize:uint, saveLog:Boolean) 37 39 { 40 canWidth = width; 41 canHeight = height; 38 42 // ビットマップ 39 data = new BitmapData(width, height);43 createBitmapData(); 40 44 createBitmap(); 41 45 … … 64 68 this.undoBufferSize = undoBufferSize; 65 69 66 history = new Array(); 70 if (saveLog){ 71 log = new Logger(width, height, undoBufferSize); 72 } 67 73 68 74 super(); 69 75 } 70 76 77 private function createBitmapData():void { 78 data = new BitmapData(canWidth, canHeight); 79 } 71 80 private function createBitmap():void { 72 81 if (bitmap) { … … 108 117 private var lineMiterLimit:Number = 3; 109 118 119 private function logWrite(info:Array):void { 120 if (log) { 121 log.write(info); 122 } 123 } 124 110 125 public function setLineThickness(t:uint):void { 111 126 this.lineThickness = t; 112 history.push(['lineStyle', 'thickness', t])127 logWrite(['lineStyle', 'thickness', t]) 113 128 } 114 129 public function setLineColor(color:uint):void { 115 130 this.lineColor = color; 116 history.push(['lineStyle', 'color', color])131 logWrite(['lineStyle', 'color', color]) 117 132 } 118 133 public function setLineAlpha(alpha:Number):void { 119 134 this.lineAlpha = alpha; 120 history.push(['lineStyle', 'alpha', alpha])135 logWrite(['lineStyle', 'alpha', alpha]) 121 136 } 122 137 public function setLineBlendMode(blend:String):void { 123 138 this.lineBlendMode = blend; 124 history.push(['lineStyle', 'blendMode', blend])139 logWrite(['lineStyle', 'blendMode', blend]) 125 140 } 126 141 public function setLineScaleMode(mode:String):void { 127 142 this.lineScaleMode = mode; 128 history.push(['lineStyle', 'scaleMode', mode])143 logWrite(['lineStyle', 'scaleMode', mode]) 129 144 } 130 145 public function setLineCapsStyle(caps:String):void { 131 146 this.lineCapsStyle = caps; 132 history.push(['lineStyle', 'caps', caps])147 logWrite(['lineStyle', 'caps', caps]) 133 148 } 134 149 public function setLineJointStyle(joints:String):void { 135 150 this.lineJointStyle = joints; 136 history.push(['lineStyle', 'joints', joints])151 logWrite(['lineStyle', 'joints', joints]) 137 152 } 138 153 public function setLineMiterLimit(miterLimit:Number):void { 139 154 this.lineMiterLimit = miterLimit; 140 history.push(['lineStyle', 'miterLimit', miterLimit])155 logWrite(['lineStyle', 'miterLimit', miterLimit]) 141 156 } 142 157 private function setLineStyle():void { … … 165 180 private function moveTo(x:Number, y:Number):void { 166 181 lineShape.graphics.moveTo(x, y); 167 history.push(['moveTo', x, y]);182 logWrite(['moveTo', x, y]); 168 183 } 169 184 private function lineTo(x:Number, y:Number):void { 170 185 lineShape.graphics.lineTo(x, y); 171 history.push(['lineTo', x, y]);186 logWrite(['lineTo', x, y]); 172 187 } 173 188 public function mouseDown(evt:MouseEvent):void { … … 276 291 additionalSkew.visible = visible; 277 292 } 293 public function playLog():void { 294 createBitmapData(); 295 createBitmap(); 296 log.resetPosition(); 297 var a:Array; 298 while (a = log.read()) { 299 switch (a) { 300 case Logger::ACTION_MOVETO: 301 moveTo(a[1], a[2]); 302 break; 303 case Logger::ACTION_LINETO: 304 lineTo(a[1], a[2]); 305 break; 306 case Logger::ACTION_LINESTYLE: 307 break; 308 } 309 } 310 } 278 311 } 279 312 } as3/gunyarapaint/trunk/gunyarapaint/src/gunyarapaint/Logger.as
r1080 r1081 1 1 package gunyarapaint 2 2 { 3 import flash.errors.EOFError; 3 4 import flash.utils.ByteArray; 5 6 import gunyarapaint.Canvas; 4 7 5 8 public class Logger 6 9 { 7 10 // Arrayだせーな。Objectにしたほうがよかったかな。まあいいや。 8 private var preX:uint = 0, preY:uint = 0;11 private var preX:uint, preY:uint; 9 12 private var preAction:String = ''; 10 13 11 14 private var log:ByteArray = new ByteArray(); 12 private var logPos:uint = 0;13 15 14 public fun tion Logger(width:uint, height:uint, undoBufferSize:uint):void {15 writeUTFBytes('GUNYARA_PAINT:0.0.1:');16 public function Logger(width:uint, height:uint, undoBufferSize:uint):void { 17 log.writeUTFBytes('GUNYARA_PAINT:0.0.1:'); 16 18 writeShort(width); 17 19 writeShort(height); 18 20 writeShort(undoBufferSize); 19 21 } 20 21 const var ACTION_MOVETO = 1;22 const var ACTION_LINETO = 2;23 const var ACTION_LINESTYLE = 3;24 22 25 private function diffEncoder(flag:uint, actionCode:uint, dx:int, dy:int) { 26 if (dx >= -4 && dx <= 3 && dy >= -4 && dy <= 3) { 27 // dxもdyも3bitに収まる場合 28 writeByte(flag | 0x01000000 | (dx << 3) & 0x111000 | dy & 0x111); 29 } else if (dx >= -64 && dx <= 63 && dy >= -64 && dy <= 63) { 30 // dxもdyも7bitに収まる場合 31 writeShort(flag << 8 | (dx << 7) & 0x0011111110000000 | dy & 0x01111111); 32 } else { 33 // それ以外の場合 dx,dyを16bit長で保存 34 writeByte(actionCode); 35 writeShort(dx); 36 writeShort(dy); 37 } 38 } 39 private function diffDecoder(firstByte:uint):Array { 40 if (firstByte & 0x01000000) { 41 return [ 42 (firstByte >> 3) & 0x11 * (firstByte & 0x100000 ? -1 : 0), 43 dy & 0x11 * (firstByte & 0x100 ? -1 : 0) 44 ]; 45 } else if 46 } 23 // ACTIONは6bit長 0x3fまで 24 public const ACTION_MOVETO:uint = 1; 25 public const ACTION_LINETO:uint = 2; 26 public const ACTION_LINESTYLE:uint = 3; 47 27 48 public function Log(info:Array):void { 28 public function write(info:Array):void { 29 var x:uint, y:uint, dx:int, dy:int; 49 30 switch(info[0]) { // 0x00 - 0x3f 50 31 case 'moveTo': 51 var x = info[1]; 52 var y = info[2]; 53 var dx = x - preX; 54 var dy = y - preY; 55 diffEncoder(0x00000000, ACTION_MOVETO, dx, dy); 56 currentX = x; 57 currentY = y; 32 x = info[1]; 33 y = info[2]; 34 dx = x - preX; 35 dy = y - preY; 36 if (dx >= -64 && dx <= 63 && dy >= -64 && dy <= 63) { 37 // dxもdyも7bitに収まる場合 38 writeShort(0x4000 | (dx << 7) & 0x3f80 | dy & 0x7f); 39 } else { 40 writeByte(ACTION_MOVETO); 41 writeShort(dx); 42 writeShort(dy); 43 } 44 preX = x; 45 preY = y; 58 46 break; 59 47 case 'lineTo': 60 var x = info[1]; 61 var y = info[2]; 62 var dx = x - preX; 63 var dy = y - preY; 64 diffEncoder(0x10000000, ACTION_LINETO, dx, dy); 65 currentX = x; 66 currentY = y; 48 x = info[1]; 49 y = info[2]; 50 dx = x - preX; 51 dy = y - preY; 52 if (dx >= -4 && dx <= 3 && dy >= -4 && dy <= 3) { 53 // dxもdyも3bitに収まる場合 54 writeByte(0x8000 | (dx << 3) & 0x38 | dy & 0x7); 55 } else if (dx >= -64 && dx <= 63 && dy >= -64 && dy <= 63) { 56 // dxもdyも7bitに収まる場合 57 writeShort(0xc000 | (dx << 7) & 0x3f80 | dy & 0x7f); 58 } else { 59 // それ以外の場合 dx,dyを16bit長で保存 60 writeByte(ACTION_LINETO); 61 writeShort(dx); 62 writeShort(dy); 63 } 64 preX = x; 65 preY = y; 67 66 break; 68 67 case 'lineStyle': 69 var type = info[1];70 var value = info[2];68 var type:uint = info[1]; 69 var value:uint = info[2]; 71 70 break; 72 71 } … … 85 84 return log.readUnsignedByte(); 86 85 } 87 public function playLog(can:Canvas):void { 88 case 'moveTo': 89 var x = info[1]; 90 var y = info[2]; 91 var dx = x - preX; 92 var dy = y - preY; 93 diffEncoder(0x00000000, ACTION_MOVETO, dx, dy); 94 currentX = x; 95 currentY = y; 96 break; 97 case 'lineTo': 98 var x = info[1]; 99 var y = info[2]; 100 var dx = x - preX; 101 var dy = y - preY; 102 diffEncoder(0x10000000, ACTION_LINETO, dx, dy); 103 currentX = x; 104 currentY = y; 105 break; 106 case 'lineStyle': 107 108 can.setLineThickness() 109 public function setLineThickness(t:uint):void { 110 this.lineThickness = t; 111 history.push(['lineStyle', 'thickness', t]) 112 } 113 public function setLineColor(color:uint):void { 114 this.lineColor = color; 115 history.push(['lineStyle', 'color', color]) 86 private function readShort():uint { 87 return log.readShort(); 88 } 89 public function read():Array { 90 var byte:uint; 91 var x:uint, y:uint; 92 var dx:int, dy:int; 93 var short:uint; 94 try { 95 byte = readByte(); 96 switch (byte) { 97 case ACTION_MOVETO: 98 x = readShort() + preX; 99 y = readShort() + preY; 100 return [ACTION_MOVETO, x, y]; 101 case ACTION_LINETO: 102 x = readShort() + preX; 103 y = readShort() + preY; 104 return [ACTION_LINETO, x, y]; 105 case ACTION_LINESTYLE: 106 return [ACTION_LINESTYLE]; 107 default: 108 if (byte & 0x80) { 109 if (byte & 0x40) { 110 // 7bit lineTo 111 log.position -= 1; 112 short = readShort(); 113 x = int((short >> 7) & 0x3f) * (short & 0x2000 ? -1 : 0) + preX; 114 y = int(short & 0x3f) * (short & 0x40 ? -1 : 0) + preY; 115 } else { 116 // 3bit lineTo 117 x = int((byte >> 3) & 0x3) * (byte & 0x20 ? -1 : 0) + preX; 118 y = int(byte & 0x3) * (byte & 0x4 ? -1 : 0) + preY; 119 } 120 return [ACTION_LINETO, x, y]; 121 } else if (byte & 0x40) { 122 // 7bit moveTo 123 log.position -= 1; 124 short = readShort(); 125 dx = (short >> 7) & 0x3f; 126 if (short & 0x2000) { dx *= -1; } 127 dy = short & 0x3f; 128 if (short & 0x40) { dy *= -1; } 129 return [ACTION_MOVETO, x + dx, y + dy]; 130 } else { 131 // error 132 } 133 } 134 } catch (e:EOFError) { 116 135 } 117 public function setLineAlpha(alpha:Number):void { 118 this.lineAlpha = alpha; 119 history.push(['lineStyle', 'alpha', alpha]) 120 } 121 public function setLineBlendMode(blend:String):void { 122 this.lineBlendMode = blend; 123 history.push(['lineStyle', 'blendMode', blend]) 124 } 125 public function setLineScaleMode(mode:String):void { 126 this.lineScaleMode = mode; 127 history.push(['lineStyle', 'scaleMode', mode]) 128 } 129 public function setLineCapsStyle(caps:String):void { 130 this.lineCapsStyle = caps; 131 history.push(['lineStyle', 'caps', caps]) 132 } 133 public function setLineJointStyle(joints:String):void { 134 this.lineJointStyle = joints; 135 history.push(['lineStyle', 'joints', joints]) 136 } 137 public function setLineMiterLimit(miterLimit:Number):void { 138 this.lineMiterLimit = miterLimit; 139 history.push(['lineStyle', 'miterLimit', miterLimit]) 140 } 136 return null; 137 } 138 public function resetPosition():void { 139 log.position = 0; 140 preX = 0; 141 preY = 0; 141 142 } 142 143 }

