root/as3/gunyarapaint/trunk/gunyarapaint/src/org/libspark/gunyarapaint/controls/GPCanvasWindowControl.as

リビジョン 1458, 7.0 kB (コミッタ: tasuku, コミット時期: 5 年 前)

post layer_infos and palettes with json

Line 
1 package org.libspark.gunyarapaint.controls
2 {
3   import flash.display.BitmapData;
4   import flash.geom.Point;
5  
6   import org.libspark.gunyarapaint.entities.GPLogger;
7   import org.libspark.gunyarapaint.entities.GPPen;
8   import org.libspark.gunyarapaint.utils.ComponentResizer;
9  
10   import mx.containers.HBox;
11   import mx.containers.TitleWindow;
12   import mx.containers.VBox;
13   import mx.controls.HScrollBar;
14   import mx.controls.VScrollBar;
15   import mx.controls.scrollClasses.ScrollBar;
16   import mx.core.Container;
17   import mx.events.ResizeEvent;
18   import mx.events.ScrollEvent;
19
20   public class GPCanvasWindowControl extends TitleWindow
21   {
22     private var _canvas:GPCanvas;
23     private var _container:Container;
24    
25     private var _hbox:HBox; // canvasと縦スクロールバー
26     private var _vbox:VBox; // _hboxと横スクロールバー
27    
28     private var hScrollBar:HScrollBar; // 横スクロールバー
29     private var vScrollBar:VScrollBar; // 縦スクロールバー
30     private var canvasX:Number, canvasY:Number; // キャンバスのスクロール位置
31     private var _canvasScale:Number; // キャンバスの倍率
32
33     public function GPCanvasWindowControl() {
34       super();
35
36       this.setStyle('backgroundColor', 0x000000);
37       // this.setStyle('backgroundAlpha', 0);
38       this.horizontalScrollPolicy = 'off';
39       this.verticalScrollPolicy = 'off';
40
41       this.addEventListener(ResizeEvent.RESIZE, resizeHandler);
42
43       ComponentResizer.addResize(this, new Point(100, 100));
44     }
45    
46     private function resizeHandler(evt:ResizeEvent):void {
47       // スクロールバー
48
49       /*
50       if (_canvas) {
51         _canvas.changeCanvasWindowSize(this.width, this.height);
52       }
53       */
54     }
55    
56     private var scrollDragStartPoint:Point;
57    
58     public function createCanvas(width:uint, height:uint, undoBufferSize:uint, logger:GPLogger, baseData:BitmapData, penDetailWindow:PenDetailWindowControl):GPCanvas {
59       if (!_canvas) {
60         _canvas = new GPCanvas(width, height, undoBufferSize, logger, baseData, penDetailWindow);
61         //_canvas.setStyle('borderColor', 0x000000);
62         //_canvas.setStyle('borderStyle', 'solid');
63         //_canvas.setStyle('borderThickness', 1);       
64
65         _vbox = new VBox();
66         _vbox.percentWidth = 100;
67         _vbox.percentHeight = 100;
68         _vbox.setStyle('verticalGap', 0);
69         _hbox = new HBox();
70         _hbox.percentWidth = 100;
71         _hbox.percentHeight = 100;
72         _hbox.setStyle('horizontalGap', 0);
73         _vbox.addChild(_hbox);
74         this.addChild(_vbox);
75        
76         this.validateNow(); // percentWidth/Height -> width/heightに更新
77
78         _container = new Container();
79         _container.setStyle('borderStyle', 'none');
80         _container.horizontalScrollPolicy = 'off';
81         _container.verticalScrollPolicy = 'off';
82
83         hScrollBar = new HScrollBar();
84         vScrollBar = new VScrollBar();
85         hScrollBar.height = ScrollBar.THICKNESS;
86         vScrollBar.width = ScrollBar.THICKNESS;
87         hScrollBar.move(0, height);
88         vScrollBar.move(width, 0);
89         hScrollBar.addEventListener(ScrollEvent.SCROLL, hScrollHandler);
90         vScrollBar.addEventListener(ScrollEvent.SCROLL, vScrollHandler);
91         hScrollBar.lineScrollSize = 1;
92         vScrollBar.lineScrollSize = 1;
93
94         _container.addChild(_canvas);
95         _hbox.addChild(_container);
96         _hbox.addChild(vScrollBar);
97         _vbox.addChild(hScrollBar);
98
99         _container.width = _container.maxWidth = _hbox.width - vScrollBar.width;
100         _container.height = _container.maxHeight = _vbox.height - hScrollBar.height;
101         hScrollBar.width = _container.width;
102         vScrollBar.height = _container.height;
103
104         _canvas.canvasWindowControl = this;
105
106         canvasX = 0; canvasY = 0; _canvasScale = 1;
107         moveCanvas();
108       }
109       return _canvas;
110     }
111
112     private function moveCanvas():void {
113       var maxX:Number = Math.floor(_canvas.canvasWidth - (_container.width / _canvasScale));
114       var maxY:Number = Math.floor(_canvas.canvasHeight - (_container.height / _canvasScale));
115       canvasX = Math.floor(canvasX);
116       canvasY = Math.floor(canvasY);
117       if (maxX <= 0) maxX = 0;
118       if (maxY <= 0) maxY = 0;
119       if (canvasX < 0) { canvasX = 0; }
120       if (canvasY < 0) { canvasY = 0; }
121       if (canvasX > maxX) { canvasX = maxX; }
122       if (canvasY > maxY) { canvasY = maxY; }
123       hScrollBar.scrollPosition = canvasX;
124       vScrollBar.scrollPosition = canvasY;
125       hScrollBar.setScrollProperties(_container.width, 0, maxX);
126       vScrollBar.setScrollProperties(_container.height, 0, maxY);
127       _canvas.move(-canvasX * _canvasScale, -canvasY * _canvasScale);
128     }
129    
130     private function hScrollHandler(evt:ScrollEvent):void {
131       canvasX = hScrollBar.scrollPosition;
132       moveCanvas();
133     }
134
135     private function vScrollHandler(evt:ScrollEvent):void {
136       canvasY = vScrollBar.scrollPosition;
137       moveCanvas();
138     }
139     public function scrollCanvas(x:Number, y:Number):void {
140       canvasX = x; canvasY = y;
141       moveCanvas();
142     }
143     /*
144     public function scrollCanvas(dx:Number, dy:Number):void {
145       canvasX += dx; canvasY += dy;
146       moveCanvas();
147     }
148     */
149    
150     public function get canvasScrollPosition():Point {
151       return new Point(canvasX, canvasY);
152     }
153    
154     public function get canvasScale():Number {
155       return _canvasScale;
156     }
157    
158     public function get canvas():GPCanvas {
159       return _canvas;
160     }
161        
162     public function set pen(pen:GPPen):void {
163       _canvas.pen = pen;
164       pen.canvasWindow = this;
165     }
166
167     public function set statusText(value:String):void {
168       this.status = value;
169     }
170
171     // NOTE: つかってない
172     public function updateCanvas():void {
173       var deg:Number = _canvas.rotation;
174       var rad:Number = deg * Math.PI / 180;
175       var scale:Number = _container.scaleX;
176      
177       // TODO: dasai
178       if (deg >= 0) {
179         if (deg <= 90) {
180           _container.x = (Math.sin(rad) * _canvas.canvasHeight * scale);
181           _container.y = 0;
182         } else {
183           _container.x = _canvas.canvasHeight * scale * Math.sin(rad) - Math.cos(rad) * _canvas.canvasWidth * scale;
184           _container.y = - Math.cos(rad) * _canvas.canvasHeight * scale;
185         }
186       } else {
187         if (deg >= -90) {
188           _container.x = 0;
189           _container.y = - Math.sin(rad) * _canvas.canvasWidth * scale;
190         } else {
191           _container.x = - Math.cos(rad) * _canvas.canvasWidth * scale;
192           _container.y = - Math.sin(rad) * _canvas.canvasWidth * scale - Math.cos(rad) * _canvas.canvasHeight * scale;
193         }
194       }
195     }
196    
197     public function zoomCanvas(m:Number):void {
198       _canvasScale = m;
199       _canvas.scaleX = _canvasScale;
200       _canvas.scaleY = _canvasScale;
201       moveCanvas();     
202     }
203    
204     public function rotateCanvas(deg:int):void {
205       this.rotation = deg;
206       updateCanvas();
207     }
208  }
209 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。