package jp.xsrv.lingua { import flash.display.Graphics; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Matrix; import flash.geom.Point; public class ObliqueCoodinate extends Sprite { private static var _graphics:Graphics; //グリッドの幅、高さ public var gridLengthX:int;public var gridLengthY:int;public var gridLengthZ:int; public var _gridXNum:int;public var _gridYNum:int;public var gridZNum:int; //座標の中心部 private var _centerX:Number;private var _centerY:Number; //左右上下 public var left:Number;public var right:Number;public var top:Number;public var bottom:Number; //格子の方向ベクトル public var _vecter_x:Point;public var _vecter_y:Point;public var _vecter_z:Point; //変換行列 public var trans:Matrix;public var reverse:Matrix; //回転 public var rotationZ:int; //角度→ラジアン変換定数 public static const rg:Number = Math.PI/180; //フォーカス領域 public var _focusRect:Shape; private var selectedX:int;private var selectedY:int; public function ObliqueCoodinate( gridXNum:uint, gridYNum:uint, vactorX:Point,vactorY:Point, centerX:Number,centerY:Number ) { _graphics = this.graphics; this._centerX = centerX;this._centerY = centerY;//中心点の設定 this._gridXNum = gridXNum;this._gridYNum = gridYNum;//グリッドの数の設定 this._vecter_x = vactorX;this._vecter_y = vactorY;//方向ベクトルの設定 gridLengthX = (_vecter_x.x + _vecter_y.x); gridLengthY = (_vecter_x.y + _vecter_y.y); updatePosition(); this._focusRect = new Shape; addChild(this._focusRect); this.addEventListener(MouseEvent.MOUSE_MOVE, move); this.trans = new Matrix(vactorX.x,vactorX.y,vactorY.x,vactorY.y); this.reverse = trans.clone(); reverse.invert(); paint(); } public function move(e:MouseEvent):void{ //(left,bottom)を(0,0)とする var _x:Number = stage.mouseX - left; var _y:Number = stage.mouseY - bottom; //直行座標に変換 var tp:Point = reverse.deltaTransformPoint(new Point(_x,_y)); var tx:Number = int(tp.x); var ty:Number = int(tp.y); if(_gridXNum <= tx || _gridYNum <= ty){ return; } this.selectedX = tx; this.selectedY = ty; //斜交座標に直す var _p:Point = trans.deltaTransformPoint(new Point(tx,ty)); _focusRect.x = left+_p.x-this.x; _focusRect.y = bottom+_p.y-this.y; } /* * 現在フォーカスされているグリッドのインデックスを返す */ public function getFocusedIndex():Point{ return new Point(selectedX,selectedY); } /* * フォーカス用Shapeのグラフィックスを返す */ public function focusRectGraphics():Graphics{ return this._focusRect.graphics; } /* * 描画 */ public function paint():void{ _graphics.clear(); //ベース描画 _graphics.moveTo(left,bottom); _graphics.beginFill(0x00ffff,0.3); _graphics.lineTo(left + _vecter_x.x*_gridXNum, bottom); _graphics.lineTo(right,top); _graphics.lineTo(left + _vecter_y.x*_gridYNum,top); _graphics.lineTo(left,bottom); _graphics.lineStyle(2,0xff00ff); var end:Point = getPointByIndex(0,_gridYNum); end.x += left; end.y += bottom; for(var i:int=0; i < _gridXNum+1; i++){ _graphics.moveTo(left+i*_vecter_x.x, bottom); _graphics.lineTo(end.x+i*_vecter_x.x, end.y); } end = getPointByIndex(_gridXNum,0); end.x += left; end.y += bottom; for(var j:int=0; j < _gridYNum+1; j++){ _graphics.moveTo(left+j*_vecter_y.x, bottom+j*_vecter_y.y); _graphics.lineTo(end.x+j*_vecter_y.x, end.y+j*_vecter_y.y); } //フォーカス範囲の描画 drawFocusRect(); } /* * グリッドのインデックスから座標(左下)を取得 */ public function getPointByIndex(_x:uint,_y:uint):Point{ var p:Point = new Point; p.x = _x*_vecter_x.x + _y*_vecter_y.x; p.y = _x*_vecter_x.y + _y*_vecter_y.y; return p; } /* * 斜交座標上の点を、スケール1の直行座標の点に変換する */ public function reversePoint(p:Point):Point{ var _r:Point = this.reverse.deltaTransformPoint(p); return _r; } /* * 基準点の更新 */ private function updatePosition():void{ this.left = this._centerX - ((this._gridXNum*0.5) * gridLengthX); this.bottom = this._centerY - (this._gridYNum*0.5) * gridLengthY; this.right = 2*_centerX - this.left; this.top = 2*_centerY - this.bottom; } /* * フォーカス範囲の描画(変えたい場合はサブクラスでオーバーライド) */ public function drawFocusRect():void{ var g:Graphics = this._focusRect.graphics; g.clear(); g.lineStyle(1,0xffffff); g.beginFill(0xff3333); g.moveTo( 0,0 ); g.lineTo( (1*_vecter_y.x), (1*_vecter_y.y) ); g.lineTo( (1*_vecter_x.x + 1*_vecter_y.x), (1*_vecter_x.y + 1*_vecter_y.y) ); g.lineTo( (1*_vecter_x.x), (1*_vecter_x.y) ); g.lineTo( 0,0 ); g.endFill(); } } }