チェンジセット 1081

差分発生行の前後
無視リスト:
コミット日時:
2008/08/27 18:15:19 (5 年前)
コミッタ:
tasuku
ログメッセージ:

r32@poppop (orig r31): tasuku | 2008-03-13 15:08:17 +0900
logger途中まで完成。これからリファクタリング。

ファイル:

凡例:

変更無し
追加
削除
更新
コピー
移動
  • as3/gunyarapaint/trunk/gunyarapaint/src/gunyarapaint.as

    r1071 r1081  
    22 
    33import gunyarapaint.Canvas; 
    4 import gunyarapaint.Com; 
    54 
    65import mx.events.ColorPickerEvent; 
     
    98import mx.events.NumericStepperEvent; 
    109import mx.events.SliderEvent; 
    11 import mx.managers.PopUpManager; 
    1210 
    1311private var gpCanvas:gunyarapaint.Canvas; 
     
    1816 
    1917public function init():void {   
    20   gpCanvas = new gunyarapaint.Canvas(CANVAS_WIDTH, CANVAS_HEIGHT, 12); 
     18  gpCanvas = new gunyarapaint.Canvas(CANVAS_WIDTH, CANVAS_HEIGHT, 12, true); 
    2119  // gpCanvas.scrollRect = new Rectangle(0, 0, 300, 300); 
    2220  canvas.addChild(gpCanvas); 
     
    6866   
    6967  postOekakiButton.addEventListener(FlexEvent.BUTTON_DOWN, postOekakiButtonHandler); 
     68   
     69  // ログ再生 
     70  playLogButton.addEventListener(FlexEvent.BUTTON_DOWN, playLogButtonHandler); 
    7071   
    7172  // いろんな初期値を保存… 
     
    164165} 
    165166 
    166  
     167private function playLogButtonHandler(evt:Event):void { 
     168  gpCanvas.playLog(); 
     169
  • as3/gunyarapaint/trunk/gunyarapaint/src/gunyarapaint.mxml

    r1072 r1081  
    3434  <mx:Canvas id="canvas" x="114" width="400" height="300" y="172"></mx:Canvas> 
    3535  <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="ログ再生"/> 
    3637</mx:Application> 
  • as3/gunyarapaint/trunk/gunyarapaint/src/gunyarapaint/Canvas.as

    r1080 r1081  
    3232    private var additionalNumber:uint = 4; // 補助線の分割数 
    3333     
    34     private var history:Array; // 描画コマンド履歴 
     34    private var log:Logger; // 描画コマンド履歴 
    3535 
    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) 
    3739        { 
     40          canWidth = width; 
     41          canHeight = height; 
    3842          // ビットマップ 
    39           data = new BitmapData(width, height); 
     43          createBitmapData(); 
    4044      createBitmap(); 
    4145       
     
    6468      this.undoBufferSize = undoBufferSize; 
    6569       
    66       history = new Array(); 
     70      if (saveLog){ 
     71        log = new Logger(width, height, undoBufferSize); 
     72      } 
    6773             
    6874      super(); 
    6975        } 
    7076         
     77    private function createBitmapData():void { 
     78      data = new BitmapData(canWidth, canHeight);       
     79    } 
    7180        private function createBitmap():void { 
    7281          if (bitmap) { 
     
    108117        private var lineMiterLimit:Number = 3; 
    109118         
     119        private function logWrite(info:Array):void { 
     120          if (log) { 
     121            log.write(info); 
     122          } 
     123        } 
     124         
    110125        public function setLineThickness(t:uint):void { 
    111126          this.lineThickness = t; 
    112           history.push(['lineStyle', 'thickness', t]) 
     127          logWrite(['lineStyle', 'thickness', t]) 
    113128        } 
    114129    public function setLineColor(color:uint):void { 
    115130      this.lineColor = color; 
    116           history.push(['lineStyle', 'color', color]) 
     131          logWrite(['lineStyle', 'color', color]) 
    117132    } 
    118133        public function setLineAlpha(alpha:Number):void { 
    119134          this.lineAlpha = alpha; 
    120           history.push(['lineStyle', 'alpha', alpha]) 
     135          logWrite(['lineStyle', 'alpha', alpha]) 
    121136        } 
    122137        public function setLineBlendMode(blend:String):void { 
    123138          this.lineBlendMode = blend; 
    124           history.push(['lineStyle', 'blendMode', blend]) 
     139          logWrite(['lineStyle', 'blendMode', blend]) 
    125140        } 
    126141        public function setLineScaleMode(mode:String):void { 
    127142          this.lineScaleMode = mode; 
    128           history.push(['lineStyle', 'scaleMode', mode]) 
     143          logWrite(['lineStyle', 'scaleMode', mode]) 
    129144        } 
    130145        public function setLineCapsStyle(caps:String):void { 
    131146          this.lineCapsStyle = caps; 
    132           history.push(['lineStyle', 'caps', caps]) 
     147          logWrite(['lineStyle', 'caps', caps]) 
    133148        } 
    134149        public function setLineJointStyle(joints:String):void { 
    135150          this.lineJointStyle = joints; 
    136           history.push(['lineStyle', 'joints', joints]) 
     151          logWrite(['lineStyle', 'joints', joints]) 
    137152        } 
    138153        public function setLineMiterLimit(miterLimit:Number):void { 
    139154          this.lineMiterLimit = miterLimit; 
    140           history.push(['lineStyle', 'miterLimit', miterLimit]) 
     155          logWrite(['lineStyle', 'miterLimit', miterLimit]) 
    141156        } 
    142157        private function setLineStyle():void { 
     
    165180        private function moveTo(x:Number, y:Number):void { 
    166181      lineShape.graphics.moveTo(x, y); 
    167       history.push(['moveTo', x, y]); 
     182      logWrite(['moveTo', x, y]); 
    168183        } 
    169184        private function lineTo(x:Number, y:Number):void { 
    170185      lineShape.graphics.lineTo(x, y); 
    171       history.push(['lineTo', x, y]); 
     186      logWrite(['lineTo', x, y]); 
    172187        } 
    173188        public function mouseDown(evt:MouseEvent):void { 
     
    276291          additionalSkew.visible = visible; 
    277292        } 
     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        } 
    278311  } 
    279312} 
  • as3/gunyarapaint/trunk/gunyarapaint/src/gunyarapaint/Logger.as

    r1080 r1081  
    11package gunyarapaint 
    22{ 
     3  import flash.errors.EOFError; 
    34  import flash.utils.ByteArray; 
     5   
     6  import gunyarapaint.Canvas; 
    47   
    58  public class Logger 
    69  { 
    710    // Arrayだせーな。Objectにしたほうがよかったかな。まあいいや。 
    8     private var preX:uint = 0, preY:uint = 0
     11    private var preX:uint, preY:uint
    912    private var preAction:String = ''; 
    1013     
    1114    private var log:ByteArray = new ByteArray(); 
    12     private var logPos:uint = 0; 
    1315 
    14     public funtion 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:'); 
    1618      writeShort(width); 
    1719      writeShort(height); 
    1820      writeShort(undoBufferSize); 
    1921    } 
    20      
    21     const var ACTION_MOVETO = 1; 
    22     const var ACTION_LINETO = 2; 
    23     const var ACTION_LINESTYLE = 3; 
    2422 
    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; 
    4727 
    48     public function Log(info:Array):void { 
     28    public function write(info:Array):void { 
     29      var x:uint, y:uint, dx:int, dy:int; 
    4930      switch(info[0]) { // 0x00 - 0x3f 
    5031      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; 
    5846        break; 
    5947      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; 
    6766        break; 
    6867      case 'lineStyle': 
    69         var type = info[1]; 
    70         var value = info[2]; 
     68        var type:uint = info[1]; 
     69        var value:uint = info[2]; 
    7170        break; 
    7271      } 
     
    8584      return log.readUnsignedByte(); 
    8685    } 
    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) { 
    116135      } 
    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; 
    141142    } 
    142143  }