当前位置: 代码迷 >> Web前端 >> Touchlib源码诠释
  详细解决方案

Touchlib源码诠释

热度:419   发布时间:2012-11-05 09:35:12.0
Touchlib源码注释
Touchlib源码注释-----TUIOObject.as TUIOEvent.as TUIOCursor.as
分类: Flex/Flash/AS3 OpenSource 2010-01-05 11:31 1113人阅读 评论(2) 收藏 举报
声明:欢迎任何人和组织转载本blog中文章,但必须标记文章原始链接和作者信息。 
本文链接:http://blog.csdn.net/li_007/archive/2010/01/05/5134270.aspx
开拓进取的小乌龟------->CSDN点滴点点滴滴Blog
还是继续把剩余的整理贴出来。欢迎大家指正。
TUIO.as文件详细注释
TUIOObject.as文件详细注释
TUIOEvent.as文件详细注释
TUIOCursor.as文件详细注释
TUIOObject.as文件
view plainprint?
package com.touchlib { 
 
    import flash.display.Sprite; 
    import flash.display.DisplayObject;  
    //import flash.display.InteractiveObject;    
    //import flash.display.MovieClip;    
    import flash.geom.Point; 
    //import flash.utils.Timer; 
    //import flash.events.MouseEvent; 
    //import flash.events.Event; 
     
    // 
    // 定义一个触摸点类,其实在这个类里面注意一下传进来的参数o 
    // 它是这个类的一个关键,也是整个lib的一个关键,由它来dispatcher 
    // 所有的touch事件,当然也是它来响应touch事件 
    //  
    public class TUIOObject  
    { 
        public var x:Number; 
        public var y:Number; 
         
        public var oldX:Number; 
        public var oldY:Number; 
         
        public var dX:Number; 
        public var dY:Number;                
         
        public var area:Number; 
         
        public var TUIOClass:String;        // cur or Obj..  
        public var sID:int; 
        public var ID:int; 
        public var angle:Number;         
        public var pressure:Number; 
         
        private var isNew:Boolean; 
        public var isAlive:Boolean;      
        public var obj; 
        public var spr:Sprite; 
        public var trlx:Sprite; 
         
        public var color:int; 
         
        var aListeners:Array; 
 
        public function TUIOObject (cls:String, id:int, px:Number, py:Number, dx:Number, dy:Number, sid:int = -1, ang:Number = 0, o = null) 
        { 
            aListeners = new Array(); 
            TUIOClass = cls; 
            ID = id; 
            x = px; 
            y = py; 
            oldX = px; 
            oldY = py; 
            dX = dx; 
            dY = dy; 
            sID = sid; 
            angle = ang; 
            isAlive = true; 
             
            var c = int(Math.random() * 4); 
             
            if(c == 0) 
                color = 0xff0000; 
            else if(c == 1) 
                color = 0x00ffff; 
            else if(c == 2) 
                color = 0x00ff00;                
            else if(c == 3) 
                color = 0x0000ff;        
             
            // 创建调试的光标,当debugMode为true事,绘制一个粉红色的圆 
            spr = new TUIOCursor(ID.toString());             
            spr.x = x; 
            spr.y = y;           
             
            try { 
                obj = o; 
            } catch (e) 
            { 
                obj = null; 
            } 
             
            //trace("Start " + ID + ", " + sID + " (" + int(px) + "," + int(py) + ")"); 
             
 
             
            isNew = true; 
        } 
         
        // 
        // 当第一次创建一个TUIOObject对象时,需要发送TUIO_DOWN和TUIO_OVER事件 
        // 
        public function notifyCreated() 
        { 
            if(obj) 
            { 
                try 
                { 
                    // 将对象舞台坐标转化为显示对象坐标 
                    var localPoint:Point = obj.parent.globalToLocal(new Point(x, y));                
                    //trace("Down : " + localPoint.x + "," + localPoint.y); 
                     
                    // 分别dispatcher TUIO_OVER和TUIO_DOWN事件 
                    obj.dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_OVER, true, false, x, y, localPoint.x, localPoint.y, 0, 0, obj, false,false,false, true, 0, TUIOClass, ID, sID, angle));                                                  
                    obj.dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_DOWN, true, false, x, y, localPoint.x, localPoint.y, 0, 0, obj, false,false,false, true, 0, TUIOClass, ID, sID, angle));                                  
                } catch (e) 
                { 
                        trace("Failed : " + e); 
//                  trace(obj.name); 
                    obj = null; 
                } 
            }            
        } 
         
        // 调用这个函数,说明在TUIOObject数组中存在当前点,也即是当前点移动了。 
        public function setObjOver(o:DisplayObject) 
        { 
            try { 
                 
                if(obj == null) 
                { 
                    obj = o;                 
                    if(obj)  
                    { 
                        // 当已存在TUIOObject对象,而obj对象为空时,需要将当前对象传递进来的显示对象 
                        //(也即是当前点下面最上层显示对象,是事件的发送者,事件也是产生于它),故需要 
                        // 赋值给obj对象,并dispatcher TUIO_OVER事件 
                        var localPoint:Point = obj.parent.globalToLocal(new Point(x, y));                
                        obj.dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_OVER, true, false, x, y, localPoint.x, localPoint.y, 0, 0, obj, false,false,false, true, 0, TUIOClass, ID, sID, angle));                  
                    } 
                }  
                else if(obj != o)  
                { 
                    // 当已存在TUIOObject对象,而obj对象与传递进来的显示对象不一致,说明当前点移动了,并且移出了 
                    // 原来的obj对象的显示范围,故需要对原来对象dispatcher TUIO_OUT事件 
                    var localPoint:Point = obj.parent.globalToLocal(new Point(x, y));                                
                    obj.dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_OUT, true, false, x, y, localPoint.x, localPoint.y, 0, 0, obj, false,false,false, true, 0, TUIOClass, ID, sID, angle)); 
                     
                    if(o) 
                    { 
                        // 当已存在TUIOObject对象,而obj对象与传递进来的显示对象不一致,并且传递进来的也是非空显示对象, 
                        // 说明当前点移动了,并且移出了原来的obj对象的显示范围,故需要对当前对象dispatcher TUIO_OVER事件 
                        localPoint = obj.parent.globalToLocal(new Point(x, y)); 
                        o.dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_OVER, true, false, x, y, localPoint.x, localPoint.y, 0, 0, obj, false,false,false, true, 0, TUIOClass, ID, sID, angle)); 
                    } 
                    obj = o;                                 
                } 
            } catch (e) 
            { 
//              trace("ERROR " + e); 
            } 
        } 
         
        // 将所有需要dispatcher的TUIOObject对象添加到数值中 
        public function addListener(reciever:Object) 
        { 
            aListeners.push(reciever); 
        } 
         
        // 从数组中删除非alive状态的TUIOObject对象 
        public function removeListener(reciever:Object) 
        { 
            for(var i:int = 0; i<aListeners.length; i++) 
            { 
                if(aListeners[i] == reciever) 
                    aListeners.splice(i, 1); 
            } 
        }        
         
        public function kill() 
        { 
            //trace("Die " + ID);            
            var localPoint:Point; 
             
            if(obj && obj.parent) 
            {    
                // 将当前TUIOObject对象中的显示对象清除,并让当前点dispatcher TUIO_OUT和TUIO_UP事件 
                localPoint = obj.parent.globalToLocal(new Point(x, y));              
                obj.dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_OUT, true, false, x, y, localPoint.x, localPoint.y, 0, 0, obj, false,false,false, true, 0, TUIOClass, ID, sID, angle));               
                obj.dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_UP, true, false, x, y, localPoint.x, localPoint.y, 0, 0, obj, false,false,false, true, 0, TUIOClass, ID, sID, angle));                                    
            }            
            obj = null; 
             
            // 为什么要所有都TUIO_UP有点不理解 
            for(var i:int=0; i<aListeners.length; i++) 
            { 
                localPoint = aListeners[i].parent.globalToLocal(new Point(x, y));                
                aListeners[i].dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_UP, true, false, x, y, localPoint.x, localPoint.y, 0, 0, aListeners[i], false,false,false, true, 0, TUIOClass, ID, sID, angle));                                
            } 
        } 
         
        // 这个有点不理解 
        public function notifyMoved() 
        { 
            var localPoint:Point; 
            for(var i:int=0; i<aListeners.length; i++) 
            { 
                //trace("Notify moved"); 
                localPoint = aListeners[i].parent.globalToLocal(new Point(x, y));                
                aListeners[i].dispatchEvent(new TUIOEvent(TUIOEvent.TUIO_MOVE, true, false, x, y, localPoint.x, localPoint.y, 0, 0, aListeners[i], false,false,false, true, 0, TUIOClass, ID, sID, angle));                              
            }            
        } 
    } 



