root/as3/gunyarapaint/branches/gunyarapaint/compat/src/org/libspark/gunyarapaint/ui/v1/Canvas.as

リビジョン 3822, 9.1 kB (コミッタ: hkrn, コミット時期: 3 年 前)

refactor

Line 
1 package org.libspark.gunyarapaint.ui.v1
2 {
3     import com.oysteinwika.ui.SWFMouseWheel;
4    
5     import flash.display.Sprite;
6     import flash.events.Event;
7     import flash.events.IEventDispatcher;
8     import flash.events.MouseEvent;
9     import flash.geom.Rectangle;
10     import flash.system.Capabilities;
11    
12     import mx.controls.Alert;
13     import mx.core.Application;
14     import mx.core.UIComponent;
15     import mx.managers.CursorManager;
16    
17     import org.libspark.gunyarapaint.framework.AuxLineView;
18     import org.libspark.gunyarapaint.framework.AuxPixelView;
19     import org.libspark.gunyarapaint.framework.LayerBitmapCollection;
20     import org.libspark.gunyarapaint.framework.TransparentBitmap;
21     import org.libspark.gunyarapaint.framework.modules.CanvasModule;
22     import org.libspark.gunyarapaint.framework.modules.DropperModule;
23     import org.libspark.gunyarapaint.framework.modules.ICanvasModule;
24     import org.libspark.gunyarapaint.framework.ui.IApplication;
25     import org.libspark.gunyarapaint.ui.events.CanvasModuleEvent;
26    
27     internal class Canvas extends UIComponent
28     {
29         public function Canvas(app:IApplication)
30         {
31             var rect:Rectangle = new Rectangle(0, 0, app.canvasWidth, app.canvasHeight);
32             var transparent:TransparentBitmap = new TransparentBitmap(rect);
33             m_auxLine = new AuxLineView(rect);
34             m_auxPixel = new AuxPixelView(rect);
35             m_auxLine.visible = true;
36             m_auxPixel.visible = false;
37             m_rect = rect;
38             // 透明画像、キャンバス本体、補助線(直線および斜線)の順番に追加される
39             addChild(transparent);
40             app.layers.setView(this);
41             app.addEventListener(CanvasModuleEvent.BEFORE_CHANGE, onModuleChangeBefore);
42             app.addEventListener(CanvasModuleEvent.AFTER_CHANGE, onModuleChangeAfter);
43             addChild(m_auxLine);
44             addChild(m_auxPixel);
45             addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
46             addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove2);
47             // Capabilities.version で OS を判断するのは適切ではないが、
48             // 少なくとも MacOSX ではマウスホイールを正しく感知することが出来無いので対処療法として
49             if (Capabilities.version.indexOf("MAC") >= 0) {
50                 SWFMouseWheel.SWFMouseWheelHandler = function(delta:Number):void
51                 {
52                     var module:MovableCanvasModule = app.canvasModule as MovableCanvasModule;
53                     if (module != null)
54                         module.wheel(0, 0, delta * 3)
55                 };
56                 SWFMouseWheel._init();
57             }
58             super();
59         }
60        
61         public function updateAuxViews():void
62         {
63             m_auxLine.update();
64             m_auxPixel.update();
65         }
66        
67         public function get auxBoxVisible():Boolean
68         {
69             return m_auxLine.boxVisible;
70         }
71        
72         public function get auxSkewVisible():Boolean
73         {
74             return m_auxLine.skewVisible;
75         }
76        
77         public function get auxDivideCount():uint
78         {
79             return m_auxLine.divideCount;
80         }
81        
82         public function get auxLineAlpha():Number
83         {
84             return m_auxLine.lineAlpha;
85         }
86        
87         public function get auxLineColor():uint
88         {
89             return m_auxLine.lineColor;
90         }
91        
92         public function get enableAuxPixel():Boolean
93         {
94             return m_auxPixel.visible;
95         }
96        
97         public function set auxBoxVisible(value:Boolean):void
98         {
99             m_auxLine.boxVisible = m_auxPixel.boxVisible = value;
100         }
101        
102         public function set auxSkewVisible(value:Boolean):void
103         {
104             m_auxLine.skewVisible = m_auxPixel.skewVisible = value;
105         }
106        
107         public function set auxDivideCount(value:uint):void
108         {
109             m_auxLine.divideCount = m_auxPixel.divideCount = value;
110         }
111        
112         public function set auxLineAlpha(value:Number):void
113         {
114             m_auxLine.lineAlpha = m_auxPixel.lineAlpha = value;
115         }
116        
117         public function set auxLineColor(value:uint):void
118         {
119             m_auxLine.lineColor = m_auxPixel.lineColor = value;
120         }
121        
122         public function set enableAuxPixel(value:Boolean):void
123         {
124             m_auxLine.visible = value ? false : true;
125             m_auxPixel.visible = value ? true : false;
126         }
127        
128         public function set enablePixelInfo(value:Boolean):void
129         {
130             removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove2);
131             if (value)
132                 addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove2);
133         }
134        
135         private function onModuleChangeBefore(event:CanvasModuleEvent):void
136         {
137             removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
138         }
139        
140         private function onModuleChangeAfter(event:CanvasModuleEvent):void
141         {
142             var application:Object = Application.application;
143             var app:IApplication = IApplication(application);
144             var module:ICanvasModule = app.canvasModule;
145             if (module is MovableCanvasModule)
146                 addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
147             CursorManager.removeCursor(CursorManager.currentCursorID);
148             switch (module.name) {
149                 case DropperModule.DROPPER:
150                     CursorManager.setCursor(application.dropperIcon);
151                     break;
152                 case MovableCanvasModule.MOVABLE_CANVAS:
153                     CursorManager.setCursor(application.handOpenIcon);
154                     break;
155             }
156         }
157        
158         private function onMouseDown(event:MouseEvent):void
159         {
160             var app:gunyarapaint = gunyarapaint(Application.application);
161             var layers:LayerBitmapCollection = app.layers;
162             try {
163                 var canvasModule:ICanvasModule = app.canvasModule;
164                 var x:Number = event.localX;
165                 var y:Number = event.localY;
166                 if (m_rect.contains(x, y)) {
167                     // 例えば非表示あるいはロック状態のあるレイヤーに対して描写を行うと例外が送出されるので、
168                     // 必ず try/catch で囲む必要がある
169                     canvasModule.start(x, y);
170                     layers.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
171                     layers.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
172                     layers.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
173                 }
174             } catch (e:Error) {
175                 removeMouseEvents(layers);
176                 Alert.show(e.message, app.canvasModuleName);
177             }
178         }
179        
180         private function onMouseMove(event:MouseEvent):void
181         {
182             var x:Number = event.localX;
183             var y:Number = event.localY;
184             if (m_rect.contains(x, y)) {
185                 var app:IApplication = IApplication(Application.application);
186                 app.canvasModule.move(x, y);
187             }
188         }
189        
190         private function onMouseMove2(event:MouseEvent):void
191         {
192             var application:Object = Application.application;
193             var app:IApplication = IApplication(application);
194             var x:Number = event.localX;
195             var y:Number = event.localY;
196             var color:uint = app.canvasModule.getPixel32(x, y);
197             var status:String = _(
198                 "Coordinates:(%s, %s) Opacity:%s Color:(%s,%s,%s)",
199                 x, y,
200                 Number(((color >> 24) & 0xff) / 255).toPrecision(2),
201                 ((color >> 16) & 0xff),
202                 ((color >> 8) & 0xff),
203                 ((color >> 0) & 0xff)
204             );
205             application.canvasController.statusText = status;
206         }
207        
208         private function onMouseUp(event:MouseEvent):void
209         {
210             var app:IApplication = IApplication(Application.application);
211             removeMouseEvents(app.layers);
212             app.canvasModule.stop(event.localX, event.localY);
213         }
214        
215         private function onMouseOut(event:MouseEvent):void
216         {
217             var app:IApplication = IApplication(Application.application);
218             removeMouseEvents(app.layers);
219             app.canvasModule.interrupt(event.localX, event.localY);
220         }
221        
222         private function onMouseWheel(event:MouseEvent):void
223         {
224             var module:MovableCanvasModule = MovableCanvasModule(Application.application.module);
225             module.wheel(event.localX, event.localY, event.delta);
226         }
227        
228         private function removeMouseEvents(layers:LayerBitmapCollection):void
229         {
230             layers.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
231             layers.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
232             layers.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
233         }
234        
235         private var m_auxLine:AuxLineView;
236         private var m_auxPixel:AuxPixelView;
237         private var m_rect:Rectangle;
238     }
239 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。