Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 2.55 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. var _camera = new Class({
  3.  
  4.                 /* Construct a new camera */           
  5.         initialize: function( name, pos, scale, rot, size )
  6.         {
  7.                 this.name                                                       = (name == undefined) ? 'camera' : name;
  8.                 this.view                                                       = new view();
  9.                 this.view.pos                                           = (pos == undefined) ? vec2(0, 0) : pos;
  10.                 this.view.rotation                              = (rot == undefined) ? 0.0 : rot;
  11.                 this.view.scale                                 = (scale == undefined) ? vec2(1, 1) : scale;
  12.                 this.view.size                                          = (size == undefined) ? vec2(phoenix.getResolution().x, phoenix.getResolution().y) : size;
  13.                 this.draggable                                  = false;
  14.                 this.dragging                                           = false;
  15.                 this.dragStartPos                               = vec2();
  16.                 this.camDragStartPos            = vec2();
  17.                 this.dragEndPos                                 = vec2();
  18.                 this.uuid                                                               = core.getuuid();
  19.                 this.zoomAmount                         = 1.0;
  20.                 this.zoomSpeed                                  = 0.05;
  21.                 this.zoomable                                   = false;
  22.                 this.dragButton                                 = 'both';
  23.                 this.shaking                                            = false;
  24.                 this.shakedir                                           = 1;
  25.                
  26.                         this.__defineGetter__("pos", function(){ return vec2(this.view.pos); });
  27.                         this.__defineGetter__("scale", function(){ return this.view.scale; });
  28.                         this.__defineGetter__("rotation", function(){ return this.view.rotation; });
  29.                         this.__defineGetter__("size", function(){ return this.view.size; });
  30.  
  31.                         this.__defineSetter__("pos", function(val){     this.view.pos = val; });               
  32.                         this.__defineSetter__("rotation", function(val){        this.view.rotation = val; });          
  33.                         this.__defineSetter__("scale", function(val){   this.view.scale = val; });             
  34.                         this.__defineSetter__("size", function(val){    this.view.size = val; });              
  35.                        
  36.                         events.connect( 'onMouseDown', this.onMouseDown.bind(this));
  37.                         events.connect( 'onMouseUp', this.onMouseUp.bind(this));
  38.                         events.connect( 'onMouseMove', this.onMouseMove.bind(this));
  39.                         events.connect('onMouseWheel', this.onMouseWheel.bind(this));
  40.         },
  41.        
  42.         activate : function() {
  43.        
  44.                 this.view.activate();
  45.                
  46.         },
  47.        
  48.         onMouseWheel : function(e) {
  49.                 if(this.zoomable == true) {
  50.                         this.zoomAmount -= ( e.dir * this.zoomSpeed );
  51.                         this.zoom( this.zoomAmount );
  52.                 }
  53.         },
  54.        
  55.         onMouseDown : function(e) {
  56.                
  57.                 var d = true;
  58.                 if( this.dragButton != 'both') {
  59.                         if(this.dragButton != e.button) {
  60.                                 d = false;
  61.                         }
  62.                 }
  63.                
  64.                 if(this.draggable == true && d == true) {
  65.                         this.dragging = true;
  66.                         this.dragStartPos = vec2(e);
  67.                         this.camDragStartPos = this.pos;
  68.                 }
  69.         },
  70.        
  71.         onMouseMove : function(e) {
  72.                 if(this.draggable == true) {
  73.                         if(this.dragging == true) {
  74.  
  75.                                 var diff = vec2(e).directionTo( this.dragStartPos );
  76.                                 newpos = this.camDragStartPos.subtract( diff );
  77.                                 this.pos = newpos;
  78.                                
  79.                         }
  80.                 }
  81.         },
  82.  
  83.        
  84.  
  85.  
  86. });