TUIOEvent.as文件
view plainprint?
package com.touchlib { 
import flash.display.DisplayObject; 
import flash.display.DisplayObjectContainer; 
import flash.events.Event;   
     
    public class TUIOEvent extends Event 
    { 
        public var TUIOClass:String; 
        public var sID:int; 
        public var ID:int; 
        public var angle:Number; 
        public var stageX:Number; 
        public var stageY:Number; 
        public var localX:Number; 
        public var localY:Number; 
        public var oldX:Number; 
        public var oldY:Number; 
        public var buttonDown:Boolean; 
        public var relatedObject:DisplayObject; 
 
        public static const TUIO_MOVE:String = "com.touchlib.TUIOEvent.TUIO_MOVE"; 
        public static const TUIO_DOWN:String = "com.touchlib.TUIOEvent.TUIO_DOWN";               
        public static const TUIO_CLICK:String = "com.nui.TUIOEvent.TUIO_CLICK"; //double tap-click 
        public static const TUIO_UP:String = "com.touchlib.TUIOEvent.TUIO_UP";               
        public static const TUIO_OVER:String = "com.touchlib.TUIOEvent.TUIO_OVER";                       
        public static const TUIO_OUT:String = "com.touchlib.TUIOEvent.TUIO_OUT";         
         
        // 根据Actionscript 3的继承机制,编写自己的事件类 
        public function TUIOEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, stageX:Number = 0, stageY:Number = 0, localX:Number = 0, localY:Number = 0, oldX:Number = 0, oldY:Number = 0, relatedObject:DisplayObject = null, ctrlKey:Boolean = false, altKey:Boolean = false, shiftKey:Boolean = false, buttonDown:Boolean = false, delta:int = 0, TUIOClass:String = "2Dcur", ID:int = -1, sID:int = -1, angle:Number = 0.0) 
        { 
            this.TUIOClass = TUIOClass; 
            this.sID = sID; 
            this.ID = ID; 
            this.angle = angle; 
            this.stageX = stageX; 
            this.stageY = stageY; 
            this.localX = localX; 
            this.localY = localY;            
            this.oldX = oldX; 
            this.oldY = oldY; 
            this.buttonDown = buttonDown; 
            this.relatedObject = relatedObject; 
             
            super(type, bubbles, cancelable); 
             
//          super(type, bubbles, cancelable, localX, localY, relatedObject, ctrlKey, altKey, shiftKey, buttonDown, delta); 
             
        } 
 
    } 


