1 // Fix: bad naming. CoverNode is meaningless
  2 goog.provide('lime.CoverNode');
  3 
  4 
  5 goog.require('goog.dom');
  6 goog.require('goog.dom.classes');
  7 goog.require('goog.math.Box');
  8 goog.require('goog.style');
  9 goog.require('lime.Node');
 10 
 11 
 12 /**
 13  * Object that covers whole viewport area and lives outside the scene.
 14  * @constructor
 15  * @extends lime.Node
 16  */
 17 lime.CoverNode = function() {
 18     lime.Node.call(this);
 19 
 20     // Switch to canvas element
 21     var oldElement = this.baseElement;
 22     this.baseElement = document.createElement('canvas');
 23     goog.dom.replaceNode(this.baseElement, oldElement);
 24 
 25     /**
 26      * Canvas drawing context
 27      * @type {Object}
 28      */
 29     this.context = this.baseElement.getContext('2d');
 30 
 31     // add extra className
 32     goog.dom.classes.add(this.baseElement, goog.getCssName('lime-cover'));
 33 
 34 };
 35 goog.inherits(lime.CoverNode, lime.Node);
 36 
 37 
 38 /**
 39  * Invalidate object size & position
 40  */
 41 lime.CoverNode.prototype.update = function() {
 42     if (!this.director) return;
 43 
 44     var size = goog.style.getSize(this.director.baseElement.parentNode),
 45         style = this.baseElement.style,
 46         dscale = this.director.getScale(),
 47         quality = this.getQuality();
 48 
 49     style['width'] = size.width + 'px';
 50     style['height'] = size.height + 'px';
 51 
 52     this.baseElement.width = (size.width / dscale.x) * quality;
 53     this.baseElement.height = (size.height / dscale.y) * quality;
 54 
 55     // some action has set update area limits?
 56     if (this.updateRect_) {
 57         this.setNeedsRedrawInRect(this.updateRect_);
 58     }
 59     else {
 60         this.setNeedsRedraw();
 61     }
 62 };
 63 
 64 /**
 65  * Tell CoverNode that it needs to redraw its contents if whole viewport
 66  */
 67 lime.CoverNode.prototype.setNeedsRedraw = function() {
 68 
 69     var box = new goog.math.Box(
 70         0, this.baseElement.width / this.getQuality(),
 71         this.baseElement.height / this.getQuality(), 0
 72     );
 73     this.setNeedsRedrawInRect(this.director.getBounds(box));
 74 
 75 };
 76 
 77 /**
 78  * Tell CoverNode that it needs to redraw its contents if
 79  * specific Box area
 80  * @param {goog.math.Box} box Box.
 81  */
 82 lime.CoverNode.prototype.setNeedsRedrawInRect = function(box) {
 83     var dir = this.director,
 84         quality = this.getQuality(),
 85         scale = dir.getScale(),
 86         position = dir.getPosition();
 87 
 88     this.context.save();
 89     this.context.scale(quality, quality);
 90     this.context.translate(position.x / scale.x, position.y / scale.y);
 91 
 92     // call abstract method
 93     this.drawInRect(box);
 94 
 95     this.context.restore();
 96 };
 97 
 98 
 99 /**
100  * Draw the contents inside given area
101  * @param {goog.math.Box} box Draw area.
102  */
103 lime.CoverNode.prototype.drawInRect = goog.abstractMethod;
104 
105