TUIOCursor.as文件
view plainprint?
package com.touchlib 

    import flash.display.Sprite; 
    import flash.text.TextFormat; 
    import flash.text.TextField; 
    import flash.text.TextFieldAutoSize; 
 
    // 此类的作用是在调试状态下,绘制一个粉红色的圆来表示当前触摸点 
    public class TUIOCursor extends Sprite 
    { 
        // 声明一个动态文本用来显示当前触摸点的id 
        private var DEBUG_TEXT:TextField;    
         
        public function TUIOCursor(debugText:String) 
        { 
            super(); 
             
            if (TUIO.debugMode) 
            { 
                // Draw us the lil' circle 
                graphics.beginFill(0xFF00FF , 1);    
                graphics.drawCircle(-5 , -5, 10); 
                graphics.endFill(); 
                 
                // Add textfield for debugging, shows the cursor id 
                if (debugText != '' || debugText != null) 
                { 
                    var format:TextFormat = new TextFormat(); 
                    DEBUG_TEXT = new TextField(); 
                    format.font = 'Verdana'; 
                    format.color = 0xFFFFFF; 
                    format.size = 10; 
                    DEBUG_TEXT.defaultTextFormat = format; 
                    DEBUG_TEXT.autoSize = TextFieldAutoSize.LEFT; 
                    DEBUG_TEXT.background = true;    
                    DEBUG_TEXT.backgroundColor = 0x000000;   
                    DEBUG_TEXT.border = true;    
                    DEBUG_TEXT.text = ''; 
                    DEBUG_TEXT.appendText(' '+debugText+'  '); 
                     
                    DEBUG_TEXT.x = 8; 
                    DEBUG_TEXT.y = -13;   
                     
                    addChild(DEBUG_TEXT); 
                } 
                else 
                { 
                // hide cursor 
                } 
            } 
        } 
    } 
  相关解决方案